3 * Table Definition for profile_tag
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Profile_tag extends Managed_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 public static function schemaDef()
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'),
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')),
42 'profile_tag_modified_idx' => array('modified'),
43 'profile_tag_tagger_tag_idx' => array('tagger', 'tag'),
44 'profile_tag_tagged_idx' => array('tagged'),
49 function pkeyGet($kv) {
50 return Memcached_DataObject::pkeyGet('Profile_tag', $kv);
55 return array('tagger,tag' => 'profile_list:tagger,tag');
60 return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
63 static function getTags($tagger, $tagged, $auth_user=null) {
65 $profile_list = new Profile_list();
68 if (!($auth_user instanceof User ||
69 $auth_user instanceof Profile) ||
70 ($auth_user->id !== $tagger)) {
72 $profile_list->private = false;
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) {
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);
89 $qry .= ' and profile_list.private = 0';
92 $profile_list->query($qry);
94 Profile_list::setCache($key, $profile_list);
99 static function getTagsArray($tagger, $tagged, $auth_user_id=null)
101 $ptag = new Profile_tag();
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 ',
111 if ($auth_user_id != $tagger) {
112 $qry .= 'and profile_list.private = 0';
119 while ($ptag->fetch()) {
120 $tags[] = $ptag->tag;
126 static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
128 $newtags = array_unique($newtags);
129 $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
131 $ptag = new Profile_tag();
133 // Delete stuff that's in old and not in new
135 $to_delete = array_diff($oldtags, $newtags);
137 // Insert stuff that's in new and not in old
139 $to_insert = array_diff($newtags, $oldtags);
141 foreach ($to_delete as $deltag) {
142 self::unTag($tagger, $tagged, $deltag);
145 foreach ($to_insert as $instag) {
146 $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
147 self::setTag($tagger, $tagged, $instag, null, $private);
153 static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
155 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
159 # if tag already exists, return it
164 $tagger_profile = Profile::staticGet('id', $tagger);
165 $tagged_profile = Profile::staticGet('id', $tagged);
167 if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
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.'));
175 $tags = new Profile_list();
176 $tags->tagger = $tagger;
177 $count = (int) $tags->count('distinct tag');
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')));
188 $plist = new Profile_list();
189 $plist->query('BEGIN');
191 $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
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));
202 $newtag = new Profile_tag();
204 $newtag->tagger = $tagger;
205 $newtag->tagged = $tagged;
208 $result = $newtag->insert();
212 common_log_db_error($newtag, 'INSERT', __FILE__);
217 $plist->query('COMMIT');
218 Event::handle('EndTagProfile', array($newtag));
219 } catch (Exception $e) {
221 $profile_list->delete();
226 $profile_list->taggedCount(true);
227 self::blowCaches($tagger, $tagged);
233 static function unTag($tagger, $tagged, $tag) {
234 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
241 if (Event::handle('StartUntagProfile', array($ptag))) {
242 $orig = clone($ptag);
243 $result = $ptag->delete();
245 common_log_db_error($this, 'DELETE', __FILE__);
248 Event::handle('EndUntagProfile', array($orig));
250 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
251 if (!empty($profile_list)) {
252 $profile_list->taggedCount(true);
254 self::blowCaches($tagger, $tagged);
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;
268 while($ptag->fetch()) {
269 if (Event::handle('StartUntagProfile', array($ptag))) {
270 $orig = clone($ptag);
271 $result = $ptag->delete();
273 common_log_db_error($this, 'DELETE', __FILE__);
275 Event::handle('EndUntagProfile', array($orig));
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" ' .
287 $result = $tags->query(sprintf($qry,
288 $tags->escape($new->tag),
289 $tags->escape($new->tagger),
290 $tags->escape($orig->tag),
291 $tags->escape($orig->tagger)));
294 common_log_db_error($tags, 'UPDATE', __FILE__);
300 static function blowCaches($tagger, $tagged) {
301 foreach (array(0, 1) as $perm) {
302 self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
307 // Return profiles with a given tag
308 static function getTagged($tagger, $tag) {
309 $profile = new Profile();
310 $profile->query('SELECT profile.* ' .
311 'FROM profile JOIN profile_tag ' .
312 'ON profile.id = profile_tag.tagged ' .
313 'WHERE profile_tag.tagger = ' . $profile->escape($tagger) . ' ' .
314 'AND profile_tag.tag = "' . $profile->escape($tag) . '" ');
316 while ($profile->fetch()) {
317 $tagged[] = clone($profile);
324 $result = parent::insert();
326 self::blow('profile_list:tagged_count:%d:%s',
335 $result = parent::delete();
337 self::blow('profile_list:tagged_count:%d:%s',