From 18dd5a5d36db9bf6854c9d773584909fafeeac4b Mon Sep 17 00:00:00 2001 From: iIronside Date: Mon, 21 Nov 2022 13:54:58 +0300 Subject: [PATCH] add footer to resume --- .../card/controllers/UserCardController.php | 81 ++++++++++++------- .../card/models/ResumeTemplateSearch.php | 7 +- .../card/views/resume-template/_form.php | 23 ++++++ .../card/views/resume-template/_search.php | 4 + .../modules/card/views/user-card/resume.php | 4 +- common/models/ResumeTemplate.php | 6 +- common/models/UserCard.php | 3 +- ...8_133250_add_footer_to_resume_template.php | 42 ++++++++++ 8 files changed, 136 insertions(+), 34 deletions(-) create mode 100644 console/migrations/m221118_133250_add_footer_to_resume_template.php diff --git a/backend/modules/card/controllers/UserCardController.php b/backend/modules/card/controllers/UserCardController.php index 7c1211c..abf8cf9 100755 --- a/backend/modules/card/controllers/UserCardController.php +++ b/backend/modules/card/controllers/UserCardController.php @@ -276,59 +276,84 @@ class UserCardController extends Controller $userCard->resume_text = $resumeText; } - public function actionDownloadResumePdf($id) + public function actionDownloadResume(int $id, string $type) { - $userCard = UserCard::findOne($id); + $model = $this->findModel($id); + $model->scenario = $model::SCENARIO_DOWNLOAD_RESUME; + + if ($model->validate()) { + if ($type == 'pdf') { + $this->downloadResumePdf($model); + } elseif ($type == 'docx') { + $this->downloadResumeDocx($model); + } + } + + return $this->render('resume', [ + 'model' => $model + ]); + } + + private function downloadResumePdf(UserCard $userCard) + { +// $userCard = UserCard::findOne($id); $resumeTemplate = ResumeTemplate::findOne($userCard->resume_template_id); - if (empty($resumeTemplate->header_text)) { - $headerText = 'Generated by ITGuild.info At: ' . date("d/m/Y"); - } else { - $headerText = $resumeTemplate->header_text; - } + + $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; $pdf = new Pdf(); $mpdf = $pdf->api; - if (!pathinfo($resumeTemplate->header_image, PATHINFO_EXTENSION)) { - $mpdf->SetHeader($headerText); - } else { - $imagePath = Yii::getAlias('@frontend') . '/web' . $resumeTemplate->header_image; - $mpdf->setAutoTopMargin='stretch'; - $mpdf->SetHTMLHeader( - "
-

$headerText

-
" - ); - } - $mpdf->SetFooter('{PAGENO}'); + $mpdf->SetHTMLHeader(" + + + + +
$headerText{PAGENO}
+ "); + $mpdf->SetHTMLFooter(" + + + + +
$footerText{PAGENO}
+ "); + $mpdf->WriteHTML("
$userCard->resume_text
"); $mpdf->Output("Resume - {$userCard->fio}", 'D'); // call the mpdf api output as needed exit; } - public function actionDownloadResumeDocx($id) + private function downloadResumeDocx(UserCard $model) { - $model = UserCard::findOne($id); $resumeTemplate = ResumeTemplate::findOne($model->resume_template_id); - $imagePath = Yii::getAlias('@frontend') . '/web' . $resumeTemplate->header_image; - if (empty($resumeTemplate->header_text)) { - $headerText = 'Generated by ITGuild.info At: ' . date("d/m/Y"); - } else { - $headerText = $resumeTemplate->header_text; - } + $headerText = $resumeTemplate->header_text ? : 'Generated by ITGuild.info At: ' . date("d/m/Y"); + $footerText = $resumeTemplate->footer_text ?? null; $pw = new \PhpOffice\PhpWord\PhpWord(); // (B) ADD HTML CONTENT $section = $pw->addSection(); $header = $section->addHeader(); + $footer = $section->addFooter(); if (pathinfo($resumeTemplate->header_image, PATHINFO_EXTENSION)) { - $header->addImage($imagePath, ['width' => 70, 'height' => 30, 'align' => 'left']); + $header->addImage(Yii::getAlias('@frontend') . '/web' . $resumeTemplate->header_image, ['width' => 70, 'height' => 30, 'align' => 'left']); } $header->addText($headerText, array('bold' => false), array('space' => array('before' => 0, 'after' => 280))); + 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']); $resumeText = str_replace(array('
', '
', '
'), ' ', $model->resume_text); \PhpOffice\PhpWord\Shared\Html::addHtml($section, $resumeText, false, false); diff --git a/backend/modules/card/models/ResumeTemplateSearch.php b/backend/modules/card/models/ResumeTemplateSearch.php index cb0ab36..c1b0fb1 100644 --- a/backend/modules/card/models/ResumeTemplateSearch.php +++ b/backend/modules/card/models/ResumeTemplateSearch.php @@ -18,7 +18,7 @@ class ResumeTemplateSearch extends ResumeTemplate { return [ [['id', 'status'], 'integer'], - [['title', 'created_at', 'updated_at', 'template_body', 'header_text', 'header_image'], 'safe'], + [['title', 'created_at', 'updated_at', 'template_body', 'header_text', 'header_image', 'footer_text', 'footer_image'], 'safe'], ]; } @@ -67,7 +67,10 @@ class ResumeTemplateSearch extends ResumeTemplate $query->andFilterWhere(['like', 'title', $this->title]) ->andFilterWhere(['like', 'template_body', $this->template_body]) ->andFilterWhere(['like', 'header_text', $this->header_text]) - ->andFilterWhere(['like', 'header_image', $this->header_image]); + ->andFilterWhere(['like', 'header_image', $this->header_image]) + ->andFilterWhere(['like', 'footer_text', $this->footer_text]) + ->andFilterWhere(['like', 'footer_image', $this->footer_image]); + return $dataProvider; } } diff --git a/backend/modules/card/views/resume-template/_form.php b/backend/modules/card/views/resume-template/_form.php index 4c436b9..bf0c138 100644 --- a/backend/modules/card/views/resume-template/_form.php +++ b/backend/modules/card/views/resume-template/_form.php @@ -56,6 +56,29 @@ use yii\widgets\ActiveForm; ?> + field($model, 'footer_text')->textInput(['maxlength' => true]) ?> + +
+
+ +
+ 'ru', + 'controller' => 'elfinder', + // вставляем название контроллера, по умолчанию равен elfinder + 'filter' => 'image', + // фильтр файлов, можно задать массив фильтров https://github.com/Studio-42/elFinder/wiki/Client-con.. + 'name' => 'ResumeTemplate[footer_image]', + 'id' => 'resumeTemplateFooter_img', + 'template' => '
{input}{button}
', + 'options' => ['class' => 'form-control itemImg', 'maxlength' => '255'], + 'buttonOptions' => ['class' => 'btn btn-primary'], + 'value' => $model->footer_image, + 'buttonName' => 'Выбрать изображение', + ]); + ?> +
+
'btn btn-success']) ?>
diff --git a/backend/modules/card/views/resume-template/_search.php b/backend/modules/card/views/resume-template/_search.php index 77a3fe2..2ccb94b 100644 --- a/backend/modules/card/views/resume-template/_search.php +++ b/backend/modules/card/views/resume-template/_search.php @@ -31,6 +31,10 @@ use yii\widgets\ActiveForm; field($model, 'header_image') ?> + field($model, 'footer_text') ?> + + field($model, 'footer_image') ?> +
'btn btn-primary']) ?> 'btn btn-default']) ?> diff --git a/backend/modules/card/views/user-card/resume.php b/backend/modules/card/views/user-card/resume.php index 3c41e79..329848e 100644 --- a/backend/modules/card/views/user-card/resume.php +++ b/backend/modules/card/views/user-card/resume.php @@ -64,7 +64,7 @@ $this->params['breadcrumbs'][] = 'Резюме';

- $model->id], ['class' => 'btn btn-success']) ?> - $model->id], ['class' => 'btn btn-success']) ?> + $model->id, 'type' => 'pdf'], ['class' => 'btn btn-success']) ?> + $model->id, 'type' => 'docx'], ['class' => 'btn btn-success']) ?>

\ No newline at end of file diff --git a/common/models/ResumeTemplate.php b/common/models/ResumeTemplate.php index e498d7a..ece9652 100644 --- a/common/models/ResumeTemplate.php +++ b/common/models/ResumeTemplate.php @@ -18,6 +18,8 @@ use yii\helpers\ArrayHelper; * @property string $template_body * @property string $header_text * @property string $header_image + * @property string $footer_text + * @property string $footer_image */ class ResumeTemplate extends \yii\db\ActiveRecord { @@ -91,7 +93,7 @@ class ResumeTemplate extends \yii\db\ActiveRecord [['created_at', 'updated_at'], 'safe'], [['status'], 'integer'], [['template_body'], 'string'], - [['title', 'header_text', 'header_image'], 'string', 'max' => 255], + [['title', 'header_text', 'header_image', 'footer_text', 'footer_image'], 'string', 'max' => 255], ]; } @@ -109,6 +111,8 @@ class ResumeTemplate extends \yii\db\ActiveRecord 'template_body' => 'Тело шаблона', 'header_text' => 'Текст в верхнем контикуле', 'header_image' => 'Картинка в верхнем контикуле', + 'footer_text' => 'Текст в нижнем контикуле', + 'footer_image' => 'Картинка в нижнем контикуле', ]; } } diff --git a/common/models/UserCard.php b/common/models/UserCard.php index 44eabd9..41ac519 100755 --- a/common/models/UserCard.php +++ b/common/models/UserCard.php @@ -61,6 +61,7 @@ class UserCard extends \yii\db\ActiveRecord const SCENARIO_GENERATE_RESUME_TEXT = 'generate_resume_text'; const SCENARIO_UPDATE_RESUME_TEXT = 'update_resume_text'; + const SCENARIO_DOWNLOAD_RESUME = 'download_resume_text'; // public $resumeTemplateId; @@ -123,7 +124,7 @@ class UserCard extends \yii\db\ActiveRecord ['resume_template_id', 'required', 'on' => self::SCENARIO_GENERATE_RESUME_TEXT], ['resume_template_id', 'integer', 'on' => self::SCENARIO_GENERATE_RESUME_TEXT], ['resume_text', 'required', 'on' => self::SCENARIO_UPDATE_RESUME_TEXT], -// [['resume_template_id'], 'exist', 'skipOnError' => true, 'targetClass' => ResumeTemplate::className(), 'targetAttribute' => ['resume_template_id' => 'id'], 'on' => self::SCENARIO_GENERATE_RESUME_TEXT], + ['resume_template_id', 'required', 'on' => self::SCENARIO_DOWNLOAD_RESUME], ]; } diff --git a/console/migrations/m221118_133250_add_footer_to_resume_template.php b/console/migrations/m221118_133250_add_footer_to_resume_template.php new file mode 100644 index 0000000..f222888 --- /dev/null +++ b/console/migrations/m221118_133250_add_footer_to_resume_template.php @@ -0,0 +1,42 @@ +addColumn('resume_template', 'footer_text', $this->string()); + $this->addColumn('resume_template', 'footer_image', $this->string()); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropColumn('resume_template', 'footer_text'); + $this->dropColumn('resume_template', 'footer_image'); + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m221118_133250_add_footer_to_resume_template cannot be reverted.\n"; + + return false; + } + */ +}