Merge pull request #99 from apuc/get_a_resume_as_a_file

Get a resume as a file
This commit is contained in:
2022-11-07 17:36:28 +03:00
committed by GitHub
21 changed files with 1046 additions and 46 deletions

View File

@ -0,0 +1,127 @@
<?php
namespace backend\modules\card\controllers;
use Yii;
use backend\modules\card\models\ResumeTemplate;
use backend\modules\card\models\ResumeTemplateSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* ResumeTemplateController implements the CRUD actions for ResumeTemplate model.
*/
class ResumeTemplateController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all ResumeTemplate models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ResumeTemplateSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single ResumeTemplate 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 ResumeTemplate model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new ResumeTemplate();
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 ResumeTemplate 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 ResumeTemplate 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 ResumeTemplate model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return ResumeTemplate the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = ResumeTemplate::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -2,14 +2,15 @@
namespace backend\modules\card\controllers;
use backend\modules\card\models\ResumeTemplate;
use backend\modules\card\models\UserCard;
use backend\modules\card\models\UserCardSearch;
use backend\modules\settings\models\Skill;
use common\models\AchievementUserCard;
use common\models\CardSkill;
use common\models\FieldsValueNew;
use common\models\User;
use kartik\mpdf\Pdf;
use PhpOffice\PhpWord\PhpWord;
use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\AccessControl;
@ -202,55 +203,109 @@ class UserCardController extends Controller
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionDownloadResume($id, $pdf = false)
public function actionResume($id): string
{
$model = $this->findModel($id);
if ($pdf) {
self::getResumePdf($model);
}
self::getResumeDocx($model);
return $this->render('resume', [
'model' => UserCard::findOne($id)
]);
}
private function getResumePdf(UserCard $model)
/**
* @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)
{
$model = $this->findModel($id);
$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) {
$resumeTemplate = ResumeTemplate::findOne($userCard->resumeTemplateId);
$resumeText = $resumeTemplate->template_body;
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);
}
}
$userCard->resume_text = $resumeText;
}
public function actionDownloadResumePdf($id)
{
$model = UserCard::findOne($id);
$pdf = new Pdf(); // or new Pdf();
$mpdf = $pdf->api; // fetches mpdf api
$mpdf->SetHeader('Resume ' . $model->fio . '||Generated At: ' . date("d/m/Y")); // call methods or set any properties
$mpdf->SetFooter('{PAGENO}');
$mpdf->WriteHtml($model->vc_text); // call mpdf write html
$mpdf->WriteHtml($model->resume_text); // call mpdf write html
echo $mpdf->Output("Resume - {$model->fio}", 'D'); // call the mpdf api output as needed
}
private function getResumeDocx(UserCard $model)
public function actionDownloadResumeDocx($id)
{
$phpWord = new PhpWord();
$model = UserCard::findOne($id);
$sectionStyle = array(
'orientation' => 'portrait',
'marginTop' => \PhpOffice\PhpWord\Shared\Converter::pixelToTwip(10),
'marginLeft' => 600,
'marginRight' => 600,
'colsNum' => 1,
'pageNumberingStart' => 1,
'borderBottomSize'=>100,
'borderBottomColor'=>'C0C0C0'
);
$section = $phpWord->addSection($sectionStyle);
$text = $model->vc_text;
$fontStyle = array('name'=>'Times New Roman', 'size'=>14, 'color'=>'000000', 'bold'=>FALSE, 'italic'=>FALSE);
$parStyle = array('align'=>'both','spaceBefore'=>10);
$pw = new \PhpOffice\PhpWord\PhpWord();
$section->addText(htmlspecialchars($text), $fontStyle,$parStyle);
// (B) ADD HTML CONTENT
$section = $pw->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $model->resume_text, false, false);
header("Content-Type: application/msword");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment;filename=Resume - {$model->fio}.docx");
header('Cache-Control: max-age=0');
// (C) SAVE TO DOCX ON SERVER
// $pw->save("convert.docx", "Word2007");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
ob_clean();
// (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");
$objWriter->save("php://output");
exit;
exit();
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace backend\modules\card\models;
class ResumeTemplate extends \common\models\ResumeTemplate
{
}

View File

@ -0,0 +1,72 @@
<?php
namespace backend\modules\card\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\card\models\ResumeTemplate;
/**
* ResumeTemplateSearch represents the model behind the search form of `backend\modules\card\models\ResumeTemplate`.
*/
class ResumeTemplateSearch extends ResumeTemplate
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'status'], 'integer'],
[['title', 'created_at', 'updated_at', 'template_body'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = ResumeTemplate::find();
// 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,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'status' => $this->status,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'template_body', $this->template_body]);
return $dataProvider;
}
}

View File

@ -123,7 +123,7 @@ class UserCard extends \common\models\UserCard
$fieldsValue->save();
}
}
if (is_array($post['fields'])) {
if (array_key_exists('fields', $post) && is_array($post['fields'])) {
CardSkill::deleteAll(['card_id' => $this->id]);
if (is_array($post['skill']))
foreach ($post['skill'] as $item) {
@ -135,7 +135,7 @@ class UserCard extends \common\models\UserCard
}
}
if(is_array($post['achievements'])){
if(array_key_exists('achievements', $post) && is_array($post['achievements'])){
AchievementUserCard::deleteAll(['user_card_id' => $this->id]);
foreach ($post['achievements'] as $item) {

View File

@ -0,0 +1,144 @@
<?php
use asmoday74\ckeditor5\EditorClassic;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\ResumeTemplate */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="row">
<div class="col-md-8">
<div class="resume-template-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'status')->dropDownList(
StatusHelper::statusList(),
[
'prompt' => 'Выберите'
]
) ?>
<?= $form->field($model, 'template_body')->widget(EditorClassic::className(), [
'clientOptions' => [
'language' => 'ru',
]
]); ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
<div class="col-md-4">
<div class="table-responsive">
<table class="table" id="fieldNameTable">
<thead>
<tr>
<th>Поле</th>
<th>Сигнатура поля</th>
</tr>
</thead>
<tbody>
<tr class="info">
<td>ФИО</td>
<td class="table-cell">${fio}</td>
</tr>
<tr class="info">
<td>Паспорт</td>
<td class="table-cell">${passport}</td>
</tr>
<tr class="info">
<td>Электронная почта</td>
<td class="table-cell">${email}</td>
</tr>
<tr class="info">
<td>Пол</td>
<td class="table-cell">${gender}</td>
</tr>
<tr class="info">
<td>Резюме</td>
<td class="table-cell">${resume}</td>
</tr>
<tr class="info">
<td>Зароботная плата</td>
<td class="table-cell">${salary}</td>
</tr>
<tr class="info">
<td>Позиция</td>
<td class="table-cell">${position_id}</td>
</tr>
<tr class="info">
<td>Город</td>
<td class="table-cell">${city}</td>
</tr>
<tr class="info">
<td>Ссылка ВК</td>
<td class="table-cell">${link_vk}</td>
</tr>
<tr class="info">
<td>Ссылка Телграм</td>
<td class="table-cell">${link_telegram}</td>
</tr>
<tr class="info">
<td>Резюме текст</td>
<td class="table-cell">${vc_text}</td>
</tr>
<tr class="info">
<td>Уровень</td>
<td class="table-cell">${level}</td>
</tr>
<tr class="info">
<td>Резюме текст</td>
<td class="table-cell">${vc_text}</td>
</tr>
<tr class="info">
<td>Резюме короткий текст</td>
<td class="table-cell">${vc_text_short}</td>
</tr>
<tr class="info">
<td>Лет опыта</td>
<td class="table-cell">${years_of_exp}/td>
</tr>
<tr class="info">
<td>Спецификация</td>
<td class="table-cell">${specification}</td>
</tr>
<tr class="info">
<td>Навыки</td>
<td class="table-cell">${skills}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!--<script>-->
<!-- document.querySelectorAll(".table-cell").forEach(function(elm){-->
<!-- elm.addEventListener("mouseover", function(e){-->
<!-- e.target.style.backgroundColor = '#76d7c4';-->
<!-- var copyText = e.target.textContent;-->
<!-- const el = document.createElement('textarea');-->
<!-- el.value = copyText;-->
<!-- document.body.appendChild(el);-->
<!-- el.select();-->
<!-- document.execCommand('copy');-->
<!-- document.body.removeChild(el);-->
<!---->
<!-- /* Alert the copied text */-->
<!-- alert("Copied the text: " + el.value);-->
<!-- });-->
<!-- })-->
<!--</script>-->

View File

@ -0,0 +1,37 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\ResumeTemplateSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="resume-template-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'created_at') ?>
<?= $form->field($model, 'updated_at') ?>
<?= $form->field($model, 'status') ?>
<?php // echo $form->field($model, 'template_body') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,18 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\ResumeTemplate */
$this->title = 'Создать шаблон резюме';
$this->params['breadcrumbs'][] = ['label' => 'Resume Templates', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="resume-template-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,39 @@
<?php
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\card\models\ResumeTemplateSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Шаблоны резюме';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="resume-template-index">
<p>
<?= Html::a('Создать шаблон', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'title',
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => function($model){
return StatusHelper::statusLabel($model->status);
}
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,19 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\ResumeTemplate */
$this->title = 'Изменить шаблон: ' . $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Resume Templates', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->title, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="resume-template-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,47 @@
<?php
use common\helpers\StatusHelper;
use yii\grid\GridView;
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\ResumeTemplate */
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Resume Templates', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="resume-template-view">
<p>
<?= Html::a('Список', ['index', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'title',
'created_at',
'updated_at',
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => StatusHelper::statusLabel($model->status),
],
'template_body:ntext'
],
]) ?>
</div>

View File

@ -0,0 +1,65 @@
<?php
use asmoday74\ckeditor5\EditorClassic;
use backend\modules\card\models\ResumeTemplate;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\card\models\UserCard */
$this->title = 'Резюме: ' . $model->fio;
$this->params['breadcrumbs'][] = ['label' => 'Профили', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Резюме';
?>
<div class="resume-form">
<?php $form = ActiveForm::begin([
'id' => 'text-by-template-form',
'action' => Url::to(['user-card/resume-text-by-template', 'id' => $model->id]),
'options' => ['method' => 'post']])
?>
<?= Html::hiddenInput('id', $model->id); ?>
<?= $form->field($model, 'resumeTemplateId')->dropDownList(
ResumeTemplate::find()->where(['status' => StatusHelper::STATUS_ACTIVE])->select(['title', 'id'])->indexBy('id')->column(),
['prompt' => 'Выберите'])
?>
<div class="form-group">
<?= Html::submitButton('Сгенерировать резюме по шаблону', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<div class="resume-form">
<?php $form = ActiveForm::begin([
'id' => 'update-resume-text-form',
'action' => Url::to(['user-card/update-resume-text', 'id' => $model->id]),
'options' => ['method' => 'post']])
?>
<?= $form->field($model, 'resume_text')->widget(EditorClassic::className(), [
'clientOptions' => [
'language' => 'ru',
]
]); ?>
<div class="form-group">
<?= Html::submitButton('Сохраниить изменения', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<div class="resume-form">
<p>
<?= Html::a('Скачать pdf', ['download-resume-pdf', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
<?= Html::a('Скачать docx', ['download-resume-docx', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
</p>
</div>

View File

@ -24,6 +24,7 @@ $this->params['breadcrumbs'][] = $this->title;
<p>
<?= Html::a('Список', ['index'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Редактировать', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Резюме', ['user-card/resume', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
</p>
<?= DetailView::widget([
@ -101,11 +102,6 @@ $this->params['breadcrumbs'][] = $this->title;
],
]) ?>
<p>
<?= Html::a('Resume pdf', ['download-resume', 'id' => $model->id, 'pdf' => true], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Resume docx', ['download-resume', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
</p>
<h2>Навыки</h2>
<?php foreach ($skills as $skill) : ?>
<span class="btn btn-default btn-sm"><?= $skill['skill']->name; ?></span>

View File

@ -65,7 +65,7 @@ class ManagerEmployeeController extends Controller
*/
public function actionCreate()
{
$post = $post = \Yii::$app->request->post('ManagerEmployee');
$post = \Yii::$app->request->post('ManagerEmployee');
if (!empty($post)) {
$user_card_id_arr = ArrayHelper::getValue($post,'user_card_id');