Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
booltype.php
1<?php
3
6
7Loc::loadMessages(__FILE__);
8
13class BoolType extends Base
14{
15
19 public static function getType()
20 {
21 return FieldType::BOOL;
22 }
23
31 public static function toSingleValue(FieldType $fieldType, $value)
32 {
33 if (is_array($value))
34 {
35 reset($value);
36 $value = current($value);
37 }
38 return $value;
39 }
40
46 protected static function formatValuePrintable(FieldType $fieldType, $value)
47 {
48 return mb_strtoupper($value) != 'N' && !empty($value)
49 ? Loc::getMessage('BPDT_BOOL_YES')
50 : Loc::getMessage('BPDT_BOOL_NO');
51 }
52
59 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
60 {
62 $type = $toTypeClass::getType();
63 switch ($type)
64 {
66 case FieldType::INT:
67 $value = (int)($value == 'Y');
68 break;
69 case FieldType::BOOL:
71 case FieldType::TEXT:
72 if (in_array(mb_strtolower($value), ['y', 'yes', 'true', '1'], true))
73 {
74 $value = 'Y';
75 }
76 elseif (in_array(mb_strtolower($value), ['n', 'no', 'false', '0'], true))
77 {
78 $value = 'N';
79 }
80
81 $value = $value == 'Y' ? 'Y' : 'N';
82 break;
83 default:
84 $value = null;
85 }
86
87 return $value;
88 }
89
94 public static function getConversionMap()
95 {
96 return array(
97 array(
103 )
104 );
105 }
106
115 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
116 {
117 $isPublicControl = $renderMode & FieldType::RENDER_MODE_PUBLIC;
118 $className = $isPublicControl ? static::generateControlClassName($fieldType, $field) : '';
119
120 $renderResult = sprintf(
121 '<select id="%s" name="%s" class="%s">',
122 htmlspecialcharsbx(static::generateControlId($field)),
123 htmlspecialcharsbx(static::generateControlName($field)),
124 htmlspecialcharsbx($className)
125 );
126
127 if (!$fieldType->isRequired())
128 {
129 $renderResult .= '<option value="">['.Loc::getMessage("BPDT_BOOL_NOT_SET").']</option>';
130 }
131
132 $renderResult .= sprintf(
133 '<option value="Y"%s>%s</option>
134 <option value="N"%s>%s</option>
135 </select>',
136 $value === 'Y' ? ' selected' : '',
137 Loc::getMessage('BPDT_BOOL_YES'),
138 $value === 'N' ? ' selected' : '',
139 Loc::getMessage('BPDT_BOOL_NO')
140 );
141
142 return $renderResult;
143 }
144
145 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
146 {
147 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
148 {
149 $allowSelection = false;
150 }
151
152 return parent::renderControlSingle($fieldType, $field, $value, $allowSelection, $renderMode);
153 }
154
155 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
156 {
157 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
158 {
159 $allowSelection = false;
160 }
161
162 return parent::renderControlMultiple($fieldType, $field, $value, $allowSelection, $renderMode);
163 }
164
169 public static function canRenderControl($renderMode)
170 {
171 return true;
172 }
173
180 protected static function extractValue(FieldType $fieldType, array $field, array $request)
181 {
182 $value = parent::extractValue($fieldType, $field, $request);
183
184 if ($value !== null && $value !== 'Y' && $value !== 'N')
185 {
186 if (is_bool($value))
187 {
188 $value = $value ? 'Y' : 'N';
189 }
190 elseif (is_string($value) && $value <> '')
191 {
192 $value = mb_strtolower($value);
193 if (in_array($value, array('y', 'yes', 'true', '1')))
194 {
195 $value = 'Y';
196 }
197 elseif (in_array($value, array('n', 'no', 'false', '0')))
198 {
199 $value = 'N';
200 }
201 else
202 {
203 $value = null;
204 static::addError(array(
205 'code' => 'ErrorValue',
206 'message' => Loc::getMessage('BPDT_BOOL_INVALID'),
207 'parameter' => static::generateControlName($field),
208 ));
209 }
210 }
211 else
212 {
213 $value = null;
214 }
215 }
216
217 return $value;
218 }
219
220 public static function externalizeValue(FieldType $fieldType, $context, $value)
221 {
222 $map = $fieldType->getSettings()['ExternalValues'] ?? null;
223 if ($map && isset($map[$value]))
224 {
225 return $map[$value];
226 }
227
228 return parent::externalizeValue($fieldType, $context, $value);
229 }
230
231 public static function compareValues($valueA, $valueB)
232 {
233 $valueA = \CBPHelper::getBool($valueA);
234 $valueB = \CBPHelper::getBool($valueB);
235
236 return parent::compareValues($valueA, $valueB);
237 }
238}
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition booltype.php:115
static extractValue(FieldType $fieldType, array $field, array $request)
Definition booltype.php:180
static externalizeValue(FieldType $fieldType, $context, $value)
Definition booltype.php:220
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition booltype.php:145
static formatValuePrintable(FieldType $fieldType, $value)
Definition booltype.php:46
static toSingleValue(FieldType $fieldType, $value)
Definition booltype.php:31
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition booltype.php:155
static compareValues($valueA, $valueB)
Definition booltype.php:231
static canRenderControl($renderMode)
Definition booltype.php:169
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29