1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Community |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Community |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Community". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Community\Model\Table; |
17
|
|
|
|
18
|
|
|
use Core\ORM\Table; |
19
|
|
|
use Cake\Utility\Hash; |
20
|
|
|
use Cake\Validation\Validator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class GroupsTable |
24
|
|
|
* |
25
|
|
|
* @package Community\Model\Table |
26
|
|
|
*/ |
27
|
|
|
class GroupsTable extends Table |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get tree group list. |
32
|
|
|
* |
33
|
|
|
* @param array $options Behavior tree options. |
34
|
|
|
* See https://book.cakephp.org/3.0/ru/orm/behaviors/tree.html |
35
|
|
|
* @return \Cake\ORM\Query |
36
|
|
|
*/ |
37
|
|
|
public function getTreeList(array $options = []) |
38
|
|
|
{ |
39
|
|
|
$options = Hash::merge([ |
40
|
|
|
'lft' => 'ASC', |
41
|
|
|
'spacer' => '|—', |
42
|
|
|
], $options); |
43
|
|
|
|
44
|
|
|
return $this->find('treeList', $options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initialize a table instance. Called after the constructor. |
49
|
|
|
* |
50
|
|
|
* @param array $config Configuration options passed to the constructor. |
51
|
|
|
* @return void |
52
|
|
|
* @throws \RuntimeException |
53
|
|
|
*/ |
54
|
|
|
public function initialize(array $config) |
55
|
|
|
{ |
56
|
|
|
parent::initialize($config); |
57
|
|
|
|
58
|
|
|
$this |
59
|
|
|
->setPrimaryKey('id') |
60
|
|
|
->setTable(CMS_TABLE_GROUPS) |
61
|
|
|
->setDisplayField('name'); |
62
|
|
|
|
63
|
|
|
$this->addBehavior('Tree'); |
64
|
|
|
$this->addAssociations([ |
65
|
|
|
'belongsTo' => [ |
66
|
|
|
'ParentGroup' => [ |
67
|
|
|
'foreignKey' => 'parent_id', |
68
|
|
|
'className' => 'Community.Group' |
69
|
|
|
] |
70
|
|
|
], |
71
|
|
|
'hasMany' => [ |
72
|
|
|
'Users' => [ |
73
|
|
|
'dependent' => true, |
74
|
|
|
'className' => 'Community.Users', |
75
|
|
|
] |
76
|
|
|
] |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Default validation rules. |
82
|
|
|
* |
83
|
|
|
* @param Validator $validator The validator that can be modified to add some rules to it. |
84
|
|
|
* @return Validator |
85
|
|
|
*/ |
86
|
|
|
public function validationDefault(Validator $validator) |
87
|
|
|
{ |
88
|
|
|
$validator |
89
|
|
|
->requirePresence('name') |
90
|
|
|
->notEmpty( |
91
|
|
|
'name', |
92
|
|
|
__d('community', 'Group name could not be empty.') |
93
|
|
|
) |
94
|
|
|
|
95
|
|
|
->requirePresence('slug') |
96
|
|
|
->notEmpty( |
97
|
|
|
'slug', |
98
|
|
|
__d('community', 'Group slug could not be empty.') |
99
|
|
|
) |
100
|
|
|
->add('slug', 'unique', [ |
101
|
|
|
'message' => __d('community', 'Group with this slug already exists.'), |
102
|
|
|
'rule' => 'validateUnique', |
103
|
|
|
'provider' => 'table', |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
return $validator; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|