/** * Calculates the dimension of the diagram object * @return null */ private function calculateDimension() { $maxLength = strlen($this->meta->getName()); $fields = $this->meta->getFields(); foreach ($fields as $fieldName => $field) { $fieldNameLength = strlen($fieldName . $this->getFieldType($field)) + 4; if ($fieldNameLength > $maxLength) { $maxLength = $fieldNameLength; } } $width = $maxLength * self::CHARACTER_WIDTH + $this->paddingLeft + $this->paddingRight; $height = (count($fields) + 2) * ($this->paddingTop + self::CHARACTER_HEIGHT) + $this->paddingTop + $this->paddingBottom; $this->dimension = new Dimension($width, $height); }
/** * Load the needed validators for this model to this object * @param zibo\library\orm\model\meta\ModelMeta $meta the meta of the model * @return null */ private function initializeValidators(ModelMeta $meta) { $validationFactory = ValidationFactory::getInstance(); $fields = $meta->getFields(); foreach ($fields as $fieldName => $field) { $fieldValidators = $field->getValidators(); if (!$fieldValidators) { continue; } $this->validators[$fieldName] = array(); foreach ($fieldValidators as $fieldValidator) { $validator = $validationFactory->createValidator($fieldValidator->getName(), $fieldValidator->getOptions()); $this->validators[$fieldName][] = $validator; } } }
/** * Create a data container for the model * @param zibo\library\orm\model\meta\ModelMeta $meta the meta of the model * @param zibo\library\ObjectFactory $objectFactory factory for new objects * @param boolean $initialize set to true to get an object with default values, false to get an empty object * @return mixed a data container for the model */ private function initData(ModelMeta $meta, ObjectFactory $objectFactory, $initialize) { $data = $objectFactory->create($meta->getDataClassName()); if (!$initialize) { return $data; } $fields = $meta->getFields(); foreach ($fields as $field) { $name = $field->getName(); if ($field instanceof BelongsToField || $field instanceof HasOneField) { $data->{$name} = null; continue; } if ($field instanceof HasManyField) { $data->{$name} = array(); continue; } $data->{$name} = $field->getDefaultValue(); } return $data; }
/** * Gets the model fields to use in this form. * @param zibo\library\orm\model\meta\ModelMeta $meta Meta of the model * @param array $fields Array with the names of the fields which need to be added to the form. If not provided, all the fields of the model will be returned * @param boolean $skipFields Set to true to skip the provided fields instead of adding them * @return array Array with the name of the field as key and a ModelField object as value */ protected function getModelFields(ModelMeta $meta, array $fieldNames = null, $skipFields = false) { if (!$fieldNames) { $fields = $meta->getFields(); } elseif ($skipFields) { $fields = $meta->getFields(); foreach ($fieldNames as $fieldName) { if ($fieldName == ModelTable::PRIMARY_KEY) { continue; } if (array_key_exists($fieldName, $fields)) { unset($fields[$fieldName]); } } } else { $fields = array(); $fields[ModelTable::PRIMARY_KEY] = $meta->getField(ModelTable::PRIMARY_KEY); foreach ($fieldNames as $fieldName) { $fields[$fieldName] = $meta->getField($fieldName); } } $this->dataFieldLabels = array(); foreach ($fields as $fieldName => $field) { $label = $field->getLabel(); if ($label) { $this->dataFieldLabels[$fieldName] = $label; } } return $fields; }
/** * Sets the localize subview to the view if necessairy * @param zibo\library\orm\model\meta\ModelMeta $meta Meta of the model * @param mixed $data Data object of the form * @return null */ protected function setLocalizeSubview(ModelMeta $meta, $data, $action = null) { if (!$this->isLocalized || !$data || !$data->id) { return; } $localizedFields = array(); $fields = $meta->getFields(); foreach ($fields as $fieldName => $field) { if (!$field->isLocalized()) { continue; } $localizedFields[$fieldName] = $fieldName; } $this->set('localizedFields', $localizedFields); $form = $this->get('form'); $this->addJavascript(self::SCRIPT_LOCALIZE); $this->addInlineJavascript("ZiboOrmInitializeLocalizedForm('" . $form->getId() . "');"); $localizeView = new LocalizeView($meta, $data, $action); $this->setSubView('localize', $localizeView); }
/** * Gets a unique link model name for the link model of the provided field * @param zibo\library\orm\model\meta\ModelMeta $modelMeta Meta of the model of the field * @param string $fieldName Name of the field for the link model * @param string $linkModelName Name of the link model * @return string Unique name for the link model */ private function getUniqueLinkModelName(ModelMeta $modelMeta, $fieldName, $linkModelName) { $index = 2; $fields = $modelMeta->getFields(); $isLinkModelNameValid = false; $tmpLinkModelName = $linkModelName; do { $isLinkModelNameValid = true; foreach ($fields as $name => $field) { if ($fieldName == $name || !$field instanceof HasField) { continue; } if ($field->getLinkModelName() == $tmpLinkModelName) { $isLinkModelNameValid = false; break; } } if ($isLinkModelNameValid) { break; } $tmpLinkModelName = $linkModelName . $index; $index++; } while ($isLinkModelNameValid == false); return $tmpLinkModelName; }