|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* this Action provides group publish and unpublish functionality. |
|
4
|
|
|
* Usage example: in backend controller |
|
5
|
|
|
* public function actions() |
|
6
|
|
|
* { |
|
7
|
|
|
* return [ |
|
8
|
|
|
* ... |
|
9
|
|
|
* 'publish-switch' => [ |
|
10
|
|
|
* 'class' => app\backend\actions\MassPublishAction::className(), |
|
11
|
|
|
* 'modelName' => Page::className(), |
|
12
|
|
|
* 'attribute' => 'published', |
|
13
|
|
|
* ] |
|
14
|
|
|
* ]; |
|
15
|
|
|
* @property string $modelName must be name of existing Model class. This property is required |
|
16
|
|
|
* @property string $attribute name of model attribute that provides public item visibility |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace app\backend\actions; |
|
20
|
|
|
|
|
21
|
|
|
use Yii; |
|
22
|
|
|
use yii\base\Action; |
|
23
|
|
|
use yii\web\Response; |
|
24
|
|
|
use yii\web\NotFoundHttpException; |
|
25
|
|
|
use app\backend\widgets\PublishSwitchButtons; |
|
26
|
|
|
use yii\web\ServerErrorHttpException; |
|
27
|
|
|
|
|
28
|
|
|
class MassPublishAction extends Action |
|
29
|
|
|
{ |
|
30
|
|
|
public $modelName = ''; |
|
31
|
|
|
public $attribute = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @throws ServerErrorHttpException |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
public function init() |
|
37
|
|
|
{ |
|
38
|
|
|
parent::init(); |
|
39
|
|
|
if (false === is_subclass_of($this->modelName, '\yii\db\ActiveRecord')) { |
|
|
|
|
|
|
40
|
|
|
throw new ServerErrorHttpException('Model class does not exists'); |
|
41
|
|
|
} |
|
42
|
|
|
if (false === in_array($this->attribute, (new $this->modelName)->attributes())) { |
|
43
|
|
|
throw new ServerErrorHttpException("Model '{$this->modelName}' has no '{$this->attribute}' field'" ); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return int |
|
49
|
|
|
* @throws NotFoundHttpException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function run() |
|
52
|
|
|
{ |
|
53
|
|
|
if (false === Yii::$app->request->isAjax) { |
|
54
|
|
|
throw new NotFoundHttpException(); |
|
55
|
|
|
} |
|
56
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
57
|
|
|
$items = is_array(Yii::$app->request->post('ps-items')) ? Yii::$app->request->post('ps-items') : []; |
|
58
|
|
|
$mode = Yii::$app->request->post('ps-action'); |
|
59
|
|
|
$active = null; |
|
60
|
|
|
$message = ''; |
|
61
|
|
|
switch ($mode) { |
|
62
|
|
|
case PublishSwitchButtons::MASS_PUBLISH : |
|
63
|
|
|
$message = 'Items published: {n}'; |
|
64
|
|
|
$active = 1; |
|
65
|
|
|
break; |
|
66
|
|
|
case PublishSwitchButtons::MASS_UNPUBLISH : |
|
67
|
|
|
$message = 'Items unpublished: {n}'; |
|
68
|
|
|
$active = 0; |
|
69
|
|
|
break; |
|
70
|
|
|
} |
|
71
|
|
|
if (false === empty($items) && null !== $active) { |
|
72
|
|
|
$class = $this->modelName; |
|
73
|
|
|
$num = $class::updateAll([$this->attribute => $active],['id' => $items]); |
|
74
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', $message, ['n' => $num])); |
|
75
|
|
|
return 1; |
|
76
|
|
|
} |
|
77
|
|
|
return 0; |
|
78
|
|
|
} |
|
79
|
|
|
} |