Esempio n. 1
0
 /**
  * Finds updates by their tags that match the given search query.
  * @param string $term the search term.
  * @param integer $page the current page.
  * @return array found tags.
  */
 public static function searchFor($term, $page = 0)
 {
     if (empty($term)) {
         return [];
     }
     $tagIds = static::getTagIds($term);
     if (0 === count($tagIds)) {
         return [];
     }
     $query = "SELECT u.*," . (CW::$app->user->isLogged() ? "0 < (SELECT count(*) FROM `update_upvoters` WHERE `update_id` = `u`.`id` AND `user_id` = " . CW::$app->user->identity->id . ") " : ' false ') . " `voted` FROM updates u JOIN (SELECT distinct update_id FROM update_tags WHERE tag_id IN (" . ArrayHelper::getArrayToString($tagIds, ',') . ") LIMIT " . static::LOAD_FACTOR . " OFFSET " . static::LOAD_FACTOR * $page . ") ut ON ut.update_id = u.id ORDER BY u.rate DESC, u.created_at DESC";
     $stmt = CW::$app->db->executeQuery($query);
     $updates = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     foreach ($updates as &$update) {
         $update['imgUrl'] = '/images/updates/' . $update['id'] . '/' . Update::IMAGE_MEDIUM_WIDTH . 'xX.jpeg';
         $update['updateUrl'] = Update::getUpdateUrl($update['id']);
         $update['postedAgo'] = BaseModel::getPostedAgoTime($update['created_at']);
         $update['created_at'] = strtotime($update['created_at']);
         $update['voted'] = (bool) $update['voted'];
     }
     if (0 < count($updates)) {
         Update::setUpdateTags($updates);
         Update::setUpdatePostedFrom($updates);
         Image::setUpdateImages($updates);
     }
     return $updates;
 }
Esempio n. 2
0
 /**
  * 
  * @param type $time
  * @param type $userId
  * @param type $type
  * @return boolean
  */
 public static function getUserUpdates($userId, $type, $page = 0)
 {
     if (!is_numeric($userId)) {
         return false;
     }
     static::validateActivityType($type);
     if ('all' === $type) {
         $where = '';
     } else {
         if ('posted' === $type) {
             $where = 'AND ua.type_posted= 1';
         } else {
             if ('upvoted' === $type) {
                 $where = 'AND ua.type_upvoted = 1';
             } else {
                 if ('commented' === $type) {
                     $where = 'AND ua.type_commented = 1';
                 } else {
                     $where = '';
                 }
             }
         }
     }
     $query = "SELECT u.*, ua.type_posted, ua.type_upvoted, ua.type_commented, " . (CW::$app->user->isLogged() ? "0 < (SELECT count(*) FROM `update_upvoters` WHERE `update_id` = `u`.`id` AND `user_id` = " . CW::$app->user->identity->id . ") " : ' false ') . " `voted` FROM `updates` u JOIN `user_update_activity` ua ON ua.update_id = u.id WHERE ua.`user_id` = {$userId} {$where} ORDER BY ua.`time` DESC LIMIT " . self::UPDATES_LOAD_COUNT . " OFFSET " . self::UPDATES_LOAD_COUNT * $page;
     $stmt = CW::$app->db->executeQuery($query);
     $updates = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     $updatesCount = count($updates);
     for ($i = 0; $i < $updatesCount; $i++) {
         $updates[$i]['description'] = htmlspecialchars($updates[$i]['description']);
         $updates[$i]['imgUrl'] = '/images/updates/' . $updates[$i]['id'] . '/' . self::IMAGE_MEDIUM_WIDTH . 'xX.jpeg';
         $updates[$i]['updateUrl'] = self::getUpdateUrl($updates[$i]['id']);
         $updates[$i]['postedAgo'] = BaseModel::getPostedAgoTime($updates[$i]['created_at']);
         $updates[$i]['created_at'] = strtotime($updates[$i]['created_at']);
         $updates[$i]['voted'] = (bool) $updates[$i]['voted'];
         $updates[$i]['activity_type'] = ($updates[$i]['type_posted'] ? Update::ACTIVITY_TYPE_POST : 0) | ($updates[$i]['type_upvoted'] ? Update::ACTIVITY_TYPE_UPVOTE : 0) | ($updates[$i]['type_commented'] ? Update::ACTIVITY_TYPE_COMMENT : 0);
     }
     if (0 < $updatesCount) {
         self::setUpdateTags($updates);
         self::setUpdatePostedFrom($updates);
         Image::setUpdateImages($updates);
     }
     return $updates;
 }