SettingsController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Profile;
  4. use yii\filters\VerbFilter;
  5. use yii\filters\AccessControl;
  6. use Yii;
  7. use yii\web\HttpException;
  8. use dektrium\user\controllers\SettingsController as BaseSettingsController;
  9. use yii\web\UploadedFile;
  10. class SettingsController extends BaseSettingsController
  11. {
  12. public function init()
  13. {
  14. if (@!Yii::$app->user->identity->confirmed_at && (@Yii::$app->user->identity->role == 1)) {
  15. return $this->redirect('/user/login');
  16. }
  17. Yii::$app->session->set('user.flags', Yii::$app->user->identity->id);
  18. $this->layout = '@app/views/layouts/backend/user';
  19. parent::init();
  20. }
  21. public function behaviors()
  22. {
  23. return array_merge(parent::behaviors(),
  24. [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'disconnect' => ['post'],
  29. ],
  30. ],
  31. 'access' => [
  32. 'class' => AccessControl::className(),
  33. 'rules' => [
  34. [
  35. 'allow' => true,
  36. 'actions' => ['account', 'confirm', 'networks', 'disconnect', 'profile', 'profile-settings'],
  37. 'roles' => ['@'],
  38. ],
  39. [
  40. 'allow' => true,
  41. 'actions' => ['bills-template', 'download-template'],
  42. 'roles' => ['admin'],
  43. ],
  44. ],
  45. ],
  46. ]) ;
  47. // return [
  48. // 'verbs' => [
  49. // 'class' => VerbFilter::className(),
  50. // 'actions' => [
  51. // 'disconnect' => ['post'],
  52. // ],
  53. // ],
  54. // 'access' => [
  55. // 'class' => AccessControl::className(),
  56. // 'rules' => [
  57. // [
  58. // 'allow' => true,
  59. // 'actions' => ['account', 'confirm', 'networks', 'disconnect', 'profile', 'profile-settings'],
  60. // 'roles' => ['@'],
  61. // ],
  62. // [
  63. // 'allow' => true,
  64. // 'actions' => ['bills-template', 'download-template'],
  65. // 'roles' => ['admin'],
  66. // ],
  67. // ],
  68. // ],
  69. // ];
  70. }
  71. public function actionProfile()
  72. {
  73. $model = Yii::$app->user->identity->profile;
  74. $this->performAjaxValidation($model);
  75. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  76. Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Your profile has been updated'));
  77. return $this->refresh();
  78. }
  79. return $this->render('profile', [
  80. 'model' => $model,
  81. ]);
  82. }
  83. public function actionProfileSettings()
  84. {
  85. $model = \app\models\Profile::findOne(['user_id' => Yii::$app->user->id]);
  86. if (!$model) {
  87. throw new \yii\web\NotFoundHttpException('Профиль не найден');
  88. }
  89. $this->performAjaxValidation($model);
  90. if ($model->load(Yii::$app->request->post())) {
  91. Yii::debug($model->attributes, 'profile.save'); // логируем то, что пришло
  92. if ($model->save()) {
  93. Yii::$app->session->setFlash('success', 'Профиль обновлён');
  94. return $this->refresh();
  95. } else {
  96. Yii::error($model->errors, 'profile.errors');
  97. Yii::$app->session->setFlash('danger', 'Ошибка при сохранении');
  98. }
  99. }
  100. return $this->render('profile-settings', [
  101. 'model' => $model,
  102. ]);
  103. }
  104. public function actionBillsTemplate()
  105. {
  106. if (false != ($file = UploadedFile::getInstanceByName('billTemplate'))) {
  107. if ($file->extension !== 'docx') {
  108. Yii::$app->session->setFlash('danger', Yii::t('app', 'Можна завантажити тільки docx'));
  109. } else {
  110. $path = dirname(__DIR__) . '/web/bill-template.docx';
  111. if ($file->saveAs($path)) {
  112. Yii::$app->session->setFlash('success', Yii::t('app', 'Шаблон успішно завантажено'));
  113. return $this->refresh();
  114. }
  115. else{
  116. Yii::$app->session->setFlash('warning', Yii::t('app', 'Не вдалося завантажити файл шаблону'));
  117. }
  118. }
  119. }
  120. return $this->render('bills-template');
  121. }
  122. public function actionDownloadTemplate() {
  123. $filename = templater()->process([
  124. 'billID' => '1',
  125. 'auctionID' => '123123123',
  126. 'date' => date('d-m-Y'),
  127. 'inn' => '09876543210',
  128. 'zkpo' => '000111222',
  129. 'uAddress' => 'Ukraine, Kiev',
  130. 'bank' => 'Ukraine, Kiev, Privat Bank',
  131. 'payAccount' => '01234567890',
  132. 'phone' => '+38-050-777-1255',
  133. 'fax' => '044-586-56-87',
  134. 'firmaFull' => 'Тестове ТОВ',
  135. 'billType' => 'Гарантійний внесок',
  136. 'amountString' => num2str(100.34),
  137. 'amountVat' => 100.34,
  138. 'amount' => 100.34,
  139. 'vat' => 'без ПДВ',
  140. ]);
  141. return Yii::$app->response->sendFile($filename);
  142. }
  143. }