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