From 8cd2eebfee30881a67a181e7ffbc1031e0f3d17b Mon Sep 17 00:00:00 2001 From: iIronside Date: Thu, 1 Dec 2022 14:11:29 +0300 Subject: [PATCH] add company-manager role --- README.md | 1 + backend/config/main.php | 8 + .../card/views/resume-template/_form.php | 11 +- .../modules/card/views/user-card/index.php | 14 +- .../controllers/CompanyManagerController.php | 145 ++++++++++++++++++ .../modules/company/models/CompanyManager.php | 10 ++ .../company/models/CompanyManagerSearch.php | 67 ++++++++ .../company/views/company-manager/_form.php | 38 +++++ .../company/views/company-manager/_search.php | 31 ++++ .../company/views/company-manager/create.php | 18 +++ .../company/views/company-manager/index.php | 64 ++++++++ .../company/views/company-manager/update.php | 19 +++ .../company/views/company-manager/view.php | 44 ++++++ .../controllers/DocumentController.php | 26 +++- .../views/document-template/_form.php | 8 +- .../modules/document/views/document/_form.php | 91 +++++------ .../document/views/document/download.php | 14 +- backend/views/layouts/left.php | 16 +- common/models/CompanyManager.php | 74 +++++++++ common/models/Document.php | 16 +- common/models/User.php | 8 +- common/models/UserCard.php | 17 +- common/services/DocumentService.php | 9 +- composer.json | 3 +- console/controllers/RbacController.php | 14 ++ ...d_manager_id_columns_in_document_table.php | 47 ++++++ ...29_100558_create_company_manager_table.php | 33 ++++ ..._table_from_manager_to_company_manager.php | 48 ++++++ 28 files changed, 812 insertions(+), 82 deletions(-) create mode 100644 backend/modules/company/controllers/CompanyManagerController.php create mode 100644 backend/modules/company/models/CompanyManager.php create mode 100644 backend/modules/company/models/CompanyManagerSearch.php create mode 100644 backend/modules/company/views/company-manager/_form.php create mode 100644 backend/modules/company/views/company-manager/_search.php create mode 100644 backend/modules/company/views/company-manager/create.php create mode 100644 backend/modules/company/views/company-manager/index.php create mode 100644 backend/modules/company/views/company-manager/update.php create mode 100644 backend/modules/company/views/company-manager/view.php create mode 100644 common/models/CompanyManager.php create mode 100644 console/migrations/m221128_114458_make_nullable_company_id_contractor_company_id_manager_id_columns_in_document_table.php create mode 100644 console/migrations/m221129_100558_create_company_manager_table.php create mode 100644 console/migrations/m221130_090104_change_keys_in_document_table_from_manager_to_company_manager.php diff --git a/README.md b/README.md index d10a4f4..a3cba05 100755 --- a/README.md +++ b/README.md @@ -43,4 +43,5 @@ environments/ contains environment-based overrides php yii migrate --migrationPath=@yii/rbac/migrations
и выполнить консольный скрипт
php yii rbac/init
+ php yii rbac/create-company-manager-role

\ No newline at end of file diff --git a/backend/config/main.php b/backend/config/main.php index e174910..bdd19d8 100755 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -14,6 +14,12 @@ return [ 'bootstrap' => ['log'], 'modules' => [ + 'permit' => [ + 'class' => 'developeruz\db_rbac\Yii2DbRbac', + 'params' => [ + 'userClass' => 'common\models\User' + ] + ], 'accesses' => [ 'class' => 'backend\modules\accesses\Accesses', ], @@ -129,6 +135,8 @@ return [ 'controller' => 'user-questionnaire', 'except' => ['delete', 'update'], ], + '//' => '//', + '///' => '//', ], ], diff --git a/backend/modules/card/views/resume-template/_form.php b/backend/modules/card/views/resume-template/_form.php index bf0c138..b9fc6c1 100644 --- a/backend/modules/card/views/resume-template/_form.php +++ b/backend/modules/card/views/resume-template/_form.php @@ -1,8 +1,8 @@ - field($model, 'template_body')->widget(EditorClassic::className(), [ - 'clientOptions' => [ - 'language' => 'ru', - ] + field($model, 'template_body')->widget(CKEditor::className(),[ + 'editorOptions' => [ + 'preset' => 'full', + 'inline' => false, + ], ]); ?> field($model, 'header_text')->textInput(['maxlength' => true]) ?> diff --git a/backend/modules/card/views/user-card/index.php b/backend/modules/card/views/user-card/index.php index 6c73bda..dc9c5b1 100755 --- a/backend/modules/card/views/user-card/index.php +++ b/backend/modules/card/views/user-card/index.php @@ -1,9 +1,8 @@ params['breadcrumbs'][] = $this->title; ]), ], - ['class' => 'yii\grid\ActionColumn'], + ['class' => 'yii\grid\ActionColumn', + 'template' => '{view}  {update}  {permit}  {delete}', + 'buttons' => + [ + 'permit' => function ($url, $model) { + return Html::a('', Url::to(['/permit/user/view', 'id' => $model->id_user]), [ + 'title' => Yii::t('yii', 'Change user role') + ]); }, + ] + ], ], ]); echo "

Сумма зарплат: " . $searchModel->total . "

"; diff --git a/backend/modules/company/controllers/CompanyManagerController.php b/backend/modules/company/controllers/CompanyManagerController.php new file mode 100644 index 0000000..6d84fac --- /dev/null +++ b/backend/modules/company/controllers/CompanyManagerController.php @@ -0,0 +1,145 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all CompanyManager models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new CompanyManagerSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single CompanyManager model. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new CompanyManager model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new CompanyManager(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing CompanyManager model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing CompanyManager model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the CompanyManager model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return CompanyManager the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = CompanyManager::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } + + /** + * Deletes an existing CompanyManager model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDismiss($id) + { +// Debug::dd('fff'); + $model = $this->findModel($id); + $model->company_id = null; + $model->update(false); + + return $this->redirect(['index']); + } +} diff --git a/backend/modules/company/models/CompanyManager.php b/backend/modules/company/models/CompanyManager.php new file mode 100644 index 0000000..08f7b6e --- /dev/null +++ b/backend/modules/company/models/CompanyManager.php @@ -0,0 +1,10 @@ +where(['not', ['company_id' => null]]); + + // add conditions that should always apply here + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'company_id' => $this->company_id, + 'user_card_id' => $this->user_card_id, + ]); + + return $dataProvider; + } +} diff --git a/backend/modules/company/views/company-manager/_form.php b/backend/modules/company/views/company-manager/_form.php new file mode 100644 index 0000000..5726146 --- /dev/null +++ b/backend/modules/company/views/company-manager/_form.php @@ -0,0 +1,38 @@ + + +
+ + + + field($model, 'company_id')->dropDownList( + ArrayHelper::map(\backend\modules\company\models\Company::find()->all(), 'id', 'name'), + [ + 'prompt' => 'Выберите' + ] + ) ?> + + field($model, 'user_card_id')->dropDownList( + ArrayHelper::map(UserCard::getCardByUserRole('company_manager'), 'id', 'fio'), + [ + 'prompt' => 'Выберите' + ] + ) + ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/backend/modules/company/views/company-manager/_search.php b/backend/modules/company/views/company-manager/_search.php new file mode 100644 index 0000000..c1a350a --- /dev/null +++ b/backend/modules/company/views/company-manager/_search.php @@ -0,0 +1,31 @@ + + + diff --git a/backend/modules/company/views/company-manager/create.php b/backend/modules/company/views/company-manager/create.php new file mode 100644 index 0000000..21ef7f6 --- /dev/null +++ b/backend/modules/company/views/company-manager/create.php @@ -0,0 +1,18 @@ +title = 'Добавить нового менеджера компании'; +$this->params['breadcrumbs'][] = ['label' => 'Company Managers', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/modules/company/views/company-manager/index.php b/backend/modules/company/views/company-manager/index.php new file mode 100644 index 0000000..4ad7308 --- /dev/null +++ b/backend/modules/company/views/company-manager/index.php @@ -0,0 +1,64 @@ +title = 'Менеджеры компании'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

+ 'btn btn-success']) ?> +

+ + $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + +// 'id', + [ + 'attribute' => 'company_id', + 'filter' => Company::find()->select(['name', 'id'])->indexBy('id')->column(), + 'value' => 'company.name', + ], + [ + 'attribute' => 'user_card_id', + 'filter' => ArrayHelper::map(UserCard::getCardByUserRole('company_manager'), 'id', 'fio'), + 'value' => 'userCard.fio', + ], + + ['class' => 'yii\grid\ActionColumn', + 'template' => '{view}  {update}  {delete}', + 'buttons' => + [ +// 'delete' => function ($url, $model) { +// return Html::a('', Url::to(['/company/company-manager/dismiss', 'id' => $model->id]), [ +// 'title' => Yii::t('yii', 'Уволить менеджера из компании?') +// ]); }, + 'delete' => function ($url,$model) { + return Html::a( + '', + [ + '/company/company-manager/dismiss', 'id' => $model->id, + ], + [ + 'data' => ['confirm' => 'Вы уверены, что хотите удалить этого менеджера?', 'method' => 'post'] + ] + ); + }, + ] + ], + ] + ]); ?> +
diff --git a/backend/modules/company/views/company-manager/update.php b/backend/modules/company/views/company-manager/update.php new file mode 100644 index 0000000..9708535 --- /dev/null +++ b/backend/modules/company/views/company-manager/update.php @@ -0,0 +1,19 @@ +title = 'Изменить менеджера компании: ' . $model->userCard->fio; +$this->params['breadcrumbs'][] = ['label' => 'Company Managers', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/modules/company/views/company-manager/view.php b/backend/modules/company/views/company-manager/view.php new file mode 100644 index 0000000..6fca811 --- /dev/null +++ b/backend/modules/company/views/company-manager/view.php @@ -0,0 +1,44 @@ +title = 'Менеджер: ' . $model->userCard->fio; +$this->params['breadcrumbs'][] = ['label' => 'Company Managers', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + [ + 'attribute' => 'company_id', + 'value' => ArrayHelper::getValue($model, 'company.name'), + ], + [ + 'attribute' => 'user_card_id', + 'value' => ArrayHelper::getValue($model, 'userCard.fio'), + ], + ], + ]) ?> + +
diff --git a/backend/modules/document/controllers/DocumentController.php b/backend/modules/document/controllers/DocumentController.php index 5a07204..c0934bb 100644 --- a/backend/modules/document/controllers/DocumentController.php +++ b/backend/modules/document/controllers/DocumentController.php @@ -2,6 +2,7 @@ namespace backend\modules\document\controllers; +use backend\modules\company\models\CompanyManager; use backend\modules\document\models\Document; use backend\modules\document\models\DocumentSearch; use common\services\DocumentService; @@ -9,6 +10,7 @@ use Yii; use yii\filters\VerbFilter; use yii\web\Controller; use yii\web\NotFoundHttpException; +use yii\web\Response; /** * DocumentController implements the CRUD actions for Document model. @@ -175,7 +177,7 @@ class DocumentController extends Controller $model->scenario = $model::SCENARIO_DOWNLOAD_DOCUMENT; if ($model->validate()) { - DocumentService::downloadPdf($id); + DocumentService::downloadDocx($id); } Yii::$app->session->setFlash('error', $model->getFirstError('body')); @@ -183,4 +185,26 @@ class DocumentController extends Controller 'model' => Document::findOne($id) ]); } + + public function actionManagers(): array + { + Yii::$app->response->format = Response::FORMAT_JSON; + + if (isset($_POST['depdrop_parents'])) { + $parents = $_POST['depdrop_parents']; + if ($parents != null) { + $company_id = $parents[0]; + /** @var CompanyManager[] $managers */ + $managers = CompanyManager::getManagersByCompany($company_id); + + $formattedManagersArr = array(); + foreach ($managers as $manager){ + $formattedManagersArr[] = array('id' => $manager->id, 'name' => $manager->userCard->fio); + } + + return ['output'=>$formattedManagersArr, 'selected'=>'']; + } + } + return ['output'=>'', 'selected'=>'']; + } } diff --git a/backend/modules/document/views/document-template/_form.php b/backend/modules/document/views/document-template/_form.php index 21e429e..81bd785 100644 --- a/backend/modules/document/views/document-template/_form.php +++ b/backend/modules/document/views/document-template/_form.php @@ -1,6 +1,5 @@ -field($model, 'template_body')->widget(EditorClassic::className(), [ -// 'clientOptions' => [ -// 'language' => 'ru', -// ] -// ]); ?> - field($model, 'template_body')->widget(CKEditor::className(),[ 'editorOptions' => [ 'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать @@ -51,6 +44,7 @@ use yii\widgets\ActiveForm;
+

Поля договоров

diff --git a/backend/modules/document/views/document/_form.php b/backend/modules/document/views/document/_form.php index f9c6d6c..c1effaa 100644 --- a/backend/modules/document/views/document/_form.php +++ b/backend/modules/document/views/document/_form.php @@ -2,9 +2,10 @@ use backend\modules\company\models\Company; use backend\modules\document\models\DocumentTemplate; -use backend\modules\employee\models\Manager; +use kartik\depdrop\DepDrop; use kartik\select2\Select2; use yii\helpers\Html; +use yii\helpers\Url; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ @@ -16,48 +17,6 @@ use yii\widgets\ActiveForm; - field($model, 'company_id')->widget(Select2::class, - [ - 'data' => Company::find()->select(['name', 'id'])->indexBy('id')->column(), - 'options' => ['placeholder' => '...','class' => 'form-control'], - 'pluginOptions' => [ - 'allowClear' => true - ], - ] - ); ?> - - field($model, 'manager_id')->widget(Select2::class, - [ - 'data' => Manager::find()->select(['fio', 'manager.id']) - ->joinWith('userCard')->indexBy('manager.id')->column(), - 'options' => ['placeholder' => '...','class' => 'form-control'], - 'pluginOptions' => [ - 'allowClear' => true - ], - ] - ); ?> - - field($model, 'contractor_company_id')->widget(Select2::class, - [ - 'data' => Company::find()->select(['name', 'id'])->indexBy('id')->column(), - 'options' => ['placeholder' => '...','class' => 'form-control'], - 'pluginOptions' => [ - 'allowClear' => true - ], - ] - ); ?> - - field($model, 'contractor_manager_id')->widget(Select2::class, - [ - 'data' => Manager::find()->select(['fio', 'manager.id']) - ->joinWith('userCard')->indexBy('manager.id')->column(), - 'options' => ['placeholder' => '...','class' => 'form-control'], - 'pluginOptions' => [ - 'allowClear' => true - ], - ] - ); ?> - field($model, 'title')->textInput(['maxlength' => true]) ?> field($model, 'template_id')->widget(Select2::class, @@ -70,6 +29,52 @@ use yii\widgets\ActiveForm; ] ); ?> +
+

Не обязательные поля:

+
+ + field($model, 'company_id')->dropDownList( + Company::find()->select(['name', 'id'])->indexBy('id')->column(), + [ + 'id' => 'company-id', + 'prompt' => 'Выберите' + ] + ); + ?> + + field($model, 'manager_id')->widget(DepDrop::className(), + [ + 'options' => ['id' => 'manager-id'], + 'pluginOptions' => [ + 'depends' => ['company-id'], + 'placeholder' => 'Выберите', + 'url' => Url::to(['/document/document/managers']) + ] + ] + ); ?> + + field($model, 'contractor_company_id')->widget(Select2::class, + [ + 'data' => Company::find()->select(['name', 'id'])->indexBy('id')->column(), + 'options' => ['id' => 'contractor-company-id','placeholder' => '...','class' => 'form-control'], + 'pluginOptions' => [ + 'allowClear' => true + ], + ] + ); ?> + + field($model, 'contractor_manager_id')->widget(DepDrop::className(), + [ + 'options' => ['id' => 'contractor-manager-id'], + 'pluginOptions' => [ + 'depends' => ['contractor-company-id'], + 'placeholder' => 'Выберите', + 'url' => Url::to(['/document/document/managers']), + 'params' => ['contractor-company-id'] + ] + ] + ); ?> +
'btn btn-success']) ?>
diff --git a/backend/modules/document/views/document/download.php b/backend/modules/document/views/document/download.php index 1813cd9..de40f1e 100644 --- a/backend/modules/document/views/document/download.php +++ b/backend/modules/document/views/document/download.php @@ -1,6 +1,6 @@ params['breadcrumbs'][] = 'Загрузить'; 'action' => Url::to(['document/update-document-body', 'id' => $model->id]), 'options' => ['method' => 'post']]) ?> - field($model, 'body')->widget(EditorClassic::className(), [ - 'clientOptions' => [ - 'language' => 'ru', - ] - ]); ?> + + field($model, 'body')->widget(CKEditor::className(),[ + 'editorOptions' => [ + 'preset' => 'full', + 'inline' => false, + ], + ]); ?>
'btn btn-primary']) ?> diff --git a/backend/views/layouts/left.php b/backend/views/layouts/left.php index 9288e59..16789bc 100755 --- a/backend/views/layouts/left.php +++ b/backend/views/layouts/left.php @@ -30,6 +30,13 @@ ['label' => 'Шаблоны резюме', 'icon' => 'address-card ', 'url' => ['/card/resume-template'], 'active' => \Yii::$app->controller->id == 'resume-template'], ['label' => 'Шаблоны документов', 'icon' => 'file', 'url' => ['/document/document-template'], 'active' => \Yii::$app->controller->id == 'document-template'], ['label' => 'Поля документов', 'icon' => 'file-text', 'url' => ['/document/document-field'], 'active' => \Yii::$app->controller->id == 'document-field'], + [ + 'label' => 'Роли', 'icon' => 'users', 'url' => '#', + 'items' => [ + ['label' => 'Управление ролями', 'icon' => ' fa-sort-amount-asc', 'url' => ['/permit/access/role'], 'active' => \Yii::$app->controller->id == 'access'], + ['label' => 'Правила доступа', 'icon' => 'list-alt', 'url' => ['/permit/access/permission'], 'active' => \Yii::$app->controller->id == 'access'], + ] + ] ], 'visible' => Yii::$app->user->can('confidential_information') ], @@ -60,7 +67,14 @@ ], 'visible' => Yii::$app->user->can('confidential_information') ], - ['label' => 'Компании', 'icon' => 'building', 'url' => ['/company/company'], 'active' => \Yii::$app->controller->id == 'company', ], // 'visible' => Yii::$app->user->can('confidential_information') + [ + 'label' => 'Компании', 'icon' => 'building', 'url' => '#', + 'items' => [ + ['label' => 'Компании', 'icon' => 'building-o', 'url' => ['/company/company'], 'active' => \Yii::$app->controller->id == 'company'], + ['label' => 'Менеджеры компаний', 'icon' => 'user-circle-o', 'url' => ['/company/company-manager'], 'active' => \Yii::$app->controller->id == 'company-manager'], + ], + 'visible' => Yii::$app->user->can('confidential_information') + ], [ 'label' => 'Hh.ru', 'icon' => 'user-circle', 'url' => '#', 'items' => [ diff --git a/common/models/CompanyManager.php b/common/models/CompanyManager.php new file mode 100644 index 0000000..2f13bf5 --- /dev/null +++ b/common/models/CompanyManager.php @@ -0,0 +1,74 @@ + ['company_id', 'user_card_id'], 'message'=>'Этот менеджер уже закреплён за компанией'], + [['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']], + [['user_card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::className(), 'targetAttribute' => ['user_card_id' => 'id']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'company_id' => 'Компания', + 'user_card_id' => 'Менеджер', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCompany() + { + return $this->hasOne(Company::className(), ['id' => 'company_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUserCard() + { + return $this->hasOne(UserCard::className(), ['id' => 'user_card_id']); + } + + public static function getManagersByCompany($company_id): array + { + return self::find()->where(['company_id' => $company_id])->all(); + } +} diff --git a/common/models/Document.php b/common/models/Document.php index 7e773b7..d84b0af 100644 --- a/common/models/Document.php +++ b/common/models/Document.php @@ -21,9 +21,9 @@ use yii\db\Expression; * * @property Company $company * @property Company $contractorCompany - * @property Manager $contractorManager + * @property CompanyManager $contractorManager * @property DocumentTemplate $template - * @property Manager $manager + * @property CompanyManager $manager */ class Document extends \yii\db\ActiveRecord { @@ -56,19 +56,19 @@ class Document extends \yii\db\ActiveRecord public function rules() { return [ - [['company_id', 'contractor_company_id', 'manager_id', 'contractor_manager_id', 'title', 'template_id'], 'required'], + [['title', 'template_id'], 'required'], [['company_id', 'contractor_company_id', 'manager_id', 'contractor_manager_id', 'template_id'], 'integer'], [['body'], 'string'], [['created_at', 'updated_at'], 'safe'], [['title'], 'string', 'max' => 255], [['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']], [['contractor_company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['contractor_company_id' => 'id']], - [['contractor_manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => Manager::className(), 'targetAttribute' => ['contractor_manager_id' => 'id']], + [['contractor_manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => CompanyManager::className(), 'targetAttribute' => ['contractor_manager_id' => 'id']], [['template_id'], 'exist', 'skipOnError' => true, 'targetClass' => DocumentTemplate::className(), 'targetAttribute' => ['template_id' => 'id']], - [['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => Manager::className(), 'targetAttribute' => ['manager_id' => 'id']], + [['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => CompanyManager::className(), 'targetAttribute' => ['manager_id' => 'id']], ['body', 'required', 'on' => self::SCENARIO_UPDATE_DOCUMENT_BODY], ['body', function ($attribute, $params) { - preg_match_all('/(\${\w+|№|№+w})/', $this->$attribute,$out); + preg_match_all('/\${(\w+|№|№+w)}/', $this->$attribute,$out); if (!empty($out[0])) { $this->addError('body', 'В теле документа все переменные должны быть заменены!'); } @@ -125,7 +125,7 @@ class Document extends \yii\db\ActiveRecord */ public function getContractorManager() { - return $this->hasOne(Manager::className(), ['id' => 'contractor_manager_id']); + return $this->hasOne(CompanyManager::className(), ['id' => 'contractor_manager_id']); } /** @@ -133,6 +133,6 @@ class Document extends \yii\db\ActiveRecord */ public function getManager() { - return $this->hasOne(Manager::className(), ['id' => 'manager_id']); + return $this->hasOne(CompanyManager::className(), ['id' => 'manager_id']); } } diff --git a/common/models/User.php b/common/models/User.php index d724ffc..f9c66be 100755 --- a/common/models/User.php +++ b/common/models/User.php @@ -2,6 +2,7 @@ namespace common\models; +use developeruz\db_rbac\interfaces\UserRbacInterface; use Yii; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; @@ -23,7 +24,7 @@ use yii\web\UnauthorizedHttpException; * @property $access_token_expired_at * @property string $password write-only password */ -class User extends ActiveRecord implements IdentityInterface +class User extends ActiveRecord implements IdentityInterface, UserRbacInterface { const STATUS_DELETED = 0; const STATUS_ACTIVE = 10; @@ -76,6 +77,11 @@ class User extends ActiveRecord implements IdentityInterface return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); } + public function getUserName() + { + return $this->username; + } + public function generateAccessToken() { $this->access_token = Yii::$app->security->generateRandomString(); diff --git a/common/models/UserCard.php b/common/models/UserCard.php index 41ac519..bc3b283 100755 --- a/common/models/UserCard.php +++ b/common/models/UserCard.php @@ -63,7 +63,6 @@ class UserCard extends \yii\db\ActiveRecord const SCENARIO_UPDATE_RESUME_TEXT = 'update_resume_text'; const SCENARIO_DOWNLOAD_RESUME = 'download_resume_text'; -// public $resumeTemplateId; /** * @return string[] @@ -251,6 +250,14 @@ class UserCard extends \yii\db\ActiveRecord return $this->hasMany(ManagerEmployee::class, ['user_card_id' => 'id']); } + /** + * @return \yii\db\ActiveQuery + */ + public function getCompanyManagers() + { + return $this->hasMany(CompanyManager::className(), ['user_card_id' => 'id']); + } + public static function generateUserForUserCard($card_id = null) { $userCardQuery = self::find(); @@ -327,4 +334,12 @@ class UserCard extends \yii\db\ActiveRecord return $userCard['id']; } + + public static function getCardByUserRole($role): array + { + $auth = Yii::$app->authManager; + $usersId = $auth->getUserIdsByRole($role); + + return UserCard::find()->where([ 'IN', 'id_user', $usersId])->all(); + } } diff --git a/common/services/DocumentService.php b/common/services/DocumentService.php index 5a69863..ec0007b 100644 --- a/common/services/DocumentService.php +++ b/common/services/DocumentService.php @@ -54,7 +54,6 @@ class DocumentService $pdf = new Pdf(); // or new Pdf(); $mpdf = $pdf->api; // fetches mpdf api - $mpdf->SetFooter('{PAGENO}'); $mpdf->WriteHtml($model->body); // call mpdf write html echo $mpdf->Output("{$model->title}", 'D'); // call the mpdf api output as needed } @@ -67,11 +66,11 @@ class DocumentService // (B) ADD HTML CONTENT $section = $pw->addSection(); - $documentText = str_replace(array('
', '
', '
'), ' ', $model->body); - \PhpOffice\PhpWord\Shared\Html::addHtml($section, $documentText, false, false); - // (C) SAVE TO DOCX ON SERVER - // $pw->save("convert.docx", "Word2007"); + $html = str_replace(array('
', '
', '
'), ' ', $model->body); + \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false); + + $section->setStyle(); // (D) OR FORCE DOWNLOAD header("Content-Type: application/octet-stream"); diff --git a/composer.json b/composer.json index 584460e..c3d40cd 100755 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "phpoffice/phpword": "^0.18.2", "kartik-v/yii2-widget-fileinput": "@dev", "kartik-v/yii2-mpdf": "dev-master", - "mihaildev/yii2-ckeditor": "*" + "mihaildev/yii2-ckeditor": "*", + "developeruz/yii2-db-rbac": "*" }, "require-dev": { "yiisoft/yii2-debug": "~2.0.0", diff --git a/console/controllers/RbacController.php b/console/controllers/RbacController.php index 94a5c6c..dd01012 100755 --- a/console/controllers/RbacController.php +++ b/console/controllers/RbacController.php @@ -35,6 +35,20 @@ class RbacController extends Controller $auth->assign($admin, 1); } + /** + * Add company manager role + */ + public function actionCreateCompanyManagerRole() + { + $auth = Yii::$app->getAuthManager(); + + $role = $auth->createRole('company_manager'); + $role->description = 'Менеджер компании контр агента'; + $auth->add($role); + + $this->stdout('Done!' . PHP_EOL); + } + public function actionCreateEditor() { $auth = Yii::$app->authManager; diff --git a/console/migrations/m221128_114458_make_nullable_company_id_contractor_company_id_manager_id_columns_in_document_table.php b/console/migrations/m221128_114458_make_nullable_company_id_contractor_company_id_manager_id_columns_in_document_table.php new file mode 100644 index 0000000..9d29071 --- /dev/null +++ b/console/migrations/m221128_114458_make_nullable_company_id_contractor_company_id_manager_id_columns_in_document_table.php @@ -0,0 +1,47 @@ +alterColumn('document', 'company_id', $this->integer(11)->null()); + $this->alterColumn('document', 'contractor_company_id', $this->integer(11)->null()); + $this->alterColumn('document', 'manager_id', $this->integer(11)->null()); + $this->alterColumn('document', 'contractor_manager_id', $this->integer(11)->null()); + + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->alterColumn('document', 'company_id', $this->integer(11)->notNull()); + $this->alterColumn('document', 'contractor_company_id', $this->integer(11)->notNull()); + $this->alterColumn('document', 'manager_id', $this->integer(11)->notNull()); + $this->alterColumn('document', 'contractor_manager_id', $this->integer(11)->notNull()); + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m221128_114458_make_nullable_company_id_contractor_company_id_manager_id_columns_in_document_table cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/console/migrations/m221129_100558_create_company_manager_table.php b/console/migrations/m221129_100558_create_company_manager_table.php new file mode 100644 index 0000000..33449e5 --- /dev/null +++ b/console/migrations/m221129_100558_create_company_manager_table.php @@ -0,0 +1,33 @@ +createTable('{{%company_manager}}', [ + 'id' => $this->primaryKey(), + 'company_id' => $this->integer(), + 'user_card_id' => $this->integer(), + ]); + $this->addForeignKey('company_company_manager', 'company_manager', 'company_id', 'company', 'id'); + $this->addForeignKey('user_card_company_manager', 'company_manager', 'user_card_id', 'user_card', 'id'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('company_company_manager', 'company_manager'); + $this->dropForeignKey('user_card_company_manager', 'company_manager'); + $this->dropTable('{{%company_manager}}'); + } +} diff --git a/console/migrations/m221130_090104_change_keys_in_document_table_from_manager_to_company_manager.php b/console/migrations/m221130_090104_change_keys_in_document_table_from_manager_to_company_manager.php new file mode 100644 index 0000000..33f5232 --- /dev/null +++ b/console/migrations/m221130_090104_change_keys_in_document_table_from_manager_to_company_manager.php @@ -0,0 +1,48 @@ +dropForeignKey('manager_document', 'document'); + $this->dropForeignKey('contractor_manager_document', 'document'); + + $this->addForeignKey('company_manager_document', 'document', 'manager_id', 'company_manager', 'id'); + $this->addForeignKey('contractor_company_manager_document', 'document', 'contractor_manager_id', 'company_manager', 'id'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('company_manager_document', 'document'); + $this->dropForeignKey('contractor_company_manager_document', 'document'); + + $this->addForeignKey('manager_document', 'document', 'manager_id','manager', 'id'); + $this->addForeignKey('contractor_manager_document', 'document', 'contractor_manager_id','manager', 'id'); + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m221130_090104_change_keys_in_document_table_from_manager_to_company_manager cannot be reverted.\n"; + + return false; + } + */ +}