Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.00 KB | None | 0 0
  1. <?php
  2.  
  3. namespace frontend\controllers;
  4.  
  5. use frontend\models\ProjectUser;
  6. use Yii;
  7. use frontend\models\Project;
  8. use yii\data\ActiveDataProvider;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use frontend\models\User;
  13. use yii\web\UploadedFile;
  14. use frontend\models\ProjectSearch;
  15. use yii\web\ForbiddenHttpException;
  16.  
  17. /**
  18. * ProjectController implements the CRUD actions for Project model.
  19. */
  20. class ProjectController extends Controller
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function behaviors()
  26. {
  27. return [
  28. 'verbs' => [
  29. 'class' => VerbFilter::className(),
  30. 'actions' => [
  31. 'delete' => ['POST'],
  32. ],
  33. ],
  34. ];
  35. }
  36.  
  37. public function beforeAction($action)
  38. {
  39. if (parent::beforeAction($action)) {
  40. if (!Yii::$app->user->identity) {
  41. return Yii::$app->response->redirect(['site/login']);
  42. }
  43. if (User::ROLE_ADMIN != Yii::$app->user->identity->role) {
  44. throw new ForbiddenHttpException('Access denied');
  45. }
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51.  
  52. /**
  53. * Lists all Project models.
  54. * @return mixed
  55. */
  56. public function actionIndex()
  57. {
  58. $searchModel = new ProjectSearch();
  59. $project = Yii::$app->request->queryParams ? $searchModel->search(Yii::$app->request->queryParams) : '';
  60.  
  61.  
  62. return $this->render('index', [
  63. 'searchModel' => $searchModel,
  64. 'project' => $project,
  65. ]);
  66.  
  67.  
  68. // $projects = Project::find()->all();
  69. // $dataProvider = new ActiveDataProvider([
  70. // 'query' => Project::find(),
  71. // ]);
  72. //
  73. // if (User::ROLE_ADMIN == Yii::$app->user->identity->role) {
  74. // $this->view->params['menuItem'] = 'Admin';
  75. // $this->view->params['menuLink'] = 'project/admin';
  76. // }
  77. //
  78. // return $this->render('index', [
  79. // 'dataProvider' => $dataProvider,
  80. // 'projects' => $projects,
  81. // ]);
  82. // $model = new ProjectUser();
  83. // $model->project_id = 103;
  84. // if ($model->load(Yii::$app->request->post()) )
  85. // {
  86. //// $attr = Yii::$app->request->post('Project');
  87. //// $user = User::findOne(['id' => $attr['user']]);
  88. //// $model->users = $user;
  89. //
  90. // $model->save();
  91. // $model = new ProjectUser();
  92. // }
  93. // $searchModel = new ProjectSearch();
  94. // $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  95. //
  96. // $project = Project::findOne(['id' => 103]);
  97. // $limits = ProjectUser::findAll(['project_id' => 103]);
  98. //
  99. // if (Yii::$app->request->post()) {
  100. // if (\yii\base\Model::loadMultiple($limits, Yii::$app->request->post())) {
  101. // foreach ($limits as $limit) {
  102. // $limit->save();
  103. // }
  104. // }
  105. //
  106. // }
  107.  
  108. return $this->render('index', [
  109. 'searchModel' => $searchModel,
  110. 'dataProvider' => $dataProvider,
  111. 'model' => $model,
  112. 'limits' => $limits,
  113. ]);
  114.  
  115.  
  116. }
  117.  
  118.  
  119. public function actionAdmin()
  120. {
  121. $user = Yii::$app->user->identity;
  122. if (!$user) {
  123. return Yii::$app->response->redirect(['site/login']);
  124. }
  125.  
  126. $projects = Project::find()->all();
  127. $dataProvider = new ActiveDataProvider([
  128. 'query' => Project::find(),
  129. ]);
  130.  
  131. if (User::ROLE_ADMIN == Yii::$app->user->identity->role) {
  132. $this->view->params['menuItem'] = 'Team member';
  133. $this->view->params['menuLink'] = '/';
  134. }
  135.  
  136. return $this->render('admin', [
  137. 'dataProvider' => $dataProvider,
  138. 'projects' => $projects,
  139. ]);
  140. }
  141.  
  142. public function actionReport()
  143. {
  144. $searchModel = new ProjectSearch();
  145. $project = Yii::$app->request->queryParams ? $searchModel->search(Yii::$app->request->queryParams) : '';
  146.  
  147.  
  148. return $this->render('report', [
  149. 'searchModel' => $searchModel,
  150. 'project' => $project,
  151. ]);
  152. }
  153.  
  154.  
  155. /**
  156. * Displays a single Project model.
  157. * @param integer $id
  158. * @return mixed
  159. * @throws NotFoundHttpException if the model cannot be found
  160. */
  161. public function actionView($id)
  162. {
  163. return $this->render('view', [
  164. 'model' => $this->findModel($id),
  165. ]);
  166. }
  167.  
  168. /**
  169. * Creates a new Project model.
  170. * If creation is successful, the browser will be redirected to the 'view' page.
  171. * @return mixed
  172. */
  173. public function actionCreate($slug = '')
  174. {
  175.  
  176. // $model = new Project();
  177.  
  178. // if ($model->load(Yii::$app->request->post()) && $model->save()) {
  179. // return $this->redirect(['view', 'id' => $model->id]);
  180. // }
  181. //
  182. // return $this->render('create', [
  183. // 'model' => $model,
  184. // ]);
  185.  
  186.  
  187. // $this->checkAccess($id);
  188. $model = Project::find()->andWhere(['not', ['step' => Project::STATUS_SAVE]])->one();
  189. if (!Yii::$app->request->post('step') && $model) {
  190. $model->delete();
  191. $model = new Project(['step' => Project::STATUS_STEP1]);
  192. $model->step = Project::STATUS_STEP1;
  193. }
  194. if (!$model) {
  195. $model = new Project(['step' => Project::STATUS_STEP1]);
  196. $model->step = Project::STATUS_STEP1;
  197. }
  198.  
  199. // var_dump(Yii::$app->request->post());
  200. // var_dump($model);
  201. // exit;
  202. if ($model->step == Project::STATUS_STEP1 && Yii::$app->request->post() && $model->load(Yii::$app->request->post())) {
  203. $model->save();
  204. // $dates = $this->loadDinamicFormPostData('TicketDatePrototype', TicketDatePrototype::className());
  205. // if (Model::loadMultiple($dates, Yii::$app->request->post())) {
  206. //
  207. // if (Model::validateMultiple($dates)) {
  208. // var_dump($model);
  209. $model->step = Yii::$app->request->post('step');
  210. // var_dump(Yii::$app->request->post());
  211. // var_dump($model);
  212. // exit;
  213. $model->save();
  214. // $model->scenario = TicketInfo::SCENARIO_STEP2;
  215. // $model->linkImageAll('ticketDatesPrototypes', $dates);
  216. // } else {
  217. //
  218. // }
  219. //
  220. // }
  221. } elseif ($model->step == Project::STATUS_STEP2 && Yii::$app->request->post()) {
  222.  
  223. // $types = $this->loadDinamicFormPostData('TicketType', TicketType::className());
  224. // if (Model::loadMultiple($types, Yii::$app->request->post())) {
  225. //
  226. // if (Model::validateMultiple($types)) {
  227. // var_dump(Yii::$app->request->post());
  228. // var_dump($model);
  229. // exit;
  230. $picture = UploadedFile::getInstance($model, 'picture');
  231.  
  232. // store the source file name
  233. // $model->filename = $picture->name;
  234. // var_dump($picture->name);
  235. // exit;
  236. if ($picture) {
  237. $tmp = explode('.', $picture->name);
  238. $ext = end($tmp);
  239. $image = Yii::$app->security->generateRandomString().".{$ext}";
  240. } else {
  241. $image = '';
  242. }
  243. // $ext = end((explode(".", $picture->name)));
  244.  
  245. // generate a unique file name
  246. $model->image = $image;
  247.  
  248. // the path to save file, you can set an uploadPath
  249. // in Yii::$app->params (as used in example below)
  250. $path = Yii::getAlias('@uploadProjectImage'. '/' . $model->image);
  251.  
  252. if($model->save()){
  253. if ($picture) {
  254. $picture->saveAs($path);
  255. }
  256. $model->step = Yii::$app->request->post('step');
  257. $model->save();
  258. if (Project::STATUS_SAVE == $model->step) {
  259. return $this->redirect(['limit', 'id' => $model->id]);
  260. }
  261. } else {
  262. // error in saving model
  263. }
  264.  
  265.  
  266.  
  267.  
  268.  
  269. // $model->scenario = TicketInfo::SCENARIO_STEP3;
  270. // $model->linkImageAll('ticketTypes', $types);
  271. // }
  272. // }
  273.  
  274. // $individualFields = $this->loadDinamicFormPostData('TicketTypeFields', TicketTypeFields::className());
  275. // Model::loadMultiple($individualFields, Yii::$app->request->post());
  276. // if (isset($_POST['TicketTypeFields'][0][0])) {
  277. // foreach ($_POST['TicketTypeFields'] as $indexType => $individualFields) {
  278. // TicketTypeFields::deleteAll(['ticket_type_id' => $model->ticketTypes[$indexType]['id']]);
  279. // foreach ($individualFields as $indexField => $field) {
  280. // $data['TicketTypeFields'] = $field;
  281. // $modelField = new TicketTypeFields;
  282. // $modelField->load($data);
  283. // $valid = $modelField->validate();
  284. // $modelField->ticket_type_id = $model->ticketTypes[$indexType]['id'];
  285. // $modelField->save();
  286. // $modelFields[$indexType][$indexField] = $modelField;
  287. // }
  288. // }
  289. // }
  290. }elseif ($model->step == Project::STATUS_STEP3 && Yii::$app->request->post() && $model->load(Yii::$app->request->post())) {
  291. // $promotions = $this->loadDinamicFormPostData('TicketPromotion', TicketPromotion::className());
  292. // Model::loadMultiple($promotions, Yii::$app->request->post());
  293. // Model::loadMultiple($types, Yii::$app->request->post());
  294. // $fields = $this->loadDinamicFormPostData('TicketInfoFields', TicketInfoFields::className());
  295. // Model::loadMultiple($fields, Yii::$app->request->post());
  296. // if (Model::validateMultiple($promotions) && Model::validateMultiple($types)) {
  297. $model->step = Yii::$app->request->post('step');
  298. //// foreach ($model->ticketTypes as $type) {
  299. //// if (3 == $type->type || 10 == $type->type) {
  300. //// $type->people_count = $type->people_count - 1;
  301. //// }
  302. //// }
  303. ///
  304. // var_dump(Yii::$app->request->post());
  305. // var_dump($model);
  306. // exit;
  307. if ($model->step != Project::STATUS_SAVE) {
  308. // $model->linkImageAll('ticketPromotions', $promotions);
  309. // $model->linkImageAll('ticketTypes', $types);
  310. // $save = $model->save();
  311. } else {
  312. $save = $model->save();
  313. // if ($save) {
  314. // $model->linkImageAll('ticketFields', $fields);
  315. // $model->linkImageAll('ticketPromotions', $promotions);
  316. // $model->linkImageAll('ticketTypes', $types);
  317. return $this->redirect(['view', 'id' => $model->id]);
  318. }
  319. // }
  320. // }
  321. }
  322. return $this->render('create', [
  323. 'model' => $model,
  324. // 'dates' => $dates,
  325. // 'types' => $types,
  326. // 'fields' => $fields,
  327. // 'modelFields' => $modelFields,
  328. // 'promotions' => $promotions,
  329. ]);
  330.  
  331.  
  332.  
  333. }
  334.  
  335. /**
  336. * Updates an existing Project model.
  337. * If update is successful, the browser will be redirected to the 'view' page.
  338. * @param integer $id
  339. * @return mixed
  340. * @throws NotFoundHttpException if the model cannot be found
  341. */
  342. public function actionUpdate($id)
  343. {
  344. $model = $this->findModel($id);
  345.  
  346. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  347. return $this->redirect(['admin']);
  348. }
  349.  
  350.  
  351. return $this->render('update1', [
  352. 'model' => $model,
  353. ]);
  354. }
  355.  
  356. public function actionUpdateLogo($id)
  357. {
  358. $model = $this->findModel($id);
  359.  
  360. if ($model->load(Yii::$app->request->post())) {
  361. $picture = UploadedFile::getInstance($model, 'picture');
  362. if (null == $picture) {
  363. return $this->redirect(['admin']);
  364. }
  365.  
  366. if ($picture) {
  367. $tmp = explode('.', $picture->name);
  368. $ext = end($tmp);
  369. $image = Yii::$app->security->generateRandomString().".{$ext}";
  370. } else {
  371. $image = '';
  372. }
  373.  
  374. $model->image = $image;
  375.  
  376. // the path to save file, you can set an uploadPath
  377. // in Yii::$app->params (as used in example below)
  378. $path = Yii::getAlias('@uploadProjectImage'. '/' . $model->image);
  379.  
  380. if($model->save()){
  381. if ($picture) {
  382. $picture->saveAs($path);
  383. }
  384. $model->save();
  385. return $this->redirect(['admin']);
  386. } else {
  387. // error in saving model
  388. }
  389. }
  390.  
  391. return $this->render('update2', [
  392. 'model' => $model,
  393. ]);
  394. }
  395.  
  396. /**
  397. * Deletes an existing Project model.
  398. * If deletion is successful, the browser will be redirected to the 'index' page.
  399. * @param integer $id
  400. * @return mixed
  401. * @throws NotFoundHttpException if the model cannot be found
  402. */
  403. public function actionDelete($id)
  404. {
  405. $this->findModel($id)->delete();
  406.  
  407.  
  408. // return $this->redirect(Yii::$app->request->referrer ?: ['admin']);
  409. return $this->redirect(['admin']);
  410. }
  411.  
  412. public function actionDeleteFromProject($id)
  413. {
  414. // var_dump(Yii::$app->request->referrer);
  415. // exit;
  416. $limit = ProjectUser::findOne($id);
  417. $limit->delete();
  418.  
  419. return $this->redirect(Yii::$app->request->referrer ?: ['admin']);
  420. return $this->redirect(['admin']);
  421. }
  422.  
  423. public function actionLimit($id)
  424. {
  425. $modelUser = new User();
  426. $model = new ProjectUser();
  427. $model->project_id = $id;
  428. if(Yii::$app->request->isPjax){
  429. if ($modelUser->load(Yii::$app->request->post()) ) {
  430. $userPost = Yii::$app->request->post('User');
  431. $user = User::findOne(['email' => $userPost['email']]);
  432. if (!$user) {
  433. $user = $this->createUserByEmail($userPost['email']);
  434.  
  435. }
  436. if ($user) {
  437. $model->user_id = $user->id;
  438. !$model->existUser() ? $model->save() : Yii::$app->getSession()->setFlash('error', Yii::t('app', 'This user already has been added to this project.'));
  439. }
  440. $modelUser = new User();
  441. }
  442. }
  443.  
  444. $limits = ProjectUser::findAll(['project_id' => $id]);
  445. if (Yii::$app->request->post()) {
  446. if (\yii\base\Model::loadMultiple($limits, Yii::$app->request->post())) {
  447. foreach ($limits as $limit) {
  448. $limit->save();
  449. }
  450. }
  451. if ('finish' == Yii::$app->request->post('step')) {
  452. return $this->redirect(['admin']);
  453. }
  454. }
  455.  
  456. $model->project_id = $id;
  457. return $this->render('limit', [
  458. 'model' => $model,
  459. 'modelUser' => $modelUser,
  460. 'limits' => $limits,
  461. ]);
  462.  
  463. }
  464.  
  465. public function actionLimitUpdate($id)
  466. {
  467. $modelUser = new User();
  468. $model = new ProjectUser();
  469. $model->project_id = $id;
  470. if(Yii::$app->request->isPjax){
  471. if ($modelUser->load(Yii::$app->request->post()) ) {
  472. $userPost = Yii::$app->request->post('User');
  473. $user = User::findOne(['email' => $userPost['email']]);
  474. if (!$user) {
  475. $user = $this->createUserByEmail($userPost['email']);
  476. }
  477. if ($user) {
  478. $model->user_id = $user->id;
  479. !$model->existUser() ? $model->save() : Yii::$app->getSession()->setFlash('error', Yii::t('app', 'This user already has been added to this project.'));
  480. }
  481. $modelUser = new User();
  482. }
  483. }
  484.  
  485. $limits = ProjectUser::findAll(['project_id' => $id]);
  486. if (Yii::$app->request->post()) {
  487. if (\yii\base\Model::loadMultiple($limits, Yii::$app->request->post())) {
  488. foreach ($limits as $limit) {
  489. $limit->save();
  490. }
  491. }
  492. if ('finish' == Yii::$app->request->post('step')) {
  493. return $this->redirect(['admin']);
  494. }
  495. }
  496.  
  497. $model->project_id = $id;
  498. return $this->render('update3', [
  499. 'model' => $model,
  500. 'modelUser' => $modelUser,
  501. 'limits' => $limits,
  502. ]);
  503.  
  504. }
  505.  
  506. /**
  507. * Finds the Project model based on its primary key value.
  508. * If the model is not found, a 404 HTTP exception will be thrown.
  509. * @param integer $id
  510. * @return Project the loaded model
  511. * @throws NotFoundHttpException if the model cannot be found
  512. */
  513. protected function findModel($id)
  514. {
  515. if (($model = Project::findOne($id)) !== null) {
  516. return $model;
  517. }
  518.  
  519. throw new NotFoundHttpException('The requested page does not exist.');
  520. }
  521.  
  522. public function createUserByEmail($email) {
  523. if ($email !== null && !preg_match('~@wonderslab.com$~', $email)) {
  524. Yii::$app->getSession()->setFlash('error', Yii::t('app', "You have entered a non-corporate email - " . $email));
  525. } else {
  526. $password = Yii::$app->security->generateRandomString(6);
  527.  
  528. $names = explode('.', strtok($email, '@'));
  529. $firstName = (isset($names[0])) ? ucfirst($names[0]) : '';
  530. $lastName = (isset($names[1])) ? ucfirst($names[1]) : '';
  531. $nickname = $firstName . ' ' . $lastName;
  532.  
  533. $user = new User([
  534. 'first_name' => $firstName,
  535. 'last_name' => $lastName,
  536. 'username' => $nickname,
  537. 'email' => $email,
  538. 'password' => $password,
  539. 'created_at' => time(),
  540. 'updated_at' => time(),
  541. ]);
  542. $user->generateAuthKey();
  543. $user->generatePasswordResetToken();
  544.  
  545.  
  546. if ($user->save()) {
  547. $user->emailAddToProject();
  548. return $user;
  549. } else {
  550. Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Unable to save user: ' . json_encode($user->getErrors())));
  551. }
  552. }
  553. }
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement