Completed
Push — master ( 14e5a2...194a6e )
by Nekrasov
01:00
created

Actions::areaForIBlockSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Arrilot\BitrixHermitage;
4
5
use CBitrixComponent;
6
use CBitrixComponentTemplate;
7
use CIBlock;
8
use InvalidArgumentException;
9
10
class Actions
11
{
12
    protected static $panelButtons = [];
13
14
    /**
15
     * Get edit area id for specific type
16
     *
17
     * @param CBitrixComponentTemplate $template
18
     * @param $type
19
     * @param $element
20
     * @return string
21
     */
22
    public static function getEditArea($template, $type, $element)
23
    {
24
        return $template->GetEditAreaId($type . '_' . $element['ID']);
25
    }
26
27
    /**
28
     * @param CBitrixComponentTemplate $template
29
     * @param $element
30
     */
31 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...
32
    {
33
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
34
            return;
35
        }
36
37
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
38
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
39
        }
40
41
        $buttons = static::getIBlockElementPanelButtons($element);
42
        $link = $buttons["edit"]["edit_element"]["ACTION_URL"];
43
44
        $template->AddEditAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_EDIT"));
45
    }
46
    
47
    /**
48
     * @param CBitrixComponentTemplate $template
49
     * @param $element
50
     * @param string $confirm
51
     */
52 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...
53
    {
54
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
55
            return;
56
        }
57
58
        if (!$element["IBLOCK_ID"] || !$element['ID']) {
59
            throw new InvalidArgumentException('Element must include ID and IBLOCK_ID');
60
        }
61
    
62
        $buttons = static::getIBlockElementPanelButtons($element);
63
        $link = $buttons["edit"]["delete_element"]["ACTION_URL"];
64
65
        $template->AddDeleteAction('iblock_element_' . $element['ID'], $link, CIBlock::GetArrayByID($element["IBLOCK_ID"], "ELEMENT_DELETE"), array("CONFIRM" => $confirm));
66
    }
67
68
    /**
69
     * @param CBitrixComponentTemplate $template
70
     * @param $element
71
     * @return string
72
     */
73
    public static function areaForIBlockElement($template, $element)
74
    {
75
        return static::getEditArea($template, 'iblock_element', $element);
76
    }
77
78
    /**
79
     * @param CBitrixComponentTemplate $template
80
     * @param $section
81
     */
82 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...
83
    {
84
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
85
            return;
86
        }
87
88
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
89
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
90
        }
91
    
92
        $buttons = static::getIBlockSectionPanelButtons($section);
93
        $link = $buttons["edit"]["edit_section"]["ACTION_URL"];
94
95
        $template->AddEditAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_EDIT"));
96
    }
97
98
    /**
99
     * @param CBitrixComponentTemplate $template
100
     * @param $section
101
     * @param string $confirm
102
     */
103 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...
104
    {
105
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
106
            return;
107
        }
108
109
        if (!$section["IBLOCK_ID"] || !$section['ID']) {
110
            throw new InvalidArgumentException('Section must include ID and IBLOCK_ID');
111
        }
112
    
113
        $buttons = static::getIBlockSectionPanelButtons($section);
114
        $link = $buttons["edit"]["delete_section"]["ACTION_URL"];
115
116
        $template->AddDeleteAction('iblock_section_' . $section['ID'], $link, CIBlock::GetArrayByID($section["IBLOCK_ID"], "SECTION_DELETE"), array("CONFIRM" => $confirm));
117
    }
118
119
    /**
120
     * @param CBitrixComponentTemplate $template
121
     * @param $section
122
     * @return string
123
     */
124
    public static function areaForIBlockSection($template, $section)
125
    {
126
        return static::getEditArea($template, 'iblock_section', $section);
127
    }
128
129
    /**
130
     * @param CBitrixComponentTemplate $template
131
     * @param $element
132
     * @param string $label
133
     */
134 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...
135
    {
136
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
137
            return;
138
        }
139
140
        if (!$element["HLBLOCK_ID"] || !$element['ID']) {
141
            throw new InvalidArgumentException('Element must include ID and HLBLOCK_ID');
142
        }
143
144
        $linkTemplate = '/bitrix/admin/highloadblock_row_edit.php?ENTITY_ID=%s&ID=%s&lang=ru&bxpublic=Y';
145
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
146
147
        $template->AddEditAction('hlblock_element_' . $element['ID'], $link, $label);
148
    }
149
    
150
    /**
151
     * @param CBitrixComponentTemplate $template
152
     * @param $element
153
     * @param string $label
154
     * @param string $confirm
155
     */
156 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...
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?action=delete&ENTITY_ID=%s&ID=%s&lang=ru';
167
        $link = sprintf($linkTemplate, (int) $element["HLBLOCK_ID"], (int) $element["ID"]);
168
    
169
        $template->AddDeleteAction('hlblock_element_' . $element['ID'], $link, $label, array("CONFIRM" => $confirm));
170
    }
171
172
    /**
173
     * @param CBitrixComponentTemplate $template
174
     * @param $element
175
     * @return string
176
     */
177
    public static function areaForHLBlockElement($template, $element)
178
    {
179
        return static::getEditArea($template, 'hlblock_element', $element);
180
    }
181
    
182
    /**
183
     * @param CBitrixComponent|CBitrixComponentTemplate $componentOrTemplate
184
     * @param $iblockId
185
     * @param array $options
186
     */
187
    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...
188
    {
189
        if (!$GLOBALS['APPLICATION']->GetShowIncludeAreas()) {
190
            return;
191
        }
192
193
        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...
194
            $componentOrTemplate = $componentOrTemplate->__component;
195
        }
196
197
        $buttons = CIBlock::GetPanelButtons($iblockId, 0, 0, $options);
198
        $menu = CIBlock::GetComponentMenu($GLOBALS['APPLICATION']->GetPublicShowMode(), $buttons);
199
200
        $componentOrTemplate->addIncludeAreaIcons($menu);
201
    }
202
203
    /**
204
     * @param $element
205
     * @return array
206
     */
207 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...
208
    {
209
        if (!isset(static::$panelButtons['iblock_element'][$element['ID']])) {
210
            static::$panelButtons['iblock_element'][$element['ID']] = CIBlock::GetPanelButtons(
211
                $element["IBLOCK_ID"],
212
                $element['ID'],
213
                0,
214
                ['SECTION_BUTTONS' => false, 'SESSID' => false]
215
            );
216
        }
217
218
        return static::$panelButtons['iblock_element'][$element['ID']];
219
    }
220
221
    /**
222
     * @param $section
223
     * @return array
224
     */
225 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...
226
    {
227
        if (!isset(static::$panelButtons['iblock_section'][$section['ID']])) {
228
            static::$panelButtons['iblock_section'][$section['ID']] = CIBlock::GetPanelButtons(
229
                $section["IBLOCK_ID"],
230
                0,
231
                $section['ID'],
232
                ['SESSID' => false]
233
            );
234
        }
235
236
        return static::$panelButtons['iblock_section'][$section['ID']];
237
    }
238
}
239