/** * 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; }
/** * constructor */ public function __construct(array $attributes = array()) { parent::__construct($attributes); // override properties set by base model // $this->timestamps = false; }
/** * attribute visibility methods */ protected function getVisible() { $visible = parent::getVisible(); // add time stamp fields // if (Config::get('model.database.soft_delete')) { $visible = array_merge($visible, array($this->getCreatedAtColumn(), $this->getUpdatedAtColumn(), $this->getDeletedAtColumn())); } else { $visible = array_merge($visible, array($this->getCreatedAtColumn(), $this->getUpdatedAtColumn())); } return $visible; }
/** * Get the table associated with the model. * * @return string */ public function getTable() { if (isset($this->table)) { // table name is explicitly defined // return $this->table; } else { // table name is derived from class name // return BaseModel::getTableName(class_basename($this)); } }
public function doCreate() { $content = CW::$app->request->post('content'); $updateId = CW::$app->request->post('updateId'); if (empty($content) || empty($updateId)) { return false; } $replyTo = CW::$app->request->post('replyTo'); $result = \models\Comment::create($content, $updateId, CW::$app->user->identity->id, 0 === $replyTo ? null : $replyTo); if ($result) { $result->owner = ['id' => CW::$app->user->identity->id, 'username' => htmlspecialchars(CW::$app->user->identity->username), 'profileUrl' => \models\User::getProfileUrl(CW::$app->user->identity->id), 'pictureUrl' => CW::$app->user->identity->getProfilePicUrl()]; $result->postedAgo = \models\BaseModel::getPostedAgoTime(date("Y-m-d H:i:s", time())); $result->upvotes = 0; $result->replies = $replyTo ? false : []; $result->voted = false; $result->content = htmlspecialchars($result->content); } return json_encode($result); }
public static function getOne($id) { if (!is_numeric($id)) { return null; } $stmt = CW::$app->db->executeQuery("SELECT *, " . (CW::$app->user->isLogged() ? "0 < (SELECT count(*) FROM `update_upvoters` WHERE `update_id` = `updates`.`id` AND `user_id` = " . CW::$app->user->identity->id . ") " : ' false ') . " `voted` FROM `updates` WHERE `id` = {$id}"); $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); if (1 === count($result)) { $update = $result[0]; $update['description'] = htmlspecialchars($update['description']); $update['postedAgo'] = BaseModel::getPostedAgoTime($update['created_at']); $update['voted'] = (bool) $update['voted']; $update['tags'] = UpdateTag::getUpdateTags($update['id']); return $update; } return null; }
/** * constructor */ public function __construct(array $attributes = array()) { // call superclass constructor // BaseModel::__construct($attributes); }
/** * Select the record, Interface with the outside (Controller Action) * @param array $parameter * @param string $columns * @param int $limit * @param int $offset * @param string $sortBy * @param string $sortType * @return mixed */ public static function getUsers($parameter = [], $columns = '*', $limit = 30, $offset = 1, $sortBy = '', $sortType = '') { $whereString = ''; $bindParams = []; $modelName = get_class(); // Begin assign keyword to search if (isset($parameter['keyword']) && $parameter['keyword'] != '') { $keyword = $parameter['keyword']; // Define default keywordIn $keywordIn = ['name', 'email']; // If controller use keywordIn if (isset($parameter['keywordIn']) && !empty($parameter['keywordIn'])) { $keywordIn = $parameter['keywordIn']; } $whereString .= $whereString != '' ? ' OR ' : ' ('; $filter = ''; foreach ($keywordIn as $in) { $filter .= ($filter != '' ? ' OR ' : '') . $in . ' LIKE :searchKeyword:'; } $whereString .= $filter . ')'; $bindParams['searchKeyword'] = '%' . $keyword . '%'; } unset($parameter['keyword']); unset($parameter['keywordIn']); // End Search // Assign name params same MetaData foreach ($parameter as $key => $value) { $whereString .= ($whereString != '' ? ' AND ' : '') . $key . ' = :' . $key . ':'; $bindParams[$key] = $value; } $conditions = []; if ($whereString != '' && !empty($bindParams)) { $conditions = [[$whereString, $bindParams]]; } // Check order if ($sortBy == '') { $sortBy = 'id'; } if (strcasecmp($sortType, 'ASC') != 0 && strcasecmp($sortType, 'DESC') != 0) { $sortType = 'DESC'; } $order = $sortBy . ' ' . $sortType; $params = ['models' => $modelName, 'columns' => $columns, 'conditions' => $conditions, 'order' => [$modelName . '.' . $order . '']]; return parent::getList($params, $limit, $offset); }
public static function getReplies($replyTo, $last = null) { if (!is_numeric($replyTo) || !is_numeric($replyTo)) { return []; } $result = []; $result['hasMore'] = false; $last = is_numeric($last) ? " AND c.`posted_on` > {$last}" : ''; $query = "SELECT c.*, u.id ownerId, u.username ownerUsername, u.profile_img_id FROM `comments` c JOIN `users` u ON c.`user_id` = u.`id` WHERE c.`reply_to` = {$replyTo} {$last} ORDER BY c.`posted_on` ASC LIMIT 7"; $stmt = \CW::$app->db->executeQuery($query); $replies = $stmt->fetchAll(\PDO::FETCH_OBJ); $repliesCount = count($replies); $last = null; for ($i = 0; $i < $repliesCount; $i++) { $last = $replies[$i]->posted_on; $replies[$i]->postedAgo = BaseModel::getPostedAgoTime($replies[$i]->posted_on); $replies[$i]->content = htmlspecialchars($replies[$i]->content); $replies[$i]->owner = ['id' => $replies[$i]->ownerId, 'username' => htmlspecialchars($replies[$i]->ownerUsername), 'profileUrl' => \models\User::getProfileUrl($replies[$i]->ownerId), 'pictureUrl' => User::getProfilePictureUrl($replies[$i]->profile_img_id, $replies[$i]->ownerId)]; } if (0 < $repliesCount) { $stmt = CW::$app->db->executeQuery("SELECT `reply_to` FROM `comments` WHERE `reply_to` = {$replyTo} AND `posted_on` > '" . $last . "' LIMIT 1"); $_result = $stmt->fetchAll(\PDO::FETCH_ASSOC); if (0 < count($_result)) { $result['hasMore'] = true; } } $result['items'] = $replies; return $result; }