/** * 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; }
public static function create($content, $updateId, $userId, $replyTo) { if (!is_numeric($updateId) || !is_numeric($userId)) { return false; } if (!empty($replyTo)) { if (!is_numeric($replyTo)) { return false; } $stmt = CW::$app->db->executeQuery('SELECT `reply_to` FROM `comments` WHERE `update_id` = ' . $updateId); $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); if (0 < count($result)) { if (null !== $result[0]['reply_to'] && null !== $replyTo) { return false; } } } CW::$app->db->beginTransaction(); if (null !== $replyTo) { $query = "INSERT INTO `comments` (`user_id`, `update_id`, `content`, `reply_to`, `posted_on`) VALUES (:user_id, :update_id, :content, :reply_to, :posted_on)"; $params = [':user_id' => $userId, ':update_id' => $updateId, ':content' => $content, ':reply_to' => $replyTo, ':posted_on' => time()]; } else { $query = "INSERT INTO `comments` (`user_id`, `update_id`, `content`, `posted_on`) VALUES (:user_id, :update_id, :content, :posted_on)"; $params = [':user_id' => $userId, ':update_id' => $updateId, ':content' => $content, ':posted_on' => time()]; } $stmt = CW::$app->db->prepare($query); $stmt->execute($params); $newCommentId = CW::$app->db->getLastInsertedId(); Update::addActivity($updateId, $userId, Update::ACTIVITY_TYPE_COMMENT); CW::$app->db->executeUpdate("UPDATE `updates` SET `comments` = `comments` + 1 WHERE `id` = {$updateId}"); CW::$app->db->commit(); $comment = new self(); $comment->id = $newCommentId; $comment->content = $content; $comment->replyTo = $replyTo; $comment->update_id = $updateId; return $comment; }
<?php use components\helpers\ArrayHelper; use components\UrlManager; use models\Update; $type = CW::$app->request->get('type'); $type = Update::isValidType($type) ? $type : \models\Update::TYPE_FRESH; $this->title = "Browse {$type} updates"; ?> <script> $(function() { App.update.category = '<?php echo CW::$app->request->get('category'); ?> '; var updateLoader = App.update.Loader({ updatesCont : document.getElementById('updates-cont'), loadingEle : document.getElementById('loading'), noResultEle : document.getElementById('no-results-found'), url : <?php echo json_encode(\components\UrlManager::to(['update/ajaxLoad'])); ?> , ajaxData : { category : App.update.category, type : <?php echo json_encode($type); ?> } });
<div style="text-align: center;font-weight:bold;"> <?php echo htmlspecialchars($model['username']); ?> </div> <div style="text-align: center;max-width: 400px;margin: auto; margin-top: 20px;"> <?php echo htmlspecialchars($model['description']); ?> </div> </div> <div style="text-align: center; background-color: white; line-height: 50px;"> <ul class="user-act-opt-cont"> <li class="user-act-opt <?php echo !in_array(CW::$app->request->get('type'), Update::getAllActivityTypesAsString()) ? 'user-act-selected' : ''; ?> "><a class="user-act-link" href="<?php echo UrlManager::to(['user/view', 'id' => $model['id']]); ?> ">All</a></li> <li class="user-act-opt <?php echo CW::$app->request->get('type') === Update::ACTIVITY_TYPE_POST_STR ? 'user-act-selected' : ''; ?> "><a class="user-act-link" href="<?php echo UrlManager::to(['user/view', 'id' => $model['id'], 'type' => 'posted']); ?> ">Posted</a></li> <li class="user-act-opt <?php echo CW::$app->request->get('type') === Update::ACTIVITY_TYPE_UPVOTE_STR ? 'user-act-selected' : ''; ?>
<?php use models\Update; use components\UrlManager; use components\helpers\ArrayHelper; $categoryName = CW::$app->request->get('category'); $type = CW::$app->request->get('type'); if (!Update::isValidType($type) && \components\web\Controller::DEFAULT_ACTION === $action && App::DEFAULT_CONTROLLER === $controller) { $type = Update::TYPE_FRESH; } $csrfHash = isset($_SESSION['_csrf']) ? \components\Security::hash($_SESSION['_csrf']) : null; ?> <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="/js/app.js"></script> <script src="http://masonry.desandro.com/masonry.pkgd.js"></script> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <link href="/css/app.css" rel="stylesheet" type="text/css"> <link rel="shortcut icon" href="/images/logo.ico"> <title><?php echo $view->title; ?> </title> <?php foreach ($view->getLinks() as $link) { echo '<link ' . ArrayHelper::getArrayToString($link, ' ', function ($v, $k) { return "\"{$k}\"=\"{$v}\"";
public function save() { $imageDg = $this->image->getImage(); $ext = "jpeg"; $stmt = CW::$app->db->prepare("INSERT INTO `updates` (`user_id`, `description`, `is_gif`, `created_at`) VALUES (:userId, :description, " . ($this->image->isGif() ? 1 : 0) . ", :created_at)"); if (0 >= $stmt->execute([':userId' => \CW::$app->user->identity->id, ':description' => $this->title, ':created_at' => time()])) { return false; } $this->newUpdateId = CW::$app->db->getLastInsertedId(); if (!Update::addActivity($this->newUpdateId, CW::$app->user->identity->id, Update::ACTIVITY_TYPE_POST)) { return false; } $this->addTags(); $a = []; foreach ($this->categories as $category) { $c = (int) $category; $a[] = "({$this->newUpdateId}, {$c})"; } $categoryInsert = 'INSERT INTO `update_categories` (`update_id`, `category_id`) VALUES ' . implode(',', $a); if (0 >= CW::$app->db->executeUpdate($categoryInsert)) { return false; } $updateDir = CW::$app->params['sitePath'] . 'public_html/images/updates/' . $this->newUpdateId; if (!file_exists(CW::$app->params['sitePath'] . 'public_html/images/updates')) { mkdir(CW::$app->params['sitePath'] . 'public_html/images/updates'); } mkdir($updateDir); if ('image/gif' === $this->image->getExt()) { if (!file_exists(CW::$app->params['sitePath'] . 'public_html/images/tmp')) { mkdir(CW::$app->params['sitePath'] . 'public_html/images/tmp'); } $i = ImageHelper::loadImage($this->image->getImage()); if (!$i) { $this->addError('image', 'Invalid gif.'); return false; } imagejpeg(ImageHelper::scaleImage($i->getImage(), 500), CW::$app->params['sitePath'] . "public_html/images/updates/{$this->newUpdateId}/poster.jpeg"); $inpFile = CW::$app->params['sitePath'] . "public_html/images/tmp/{$this->newUpdateId}_medium.gif"; $outFileMedium = "{$updateDir}/medium"; foreach (['mp4', 'webm'] as $ext) { $mediumBytes = ImageHelper::resizeGif($this->image->getImage(), 500); file_put_contents($inpFile, $mediumBytes); ImageHelper::gifToVideo($inpFile, "{$outFileMedium}.{$ext}"); unlink($inpFile); } $imageType = Image::IMAGE_TYPE_VIDEO; $type = Image::TYPE_IMAGE; } else { $imageBig = ImageHelper::scaleImage($imageDg, Update::IMAGE_BIG_WIDTH, PHP_INT_MAX); $imageMedium = ImageHelper::scaleImage($imageDg, Update::IMAGE_MEDIUM_WIDTH, PHP_INT_MAX); $highImage = imagesy($imageMedium) > imagesx($imageMedium) + 150; if ($highImage) { $imageMedium = ImageHelper::cropImage(0, 0, imagesx($imageMedium), 300, $imageMedium); } $imageSmall = ImageHelper::scaleImage($imageDg, Update::IMAGE_SMALL_WIDTH, PHP_INT_MAX); if ($highImage) { $imageSmall = ImageHelper::cropImage(0, 0, imagesx($imageSmall), 150, $imageSmall); } imagejpeg($imageBig, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_BIG_WIDTH, $ext)); imagejpeg($imageMedium, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_MEDIUM_WIDTH, $ext)); imagejpeg($imageSmall, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_SMALL_WIDTH, $ext)); imagedestroy($imageBig); imagedestroy($imageMedium); imagedestroy($imageSmall); $imageType = Image::IMAGE_TYPE_NORMAL; $type = $highImage ? Image::TYPE_HIGH_IMAGE : Image::TYPE_IMAGE; } Image::create(['rel_id' => $this->newUpdateId, 'rel_type' => Image::REL_TYPE_UPDATE, 'type' => $type, 'image_type' => $imageType]); return true; }
upvote.setAttribute('style', voted ? 'margin-right: 7px; color: #09f;' : 'margin-right: 7px;'); upvote.setAttribute('title', voted ? 'unvote' : 'upvote'); } } }); upvote.setAttribute('class', 'fa fa-thumbs-up update-btn'); upvote.setAttribute('title', voted ? 'unvote' : 'upvote'); upvote.setAttribute('style', voted ? 'margin-right: 7px; color: #09f;' : 'margin-right: 7px;'); <?php } ?> }); var updateUrl = <?php echo json_encode(Update::getUpdateUrl($update['id'])); ?> ; function share(type) { App.share({ type : type, url : updateUrl, text : 'google-plus' === type ? <?php echo json_encode($update['description']); ?> : '' }); } </script>
public function doAjaxLoad() { $result = Update::getUpdates(CW::$app->request->get('page'), Category::getIdByName(CW::$app->request->get('category')), CW::$app->request->get('type'), CW::$app->request->get('category')); CW::$app->db->close(); return json_encode($result); }