Completed
Push — master ( a14113...10939c )
by Nekrasov
01:03
created

Action::editAndDeleteHLBlockElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
47
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
48
        }
49
50
        $buttons = static::getIBlockElementPanelButtons($element);
51
        $link = $buttons["edit"]["edit_element"]["ACTION_URL"];
52
53
        $template->AddEditAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_EDIT"));
54
    }
55
    
56
    /**
57
     * @param CBitrixComponentTemplate $template
58
     * @param $element
59
     * @param string $confirm
60
     */
61 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...
62
    {
63
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
64
            return;
65
        }
66
67
        if (is_numeric($element)) {
68
            $element = static::prepareIBlockElementArrayById($element);
69
        }
70
71
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
72
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
73
        }
74
    
75
        $buttons = static::getIBlockElementPanelButtons($element);
76
        $link = $buttons["edit"]["delete_element"]["ACTION_URL"];
77
78
        $template->AddDeleteAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_DELETE"), array("CONFIRM" => $confirm));
79
    }
80
81
    /**
82
     * @param CBitrixComponentTemplate $template
83
     * @param $element
84
     */
85
    public static function editAndDeleteIBlockElement($template, $element)
86
    {
87
        static::editIBlockElement($template, $element);
88
        static::deleteIBlockElement($template, $element);
89
    }
90
91
    /**
92
     * @param CBitrixComponentTemplate $template
93
     * @param $element
94
     * @return string
95
     */
96
    public static function areaForIBlockElement($template, $element)
97
    {
98
        return static::getEditArea($template, 'iblock_element', $element);
99
    }
100
101
    /**
102
     * @param CBitrixComponentTemplate $template
103
     * @param $section
104
     */
105 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...
106
    {
107
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
108
            return;
109
        }
110
111
        if (is_numeric($section)) {
112
            $section = static::prepareIBlockSectionArrayById($section);
113
        }
114
115
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
116
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
117
        }
118
    
119
        $buttons = static::getIBlockSectionPanelButtons($section);
120
        $link = $buttons["edit"]["edit_section"]["ACTION_URL"];
121
122
        $template->AddEditAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_EDIT"));
123
    }
124
125
    /**
126
     * @param CBitrixComponentTemplate $template
127
     * @param $section
128
     * @param string $confirm
129
     */
130 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...
131
    {
132
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
133
            return;
134
        }
135
136
        if (is_numeric($section)) {
137
            $section = static::prepareIBlockSectionArrayById($section);
138
        }
139
140
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
141
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
142
        }
143
    
144
        $buttons = static::getIBlockSectionPanelButtons($section);
145
        $link = $buttons["edit"]["delete_section"]["ACTION_URL"];
146
147
        $template->AddDeleteAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_DELETE"), array("CONFIRM" => $confirm));
148
    }
149
150
    /**
151
     * @param CBitrixComponentTemplate $template
152
     * @param $section
153
     */
154
    public static function editAndDeleteIBlockSection($template, $section)
155
    {
156
        static::editIBlockSection($template, $section);
157
        static::deleteIBlockSection($template, $section);
158
    }
159
160
    /**
161
     * @param CBitrixComponentTemplate $template
162
     * @param $section
163
     * @return string
164
     */
165
    public static function areaForIBlockSection($template, $section)
166
    {
167
        return static::getEditArea($template, 'iblock_section', $section);
168
    }
169
170
    /**
171
     * @param CBitrixComponentTemplate $template
172
     * @param $element
173
     * @param string $label
174
     */
175 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...
176
    {
177
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
178
            return;
179
        }
180
181
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
182
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID');
183
        }
184
185
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?ENTITY_ID=%s&ID=%s&lang=ru&bxpublic=Y';
186
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
187
188
        $template->AddEditAction('hlblock_element_' . $element['ID'], $link, $label);
189
    }
190
    
191
    /**
192
     * @param CBitrixComponentTemplate $template
193
     * @param $element
194
     * @param string $label
195
     * @param string $confirm
196
     */
197 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...
198
    {
199
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
200
            return;
201
        }
202
        
203
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
204
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID');
205
        }
206
207
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?action=delete&ENTITY_ID=%s&ID=%s&lang=ru';
208
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
209
    
210
        $template->AddDeleteAction('hlblock_element_' . $element['ID'], $link, $label, array("CONFIRM" => $confirm));
211
    }
212
213
    /**
214
     * @param CBitrixComponentTemplate $template
215
     * @param $element
216
     */
217
    public static function editAndDeleteHLBlockElement($template, $element)
218
    {
219
        static::editHLBlockElement($template, $element);
220
        static::deleteHLBlockElement($template, $element);
221
    }
222
223
    /**
224
     * @param CBitrixComponentTemplate $template
225
     * @param $element
226
     * @return string
227
     */
228
    public static function areaForHLBlockElement($template, $element)
229
    {
230
        return static::getEditArea($template, 'hlblock_element', $element);
231
    }
232
    
233
    /**
234
     * @param CBitrixComponent|CBitrixComponentTemplate $componentOrTemplate
235
     * @param $iblockId
236
     * @param array $options
237
     */
238
    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...
239
    {
240
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
241
            return;
242
        }
243
244
        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...
245
            $componentOrTemplate = $componentOrTemplate->__component;
246
        }
247
248
        $buttons = CIBlock::GetPanelButtons($iblockId, 0, 0, $options);
249
        $menu = CIBlock::GetComponentMenu($GLOBALS['APPLICATION']->GetPublicShowMode(), $buttons);
250
251
        $componentOrTemplate->addIncludeAreaIcons($menu);
252
    }
253
254
    /**
255
     * @param $element
256
     * @return array
257
     */
258 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...
259
    {
260
        if (!isset(static::$panelButtons['iblock_element'][$element['ID']])) {
261
            static::$panelButtons['iblock_element'][$element['ID']] = CIBlock::GetPanelButtons(
262
                $element["IBLOCK_ID"],
263
                $element['ID'],
264
                0,
265
                ['SECTION_BUTTONS' => false, 'SESSID' => false]
266
            );
267
        }
268
269
        return static::$panelButtons['iblock_element'][$element['ID']];
270
    }
271
272
    /**
273
     * @param $section
274
     * @return array
275
     */
276 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...
277
    {
278
        if (!isset(static::$panelButtons['iblock_section'][$section['ID']])) {
279
            static::$panelButtons['iblock_section'][$section['ID']] = CIBlock::GetPanelButtons(
280
                $section["IBLOCK_ID"],
281
                0,
282
                $section['ID'],
283
                ['SESSID' => false]
284
            );
285
        }
286
287
        return static::$panelButtons['iblock_section'][$section['ID']];
288
    }
289
    
290
    /**
291
     * @param int $id
292
     * @return array
293
     */
294 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...
295
    {
296
        $id = (int) $id;
297
        if (!$id) {
298
            return [];
299
        }
300
301
        if (empty(static::$iblockElementArray[$id])) {
302
            $connection = Application::getConnection();
303
            $el = $connection->query("SELECT ID, IBLOCK_ID FROM b_iblock_element WHERE ID = {$id}")->fetch();
304
            static::$iblockElementArray[$id] = $el ? $el : [];
305
        }
306
307
        return static::$iblockElementArray[$id];
308
    }
309
310
    /**
311
     * @param int $id
312
     * @return array
313
     */
314 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...
315
    {
316
        $id = (int) $id;
317
        if (!$id) {
318
            return [];
319
        }
320
321
        if (empty(static::$iblockSectionArray[$id])) {
322
            $connection = Application::getConnection();
323
            $el = $connection->query("SELECT ID, IBLOCK_ID FROM b_iblock_section WHERE ID = {$id}")->fetch();
324
            static::$iblockSectionArray[$id] = $el ? $el : [];
325
        }
326
327
        return static::$iblockSectionArray[$id];
328
    }
329
}
330