Skip to content

Commit

Permalink
fix ActiveField imgInput, and custom setting image type input
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Nov 8, 2017
1 parent 17a47c1 commit 7db8cae
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions backend/views/setting/custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<div class=\"col-sm-8 image\">{input}{img}\n{error}</div>\n{hint}<div class='col-sm-2'><span class='tips'><i class='fa fa-info-circle'></i> {$setting->tips} <a class='btn-delete' href='{$deleteUrl}' title='' data-confirm='' data-method='' data-pjax='1'><i style='float: right' class='fa fa-trash-o'></i></a><a href='{$editUrl}' class='btn_edit' title='编辑' data-pjax=''><i style='float: right;margin-right: 10px;' class='fa fa-pencil'></i></a> </span></div>"])
->label($setting->name)
->imgInput( ['value' => $setting->value] );
} else {
if ($setting->input_type == Constants::INPUT_INPUT) {
echo $form->field($setting, "[$index]value", ['template' => $template])
Expand Down
19 changes: 11 additions & 8 deletions backend/widgets/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -200,18 +200,21 @@ public function textarea($options = [])
}

/**
* 图片输入框
*
* @param array $options
* @return $this
*/
public function imgInput($options = [])
{
$this->template = "{label}\n<div class=\"col-sm-{size} image\">{input}{img}\n{error}</div>\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<div class=\"col-sm-{size}\">{input}\n{error}</div>\n{hint}" ) {
$this->template = "{label}\n<div class=\"col-sm-{size} image\">{input}{img}\n{error}</div>\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
Expand Down
2 changes: 2 additions & 0 deletions common/libs/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ 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)
{
$items = [
self::INPUT_INPUT => 'input',
self::INPUT_TEXTAREA => 'textarea',
self::INPUT_UEDITOR => 'ueditor',
self::INPUT_IMG => 'image',
];
return self::getItems($items, $key);
}
Expand Down
36 changes: 36 additions & 0 deletions common/models/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}}".
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 7db8cae

Please sign in to comment.