Your IP : 216.73.216.79


Current Path : /var/www/html/sprm/backend/models/
Upload File :
Current File : /var/www/html/sprm/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'],
            [['content_photo_title', 'content_photo_title_en'], 'required'],
            [['thumbup', 'uploadImage', 'created_at', 'updated_at', 'content_photo_date', 'content_photo_thumb_image', 'content_photo_description_en', 'content_photo_description', 'content_photo_file_url', 'content_photo_file_status', 'content_photo_category', 'content_photo_title_ch', 'content_photo_description_ch', 'content_photo_info_ch', 'content_photo_subinfo_ch'], 'safe'],
            [['content_photo_title', 'content_photo_title_en'], 'string', 'max' => 200],
            [['content_photo_info', 'content_photo_info_en', 'content_photo_subinfo', 'content_photo_subinfo_en', 'content_photo_location'], 'string'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'content_photo_id' => Yii::t('app', 'ID'),
            'content_photo_title' => Yii::t('app', 'Tajuk'),
            'content_photo_title_en' => Yii::t('app', 'Tajuk (EN)'),
            'content_photo_title_ch' => Yii::t('app', 'Tajuk (CH)'),
            'content_photo_description' => Yii::t('app', 'Keterangan'),
            'content_photo_description_en' => Yii::t('app', 'Keterangan (EN)'),
            'content_photo_description_ch' => Yii::t('app', 'Keterangan (CH)'),
            'content_photo_date' => Yii::t('app', 'Tarikh'),
            'content_photo_location' => Yii::t('app', 'Lokasi'),
            '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'),
            'content_photo_info' => Yii::t('app', 'Info'),
            'content_photo_info_en' => Yii::t('app', 'Info (EN)'),
            'content_photo_info_ch' => Yii::t('app', 'Info (CH)'),
            'content_photo_subinfo' => Yii::t('app', 'SubInfo'),
            'content_photo_subinfo_en' => Yii::t('app', 'SubInfo (EN)'),
            'content_photo_subinfo_ch' => Yii::t('app', 'SubInfo (CH)'),
            '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'),
            'content_photo_category' => Yii::t('app', 'Kategori'),
        ];
    }

    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;
            }

            $count = 0;
            if (!empty($this->uploadImage)) {

                foreach ($this->uploadImage as $file) {
                    $count++;
                    $filename = 'uploads/galeryfoto_' . $aid . '/' . $file->baseName . date('dmyhi') . '_' . $count . '.' . $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']);
    }

    public static function getPhotoList() {
        $arr2 = [];

        $arr = self::find()->all();

        foreach ($arr as $photo) {
            $arr2[$photo->content_photo_id] = $photo->content_photo_title;
        }
        return $arr2;
    }

}