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 throw new ClientException(_('You cannot tag this user.'));
150 $tags = new Profile_list();
151 $tags->tagger = $tagger;
152 $count = (int) $tags->count('distinct tag');
154 if ($count >= common_config('peopletag', 'maxtags')) {
155 throw new ClientException(sprintf(_('You already have created %d or more tags ' .
156 'which is the maximum allowed number of tags. ' .
157 'Try using or deleting some existing tags.'),
158 common_config('peopletag', 'maxtags')));
162 $plist = new Profile_list();
163 $plist->query('BEGIN');
165 $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private);
167 if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) {
168 throw new ClientException(sprintf(_('You already have %d or more people tagged %s ' .
169 'which is the maximum allowed number.' .
170 'Try untagging others with the same tag 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 $profile_list->taggedCount(true);
225 self::blowCaches($tagger, $tagged);
232 // @fixme: move this to Profile_list?
233 static function cleanup($profile_list) {
234 $ptag = new Profile_tag();
235 $ptag->tagger = $profile_list->tagger;
236 $ptag->tag = $profile_list->tag;
239 while($ptag->fetch()) {
240 if (Event::handle('StartUntagProfile', array($ptag))) {
241 $orig = clone($ptag);
242 $result = $ptag->delete();
244 common_log_db_error($this, 'DELETE', __FILE__);
246 Event::handle('EndUntagProfile', array($orig));
252 static function moveTag($orig, $new) {
253 $tags = new Profile_tag();
254 $qry = 'UPDATE profile_tag SET ' .
255 'tag = "%s", tagger = "%s" ' .
256 'WHERE tag = "%s" ' .
258 $result = $tags->query(sprintf($qry, $new->tag, $new->tagger,
259 $orig->tag, $orig->tagger));
262 common_log_db_error($tags, 'UPDATE', __FILE__);
268 static function blowCaches($tagger, $tagged) {
269 foreach (array(0, 1) as $perm) {
270 self::blow(sprintf('profile_tag:tagger_tagged_privacy:%d-%d-%d', $tagger, $tagged, $perm));
275 // Return profiles with a given tag
276 static function getTagged($tagger, $tag) {
277 $profile = new Profile();
278 $profile->query('SELECT profile.* ' .
279 'FROM profile JOIN profile_tag ' .
280 'ON profile.id = profile_tag.tagged ' .
281 'WHERE profile_tag.tagger = ' . $tagger . ' ' .
282 'AND profile_tag.tag = "' . $tag . '" ');
284 while ($profile->fetch()) {
285 $tagged[] = clone($profile);