3 * Table Definition for profile_tag
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Profile_tag extends Memcached_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
19 function staticGet($k,$v=null)
20 { return Memcached_DataObject::staticGet('Profile_tag',$k,$v); }
22 /* the code above is auto generated do not remove the tag below */
25 function pkeyGet($kv) {
26 return Memcached_DataObject::pkeyGet('Profile_tag', $kv);
31 return array('tagger,tag' => 'profile_list:tagger,tag');
36 return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
39 static function getTags($tagger, $tagged, $auth_user=null) {
41 $profile_list = new Profile_list();
44 if (!($auth_user instanceof User ||
45 $auth_user instanceof Profile) ||
46 ($auth_user->id !== $tagger)) {
48 $profile_list->private = false;
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) {
58 $profile_tag = new Profile_tag();
59 $profile_list->tagger = $tagger;
60 $profile_tag->tagged = $tagged;
62 $profile_list->selectAdd();
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();
73 Profile_list::setCache($key, $profile_list);
78 static function getTagsArray($tagger, $tagged, $auth_user_id=null)
80 $ptag = new Profile_tag();
81 $ptag->tagger = $tagger;
82 $ptag->tagged = $tagged;
84 if ($tagger != $auth_user_id) {
85 $list = new Profile_list();
86 $list->private = false;
87 $ptag->joinAdd($list);
89 $ptag->selectAdd('profile_tag.tag');
94 while ($ptag->fetch()) {
102 static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
104 $newtags = array_unique($newtags);
105 $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
107 $ptag = new Profile_tag();
109 // Delete stuff that's in old and not in new
111 $to_delete = array_diff($oldtags, $newtags);
113 // Insert stuff that's in new and not in old
115 $to_insert = array_diff($newtags, $oldtags);
117 foreach ($to_delete as $deltag) {
118 self::unTag($tagger, $tagged, $deltag);
121 foreach ($to_insert as $instag) {
122 $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
123 self::setTag($tagger, $tagged, $instag, null, $private);
129 static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
131 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
135 # if tag already exists, return it
140 $tagger_profile = Profile::staticGet('id', $tagger);
141 $tagged_profile = Profile::staticGet('id', $tagged);
143 if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
145 if (!$tagger_profile->canTag($tagged_profile)) {
146 // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged.
147 throw new ClientException(_('You cannot tag this user.'));
151 $tags = new Profile_list();
152 $tags->tagger = $tagger;
153 $count = (int) $tags->count('distinct tag');
155 if ($count >= common_config('peopletag', 'maxtags')) {
156 // TRANS: Client exception thrown trying to set more tags than allowed.
157 throw new ClientException(sprintf(_('You already have created %d or more tags ' .
158 'which is the maximum allowed number of tags. ' .
159 'Try using or deleting some existing tags.'),
160 common_config('peopletag', 'maxtags')));
164 $plist = new Profile_list();
165 $plist->query('BEGIN');
167 $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
169 if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
170 // TRANS: Client exception thrown trying to set one tag for more people than allowed.
171 throw new ClientException(sprintf(_('You already have %1$d or more people tagged %2$s, ' .
172 'which is the maximum allowed number.' .
173 'Try untagging others with the same tag first.'),
174 common_config('peopletag', 'maxpeople'), $tag));
178 $newtag = new Profile_tag();
180 $newtag->tagger = $tagger;
181 $newtag->tagged = $tagged;
184 $result = $newtag->insert();
188 common_log_db_error($newtag, 'INSERT', __FILE__);
193 $plist->query('COMMIT');
194 Event::handle('EndTagProfile', array($newtag));
195 } catch (Exception $e) {
197 $profile_list->delete();
202 $profile_list->taggedCount(true);
203 self::blowCaches($tagger, $tagged);
209 static function unTag($tagger, $tagged, $tag) {
210 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
217 if (Event::handle('StartUntagProfile', array($ptag))) {
218 $orig = clone($ptag);
219 $result = $ptag->delete();
221 common_log_db_error($this, 'DELETE', __FILE__);
224 Event::handle('EndUntagProfile', array($orig));
226 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
227 if (!empty($profile_list)) {
228 $profile_list->taggedCount(true);
230 self::blowCaches($tagger, $tagged);
237 // @fixme: move this to Profile_list?
238 static function cleanup($profile_list) {
239 $ptag = new Profile_tag();
240 $ptag->tagger = $profile_list->tagger;
241 $ptag->tag = $profile_list->tag;
244 while($ptag->fetch()) {
245 if (Event::handle('StartUntagProfile', array($ptag))) {
246 $orig = clone($ptag);
247 $result = $ptag->delete();
249 common_log_db_error($this, 'DELETE', __FILE__);
251 Event::handle('EndUntagProfile', array($orig));
257 static function moveTag($orig, $new) {
258 $tags = new Profile_tag();
259 $qry = 'UPDATE profile_tag SET ' .
260 'tag = "%s", tagger = "%s" ' .
261 'WHERE tag = "%s" ' .
263 $result = $tags->query(sprintf($qry, $new->tag, $new->tagger,
264 $orig->tag, $orig->tagger));
267 common_log_db_error($tags, 'UPDATE', __FILE__);
273 static function blowCaches($tagger, $tagged) {
274 foreach (array(0, 1) as $perm) {
275 self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
280 // Return profiles with a given tag
281 static function getTagged($tagger, $tag) {
282 $profile = new Profile();
283 $profile->query('SELECT profile.* ' .
284 'FROM profile JOIN profile_tag ' .
285 'ON profile.id = profile_tag.tagged ' .
286 'WHERE profile_tag.tagger = ' . $tagger . ' ' .
287 'AND profile_tag.tag = "' . $tag . '" ');
289 while ($profile->fetch()) {
290 $tagged[] = clone($profile);
297 $result = parent::insert();
299 self::blow('profile_list:tagged_count:%d:%s',
308 $result = parent::delete();
310 self::blow('profile_list:tagged_count:%d:%s',