Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
iblocktable.php
1<?php
2namespace Bitrix\Iblock;
3
21use CIBlockProperty;
22
23Loc::loadMessages(__FILE__);
24
79{
82
83 const RIGHTS_SIMPLE = 'S';
84 const RIGHTS_EXTENDED = 'E';
85
89
90 const LIST_MODE_SEPARATE = 'S';
91 const LIST_MODE_COMBINED = 'C';
92
96
97 /* deprecated constants */
106
107 const DATA_CLASS_NAMESPACE = 'Bitrix\Iblock\Elements';
108
109 const DATA_CLASS_PREFIX = 'Element';
110
116 public static function getTableName()
117 {
118 return 'b_iblock';
119 }
120
121 public static function getObjectClass()
122 {
123 return Iblock::class;
124 }
125
132 public static function getMap()
133 {
134 return array(
135 'ID' => array(
136 'data_type' => 'integer',
137 'primary' => true,
138 'autocomplete' => true,
139 'title' => Loc::getMessage('IBLOCK_ENTITY_ID_FIELD'),
140 ),
141 'TIMESTAMP_X' => array(
142 'data_type' => 'datetime',
143 'title' => Loc::getMessage('IBLOCK_ENTITY_TIMESTAMP_X_FIELD'),
144 ),
145 'IBLOCK_TYPE_ID' => array(
146 'data_type' => 'string',
147 'required' => true,
148 'title' => Loc::getMessage('IBLOCK_ENTITY_IBLOCK_TYPE_ID_FIELD'),
149 'validation' => array(__CLASS__, 'validateIblockTypeId'),
150 ),
151 'LID' => array(
152 'data_type' => 'string',
153 'validation' => array(__CLASS__, 'validateLid'),
154 ),
155 'CODE' => array(
156 'data_type' => 'string',
157 'title' => Loc::getMessage('IBLOCK_ENTITY_CODE_FIELD'),
158 'validation' => array(__CLASS__, 'validateCode'),
159 ),
160 (new StringField('API_CODE'))
161 ->configureSize(50),
162 (new BooleanField('REST_ON'))
163 ->configureValues('N', 'Y')
164 ->configureDefaultValue('N'),
165 'NAME' => array(
166 'data_type' => 'string',
167 'required' => true,
168 'title' => Loc::getMessage('IBLOCK_ENTITY_NAME_FIELD'),
169 'validation' => array(__CLASS__, 'validateName'),
170 ),
171 'ACTIVE' => array(
172 'data_type' => 'boolean',
173 'values' => array('N','Y'),
174 'title' => Loc::getMessage('IBLOCK_ENTITY_ACTIVE_FIELD'),
175 ),
176 'SORT' => array(
177 'data_type' => 'integer',
178 'title' => Loc::getMessage('IBLOCK_ENTITY_SORT_FIELD'),
179 ),
180 'LIST_PAGE_URL' => array(
181 'data_type' => 'string',
182 'title' => Loc::getMessage('IBLOCK_ENTITY_LIST_PAGE_URL_FIELD'),
183 'validation' => array(__CLASS__, 'validateListPageUrl'),
184 ),
185 'DETAIL_PAGE_URL' => array(
186 'data_type' => 'string',
187 'title' => Loc::getMessage('IBLOCK_ENTITY_DETAIL_PAGE_URL_FIELD'),
188 'validation' => array(__CLASS__, 'validateDetailPageUrl'),
189 ),
190 'SECTION_PAGE_URL' => array(
191 'data_type' => 'string',
192 'title' => Loc::getMessage('IBLOCK_ENTITY_SECTION_PAGE_URL_FIELD'),
193 'validation' => array(__CLASS__, 'validateSectionPageUrl'),
194 ),
195 'CANONICAL_PAGE_URL' => array(
196 'data_type' => 'string',
197 'title' => Loc::getMessage('IBLOCK_ENTITY_CANONICAL_PAGE_URL_FIELD'),
198 'validation' => array(__CLASS__, 'validateCanonicalPageUrl'),
199 ),
200 'PICTURE' => array(
201 'data_type' => 'integer',
202 'title' => Loc::getMessage('IBLOCK_ENTITY_PICTURE_FIELD'),
203 ),
204 'DESCRIPTION' => array(
205 'data_type' => 'string',
206 'title' => Loc::getMessage('IBLOCK_ENTITY_DESCRIPTION_FIELD'),
207 ),
208 'DESCRIPTION_TYPE' => array(
209 'data_type' => 'enum',
210 'values' => array('text', 'html'),
211 'title' => Loc::getMessage('IBLOCK_ENTITY_DESCRIPTION_TYPE_FIELD'),
212 ),
213 'XML_ID' => array(
214 'data_type' => 'string',
215 'title' => Loc::getMessage('IBLOCK_ENTITY_XML_ID_FIELD'),
216 'validation' => array(__CLASS__, 'validateXmlId'),
217 ),
218 'TMP_ID' => array(
219 'data_type' => 'string',
220 'title' => Loc::getMessage('IBLOCK_ENTITY_TMP_ID_FIELD'),
221 'validation' => array(__CLASS__, 'validateTmpId'),
222 ),
223 'INDEX_ELEMENT' => array(
224 'data_type' => 'boolean',
225 'values' => array('N','Y'),
226 'title' => Loc::getMessage('IBLOCK_ENTITY_INDEX_ELEMENT_FIELD'),
227 ),
228 'INDEX_SECTION' => array(
229 'data_type' => 'boolean',
230 'values' => array('N','Y'),
231 'title' => Loc::getMessage('IBLOCK_ENTITY_INDEX_SECTION_FIELD'),
232 ),
233 'WORKFLOW' => array(
234 'data_type' => 'boolean',
235 'values' => array('N','Y'),
236 'title' => Loc::getMessage('IBLOCK_ENTITY_WORKFLOW_FIELD'),
237 ),
238 'BIZPROC' => array(
239 'data_type' => 'boolean',
240 'values' => array('N','Y'),
241 'title' => Loc::getMessage('IBLOCK_ENTITY_BIZPROC_FIELD'),
242 ),
243 'SECTION_CHOOSER' => array(
244 'data_type' => 'enum',
245 'values' => array(
246 self::SECTION_CHOOSER_SELECT,
247 self::SECTION_CHOOSER_DROPDOWNS,
248 self::SECTION_CHOOSER_PATH
249 ),
250 'default_value' => self::SECTION_CHOOSER_SELECT,
251 'title' => Loc::getMessage('IBLOCK_ENTITY_SECTION_CHOOSER_FIELD'),
252 ),
253 'LIST_MODE' => array(
254 'data_type' => 'enum',
255 'values' => array(self::LIST_MODE_COMBINED, self::LIST_MODE_SEPARATE),
256 'default_value' => self::LIST_MODE_COMBINED,
257 'title' => Loc::getMessage('IBLOCK_ENTITY_LIST_MODE_FIELD'),
258 ),
259 'RIGHTS_MODE' => array(
260 'data_type' => 'enum',
261 'values' => array(self::RIGHTS_SIMPLE, self::RIGHTS_EXTENDED),
262 'default_value' => self::RIGHTS_SIMPLE,
263 'title' => Loc::getMessage('IBLOCK_ENTITY_RIGHTS_MODE_FIELD'),
264 ),
265 'SECTION_PROPERTY' => array(
266 'data_type' => 'boolean',
267 'values' => array('N','Y'),
268 'title' => Loc::getMessage('IBLOCK_ENTITY_SECTION_PROPERTY_FIELD'),
269 ),
270 'PROPERTY_INDEX' => array(
271 'data_type' => 'enum',
272 'values' => array(self::PROPERTY_INDEX_DISABLE, self::PROPERTY_INDEX_ENABLE, self::PROPERTY_INDEX_INVALID),
273 'default' => self::PROPERTY_INDEX_DISABLE,
274 'title' => Loc::getMessage('IBLOCK_ENTITY_PROPERTY_INDEX_FIELD'),
275 ),
276 'VERSION' => array(
277 'data_type' => 'enum',
278 'values' => array(self::PROPERTY_STORAGE_COMMON, self::PROPERTY_STORAGE_SEPARATE),
279 'default_value' => self::PROPERTY_STORAGE_COMMON,
280 'title' => Loc::getMessage('IBLOCK_ENTITY_VERSION_FIELD'),
281 ),
282 'LAST_CONV_ELEMENT' => array(
283 'data_type' => 'integer',
284 'title' => Loc::getMessage('IBLOCK_ENTITY_LAST_CONV_ELEMENT_FIELD'),
285 ),
286 'SOCNET_GROUP_ID' => array(
287 'data_type' => 'integer',
288 'title' => Loc::getMessage('IBLOCK_ENTITY_SOCNET_GROUP_ID_FIELD'),
289 ),
290 'EDIT_FILE_BEFORE' => array(
291 'data_type' => 'string',
292 'title' => Loc::getMessage('IBLOCK_ENTITY_EDIT_FILE_BEFORE_FIELD'),
293 'validation' => array(__CLASS__, 'validateEditFileBefore'),
294 ),
295 'EDIT_FILE_AFTER' => array(
296 'data_type' => 'string',
297 'title' => Loc::getMessage('IBLOCK_ENTITY_EDIT_FILE_AFTER_FIELD'),
298 'validation' => array(__CLASS__, 'validateEditFileAfter'),
299 ),
300 'TYPE' => array(
301 'data_type' => 'Bitrix\Iblock\Type',
302 'reference' => array('=this.IBLOCK_TYPE_ID' => 'ref.ID'),
303 ),
304
305 new OneToMany('PROPERTIES', PropertyTable::class, 'IBLOCK')
306 );
307 }
308
317 public static function compileEntity($iblockApiCode)
318 {
319 // get iblock
320 if ($iblockApiCode instanceof Iblock)
321 {
322 $iblock = $iblockApiCode;
323 $iblock->fillApiCode();
324 }
325 else
326 {
327 $iblock = IblockTable::getList([
328 'select' => ['ID', 'API_CODE'],
329 'filter' => Query::filter()->where('API_CODE', $iblockApiCode)
330 ])->fetchObject();
331 }
332
333 if (!$iblock || empty($iblock->getApiCode()))
334 {
335 return false;
336 }
337
338 // class info
339 $iblockNamespace = static::DATA_CLASS_NAMESPACE;
340 $iblockDataClassName = $iblock->getEntityDataClassName();
341
342 if ($iblockDataClassName == '')
343 {
344 return false;
345 }
346
347 // check if already compiled
348 $iblockDataClass = $iblock->getEntityDataClass();
349
350 if (class_exists($iblockDataClass, false))
351 {
352 return $iblockDataClass::getEntity();
353 }
354
355 // fill some necessary info
356 $iblock->fill(['VERSION', 'PROPERTIES']);
357
358 // iblock personal entity
359 $parentDataClass = $iblock->getVersion() == 1
360 ? ElementV1Table::class
361 : ElementV2Table::class;
362
364 $elementEntity = \Bitrix\Main\ORM\Entity::compileEntity(
365 $iblockDataClassName,
366 [],
367 [
368 'namespace' => $iblockNamespace,
369 'parent' => $parentDataClass,
370 ]
371 );
372
373 // set iblock to the entity
374 $elementEntity->setIblock($iblock);
375
376 // set relation with sections
377 SectionElementTable::getEntity()->addField(
378 (new Reference('REGULAR_ELEMENT_'.$iblock->getId(), $elementEntity,
379 Join::on('this.IBLOCK_ELEMENT_ID', 'ref.ID')->whereNull('this.ADDITIONAL_PROPERTY_ID')))
380 );
381
382 $elementEntity->addField((new ManyToMany('SECTIONS', SectionTable::class))
383 ->configureMediatorEntity(SectionElementTable::class)
384 ->configureLocalReference('REGULAR_ELEMENT_'.$iblock->getId())
385 ->configureRemoteReference('IBLOCK_SECTION')
386 );
387
388 //$baseTypeList = \Bitrix\Iblock\Helpers\Admin\Property::getBaseTypeList(true);
389 $userTypeList = CIBlockProperty::GetUserType();
390
391 // get properties
392 foreach ($iblock->getProperties() as $property)
393 {
394 if (empty($property->getCode()))
395 {
396 continue;
397 }
398
399 // build property entity with base fields
400 $propertyValueEntity = $property->getValueEntity($elementEntity);
401
402 // add custom fields
403 if (!empty($property->getUserType()) && !empty($userTypeList[$property->getUserType()]['GetORMFields']))
404 {
405 call_user_func($userTypeList[$property->getUserType()]['GetORMFields'], $propertyValueEntity, $property);
406 }
407
408 // add relations with property entity
409 $niceColumnName = $property->getCode();
410
411 if ($property->getMultiple())
412 {
413 // classic OneToMany
414 $elementRefName = 'SRC_ELEMENT';
415
416 // ref from prop entity to src element
417 $propertyValueEntity->addField(
418 (new Reference(
419 $elementRefName,
420 $elementEntity,
421 Join::on('this.IBLOCK_ELEMENT_ID', 'ref.ID')
422 ->where('this.IBLOCK_PROPERTY_ID', $property->getId())
423 ))
424 ->configureJoinType(Join::TYPE_INNER)
425
426 );
427
428 // OneToMany from src element to prop entity
429 $elementEntity->addField(
430 (new PropertyOneToMany($niceColumnName, $propertyValueEntity, $elementRefName))
431 ->configureJoinType(Join::TYPE_LEFT)
433 ->configureCascadeDeletePolicy(
435 )
436 );
437 }
438 else
439 {
440 // classic ref
441 $joinType = Join::TYPE_INNER;
442 $joinFilter = Join::on('this.ID', 'ref.IBLOCK_ELEMENT_ID');
443
444 if ($iblock->getVersion() == 1)
445 {
446 // additional clause for shared value table in v1.0
447 $joinType = Join::TYPE_LEFT;
448 $joinFilter->where('ref.IBLOCK_PROPERTY_ID', $property->getId());
449 }
450
451 // ref from src element to prop entity
452 $elementEntity->addField(
453 (new PropertyReference($niceColumnName, $propertyValueEntity, $joinFilter))
454 ->configureJoinType($joinType)
456 ->configureCascadeDeletePolicy(
457 CascadePolicy::NO_ACTION // will be removed together in onAfterDelete
458 )
459 );
460 }
461 }
462
463 return $elementEntity;
464 }
465
471 public static function compileAllEntities()
472 {
473 foreach (static::getList(['select' => ['ID', 'API_CODE']])->fetchCollection() as $iblock)
474 {
475 static::compileEntity($iblock);
476 }
477 }
478
484 public static function validateIblockTypeId()
485 {
486 return array(
487 new Entity\Validator\Length(null, 50),
488 );
489 }
490
496 public static function validateLid()
497 {
498 return array(
499 new Entity\Validator\Length(null, 2),
500 );
501 }
502
508 public static function validateCode()
509 {
510 return array(
511 new Entity\Validator\Length(null, 50),
512 );
513 }
514
520 public static function validateName()
521 {
522 return array(
523 new Entity\Validator\Length(null, 255),
524 );
525 }
526
532 public static function validateListPageUrl()
533 {
534 return array(
535 new Entity\Validator\Length(null, 255),
536 );
537 }
538
544 public static function validateDetailPageUrl()
545 {
546 return array(
547 new Entity\Validator\Length(null, 255),
548 );
549 }
550
556 public static function validateSectionPageUrl()
557 {
558 return array(
559 new Entity\Validator\Length(null, 255),
560 );
561 }
562
568 public static function validateCanonicalPageUrl()
569 {
570 return array(
571 new Entity\Validator\Length(null, 255),
572 );
573 }
574
580 public static function validateXmlId()
581 {
582 return array(
583 new Entity\Validator\Length(null, 255),
584 );
585 }
586
592 public static function validateTmpId()
593 {
594 return array(
595 new Entity\Validator\Length(null, 40),
596 );
597 }
598
604 public static function validateEditFileBefore()
605 {
606 return array(
607 new Entity\Validator\Length(null, 255),
608 );
609 }
610
616 public static function validateEditFileAfter()
617 {
618 return array(
619 new Entity\Validator\Length(null, 255),
620 );
621 }
622
629 public static function onAfterAdd(Event $event)
630 {
631 $primary = $event->getParameter('primary');
632 \CIBlock::CleanCache($primary['ID']);
633 }
634
641 public static function onAfterUpdate(Event $event)
642 {
643 $primary = $event->getParameter('primary');
644 \CIBlock::CleanCache($primary['ID']);
645 }
646
653 public static function onAfterDelete(Event $event)
654 {
655 $primary = $event->getParameter('primary');
656 \CIBlock::CleanCache($primary['ID']);
657 }
658}
static onAfterUpdate(Event $event)
static onAfterAdd(Event $event)
static onAfterDelete(Event $event)
getParameter($key)
Definition event.php:80
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
configureIblockElementProperty($property)