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)); }
/** * 更新统计数据 * * @param array $contents * @param string $count_field */ private function _updateLinkData($contents = array(), $count_field = '') { $model_type = new ModelType(); var_dump($contents); foreach ($contents as $c) { $type = $model_type->findByPk($c->type); if ($type && $count_field) { $type_name = ucfirst($type->type_key); $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)); } } } }
/** * 所有栏目分类 * @param number $parentid * @param number $level * @param array $array * @param number $add * @param string $repeat * @return Ambigous <multitype:, multitype:multitype:number unknown string > */ public static function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = ' ') { $modelType = new ModelType(); $str_repeat = ''; if ($level) { for ($j = 0; $j < $level; $j++) { $str_repeat .= $repeat; } } $newarray = array(); $temparray = array(); foreach ((array) $array as $v) { if ($v['parent_id'] == $parentid) { $v['level'] = $level; $v['str_repeat'] = $str_repeat; //模型标示 $typeinfo = $modelType->findByPk($v['type']); $v['type_key'] = $typeinfo->type_key; $newarray[] = $v; $temparray = self::get($v['id'], $array, $level + $add); if ($temparray) { $newarray = array_merge($newarray, $temparray); } } } return $newarray; }