/** * 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; }
/** * Logic for editing an entry. * @param string $modelTableName * @param string $entityPrimaryKey */ protected function editEntry($modelTableName, $entityPrimaryKey) { $this->viewData->set('ModelTableName', $modelTableName); $modelTable = ModelTable::get($modelTableName); $entity = $modelTable->loadByPrimaryKey($entityPrimaryKey); if ($entity != null) { // sets the redirect back url // if somebody clicks on new in a foreign table and submit the form he gets redirect back to this entry $redirectBackUrl = '~' . \Pvik\Core\Config::$config['PvikAdminTools']['Url'] . 'tables/' . strtolower($modelTableName) . ':edit:' . $entity->getPrimaryKey() . '/'; $fields = array(); // data send if ($this->request->isPOST('submit')) { $validationState = new ValidationState(); $fields = array(); 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, $entity, $this->request, $validationState); /* @var $field PvikAdminToolsBaseField */ array_push($fields, $field); $validationState = $field->validation(); } $this->viewData->set('ValidationState', $validationState); if ($validationState->isValid()) { // update all fields foreach ($fields as $field) { /* @var $field PvikAdminToolsBaseField */ $field->update(); } $entity->update(); } $redirectBackUrlAsParameter = $this->request->getGET('redirect-back-url'); if ($redirectBackUrlAsParameter != 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($redirectBackUrlAsParameter)); } else { $singleHtml = new \PvikAdminTools\Library\SingleHtml($entity, $validationState, $this->request); $singleHtml->setForeignTableButtonRedirectBackUrl($redirectBackUrl); $this->viewData->set('SingleHtml', $singleHtml); $this->executeViewByAction('EditEntry'); } } else { $validationState = new ValidationState(); $singleHtml = new \PvikAdminTools\Library\SingleHtml($entity, $validationState, $this->request); $singleHtml->setForeignTableButtonRedirectBackUrl($redirectBackUrl); $this->viewData->set('SingleHtml', $singleHtml); $this->executeViewByAction('EditEntry'); } } else { $this->redirectToTables(); } }
/** * Updates a model field */ public function update() { $fieldName = $this->fieldName; if ($this->configurationHelper->isDisabled($this->fieldName)) { if (!$this->configurationHelper->hasValueField($this->fieldName, 'Preset') && !$this->configurationHelper->isNullable($this->fieldName)) { throw new \Exception('PvikAdminTools: Field ' . $this->fieldName . ' is not nullable and is disabled but does not have a "Preset" value.'); } $this->entity->{$fieldName} = $this->configurationHelper->getValue($this->fieldName, 'Preset'); } else { $this->entity->{$fieldName} = $this->getPOST(); } }
/** * Adds a table row to the html. * @param Entity $entity */ protected function addHtmlTableRow(Entity $entity) { $this->html .= '<tr>'; $modelTable = $entity->getModelTable(); foreach ($this->configurationHelper->getFieldList() as $fieldName) { if ($this->configurationHelper->showInOverView($fieldName) && !in_array($fieldName, $this->hiddenFields)) { $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, $entity, $this->request, new ValidationState()); /* @var $field PvikAdminToolsBaseField */ $this->html .= '<td>' . $field->htmlOverview() . '</td>'; } } if ($this->configurationHelper->hasForeignTables()) { foreach ($this->configurationHelper->getForeignTables() as $foreignTableName => $foreignTable) { if (isset($foreignTable['ShowCountInOverview']) && $foreignTable['ShowCountInOverview'] == true) { // much faster than accessing view $entity->$foreignTableName->count // this would load the entire entries $this->html .= '<td>' . count($entity->getKeys(lcfirst($foreignTableName))) . '</td>'; } } } // add options $this->html .= '<td class="options">['; $editButtonUrl = \Pvik\Core\Path::relativePath('~' . \Pvik\Core\Config::$config['PvikAdminTools']['Url'] . 'tables/' . strtolower($modelTable->getModelTableName()) . ':edit:' . $entity->getPrimaryKey() . '/'); if ($this->buttonRedirectBack != null) { $editButtonUrl .= '?redirect-back-url=' . urlencode($this->buttonRedirectBack); } $this->html .= '<a href="' . $editButtonUrl . '">edit</a>|'; $deleteButtonUrl = \Pvik\Core\Path::relativePath('~' . \Pvik\Core\Config::$config['PvikAdminTools']['Url'] . 'tables/' . strtolower($modelTable->getModelTableName()) . ':delete:' . $entity->getPrimaryKey() . '/'); if ($this->buttonRedirectBack != null) { $deleteButtonUrl .= '?redirect-back-url=' . urlencode($this->buttonRedirectBack); } $this->html .= '<a href="' . $deleteButtonUrl . '" onclick="return confirm(\'Do you really want to delete this entry?\')">delete</a>'; $this->html .= ']</td>'; $this->html .= '</tr>'; }