public function __construct($model, $package, $path, $hooksClass) { if (is_file($model)) { $this->prefix = $path; $this->package = $package; $this->xml = simplexml_load_file($model); $this->database = (string) $this->xml["database"]; $this->label = (string) $this->xml["label"]; $this->name = (string) $this->xml["name"]; $this->showInMenu = (string) $this->xml["showInMenu"]; $description = $this->xml->xpath("/model:model/model:description"); $this->connect(); if (count($description) > 0) { $this->description = (string) $description[0]; } // Get a list of all the fields from the model into an array $this->fields = array(); $field_xml = $this->xml->xpath("/model:model/model:fields/model:field"); //[@type!='displayReference']"); $this->explicitRelations = $this->xml->xpath("/model:model/model:explicitRelations/model:model"); foreach ($this->explicitRelations as $key => $explicitRelation) { $this->explicitRelations = (string) $explicitRelation; } $fixedConditions = array(); foreach ($field_xml as $field) { $description = $field->xpath("model:description"); $validatorsXML = $field->xpath("model:validator"); $validators = array(); $optionsXML = $field->xpath("model:options/model:option"); $options = array(); foreach ($validatorsXML as $validator) { $validators[] = array("type" => (string) $validator["type"], "parameter" => (string) $validator); } foreach ($optionsXML as $option) { //$options[] = array("value"=>(string)$option["value"],"label"=>(string)$option); $options[(string) $option["value"]] = (string) $option; } $fieldInfo = array("name" => (string) $field["name"], "type" => (string) $field["type"], "label" => (string) $field["label"], "reference" => (string) $field["reference"], "referenceValue" => $this->datastore->concatenate(explode(",", (string) $field["referenceValue"])), "key" => (string) $field["key"], "description" => isset($description[0]) ? (string) $description[0] : "", "validators" => $validators, "options" => $options); if ($fieldInfo["key"] == "primary") { $this->keyField = $fieldInfo["name"]; } if (isset($field["value"])) { $fieldInfo["value"] = (string) $field["value"]; $fixedConditions[] = "{$this->database}.{$field["name"]} = '{$field["value"]}'"; $this->fixedValues[(string) $field["name"]] = $field["value"]; } $this->fields[(string) $field["name"]] = $fieldInfo; if ($field["type"] != "displayReference") { $this->storedFields[(string) $field["name"]] = $fieldInfo; } } $this->fixedConditions = implode(" AND ", $fixedConditions); } else { throw new Exception("Could not load XML defined model from {$model}!"); } $references = $this->getXpathArray("/model:model/model:fields/model:field/@reference"); $fields = $this->getXpathArray("/model:model/model:fields/model:field[@reference!='']/@name"); $values = $this->getXpathArray("/model:model/model:fields/model:field[@reference!='']/@referenceValue"); $return = array(); for ($i = 0; $i < count($references); $i++) { $reference = array(); $reference["referencing_field"] = $fields[$i]; $reference["reference"] = $references[$i]; $reference["referenced_value_field"] = $this->datastore->concatenate(explode(",", $values[$i])); $fieldInfo = model::resolvePath($reference["reference"]); $tempModel = model::load($fieldInfo["model"], $this->prefix); $table = $tempModel->getDatabase(); $reference["table"] = (string) $table; $reference["referenced_field"] = $fieldInfo["field"]; $return[] = $reference; } $this->referencedFields = $return; $this->datastore->referencedFields = $this->referencedFields; $this->datastore->explicitRelations = $this->explicitRelations; $this->datastore->fields = $this->fields; $this->datastore->database = $this->database; $this->datastore->storedFields = $this->storedFields; $this->datastore->keyField = $this->keyField; $this->hooks = new $hooksClass(); $this->hooks->setModel($this); //parent::__construct(); }
public function __construct($package, $name) { global $redirectedPackage; parent::__construct(); $this->package = $package; $this->name = $name; $this->label = $this->label == "" ? Application::labelize($name) : $this->label; $this->connect(); $this->datastore->database = $this->database; $fields = $this->datastore->describe(); if ($fields == null) { throw new Exception("Database table does not exists"); } $fixedConditions = array(); foreach ($fields as $field) { $name = $field["name"]; $this->fields[$name] = array("name" => $field["name"], "type" => isset($this->types[$name]) ? $this->types[$name] : $field["type"], "label" => Application::labelize($field["name"]), "validators" => array()); if (isset($field["key"])) { $this->fields[$name]["key"] = $field["key"]; $this->keyField = $field["name"]; } if (isset($this->options[$name])) { $this->fields[$name]["type"] = "enum"; $this->fields[$name]["options"] = $this->options[$name]; } if (isset($this->values[$name])) { $this->fields[$name]["value"] = $this->values[$name]; $fixedConditions[] = "{$this->database}.{$name} = '{$this->values[$name]}'"; $this->fixedValues[$name] = $this->values[$name]; } if (isset($this->validators[$name])) { $this->fields[$name]["validators"] = $this->validators[$name]; } if (isset($this->keys[$name])) { // Reset any existing keys foreach ($this->fields as $tempName => $tempField) { if ($tempField["key"] == $this->keys[$name]) { unset($this->fields[$tempName]['key']); } } $this->fields[$name]["key"] = $this->keys[$name]; $this->fields[$name]["validators"] = array(); } if (array_search($name, $this->unique) !== false) { $field['unique'] = true; } if (array_search($name, $this->required) !== false) { $field['required'] = true; } if (isset($this->references[$name])) { $this->fields[$name]["type"] = "reference"; $this->fields[$name]["reference"] = $this->references[$name]["reference"]; $this->fields[$name]["referenceValue"] = $this->references[$name]["referenceValue"]; $fieldInfo = model::resolvePath($this->references[$name]["reference"]); if ($package == $fieldInfo["model"]) { $table = $this->database; } else { $tempModel = Model::load((substr($fieldInfo["model"], 0, 1) == "." ? $redirectedPackage : "") . $fieldInfo["model"], $this->prefix); $table = $tempModel->getDatabase(); } $this->referencedFields[] = array("referencing_field" => $name, "reference" => $this->references[$name]["reference"], "referenced_value_field" => $this->references[$name]["referenceValue"], "table" => (string) $table, "referenced_field" => $fieldInfo["field"]); } if ($field["required"]) { $this->fields[$name]["validators"][] = array("type" => "required", "parameter" => ""); } switch ($field["type"]) { case "integer": case "double": $this->fields[$name]['validators'][] = array("type" => "numeric", "parameter" => ""); break; case "date": $this->fields[$name]['validators'][] = array("type" => "date", "parameter" => ""); break; } if ($field["unique"]) { $this->fields[$name]["validators"][] = array("type" => "unique", "parameter" => ""); } } $this->fixedConditions = implode(" AND ", $fixedConditions); $this->datastore->referencedFields = $this->referencedFields; $this->datastore->explicitRelations = $this->explicitRelations; $this->datastore->fields = $this->fields; $this->datastore->database = $this->database; $this->datastore->storedFields = $this->storedFields; $this->datastore->keyField = $this->keyField; }