function setModel($model, $actual_fields = undefined) { parent::setModel($model, $actual_fields); //$this->model->addHook('afterLoad',array($this,'afterLoad')); $this->renderJs(); $this->onSubmit(array($this, 'checkForm')); return $this->model; }
public function setModel($model, $actual_fields = UNDEFINED) { $to_field_assotiations = []; // get field list form model if no fields provided if (!$actual_fields) { $actual_fields = $model->getActualFields(); } // to type drop down if ($actual_fields && ($key = array_search('to_type', $actual_fields)) !== false) { $this->to_type_field = $this->addToTypeField(); $to_field_assotiations['to_type'] = $model->getElement('to_type'); /*if (!$_POST['ajax_submit']=='form_submit')*/ unset($actual_fields[$key]); } // to autocomplete if ($actual_fields && ($key = array_search('to_id', $actual_fields)) !== false) { $this->to_id_field = $this->addToField(); $to_field_assotiations['to_id'] = $model->getElement('to_id'); unset($actual_fields[$key]); } // add reload if ($this->to_type_field && $this->to_id_field) { $this->addReloadOnChange($this->to_type_field, $this->to_id_field); } // from type drop down if ($actual_fields && ($key = array_search('from_type', $actual_fields)) !== false) { $this->from_type_field = $this->addFromTypeField(); $to_field_assotiations['from_type'] = $model->getElement('from_type'); unset($actual_fields[$key]); } // from autocomplete if ($actual_fields && ($key = array_search('from_id', $actual_fields)) !== false) { $this->from_id_field = $this->addFromField(); $to_field_assotiations['from_id'] = $model->getElement('from_id'); unset($actual_fields[$key]); } // add reload if ($this->from_type_field && $this->from_id_field) { $this->addReloadOnChange($this->from_type_field, $this->from_id_field); } // it is time to set model to parent parent::setModel($model, $actual_fields); // hook wich will do actual work with model data and custom added fields // this hook will work both on add and edit $this->model->addHook('afterLoad', array($this, 'setValues')); // there is no model hooks if form are in add mode but we still have to do something if ($this->crud && $this->crud->virtual_page->isActive() == 'add') { $this->setValues(); } foreach ($to_field_assotiations as $ff => $mf) { $this->controller->field_associations[$ff] = $mf; } // return model (required if form are in CRUD) return $this->model; }
/** * Configures necessary components when CRUD is in the editing mode. * * @param array $fields List of fields for add form * * @return void|Model If model, then bail out, no greed needed */ protected function configureEdit($fields = null) { // We are actually in the frame! if ($this->isEditing('edit')) { $m = $this->form->setModel($this->model, $fields); $m->load($this->id); $this->form->addSubmit(); $this->form->onSubmit(array($this, 'formSubmit')); return $m; } elseif ($this->isEditing()) { return; } $this->virtual_page->addColumn('edit', 'Editing ' . $this->entity_name, array('descr' => 'Edit', 'icon' => 'pencil'), $this->grid); }
/** * Returns the form that this controller uses to manipulate the data stored * in its model. As stated earlier the form is either automatically generated * or it is loaded from an existing file which is located in the same * directory as the model and bears the model's name. * * @return Form */ protected function getForm() { // Load a local form if it exists. if ($this->redirected) { $formName = $this->redirectedPackageName . Application::camelize($this->mainRedirectedPackage) . "Form"; $formPath = $this->redirectPath . "/" . str_replace(".", "/", $this->mainRedirectedPackage) . "/" . $formName . ".php"; } else { $formName = Application::camelize($this->model->package) . "Form"; $formPath = $this->localPath . "/" . $formName . ".php"; } if (is_file($formPath)) { include_once $formPath; $form = new $formName(); } else { if (is_file($this->localPath . "/" . $this->name . "Form.php")) { include_once $this->localPath . "/" . $this->name . "Form.php"; $formclass = $this->name . "Form"; $form = new $formclass(); $form->setModel($this->model); } else { // Generate a form automatically $fieldNames = array(); $fields = $this->model->getFields(); $form = new Form(); $form->setModel($this->model); $names = array_keys($fields); for ($i = 0; $i < count($fields); $i++) { $field = $fields[$names[$i]]; if ($field['key'] == 'primary') { continue; } if ($fieldNames[$i]["renderer"] == "") { if ($field["reference"] == "") { switch ($field["type"]) { case "boolean": $element = new Checkbox($field["label"], $field["name"], $field["description"], 1); break; case "enum": $element = new SelectionList($field["label"], $field["name"]); foreach ($field["options"] as $value => $option) { $element->addOption($option, $value . ""); } break; case "date": case "datetime": $element = new DateField($field["label"], $field["name"]); break; case "integer": case "double": $element = new TextField($field["label"], $field["name"], $field["description"]); $element->setAsNumeric(); break; case "textarea": $element = new TextArea($field["label"], $field["name"], $field["description"]); break; default: $element = new TextField($field["label"], $field["name"], $field["description"]); break; } } else { $element = new ModelField($field["reference"], $field["referenceValue"]); } foreach ($field["validators"] as $validator) { switch ($validator["type"]) { case "required": $element->setRequired(true); break; case "unique": $element->setUnique(true); break; case "regexp": $element->setRegexp((string) $validator["parameter"]); break; } } } else { $renderer = (string) $fieldNames[$i]["renderer"]; $element = new $renderer(); } $form->add($element); } $form->addAttribute("style", "width:50%"); $form->useAjax(true, false); } } return $form; }