Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
double.php
1<?php
3
7
8Loc::loadMessages(__FILE__);
9
14class Double extends Base
15{
16
20 public static function getType()
21 {
22 return FieldType::DOUBLE;
23 }
24
32 public static function toSingleValue(FieldType $fieldType, $value)
33 {
34 if (is_array($value))
35 {
36 reset($value);
37 $value = current($value);
38 }
39 return $value;
40 }
41
48 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
49 {
51 $type = $toTypeClass::getType();
52 switch ($type)
53 {
54 case FieldType::BOOL:
55 $value = (bool)$value ? 'Y' : 'N';
56 break;
57 case FieldType::DATE:
58 $value = date(Main\Type\Date::convertFormatToPhp(\FORMAT_DATE), (int)$value);
59 break;
61 $value = date(Main\Type\DateTime::convertFormatToPhp(\FORMAT_DATETIME), (int)$value);
62 break;
64 $value = (float)$value;
65 break;
66 case FieldType::INT:
67 $value = (int)$value;
68 break;
70 case FieldType::TEXT:
71 $value = (string) $value;
72 break;
73 case FieldType::USER:
74 $value = 'user_'.(int)$value;
75 break;
76 default:
77 $value = null;
78 }
79
80 return $value;
81 }
82
87 public static function getConversionMap()
88 {
89 return array(
90 array(
99 )
100 );
101 }
102
111 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
112 {
113 if ($allowSelection && !($renderMode & FieldType::RENDER_MODE_PUBLIC))
114 {
115 return static::renderControlSelector($field, $value, 'combine', '', $fieldType);
116 }
117
118 $name = static::generateControlName($field);
119 $controlId = static::generateControlId($field);
120 $className = static::generateControlClassName($fieldType, $field);
121
122 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
123 {
124 $selectorAttributes = '';
125 if ($allowSelection)
126 {
127 $selectorAttributes = sprintf(
128 'data-role="inline-selector-target" data-property="%s" ',
129 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
130 );
131 }
132
133 $renderResult = sprintf(
134 '<input type="text" class="%s" name="%s" value="%s" placeholder="%s" %s/>',
135 htmlspecialcharsbx($className),
136 htmlspecialcharsbx($name),
137 htmlspecialcharsbx((string)$value),
138 htmlspecialcharsbx($fieldType->getDescription()),
139 $selectorAttributes
140 );
141 }
142 else
143 {
144 $renderResult = '<input type="text" class="'.htmlspecialcharsbx($className)
145 .'" size="10" id="'.htmlspecialcharsbx($controlId).'" name="'
146 .htmlspecialcharsbx($name).'" value="'.htmlspecialcharsbx((string) $value).'"/>';
147 }
148
149 return $renderResult;
150 }
151
156 public static function canRenderControl($renderMode)
157 {
158 return true;
159 }
160
169 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
170 {
171 $value = static::toSingleValue($fieldType, $value);
172 return static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
173 }
174
183 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
184 {
185 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
186 $value = array($value);
187
188 if (empty($value))
189 $value[] = null;
190
191 $controls = array();
192
193 foreach ($value as $k => $v)
194 {
195 $singleField = $field;
196 $singleField['Index'] = $k;
197 $controls[] = static::renderControl(
198 $fieldType,
199 $singleField,
200 $v,
201 $allowSelection,
202 $renderMode
203 );
204 }
205
206 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
207 {
208 $renderResult = static::renderPublicMultipleWrapper($fieldType, $field, $controls);
209 }
210 else
211 {
212 $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
213 }
214
215 return $renderResult;
216 }
217
224 protected static function extractValue(FieldType $fieldType, array $field, array $request)
225 {
226 $value = parent::extractValue($fieldType, $field, $request);
227
228 if ($value !== null && is_string($value) && $value <> '')
229 {
230 if (\CBPActivity::isExpression($value))
231 return $value;
232
233 $value = str_replace(' ', '', str_replace(',', '.', $value));
234 if (is_numeric($value))
235 {
236 $value = (float) $value;
237 }
238 else
239 {
240 $value = null;
241 static::addError(array(
242 'code' => 'ErrorValue',
243 'message' => Loc::getMessage('BPDT_DOUBLE_INVALID'),
244 'parameter' => static::generateControlName($field),
245 ));
246 }
247 }
248 else
249 {
250 $value = null;
251 }
252
253 return $value;
254 }
255}
static convertTo(FieldType $fieldType, $value, $toTypeClass)
Definition base.php:209
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition double.php:111
static extractValue(FieldType $fieldType, array $field, array $request)
Definition double.php:224
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition double.php:169
static toSingleValue(FieldType $fieldType, $value)
Definition double.php:32
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition double.php:183
static canRenderControl($renderMode)
Definition double.php:156
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static convertFormatToPhp($format)
Definition date.php:306