public function run() { $controller = $this->getController(); $this->_setting = $controller->_setting; $this->_stylePath = $controller->_stylePath; $this->_static_public = $controller->_static_public; $controller->_seoTitle = Yii::t('common', 'User Center') . ' - ' . Yii::t('common', 'Attention Manage') . ' - ' . $this->_setting['site_name']; //加载css,js Yii::app()->clientScript->registerCssFile($this->_stylePath . "/css/user.css"); Yii::app()->clientScript->registerScriptFile($this->_static_public . "/js/jquery/jquery.js"); //关注列表 $attention_mod = new Attention(); $uid = Yii::app()->user->id; $criteria = new CDbCriteria(); $criteria->condition = 't.user_id=' . $uid; $criteria->order = 't.id DESC'; //分页 $count = $attention_mod->count($criteria); $pages = new CPagination($count); $pages->pageSize = 15; $criteria->limit = $pages->pageSize; $criteria->offset = $pages->currentPage * $pages->pageSize; $datalist = $attention_mod->findAll($criteria); $controller->render('my_attention', array('datalist' => $datalist, 'pages' => $pages)); }
public static function saveNew(Notice $notice, Profile $profile, $reason = null) { $att = new Attention(); $att->notice_id = $notice->getID(); $att->profile_id = $profile->getID(); $att->reason = $reason; $att->created = common_sql_now(); $result = $att->insert(); if ($result === false) { throw new Exception('Could not saveNew in Attention'); } return $att; }
function handle($data) { // JSON object with Twitter data $status = $data['status']; // Twitter user ID this incoming data belongs to. $receiver = $data['for_user']; $importer = new TwitterImport(); $notice = $importer->importStatus($status); if ($notice instanceof Notice) { try { $flink = Foreign_link::getByForeignID($receiver, TWITTER_SERVICE); common_log(LOG_DEBUG, "TweetInQueueHandler - Got flink so add notice " . $notice->id . " to attentions for user " . $flink->user_id); try { Attention::saveNew($notice, $flink->getProfile()); } catch (Exception $e) { // Log the exception, but make sure we don't bail out, we // still have a queue item to remove here-after. common_log(LOG_ERR, "Failed adding notice {$notice->id} to attentions for user {$flink->user_id}: " . $e->getMessage()); } } catch (NoResultException $e) { common_log(LOG_DEBUG, "TweetInQueueHandler - No flink found for foreign user " . $receiver); } } return true; }
public static function saveNew(Notice $notice, Profile $target, $reason = null) { try { $att = Attention::getByKeys(['notice_id' => $notice->getID(), 'profile_id' => $target->getID()]); throw new AlreadyFulfilledException('Attention already exists with reason: ' . var_export($att->reason, true)); } catch (NoResultException $e) { $att = new Attention(); $att->notice_id = $notice->getID(); $att->profile_id = $target->getID(); $att->reason = $reason; $att->created = common_sql_now(); $result = $att->insert(); if ($result === false) { throw new Exception('Failed Attention::saveNew for notice id==' . $notice->getID() . ' target id==' . $target->getID() . ', reason=="' . $reason . '"'); } } return $att; }
public function createOrDelete($id) { $topic = Topic::find($id); if (Attention::isUserAttentedTopic(Auth::user(), $topic)) { $message = lang('Successfully remove attention.'); Auth::user()->attentTopics()->detach($topic->id); } else { $message = lang('Successfully_attention'); Auth::user()->attentTopics()->attach($topic->id); Notification::notify('topic_attent', Auth::user(), $topic->user, $topic); } Flash::success($message); return Redirect::route('topics.show', $topic->id); }
/** * ajax关注操作 * * @return string */ private function _opAttention() { $uid = Yii::app()->user->id; $ret = array(); if (!$uid) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'You Need Login')); return $ret; } $postid = Yii::app()->request->getParam('id'); $post = Post::model()->findByPk($postid); if (!$post) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'Collect Failed')); return $ret; } $attention_mod = new Attention(); //判断是否已经关注了 $type_id = $this->controller->_type_ids['post']; $exist_attention = $attention_mod->find('user_id=:uid AND cid=:cid AND type=:type_id', array(':uid' => $uid, ':cid' => $postid, ':type_id' => $type_id)); if ($exist_attention) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'You Have Attented it')); } else { //关注 $attention_mod->cid = $postid; $attention_mod->title = $post->title; $attention_mod->user_id = $uid; $attention_mod->url = Yii::app()->request->hostinfo . $this->controller->createUrl('post/view', array('id' => $postid)); $attention_mod->type = $type_id; $attention_mod->create_time = time(); if ($attention_mod->save()) { $post->updateCounters(array('attention_count' => 1), 'id=:id', array('id' => $postid)); $ret = array('state' => 'success', 'message' => Yii::t('common', 'Attent Success'), 'count' => $post->attention_count + 1); } else { $ret = array('state' => 'error', 'message' => Yii::t('common', 'Attent Failed')); } } return $ret; }
/** * Handle distribution of a notice after we've saved it: * @li add to local recipient inboxes * @li send email notifications to local @-reply targets * @li run final EndNoticeSave plugin events * @li put any remaining post-processing into the queues * * If this function indicates failure, a warning will be logged * and the item is placed back in the queue to be re-run. * * @param Notice $notice * @return boolean true on success, false on failure */ public function handle(Notice $notice) { // We have to manually add attentions to non-profile subs and non-mentions $ptAtts = $notice->getAttentionsFromProfileTags(); foreach (array_keys($ptAtts) as $profile_id) { $profile = Profile::getKV('id', $profile_id); if ($profile instanceof Profile) { try { common_debug('Adding Attention for ' . $notice->getID() . ' profile ' . $profile->getID()); Attention::saveNew($notice, $profile); } catch (Exception $e) { $this->logit($notice, $e); } } } try { $notice->sendReplyNotifications(); } catch (Exception $e) { $this->logit($notice, $e); } try { Event::handle('EndNoticeDistribute', array($notice)); } catch (Exception $e) { $this->logit($notice, $e); } try { Event::handle('EndNoticeSave', array($notice)); } catch (Exception $e) { $this->logit($notice, $e); } try { // Enqueue for other handlers common_enqueue_notice($notice); } catch (Exception $e) { $this->logit($notice, $e); } return true; }
public static function isUserAttentedTopic(User $user, Topic $topic) { return Attention::where('user_id', $user->id)->where('topic_id', $topic->id)->first(); }
function clearAttentions() { $att = new Attention(); $att->notice_id = $this->getID(); if ($att->find()) { while ($att->fetch()) { // Can't do delete() on the object directly since it won't remove all of it $other = clone $att; $other->delete(); } } }
public function run() { $controller = $this->getController(); $op = Yii::app()->request->getParam('op'); $ids = Yii::app()->request->getParam('id'); $uid = Yii::app()->user->id; $res = false; //操作状态 $count = true; //是否需要统计 $count_field = ''; //统计数据的字段名 $action = ''; //要返回的action名 if (!$uid) { $message = Yii::t('common', 'You Need Login'); } else { if (!$ids) { $message = Yii::t('common', 'Operation Failed'); } else { switch ($op) { case 'collect': $collect_mod = new Collect(); //检测是否是自己收藏的 foreach ((array) $ids as $k => $id) { $tmp = $collect_mod->findByPk($id); if ($tmp->user_id != $uid) { unset($ids[$k]); } else { $content[] = $collect_mod->findByPk($id); } } $collect_mod->deleteByPk($ids); $res = true; $count_field = 'favorite_count'; $action = 'mycollect'; break; case 'attention': $attention_mod = new Attention(); //检测是否是自己关注的 foreach ((array) $ids as $k => $id) { $tmp = $attention_mod->findByPk($id); if ($tmp->user_id != $uid) { unset($ids[$k]); } else { $content[] = $attention_mod->findByPk($id); } } $attention_mod->deleteByPk($ids); $res = true; $count_field = 'attention_count'; $action = 'myattention'; break; case 'friend': $friend_mod = new Friend(); //检测是否是自己的好友 foreach ((array) $ids as $k => $id) { $tmp = $friend_mod->findByPk($id); if ($tmp->uid1 != $uid && $tmp->uid2 != $uid) { unset($ids[$k]); } } $friend_mod->deleteByPk($ids); $res = true; $count = false; $action = 'myfriends'; break; default: break; } if ($res) { $message = Yii::t('common', 'Cancel Success'); if ($count && $content) { //减少统计数据 $model_type = new ModelType(); foreach ($content as $c) { $type = $model_type->findByPk($c->type); $type_name = ucfirst($type->type_key); if ($type_name && $c && $count_field) { $content_mod = new $type_name(); $cur_post = $content_mod->findByPk($c->cid); if ($cur_post->{$count_field} > 0) { $content_mod->updateCounters(array($count_field => -1), 'id=:id', array('id' => $c->cid)); } } } } } else { $message = Yii::t('common', 'Operation Failed'); } } } //用setFlash提示信息(类似alert) $controller->layout = false; Yii::app()->user->setFlash($res ? 'success' : 'error', $message); $controller->redirect($controller->createUrl('user/' . $action)); }
/** * ajax操作 (收藏、关注) * */ public function actionAjax() { $uid = Yii::app()->user->id; $ret = array(); if (!$uid) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'You Need Login')); } else { $act = $this->_request->getParam('act'); $postid = $this->_request->getParam('id'); $post = Post::model()->findByPk($postid); $type_id = $this->_type_ids['post']; if (!$post) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'Collect Failed')); } else { $attention_mod = new Attention(); $collect_mod = new Collect(); switch ($act) { case 'attention': //判断是否已经关注了 $exist_attention = $attention_mod->find('user_id=:uid AND cid=:cid AND type=:type_id', array(':uid' => $uid, ':cid' => $postid, ':type_id' => $type_id)); if ($exist_attention) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'You Have Attented it')); } else { //关注 $attention_mod->cid = $postid; $attention_mod->title = $post->title; $attention_mod->user_id = $uid; $attention_mod->url = $this->_request->hostinfo . $this->createUrl('post/view', array('id' => $postid)); $attention_mod->type = $type_id; $attention_mod->create_time = time(); if ($attention_mod->save()) { $post->updateCounters(array('attention_count' => 1), 'id=:id', array('id' => $postid)); $ret = array('state' => 'success', 'message' => Yii::t('common', 'Attent Success'), 'count' => $post->attention_count + 1); } else { $ret = array('state' => 'error', 'message' => Yii::t('common', 'Attent Failed')); } } break; case 'collect': //判断是否已经收藏 $exist_collect = $collect_mod->find('user_id=:uid AND cid=:cid AND type=:type_id', array(':uid' => $uid, ':cid' => $postid, ':type_id' => $type_id)); if ($exist_collect) { $ret = array('state' => 'error', 'message' => Yii::t('common', 'You Have Collected it')); } else { //收藏 $collect_mod->cid = $postid; $collect_mod->title = $post->title; $collect_mod->user_id = $uid; $collect_mod->url = $this->_request->hostinfo . $this->createUrl('post/view', array('id' => $postid)); $collect_mod->type = $type_id; $collect_mod->create_time = time(); if ($collect_mod->save()) { $post->updateCounters(array('favorite_count' => 1), 'id=:id', array('id' => $postid)); $ret = array('state' => 'success', 'message' => Yii::t('common', 'Collect Success'), 'count' => $post->favorite_count + 1); } else { $ret = array('state' => 'error', 'message' => Yii::t('common', 'Collect Failed')); } } break; default: $ret = array('state' => 'error', 'message' => Yii::t('common', 'Operation Failed')); break; } } } exit(CJSON::encode($ret)); }
public static function unfollow($from, $to) { $attention = Attention::where('u_fans_id', '=', $from->u_id)->where('u_id', '=', $to->u_id)->first(); if (!isset($attention->a_id)) { return true; } if (!$attention->delete()) { throw new Exception("取消关注失败", 1); } $from->u_following_count -= 1; if ($from->u_following_count <= 0) { $from->u_following_count = 0; } $to->u_follower_count -= 1; if ($to->u_follower_count <= 0) { $to->u_follower_count = 0; } if ($from->save() && $to->save()) { return true; } else { throw new Exception("取消关注失败", 1); } }