Completed
Push — master ( 10939c...949f9f )
by Nekrasov
01:08
created

Action::prepareHLBlockIdByTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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