forked from noumo/easyii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryWithFieldsModel.php
83 lines (75 loc) · 2.3 KB
/
CategoryWithFieldsModel.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace yii\easyii\components;
use yii\easyii\behaviors\JsonColumns;
use yii\easyii\behaviors\SortableModel;
/**
* Extended CategoryModel with fields. Shared by categories
* @package yii\easyii\components
* @inheritdoc
*/
class CategoryWithFieldsModel extends CategoryModel
{
const FIELD_TYPE_STRING = 'string';
const FIELD_TYPE_TEXT = 'text';
const FIELD_TYPE_HTML = 'html';
const FIELD_TYPE_BOOLEAN = 'boolean';
const FIELD_TYPE_SELECT = 'select';
const FIELD_TYPE_CHECKBOX = 'checkbox';
const FIELD_TYPE_FILE = 'file';
const FIELD_TYPE_DATE = 'date';
const FIELD_TYPE_ADDRESS = 'address';
public static $FIELD_TYPES = [
self::FIELD_TYPE_STRING => 'String',
self::FIELD_TYPE_TEXT => 'Text',
self::FIELD_TYPE_HTML => 'Html',
self::FIELD_TYPE_BOOLEAN => 'Boolean',
self::FIELD_TYPE_SELECT => 'Select',
self::FIELD_TYPE_CHECKBOX => 'Checkbox',
self::FIELD_TYPE_FILE => 'File',
self::FIELD_TYPE_DATE => 'Date',
self::FIELD_TYPE_ADDRESS => 'Address'
];
public static $FIELDS_WITH_OPTIONS = [
self::FIELD_TYPE_SELECT,
self::FIELD_TYPE_CHECKBOX,
self::FIELD_TYPE_FILE,
self::FIELD_TYPE_ADDRESS
];
public $parent_id;
public function rules()
{
return array_merge([
['fields', 'safe'],
], parent::rules());
}
public function behaviors()
{
return array_merge(parent::behaviors(), [
'jsonColumns' => [
'class' => JsonColumns::className(),
'columns' => ['fields']
]
]);
}
public function getFieldByName($name)
{
foreach($this->fields as $field){
if($field->name == $name){
return $field;
}
}
return null;
}
public function create($parent_id = null)
{
if ($parent_id && ($parentCategory = static::findOne($parent_id))) {
$this->fields = $parentCategory->fields;
$this->order_num = $parentCategory->order_num;
$this->appendTo($parentCategory);
} else {
$this->attachBehavior('sortable', SortableModel::className());
$this->makeRoot();
}
return $this->hasErrors() ? false : true;
}
}