diff --git a/.travis.yml b/.travis.yml index 39a9c849..08310e24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ php: - 5.6 - 7.0 - 7.1 - - 7.2 +# - 7.2 # - hhvm # faster builds on new travis setup not using sudo diff --git a/backend/views/setting/custom.php b/backend/views/setting/custom.php index 278b58cc..e90f10a0 100644 --- a/backend/views/setting/custom.php +++ b/backend/views/setting/custom.php @@ -36,6 +36,10 @@ ->label($setting->name) ->widget(Ueditor::className(), ['name' => 'value' . $index]); + } else if($setting->input_type == Constants::INPUT_IMG){ + echo $form->field($setting,"[$index]value", ['template'=>"{label}\n
{input}{img}\n{error}
\n{hint}
{$setting->tips}
"]) + ->label($setting->name) + ->imgInput( ['value' => $setting->value] ); } else { if ($setting->input_type == Constants::INPUT_INPUT) { echo $form->field($setting, "[$index]value", ['template' => $template]) diff --git a/backend/widgets/ActiveField.php b/backend/widgets/ActiveField.php index 33c86398..d71393de 100644 --- a/backend/widgets/ActiveField.php +++ b/backend/widgets/ActiveField.php @@ -112,7 +112,7 @@ public function dropDownList($items, $options = [], $generateDefault = true) /** * @inheritdoc */ - public function reayOnly($value = null, $options = []) + public function readOnly($value = null, $options = []) { $options = array_merge($this->inputOptions, $options); @@ -200,18 +200,21 @@ public function textarea($options = []) } /** - * 图片输入框 - * + * @param array $options + * @return $this */ public function imgInput($options = []) { - $this->template = "{label}\n
{input}{img}\n{error}
\n{hint}"; - $pic = $this->attribute; - $src = yii::$app->params['site']['url'] . '/static/images/none.jpg'; - if ($this->model->$pic != '') { - $src = $this->model->$pic; + if( $this->template === "{label}\n
{input}\n{error}
\n{hint}" ) { + $this->template = "{label}\n
{input}{img}\n{error}
\n{hint}"; + } + $attribute = $this->attribute; + $src = key_exists('value', $options) ? $options['value'] : $this->model->$attribute; + if ($src != '') { $temp = parse_url($src); $src = isset($temp['host']) ? $src : yii::$app->params['site']['url'] . $src; + }else{ + $src = isset($options['default']) ? $options['default'] : yii::$app->params['site']['url'] . '/static/images/none.jpg'; } $this->parts['{img}'] = Html::img($src, $options); return parent::fileInput($options); // TODO: Change the autogenerated stub diff --git a/common/libs/Constants.php b/common/libs/Constants.php index de262003..0f3e7b3b 100644 --- a/common/libs/Constants.php +++ b/common/libs/Constants.php @@ -91,6 +91,7 @@ public static function getArticleStatus($key = null) const INPUT_INPUT = 1; const INPUT_TEXTAREA = 2; const INPUT_UEDITOR = 3; + const INPUT_IMG = 4; public static function getInputTypeItems($key = null) { @@ -98,6 +99,7 @@ public static function getInputTypeItems($key = null) self::INPUT_INPUT => 'input', self::INPUT_TEXTAREA => 'textarea', self::INPUT_UEDITOR => 'ueditor', + self::INPUT_IMG => 'image', ]; return self::getItems($items, $key); } diff --git a/common/models/Options.php b/common/models/Options.php index 603106f1..0037e920 100644 --- a/common/models/Options.php +++ b/common/models/Options.php @@ -8,8 +8,11 @@ namespace common\models; +use common\libs\Constants; use Yii; use common\helpers\FileDependencyHelper; +use yii\helpers\FileHelper; +use yii\web\UploadedFile; /** * This is the model class for table "{{%options}}". @@ -95,4 +98,37 @@ public function afterSave($insert, $changedAttributes) 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); + if($upload !== null){ + $uploadPath = yii::getAlias('@admin/uploads/custom-setting/'); + if (! FileHelper::createDirectory($uploadPath)) { + $this->addError($key, "Create directory failed " . $uploadPath); + return false; + } + $fullName = $uploadPath . uniqid() . '_' . $upload->baseName . '.' . $upload->extension; + 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); + if( $old !== null ){ + $file = yii::getAlias('@frontend/web') . $old->value; + if( file_exists($file) && is_file($file) ) unlink($file); + } + }else{ + $this->value = $old->value; + } + } + } + return true; + } + }