This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArrayInput.php
48 lines (43 loc) · 1.89 KB
/
ArrayInput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* Author: Eugine Terentev <eugine@terentev.net>
*/
namespace trntv\arrayinput;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\InputWidget;
class ArrayInput extends InputWidget{
protected $inputOptions;
public function init(){
parent::init();
$this->inputOptions = ArrayHelper::merge([
'type'=>'text',
'class'=>'form-control'
],
$this->options
);
unset($this->inputOptions['id']);
ArrayInputAsset::register($this->getView());
if($this->hasModel()){
$this->name = Html::getInputName($this->model, $this->attribute);
$this->value = Html::getAttributeValue($this->model, $this->attribute);
}
$this->name = $this->name.'[]'; // todo: Проверка не массив ли это уже
$this->value = is_array($this->value) ? $this->value : [$this->value];
}
public function run(){
$content = Html::beginTag('div', ['id'=>$this->options['id']]);
foreach($this->value as $k => $v){
$content .= Html::beginTag('div', ['class'=>'input-group']);
$content .= Html::input($this->inputOptions['type'], $this->name, $v, $this->inputOptions);
$content .= Html::tag('a', Html::tag('i', '', ['class'=>'glyphicon glyphicon-remove']), ['class'=>'input-group-addon array-input-remove', 'href'=>'#']);
$content .= Html::endTag('div');
}
$content .= Html::beginTag('div', ['class'=>'input-group']);
$content .= Html::input($this->inputOptions['type'], $this->name, null, $this->inputOptions);
$content .= Html::tag('a', Html::tag('i', '', ['class'=>'glyphicon glyphicon-plus']), ['class'=>'input-group-addon array-input-plus', 'href'=>'#']);
$content .= Html::endTag('div');
$content .= Html::endTag('div');
return $content;
}
}