guild/backend/modules/card/controllers/UserCardController.php

387 lines
14 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace backend\modules\card\controllers;
2022-11-03 17:46:36 +03:00
use backend\modules\card\models\ResumeTemplate;
2022-10-11 10:39:22 +03:00
use backend\modules\card\models\UserCard;
use backend\modules\card\models\UserCardSearch;
2022-11-03 17:46:36 +03:00
use backend\modules\settings\models\Skill;
use common\models\AchievementUserCard;
2018-10-12 14:52:08 +03:00
use common\models\CardSkill;
2019-06-27 15:41:58 +03:00
use common\models\FieldsValueNew;
2022-10-11 10:39:22 +03:00
use common\models\User;
use kartik\mpdf\Pdf;
2018-10-11 11:15:09 +03:00
use Yii;
use yii\data\ActiveDataProvider;
2020-01-29 11:26:01 +03:00
use yii\filters\AccessControl;
2022-10-11 10:39:22 +03:00
use yii\filters\VerbFilter;
2018-10-11 11:15:09 +03:00
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* UserCardController implements the CRUD actions for UserCard model.
*/
class UserCardController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
2020-01-29 11:26:01 +03:00
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
2021-08-12 13:57:00 +03:00
'roles' => ['admin', 'profileEditor'],
2020-01-29 11:26:01 +03:00
],
],
],
2018-10-11 11:15:09 +03:00
];
}
/**
* Lists all UserCard models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new UserCardSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
2020-01-23 13:16:54 +03:00
public function actionPassword($id)
{
$user_card = UserCard::findOne($id);
$model = User::findOne(['id' => $user_card->id_user]);
return $this->render('password', [
'model' => $model,
]);
}
public function actionAjax() {
if(Yii::$app->request->isAjax) {
$id = $_POST['id'];
$password = $_POST['password'];
$user_card = UserCard::findOne($id);
$user = User::findOne(['id' => $user_card->id_user]);
$user->password = $password;
$user->save();
}
}
2018-10-11 11:15:09 +03:00
/**
* Displays a single UserCard model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
$dataProvider = new ActiveDataProvider([
2019-06-27 15:41:58 +03:00
'query' => FieldsValueNew::find()
->where(['item_id' => $id, 'item_type' => FieldsValueNew::TYPE_PROFILE])
->orderBy('order'),
2018-10-11 11:15:09 +03:00
'pagination' => [
'pageSize' => 200,
],
]);
2018-10-12 14:52:08 +03:00
$skills = CardSkill::find()->where(['card_id' => $id])->with('skill')->all();
$achievements =
AchievementUserCard::find()->where(['user_card_id' => $id])
->innerJoinWith(['achievement' => function($query) {
$query->andWhere(['status' => \common\models\Achievement::STATUS_ACTIVE]);
}])
->all();
2018-10-12 14:52:08 +03:00
$id_current_user = $this->findModel($id)->id_user;
2020-08-04 10:47:53 +03:00
$changeDataProvider = new ActiveDataProvider([
'query' => \common\models\ChangeHistory::find()->where(['type_id' => $this->findModel($id)->id]),
'pagination' => [
'pageSize' => 200,
]
]);
2018-10-11 11:15:09 +03:00
return $this->render('view', [
'model' => $this->findModel($id),
2020-08-04 10:47:53 +03:00
'modelFieldValue' => $dataProvider,
2018-10-12 14:52:08 +03:00
'skills' => $skills,
'achievements' => $achievements,
'userData' => User::findOne($id_current_user),
2020-08-04 10:47:53 +03:00
'changeDataProvider' => $changeDataProvider,
2018-10-11 11:15:09 +03:00
]);
}
/**
* Creates a new UserCard model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new UserCard();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
UserCard::generateUserForUserCard($model->id);
2018-10-11 11:15:09 +03:00
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing UserCard 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);
2021-08-09 18:23:56 +03:00
if ($model->load(Yii::$app->request->post())) {
$model->updated_at = date('Y-m-d h:i:s');
$model->save();
2018-10-11 11:15:09 +03:00
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing UserCard 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)
{
2018-11-06 11:44:27 +03:00
//CardSkill::deleteAll(['card_id' => $id]);
//FieldsValue::deleteAll(['card_id' => $id]);
$model = $this->findModel($id);
$d = new \DateTime();
$model->deleted_at = $d->format('Y-m-d H:i');
$model->save();
2018-10-11 11:15:09 +03:00
return $this->redirect(['index']);
}
/**
* Finds the UserCard model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return UserCard the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = UserCard::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
2022-10-11 10:39:22 +03:00
2022-11-03 17:46:36 +03:00
public function actionResume($id): string
{
return $this->render('resume', [
'model' => UserCard::findOne($id)
]);
}
/**
* @param integer $id
* @throws NotFoundHttpException
*/
public function actionResumeTextByTemplate(int $id): string
{
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_GENERATE_RESUME_TEXT;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$this->generateText($model);
$model->save(false);
}
return $this->render('resume', [
'model' => $model
]);
}
/**
* @param integer $id
* @throws NotFoundHttpException
*/
public function actionUpdateResumeText($id)
2022-10-11 10:39:22 +03:00
{
$model = $this->findModel($id);
2022-11-03 17:46:36 +03:00
$model->scenario = $model::SCENARIO_UPDATE_RESUME_TEXT;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->updated_at = date('Y-m-d h:i:s');
$model->save();
}
return $this->render('resume', [
'model' => $model
]);
}
private function generateText(UserCard $userCard) {
2022-11-14 13:48:31 +03:00
$resumeTemplate = ResumeTemplate::findOne($userCard->resume_template_id);
2022-11-03 17:46:36 +03:00
$resumeText = $resumeTemplate->template_body;
2022-10-11 10:39:22 +03:00
2022-11-03 17:46:36 +03:00
foreach (ResumeTemplate::$fieldSignatureDbName as $fieldSignature => $fieldDbName ) {
if (str_contains($resumeText, $fieldSignature)) {
if($fieldDbName == 'position_id') {
$fieldValue = $userCard->position->name;
} elseif ($fieldDbName == 'gender') {
$fieldValue = $userCard->getGendersText();
} elseif ($fieldDbName == 'level') {
$fieldValue = UserCard::getLevelLabel($userCard->level);
} elseif($fieldDbName == 'skills') {
$skills = Skill::find()->select('name')
->joinWith('cardSkills')
->where(['card_skill.card_id' => $userCard->id])
->column();
$fieldValue = implode(', ', $skills);
} else {
$fieldValue = $userCard[$fieldDbName];
}
$resumeText = str_replace($fieldSignature, $fieldValue, $resumeText);
}
2022-10-11 10:39:22 +03:00
}
2022-11-03 17:46:36 +03:00
$userCard->resume_text = $resumeText;
2022-10-11 10:39:22 +03:00
}
2022-11-21 13:54:58 +03:00
public function actionDownloadResume(int $id, string $type)
2022-10-11 10:39:22 +03:00
{
2022-11-21 13:54:58 +03:00
$model = $this->findModel($id);
$model->scenario = $model::SCENARIO_DOWNLOAD_RESUME;
2022-11-14 13:48:31 +03:00
2022-11-21 13:54:58 +03:00
if ($model->validate()) {
if ($type == 'pdf') {
$this->downloadResumePdf($model);
} elseif ($type == 'docx') {
$this->downloadResumeDocx($model);
}
2022-11-14 13:48:31 +03:00
}
2022-11-03 17:46:36 +03:00
2022-11-21 13:54:58 +03:00
return $this->render('resume', [
'model' => $model
]);
}
private function downloadResumePdf(UserCard $userCard)
{
// $userCard = UserCard::findOne($id);
$resumeTemplate = ResumeTemplate::findOne($userCard->resume_template_id);
$headerText = $resumeTemplate->header_text ? : 'Generated by ITGuild.info At: ' . date("d/m/Y");
$headerImagePath = $resumeTemplate->header_image ? Yii::getAlias('@frontend') . '/web' . $resumeTemplate->header_image : null;
$footerText = $resumeTemplate->footer_text ?? null;
$footerImg = $resumeTemplate->footer_image ? Yii::getAlias('@frontend') . '/web' . $resumeTemplate->footer_image : null;
2022-11-14 13:48:31 +03:00
$pdf = new Pdf();
$mpdf = $pdf->api;
2022-11-21 15:26:08 +03:00
if (!pathinfo($resumeTemplate->header_image, PATHINFO_EXTENSION)) {
$mpdf->SetHeader($headerText);
} else {
$imagePath = Yii::getAlias('@frontend') . '/web' . $resumeTemplate->header_image;
$mpdf->setAutoTopMargin='stretch';
$mpdf->SetHTMLHeader(
"<div style='border-bottom: 1px solid #999;'>
<p><img src=$imagePath style='width: 100px; height: 40px; margin: 0; vertical-align: middle;'/>$headerText</p>
</div>"
);
}
2022-11-21 13:54:58 +03:00
2022-11-21 15:26:08 +03:00
// $mpdf->SetHTMLHeader("
// <div><table style='width:100%; height:100%; border-bottom: 1px solid #999; font-family: serif; font-size: 8pt; color:
// #000000; font-weight: bold; font-style: italic;'><tr>
// <td width='33%'><span style='font-weight: bold; font-style: italic;'><img src=$headerImagePath style='width: 100px; height: 40px; margin: 0; vertical-align: middle;'></span></td>
// <td width='33%' align='center' style='font-weight: bold; font-style: italic;'>$headerText</td>
// <td width='33%' style='text-align: right; '></td>
// </tr></table></div>
// ");
// $mpdf->SetHTMLFooter("
// <div><table width='100%' style='border-top: 1px solid #999; vertical-align: bottom; font-family: serif; font-size: 8pt; color:
// #000000; font-weight: bold; font-style: italic;'><tr>
// <td width='33%'><span style='font-weight: bold; font-style: italic;'><img src=$footerImg style='width: 100px; height: 40px; margin: 0; vertical-align: middle;'></span></td>
// <td width='33%' align='center' style='font-weight: bold; font-style: italic;'>$footerText</td>
// <td width='33%' style='text-align: right; '>{PAGENO}</td>
// </tr></table></div>
// ");
2022-11-14 13:48:31 +03:00
$mpdf->WriteHTML("<div>$userCard->resume_text</div>");
2022-11-21 15:26:08 +03:00
$mpdf->SetFooter('{PAGENO}');
2022-11-14 13:48:31 +03:00
$mpdf->Output("Resume - {$userCard->fio}", 'D'); // call the mpdf api output as needed
exit;
2022-10-11 10:39:22 +03:00
}
2022-11-21 13:54:58 +03:00
private function downloadResumeDocx(UserCard $model)
2022-10-11 10:39:22 +03:00
{
2022-11-14 13:48:31 +03:00
$resumeTemplate = ResumeTemplate::findOne($model->resume_template_id);
2022-11-03 17:46:36 +03:00
2022-11-21 13:54:58 +03:00
$headerText = $resumeTemplate->header_text ? : 'Generated by ITGuild.info At: ' . date("d/m/Y");
$footerText = $resumeTemplate->footer_text ?? null;
2022-11-14 13:48:31 +03:00
2022-11-14 15:11:46 +03:00
$pw = new \PhpOffice\PhpWord\PhpWord();
2022-11-14 13:48:31 +03:00
2022-11-03 17:46:36 +03:00
// (B) ADD HTML CONTENT
$section = $pw->addSection();
2022-11-14 15:11:46 +03:00
$header = $section->addHeader();
2022-11-21 13:54:58 +03:00
$footer = $section->addFooter();
2022-11-14 13:48:31 +03:00
2022-11-14 15:11:46 +03:00
if (pathinfo($resumeTemplate->header_image, PATHINFO_EXTENSION)) {
2022-11-21 13:54:58 +03:00
$header->addImage(Yii::getAlias('@frontend') . '/web' . $resumeTemplate->header_image, ['width' => 70, 'height' => 30, 'align' => 'left']);
2022-11-14 15:11:46 +03:00
}
$header->addText($headerText, array('bold' => false), array('space' => array('before' => 0, 'after' => 280)));
2022-11-21 13:54:58 +03:00
if (pathinfo($resumeTemplate->footer_image, PATHINFO_EXTENSION)) {
$footer->addImage(Yii::getAlias('@frontend') . '/web' . $resumeTemplate->footer_image, ['width' => 70, 'height' => 30, 'align' => 'left']);
}
$footer->addText($footerText, array('bold' => false), array('space' => array('before' => 0, 'after' => 280)));
$footer->addPreserveText('{PAGE}', null, ['align' => 'right']);
2022-11-14 13:48:31 +03:00
2022-11-08 14:15:31 +03:00
$resumeText = str_replace(array('<br/>', '<br>', '</br>'), ' ', $model->resume_text);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $resumeText, false, false);
2022-11-03 17:46:36 +03:00
// (C) SAVE TO DOCX ON SERVER
// $pw->save("convert.docx", "Word2007");
// (D) OR FORCE DOWNLOAD
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=\"Resume-$model->fio.docx\"");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pw, "Word2007");
2022-10-11 10:39:22 +03:00
$objWriter->save("php://output");
2022-11-03 17:46:36 +03:00
exit();
2022-10-11 10:39:22 +03:00
}
2018-10-11 11:15:09 +03:00
}