IblockElementWidget   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getEditHtml() 0 35 3
B getValueReadonly() 0 24 2
B generateRow() 0 28 2
1
<?php
2
3
namespace DigitalWand\AdminHelper\Widget;
4
5
use Bitrix\Iblock\ElementTable;
6
use Bitrix\Main\Loader;
7
use Bitrix\Main\Localization\Loc;
8
9
/**
10
 * Виджет для выбора элемента инфоблока.
11
 *
12
 * Доступные опции:
13
 * <ul>
14
 * <li> <b>IBLOCK_ID</b> - (int) ID инфоблока
15
 * <li> <b>INPUT_SIZE</b> - (int) значение атрибута size для input </li>
16
 * <li> <b>WINDOW_WIDTH</b> - (int) значение width для всплывающего окна выбора элемента </li>
17
 * <li> <b>WINDOW_HEIGHT</b> - (int) значение height для всплывающего окна выбора элемента </li>
18
 * </ul>
19
 *
20
 * @author Nik Samokhvalov <[email protected]>
21
 */
22
class IblockElementWidget extends NumberWidget
23
{
24
    static protected $defaults = array(
25
        'FILTER' => '=',
26
        'INPUT_SIZE' => 5,
27
        'WINDOW_WIDTH' => 600,
28
        'WINDOW_HEIGHT' => 500,
29
    );
30
    
31
    public function __construct(array $settings = array())
32
    {
33
        Loc::loadMessages(__FILE__);
34
        Loader::includeModule('iblock');
35
        
36
        parent::__construct($settings);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getEditHtml()
43
    {
44
        $iblockId = (int) $this->getSettings('IBLOCK_ID');
45
        $inputSize = (int) $this->getSettings('INPUT_SIZE');
46
        $windowWidth = (int) $this->getSettings('WINDOW_WIDTH');
47
        $windowHeight = (int) $this->getSettings('WINDOW_HEIGHT');
48
49
        $name = 'FIELDS';
50
        $key = $this->getCode();
51
52
        $elementId = $this->getValue();
53
54
        if (!empty($elementId)) {
55
            $rsElement = ElementTable::getById($elementId);
56
57
            if (!$element = $rsElement->fetchAll()) {
58
                $element['NAME'] = Loc::getMessage('IBLOCK_ELEMENT_NOT_FOUND');
59
            }
60
        } else {
61
            $elementId = '';
62
        }
63
64
        return '<input name="' . $this->getEditInputName() . '"
65
                     id="' . $name . '[' . $key . ']"
66
                     value="' . $elementId . '"
67
                     size="' . $inputSize . '"
68
                     type="text">' .
69
        '<input type="button"
70
                    value="..."
71
                    onClick="jsUtils.OpenWindow(\'/bitrix/admin/iblock_element_search.php?lang=' . LANGUAGE_ID
72
        . '&amp;IBLOCK_ID=' . $iblockId . '&amp;n=' . $name . '&amp;k=' . $key . '\', ' . $windowWidth . ', '
73
        . $windowHeight . ');">' . '&nbsp;<span id="sp_' . md5($name) . '_' . $key . '" >'
74
        . static::prepareToOutput($element['NAME'])
0 ignored issues
show
Bug introduced by
The variable $element does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
75
        . '</span>';
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getValueReadonly()
82
    {
83
        $elementId = $this->getValue();
84
85
        if (!empty($elementId)) {
86
            $rsElement = ElementTable::getList([
87
                'filter' => [
88
                    'ID' => $elementId
89
                ],
90
                'select' => [
91
                    'ID',
92
                    'NAME',
93
                    'IBLOCK_ID',
94
                    'IBLOCK.IBLOCK_TYPE_ID',
95
                ]
96
            ]);
97
98
            $element = $rsElement->fetch();
99
            
100
            return '<a href="/bitrix/admin/iblock_element_edit.php?IBLOCK_ID=' . $element['IBLOCK_ID']
101
            . '&type=' . $element['IBLOCK_ELEMENT_IBLOCK_IBLOCK_TYPE_ID'] . '&ID='
102
            . $elementId . '&lang=ru">[' . $elementId . '] ' . static::prepareToOutput($element['NAME']) . '</a>';
103
        }
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function generateRow(&$row, $data)
110
    {
111
        $elementId = $this->getValue();
112
113
        if (!empty($elementId)) {
114
            $rsElement = ElementTable::getList([
115
                'filter' => [
116
                    'ID' => $elementId
117
                ],
118
                'select' => [
119
                    'ID',
120
                    'NAME',
121
                    'IBLOCK_ID',
122
                    'IBLOCK.IBLOCK_TYPE_ID',
123
                ]
124
            ]);
125
            
126
            $element = $rsElement->fetch();
127
            
128
            $html = '<a href="/bitrix/admin/iblock_element_edit.php?IBLOCK_ID=' . $element['IBLOCK_ID']
129
                . '&type=' . $element['IBLOCK_ELEMENT_IBLOCK_IBLOCK_TYPE_ID'] . '&ID='
130
                . $elementId . '&lang=ru">[' . $elementId . '] ' . static::prepareToOutput($element['NAME']) . '</a>';
131
        } else {
132
            $html = '';
133
        }
134
135
        $row->AddViewField($this->getCode(), $html);
136
    }
137
}
138