Пример #1
0
 public function actionIndex()
 {
     $product_id = intval($_REQUEST['id']);
     $pInfo = Product::model()->getProductInfoById($product_id);
     if (empty($pInfo)) {
         $this->redirect('/?from=no_goods');
         //跳转到首页
     }
     $brandInfo = '';
     if ($pInfo['brand_id']) {
         $brandInfo = Brand::model()->findByPk($pInfo['brand_id']);
     }
     $stock = Product::model()->getProductStock($product_id, $pInfo['is_multiple']);
     $attrList = ProductAttributes::model()->getProductAttrNameList();
     $extendAttrList = ProductExtend::model()->getProductExtendAttrs($product_id);
     $is_like = Like::model()->getLikeStatus($this->user_id, $product_id);
     $cake = Category::model()->getCakeLine($pInfo['cat_id']);
     //获取商品的面包屑
     $viewData = array();
     $viewData['pInfo'] = $pInfo;
     $viewData['brandInfo'] = $brandInfo;
     $viewData['is_like'] = $is_like;
     $viewData['cake'] = $cake;
     $viewData['stock'] = $stock;
     $viewData['attrList'] = $attrList;
     $viewData['extendAttrList'] = $extendAttrList;
     $this->render('item/index', $viewData);
 }
Пример #2
0
 public function liked($id)
 {
     $result = Like::model()->findByAttributes(array('id' => $id));
     $result->status = 0;
     $post = Posts::model()->findByAttributes(array('id' => $result->postId));
     $post->like_num--;
     if ($result->save() && $post->save()) {
         echo CJSON::encode(array('code' => 200, 'mes' => 'success', 'data' => array('postId' => $result->postId)));
     }
 }
Пример #3
0
 public function getpost($id)
 {
     $result = Yii::app()->db->createCommand()->setText('SELECT a.*,b.nickName,b.portrait,b.title,c.name as companyName,d.name as location from post a  LEFT JOIN user b on b.id = a.userId left join company c on c.id = b.company left join hub d on d.id = b.location where a.status=1 and a.id=' . $id)->queryRow();
     $item = Like::model()->findByAttributes(array('userId' => Yii::app()->user->id, 'postId' => $result['id'], 'status' => 1));
     $result['islike'] = $item ? 1 : 0;
     $result['likeId'] = $item->id;
     $likelist = Yii::app()->db->createCommand()->setText('select b.portrait,b.nickName,b.id as userId,b.title,c.name as companyName,d.name as locationName from `like` a left join user b on a.userId=b.id left join company c on c.id = b.company left join hub d on d.id=b.location where a.status=1 and a.postId=' . $id)->queryAll();
     $commentlist = Yii::app()->db->createCommand()->setText('select b.id as userId,a.createTime,a.content,b.nickName,b.portrait,b.title,c.name as companyName,d.name as location from comment a left join user b on a.userId=b.id left join company c on b.company=c.id left join hub d on d.id=b.location where  a.status=1 and a.postId=' . $id)->queryAll();
     return array('code' => 200, 'mes' => 'success', 'data' => array('post' => $result, 'likelist' => $likelist, 'commentlist' => $commentlist));
 }
Пример #4
0
 /**
  * Like Count for specifc model
  */
 public static function GetLikes($objectModel, $objectId)
 {
     $cacheId = "likes_" . $objectModel . "_" . $objectId;
     $cacheValue = Yii::app()->cache->get($cacheId);
     if ($cacheValue === false) {
         $newCacheValue = Like::model()->findAllByAttributes(array('object_model' => $objectModel, 'object_id' => $objectId));
         Yii::app()->cache->set($cacheId, $newCacheValue, HSetting::Get('expireTime', 'cache'));
         return $newCacheValue;
     } else {
         return $cacheValue;
     }
 }
Пример #5
0
 /**
  * On run of integrity check command, validate all module data
  *
  * @param type $event
  */
 public static function onIntegrityCheck($event)
 {
     $integrityChecker = $event->sender;
     $integrityChecker->showTestHeadline("Validating Like Module (" . Like::model()->count() . " entries)");
     foreach (Like::model()->findAll() as $l) {
         if ($l->source === null) {
             $integrityChecker->showFix("Deleting like id " . $l->id . " without existing target!");
             if (!$integrityChecker->simulate) {
                 $l->delete();
             }
         }
     }
 }
Пример #6
0
 /**
  * Returns a user list which contains all users who likes it
  */
 public function actionUserList()
 {
     $page = (int) Yii::app()->request->getParam('page', 1);
     $total = Like::model()->count('object_model=:omodel AND object_id=:oid', array(':omodel' => $this->contentModel, 'oid' => $this->contentId));
     $usersPerPage = HSetting::Get('paginationSize');
     $sql = "SELECT u.* FROM `like` l " . "LEFT JOIN user u ON l.created_by = u.id " . "WHERE l.object_model=:omodel AND l.object_id=:oid AND u.status=" . User::STATUS_ENABLED . " " . "ORDER BY l.created_at DESC " . "LIMIT " . intval(($page - 1) * $usersPerPage) . "," . intval($usersPerPage);
     $params = array(':omodel' => $this->contentModel, ':oid' => $this->contentId);
     $pagination = new CPagination($total);
     $pagination->setPageSize($usersPerPage);
     $users = User::model()->findAllBySql($sql, $params);
     $output = $this->renderPartial('application.modules_core.user.views._listUsers', array('title' => Yii::t('LikeModule.controllers_LikeController', "<strong>Users</strong> who like this"), 'users' => $users, 'pagination' => $pagination), true);
     Yii::app()->clientScript->render($output);
     echo $output;
     Yii::app()->end();
 }
Пример #7
0
 /**
  * Fire this notification on given like object
  *
  * @param type $comment
  */
 public static function fire($like)
 {
     // Get Users which are also likes this model
     $userIds = array();
     $otherLikes = Like::model()->findAllByAttributes(array('object_model' => $like->object_model, 'object_id' => $like->object_id));
     foreach ($otherLikes as $otherLike) {
         if ($like->created_by == $otherLike->created_by) {
             continue;
         }
         // This user will also gets a "New Like" notification
         if ($like->source->created_by == $otherLike->created_by) {
             continue;
         }
         $userIds[] = $otherLike->created_by;
     }
     $userIds = array_unique($userIds);
     // Determine Space Id if exists
     $spaceId = "";
     if ($like->content->container instanceof Space) {
         $spaceId = $like->content->container->id;
     }
     // Write new Notification for them
     foreach ($userIds as $userId) {
         $notification = new Notification();
         $notification->class = "AlsoLikesNotification";
         $notification->user_id = $userId;
         $notification->space_id = $spaceId;
         // Which object throws this notification?
         $notification->source_object_model = 'Like';
         $notification->source_object_id = $like->id;
         // Which object gets this notifiction
         $notification->target_object_model = $like->object_model;
         $notification->target_object_id = $like->object_id;
         $notification->save();
     }
 }
Пример #8
0
 /**
  * Updates the involved Users of this object.
  * Currently this will be execution always after saving, maybe find a better way.
  *
  * Fast Hack!11!
  *
  * ToDo: - Make it more flexible!
  *       - Make it faster!
  *       - Missing Users which likes a comment
  */
 public function updateInvolvedUsers()
 {
     // Collect User Ids
     $foundUsersIds = array();
     $foundUsersIds[] = $this->created_by;
     if ($this->object_model != "Activity") {
         $comments = Comment::model()->findAllByAttributes(array('object_model' => $this->object_model, 'object_id' => $this->object_id));
         foreach ($comments as $comment) {
             $foundUsersIds[] = $comment->created_by;
             // Comment Likes
             $likes = Like::model()->findAllByAttributes(array('object_model' => 'Comment', 'object_id' => $comment->id));
             foreach ($likes as $like) {
                 $foundUsersIds[] = $like->created_by;
             }
         }
         $likes = Like::model()->findAllByAttributes(array('object_model' => $this->object_model, 'object_id' => $this->object_id));
         foreach ($likes as $like) {
             $foundUsersIds[] = $like->created_by;
         }
     }
     UserContent::model()->deleteAllByAttributes(array('object_model' => $this->object_model, 'object_id' => $this->object_id));
     // Add currently involved users
     foreach (array_unique($foundUsersIds) as $userId) {
         $userContent = new UserContent();
         $userContent->object_model = $this->object_model;
         $userContent->object_id = $this->object_id;
         $userContent->user_id = $userId;
         $userContent->save();
     }
     // Rewrite!
 }
Пример #9
0
 public function checkIfPostIsLiked($post_id, $user_id)
 {
     $check = Like::model()->findByAttributes(array('post_id' => $post_id, 'from' => $user_id, 'status' => 1));
     if ($check) {
         return TRUE;
     }
     return FALSE;
 }
Пример #10
0
 /**
  * 添加喜欢.
  */
 public function actionAddLike()
 {
     $result = array('status' => 1, 'msg' => '添加喜欢成功!');
     $request = $_REQUEST;
     $product_id = trim($request['id']);
     try {
         $userId = $this->user_id;
         if (empty($userId)) {
             throw new exception('用户未登陆!', 2);
         }
         Like::model()->addLike($userId, $product_id);
     } catch (exception $e) {
         $result['status'] = $e->getCode();
         $result['msg'] = '添加喜欢失败!';
     }
     exit(json_encode($result));
 }
Пример #11
0
 /**
  * 处理分类商品的喜欢状态.
  * @param $userId
  * @param $list
  * @return string
  */
 public function getCategoryProductLikeStatus($userId, $list)
 {
     if (empty($userId) || empty($list)) {
         return '';
     }
     $product_ids = array();
     foreach ($list as $row) {
         array_push($product_ids, $row['id']);
     }
     $likeList = Like::model()->findAllByAttributes(array('user_id' => $userId, 'product_id' => $product_ids));
     if (empty($likeList)) {
         return $list;
     }
     foreach ($likeList as $row) {
         if (isset($list[$row->product_id])) {
             $list[$row->product_id]['is_like'] = 1;
         }
     }
     return $list;
 }
 /**
  * Count all likes a content object has received. Do not count likes 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 Like[]
  */
 protected function getLikesFromContent(Content $content, $userId, $cacheId, $forceUpdate = false)
 {
     $likes = Yii::app()->cache->get($cacheId);
     if ($likes === false || $forceUpdate === true) {
         $objectModel = strtolower($content->object_model);
         $likes = array();
         // comments have to be handled otherwise
         if (strcmp($objectModel, 'comment') == 0) {
             return array();
         }
         try {
             $criteria = new CDbCriteria();
             $criteria->alias = 'l';
             $criteria->join = 'LEFT JOIN ' . $objectModel . ' p ON l.object_id = p.id';
             $criteria->join .= ' LEFT JOIN content ct ON p.id=ct.object_id';
             $criteria->condition = 'ct.id=:contentId AND l.created_by!=:userId AND ct.created_by=:userId AND l.object_model=:objectModel AND ct.object_model=:objectModel';
             $criteria->params = array(':contentId' => $content->id, ':objectModel' => $objectModel, ':userId' => $userId);
             $likes = Like::model()->findAll($criteria);
             Yii::app()->cache->set($cacheId, $likes, ReputationBase::CACHE_TIME_SECONDS);
         } catch (Exception $e) {
             Yii::trace('Couldn\'t fetch likes from object model: ' . $objectModel);
         }
     }
     return $likes;
 }
 /**
  * Get all likes that a user got for a comment he made
  * When a user likes his own comment it will not be counted
  *
  * @param $userId
  * @param Content $content
  * @param $forceUpdate : Ignore cache
  * @return int
  */
 public function GetCommentsGeneratedByUserLikedByOthers($userId, Content $content, $forceUpdate)
 {
     $cacheId = 'comments_liked_cache_' . $userId . '_' . $content->id;
     $commentsLiked = Yii::app()->cache->get($cacheId);
     if ($commentsLiked === false || $forceUpdate === true) {
         $object_model = strtolower($content->object_model);
         $commentsLiked = array();
         try {
             $criteria = new CDbCriteria();
             $criteria->alias = 'l';
             $criteria->join = 'LEFT JOIN comment c ON c.id=l.object_id';
             $criteria->join .= ' LEFT JOIN ' . $object_model . ' o ON c.object_id = o.id';
             $criteria->join .= ' LEFT JOIN content ct ON o.id=ct.object_id';
             $criteria->condition = 'l.object_model=\'comment\' AND l.created_by!=:userId AND ct.id=:contentId AND c.created_by=:userId AND c.object_model=ct.object_model';
             $criteria->params = array(':contentId' => $content->id, ':userId' => $userId);
             $commentsLiked = Like::model()->findAll($criteria);
             Yii::app()->cache->set($cacheId, $commentsLiked, ReputationBase::CACHE_TIME_SECONDS);
         } catch (Exception $e) {
             Yii::trace('Couldn\'t count generated comments from object model: ' . $object_model);
         }
     }
     return $commentsLiked;
 }
Пример #14
0
 public function getLikeStatus($user_id, $product_id)
 {
     if (empty($product_id) || empty($user_id)) {
         return false;
     }
     $likeInfo = Like::model()->findByAttributes(array('user_id' => $user_id, 'product_id' => $product_id));
     if (empty($likeInfo)) {
         return false;
     }
     return true;
 }
Пример #15
0
 /**
  * Determines if an item is liked or not
  * @return boolean
  */
 public function isLiked()
 {
     $like = Like::model()->findByAttributes(array('user_id' => Yii::app()->user->id, 'share_id' => $this->id));
     return $like != NULL;
 }