public function loadModel($id) { $model = Favorites::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested user does not exist.'); } return $model; }
public function actionSetFavorites($id) { $model = Favorites::model()->find(" user_id='" . Yii::app()->user->id . "' and obj_id='" . $id . "' and obj_type='0'"); if ($model) { $model->delete(); echo 'false'; } else { $model = New Favorites(); $model->user_id = Yii::app()->user->id; $model->obj_id = $id; $model->obj_type = 0; $model->save(); echo 'true'; } }
public static function checkFavored($logid, $type) { if (Yii::app()->user->isGuest) { return false; } if (!is_numeric($logid)) { return false; } if (!isset($type) or !in_array($type, array('posts', 'attachments', 'poi', 'user', 'question', 'goods'))) { return false; } $attr = array('uid' => zmf::uid(), 'logid' => $logid, 'classify' => $type); if (Favorites::model()->findByAttributes($attr)) { return true; } else { return false; } }
public static function checkFavored($logid, $type, $uid = '') { if (!$uid) { $uid = zmf::uid(); } if (!$uid) { if (zmf::actionLimit('favorite-' . $type, $logid, 1, 86400, true, true)) { return true; } return false; } if (!is_numeric($logid)) { return false; } if (!isset($type) or !in_array($type, array('post'))) { return false; } $attr = array('uid' => $uid, 'logid' => $logid, 'classify' => $type); if (Favorites::model()->findByAttributes($attr)) { return true; } else { return false; } }
public static function favorite($code, $type, $from = 'web', $uid = '') { if (!$code || !$type) { return array('status' => 0, 'msg' => '数据不全,请核实'); } if (!in_array($type, array('post'))) { return array('status' => 0, 'msg' => '暂不允许的分类'); } if (is_numeric($code)) { $id = $code; } else { $codeArr = Posts::decode($code); if ($codeArr['type'] != $type || !is_numeric($codeArr['id']) || $codeArr['id'] < 1) { $this->jsonOutPut(0, '您所查看的内容不存在'); } $id = $codeArr['id']; } if (!$uid) { $uid = zmf::uid(); } if ($uid) { if (zmf::actionLimit('favorite-' . $type, $id)) { return array('status' => 0, 'msg' => '操作太频繁,请稍后再试'); } } else { //没有登录的访客点收藏时判断是否已收藏过 if (zmf::actionLimit('favorite-' . $type, $id, 1, 86400, true)) { return array('status' => 1, 'msg' => '已点赞', 'state' => 1); } $uid = 0; } $postInfo = Posts::model()->findByPk($id); if (!$postInfo || $postInfo['status'] != Posts::STATUS_PASSED) { return array('status' => 0, 'msg' => '文章不存在'); } $attr = array('uid' => $uid, 'logid' => $id, 'classify' => $type); $info = false; if ($uid) { $info = Favorites::model()->findByAttributes($attr); } if ($info) { if (Favorites::model()->deleteByPk($info['id'])) { if ($type == 'post') { Posts::updateCount($id, 'Posts', -1, 'favorite'); } return array('status' => 1, 'msg' => '取消点赞', 'state' => 3); } else { return array('status' => 0, 'msg' => '取消点赞失败', 'state' => 4); } } else { $attr['cTime'] = zmf::now(); $model = new Favorites(); $model->attributes = $attr; if ($model->save()) { if ($type == 'post') { Posts::updateCount($id, 'Posts', 1, 'favorite'); } //点赞后给对方发提醒 $_noticedata = array('uid' => $postInfo['uid'], 'authorid' => $uid, 'content' => "您的文章【{$postInfo['title']}】有了新的赞", 'new' => 1, 'type' => 'favorite', 'cTime' => zmf::now(), 'from_id' => $model->id, 'from_num' => 1); Notification::add($_noticedata); return array('status' => 1, 'msg' => '点赞成功', 'state' => 1); } else { return array('status' => 0, 'msg' => '点赞失败', 'state' => 2); } } }
/** * 统计用户的内容数 */ public static function getCounts($uid, $info = '') { if (!$uid) { return array(); } if ($info) { if (zmf::now() - $info['last_update'] <= 3600) { $data = array('posts' => $info['posts'], 'answers' => $info['answers'], 'tips' => $info['tips']); return $data; } } $data = array('posts' => Posts::model()->count('uid=:uid AND status=' . Posts::STATUS_PASSED, array(':uid' => $uid)), 'answers' => Answer::model()->count('uid=:uid AND status=' . Posts::STATUS_PASSED, array(':uid' => $uid)), 'tips' => PoiPost::model()->count('uid=:uid AND status=' . Posts::STATUS_PASSED, array(':uid' => $uid)), 'favors' => Favorites::model()->count('uid=:uid AND classify="user"', array(':uid' => $uid)), 'fans' => Favorites::model()->count('logid=:uid AND classify="user"', array(':uid' => $uid))); //将统计数据更新到用户统计表 $data['last_update'] = zmf::now(); Users::model()->updateByPk($uid, $data); return $data; }
public function actionAddFavorite() { $res = ''; if (isset($_POST)) { $f = Favorites::model()->count('uid=:uid and user_id=:user_id', array(':uid' => $_POST['uid'], ':user_id' => $_POST['user_id'])); if ($f > 0) { $res = 'Пользователь уже есть в Вашем списке Избранное'; } else { $m = new Favorites(); $m->uid = $_POST['uid']; $m->user_id = $_POST['user_id']; $user_name = Users::model()->findByPk($_POST['user_id'])->name; if ($m->save()) { $res = $user_name . ' добавлен в Избранное'; } else { $res = 'Произошла ошибка.'; } } //echo '<div class="apply">'.$res.'</div>'; Yii::app()->user->setFlash('add_favorite', '<span id="apply1">' . $res . '</span>'); return true; } }
public function isFavorite($bookId = null) { $favorites = Favorites::model()->findAllBySql(' SELECT f.* FROM db_favorites AS f WHERE f.user_id = ' . Yii::app()->user->userId . ' AND f.book_id = ' . $bookId); if ($favorites) { return true; } else { return false; } }
/** * 用户的关注与取消 * @param type $uid */ public function actionFavorUser($uid) { Users::checkPower('favorite'); $uid = zmf::filterInput($uid); if (!$uid) { $this->jsonOutPut(0, '数据不全'); } if ($uid == zmf::uid()) { $this->jsonOutPut(0, '不能关注自己哦'); } if (zmf::actionLimit('favorUser', $uid)) { $this->jsonOutPut(0, '操作太频繁,请稍后再试'); } $attr = array('uid' => zmf::uid(), 'logid' => $uid, 'classify' => 'user'); $info = Favorites::model()->findByAttributes($attr); if ($info) { if (Favorites::model()->deleteByPk($info['id'])) { $this->jsonOutPut(3, '取消关注成功'); } else { $this->jsonOutPut(0, '取消关注失败'); } } else { $attr['status'] = 1; $attr['cTime'] = zmf::now(); $attr['open'] = 1; $model = new Favorites(); $model->attributes = $attr; if ($model->save()) { $_noticedata = array('uid' => $uid, 'authorid' => zmf::uid(), 'content' => '您有了新的粉丝', 'new' => 1, 'type' => 'user', 'cTime' => zmf::now(), 'from_id' => $uid, 'from_num' => 1); Notification::add($_noticedata); $this->jsonOutPut(1, '已关注'); } else { $this->jsonOutPut(0, '关注失败'); } } }