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
18 /* the code above is auto generated do not remove the tag below */
21 public static function schemaDef()
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'),
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')),
38 'profile_tag_modified_idx' => array('modified'),
39 'profile_tag_tagger_tag_idx' => array('tagger', 'tag'),
40 'profile_tag_tagged_idx' => array('tagged'),
47 return array('tagger,tag' => 'profile_list:tagger,tag');
52 return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
55 static function getTags($tagger, $tagged, $auth_user=null) {
57 $profile_list = new Profile_list();
60 if (!($auth_user instanceof User ||
61 $auth_user instanceof Profile) ||
62 ($auth_user->id !== $tagger)) {
64 $profile_list->private = false;
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) {
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);
81 $qry .= ' and profile_list.private = 0';
84 $profile_list->query($qry);
86 Profile_list::setCache($key, $profile_list);
91 static function getTagsArray($tagger, $tagged, $auth_user_id=null)
93 $ptag = new Profile_tag();
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 ',
103 if ($auth_user_id != $tagger) {
104 $qry .= 'and profile_list.private = 0';
111 while ($ptag->fetch()) {
112 $tags[] = $ptag->tag;
118 static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
120 $newtags = array_unique($newtags);
121 $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
123 $ptag = new Profile_tag();
125 // Delete stuff that's in old and not in new
127 $to_delete = array_diff($oldtags, $newtags);
129 // Insert stuff that's in new and not in old
131 $to_insert = array_diff($newtags, $oldtags);
133 foreach ($to_delete as $deltag) {
134 self::unTag($tagger, $tagged, $deltag);
137 foreach ($to_insert as $instag) {
138 $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
139 self::setTag($tagger, $tagged, $instag, null, $private);
145 static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
147 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
151 # if tag already exists, return it
156 $tagger_profile = Profile::getKV('id', $tagger);
157 $tagged_profile = Profile::getKV('id', $tagged);
159 if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
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.'));
167 $tags = new Profile_list();
168 $tags->tagger = $tagger;
169 $count = (int) $tags->count('distinct tag');
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')));
180 $plist = new Profile_list();
181 $plist->query('BEGIN');
183 $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
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));
194 $newtag = new Profile_tag();
196 $newtag->tagger = $tagger;
197 $newtag->tagged = $tagged;
200 $result = $newtag->insert();
204 common_log_db_error($newtag, 'INSERT', __FILE__);
209 $plist->query('COMMIT');
210 Event::handle('EndTagProfile', array($newtag));
211 } catch (Exception $e) {
213 $profile_list->delete();
218 $profile_list->taggedCount(true);
219 self::blowCaches($tagger, $tagged);
225 static function unTag($tagger, $tagged, $tag) {
226 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
233 if (Event::handle('StartUntagProfile', array($ptag))) {
234 $orig = clone($ptag);
235 $result = $ptag->delete();
237 common_log_db_error($this, 'DELETE', __FILE__);
240 Event::handle('EndUntagProfile', array($orig));
242 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
243 if (!empty($profile_list)) {
244 $profile_list->taggedCount(true);
246 self::blowCaches($tagger, $tagged);
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;
260 while($ptag->fetch()) {
261 if (Event::handle('StartUntagProfile', array($ptag))) {
262 $orig = clone($ptag);
263 $result = $ptag->delete();
265 common_log_db_error($this, 'DELETE', __FILE__);
267 Event::handle('EndUntagProfile', array($orig));
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" ' .
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)));
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.');
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));
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) . '" ');
308 while ($profile->fetch()) {
309 $tagged[] = clone($profile);
316 $result = parent::insert();
318 self::blow('profile_list:tagged_count:%d:%s',
325 function delete($useWhere=false)
327 $result = parent::delete($useWhere);
328 if ($result !== false) {
329 self::blow('profile_list:tagged_count:%d:%s',