]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag.php
Fix problems in joinAdd with xampp
[quix0rs-gnu-social.git] / classes / Profile_tag.php
1 <?php
2 /**
3  * Table Definition for profile_tag
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Profile_tag extends Memcached_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'profile_tag';                     // table name
13     public $tagger;                          // int(4)  primary_key not_null
14     public $tagged;                          // int(4)  primary_key not_null
15     public $tag;                             // varchar(64)  primary_key not_null
16     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
17
18     /* Static get */
19     function staticGet($k,$v=null)
20     { return Memcached_DataObject::staticGet('Profile_tag',$k,$v); }
21
22     /* the code above is auto generated do not remove the tag below */
23     ###END_AUTOCODE
24
25     function pkeyGet($kv) {
26         return Memcached_DataObject::pkeyGet('Profile_tag', $kv);
27     }
28
29     function links()
30     {
31         return array('tagger,tag' => 'profile_list:tagger,tag');
32     }
33
34     function getMeta()
35     {
36         return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
37     }
38
39     static function getTags($tagger, $tagged, $auth_user=null) {
40
41         $profile_list = new Profile_list();
42         $include_priv = 1;
43
44         if (!($auth_user instanceof User ||
45             $auth_user instanceof Profile) ||
46             ($auth_user->id !== $tagger)) {
47
48             $profile_list->private = false;
49             $include_priv = 0;
50         }
51
52         $key = sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $include_priv);
53         $tags = Profile_list::getCached($key);
54         if ($tags !== false) {
55             return $tags;
56         }
57
58         $qry = 'select profile_list.* from profile_list left join '.
59                'profile_tag on (profile_list.tag = profile_tag.tag and '.
60                'profile_list.tagger = profile_tag.tagger) where '.
61                'profile_tag.tagger = %d and profile_tag.tagged = %d ';
62         $qry = sprintf($qry, $tagger, $tagged);
63
64         if (!$include_priv) {
65             $qry .= 'profile_list.private = 0';
66         }
67
68         $profile_list->query($qry);
69
70         Profile_list::setCache($key, $profile_list);
71
72         return $profile_list;
73     }
74
75     static function getTagsArray($tagger, $tagged, $auth_user_id=null)
76     {
77         $ptag = new Profile_tag();
78         $ptag->tagger = $tagger;
79         $ptag->tagged = $tagged;
80
81         if ($tagger != $auth_user_id) {
82             $list = new Profile_list();
83             $list->private = false;
84             $ptag->joinAdd($list);
85             $ptag->selectAdd();
86             $ptag->selectAdd('profile_tag.tag');
87         }
88
89         $tags = array();
90         $ptag->find();
91         while ($ptag->fetch()) {
92             $tags[] = $ptag->tag;
93         }
94         $ptag->free();
95
96         return $tags;
97     }
98
99     static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
100
101         $newtags = array_unique($newtags);
102         $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
103
104         $ptag = new Profile_tag();
105
106         // Delete stuff that's in old and not in new
107
108         $to_delete = array_diff($oldtags, $newtags);
109
110         // Insert stuff that's in new and not in old
111
112         $to_insert = array_diff($newtags, $oldtags);
113
114         foreach ($to_delete as $deltag) {
115             self::unTag($tagger, $tagged, $deltag);
116         }
117
118         foreach ($to_insert as $instag) {
119             $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
120             self::setTag($tagger, $tagged, $instag, null, $private);
121         }
122         return true;
123     }
124
125     # set a single tag
126     static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
127
128         $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
129                                            'tagged' => $tagged,
130                                            'tag' => $tag));
131
132         # if tag already exists, return it
133         if(!empty($ptag)) {
134             return $ptag;
135         }
136
137         $tagger_profile = Profile::staticGet('id', $tagger);
138         $tagged_profile = Profile::staticGet('id', $tagged);
139
140         if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
141
142             if (!$tagger_profile->canTag($tagged_profile)) {
143                 // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged.
144                 throw new ClientException(_('You cannot tag this user.'));
145                 return false;
146             }
147
148             $tags = new Profile_list();
149             $tags->tagger = $tagger;
150             $count = (int) $tags->count('distinct tag');
151
152             if ($count >= common_config('peopletag', 'maxtags')) {
153                 // TRANS: Client exception thrown trying to set more tags than allowed.
154                 throw new ClientException(sprintf(_('You already have created %d or more tags ' .
155                                                     'which is the maximum allowed number of tags. ' .
156                                                     'Try using or deleting some existing tags.'),
157                                                     common_config('peopletag', 'maxtags')));
158                 return false;
159             }
160
161             $plist = new Profile_list();
162             $plist->query('BEGIN');
163
164             $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
165
166             if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
167                 // TRANS: Client exception thrown when trying to add more people than allowed to a list.
168                 throw new ClientException(sprintf(_('You already have %1$d or more people in list %2$s, ' .
169                                                     'which is the maximum allowed number.' .
170                                                     'Try unlisting others first.'),
171                                                     common_config('peopletag', 'maxpeople'), $tag));
172                 return false;
173             }
174
175             $newtag = new Profile_tag();
176
177             $newtag->tagger = $tagger;
178             $newtag->tagged = $tagged;
179             $newtag->tag = $tag;
180
181             $result = $newtag->insert();
182
183
184             if (!$result) {
185                 common_log_db_error($newtag, 'INSERT', __FILE__);
186                 return false;
187             }
188
189             try {
190                 $plist->query('COMMIT');
191                 Event::handle('EndTagProfile', array($newtag));
192             } catch (Exception $e) {
193                 $newtag->delete();
194                 $profile_list->delete();
195                 throw $e;
196                 return false;
197             }
198
199             $profile_list->taggedCount(true);
200             self::blowCaches($tagger, $tagged);
201         }
202
203         return $newtag;
204     }
205
206     static function unTag($tagger, $tagged, $tag) {
207         $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
208                                            'tagged' => $tagged,
209                                            'tag'    => $tag));
210         if (!$ptag) {
211             return true;
212         }
213
214         if (Event::handle('StartUntagProfile', array($ptag))) {
215             $orig = clone($ptag);
216             $result = $ptag->delete();
217             if (!$result) {
218                 common_log_db_error($this, 'DELETE', __FILE__);
219                 return false;
220             }
221             Event::handle('EndUntagProfile', array($orig));
222             if ($result) {
223                 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
224                 if (!empty($profile_list)) {
225                     $profile_list->taggedCount(true);
226                 }
227                 self::blowCaches($tagger, $tagged);
228                 return true;
229             }
230             return false;
231         }
232     }
233
234     // @fixme: move this to Profile_list?
235     static function cleanup($profile_list) {
236         $ptag = new Profile_tag();
237         $ptag->tagger = $profile_list->tagger;
238         $ptag->tag = $profile_list->tag;
239         $ptag->find();
240
241         while($ptag->fetch()) {
242             if (Event::handle('StartUntagProfile', array($ptag))) {
243                 $orig = clone($ptag);
244                 $result = $ptag->delete();
245                 if (!$result) {
246                     common_log_db_error($this, 'DELETE', __FILE__);
247                 }
248                 Event::handle('EndUntagProfile', array($orig));
249             }
250         }
251     }
252
253     // move a tag!
254     static function moveTag($orig, $new) {
255         $tags = new Profile_tag();
256         $qry = 'UPDATE profile_tag SET ' .
257                'tag = "%s", tagger = "%s" ' .
258                'WHERE tag = "%s" ' .
259                'AND tagger = "%s"';
260         $result = $tags->query(sprintf($qry, $new->tag, $new->tagger,
261                                              $orig->tag, $orig->tagger));
262
263         if (!$result) {
264             common_log_db_error($tags, 'UPDATE', __FILE__);
265             return false;
266         }
267         return true;
268     }
269
270     static function blowCaches($tagger, $tagged) {
271         foreach (array(0, 1) as $perm) {
272             self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
273         }
274         return true;
275     }
276
277     // Return profiles with a given tag
278     static function getTagged($tagger, $tag) {
279         $profile = new Profile();
280         $profile->query('SELECT profile.* ' .
281                         'FROM profile JOIN profile_tag ' .
282                         'ON profile.id = profile_tag.tagged ' .
283                         'WHERE profile_tag.tagger = ' . $tagger . ' ' .
284                         'AND profile_tag.tag = "' . $tag . '" ');
285         $tagged = array();
286         while ($profile->fetch()) {
287             $tagged[] = clone($profile);
288         }
289         return true;
290     }
291
292     function insert()
293     {
294         $result = parent::insert();
295         if ($result) {
296             self::blow('profile_list:tagged_count:%d:%s', 
297                        $this->tagger,
298                        $this->tag);
299         }
300         return $result;
301     }
302
303     function delete()
304     {
305         $result = parent::delete();
306         if ($result) {
307             self::blow('profile_list:tagged_count:%d:%s', 
308                        $this->tagger,
309                        $this->tag);
310         }
311         return $result;
312     }
313 }