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'),
45 function pkeyGet($kv) {
46 return Memcached_DataObject::pkeyGet('Profile_tag', $kv);
51 return array('tagger,tag' => 'profile_list:tagger,tag');
56 return Profile_list::pkeyGet(array('tagger' => $this->tagger, 'tag' => $this->tag));
59 static function getTags($tagger, $tagged, $auth_user=null) {
61 $profile_list = new Profile_list();
64 if (!($auth_user instanceof User ||
65 $auth_user instanceof Profile) ||
66 ($auth_user->id !== $tagger)) {
68 $profile_list->private = false;
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) {
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);
85 $qry .= ' and profile_list.private = 0';
88 $profile_list->query($qry);
90 Profile_list::setCache($key, $profile_list);
95 static function getTagsArray($tagger, $tagged, $auth_user_id=null)
97 $ptag = new Profile_tag();
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 ',
107 if ($auth_user_id != $tagger) {
108 $qry .= 'and profile_list.private = 0';
115 while ($ptag->fetch()) {
116 $tags[] = $ptag->tag;
122 static function setTags($tagger, $tagged, $newtags, $privacy=array()) {
124 $newtags = array_unique($newtags);
125 $oldtags = self::getTagsArray($tagger, $tagged, $tagger);
127 $ptag = new Profile_tag();
129 // Delete stuff that's in old and not in new
131 $to_delete = array_diff($oldtags, $newtags);
133 // Insert stuff that's in new and not in old
135 $to_insert = array_diff($newtags, $oldtags);
137 foreach ($to_delete as $deltag) {
138 self::unTag($tagger, $tagged, $deltag);
141 foreach ($to_insert as $instag) {
142 $private = isset($privacy[$instag]) ? $privacy[$instag] : false;
143 self::setTag($tagger, $tagged, $instag, null, $private);
149 static function setTag($tagger, $tagged, $tag, $desc=null, $private=false) {
151 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
155 # if tag already exists, return it
160 $tagger_profile = Profile::staticGet('id', $tagger);
161 $tagged_profile = Profile::staticGet('id', $tagged);
163 if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) {
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.'));
171 $tags = new Profile_list();
172 $tags->tagger = $tagger;
173 $count = (int) $tags->count('distinct tag');
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')));
184 $plist = new Profile_list();
185 $plist->query('BEGIN');
187 $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
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));
198 $newtag = new Profile_tag();
200 $newtag->tagger = $tagger;
201 $newtag->tagged = $tagged;
204 $result = $newtag->insert();
208 common_log_db_error($newtag, 'INSERT', __FILE__);
213 $plist->query('COMMIT');
214 Event::handle('EndTagProfile', array($newtag));
215 } catch (Exception $e) {
217 $profile_list->delete();
222 $profile_list->taggedCount(true);
223 self::blowCaches($tagger, $tagged);
229 static function unTag($tagger, $tagged, $tag) {
230 $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger,
237 if (Event::handle('StartUntagProfile', array($ptag))) {
238 $orig = clone($ptag);
239 $result = $ptag->delete();
241 common_log_db_error($this, 'DELETE', __FILE__);
244 Event::handle('EndUntagProfile', array($orig));
246 $profile_list = Profile_list::pkeyGet(array('tag' => $tag, 'tagger' => $tagger));
247 if (!empty($profile_list)) {
248 $profile_list->taggedCount(true);
250 self::blowCaches($tagger, $tagged);
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;
264 while($ptag->fetch()) {
265 if (Event::handle('StartUntagProfile', array($ptag))) {
266 $orig = clone($ptag);
267 $result = $ptag->delete();
269 common_log_db_error($this, 'DELETE', __FILE__);
271 Event::handle('EndUntagProfile', array($orig));
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" ' .
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)));
290 common_log_db_error($tags, 'UPDATE', __FILE__);
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));
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) . '" ');
312 while ($profile->fetch()) {
313 $tagged[] = clone($profile);
320 $result = parent::insert();
322 self::blow('profile_list:tagged_count:%d:%s',
331 $result = parent::delete();
333 self::blow('profile_list:tagged_count:%d:%s',