PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/ConfigurationController.php

https://gitlab.com/nitm/yii2-module
PHP | 330 lines | 267 code | 48 blank | 15 comment | 34 complexity | d487aac17400dfa14b2c3a151ba073ef MD5 | raw file
  1. <?php
  2. namespace nitm\controllers;
  3. use yii\helpers\ArrayHelper;
  4. use nitm\helpers\Helper;
  5. use nitm\helpers\Session;
  6. use nitm\helpers\Response;
  7. class ConfigurationController extends DefaultController
  8. {
  9. public function init()
  10. {
  11. parent::init();
  12. $this->model = \Yii::$app->getModule('nitm')->config;
  13. }
  14. public function behaviors()
  15. {
  16. $behaviors = [
  17. 'access' => [
  18. 'class' => \yii\filters\AccessControl::className(),
  19. //'only' => ['index', 'update', 'create', 'index', 'get', 'delete', 'convert', 'undelete'],
  20. 'rules' => [
  21. [
  22. 'actions' => ['index', 'create', 'update', 'delete', 'get', 'convert', 'undelete'],
  23. 'allow' => true,
  24. 'roles' => ['@'],
  25. ],
  26. ],
  27. ],
  28. 'verbs' => [
  29. 'class' => \yii\filters\VerbFilter::className(),
  30. 'actions' => [
  31. 'index' => ['get'],
  32. 'delete' => ['post'],
  33. 'undelete' => ['post'],
  34. 'create' => ['post'],
  35. 'update' => ['post'],
  36. 'convert' => ['post'],
  37. ],
  38. ],
  39. ];
  40. return array_replace_recursive(parent::behaviors(), $behaviors);
  41. }
  42. public static function assets()
  43. {
  44. return [
  45. 'configuration',
  46. ];
  47. }
  48. public function beforeAction($action)
  49. {
  50. $beforeAction = parent::beforeAction($action);
  51. if (isset($_GET['engine'])) {
  52. $this->model->setEngine($_GET['engine']);
  53. }
  54. if (isset($_GET['container'])) {
  55. $this->model->container = $_GET['container'];
  56. }
  57. $this->model->load($_REQUEST);
  58. switch (1) {
  59. case $_SERVER['REQUEST_METHOD'] == 'POST':
  60. case $_SERVER['REQUEST_METHOD'] == 'PUT':
  61. $params = $_POST;
  62. break;
  63. case $_SERVER['REQUEST_METHOD'] == 'GET':
  64. $params = $_GET;
  65. break;
  66. }
  67. $dm = $this->model->getDm();
  68. $container = Session::getVal($dm.'.current.container');
  69. //determine the correct container
  70. $this->model->container = $container ? $container : $this->model->container;
  71. //if we're not requesting a specific section then only load the sections and no values
  72. $this->model->prepareConfig($this->model->container, $this->model->getValues);
  73. return $beforeAction;
  74. }
  75. public function actionIndex($className = null, $options = [])
  76. {
  77. return $this->render('index', ['model' => $this->model]);
  78. }
  79. /*
  80. * Convert configuration from one format to antoher
  81. */
  82. public function actionConvert()
  83. {
  84. $this->model->setScenario($this->action->id);
  85. $this->model->load($_POST);
  86. switch ($this->model->convert['do']) {
  87. case true:
  88. $this->model->convert($this->model->convert['container'], $this->model->convert['from'], $this->model->convert['to']);
  89. break;
  90. }
  91. return $this->finalAction();
  92. }
  93. public function actionUndelete()
  94. {
  95. $section = explode('.', $_POST[$this->model->formName()]['name']);
  96. $name = explode('.', $_POST[$this->model->formName()]['name']);
  97. $_POST[$this->model->formName()]['section'] = array_shift($section);
  98. $_POST[$this->model->formName()]['name'] = array_pop($name);
  99. $this->action->id = 'create';
  100. return $this->actionCreate();
  101. }
  102. public function actionCreate($modelClass = null, $options = [])
  103. {
  104. if (isset($_POST[$this->model->formName()])) {
  105. $this->model->setScenario($this->action->id.ucfirst($_POST[$this->model->formName()]['what']));
  106. $this->model->load($_POST);
  107. if (\Yii::$app->request->isAjax && (@Helper::boolval($_REQUEST['do']) !== true)) {
  108. $this->setResponseFormat('json');
  109. return \yii\widgets\ActiveForm::validate($this->model);
  110. }
  111. if ($this->model->validate()) {
  112. switch ($this->model->getScenario()) {
  113. case 'createContainer':
  114. $this->model->createContainer($this->model->value, null, $this->model->engine);
  115. break;
  116. case 'createValue':
  117. $view['data']['data'] = $this->model->create($this->model->section.'.'.$this->model->name);
  118. $view = [
  119. 'view' => 'values/value',
  120. 'data' => [
  121. 'model' => $this->model,
  122. 'data' => $this->model->config('current.action'),
  123. 'parent' => $this->model->section,
  124. ],
  125. ];
  126. break;
  127. case 'createSection':
  128. $this->model->create();
  129. $view = [
  130. 'view' => 'values/index',
  131. 'data' => [
  132. 'model' => $this->model,
  133. 'data' => [],
  134. ],
  135. ];
  136. break;
  137. }
  138. }
  139. }
  140. switch ($this->model->config('current.action.success') && \Yii::$app->request->isAjax && (Helper::boolval(@$_REQUEST['getHtml']) === true)) {
  141. case true:
  142. $this->model->config('current.action.data', $this->renderAjax($view['view'], $view['data']));
  143. break;
  144. }
  145. return $this->finalAction();
  146. }
  147. public function actionGet()
  148. {
  149. $ret_val = [
  150. 'success' => false,
  151. 'action' => 'get',
  152. 'message' => 'Get configuration',
  153. 'class' => '',
  154. ];
  155. switch ($this->model->validate()) {
  156. case true:
  157. switch ($this->model->what) {
  158. case 'section':
  159. if ($this->model->section) {
  160. $ret_val['success'] = true;
  161. $ret_val['section'] = $this->model->section;
  162. if ($this->model->section) {
  163. $values = $this->model->config('current.config.'.$this->model->section);
  164. } else {
  165. $values = $this->model->config('current.config');
  166. }
  167. switch (Response::getFormat()) {
  168. case 'html':
  169. case 'modal':
  170. $ret_val['data'] = $this->renderAjax('values/index', [
  171. 'model' => $this->model,
  172. 'values' => $values,
  173. 'parent' => $this->model->section,
  174. ]);
  175. break;
  176. case 'json':
  177. if (\Yii::$app->request->get('getHtml')) {
  178. $ret_val['data'] = $this->renderAjax('values/index', [
  179. 'model' => $this->model,
  180. 'values' => $values,
  181. 'parent' => $this->model->section,
  182. ]);
  183. } else {
  184. $ret_val['data'] = $values;
  185. }
  186. break;
  187. default:
  188. $ret_val['data'] = $values;
  189. break;
  190. }
  191. }
  192. break;
  193. }
  194. break;
  195. }
  196. Response::viewOptions('args', [
  197. 'content' => ArrayHelper::getValue($ret_val, 'data', ''),
  198. ]);
  199. $this->model->config('current.action', $ret_val);
  200. return $this->finalAction();
  201. }
  202. public function actionDelete($id, $modelClass = null)
  203. {
  204. switch (isset($_REQUEST[$this->model->formName()])) {
  205. case true:
  206. $this->model->setScenario($this->action->id.ucfirst($_POST[$this->model->formName()]['what']));
  207. $this->model->load($_POST);
  208. if (\Yii::$app->request->isAjax && (@Helper::boolval($_REQUEST['do']) !== true)) {
  209. $this->setResponseFormat('json');
  210. return \yii\widgets\ActiveForm::validate($this->model);
  211. }
  212. if ($this->model->validate()) {
  213. switch ($this->model->getScenario()) {
  214. case 'deleteContainer':
  215. $this->model->deleteContainer($this->model->value, null, $this->model->engine);
  216. break;
  217. case 'deleteValue':
  218. case 'deleteSection':
  219. $this->model->delete();
  220. break;
  221. }
  222. }
  223. break;
  224. }
  225. return $this->finalAction();
  226. }
  227. public function actionUpdate($id, $modelClass = null, $with = [], $viewOptions = [])
  228. {
  229. switch (isset($_POST[$this->model->formName()])) {
  230. case true:
  231. $this->model->setScenario($this->action->id.ucfirst($_POST[$this->model->formName()]['what']));
  232. $this->model->load($_POST);
  233. if (\Yii::$app->request->isAjax && (@Helper::boolval($_REQUEST['do']) !== true)) {
  234. $this->setResponseFormat('json');
  235. return \yii\widgets\ActiveForm::validate($this->model);
  236. }
  237. if ($this->model->validate()) {
  238. switch ($this->model->getScenario()) {
  239. case 'updateContainer':
  240. case 'updateValue':
  241. case 'updateSection':
  242. $this->model->update();
  243. break;
  244. case 'updateComment':
  245. $this->model->comment();
  246. break;
  247. }
  248. }
  249. break;
  250. }
  251. return $this->finalAction();
  252. }
  253. /*
  254. * Where do we go after an action?
  255. * @params mixed $params
  256. */
  257. protected function finalAction($params = null)
  258. {
  259. \Yii::$app->getSession()->setFlash(
  260. @$this->model->config('current.action.class'),
  261. $this->model->config('current.action.message')
  262. );
  263. switch (\Yii::$app->request->isAjax) {
  264. //if this is an ajax call then print the result
  265. case true:
  266. $this->model->config('current.action.flash', \Yii::$app->getSession()->getFlash(
  267. $this->model->config('current.action.class'), null, true));
  268. Response::viewOptions('args.content', $this->model->config('current.action'));
  269. $format = Response::formatSpecified() ? $this->getResponseFormat() : 'json';
  270. $this->setResponseFormat($format);
  271. return $this->renderResponse($this->model->config('current.action'), null, true);
  272. break;
  273. //otherwise we're going back to the index
  274. default:
  275. $this->redirect(\Yii::$app->request->getReferrer());
  276. break;
  277. }
  278. }
  279. /*---------------------
  280. Private functions
  281. --------------------*/
  282. }