|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace app\controllers; |
| 4 | + |
| 5 | +use Yii; |
| 6 | +use app\models\Post; |
| 7 | +use app\models\Tag; |
| 8 | +use app\models\Comment; |
| 9 | +use app\models\Category; |
| 10 | +use app\models\search\PostSearch; |
| 11 | +use yii\web\NotFoundHttpException; |
| 12 | +use yii\filters\VerbFilter; |
| 13 | +use yii\filters\AccessControl; |
| 14 | +use app\components\Common; |
| 15 | +use app\components\Controller; |
| 16 | +use yii\data\Pagination; |
| 17 | +use yii\helpers\Url; |
| 18 | +use yii\helpers\Html; |
| 19 | +use yii\helpers\Json; |
| 20 | +use yii\data\ActiveDataProvider; |
| 21 | + |
| 22 | +/** |
| 23 | + * PostController implements the CRUD actions for Post model. |
| 24 | + */ |
| 25 | +class PostController extends Controller |
| 26 | +{ |
| 27 | + public function behaviors() |
| 28 | + { |
| 29 | + return [ |
| 30 | + 'access' => [ |
| 31 | + 'class' => AccessControl::className(), |
| 32 | + 'rules' => [ |
| 33 | + [ |
| 34 | + 'actions' => ['index', 'view', 'error', 'like', 'comment-up', 'comment-down', 'comment-page'], |
| 35 | + 'allow' => true, |
| 36 | + ], |
| 37 | + [ |
| 38 | + 'actions' => ['create', 'update', 'upload-ajax', 'filemanager', 'create-img-ajax', 'suggest-tags', 'comment-ajax'], |
| 39 | + 'allow' => true, |
| 40 | + 'roles' => ['@'], |
| 41 | + ], |
| 42 | + ], |
| 43 | + ], |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + public function actions() |
| 48 | + { |
| 49 | + return [ |
| 50 | + 'autocomplete' => [ |
| 51 | + 'class' => 'app\components\AutocompleteAction', |
| 52 | + 'tableName' => Tag::tableName(), |
| 53 | + 'field' => 'name' |
| 54 | + ] |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + public function beforeAction($action) |
| 59 | + { |
| 60 | + if ($action->id=="create-img-ajax" || $action->id=="filemanager" || $action->id=="crop-ajax") { |
| 61 | + $this->enableCsrfValidation=false; |
| 62 | + } |
| 63 | + return parent::beforeAction($action); |
| 64 | + } |
| 65 | + |
| 66 | + public function actionIndex() |
| 67 | + { |
| 68 | + $queryParams = Yii::$app->request->getQueryParams(); |
| 69 | + $query = Post::find()->where(['status' => 1]); |
| 70 | + $category=""; |
| 71 | + if (isset($queryParams['id'])) { |
| 72 | + $category=Category::findOne($queryParams['id']); |
| 73 | + $query = $query->andWhere(['category_id' => $queryParams['id']]); |
| 74 | + } |
| 75 | + if (isset($queryParams['tag'])) { |
| 76 | + $query = $query->andWhere(['like', 'tags', $queryParams['tag']]); |
| 77 | + } |
| 78 | + $dataProvider = new ActiveDataProvider([ |
| 79 | + 'query' => $query, |
| 80 | + 'sort'=>['defaultOrder' => |
| 81 | + ['created_at' => SORT_DESC] |
| 82 | + ], |
| 83 | + 'pagination' => [ |
| 84 | + 'pageSize' => 20, |
| 85 | + ], |
| 86 | + ]); |
| 87 | + |
| 88 | + return $this->render('index', [ |
| 89 | + 'dataProvider' => $dataProvider, |
| 90 | + 'category' => $category |
| 91 | + ]); |
| 92 | + } |
| 93 | + |
| 94 | + public function actionCommentAjax($id) |
| 95 | + { |
| 96 | + $model = new Comment; |
| 97 | + |
| 98 | + if ($model->load(Yii::$app->request->post())) { |
| 99 | + if (Yii::$app->params['commentNeedApproval']) { |
| 100 | + $model->type=0; |
| 101 | + }else{ |
| 102 | + $model->type=1; |
| 103 | + } |
| 104 | + $model->status=1; |
| 105 | + $model->post_id=$id; |
| 106 | + if ($model->save()) { |
| 107 | + if (isset($model->user)) { |
| 108 | + $username=$model->user->username; |
| 109 | + $avatar=$model->user->avatar; |
| 110 | + $url=Url::to(['user/view', 'id' => $model->user_id]); |
| 111 | + }else{ |
| 112 | + $username=$model->author?$model->author:"游客"; |
| 113 | + $avatar=Yii::$app->homeUrl."upload/avatar/default.png"; |
| 114 | + $url="javascript:;"; |
| 115 | + } |
| 116 | + if ($model->parent_id) { |
| 117 | + $li='<li class="media">'; |
| 118 | + $endli='</li>'; |
| 119 | + }else{ |
| 120 | + $li=''; |
| 121 | + $endli=''; |
| 122 | + } |
| 123 | + echo Json::encode($li.'<div class="media"><a class="pull-left" href="'.$url.'"> <img class="media-object img-circle" alt="'.Html::encode($username).'" src="'.$avatar.'" style="width: 48px; height: 48px;"> </a> <div class="media-body"> <h4 class="media-heading"><a href="'.$url.'">'.Html::encode($username).'</a> • <span title="'.date("Y-m-d H:i:s", $model->create_time).'">'.Common::formatTime($model->create_time).'</span></h4> <p>'.Html::encode($model->content).'</p><div class="ops"><a href="" class="comment-up" data-id="'.$model->id.'"><i class="glyphicon glyphicon-thumbs-up"></i> (<span>0</span>)</a> <a href="" class="comment-down" data-id="'.$model->id.'"><i class="glyphicon glyphicon-thumbs-down"></i> (<span>0</span>)</a> <a href="" class="comment-reply" data-id="'.$model->id.'" data-postid="'.$id.'" title="回复"><i class="glyphicon glyphicon-share-alt"></i></a></div>'.$endli); |
| 124 | + }else{ |
| 125 | + echo "0"; |
| 126 | + } |
| 127 | + }else{ |
| 128 | + echo "0"; |
| 129 | + } |
| 130 | + Yii::$app->end(); |
| 131 | + } |
| 132 | + |
| 133 | + public function actionCommentPage($id) |
| 134 | + { |
| 135 | + return $this->renderPartial("_comments", ['id' => $id]); |
| 136 | + } |
| 137 | + |
| 138 | + public function actionLike($id) |
| 139 | + { |
| 140 | + if(Post::updateAllCounters(['likes' => 1],['id' => $id])){ |
| 141 | + echo "1"; |
| 142 | + }else{ |
| 143 | + echo "0"; |
| 144 | + } |
| 145 | + Yii::$app->end(); |
| 146 | + } |
| 147 | + |
| 148 | + public function actionCommentUp($id) |
| 149 | + { |
| 150 | + if(Comment::updateAllCounters(['up' => 1],['id' => $id])){ |
| 151 | + echo "1"; |
| 152 | + }else{ |
| 153 | + echo "0"; |
| 154 | + } |
| 155 | + Yii::$app->end(); |
| 156 | + } |
| 157 | + |
| 158 | + public function actionCommentDown($id) |
| 159 | + { |
| 160 | + if(Comment::updateAllCounters(['down' => 1],['id' => $id])){ |
| 161 | + echo "1"; |
| 162 | + }else{ |
| 163 | + echo "0"; |
| 164 | + } |
| 165 | + Yii::$app->end(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Displays a single Post model. |
| 170 | + * @param integer $id |
| 171 | + * @return mixed |
| 172 | + */ |
| 173 | + public function actionView($id) |
| 174 | + { |
| 175 | + $model = $this->findModel($id); |
| 176 | + if (!empty($model->url)) { |
| 177 | + return $this->redirect($model->url); |
| 178 | + } |
| 179 | + $model->updateCounters(['views' => 1]); |
| 180 | + $content=explode(Yii::$app->params['pagebreakHtml'], $model->content); |
| 181 | + $count=count($content); |
| 182 | + $pages = new Pagination(['totalCount' => $count, 'pageSize' => 1]); |
| 183 | + $model->content=$content[$pages->page]; |
| 184 | + return $this->render('view', [ |
| 185 | + 'model' => $model, |
| 186 | + 'pages' => $pages |
| 187 | + ]); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Creates a new Post model. |
| 192 | + * If creation is successful, the browser will be redirected to the 'view' page. |
| 193 | + * @return mixed |
| 194 | + */ |
| 195 | + public function actionCreate() |
| 196 | + { |
| 197 | + $model = new Post; |
| 198 | + |
| 199 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { |
| 200 | + return $this->redirect(['view', 'id' => $model->id]); |
| 201 | + } else { |
| 202 | + return $this->render('create', [ |
| 203 | + 'model' => $model, |
| 204 | + ]); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Updates an existing Post model. |
| 210 | + * If update is successful, the browser will be redirected to the 'view' page. |
| 211 | + * @param integer $id |
| 212 | + * @return mixed |
| 213 | + */ |
| 214 | + public function actionUpdate($id) |
| 215 | + { |
| 216 | + $model = $this->findModel($id); |
| 217 | + |
| 218 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { |
| 219 | + return $this->redirect(['view', 'id' => $model->id]); |
| 220 | + } else { |
| 221 | + return $this->render('update', [ |
| 222 | + 'model' => $model, |
| 223 | + ]); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Suggests tags based on the current user input. |
| 229 | + * This is called via AJAX when the user is entering the tags input. |
| 230 | + */ |
| 231 | + public function actionSuggestTags() |
| 232 | + { |
| 233 | + if(isset($_GET['term']) && ($keyword = trim($_GET['term'])) !== '') |
| 234 | + { |
| 235 | + $model = new Tag; |
| 236 | + $tags = $model->suggestTags($keyword); |
| 237 | + if($tags !== array()){ |
| 238 | + echo Json::encode($tags); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Finds the Post model based on its primary key value. |
| 245 | + * If the model is not found, a 404 HTTP exception will be thrown. |
| 246 | + * @param integer $id |
| 247 | + * @return Post the loaded model |
| 248 | + * @throws NotFoundHttpException if the model cannot be found |
| 249 | + */ |
| 250 | + protected function findModel($id) |
| 251 | + { |
| 252 | + if (($model = Post::findOne($id)) !== null) { |
| 253 | + return $model; |
| 254 | + } else { |
| 255 | + throw new NotFoundHttpException('The requested page does not exist.'); |
| 256 | + } |
| 257 | + } |
| 258 | +} |
0 commit comments