public static function fillFilterReferenceColumn(&$filterElement, Entity\ReferenceField $field) { if ($field->GetDataType() == 'Bitrix\\Main\\User') { // USER if ($filterElement['value']) { $res = CUser::GetByID($filterElement['value']); $user = $res->fetch(); if ($user) { $username = CUser::FormatName(CSite::GetNameFormat(false), $user, true); $filterElement['value'] = array('id' => $user['ID'], 'name' => $username); } else { $filterElement['value'] = array('id' => $filterElement['value'], 'name' => GetMessage('REPORT_USER_NOT_FOUND')); } } else { $filterElement['value'] = array('id' => ''); } } else { if ($field->GetDataType() == 'Bitrix\\Socialnetwork\\Workgroup') { // GROUP if ($filterElement['value']) { $group = CSocNetGroup::GetByID($filterElement['value']); if ($group) { $filterElement['value'] = array(array('id' => $group['ID'], 'title' => $group['NAME'])); } else { $filterElement['value'] = array(array('id' => $filterElement['value'], 'title' => GetMessage('REPORT_PROJECT_NOT_FOUND'))); } } else { $filterElement['value'] = array(array('id' => '')); } } } }
public static function fillFilterReferenceColumn(&$filterElement, Entity\ReferenceField $field) { if ($field->getRefEntityName() == '\\Bitrix\\Main\\User') { // USER if ($filterElement['value']) { $res = CUser::GetByID($filterElement['value']); $user = $res->fetch(); if ($user) { $username = CUser::FormatName(CSite::GetNameFormat(null, self::getDefaultSiteId()), $user, true, false); $filterElement['value'] = array('id' => $user['ID'], 'name' => $username); } else { $filterElement['value'] = array('id' => $filterElement['value'], 'name' => GetMessage('REPORT_USER_NOT_FOUND')); } } else { $filterElement['value'] = array('id' => ''); } } else { if ($field->getRefEntityName() == '\\Bitrix\\Main\\Group') { // GROUP if ($filterElement['value']) { $res = CGroup::GetByID($filterElement['value']); $group = $res->fetch(); if ($group) { $filterElement['value'] = array('id' => $group['ID'], 'name' => $group['NAME']); } else { $filterElement['value'] = array('id' => $filterElement['value'], 'name' => GetMessage('SALE_REPORT_GROUP_NOT_FOUND')); } } else { $filterElement['value'] = array('id' => ''); } } } }
/** * @param Field $field * * @return bool */ protected function appendField(Field $field) { if (isset($this->fields[$field->getName()]) && !$this->isClone) { trigger_error(sprintf('Entity `%s` already has Field with name `%s`.', $this->getFullName(), $field->getName()), E_USER_WARNING); return false; } if ($field instanceof ReferenceField) { // references cache $this->references[$field->getRefEntityName()][] = $field; } $this->fields[$field->getName()] = $field; if ($field instanceof ScalarField && $field->isPrimary()) { $this->primary[] = $field->getName(); if ($field->isAutocomplete()) { $this->autoIncrement = $field->getName(); } } // add reference field for UField iblock_section if ($field instanceof UField && $field->getTypeId() == 'iblock_section') { $refFieldName = $field->getName() . '_BY'; if ($field->isMultiple()) { $localFieldName = $field->getValueFieldName(); } else { $localFieldName = $field->getName(); } $newFieldInfo = array('data_type' => 'Bitrix\\Iblock\\Section', 'reference' => array($localFieldName, 'ID')); $newRefField = new ReferenceField($refFieldName, $newFieldInfo['data_type'], $newFieldInfo['reference'][0], $newFieldInfo['reference'][1]); $newRefField->setEntity($this); $this->fields[$refFieldName] = $newRefField; } return true; }
/** * Парсинг условий связи между моделями. * * Ничего сложного нет, просто определяются соответствия полей основной модели и модели из связи. Например: * * `FilmLinksTable::FILM_ID => FilmTable::ID (ref.FILM_ID => this.ID)` * * Или, например: * * `MediaTable::TYPE => 'FILM' (ref.TYPE => new DB\SqlExpression('?s', 'FILM'))` * * @param Entity\ReferenceField $reference Данные поля из getMap(). * * @return array Условия связи преобразованные в массив вида $conditions[$refField]['thisField' => $thisField, * 'customValue' => $customValue]. * $customValue - это результат парсинга SqlExpression. * Если шаблон SqlExpression не равен %s, то условие исключается из результата. */ protected function getReferenceConditions(Entity\ReferenceField $reference) { $conditionsFields = array(); foreach ($reference->getReference() as $leftCondition => $rightCondition) { $thisField = null; $refField = null; $customValue = null; // Поиск this.... в левом условии $thisFieldMatch = array(); $refFieldMatch = array(); if (preg_match('/=this\\.([A-z]+)/', $leftCondition, $thisFieldMatch) == 1) { $thisField = $thisFieldMatch[1]; } else { if (preg_match('/ref\\.([A-z]+)/', $leftCondition, $refFieldMatch) == 1) { $refField = $refFieldMatch[1]; } } // Поиск expression value... в правом условии $refFieldMatch = array(); if ($rightCondition instanceof \Bitrix\Main\DB\SqlExpression) { $customValueDirty = $rightCondition->compile(); $customValue = preg_replace('/^([\'"])(.+)\\1$/', '$2', $customValueDirty); if ($customValueDirty == $customValue) { // Если значение выражения не обрамлено кавычками, значит оно не нужно нам $customValue = null; } } else { if (preg_match('/ref\\.([A-z]+)/', $rightCondition, $refFieldMatch) > 0) { $refField = $refFieldMatch[1]; } } // Если не указано поле, которое нужно заполнить или не найдено содержимое для него, то исключаем условие if (empty($refField) || empty($thisField) && empty($customValue)) { continue; } else { $conditionsFields[$refField] = array('thisField' => $thisField, 'customValue' => $customValue); } } return $conditionsFields; }