/** * * @param string $fieldName * @param Entity $entity * @param ValidationState $validationState */ public function __construct($fieldName, Entity $entity, Request $request, ValidationState $validationState) { $this->fieldName = $fieldName; $this->entity = $entity; $this->modelTable = $this->entity->getModelTable(); $this->request = $request; $this->fieldDefinitionHelper = $this->modelTable->getFieldDefinitionHelper(); $this->configurationHelper = new \PvikAdminTools\Library\ConfigurationHelper(); $this->configurationHelper->setCurrentTable($this->modelTable->getModelTableName()); $this->validationState = $validationState; if ($this->entity->getPrimaryKey() == null || $this->entity->getPrimaryKey() == '') { $this->isNewEntity = true; } else { $this->isNewEntity = false; } $this->preset = ''; }
/** * Returns the html for the field. * @return string */ protected function addHtmlSingleControl() { if ($this->fieldDefinitionHelper->isTypeForeignObject($this->fieldName)) { $modelTableName = $this->fieldDefinitionHelper->getModelTableNameForForeignObject($this->fieldName); $foreignKeyFieldName = $this->fieldDefinitionHelper->getForeignKeyFieldName($this->fieldName); $field = $this->configurationHelper->getField($this->fieldName); $useField = $field['UseField']; $entityArray = ModelTable::get($modelTableName)->loadAll(); $this->html .= '<select class="span8" name="' . strtolower($foreignKeyFieldName) . '">'; if ($this->configurationHelper->isNullable($this->fieldName) || !$this->isNewEntity) { $this->html .= '<option value="">(none)</option>'; } foreach ($entityArray as $entity) { $this->html .= '<option value="' . $entity->getPrimaryKey() . '" ' . $this->getSelectPresetValue($foreignKeyFieldName, $entity->getPrimaryKey()) . '>'; $this->html .= utf8_decode($entity->{$useField}); $this->html .= '</option>'; } $this->html .= '</select>'; } else { $this->html .= 'Error'; } }
/** * Deletes the reference from entities to the object by the current foreign key for a field name * @param \Pvik\Database\ORM\Entity $object * @param string $fieldName */ public function deleteForeignKeyReference(\Pvik\Database\ORM\Entity $object, $fieldName) { $helper = $this->modelTable->getFieldDefinitionHelper(); $foreignModelTable = $helper->getModelTable($fieldName); // get the key that refers to the foreign object (AuthorID from a book) $foreignKey = $object->getFieldData($fieldName); $foreignObject = $foreignModelTable->getCache()->loadByPrimaryKey($foreignKey); // if object exist in cache and needs to be updated if ($foreignObject != null) { // look through foreign model $foreignHelper = $foreignModelTable->getFieldDefinitionHelper(); foreach ($foreignHelper->getManyForeignObjectsFieldList() as $foreignModelTableFieldName) { // searching for a ManyForeignObjects field with ForeignKey reference to this field if ($foreignHelper->getModelTableName($foreignModelTableFieldName) == $this->modelTable->getModelTableName() && $foreignHelper->getForeignKeyFieldName($foreignModelTableFieldName) == $fieldName) { // Author.Books.ForeignKey is AuthorID $oldKeys = $foreignObject->getFieldData($foreignModelTableFieldName); // delete from old keys $foreignObject->setFieldData($foreignModelTableFieldName, str_replace($object->getPrimaryKey(), '', $oldKeys)); break; } } } }
/** * Logic for deleting an entry. * @param type $modelTableName * @param type $entityPrimaryKey */ protected function deleteEntry($modelTableName, $entityPrimaryKey) { $entity = ModelTable::get($modelTableName)->loadByPrimaryKey($entityPrimaryKey); if ($entity != null) { $entity->delete(); } $redirectBackUrl = $this->request->getGET('redirect-back-url'); if ($redirectBackUrl != null) { // the user was was in edit mode of an table entry // clicked on new in a foreign id and and created/updated/deleted a entry // now we redirect back to the edit mode $this->redirectToPath(urldecode($redirectBackUrl)); } else { $url = '~' . \Pvik\Core\Config::$config['PvikAdminTools']['Url'] . 'tables/' . strtolower($modelTableName) . ':list/'; $this->redirectToPath($url); } }
/** * Returns the ModelTable for a ForeignKey, ManyForeignObjects or ForeignObject field. * @param string $fieldName * @return ModelTable */ public function getModelTable($fieldName) { if ($this->isTypeForeignKey($fieldName) || $this->isTypeManyForeignObjects($fieldName)) { $modelTableName = $this->getModelTableName($fieldName); return ModelTable::get($modelTableName); } elseif ($this->isTypeForeignObject($fieldName)) { $foreignKeyFieldName = $this->getForeignKeyFieldName($fieldName); $modelTableName = $this->getModelTableName($foreignKeyFieldName); return ModelTable::get($modelTableName); } else { throw new \Exception('Only the types ForeignKey, ManyForeignObjects, ForeignObject have a ModelTable.'); } }
/** * * @param string $modelTableName */ protected function __construct($modelTableName) { $this->modelTable = \Pvik\Database\ORM\ModelTable::get($modelTableName); }
/** * * @param string $modelTableName */ protected function __construct($modelTableName) { if (!is_string($modelTableName)) { throw new \Exception('ModelTableName must be a string.'); } $this->modelTable = \Pvik\Database\ORM\ModelTable::get($modelTableName); $this->prepare(); }
/** * Returns the html of the entry. * @return string */ public function toHtml() { $this->html = '<form class="form-vertical" method="post">'; foreach ($this->configurationHelper->getFieldList() as $fieldName) { $type = $this->configurationHelper->getFieldType($fieldName); $fieldClassName = '\\PvikAdminTools\\Library\\Fields\\' . $type; if (!class_exists($fieldClassName)) { throw new \Exception('PvikAdminTools: The type ' . $type . ' does not exists. Used for the field ' . $fieldName); } $field = new $fieldClassName($fieldName, $this->entity, $this->request, $this->validationState); /* @var $field \PvikAdminTools\Library\Fields\Base */ if ($field->isVisibleSingle()) { if (isset($this->presetValues[strtolower($fieldName)])) { $field->setPreset($this->presetValues[strtolower($fieldName)]); } $this->html .= $field->htmlSingle(); } } $this->addHtmlSubmit(); $this->html .= '</form>'; if (!$this->isNewEntity() && $this->configurationHelper->hasForeignTables()) { $this->html .= '<div class="foreign-tables-field">'; foreach ($this->configurationHelper->getForeignTables() as $foreignTable => $configuration) { $primaryKey = $this->entity->getPrimaryKey(); $foreignKey = $configuration['ForeignKey']; $modelTable = ModelTable::get($foreignTable); $entityArray = $modelTable->loadAll(); $entityArray = $entityArray->filterEquals($foreignKey, $primaryKey); $tableHtml = new \PvikAdminTools\Library\TableHtml($entityArray, $this->request); // saerch for fields that we don't need to show $foreignObjectFieldNames = array(); $helper = $modelTable->getFieldDefinitionHelper(); foreach ($helper->getFieldList() as $fieldName) { // search for a foreign object that uses that refers to the original model // we don't need to show this table column if ($helper->isTypeForeignObject($fieldName)) { if ($helper->getForeignKeyFieldName($fieldName) == $foreignKey) { array_push($foreignObjectFieldNames, $fieldName); } } } $tableHtml->setHiddenFields($foreignObjectFieldNames); // set preset values $presetValues = array(); foreach ($foreignObjectFieldNames as $foreignObjectFieldName) { $presetValues[$foreignObjectFieldName] = $primaryKey; } $tableHtml->setNewButtonPresetValues($presetValues); if ($this->foreignTableButtonRedirectBackUrl != null) { $tableHtml->setButtonRedirectBack($this->foreignTableButtonRedirectBackUrl); } $this->html .= '<div class="field">'; $this->html .= '<label class="label-field">' . $foreignTable . '</label>'; $this->html .= $tableHtml->toHtml(); $this->html .= '</div>'; } $this->html .= '</div>'; } return $this->html; }