Completed
Push — master ( 194a6e...a14113 )
by Nekrasov
01:18
created

Action::editIBlockSection()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 2
1
<?php
2
3
namespace Arrilot\BitrixHermitage;
4
5
use Bitrix\Main\Application;
6
use CBitrixComponent;
7
use CBitrixComponentTemplate;
8
use CIBlock;
9
use InvalidArgumentException;
10
11
class Action
12
{
13
    protected static $panelButtons = [];
14
    
15
    protected static $iblockElementArray = [];
16
    
17
    protected static $iblockSectionArray = [];
18
    
19
    /**
20
     * Get edit area id for specific type
21
     *
22
     * @param CBitrixComponentTemplate $template
23
     * @param $type
24
     * @param $element
25
     * @return string
26
     */
27
    public static function getEditArea($template, $type, $element)
28
    {
29
        $id = is_numeric($element) ? $element : $element['ID'];
30
        return $template->GetEditAreaId("{$type}_{$id}");
31
    }
32
33
    /**
34
     * @param CBitrixComponentTemplate $template
35
     * @param $element
36
     */
37 View Code Duplication
    public static function editIBlockElement($template, $element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
editIBlockElement uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
38
    {
39
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
40
            return;
41
        }
42
43
        if (is_numeric($element)) {
44
            $element = static::prepareIBlockElementArrayById($element);
45
        }
46
47
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
48
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
49
        }
50
51
        $buttons = static::getIBlockElementPanelButtons($element);
52
        $link = $buttons["edit"]["edit_element"]["ACTION_URL"];
53
54
        $template->AddEditAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_EDIT"));
55
    }
56
    
57
    /**
58
     * @param CBitrixComponentTemplate $template
59
     * @param $element
60
     * @param string $confirm
61
     */
62 View Code Duplication
    public static function deleteIBlockElement($template, $element, $confirm = 'Вы уверены, что хотите удалить элемент?')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
deleteIBlockElement uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
    {
64
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
65
            return;
66
        }
67
68
        if (is_numeric($element)) {
69
            $element = static::prepareIBlockElementArrayById($element);
70
        }
71
72
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
73
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
74
        }
75
    
76
        $buttons = static::getIBlockElementPanelButtons($element);
77
        $link = $buttons["edit"]["delete_element"]["ACTION_URL"];
78
79
        $template->AddDeleteAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_DELETE"), array("CONFIRM" => $confirm));
80
    }
81
82
    /**
83
     * @param CBitrixComponentTemplate $template
84
     * @param $element
85
     * @return string
86
     */
87
    public static function areaForIBlockElement($template, $element)
88
    {
89
        return static::getEditArea($template, 'iblock_element', $element);
90
    }
91
92
    /**
93
     * @param CBitrixComponentTemplate $template
94
     * @param $section
95
     */
96 View Code Duplication
    public static function editIBlockSection($template, $section)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
editIBlockSection uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
97
    {
98
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
99
            return;
100
        }
101
102
        if (is_numeric($section)) {
103
            $section = static::prepareIBlockSectionArrayById($section);
104
        }
105
106
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
107
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
108
        }
109
    
110
        $buttons = static::getIBlockSectionPanelButtons($section);
111
        $link = $buttons["edit"]["edit_section"]["ACTION_URL"];
112
113
        $template->AddEditAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_EDIT"));
114
    }
115
116
    /**
117
     * @param CBitrixComponentTemplate $template
118
     * @param $section
119
     * @param string $confirm
120
     */
121 View Code Duplication
    public static function deleteIBlockSection($template, $section, $confirm = 'Вы уверены, что хотите удалить раздел?')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
deleteIBlockSection uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
122
    {
123
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
124
            return;
125
        }
126
127
        if (is_numeric($section)) {
128
            $section = static::prepareIBlockSectionArrayById($section);
129
        }
130
131
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
132
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
133
        }
134
    
135
        $buttons = static::getIBlockSectionPanelButtons($section);
136
        $link = $buttons["edit"]["delete_section"]["ACTION_URL"];
137
138
        $template->AddDeleteAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_DELETE"), array("CONFIRM" => $confirm));
139
    }
140
141
    /**
142
     * @param CBitrixComponentTemplate $template
143
     * @param $section
144
     * @return string
145
     */
146
    public static function areaForIBlockSection($template, $section)
147
    {
148
        return static::getEditArea($template, 'iblock_section', $section);
149
    }
150
151
    /**
152
     * @param CBitrixComponentTemplate $template
153
     * @param $element
154
     * @param string $label
155
     */
156 View Code Duplication
    public static function editHLBlockElement($template, $element, $label = 'Изменить элемент')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
editHLBlockElement uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
157
    {
158
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
159
            return;
160
        }
161
162
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
163
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID');
164
        }
165
166
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?ENTITY_ID=%s&ID=%s&lang=ru&bxpublic=Y';
167
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
168
169
        $template->AddEditAction('hlblock_element_' . $element['ID'], $link, $label);
170
    }
171
    
172
    /**
173
     * @param CBitrixComponentTemplate $template
174
     * @param $element
175
     * @param string $label
176
     * @param string $confirm
177
     */
178 View Code Duplication
    public static function deleteHLBlockElement($template, $element, $label = 'Удалить элемент', $confirm = 'Вы уверены, что хотите удалить элемент?')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
deleteHLBlockElement uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
179
    {
180
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
181
            return;
182
        }
183
        
184
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
185
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID');
186
        }
187
188
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?action=delete&ENTITY_ID=%s&ID=%s&lang=ru';
189
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
190
    
191
        $template->AddDeleteAction('hlblock_element_' . $element['ID'], $link, $label, array("CONFIRM" => $confirm));
192
    }
193
194
    /**
195
     * @param CBitrixComponentTemplate $template
196
     * @param $element
197
     * @return string
198
     */
199
    public static function areaForHLBlockElement($template, $element)
200
    {
201
        return static::getEditArea($template, 'hlblock_element', $element);
202
    }
203
    
204
    /**
205
     * @param CBitrixComponent|CBitrixComponentTemplate $componentOrTemplate
206
     * @param $iblockId
207
     * @param array $options
208
     */
209
    public static function addForIBlock($componentOrTemplate, $iblockId, $options = [])
0 ignored issues
show
Coding Style introduced by
addForIBlock uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
210
    {
211
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
212
            return;
213
        }
214
215
        if ($componentOrTemplate instanceof CBitrixComponentTemplate) {
0 ignored issues
show
Bug introduced by
The class CBitrixComponentTemplate does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
216
            $componentOrTemplate = $componentOrTemplate->__component;
217
        }
218
219
        $buttons = CIBlock::GetPanelButtons($iblockId, 0, 0, $options);
220
        $menu = CIBlock::GetComponentMenu($GLOBALS['APPLICATION']->GetPublicShowMode(), $buttons);
221
222
        $componentOrTemplate->addIncludeAreaIcons($menu);
223
    }
224
225
    /**
226
     * @param $element
227
     * @return array
228
     */
229 View Code Duplication
    protected static function getIBlockElementPanelButtons($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
    {
231
        if (!isset(static::$panelButtons['iblock_element'][$element['ID']])) {
232
            static::$panelButtons['iblock_element'][$element['ID']] = CIBlock::GetPanelButtons(
233
                $element["IBLOCK_ID"],
234
                $element['ID'],
235
                0,
236
                ['SECTION_BUTTONS' => false, 'SESSID' => false]
237
            );
238
        }
239
240
        return static::$panelButtons['iblock_element'][$element['ID']];
241
    }
242
243
    /**
244
     * @param $section
245
     * @return array
246
     */
247 View Code Duplication
    protected static function getIBlockSectionPanelButtons($section)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
    {
249
        if (!isset(static::$panelButtons['iblock_section'][$section['ID']])) {
250
            static::$panelButtons['iblock_section'][$section['ID']] = CIBlock::GetPanelButtons(
251
                $section["IBLOCK_ID"],
252
                0,
253
                $section['ID'],
254
                ['SESSID' => false]
255
            );
256
        }
257
258
        return static::$panelButtons['iblock_section'][$section['ID']];
259
    }
260
    
261
    /**
262
     * @param int $id
263
     * @return array
264
     */
265 View Code Duplication
    protected static function prepareIBlockElementArrayById($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
    {
267
        $id = (int) $id;
268
        if (!$id) {
269
            return [];
270
        }
271
272
        if (empty(static::$iblockElementArray[$id])) {
273
            $connection = Application::getConnection();
274
            $el = $connection->query("SELECT ID, IBLOCK_ID FROM b_iblock_element WHERE ID = {$id}")->fetch();
275
            static::$iblockElementArray[$id] = $el ? $el : [];
276
        }
277
278
        return static::$iblockElementArray[$id];
279
    }
280
281
    /**
282
     * @param int $id
283
     * @return array
284
     */
285 View Code Duplication
    protected static function prepareIBlockSectionArrayById($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
    {
287
        $id = (int) $id;
288
        if (!$id) {
289
            return [];
290
        }
291
292
        if (empty(static::$iblockSectionArray[$id])) {
293
            $connection = Application::getConnection();
294
            $el = $connection->query("SELECT ID, IBLOCK_ID FROM b_iblock_section WHERE ID = {$id}")->fetch();
295
            static::$iblockSectionArray[$id] = $el ? $el : [];
296
        }
297
298
        return static::$iblockSectionArray[$id];
299
    }
300
}
301