Example #1
0
 /**
  * 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;
 }
Example #2
0
function initProfileLists()
{
    printfnq("Ensuring all profile tags have a corresponding list...");
    $ptag = new Profile_tag();
    $ptag->selectAdd();
    $ptag->selectAdd('tagger, tag, count(*) as tagged_count');
    $ptag->whereAdd('NOT EXISTS (SELECT tagger, tagged from profile_list ' . 'where profile_tag.tagger = profile_list.tagger ' . 'and profile_tag.tag = profile_list.tag)');
    $ptag->groupBy('tagger, tag');
    $ptag->orderBy('tagger, tag');
    if ($ptag->find()) {
        while ($ptag->fetch()) {
            $plist = new Profile_list();
            $plist->tagger = $ptag->tagger;
            $plist->tag = $ptag->tag;
            $plist->private = 0;
            $plist->created = common_sql_now();
            $plist->modified = $plist->created;
            $plist->mainpage = common_local_url('showprofiletag', array('tagger' => $plist->getTagger()->nickname, 'tag' => $plist->tag));
            $plist->tagged_count = $ptag->tagged_count;
            $plist->subscriber_count = 0;
            $plist->insert();
            $orig = clone $plist;
            // After insert since it uses auto-generated ID
            $plist->uri = common_local_url('profiletagbyid', array('id' => $plist->id, 'tagger_id' => $plist->tagger));
            $plist->update($orig);
        }
    }
    printfnq("DONE.\n");
}