'/^[a-zA-Z][0-9_]*/', 'message' => Yii::t('app', 'Must begin with alphabet and can only includes alphabet,_,and number') ], [['value'], 'string'], [['value'], 'default', 'value' => ''], [['name', 'tips'], 'string', 'max' => 255], [['sort'], 'default', 'value' => 0], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => Yii::t('app', 'ID'), 'type' => Yii::t('app', 'Type'), 'name' => Yii::t('app', 'Name'), 'value' => Yii::t('app', 'Value'), 'input_type' => Yii::t('app', 'Input Type'), 'tips' => Yii::t('app', 'Tips'), 'autoload' => Yii::t('app', 'Autoload'), 'sort' => Yii::t('app', 'Sort'), ]; } /** * @return array */ public function getNames() { return array_keys($this->attributeLabels()); } /** * @inheritdoc */ public function afterSave($insert, $changedAttributes) { $object = Yii::createObject([ 'class' => FileDependencyHelper::className(), 'fileName' => self::CACHE_DEPENDENCY_TYPE_SYSTEM_FILE_NAME, ]); $object->updateFile(); parent::afterSave($insert, $changedAttributes); } public function beforeSave($insert) { if( !$insert ){ if( $this->input_type == Constants::INPUT_IMG ) { $temp = explode('\\', self::className()); $modelName = end( $temp ); $key = "{$modelName}[{$this->id}][value]"; $upload = UploadedFile::getInstanceByName($key); $old = Options::findOne($this->id); /* @var $cdn \feehi\cdn\TargetInterface */ $cdn = Yii::$app->get('cdn'); if($upload !== null){ $uploadPath = Yii::getAlias('@uploads/setting/custom-setting/'); if (! FileHelper::createDirectory($uploadPath)) { $this->addError($key, "Create directory failed " . $uploadPath); return false; } $fullName = $uploadPath . date('YmdHis') . '_' . uniqid() . '.' . $upload->getExtension(); if (! $upload->saveAs($fullName)) { $this->addError($key, Yii::t('app', 'Upload {attribute} error: ' . $upload->error, ['attribute' => Yii::t('app', 'Picture')]) . ': ' . $fullName); return false; } $this->value = str_replace(Yii::getAlias('@frontend/web'), '', $fullName); $cdn->upload($fullName, $this->value); if( $old !== null ){ $file = Yii::getAlias('@frontend/web') . $old->value; if( file_exists($file) && is_file($file) ) unlink($file); if( $cdn->exists($old->value) ) $cdn->delete($old->value); } }else{ if( $this->value !== '' ){ $file = Yii::getAlias('@frontend/web') . $old->value; if( file_exists($file) && is_file($file) ) unlink($file); if( $cdn->exists($old->value) ) $cdn->delete($old->value); $this->value = ''; }else { $this->value = $old->value; } } } } return parent::beforeSave($insert); } }