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 $qry = 'select profile_list.* from profile_list left join '.
59 'profile_tag on (profile_list.tag = profile_tag.tag and '.
60 'profile_list.tagger = profile_tag.tagger) where '.
61 'profile_tag.tagger = %d and profile_tag.tagged = %d ';
62 $qry = sprintf($qry, $tagger, $tagged);
65 $qry .= 'profile_list.private = 0';
68 $profile_list->query($qry);
70 Profile_list::setCache($key, $profile_list);
75 static function getTagsArray($tagger, $tagged, $auth_user_id=null)
77 $ptag = new Profile_tag();
78 $ptag->tagger = $tagger;
79 $ptag->tagged = $tagged;
81 if ($tagger != $auth_user_id) {
82 $list = new Profile_list();
83 $list->private = false;
84 $ptag->joinAdd($list);
86 $ptag->selectAdd('profile_tag.tag');
91 while ($ptag->fetch()) {
99 static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
101 $newtags = array_unique($newtags);
102 $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
104 $ptag = new Profile_tag();
106 // Delete stuff that's in old and not in new
108 $to_delete = array_diff($oldtags, $newtags);
110 // Insert stuff that's in new and not in old
112 $to_insert = array_diff($newtags, $oldtags);
114 foreach ($to_delete as $deltag) {
115 self::unTag($tagger, $tagged, $deltag);
118 foreach ($to_insert as $instag) {
119 $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
120 self::setTag($tagger, $tagged, $instag, null, $private);
126 static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
128 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
132 # if tag already exists, return it
137 $tagger_profile = Profile::staticGet('id', $tagger);
138 $tagged_profile = Profile::staticGet('id', $tagged);
140 if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
142 if (!$tagger_profile->canTag($tagged_profile)) {
143 // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged.
144 throw new ClientException(_('You cannot tag this user.'));
148 $tags = new Profile_list();
149 $tags->tagger = $tagger;
150 $count = (int) $tags->count('distinct tag');
152 if ($count >= common_config('peopletag', 'maxtags')) {
153 // TRANS: Client exception thrown trying to set more tags than allowed.
154 throw new ClientException(sprintf(_('You already have created %d or more tags ' .
155 'which is the maximum allowed number of tags. ' .
156 'Try using or deleting some existing tags.'),
157 common_config('peopletag', 'maxtags')));
161 $plist = new Profile_list();
162 $plist->query('BEGIN');
164 $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
166 if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
167 // TRANS: Client exception thrown when trying to add more people than allowed to a list.
168 throw new ClientException(sprintf(_('You already have %1$d or more people in list %2$s, ' .
169 'which is the maximum allowed number.' .
170 'Try unlisting others first.'),
171 common_config('peopletag', 'maxpeople'), $tag));
175 $newtag = new Profile_tag();
177 $newtag->tagger = $tagger;
178 $newtag->tagged = $tagged;
181 $result = $newtag->insert();
185 common_log_db_error($newtag, 'INSERT', __FILE__);
190 $plist->query('COMMIT');
191 Event::handle('EndTagProfile', array($newtag));
192 } catch (Exception $e) {
194 $profile_list->delete();
199 $profile_list->taggedCount(true);
200 self::blowCaches($tagger, $tagged);
206 static function unTag($tagger, $tagged, $tag) {
207 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
214 if (Event::handle('StartUntagProfile', array($ptag))) {
215 $orig = clone($ptag);
216 $result = $ptag->delete();
218 common_log_db_error($this, 'DELETE', __FILE__);
221 Event::handle('EndUntagProfile', array($orig));
223 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
224 if (!empty($profile_list)) {
225 $profile_list->taggedCount(true);
227 self::blowCaches($tagger, $tagged);
234 // @fixme: move this to Profile_list?
235 static function cleanup($profile_list) {
236 $ptag = new Profile_tag();
237 $ptag->tagger = $profile_list->tagger;
238 $ptag->tag = $profile_list->tag;
241 while($ptag->fetch()) {
242 if (Event::handle('StartUntagProfile', array($ptag))) {
243 $orig = clone($ptag);
244 $result = $ptag->delete();
246 common_log_db_error($this, 'DELETE', __FILE__);
248 Event::handle('EndUntagProfile', array($orig));
254 static function moveTag($orig, $new) {
255 $tags = new Profile_tag();
256 $qry = 'UPDATE profile_tag SET ' .
257 'tag = "%s", tagger = "%s" ' .
258 'WHERE tag = "%s" ' .
260 $result = $tags->query(sprintf($qry, $new->tag, $new->tagger,
261 $orig->tag, $orig->tagger));
264 common_log_db_error($tags, 'UPDATE', __FILE__);
270 static function blowCaches($tagger, $tagged) {
271 foreach (array(0, 1) as $perm) {
272 self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
277 // Return profiles with a given tag
278 static function getTagged($tagger, $tag) {
279 $profile = new Profile();
280 $profile->query('SELECT profile.* ' .
281 'FROM profile JOIN profile_tag ' .
282 'ON profile.id = profile_tag.tagged ' .
283 'WHERE profile_tag.tagger = ' . $tagger . ' ' .
284 'AND profile_tag.tag = "' . $tag . '" ');
286 while ($profile->fetch()) {
287 $tagged[] = clone($profile);
294 $result = parent::insert();
296 self::blow('profile_list:tagged_count:%d:%s',
305 $result = parent::delete();
307 self::blow('profile_list:tagged_count:%d:%s',