Advertisement
Guest User

before documentcontroller

a guest
Mar 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace app\controllers;
  5.  
  6.  
  7. use app\models\Document;
  8. use Imagick;
  9. use Spatie\PdfToImage\Pdf;
  10. use Yii;
  11. use yii\filters\AccessControl;
  12. use yii\helpers\Url;
  13. use yii\web\UploadedFile;
  14.  
  15. class DocumentController extends AbstractController
  16. {
  17.     public function behaviors()
  18.     {
  19.         return [
  20.             'access' => [
  21.                 'class' => AccessControl::className(),
  22.                 'rules' => [
  23.                     [
  24.                         'allow' => true,
  25.                         'roles' => ['@'],
  26.                     ],
  27.                 ],
  28.             ],
  29.         ];
  30.     }
  31.  
  32.     public function actionUpdate($id = null)
  33.     {
  34.         if ($id) {
  35.             $model = $this->findModel($id, Document::className());
  36.         } else {
  37.             $model = new Document;
  38.         }
  39.         if ($post = Yii::$app->request->post()) {
  40.             if ($model->load($post) && $model->save()) {
  41.                 $this->notice('Your changes have been saved successfully.');
  42.             } else {
  43.                 $this->notice($model->errors, 'error');
  44.             }
  45.             return $this->redirect(['site/dashboard']);
  46.         }
  47.  
  48.         return $this->render('update', [
  49.             'model' => $model,
  50.             'id' => $id,
  51.         ]);
  52.     }
  53.  
  54.  
  55.     public function actionSave()
  56.     {
  57.  
  58.         if ($id = Yii::$app->request->post('document_id')) {
  59.             $model = $this->findModel($id, Document::className());
  60.         } else {
  61.             $model = new Document;
  62.         }
  63.         $model->updated_at = time();
  64.  
  65.  
  66.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  67.             return $this->redirect(Url::to(['site/dashboard']));
  68.  
  69.         }
  70.         return 'failed!';
  71.     }
  72.  
  73.  
  74.     public function actionGeneratePdf($id)
  75.     {
  76.         $model = Document::findOne(['id' => $id]);
  77.         $pdf = Yii::$app->pdf;
  78.         $pdf->content = $this->renderPartial('document', ['model'=>$model]);
  79.         $pdf->cssInline = '
  80.        .title-page {
  81.            padding-top:50%;
  82.            text-align:center;
  83.            height:100%;
  84.        }
  85.  
  86.        .title {
  87.            padding: 10px 0 10px 0;
  88.            border-top: 1px solid black;
  89.            border-bottom: 1px solid black;
  90.            font-size: 20px;
  91.            font-weight: bold;
  92.        }
  93.        ';
  94.         $pdf->execute('SetHeader', $this->renderPartial('parts/header'));
  95.         return $pdf->render();
  96.     }
  97.  
  98.     public function actionDelete(array $id)
  99.     {
  100.         return $this->delete(Document::className(), $id);
  101.     }
  102.  
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement