123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace manager\controllers;
- use manager\models\Comments;
- use app\models\CommentsConf;
- use yii\data\ActiveDataProvider;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- /**
- * CommentsController implements the CRUD actions for Comments model.
- */
- class CommentsController extends BaseController
- {
- /**
- * @inheritDoc
- */
- public function behaviors()
- {
- return array_merge(
- parent::behaviors(),
- [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ]
- );
- }
- /**
- * Lists all Comments models.
- *
- * @return string
- */
- public function actionIndex()
- {
- $dataProvider = new ActiveDataProvider([
- 'query' => Comments::find(),
- 'pagination' => [
- 'pageSize' => 50
- ],
- 'sort' => [
- 'defaultOrder' => [
- 'visible' => SORT_DESC,
- 'created_at' => SORT_DESC,
- ]
- ],
- ]);
- return $this->render('index', [
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Comments model.
- * @param int $id
- * @return string
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionFilter($id)
- {
- $model = $this->findModel($id);
- return $this->redirect(array('/manager/comments-filter/create', 'uid1'=>$model->ip_address, 'uid2'=>md5($model->user_agent), 'ntext'=>$model->fakename));
- }
- /**
- * Displays a single Comments model.
- * @param int $id
- * @return string
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Comments model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return string|\yii\web\Response
- */
- public function actionCreate()
- {
- $model = new Comments();
- if ($this->request->isPost) {
- if ($model->load($this->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- }
- } else {
- $model->loadDefaultValues();
- }
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- /**
- * Updates an existing Comments model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param int $id
- * @return string|\yii\web\Response
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if (isset($_POST['Comments'])) {
- $model->message = $_POST['Comments']['message'];
- $model->visible = $_POST['Comments']['visible'];
- if ($model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- }
- }
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- /**
- * Deletes an existing Comments model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param int $id
- * @return \yii\web\Response
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * @param $id
- * @return string
- * @throws NotFoundHttpException
- */
- public function actionToggle($id,$value)
- {
- $model = $this->findModel($id);
- $model->updateAttributes(['visible'=>$value]);
- return $this->actionIndex();
- }
- public function actionAjaxtoggle()
- {
- $ret = ['stat'=>'err'];
- if( $this->request->isPost && $this->request->post('id') && $this->request->post('value') ){
- $id = $this->request->post('id');
- $value = $this->request->post('value');
- $model = $this->findModel($id);
- if( $model->updateAttributes(['visible'=>$value]) ){
- $ret = ['stat'=>'ok', 'id'=>$id, 'value'=>$value];
- }
- }
- return \yii\helpers\Json::encode($ret);;
- }
- /**
- * Finds the Comments model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param int $id
- * @return Comments the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Comments::findOne(['id' => $id])) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- public function actionConf()
- {
- $model = $this->confModel();
- if ($this->request->isPost && $post = $this->request->post()){
- $post['Conf'] = $this->formNormalizer($post['Conf']);
- if( $model->load($post,'Conf') && $model->save()) {
- \Yii::$app->cache->delete(CommentsConf::$keysCache);
- return $this->redirect('index');
- }
- }
- return $this->render('conf', [
- 'model' => $model,
- ]);
- }
- public function formNormalizer($post)
- {
- $post['AllStop'] = $this->CheckYN(@$post['AllStop']);
- $post['reg'] = $this->CheckYN(@$post['reg']);
- $post['d0'] = $this->CheckYN(@$post['d0']);
- $post['d1'] = $this->CheckYN(@$post['d1']);
- $post['d2'] = $this->CheckYN(@$post['d2']);
- $post['d3'] = $this->CheckYN(@$post['d3']);
- $post['d4'] = $this->CheckYN(@$post['d4']);
- $post['d5'] = $this->CheckYN(@$post['d5']);
- $post['d6'] = $this->CheckYN(@$post['d6']);
- return $post;
- }
- public function CheckYN($attr)
- {
- return (isset($attr) && $attr == 'Y')?'Y':'N';
- }
- protected function confModel()
- {
- if (($model = CommentsConf::findOne(0)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
|