add footer to resume
This commit is contained in:
		@@ -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(
 | 
			
		||||
                "<div style='border-bottom: 1px solid #999;'>
 | 
			
		||||
                    <p><img src=$imagePath style='width: 100px; height: 40px; margin: 0; vertical-align: middle;'/>$headerText</p>
 | 
			
		||||
                </div>"
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
        $mpdf->SetFooter('{PAGENO}');
 | 
			
		||||
        $mpdf->SetHTMLHeader("
 | 
			
		||||
                <table width='100%' style='border-bottom: 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=$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; '>{PAGENO}</td>
 | 
			
		||||
                </tr></table>
 | 
			
		||||
            ");
 | 
			
		||||
        $mpdf->SetHTMLFooter("
 | 
			
		||||
                <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>
 | 
			
		||||
            ");
 | 
			
		||||
 | 
			
		||||
        $mpdf->WriteHTML("<div>$userCard->resume_text</div>");
 | 
			
		||||
        $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('<br/>', '<br>', '</br>'), ' ', $model->resume_text);
 | 
			
		||||
        \PhpOffice\PhpWord\Shared\Html::addHtml($section, $resumeText, false, false);
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -56,6 +56,29 @@ use yii\widgets\ActiveForm;
 | 
			
		||||
                ?>
 | 
			
		||||
            </div>
 | 
			
		||||
 | 
			
		||||
            <?= $form->field($model, 'footer_text')->textInput(['maxlength' => true]) ?>
 | 
			
		||||
 | 
			
		||||
            <div class="imgUpload form-group">
 | 
			
		||||
                <div class="media__upload_img">
 | 
			
		||||
                    <img src="<?= $model->footer_image; ?>" width="100px"/>
 | 
			
		||||
                </div>
 | 
			
		||||
                <?php echo InputFile::widget([
 | 
			
		||||
                    'language' => 'ru',
 | 
			
		||||
                    'controller' => 'elfinder',
 | 
			
		||||
                    // вставляем название контроллера, по умолчанию равен elfinder
 | 
			
		||||
                    'filter' => 'image',
 | 
			
		||||
                    // фильтр файлов, можно задать массив фильтров https://github.com/Studio-42/elFinder/wiki/Client-con..
 | 
			
		||||
                    'name' => 'ResumeTemplate[footer_image]',
 | 
			
		||||
                    'id' => 'resumeTemplateFooter_img',
 | 
			
		||||
                    'template' => '<label>Картинка в верхнем контикуле</label><div class="input-group">{input}<span class="span-btn">{button}</span></div>',
 | 
			
		||||
                    'options' => ['class' => 'form-control itemImg', 'maxlength' => '255'],
 | 
			
		||||
                    'buttonOptions' => ['class' => 'btn btn-primary'],
 | 
			
		||||
                    'value' => $model->footer_image,
 | 
			
		||||
                    'buttonName' => 'Выбрать изображение',
 | 
			
		||||
                ]);
 | 
			
		||||
                ?>
 | 
			
		||||
            </div>
 | 
			
		||||
 | 
			
		||||
            <div class="form-group">
 | 
			
		||||
                <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
 | 
			
		||||
            </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -31,6 +31,10 @@ use yii\widgets\ActiveForm;
 | 
			
		||||
 | 
			
		||||
    <?php // echo $form->field($model, 'header_image') ?>
 | 
			
		||||
 | 
			
		||||
    <?php // echo $form->field($model, 'footer_text') ?>
 | 
			
		||||
 | 
			
		||||
    <?php // echo $form->field($model, 'footer_image') ?>
 | 
			
		||||
 | 
			
		||||
    <div class="form-group">
 | 
			
		||||
        <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
 | 
			
		||||
        <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
 | 
			
		||||
 
 | 
			
		||||
@@ -64,7 +64,7 @@ $this->params['breadcrumbs'][] = 'Резюме';
 | 
			
		||||
 | 
			
		||||
<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']) ?>
 | 
			
		||||
        <?= Html::a('Скачать pdf', ['download-resume', 'id' => $model->id, 'type' => 'pdf'], ['class' => 'btn btn-success']) ?>
 | 
			
		||||
        <?= Html::a('Скачать docx', ['download-resume', 'id' => $model->id, 'type' => 'docx'], ['class' => 'btn btn-success']) ?>
 | 
			
		||||
    </p>
 | 
			
		||||
</div>
 | 
			
		||||
@@ -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' => 'Картинка в нижнем контикуле',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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],
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,42 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
use yii\db\Migration;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class m221118_133250_add_footer_to_resume_template
 | 
			
		||||
 */
 | 
			
		||||
class m221118_133250_add_footer_to_resume_template extends Migration
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     */
 | 
			
		||||
    public function safeUp()
 | 
			
		||||
    {
 | 
			
		||||
        $this->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;
 | 
			
		||||
    }
 | 
			
		||||
    */
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user