Completed
Push — master ( 721877...45fe5c )
by Cheren
02:21
created

MoveComponent::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * CakeCMS Core
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   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Controller\Component;
17
18
use Cake\ORM\Table;
19
use JBZoo\Utils\Arr;
20
use Cake\Controller\Component;
21
use Cake\Datasource\EntityInterface;
22
use Cake\ORM\Behavior\TreeBehavior;
23
24
/**
25
 * Class MoveComponent
26
 *
27
 * @package Core\Controller\Component
28
 * @property FlashComponent $Flash
29
 */
30
class MoveComponent extends AppComponent
31
{
32
33
    const TYPE_UP   = 'moveUp';
34
    const TYPE_DOWN = 'moveDown';
35
36
    /**
37
     * Other Components this component uses.
38
     *
39
     * @var array
40
     */
41
    public $components = [
42
        'Core.Flash'
43
    ];
44
45
    /**
46
     * Reading the whole config.
47
     *
48
     * @param null|string $key
49
     * @param null|string $value
50
     * @param bool $merge
51
     * @return mixed
52
     */
53
    public function config($key = null, $value = null, $merge = true)
54
    {
55
        $this->_defaultConfig = [
56
            'messages' => [
57
                'success' => __d('core', 'Object has been moved'),
58
                'error'   => __d('core', 'Object could not been moved')
59
            ],
60
            'action' => 'index',
61
        ];
62
63
        return parent::config($key, $value, $merge);
64
    }
65
66
    /**
67
     * Move down record in tree.
68
     *
69
     * @param Table $table
70
     * @param EntityInterface $entity
71
     * @param int $step
72
     * @return \Cake\Network\Response|null
73
     */
74
    public function down(Table $table, EntityInterface $entity, $step = 1)
75
    {
76
        return $this->_move($table, $entity, $step, self::TYPE_DOWN);
77
    }
78
79
    /**
80
     * Move up record in tree.
81
     *
82
     * @param Table $table
83
     * @param EntityInterface $entity
84
     * @param int $step
85
     * @return \Cake\Network\Response|null
86
     * @SuppressWarnings(PHPMD.ShortMethodName)
87
     */
88
    public function up(Table $table, EntityInterface $entity, $step = 1)
89
    {
90
        return $this->_move($table, $entity, $step);
91
    }
92
93
    /**
94
     * Move object in tree table.
95
     *
96
     * @param Table $table
97
     * @param EntityInterface $entity
98
     * @param string $type
99
     * @param int $step
100
     * @return \Cake\Network\Response|null
101
     */
102
    protected function _move(Table $table, EntityInterface $entity, $step = 1, $type = self::TYPE_UP)
103
    {
104
        $behaviors = $table->behaviors();
105
        if (!Arr::in('Tree', $behaviors->loaded())) {
106
            $behaviors->load('Tree');
107
        }
108
109
        /** @var TreeBehavior $treeBehavior */
110
        $treeBehavior = $behaviors->get('Tree');
111
        $treeBehavior->config('scope', $entity->get('id'));
112
113
        if ($table->{$type}($entity, $step)) {
114
            $this->Flash->success($this->_configRead('messages.success'));
115
        } else {
116
            $this->Flash->error($this->_configRead('messages.error'));
117
        }
118
119
        return $this->_redirect();
120
    }
121
122
    /**
123
     * Process redirect.
124
     *
125
     * @return \Cake\Network\Response|null
126
     */
127
    protected function _redirect()
128
    {
129
        return $this->_controller->redirect([
130
            'prefix'     => $this->request->param('prefix'),
131
            'plugin'     => $this->request->param('plugin'),
132
            'controller' => $this->request->param('controller'),
133
            'action'     => $this->_configRead('action'),
134
        ]);
135
    }
136
}
137