Example #1
0
 public function actionSave($id, $action)
 {
     if (!in_array($action, ['save', 'autosave', 'publish'])) {
         $action = 'save';
     }
     $article = Article::findOne($id);
     if (!$article) {
         throw new NotFoundHttpException(\Yii::t('yii', 'Page not found.'));
     }
     /// Csrf валидация: проверить что именно этот пользователь блокировал эту статью
     if (!\Yii::$app->user->can('articles.article.update', ['model' => $article])) {
         $this->accessDenied();
     }
     $this->checkLock($article);
     $rev = new Revision();
     $rev->articleid = $article->id;
     $rev->authorid = \Yii::$app->getUser()->id;
     $rev->updated_at = time();
     //		$rev->load($article->attributes);
     if (Yii::$app->request->isAjax && $rev->load(Yii::$app->request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         $prev = Revision::find()->where(['articleid' => $rev->articleid])->orderBy(['updated_at' => SORT_DESC])->one();
         if ($rev->snippet == $prev->snippet && $rev->content == $prev->content && $rev->title == $prev->title && $rev->tags == $prev->tags && $rev->thumbnailid == $prev->thumbnailid) {
             switch ($action) {
                 case 'publish':
                     $rev = $prev;
                     $rev->article->currentVersion;
                     break;
                 case 'autosave':
                     return [];
                 case 'save':
                     $rev = $prev;
                     if ($rev->role != Revision::ROLE_CURRENT) {
                         break;
                     }
                 default:
                     return ['message' => Module::t('articles', 'Saving: document not changed')];
             }
         }
         switch ($action) {
             case 'save':
                 if ($rev->role != Revision::ROLE_CURRENT) {
                     $rev->role = Revision::ROLE_NEW;
                 }
                 break;
             case 'autosave':
                 $rev->role = Revision::ROLE_AUTOSAVE;
                 break;
             case 'publish':
                 $rev->role = Revision::ROLE_CURRENT;
                 $rev->published_at = time();
                 break;
         }
         if ($rev->save()) {
             if (in_array($action, ['publish', 'save'])) {
                 $rev->clearOldVersions($action);
             }
             $msg = Module::t('articles', 'Document saved');
             if ($action == 'publish') {
                 $rev->applyPlugins($this->module->plugins);
                 $article->title = $rev->title;
                 $article->snippet = $rev->snippet;
                 $article->content = $rev->output;
                 $article->updated_at = time();
                 $article->tags = $rev->tags;
                 $article->thumbnailid = $rev->thumbnailid;
                 $article->status = Article::STATUS_RELEASE;
                 $article->save(false);
                 $msg = Module::t('articles', 'Document was published');
             }
             return ['message' => $msg, 'diff' => $rev->updateDiff()];
         } else {
             return $rev->errors;
         }
     }
     return "Uknown mode";
     //		if ($rev->load(Yii::$app->request->post()) && $rev->save()) {
 }