/** * Creates a new ModelField. The example below creates a ModelField which * lists client accounts for selection. * * @code * $clients = new ModelField("brokerage.setup.clients.client_id", "account"); * @endcode * @param $path The full path to the field in the module which is to be returned by this field. * @param $value The name of the field from the model whose value should be displayed in the list. */ public function __construct($path, $value) { global $redirectedPackage; $this->info = Model::resolvePath($path); $this->model = Model::load((substr($this->info["model"], 0, 1) == "." ? $redirectedPackage : "") . $this->info["model"]); $this->valueField = $value; $field = $this->model->getFields(array($value)); $this->setLabel($field[0]["label"]); $this->setDescription($field[0]["description"]); $this->setName($this->info["field"]); $params = array("fields" => array($this->info["field"], $this->valueField), "sort_field" => $this->valueField); if ($this->conditions != '') { $params['conditions'] = $this->conditions; } $data = $this->model->get($params, Model::MODE_ARRAY); $this->addOption("Add new " . Utils::singular($this->model->getEntity()), 'NEW'); foreach ($data as $datum) { if ($datum[1] == "") { $this->addOption($datum[0]); } else { $this->addOption($datum[1], $datum[0]); } } $modelPath = str_replace('.', '/', $this->model->package); $this->addAttribute('onchange', "fapiAddModelItem('{$modelPath}', this, '{$value}')"); }
public function beforeAction(Model $model) { $model->getFields($model); $model->Message = $model->controller->Message; $model->Session = $model->controller->Session; $model->Navigation = $model->controller->Navigation; $model->request = $model->controller->request; $model->controller->set('model', $model->alias); }
/** * Creates a new ModelField. The example below creates a ModelField which * lists client accounts for selection. * * @code * $clients = new ModelField("brokerage.setup.clients.client_id", "account"); * @endcode * @param $path The full path to the field in the module which is to be returned by this field. * @param $value The name of the field from the model whose value should be displayed in the list. */ public function __construct($path, $value) { global $redirectedPackage; $info = Model::resolvePath($path); $this->model = Model::load((substr($info["model"], 0, 1) == "." ? $redirectedPackage : "") . $info["model"]); $this->valueField = $value; $field = $this->model->getFields(array($value)); $this->setLabel($field[0]["label"]); $this->setDescription($field[0]["description"]); $this->setName($info["field"]); $data = $this->model->get(array("fields" => array($info["field"], $value), "sort_field" => $value), Model::MODE_ARRAY); $this->addOption("", ""); foreach ($data as $datum) { if ($datum[1] == "") { $this->addOption($datum[0]); } else { $this->addOption($datum[1], $datum[0]); } } }
public function __construct(Model $model) { parent::__construct(); $fields = $model->getFields(); foreach ($fields as $name => $field) { if ($field['key'] == 'primary') { continue; } if ($field['reference'] == '') { $method = "get{$field['type']}field"; $element = $this->{$method}($field); } else { $element = new ModelField($field["reference"], $field["referenceValue"]); } $element->setRequired($field['required']); $this->add($element); } $this->addAttribute('style', 'width:50%'); }
public function setModel($model) { $this->model = $model; $this->data = $model->getData(); $fields = $this->model->getFields(); }
/** * 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; }
private function createDefaultForm() { // Generate a form automatically $fieldNames = array(); $fields = $this->model->getFields(); $form = new Form(); $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; }
public function getOptionFields(Model $model) { $fields = $model->getFields(); if (array_key_exists('order', $fields)) { $fields['order']['type'] = 'hidden'; $fields['order']['default'] = 0; } if (array_key_exists('visible', $fields)) { $fields['visible']['labelKey'] = 'FieldOption'; $fields['visible']['type'] = 'select'; $fields['visible']['default'] = 1; $fields['visible']['options'] = array(1 => __('Enabled'), 0 => __('Disabled')); } if (array_key_exists('default', $fields)) { $fields['default']['default'] = 0; $fields['default']['type'] = 'select'; $fields['default']['options'] = array(1 => __('Yes'), 0 => __('No')); } if (array_key_exists('editable', $fields)) { $fields['editable']['visible'] = false; } if (array_key_exists('field_option_id', $fields)) { $fields['field_option_id']['type'] = 'hidden'; } if (array_key_exists('international_code', $fields)) { $fields['international_code']['labelKey'] = 'general'; $fields['international_code']['visible'] = array('index' => true, 'view' => true, 'edit' => true); } if (array_key_exists('national_code', $fields)) { $fields['national_code']['labelKey'] = 'general'; $fields['national_code']['visible'] = array('index' => true, 'view' => true, 'edit' => true); } $model->fields = $fields; return $fields; }