1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
tablefields.php
См. документацию.
1<?php
2
4{
5 const VT_NUMBER = "number";
6 const VT_STRING = "string";
7 const VT_Y_N = "Y_N";
8 const VT_Y_N_NULL = "Y_N_NULL";
9 const VT_DATE = "date";
10 const VT_DATE_TIME = "datetime";
11
12
13 const JS_HREF = 1;
14 const JS_HREF_ALERT = 2;
15 const JS_IN_QUOTES = 3;
16 const JS_EVENT = 4;
17 const ATTRIBUTE = 5;
18 const ATTRIBUTE_EX = 6;
19 const HREF_LOCATION = 7;
20 const ID = 8;
21
22 const NOT_NULL = 1;
23 const NOT_DEFAULT = 2;
24 const ONLY_CHANGED = 3;
25 const MORE0 = 4;
26 const NOT_EMTY_STR = 5;
27
28 const ALL = null;
29
30 const C_Array = 1;
31 const C_Table = 2;
32
33 protected $_arFieldsTypes = array(); /*
34 array(
35 "aaa" => array(
36 "TYPE" => self::VT_STRING,
37 "DEF_VAL" => null,
38 "MAX_STR_LEN" => 255,
39 "AUTO_CALCULATED" => true,
40 "LIST" => array("q1","q2")
41 )
42 )*/
43 protected $_arFields = array(); //array(0 => array("aaa" => null))
44 protected $_arModifiedFields = array(); //array(0 => array("aaa", "bbb"))
45 protected $_classType = self::C_Array;
46 protected $_currentRow = 0;
47 protected $_resetNextF = false;
48 protected $_sortFields = array();
49
50 static function Convert($type, $value, $op)
51 {
52 $maxStrLen = (array_key_exists("MAX_STR_LEN", $op) ? $op["MAX_STR_LEN"] : 0);
53 $list = (array_key_exists("LIST", $op) ? $op["LIST"] : null);
54 $defVal = (array_key_exists("DEF_VAL", $op) ? $op["DEF_VAL"] : null);
55 $res = null;
56 switch($type)
57 {
58 case self::VT_NUMBER:
59 $res = (intval($value) == floatval($value)) ? intval($value) : floatval($value);
60 break;
61 case self::VT_STRING:
62 $res = ($maxStrLen == 0? $value : mb_substr($value, 0, $maxStrLen)) ;
63 break;
64 case self::VT_Y_N_NULL:
65 $res = ($value == "Y") ? "Y" : ($value === null ? null : "N");
66 break;
67 case self::VT_Y_N:
68 $res = ($value == "Y") ? "Y" : "N";
69 break;
70 case self::VT_DATE:
71 $res = (is_int($value) || $value===null) ? $value : MakeTimeStamp($value , FORMAT_DATE);
72 $res = (is_int($res) || $res===null) ? $res : null;
73 break;
74 case self::VT_DATE_TIME:
75 $res = (is_int($value) || $value===null) ? $value : MakeTimeStamp($value , FORMAT_DATETIME);
76 $res = (is_int($res) || $res===null) ? $res : null;
77 break;
78 }
79 if($list != null) $res = (in_array($res, $list) ? $res : $defVal);
80
81 return $res;
82 }
83
84 static function ConvertForSQL($type, $value)
85 {
86 global $DB;
87 $sf = "FULL";
88 if($value === null) return "null";
89 if($value === 0) return "0";
90 switch($type)
91 {
92 case self::VT_NUMBER: return (is_float($value)) ? floatval($value) : intval($value);
93
94 case self::VT_Y_N:
95 case self::VT_Y_N_NULL:
96 case self::VT_STRING: return "'" . $DB->ForSql($value) . "'";
97
98 case self::VT_DATE: $sf = "SHORT";
99 case self::VT_DATE_TIME: return (is_int($value) ? $DB->CharToDateFunction(GetTime($value, $sf)) : $value);
100 }
101 }
102
103 static function ConvertForHTML($type, $place, $value, $op)
104 {
105 switch($type)
106 {
107 case self::VT_DATE: return GetTime($value, "SHORT");
108 case self::VT_DATE_TIME: return GetTime($value, "FULL");
109 case self::VT_STRING: break;
110 default: return $value;
111 }
112
113 $WHITE_LIST = (array_key_exists("WHITE_LIST", $op) ? $op["WHITE_LIST"] : array());
114 $DEF_VAL = (array_key_exists("DEF_VAL", $op) ? $op["DEF_VAL"] : null);
115 switch($place)
116 {
117 case self::JS_HREF: return urlencode(urlencode($value));
118 case self::JS_HREF_ALERT: return urlencode(CUtil::addslashes($value));
119 case self::JS_IN_QUOTES: return CUtil::JSEscape($value);
120 case self::JS_EVENT: return CUtil::addslashes(htmlspecialcharsbx($value));
121 case self::ATTRIBUTE: return htmlspecialcharsbx($value);
122 case self::ATTRIBUTE_EX: return htmlspecialcharsEx($value);
123 case self::ID: return preg_replace("/[^a-zA-Z0-9_]/", "", $value);
124 case self::HREF_LOCATION:
125 $res = null;
126 foreach($WHITE_LIST as $key => $value) if(mb_substr($value, 0, mb_strlen($value)) == $value) $res = $value;
127 if($res == null) $res = "/" . $value;
128 return CUtil::addslashes(htmlspecialcharsbx($res));
129 }
130 return $DEF_VAL;
131 }
132
133 function SortMethod($a, $b)
134 {
135 foreach($this->_sortFields as $k => $name)
136 {
137 if(array_key_exists($name, $this->_arFieldsTypes) && $a[$name] != $b[$name])
138 {
139 return ($a[$name] < $b[$name]) ? -1 : 1;
140 }
141 }
142 return 0;
143 }
144
145 public function __construct($f, $arOrTable = self::C_Array)
146 {
147 $this->_arFieldsTypes = $f;
148 $this->_classType = $arOrTable;
149 $this->CleanVar();
150 }
151
152 public function checkRow($row)
153 {
154 if($this->_classType == self::C_Array || !array_key_exists($this->_currentRow, $this->_arFields)) $this->First();
155 if($row != null && array_key_exists($row, $this->_arFields)) return $row;
156 return $this->_currentRow;
157 }
158
159 public function First()
160 {
161 $this->_currentRow = 0;
162 }
163
164 public function Last()
165 {
166 $this->_currentRow = (count($this->_arFields) - 1);
167 }
168
169 public function ResetNext()
170 {
171 $this->_resetNextF = true;
172 }
173
174 public function Next()
175 {
176 if($this->_resetNextF)
177 {
178 $this->_currentRow = -1;
179 $this->_resetNextF = false;
180 }
181 if($this->_currentRow >= (count($this->_arFields) - 1))
182 {
183 return false;
184 }
185 $this->_currentRow++;
186 return true;
187 }
188
189 public function Previous()
190 {
191 if($this->_currentRow <= 0) return false;
192 $this->_currentRow--;
193 return true;
194 }
195
196 public function Row($row)
197 {
198 $r = intval($row);
199 if((count($this->table) - 1) < $r) return false;
200 $this->_currentRow = $r;
201 return true;
202 }
203
204 public function CleanVar($row = null, $removeExistingRows = false)
205 {
206 if($removeExistingRows) $this->RemoveExistingRows();
207 $row = $this->checkRow($row);
208 $this->_arFields[$row] = array();
209 $this->_arModifiedFields[$row] = array();
210 foreach($this->_arFieldsTypes as $key => $value) $this->Set($key, $value["DEF_VAL"], array(), $row, false);
211 }
212
213 public function RemoveExistingRows()
214 {
215 $this->_arFields = array();
216 $this->_arModifiedFields = array();
217 $this->First();
218 }
219
220 public function __set($name, $value)
221 {
222 $this->Set($name, $value);
223 }
224
225 public function AddRow()
226 {
227 $this->_arFields[] = array();
228 $this->_arModifiedFields[] = array();
229 $this->Last();
230 $this->CleanVar();
231 }
232
233 /* заполнить поля из массива
234 $sf = "Имя поля,Имя поля2,..."
235 $sf = array("Имя поля", "Имя поля2",...) */
236 public function SortRow($sf)
237 {
238 $this->_sortFields = CSupportTools::prepareParamArray($sf);
240 uasort($arr, array($this, 'SortMethod'));
241 $this->_arFields = $arr;
242 }
243
244 //$notNull = array(self::NOT_NULL, self::MORE0, self::NOT_EMTY_STR)
245 public function Set($name, $value, $notNull = array(), $row = null, $isModified = true)
246 {
247 if(!array_key_exists($name, $this->_arFieldsTypes)) return;
248 $row = $this->checkRow($row);
249 $op = array();
250 $ft = $this->_arFieldsTypes[$name];
251 if((in_array(self::NOT_NULL, $notNull) && $value === null)
252 || (in_array(self::MORE0, $notNull) && $ft["TYPE"] == self::VT_NUMBER && intval($value) <= 0)
253 || (in_array(self::NOT_EMTY_STR, $notNull) && $value === "")
254 ) return;
255 if(array_key_exists("MAX_STR_LEN", $ft)) $op["MAX_STR_LEN"] = $ft["MAX_STR_LEN"];
256 if(array_key_exists("LIST", $ft))
257 {
258 $op["LIST"] = $ft["LIST"];
259 $op["DEF_VAL"] = $ft["DEF_VAL"];
260 }
261 $this->_arFields[$row][$name] = self::Convert($ft["TYPE"], $value, $op);
262 $this->_arModifiedFields[$row][$name] = $isModified;
263 }
264
265 public function SetCurrentTime($name, $row = null)
266 {
267 global $DB;
268 $row = $this->checkRow($row);
269 $arName = CSupportTools::prepareParamArray($name);
270 foreach($arName as $key => $n)
271 {
272 if(!array_key_exists($n, $this->_arFieldsTypes)) return;
273 if($this->_arFieldsTypes[$n]["TYPE"] == self::VT_DATE)
274 {
275 $this->_arFields[$row][$n] = time() + CTimeZone::GetOffset();
276 $this->_arModifiedFields[$row][$n] = true;
277 }
278 elseif($this->_arFieldsTypes[$n]["TYPE"] == self::VT_DATE_TIME)
279 {
280 $this->_arFields[$row][$n] = time() + CTimeZone::GetOffset();
281 $this->_arModifiedFields[$row][$n] = true;
282 }
283 }
284 }
285
286 /* заполнить поля из массива
287 $fields = CSupportTableFields::ALL
288 $fields = "Имя поля,Имя поля2,..."
289 $fields = array("Имя поля", "Имя поля2",...)
290 $fields = array("Имя поля" => "Имя поля в массиве", "Имя поля2" => "Имя поля в массиве2",...)
291 $notNull = array(self::NOT_EMTY_STR, self::MORE0, self::NOT_EMTY_STR) */
292 public function FromArray($arr, $fields = self::ALL, $notNull = array(), $row = null) //setFromArr
293 {
294 if(!is_array($arr)) return;
295 $row = $this->checkRow($row);
296 $fieldsArr = CSupportTools::prepareParamArray($fields, array_keys($arr));
297 foreach($fieldsArr as $key => $name)
298 {
299 $nameF = is_int($key) ? $name : $key;
300 if(array_key_exists($name, $arr)) $this->Set($nameF, $arr[$name], $notNull, $row);
301 }
302 }
303
304 /* заполнить поля из массива
305 $fields = CSupportTableFields::ALL
306 $fields = "Имя поля,Имя поля2,..."
307 $fields = array("Имя поля", "Имя поля2",...)
308 $fields = array("Имя поля" => "Имя поля в массиве", "Имя поля2" => "Имя поля в массиве2",...)
309 $notNull = array(self::NOT_EMTY_STR, self::MORE0, self::NOT_EMTY_STR) */
310 public function FromTable($table, $fields = self::ALL, $notNull = array(), $removeExistingRows = false) //setFromTable
311 {
312 if($removeExistingRows)
313 {
314 $this->RemoveExistingRows();
315 }
316 if(!is_array($table))
317 {
318 return;
319 }
320 foreach($table as $key => $arr)
321 {
322 $this->AddRow();
323 $this->FromArray($arr, $fields, $notNull);
324 }
325 }
326
327 public function __get($name)
328 {
329 return $this->Get($name);
330 }
331
332 public function Get($name, $row = null)
333 {
334 if(!array_key_exists($name, $this->_arFieldsTypes)) return null;
335 $row = $this->checkRow($row);
336 return $this->_arFields[$row][$name];
337 }
338
339 /*Выгрузить перечисленные поля в массив
340 $notNull = array(self::NOT_NULL, self::NOT_DEFAULT, self::ONLY_CHANGED)
341 $fields = CSupportTableFields::ALL
342 $fields = "Имя поля,Имя поля2,..."
343 $fields = array("Имя поля", "Имя поля2",...)
344 $fields = array("Имя поля" => "Имя поля в массиве", "Имя поля2" => "Имя поля в массиве2",...)*/
345 public function ToArray($fields = self::ALL, $notNull = array(), $forSQL = false, $row = null) //getArr
346 {
347 $row = $this->checkRow($row);
348 $res = array();
349 $arFields = CSupportTools::prepareParamArray($fields, array_keys($this->_arFields[$row]));
350 foreach($arFields as $key => $name)
351 {
352 $fName = is_int($key) ? $name : $key;
353 if(!array_key_exists($fName, $this->_arFieldsTypes)) continue;
354 $v = $this->_arFields[$row][$fName];
355 $ft = $this->_arFieldsTypes[$fName];
356 if(in_array(self::ONLY_CHANGED, $notNull) && (!isset($this->_arModifiedFields[$row][$fName]) || $this->_arModifiedFields[$row][$fName] != true))
357 {
358 continue;
359 }
360 elseif(in_array(self::NOT_NULL, $notNull) && $v === null)
361 {
362 continue;
363 }
364 elseif(in_array(self::NOT_DEFAULT, $notNull) && $v === $ft["DEF_VAL"])
365 {
366 continue;
367 }
368 if($forSQL)
369 {
370 if(array_key_exists("AUTO_CALCULATED", $ft)) continue;
371 $res[$name] = self::ConvertForSQL($ft["TYPE"], $v);
372 }
373 else $res[$name] = $v;
374 }
375 return $res;
376 }
377
378 public function GetFieldForOutput($name, $place, $whiteList = array("http", "ftp", "/"), $row = null)
379 {
380 $row = $this->checkRow($row);
381 if(!array_key_exists($name, $this->_arFieldsTypes))
382 {
383 return null;
384 }
385 $ft = $this->_arFieldsTypes[$name];
386 $value = $this->_arFields[$row][$name];
387 $op = array(
388 "WHITE_LIST" => $whiteList,
389 "DEF_VAL" => $ft["DEF_VAL"]
390 );
391 return self::ConvertForHTML($ft["TYPE"], $place, $value, $op);
392 }
393
394 public function GetColumn($name)
395 {
396 $res = array();
397 if(!array_key_exists($name, $this->_arFieldsTypes))
398 {
399 return false;
400 }
401 foreach($this->_arFields as $nom => $row) $res[$nom] = $row[$name];
402 return $res;
403 }
404
405}
406?>
$type
Определения options.php:106
const MORE0
Определения tablefields.php:25
const ID
Определения tablefields.php:20
SortMethod($a, $b)
Определения tablefields.php:133
$_arModifiedFields
Определения tablefields.php:44
const JS_IN_QUOTES
Определения tablefields.php:15
const C_Array
Определения tablefields.php:30
AddRow()
Определения tablefields.php:225
Last()
Определения tablefields.php:164
RemoveExistingRows()
Определения tablefields.php:213
static Convert($type, $value, $op)
Определения tablefields.php:50
const NOT_NULL
Определения tablefields.php:22
FromTable($table, $fields=self::ALL, $notNull=array(), $removeExistingRows=false)
Определения tablefields.php:310
GetFieldForOutput($name, $place, $whiteList=array("http", "ftp", "/"), $row=null)
Определения tablefields.php:378
const NOT_EMTY_STR
Определения tablefields.php:26
const HREF_LOCATION
Определения tablefields.php:19
const NOT_DEFAULT
Определения tablefields.php:23
$_arFieldsTypes
Определения tablefields.php:33
CleanVar($row=null, $removeExistingRows=false)
Определения tablefields.php:204
$_classType
Определения tablefields.php:45
static ConvertForHTML($type, $place, $value, $op)
Определения tablefields.php:103
const VT_Y_N
Определения tablefields.php:7
const JS_HREF_ALERT
Определения tablefields.php:14
$_currentRow
Определения tablefields.php:46
const VT_Y_N_NULL
Определения tablefields.php:8
__construct($f, $arOrTable=self::C_Array)
Определения tablefields.php:145
const JS_EVENT
Определения tablefields.php:16
static ConvertForSQL($type, $value)
Определения tablefields.php:84
__set($name, $value)
Определения tablefields.php:220
checkRow($row)
Определения tablefields.php:152
GetColumn($name)
Определения tablefields.php:394
const ONLY_CHANGED
Определения tablefields.php:24
const VT_DATE
Определения tablefields.php:9
Set($name, $value, $notNull=array(), $row=null, $isModified=true)
Определения tablefields.php:245
$_sortFields
Определения tablefields.php:48
Previous()
Определения tablefields.php:189
const VT_NUMBER
Определения tablefields.php:5
const C_Table
Определения tablefields.php:31
SortRow($sf)
Определения tablefields.php:236
FromArray($arr, $fields=self::ALL, $notNull=array(), $row=null)
Определения tablefields.php:292
SetCurrentTime($name, $row=null)
Определения tablefields.php:265
First()
Определения tablefields.php:159
const ALL
Определения tablefields.php:28
const ATTRIBUTE_EX
Определения tablefields.php:18
$_arFields
Определения tablefields.php:43
Row($row)
Определения tablefields.php:196
__get($name)
Определения tablefields.php:327
const VT_STRING
Определения tablefields.php:6
ToArray($fields=self::ALL, $notNull=array(), $forSQL=false, $row=null)
Определения tablefields.php:345
Next()
Определения tablefields.php:174
$_resetNextF
Определения tablefields.php:47
const JS_HREF
Определения tablefields.php:13
ResetNext()
Определения tablefields.php:169
const ATTRIBUTE
Определения tablefields.php:17
const VT_DATE_TIME
Определения tablefields.php:10
Get($name, $row=null)
Определения tablefields.php:332
$f
Определения component_props.php:52
$arFields
Определения dblapprove.php:5
$arr
Определения file_new.php:624
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
Get()
Определения idea_idea_comment.php:22
global $DB
Определения cron_frame.php:29
const FORMAT_DATETIME
Определения include.php:64
const FORMAT_DATE
Определения include.php:63
htmlspecialcharsEx($str)
Определения tools.php:2685
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
GetTime($timestamp, $type="SHORT", $site=false, $bSearchInSitesOnly=false)
Определения tools.php:1890
MakeTimeStamp($datetime, $format=false)
Определения tools.php:538
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
else $a
Определения template.php:137
$k
Определения template_pdf.php:567
$n
Определения update_log.php:107
$fields
Определения yandex_run.php:501