/** * @param Entity\Field $entityField * @param Entity\Field|array $fieldAsType */ public static function setMultipleFieldSerialization(Entity\Field $entityField, $fieldAsType) { global $USER_FIELD_MANAGER; if (!$fieldAsType instanceof Entity\Field) { $fieldAsType = $USER_FIELD_MANAGER->getEntityField($fieldAsType); } if ($fieldAsType instanceof Entity\DatetimeField) { $entityField->addSaveDataModifier(array(__CLASS__, 'serializeMultipleDatetime')); $entityField->addFetchDataModifier(array(__CLASS__, 'unserializeMultipleDatetime')); } elseif ($fieldAsType instanceof Entity\DateField) { $entityField->addSaveDataModifier(array(__CLASS__, 'serializeMultipleDate')); $entityField->addFetchDataModifier(array(__CLASS__, 'unserializeMultipleDate')); } else { $entityField->setSerialized(); } }
/** * @param $value * @param $primary * @param array $row * @param Entity\Field | Entity\EnumField | Entity\BooleanField $field * @return bool|string */ public function validate($value, $primary, array $row, Entity\Field $field) { if (in_array($value, $field->getValues(), true) || $value == '') { return true; } return $this->getErrorMessage($value, $field); }
/** * @param $value * @param \Bitrix\Main\Entity\Field $field * @param null|string $errorPhrase * @param null|array $additionalTemplates * * @return string */ protected function getErrorMessage($value, Entity\Field $field, $errorPhrase = null, $additionalTemplates = null) { if ($errorPhrase === null) { $errorPhrase = $this->errorPhrase !== null ? $this->errorPhrase : Loc::getMessage($this->errorPhraseCode); } $langValues = array('#VALUE#' => $value, '#FIELD_NAME#' => $field->getName(), '#FIELD_TITLE#' => $field->getTitle()); if (is_array($additionalTemplates)) { $langValues += $additionalTemplates; } return str_replace(array_keys($langValues), array_values($langValues), $errorPhrase); }
/** * @param $value * @param \Bitrix\Main\Entity\Field $field * @param null $errorPhrase * * @return mixed */ protected function getErrorMessage($value, Entity\Field $field, $errorPhrase = null) { if ($errorPhrase === null) { $errorPhrase = $this->errorPhrase; } $langValues = array('#VALUE#' => $value, '#FIELD_NAME#' => $field->getName(), '#FIELD_TITLE#' => $field->getTitle()); if (HasMessage($errorPhrase)) { return GetMessage($errorPhrase, $langValues); } else { return str_replace(array_keys($langValues), array_values($langValues), $errorPhrase); } }
public function validate($value, $primary, array $row, Entity\Field $field) { $entity = $field->getEntity(); $primaryNames = $entity->getPrimaryArray(); $query = new Entity\Query($entity); $query->setSelect($primaryNames); $query->setFilter(array('=' . $field->getName() => $value)); $query->setLimit(2); $result = $query->exec(); while ($existing = $result->fetch()) { // check primary foreach ($existing as $k => $v) { if (!isset($primary[$k]) || $primary[$k] != $existing[$k]) { return $this->getErrorMessage($value, $field); } } } return true; }
public function __construct($name, $parameters = array()) { parent::__construct($name, $parameters); $this->is_primary = isset($parameters['primary']) && $parameters['primary']; $this->is_unique = isset($parameters['unique']) && $parameters['unique']; $this->is_required = isset($parameters['required']) && $parameters['required']; $this->is_autocomplete = isset($parameters['autocomplete']) && $parameters['autocomplete']; $this->column_name = isset($parameters['column_name']) ? $parameters['column_name'] : $this->name; $this->default_value = isset($parameters['default_value']) ? $parameters['default_value'] : null; }
public function setEntity(Base $entity) { parent::setEntity($entity); $parameters = $this->initialParameters; unset($parameters['expression']); $this->valueField = $this->entity->initializeField($this->name, $parameters); if (!$this->valueField instanceof ScalarField) { throw new SystemException('expression field can only be a scalar type.'); } }
/** * @param string $name * @param string|Base $refEntity * @param array $reference * @param array $parameters * * @throws ArgumentException */ public function __construct($name, $refEntity, $reference, $parameters = array()) { parent::__construct($name); if ($refEntity instanceof Base) { $this->refEntity = $refEntity; $this->refEntityName = $refEntity->getFullName(); } else { // this one could be without leading backslash and/or with Table-postfix $this->refEntityName = Base::normalizeName($refEntity); } if (empty($reference)) { throw new ArgumentException('Reference for `' . $name . '` shouldn\'t be empty'); } $this->reference = $reference; if (isset($parameters['join_type'])) { $join_type = strtoupper($parameters['join_type']); if (in_array($join_type, array('LEFT', 'INNER', 'RIGHT'), true)) { $this->join_type = $join_type; } } }
/** * Adds a runtime field (being created dinamycally, opposite to being described statically in the entity map) * * @param string|null $name * @param array|Field $fieldInfo * * @return Query */ public function registerRuntimeField($name, $fieldInfo) { if ((empty($name) || is_numeric($name)) && $fieldInfo instanceof Field) { $name = $fieldInfo->getName(); } $this->init_entity->addField($fieldInfo, $name); // force chain creation for further needs $chain = $this->getRegisteredChain($name, true); $this->registerChain('runtime', $chain); if ($chain->getLastElement()->getValue() instanceof ExpressionField) { $this->collectExprChains($chain, array('hidden')); } return $this; }
public static function getInstanceByQuery(Query $query, &$entity_name = null) { if ($entity_name === null) { $entity_name = 'Tmp' . randString(); } elseif (!preg_match('/^[a-z0-9_]+$/i', $entity_name)) { throw new Main\ArgumentException(sprintf('Invalid entity name `%s`.', $entity_name)); } $query_string = '(' . $query->getQuery() . ')'; $query_chains = $query->getChains(); $replaced_aliases = array_flip($query->getReplacedAliases()); // generate fieldsMap $fieldsMap = array('TMP_ID' => array('data_type' => 'integer', 'primary' => true)); foreach ($query->getSelect() as $k => $v) { if (is_array($v)) { // expression $fieldsMap[$k] = array('data_type' => $v['data_type']); } else { if ($v instanceof ExpressionField) { $fieldDefinition = $v->getName(); } else { $fieldDefinition = is_numeric($k) ? $v : $k; } // better to initialize fields as objects after entity is created $dataType = Field::getOldDataTypeByField($query_chains[$fieldDefinition]->getLastElement()->getValue()); $fieldsMap[$fieldDefinition] = array('data_type' => $dataType); } if (isset($replaced_aliases[$k])) { $fieldsMap[$k]['column_name'] = $replaced_aliases[$k]; } } // generate class content $eval = 'class ' . $entity_name . 'Table extends ' . __NAMESPACE__ . '\\DataManager {' . PHP_EOL; $eval .= 'public static function getMap() {' . PHP_EOL; $eval .= 'return ' . var_export($fieldsMap, true) . ';' . PHP_EOL; $eval .= '}'; $eval .= 'public static function getTableName() {' . PHP_EOL; $eval .= 'return ' . var_export($query_string, true) . ';' . PHP_EOL; $eval .= '}'; $eval .= '}'; eval($eval); return self::getInstance($entity_name); }
/** * @param array $prop * @param Entity\Field $tableField * @return Entity\ExpressionField */ private static function linkPropertyValue(array $prop, Entity\Field &$tableField) { $propValueShortcut = "PROPERTY_{$prop['CODE']}_VALUE"; $isMultiple = $prop['MULTIPLE'] == 'Y'; $isOldProps = $prop['VERSION'] == 2; $concatSubquery = "GROUP_CONCAT(%s SEPARATOR '" . static::$concatSeparator . "')"; $propValueColumn = $isMultiple || $isOldProps ? 'VALUE' : "PROPERTY_{$prop['ID']}"; $mapEntity = new Entity\ExpressionField($propValueShortcut, $isMultiple ? $concatSubquery : '%s', "{$tableField->getName()}.{$propValueColumn}"); return $mapEntity; }