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