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