Your IP : 216.73.216.124


Current Path : /var/www/html/chatbot/backend/models/
Upload File :
Current File : /var/www/html/chatbot/backend/models/ContentPhoto.php

<?php

namespace backend\models;

use Yii;
use asinfotrack\yii2\audittrail\behaviors\AuditTrailBehavior;

/**
 * This is the model class for table "content_photo".
 *
 * @property int $content_photo_id
 * @property string $content_photo_title
 * @property string $content_photo_file_url
 * @property int $content_photo_file_type 1-Image,2-Video
 * @property int $content_photo_article_id
 * @property string $created_at
 * @property int $created_by
 * @property string $updated_at
 * @property int $updated_by
 */
class ContentPhoto extends \common\models\Dermaga
{   

    /* type of media */
    public static $mediaType = ['1'=>'Gambar','2'=>'Video'];

    public $uploadImage;

    public $uploadArr = [];

    public $thumbpath;

    public $thumbup;
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'content_photo';
    }

    public function behaviors()
    {
        return [
            'audittrail'=>[
                'class'=>AuditTrailBehavior::className(),
                
                // some of the optional configurations
                'ignoredAttributes'=>['created_at','updated_at'],
                'consoleUserId'=>1, 
                'attributeOutput'=>[
                    'desktop_id'=>function ($value) {
                        $model = Desktop::findOne($value);
                        return sprintf('%s %s', $model->manufacturer, $model->device_name);
                    },
                    'last_checked'=>'datetime',
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['content_photo_file_type', 'content_photo_article_id', 'created_by', 'updated_by'], 'integer'],
            [['created_at', 'updated_at','content_photo_thumb_image','content_photo_description_en','content_photo_description','content_photo_file_url','content_photo_file_status','content_photo_category'], 'safe'],
            [['content_photo_title','content_photo_title_en'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'content_photo_id' => Yii::t('app', 'ID'),
            'content_photo_title' => Yii::t('app', 'Tajuk Galeri Foto'),
            'content_photo_file_url' => Yii::t('app', 'Pautan'),
            'content_photo_file_type' => Yii::t('app', 'Jenis'),
            'content_photo_article_id' => Yii::t('app', 'Artikel Id'),
            'created_at' => Yii::t('app', 'Created At'),
            'created_by' => Yii::t('app', 'Created By'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'updated_by' => Yii::t('app', 'Updated By'),
        ];
    }

    public function upload($aid)
    {   
        if ($this->validate()) {
            /* if directory empty create it */
            if (!file_exists('uploads/galeryfoto_'. $aid . '/')) {
                mkdir('uploads/galeryfoto_'. $aid . '/', 0777, true);
            }

            if (!file_exists('uploads/galeryfoto_'. $aid . '/thumbnail')) {
                mkdir('uploads/galeryfoto_'. $aid . '/thumbnail', 0777, true);
            }
            
            /* save thumb */
            if(!empty($this->thumbup)){
                $thumbfilename = 'uploads/galeryfoto_'. $aid . '/thumbnail/' . $this->thumbup->baseName . date('dmyhi') . '.' . $this->thumbup->extension;
                $this->thumbup->saveAs($thumbfilename);
                $this->thumbpath = $thumbfilename;
            }

            if(!empty($this->uploadImage)){

                foreach ($this->uploadImage as $file) {
                    $filename = 'uploads/galeryfoto_'. $aid . '/' . $file->baseName . date('dmyhi') . '.' . $file->extension;
                    $file->saveAs($filename);
    
                    $this->uploadArr[] = $filename;
                    /* rewrite url path for uniquenes */
                    // $this->content_photo_file_url = $this->content_photo_file_url->baseName . date('dmyhi') . '.' . $this->content_photo_file_url->extension;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    public function getArticle(){   
        return $this->hasOne(BanciArticle::className(),['banci_article_id'=>'content_photo_article_id']);
    }
}