]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag.php
Merge branch 'people_tags_rebase' into 1.0.x
[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         $profile_tag = new Profile_tag();
59         $profile_list->tagger = $tagger;
60         $profile_tag->tagged = $tagged;
61
62         $profile_list->selectAdd();
63
64         // only fetch id, tag, mainpage and
65         // private hoping this will be faster
66         $profile_list->selectAdd('profile_list.id, ' .
67                                  'profile_list.tag, ' .
68                                  'profile_list.mainpage, ' .
69                                  'profile_list.private');
70         $profile_list->joinAdd($profile_tag);
71         $profile_list->find();
72
73         Profile_list::setCache($key, $profile_list);
74
75         return $profile_list;
76     }
77
78     static function getTagsArray($tagger, $tagged, $auth_user_id=null)
79     {
80         $ptag = new Profile_tag();
81         $ptag->tagger = $tagger;
82         $ptag->tagged = $tagged;
83
84         if ($tagger != $auth_user_id) {
85             $list = new Profile_list();
86             $list->private = false;
87             $ptag->joinAdd($list);
88             $ptag->selectAdd();
89             $ptag->selectAdd('profile_tag.tag');
90         }
91
92         $tags = array();
93         $ptag->find();
94         while ($ptag->fetch()) {
95             $tags[] = $ptag->tag;
96         }
97         $ptag->free();
98
99         return $tags;
100     }
101
102     static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
103
104         $newtags = array_unique($newtags);
105         $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
106
107         $ptag = new Profile_tag();
108
109         // Delete stuff that's in old and not in new
110
111         $to_delete = array_diff($oldtags, $newtags);
112
113         // Insert stuff that's in new and not in old
114
115         $to_insert = array_diff($newtags, $oldtags);
116
117         foreach ($to_delete as $deltag) {
118             self::unTag($tagger, $tagged, $deltag);
119         }
120
121         foreach ($to_insert as $instag) {
122             $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
123             self::setTag($tagger, $tagged, $instag, null, $private);
124         }
125         return true;
126     }
127
128     # set a single tag
129     static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
130
131         $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
132                                            'tagged' => $tagged,
133                                            'tag' => $tag));
134
135         # if tag already exists, return it
136         if(!empty($ptag)) {
137             return $ptag;
138         }
139
140         $tagger_profile = Profile::staticGet('id', $tagger);
141         $tagged_profile = Profile::staticGet('id', $tagged);
142
143         if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
144
145             if (!$tagger_profile->canTag($tagged_profile)) {
146                 throw new ClientException(_('You cannot tag this user.'));
147                 return false;
148             }
149
150             $tags = new Profile_list();
151             $tags->tagger = $tagger;
152             $count = (int) $tags->count('distinct tag');
153
154             if ($count >= common_config('peopletag', 'maxtags')) {
155                 throw new ClientException(sprintf(_('You already have created %d or more tags ' .
156                                                     'which is the maximum allowed number of tags. ' .
157                                                     'Try using or deleting some existing tags.'),
158                                                     common_config('peopletag', 'maxtags')));
159                 return false;
160             }
161
162             $plist = new Profile_list();
163             $plist->query('BEGIN');
164
165             $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
166
167             if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
168                 throw new ClientException(sprintf(_('You already have %d or more people tagged %s ' .
169                                                     'which is the maximum allowed number.' .
170                                                     'Try untagging others with the same tag 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                 $profile_list->taggedCount(true);
225                 self::blowCaches($tagger, $tagged);
226                 return true;
227             }
228             return false;
229         }
230     }
231
232     // @fixme: move this to Profile_list?
233     static function cleanup($profile_list) {
234         $ptag = new Profile_tag();
235         $ptag->tagger = $profile_list->tagger;
236         $ptag->tag = $profile_list->tag;
237         $ptag->find();
238
239         while($ptag->fetch()) {
240             if (Event::handle('StartUntagProfile', array($ptag))) {
241                 $orig = clone($ptag);
242                 $result = $ptag->delete();
243                 if (!$result) {
244                     common_log_db_error($this, 'DELETE', __FILE__);
245                 }
246                 Event::handle('EndUntagProfile', array($orig));
247             }
248         }
249     }
250
251     // move a tag!
252     static function moveTag($orig, $new) {
253         $tags = new Profile_tag();
254         $qry = 'UPDATE profile_tag SET ' .
255                'tag = "%s", tagger = "%s" ' .
256                'WHERE tag = "%s" ' .
257                'AND tagger = "%s"';
258         $result = $tags->query(sprintf($qry, $new->tag, $new->tagger,
259                                              $orig->tag, $orig->tagger));
260
261         if (!$result) {
262             common_log_db_error($tags, 'UPDATE', __FILE__);
263             return false;
264         }
265         return true;
266     }
267
268     static function blowCaches($tagger, $tagged) {
269         foreach (array(0, 1) as $perm) {
270             self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
271         }
272         return true;
273     }
274
275     // Return profiles with a given tag
276     static function getTagged($tagger, $tag) {
277         $profile = new Profile();
278         $profile->query('SELECT profile.* ' .
279                         'FROM profile JOIN profile_tag ' .
280                         'ON profile.id = profile_tag.tagged ' .
281                         'WHERE profile_tag.tagger = ' . $tagger . ' ' .
282                         'AND profile_tag.tag = "' . $tag . '" ');
283         $tagged = array();
284         while ($profile->fetch()) {
285             $tagged[] = clone($profile);
286         }
287         return true;
288     }
289 }