| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Author: lf
- * Blog: https://blog.feehi.com
- * Email: job@feehi.com
- * Created at: 2017-12-05 12:47
- */
- namespace backend\models\form;
- use Yii;
- use common\helpers\Util;
- use common\libs\Constants;
- use yii\web\UploadedFile;
- class AdForm extends \common\models\Options
- {
- /** @var string advertisement content. if type is image, this will be the image url. */
- public $ad;
- /** @var string click advertisement redirect url */
- public $link;
- /** @var string advertisement description, oftens be remark info */
- public $desc;
- /** @var string click advertisement open method */
- public $target = Constants::TARGET_BLANK;
- public $created_at;
- public $updated_at;
- /** @var string $advertisement store file path */
- private $advertisementFilePath = '@uploads/setting/ad/';
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'name' => Yii::t('app', 'Sign'),
- 'input_type' => Yii::t('app', 'Ad Type'),
- 'tips' => Yii::t('app', 'Description'),
- 'ad' => Yii::t('app', 'Ad'),
- 'link' => Yii::t('app', 'Jump Link'),
- 'desc' => Yii::t('app', 'Ad Explain'),
- 'autoload' => Yii::t('app', 'Status'),
- 'sort' => Yii::t('app', 'Sort'),
- 'target' => Yii::t('app', 'Target'),
- 'created_at' => Yii::t('app', 'Created At'),
- 'updated_at' => Yii::t('app', 'Updated At'),
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['name'], 'unique'],
- [
- ['name'],
- 'match',
- 'pattern' => '/^[a-zA-Z][0-9_]*/',
- 'message' => Yii::t('app', 'Must begin with alphabet and can only includes alphabet,_,and number')
- ],
- [['name', 'tips', 'input_type'], 'required'],
- [['sort', 'autoload'], 'integer'],
- [[ 'link', 'target', 'desc'], 'string'],
- [['ad'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'],
- ];
- }
- public function beforeValidate()
- {
- if( $this->type === Constants::AD_IMG ) {;
- if ($this->ad !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
- $this->ad = UploadedFile::getInstance($this, "ad");
- }
- }
- return parent::beforeValidate();
- }
- public function beforeSave($insert)
- {
- $this->type = self::TYPE_AD;
- if( $this->input_type == Constants::AD_TEXT ){
- $oldInput = $this->getOldAttribute('input_type');
- if( $oldInput != Constants::AD_TEXT ){//delete origin advertisement file
- $text = $this->ad;
- Util::handleModelSingleFileUploadAbnormal($this, 'ad', $this->advertisementFilePath, $this->getOldAttribute('ad'), ['deleteOldFile'=>true]);
- $this->ad = $text;
- }
- }else {
- Util::handleModelSingleFileUploadAbnormal($this, 'ad', $this->advertisementFilePath, $this->getOldAttribute('ad'));
- }
- $value = [
- 'ad' => $this->ad,
- 'link' => $this->link,
- 'target' => $this->target,
- 'desc' => $this->desc,
- 'created_at' => $this->getIsNewRecord() ? time() : $this->created_at,
- 'updated_at' => time(),
- ];
- $this->value = json_encode($value);
- return parent::beforeSave($insert);
- }
- public function afterFind()
- {
- $value = json_decode($this->value);
- if( $this->input_type !== Constants::AD_TEXT){
- /** @var $cdn \feehi\cdn\TargetAbstract */
- $cdn = Yii::$app->get('cdn');
- $this->ad = $cdn->getCdnUrl($value->ad);
- }else{
- $this->ad = $value->ad;
- }
- $this->link = $value->link;
- $this->desc = $value->desc;
- $this->target = $value->target;
- $this->updated_at = $value->updated_at;
- $this->created_at = $value->created_at;
- $this->setOldAttributes([
- 'id' => $this->id,
- 'name' => $this->name,
- 'value' => $this->value,
- 'input_type' => $this->input_type,
- 'autoload' => $this->autoload,
- 'tips' => $this->tips,
- 'sort' => $this->sort,
- 'ad' => $value->ad,
- 'link' => $value->link,
- 'desc' => $value->desc,
- 'target' => $value->target,
- 'created_at' => $value->created_at,
- 'updated_at' => $value->updated_at,
- ]);
- parent::afterFind();
- }
- public function afterDelete()
- {
- if ($this->input_type != Constants::AD_TEXT) {
- $file = Yii::getAlias('@frontend/web') . $this->ad;
- if (file_exists($file) && is_file($file)) unlink($file);
- }
- return parent::beforeDelete();
- }
- }
|