PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/AdminpController.php

https://gitlab.com/deadgood/sportas
PHP | 357 lines | 267 code | 50 blank | 40 comment | 39 complexity | 3943a853dd18e50051adad1c991f7d3d MD5 | raw file
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Acreditations;
  4. use app\models\Beds;
  5. use app\models\Capitals;
  6. use app\models\Countries;
  7. use app\models\Scopes;
  8. use app\models\Sex;
  9. use app\models\Tour;
  10. use app\models\Transport;
  11. use Yii;
  12. use app\models\Participant;
  13. use yii\data\ActiveDataProvider;
  14. use yii\web\Controller;
  15. use yii\web\NotFoundHttpException;
  16. use yii\filters\VerbFilter;
  17. use yii\filters\AccessControl;
  18. use yii\helpers\ArrayHelper;
  19. use app\models\Departments;
  20. /**
  21. * ParticipantController implements the CRUD actions for participant model.
  22. */
  23. class AdminpController extends Controller
  24. {
  25. /**
  26. * @inheritdoc
  27. */
  28. public function behaviors()
  29. {
  30. return [
  31. 'access' => [
  32. 'class' => AccessControl::className(),
  33. 'only' => ['logout'],
  34. 'rules' => [
  35. [
  36. 'actions' => ['logout'],
  37. 'allow' => true,
  38. 'roles' => ['@'],
  39. ],
  40. ],
  41. ],
  42. 'verbs' => [
  43. 'class' => VerbFilter::className(),
  44. 'actions' => [
  45. 'logout' => ['post'],
  46. ],
  47. ],
  48. 'as access' => [
  49. 'class' => '\app\components\AccessBehavior',
  50. ]
  51. ];
  52. }
  53. /**
  54. * Lists all participant models.
  55. * @return mixed
  56. */
  57. public function beforeAction($action)
  58. {
  59. $this->enableCsrfValidation = false;
  60. return parent::beforeAction($action);
  61. }
  62. public function actionDates()
  63. {
  64. if (isset($_POST['county'])) {
  65. $this->layout = 'white';
  66. if (isset($_POST['iditem'])) {
  67. $model = Participant::find()->where(['id' => $_POST['iditem']])->one();
  68. } else {
  69. $model = new Participant();
  70. }
  71. if ($_POST['county'] == 127) {
  72. $dateLapse = Beds::find()->where(['id' => 4])->one();
  73. } else {
  74. $dateLapse = Beds::find()->where(['id' => 5])->one();
  75. }
  76. return $this->render('/adminp/dates', ['dateLapse' => $dateLapse, 'model' => $model]);
  77. }
  78. }
  79. public function actionIndex()
  80. {
  81. $this->layout = 'admin';
  82. $departments = ArrayHelper::map(Departments::find()->all(), 'id', 'name');
  83. return $this->render('index', [
  84. 'departments' => $departments,
  85. ]);
  86. }
  87. public function actionMain()
  88. {
  89. // $this->layout='admin';
  90. if (isset($_POST)) {
  91. $this->layout = 'white';
  92. $dataProvider = new ActiveDataProvider([
  93. 'query' => participant::find()->where(['department_id' => $_POST['id']]),
  94. ]);
  95. $dataProvider->pagination = false;
  96. return $this->render('/adminp/main', [
  97. 'dataProvider' => $dataProvider,
  98. ]);
  99. }
  100. }
  101. /**
  102. * Displays a single participant model.
  103. * @param integer $id
  104. * @return mixed
  105. */
  106. public function actionView($id)
  107. {
  108. $this->layout = 'admin';
  109. $model = Participant::find()->where(['id' => $id])->
  110. with('county')->
  111. with('department')->
  112. with('sex')->
  113. with('scopeFirst')->
  114. with('scopeSecond')->
  115. with('scopeThird')->
  116. with('capitals')->
  117. with('transport')->
  118. with('offCapital')->
  119. with('offTransport')->
  120. one();
  121. $explodedfirst = explode('_', $model->accreditation_first);
  122. $explodedfirst = $explodedfirst[0];
  123. $firstAcr = Acreditations::find()->where(['id' => $explodedfirst])->one();
  124. $firstAcr = $firstAcr['name'];
  125. if (!empty($model->accreditation_second)) {
  126. $explodedsecond = explode('_', $model->accreditation_second);
  127. $explodedsecond = $explodedsecond[0];
  128. $model->accreditation_second = $explodedsecond;
  129. $secondAcr = Acreditations::find()->where(['id' => $explodedsecond])->one();
  130. $secondAcr = $secondAcr['name'];
  131. } else {
  132. $secondAcr = array();
  133. }
  134. $model->accreditation_first = $explodedfirst;
  135. $model->arrival = substr($model->arrival, 0, -3);
  136. $model->departue = substr($model->departue, 0, -3);
  137. return $this->render('view', [
  138. 'model' => $model,
  139. 'secondAcr' => $secondAcr,
  140. 'firstAcr' => $firstAcr
  141. ]);
  142. }
  143. /**
  144. * Creates a new participant model.
  145. * If creation is successful, the browser will be redirected to the 'view' page.
  146. * @return mixed
  147. */
  148. public function actionCreate()
  149. {
  150. $this->layout = 'admin';
  151. $countyArr = ArrayHelper::map(Countries::find()->orderBy(['country_name'=>SORT_ASC])->all(), 'id', 'country_name');
  152. $sex = ArrayHelper::map(Sex::find()->all(), 'id', 'name');
  153. $transport = ArrayHelper::map(Transport::find()->all(), 'id', 'name');
  154. $age = range(12, 100);
  155. $tmpAge = array();
  156. foreach ($age as $key => $item) {
  157. $tmpAge[$item] = $item;
  158. }
  159. $age = $tmpAge;
  160. if (!empty(Yii::$app->user->Identity)) {
  161. $respCheck = Participant::find()->where(['department_id' => Yii::$app->user->Identity->department_id])->andWhere(['responsible' => 1])->one();
  162. }
  163. if (empty($respCheck)) {
  164. $acreditationArr = Acreditations::find()->all();
  165. } else {
  166. $acreditationArr = Acreditations::find()->where(['<>', 'name', 'Delegacijos vadovas'])->all();
  167. }
  168. $capitals = ArrayHelper::map(Capitals::find()->all(), 'id', 'name');
  169. $arrivalInterval = Tour::find()->where(['id' => 2])->one();
  170. foreach ($acreditationArr as $key => $item) {
  171. $acreditation[$item['id'] . '_' . $item['dependency']] = $item['name'];
  172. }
  173. $acreditationArr = $acreditation;
  174. $scopes = ArrayHelper::map(Scopes::find()->all(), 'id', 'name');
  175. $model = new participant();
  176. if (Yii::$app->request->post()) {
  177. $model->load(Yii::$app->request->post());
  178. $model->department_id = !empty($_GET['department']) ? $_GET['department'] : '';
  179. if ($model->accreditation_first == '4_0' || $model->accreditation_second == '4_0') {
  180. $model->responsible = 1;
  181. }
  182. $model->save();
  183. return $this->redirect(['view', 'id' => $model->id, 'department' => $_GET['department']]);
  184. } else {
  185. return $this->render('create', [
  186. 'model' => $model,
  187. 'countyArr' => $countyArr,
  188. 'sex' => $sex,
  189. 'age' => $age,
  190. 'acreditationArr' => $acreditationArr,
  191. 'scopes' => $scopes,
  192. 'transport' => $transport,
  193. 'capitals' => $capitals,
  194. 'arrivalInterval' => $arrivalInterval
  195. ]);
  196. }
  197. }
  198. /**
  199. * Updates an existing participant model.
  200. * If update is successful, the browser will be redirected to the 'view' page.
  201. * @param integer $id
  202. * @return mixed
  203. */
  204. public function actionUpdate($id)
  205. {
  206. $this->layout = 'admin';
  207. $model = $this->findModel($id);
  208. $countyArr = ArrayHelper::map(Countries::find()->orderBy(['country_name'=>SORT_ASC])->all(), 'id', 'country_name');
  209. $sex = ArrayHelper::map(Sex::find()->all(), 'id', 'name');
  210. $transport = ArrayHelper::map(Transport::find()->all(), 'id', 'name');
  211. $age = range(12, 100);
  212. $tmpAge = array();
  213. foreach ($age as $key => $item) {
  214. $tmpAge[$item] = $item;
  215. }
  216. $age = $tmpAge;
  217. if (!empty(Yii::$app->user->Identity)) {
  218. $respCheck = Participant::find()->where(['department_id' => Yii::$app->user->Identity->department_id])->andWhere(['responsible' => 1])->one();
  219. }
  220. if ($model->responsible != 1) {
  221. if (empty($respCheck)) {
  222. $acreditationArr = Acreditations::find()->all();
  223. } else {
  224. $acreditationArr = Acreditations::find()->where(['<>', 'name', 'Delegacijos vadovas'])->all();
  225. }
  226. } else {
  227. $acreditationArr = Acreditations::find()->all();
  228. }
  229. $acreditationArr = Acreditations::find()->all();
  230. $capitals = ArrayHelper::map(Capitals::find()->all(), 'id', 'name');
  231. $arrivalInterval = Tour::find()->where(['id' => 2])->one();
  232. foreach ($acreditationArr as $key => $item) {
  233. $acreditation[$item['id'] . '_' . $item['dependency']] = $item['name'];
  234. if($model->accreditation_first == $item['id']){
  235. $model->accreditation_first = $item['id'] . '_' . $item['dependency'];
  236. }
  237. if($model->accreditation_second == $item['id']){
  238. $model->accreditation_second = $item['id'] . '_' . $item['dependency'];
  239. }
  240. }
  241. $acreditationArr = $acreditation;
  242. $scopes = ArrayHelper::map(Scopes::find()->all(), 'id', 'name');
  243. if (Yii::$app->request->post()) {
  244. if ($model->accreditation_first == '4_0' || $model->accreditation_second == '4_0') {
  245. $model->responsible = 1;
  246. }
  247. if (!isset($_POST['arrival_custom'])) {
  248. $model->transport_id = null;
  249. $model->capitals_id = null;
  250. $model->arrival = null;
  251. }
  252. if (isset($_POST['departure_custom'])) {
  253. $model->off_transport_id = null;
  254. $model->off_capital_id = null;
  255. $model->departue = null;
  256. }
  257. if ($_POST['Participant']['bed']) {
  258. $model->bed_from = null;
  259. $model->bed_to = null;
  260. }
  261. $model->load(Yii::$app->request->post());
  262. $model->save();
  263. return $this->redirect(['view', 'id' => $model->id, 'department' => $_GET['department']]);
  264. } else {
  265. $model->arrival = substr($model->arrival, 0, -3);
  266. $model->departue = substr($model->departue, 0, -3);
  267. return $this->render('update', [
  268. 'model' => $model,
  269. 'countyArr' => $countyArr,
  270. 'sex' => $sex,
  271. 'age' => $age,
  272. 'acreditationArr' => $acreditationArr,
  273. 'scopes' => $scopes,
  274. 'transport' => $transport,
  275. 'capitals' => $capitals,
  276. 'arrivalInterval' => $arrivalInterval
  277. ]);
  278. }
  279. }
  280. /**
  281. * Deletes an existing participant model.
  282. * If deletion is successful, the browser will be redirected to the 'index' page.
  283. * @param integer $id
  284. * @return mixed
  285. */
  286. public function actionDelete($id)
  287. {
  288. $this->layout = 'admin';
  289. $this->findModel($id)->delete();
  290. return $this->redirect(['index']);
  291. }
  292. /**
  293. * Finds the participant model based on its primary key value.
  294. * If the model is not found, a 404 HTTP exception will be thrown.
  295. * @param integer $id
  296. * @return participant the loaded model
  297. * @throws NotFoundHttpException if the model cannot be found
  298. */
  299. protected function findModel($id)
  300. {
  301. if (($model = participant::findOne($id)) !== null) {
  302. return $model;
  303. } else {
  304. throw new NotFoundHttpException('The requested page does not exist.');
  305. }
  306. }
  307. }