| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace common\widgets\cropper;
- use Yii;
- use yii\helpers\Url;
- use yii\widgets\InputWidget;
- use common\helpers\ArrayHelper;
- use common\helpers\Html;
- use common\helpers\StringHelper;
- /**
- * Class Cropper
- * @package common\widgets\cropper
- * @author jianyan74 <751393839@qq.com>
- */
- class Cropper extends InputWidget
- {
- /**
- * @var array
- */
- public $config = [];
- /**
- * @var array
- */
- public $formData = [];
- /**
- * 默认主题
- *
- * @var string
- */
- public $theme = 'default';
- /**
- * 默认主题配置
- *
- * @var array
- */
- public $themeConfig = [];
- /**
- * 盒子ID
- *
- * @var
- */
- protected $boxId;
- /**
- * @throws \yii\base\InvalidConfigException
- * @throws \Exception
- */
- public function init()
- {
- parent::init();
- $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
- $this->boxId = md5($name) . StringHelper::uuid('uniqid');
- $this->config = ArrayHelper::merge([
- 'aspectRatio' => '1',
- 'multiple' => false,
- 'server' => Url::to(['/files/base64']),
- ], $this->config);
- $this->formData = ArrayHelper::merge([
- 'drive' => Yii::$app->services->config->backendConfig('storage_default'),
- 'image' => '',
- ], $this->formData);
- $this->themeConfig = ArrayHelper::merge([
- 'select' => true
- ], $this->themeConfig);
- }
- /**
- * @return string
- */
- public function run()
- {
- $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
- $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
- empty($value) && $value = [];
- if ($this->config['multiple'] == true) {
- // 赋予默认值
- $name = $name . '[]';
- if ($value && !is_array($value)) {
- $value = json_decode($value, true);
- empty($value) && $value = unserialize($value);
- empty($value) && $value = [];
- }
- }
- if (!is_array($value)) {
- $tmp = $value;
- $value = [];
- $value[] = $tmp;
- }
- return $this->render($this->theme, [
- 'name' => $name,
- 'value' => $value,
- 'boxId' => $this->boxId,
- 'config' => $this->config,
- 'type' => 'images',
- 'formData' => $this->formData,
- 'themeConfig' => $this->themeConfig,
- ]);
- }
- }
|