GroupsTable::initialize()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 16
nc 1
nop 1
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
     * @throws  \Aura\Intl\Exception
87
     */
88
    public function validationDefault(Validator $validator)
89
    {
90
        $validator
91
            ->requirePresence('name')
92
            ->notEmpty(
93
                'name',
94
                __d('community', 'Group name could not be empty.')
95
            )
96
97
            ->requirePresence('slug')
98
            ->notEmpty(
99
                'slug',
100
                __d('community', 'Group slug could not be empty.')
101
            )
102
            ->add('slug', 'unique', [
103
                'message'  => __d('community', 'Group with this slug already exists.'),
104
                'rule'     => 'validateUnique',
105
                'provider' => 'table'
106
            ]);
107
108
        return $validator;
109
    }
110
}
111