|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace arogachev\tree\controllers; |
| 4 | + |
| 5 | +use arogachev\tree\helpers\NestedSetsHelper; |
| 6 | +use Yii; |
| 7 | +use yii\db\ActiveRecord; |
| 8 | +use yii\filters\ContentNegotiator; |
| 9 | +use yii\web\BadRequestHttpException; |
| 10 | +use yii\web\Controller; |
| 11 | +use yii\web\ForbiddenHttpException; |
| 12 | +use yii\web\NotFoundHttpException; |
| 13 | +use yii\web\Response; |
| 14 | + |
| 15 | +class TreeController extends Controller |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @inheritdoc |
| 19 | + */ |
| 20 | + public function behaviors() |
| 21 | + { |
| 22 | + return [ |
| 23 | + [ |
| 24 | + 'class' => ContentNegotiator::className(), |
| 25 | + 'formats' => [ |
| 26 | + 'application/json' => Response::FORMAT_JSON, |
| 27 | + ], |
| 28 | + ], |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritdoc |
| 34 | + */ |
| 35 | + public function beforeAction($action) |
| 36 | + { |
| 37 | + if (!parent::beforeAction($action)) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + if (!Yii::$app->request->isAjax) { |
| 42 | + throw new ForbiddenHttpException('This page is not allowed for view.'); |
| 43 | + } |
| 44 | + |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return array |
| 50 | + */ |
| 51 | + public function actionGetTree() |
| 52 | + { |
| 53 | + return NestedSetsHelper::getHierarchicalArray(Yii::$app->request->get('modelClass')); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return array |
| 58 | + * @throws BadRequestHttpException |
| 59 | + * @throws NotFoundHttpException |
| 60 | + */ |
| 61 | + public function actionAppendTo() |
| 62 | + { |
| 63 | + $parent = $this->getModel('parentPk'); |
| 64 | + $model = $this->getModel(null, true); |
| 65 | + $model->appendTo($parent); |
| 66 | + |
| 67 | + return ['pk' => $model->primaryKey]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @throws BadRequestHttpException |
| 72 | + * @throws NotFoundHttpException |
| 73 | + */ |
| 74 | + public function actionRename() |
| 75 | + { |
| 76 | + $model = $this->getModel(); |
| 77 | + $model->name = Yii::$app->request->post('name'); |
| 78 | + $model->save(false); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @throws BadRequestHttpException |
| 83 | + * @throws NotFoundHttpException |
| 84 | + * @throws \Exception |
| 85 | + */ |
| 86 | + public function actionDelete() |
| 87 | + { |
| 88 | + $this->getModel()->deleteWithChildren(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @throws BadRequestHttpException |
| 93 | + * @throws NotFoundHttpException |
| 94 | + */ |
| 95 | + public function actionInsertBefore() |
| 96 | + { |
| 97 | + $model = $this->getModel(); |
| 98 | + $prevModel = $this->getModel('nextModelPk'); |
| 99 | + $model->insertBefore($prevModel); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @throws BadRequestHttpException |
| 104 | + * @throws NotFoundHttpException |
| 105 | + */ |
| 106 | + public function actionInsertAfter() |
| 107 | + { |
| 108 | + $model = $this->getModel(); |
| 109 | + $nextModel = $this->getModel('prevModelPk'); |
| 110 | + $model->insertAfter($nextModel); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param null|string $paramName |
| 115 | + * @param boolean $createIfNotFound |
| 116 | + * @return \yii\db\ActiveRecord|\creocoder\nestedsets\NestedSetsBehavior |
| 117 | + * @throws BadRequestHttpException |
| 118 | + * @throws NotFoundHttpException |
| 119 | + */ |
| 120 | + protected function getModel($paramName = null, $createIfNotFound = false) |
| 121 | + { |
| 122 | + $modelClass = Yii::$app->request->post('modelClass'); |
| 123 | + if (!$modelClass) { |
| 124 | + throw new BadRequestHttpException('Model class must be specified in order to find model.'); |
| 125 | + } |
| 126 | + |
| 127 | + $pk = Yii::$app->request->post($paramName ?: 'modelPk'); |
| 128 | + if (!$pk && !$createIfNotFound) { |
| 129 | + throw new BadRequestHttpException('Model primary key must be specified in order to find model.'); |
| 130 | + } |
| 131 | + |
| 132 | + if (!$createIfNotFound) { |
| 133 | + $model = $modelClass::findOne($pk); |
| 134 | + if (!$model) { |
| 135 | + throw new NotFoundHttpException('Model not found.'); |
| 136 | + } |
| 137 | + } else { |
| 138 | + $model = new $modelClass; |
| 139 | + } |
| 140 | + |
| 141 | + if (!($model instanceof ActiveRecord)) { |
| 142 | + throw new BadRequestHttpException('Valid ActiveRecord model class must be specified.'); |
| 143 | + } |
| 144 | + |
| 145 | + return $model; |
| 146 | + } |
| 147 | +} |
0 commit comments