AjaxController.php
4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace common\modules\blog\controllers;
use common\models\Language;
use common\modules\blog\models\ArticleCategory;
use common\modules\blog\models\ArticleCategoryLang;
use common\modules\blog\models\ArticleCategoryMedia;
use common\modules\blog\models\ArticleLang;
use common\modules\blog\models\ArticleMedia;
use yii\base\InvalidParamException;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
class AjaxController extends Controller
{
public function beforeAction($action)
{
if(!\Yii::$app->request->getIsAjax()) {
//throw new ForbiddenHttpException('Permission denied');
}
if(!parent::beforeAction($action)) {
return false;
}
return true;
}
public function actionCategoryForm($lang_id, $widget_id)
{
$model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['active' => 1, 'language_id' => $lang_id])->one();
if(!$model) {
throw new NotFoundHttpException('Language not found');
}
$category_lang = new ArticleCategoryLang();
return $this->renderAjax('_category_form', ['model' => $model, 'category_lang' => $category_lang, 'widget_id' => $widget_id]);
}
public function actionArticleForm($lang_id, $widget_id)
{
$model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['active' => 1, 'language_id' => $lang_id])->one();
if(!$model) {
throw new NotFoundHttpException('Language not found');
}
$article_lang = new ArticleLang();
return $this->renderAjax('_article_form', ['model' => $model, 'article_lang' => $article_lang, 'widget_id' => $widget_id]);
}
public function actionArticleMediaForm($lang_id, $widget_id, $type)
{
$model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['active' => 1, 'language_id' => $lang_id])->one();
if(!$model) {
throw new NotFoundHttpException('Language not found');
}
if(!in_array($type, ['full', 'preview'])) {
throw new InvalidParamException('Type must only be full/preview');
}
$article_lang = new ArticleMedia();
return $this->renderAjax('_article_media_form', ['model' => $model, 'article_lang' => $article_lang, 'widget_id' => $widget_id, 'type' => $type]);
}
public function actionArticleCategoryMediaForm($lang_id, $widget_id, $type)
{
$model = Language::find()->where(['>=', 'language_id', 1])->andWhere(['active' => 1, 'language_id' => $lang_id])->one();
if(!$model) {
throw new NotFoundHttpException('Language not found');
}
if(!in_array($type, ['full', 'preview'])) {
throw new InvalidParamException('Type must only be full/preview');
}
$article_lang = new ArticleCategoryMedia();
return $this->renderAjax('_article_media_form', ['model' => $model, 'article_lang' => $article_lang, 'widget_id' => $widget_id, 'type' => $type]);
}
public function actionRemoveImage()
{
$post = \Yii::$app->request->post();
if(!empty($post['article_media_id'])) {
$article_media = ArticleMedia::findOne($post['article_media_id']);
if($post['remove_media']) {
$media = $article_media->media->delete();
}
if(!empty($article_media)) {
$article_media->delete();
}
return true;
} else {
return false;
}
}
public function actionRemoveCategoryImage()
{
$post = \Yii::$app->request->post();
if(!empty($post['category_media_id'])) {
$category_media = ArticleCategoryMedia::findOne($post['category_media_id']);
if($post['remove_media']) {
$media = $category_media->media->delete();
}
if(!empty($category_media)) {
$category_media->delete();
}
return true;
} else {
return false;
}
}
public function actionRemoveImageCategory()
{
$post = \Yii::$app->request->post();
if(!empty($post['category_media_id'])) {
$category_media = ArticleCategoryMedia::findOne($post['category_media_id']);
if($post['remove_media']) {
$media = $category_media->media->delete();
}
if(!empty($category_media)) {
$category_media->delete();
}
return true;
} else {
return false;
}
}
}