public function run() { $object_id = $this->model->id; $model_id = get_class($this->model); $count = Favorite::model()->countByAttributes(array('object_id' => $object_id, 'model_id' => $model_id)); $added = false; if (!Yii::app()->user->isGuest) { $added = Favorite::model()->exists("user_id = " . Yii::app()->user->id . " AND object_id = {$object_id} AND model_id = '{$model_id}'"); } $this->render('FavoriteWidget', array('model_id' => $model_id, 'object_id' => $object_id, 'model' => $this->model, 'count' => $count, 'added' => $added, 'guest_msg' => t('Только зарегистрированные пользователи могут добавлять посты в избранное'), 'user_msg' => t('Добавить в избранное'), 'count_msg' => t('Количество пользователей, добавивших пост в избранное'))); }
/** * Remove entry in favorite table */ public function actionUnFavorite() { $this->forcePostRequest(); if (!Yii::app()->user->isGuest) { $attributes = array('object_model' => $this->contentModel, 'object_id' => $this->contentId, 'created_by' => Yii::app()->user->id); $favorite = Favorite::model()->findByAttributes($attributes); if ($favorite != null) { $favorite->delete(); } } $this->actionShowFavorites(); }
/** * Favorites Count for specific model */ public static function GetFavorites($objectModel, $objectId) { $cacheId = "favorites_" . $objectModel . "_" . $objectId; $cacheValue = Yii::app()->cache->get($cacheId); if ($cacheValue === false) { $newCacheValue = Favorite::model()->findAllByAttributes(array('object_model' => $objectModel, 'object_id' => $objectId)); Yii::app()->cache->set($cacheId, $newCacheValue, HSetting::Get('expireTime', 'cache')); return $newCacheValue; } else { return $cacheValue; } }
public function actionCreateOrDelete() { if (!isset($_POST['Favorite'])) { $this->badRequest(); } $response = array(); $attributes = array('user_id' => Yii::app()->user->id, 'object_id' => $_POST['Favorite']['object_id'], 'model_id' => $_POST['Favorite']['model_id']); $favorite = Favorite::model()->findByAttributes($attributes); if ($favorite) { $response['action'] = 'delete'; Favorite::model()->deleteAllByAttributes($attributes); } else { $response['action'] = 'create'; $favorite = new Favorite(); $favorite->attributes = $attributes; if (!$favorite->save()) { $response['errors'] = $favorite->errors_flat_array; } } $response['count'] = Favorite::model()->countByAttributes(array('object_id' => $favorite->object_id, 'model_id' => $favorite->model_id)); echo CJSON::encode($response); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Favorite::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
/** * get Favorite model by model name and Id */ public static function getFavoriteByModelNameAndId($model, $id) { return Favorite::model()->find('modelName = :model and modelId = :id', array(':id' => $id, ':model' => $model)); }
/** * Count all favorites a content object has received. Do not count favorites from user who created this post * * @param Content $content : The content object * @param $userId : The user id * @param $cacheId : The cache id * @param bool $forceUpdate : true if cache should be ignored * @return Favorite[] */ protected function getFavoritesFromContent(Content $content, $userId, $cacheId, $forceUpdate = false) { $favorites = Yii::app()->cache->get($cacheId); if ($favorites === false || $forceUpdate === true) { $objectModel = strtolower($content->object_model); $favorites = array(); // not possible to favorite comments atm if (strcmp($objectModel, 'comment') == 0) { return array(); } try { $criteria = new CDbCriteria(); $criteria->alias = 'f'; $criteria->join = 'LEFT JOIN ' . $objectModel . ' p ON f.object_id = p.id'; $criteria->join .= ' LEFT JOIN content ct ON p.id=ct.object_id'; $criteria->condition = 'ct.id=:contentId AND f.created_by!=:userId AND ct.created_by=:userId AND f.object_model=:objectModel AND ct.object_model=:objectModel'; $criteria->params = array(':contentId' => $content->id, ':objectModel' => $objectModel, ':userId' => $userId); $favorites = Favorite::model()->findAll($criteria); Yii::app()->cache->set($cacheId, $favorites, ReputationBase::CACHE_TIME_SECONDS); } catch (Exception $e) { Yii::trace('Couldn\'t fetch favorites from object model: ' . $objectModel); } } return $favorites; }
/** * On run of integrity check command, validate all module data * * @param type $event */ public static function onIntegrityCheck($event) { $integrityChecker = $event->sender; $integrityChecker->showTestHeadline("Validating Favorite Module (" . Favorite::model()->count() . " entries)"); }