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

257 lines
7.8 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace backend\modules\card\controllers;
2022-10-11 10:39:22 +03:00
use backend\modules\card\models\UserCard;
use backend\modules\card\models\UserCardSearch;
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;
use PhpOffice\PhpWord\PhpWord;
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
public function actionDownloadResume($id, $pdf = false)
{
$model = $this->findModel($id);
if ($pdf) {
self::getResumePdf($model);
}
self::getResumeDocx($model);
}
private function getResumePdf(UserCard $model)
{
$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
echo $mpdf->Output("Resume - {$model->fio}", 'D'); // call the mpdf api output as needed
}
private function getResumeDocx(UserCard $model)
{
$phpWord = new PhpWord();
$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);
$section->addText(htmlspecialchars($text), $fontStyle,$parStyle);
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');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
ob_clean();
$objWriter->save("php://output");
exit;
}
2018-10-11 11:15:09 +03:00
}