/** * save a new people tag, this should be always used * since it makes uri, homeurl, created and modified * timestamps and performs checks. * * @param array $fields an array with fields and their values * * @return mixed Profile_list on success, false on fail */ static function saveNew(array $fields) { extract($fields); $ptag = new Profile_list(); $ptag->query('BEGIN'); if (empty($tagger)) { // TRANS: Server exception saving new tag without having a tagger specified. throw new Exception(_('No tagger specified.')); } if (empty($tag)) { // TRANS: Server exception saving new tag without having a tag specified. throw new Exception(_('No tag specified.')); } if (empty($mainpage)) { $mainpage = null; } if (empty($uri)) { // fill in later... $uri = null; } if (empty($mainpage)) { $mainpage = null; } if (empty($description)) { $description = null; } if (empty($private)) { $private = false; } $ptag->tagger = $tagger; $ptag->tag = $tag; $ptag->description = $description; $ptag->private = $private; $ptag->uri = $uri; $ptag->mainpage = $mainpage; $ptag->created = common_sql_now(); $ptag->modified = common_sql_now(); $result = $ptag->insert(); if (!$result) { common_log_db_error($ptag, 'INSERT', __FILE__); // TRANS: Server exception saving new tag. throw new ServerException(_('Could not create profile tag.')); } if (!isset($uri) || empty($uri)) { $orig = clone $ptag; $ptag->uri = common_local_url('profiletagbyid', array('id' => $ptag->id, 'tagger_id' => $ptag->tagger)); $result = $ptag->update($orig); if (!$result) { common_log_db_error($ptag, 'UPDATE', __FILE__); // TRANS: Server exception saving new tag. throw new ServerException(_('Could not set profile tag URI.')); } } if (!isset($mainpage) || empty($mainpage)) { $orig = clone $ptag; $user = User::getKV('id', $ptag->tagger); if (!empty($user)) { $ptag->mainpage = common_local_url('showprofiletag', array('tag' => $ptag->tag, 'nickname' => $user->getNickname())); } else { $ptag->mainpage = $uri; // assume this is a remote peopletag and the uri works } $result = $ptag->update($orig); if (!$result) { common_log_db_error($ptag, 'UPDATE', __FILE__); // TRANS: Server exception saving new tag. throw new ServerException(_('Could not set profile tag mainpage.')); } } return $ptag; }
static function setTag($tagger, $tagged, $tag, $desc = null, $private = false) { $ptag = Profile_tag::pkeyGet(array('tagger' => $tagger, 'tagged' => $tagged, 'tag' => $tag)); # if tag already exists, return it if (!empty($ptag)) { return $ptag; } $tagger_profile = Profile::staticGet('id', $tagger); $tagged_profile = Profile::staticGet('id', $tagged); if (Event::handle('StartTagProfile', array($tagger_profile, $tagged_profile, $tag))) { if (!$tagger_profile->canTag($tagged_profile)) { // TRANS: Client exception thrown trying to set a tag for a user that cannot be tagged. throw new ClientException(_('You cannot tag this user.')); return false; } $tags = new Profile_list(); $tags->tagger = $tagger; $count = (int) $tags->count('distinct tag'); if ($count >= common_config('peopletag', 'maxtags')) { // TRANS: Client exception thrown trying to set more tags than allowed. throw new ClientException(sprintf(_('You already have created %d or more tags ' . 'which is the maximum allowed number of tags. ' . 'Try using or deleting some existing tags.'), common_config('peopletag', 'maxtags'))); return false; } $plist = new Profile_list(); $plist->query('BEGIN'); $profile_list = Profile_list::ensureTag($tagger, $tag, $desc, $private); if ($profile_list->taggedCount() >= common_config('peopletag', 'maxpeople')) { // TRANS: Client exception thrown when trying to add more people than allowed to a list. throw new ClientException(sprintf(_('You already have %1$d or more people in list %2$s, ' . 'which is the maximum allowed number.' . 'Try unlisting others first.'), common_config('peopletag', 'maxpeople'), $tag)); return false; } $newtag = new Profile_tag(); $newtag->tagger = $tagger; $newtag->tagged = $tagged; $newtag->tag = $tag; $result = $newtag->insert(); if (!$result) { common_log_db_error($newtag, 'INSERT', __FILE__); return false; } try { $plist->query('COMMIT'); Event::handle('EndTagProfile', array($newtag)); } catch (Exception $e) { $newtag->delete(); $profile_list->delete(); throw $e; return false; } $profile_list->taggedCount(true); self::blowCaches($tagger, $tagged); } return $newtag; }
/** * Get tags that other people put on this profile, in reverse-chron order * * @param (Profile|User) $auth_user Authorized user (used for privacy) * @param int $offset Offset from latest * @param int $limit Max number to get * @param datetime $since_id max date * @param datetime $max_id min date * * @return Profile_list resulting lists */ function getOtherTags($auth_user = null, $offset = 0, $limit = null, $since_id = 0, $max_id = 0) { $list = new Profile_list(); $qry = sprintf('select profile_list.*, unix_timestamp(profile_tag.modified) as "cursor" ' . 'from profile_tag join profile_list ' . 'on (profile_tag.tagger = profile_list.tagger ' . ' and profile_tag.tag = profile_list.tag) ' . 'where profile_tag.tagged = %d ', $this->id); if ($auth_user instanceof User || $auth_user instanceof Profile) { $qry .= sprintf('AND ( ( profile_list.private = false ) ' . 'OR ( profile_list.tagger = %d AND ' . 'profile_list.private = true ) )', $auth_user->id); } else { $qry .= 'AND profile_list.private = 0 '; } if ($since_id > 0) { $qry .= sprintf('AND (cursor > %d) ', $since_id); } if ($max_id > 0) { $qry .= sprintf('AND (cursor < %d) ', $max_id); } $qry .= 'ORDER BY profile_tag.modified DESC '; if ($offset >= 0 && !is_null($limit)) { $qry .= sprintf('LIMIT %d OFFSET %d ', $limit, $offset); } $list->query($qry); return $list; }