Esempio n. 1
0
 public function __construct($model)
 {
     $this->renderWithType = 'select';
     $model = \ntentan\models\Model::load($model);
     $entity = Ntentan::singular($model->getName());
     $this->setLabel(Ntentan::toSentence($entity));
     $this->setName("{$entity}_id");
     $options = $model->getAll();
     foreach ($options as $option) {
         $this->option((string) $option, $option->id);
     }
 }
Esempio n. 2
0
 public function __construct($label, $model, $value = null, $extraConditions = array())
 {
     parent::__construct();
     $this->label = $label;
     $modelInstance = Model::load($model);
     if ($value === null) {
         $data = $modelInstance->get('all', count($extraConditions) > 0 ? array('conditions' => $extraConditions) : null);
     } else {
         $data = $modelInstance->get('all', array('fields' => array('id', $value), 'conditions' => count($extraConditions) > 0 ? $extraConditions : null));
     }
     $this->setName(Ntentan::singular($model) . "_id");
     for ($i = 0; $i < $data->count(); $i++) {
         $this->addOption($value == null ? $data[$i] : $data[$i][$value], $data[$i]["id"]);
     }
 }
Esempio n. 3
0
 public function __construct($model, $submodel)
 {
     $this->renderWithType = 'select';
     $model = \ntentan\models\Model::load($model);
     $submodel = \ntentan\models\Model::load($submodel);
     $entity = Ntentan::singular($submodel->getName());
     $parentEntity = Ntentan::singular($model->getName());
     $this->setLabel(Ntentan::toSentence($entity));
     $this->setName("{$entity}_id");
     $parentId = "{$parentEntity}_id";
     $options = $model->getAll();
     foreach ($options as $option) {
         $suboptions = $submodel->getAll(array('conditions' => array($parentId => $option->id)));
         foreach ($suboptions as $suboption) {
             $this->option("{$option} / {$suboption}", $suboption->id);
         }
     }
 }
Esempio n. 4
0
 public function describe()
 {
     if (!Cache::exists("model_" . $this->route)) {
         $description = $this->dataStore->describe();
         if (is_array($this->mustBeUnique)) {
             foreach ($description["fields"] as $i => $field) {
                 $uniqueField = false;
                 foreach ($this->mustBeUnique as $unique) {
                     if (is_array($unique)) {
                         if (isset($unique['field'])) {
                             if ($field["name"] == $unique["field"]) {
                                 $uniqueField = true;
                                 $uniqueMessage = $unique["message"];
                             }
                         } else {
                             throw new exceptions\DescriptionException("A mustBeUnique constraint specified as an array must always contain a field property");
                         }
                     } else {
                         if ($field["name"] == $unique) {
                             $uniqueField = true;
                             $uniqueMessage = null;
                         }
                     }
                 }
                 if ($uniqueField) {
                     $description["fields"][$i]["unique"] = true;
                     if ($uniqueMessage != null) {
                         $description["fields"][$i]["unique_violation_message"] = $uniqueMessage;
                     }
                 }
             }
         }
         if (is_array($this->belongsTo)) {
             foreach ($this->belongsTo as $belongsTo) {
                 $belongsToModel = is_array($belongsTo) ? $belongsTo[0] : $belongsTo;
                 $description["belongs_to"][] = $belongsToModel;
                 $alias = null;
                 if (is_array($belongsTo)) {
                     $fieldName = $belongsTo["as"];
                     $alias = $belongsTo["as"];
                 } else {
                     $alias = strtolower(Ntentan::singular($this->getBelongsTo($belongsTo)));
                     $fieldName = $alias . "_id";
                 }
                 foreach ($description["fields"] as $i => $field) {
                     if ($field["name"] == $fieldName) {
                         $description["fields"][$i]["model"] = Ntentan::plural($belongsToModel);
                         $description["fields"][$i]["foreign_key"] = true;
                         $description["fields"][$i]["field_name"] = $fieldName;
                         if ($alias != '') {
                             $description["fields"][$i]["alias"] = $alias;
                         }
                     }
                 }
             }
         } else {
             if ($this->belongsTo != null) {
                 $description["belongs_to"][] = $this->belongsTo;
                 $fieldName = strtolower(Ntentan::singular($this->belongsTo)) . "_id";
                 foreach ($description["fields"] as $i => $field) {
                     if ($field["name"] == $fieldName) {
                         $description["fields"][$i]["model"] = $this->belongsTo;
                         $description["fields"][$i]["foreign_key"] = true;
                     }
                 }
             }
         }
         Cache::add("model_" . $this->route, $description);
     }
     return Cache::get("model_" . $this->route);
 }
Esempio n. 5
0
 public function addSection($section)
 {
     if (is_string($section)) {
         $array = explode('.', str_replace('/', '.', $section));
         $newSection = array('route' => $section, 'label' => \ucwords(str_replace(array('/', '_'), array(' ', ' '), $section)), 'model' => str_replace('/', '.', $section), 'entity' => Ntentan::singular(str_replace('_', ' ', end($array))));
         $section = $newSection;
     }
     $this->sections[$section['route']] = $section;
     return $this;
 }
Esempio n. 6
0
 /**
  * (non-PHPdoc)
  * @see models/datastores/ntentan\models\datastores.SqlDatabase::describeSchema()
  */
 public function describeModel()
 {
     $description = array();
     $description["tables"] = array();
     $schema = $this->schema;
     $tables = $this->query(sprintf("SELECT table_name \n                 FROM information_schema.tables \n                 WHERE table_schema = '%s'", $schema));
     foreach ($tables as $table) {
         $description["tables"][$table["table_name"]] = array();
         $description["tables"][$table["table_name"]]["belongs_to"] = array();
         $description["tables"][$table["table_name"]]["has_many"] = array();
         $description["tables"][$table["table_name"]]["has_a"] = array();
         $tableDescription = $this->describeTable($table['table_name'], $this->schema);
         // Get the schemas which belong to
         $belongsToTables = $this->query(sprintf("select referenced_table_name, column_name\n                    from information_schema.table_constraints \n                    join information_schema.key_column_usage using(constraint_name) \n                    where \n                        table_constraints.table_schema = '%s' and \n                        constraint_type = 'FOREIGN KEY' and \n                        table_constraints.table_name = '%s' and\n                        referenced_column_name = 'id'", $schema, $table["table_name"]));
         foreach ($belongsToTables as $belongsToTable) {
             $singular = Ntentan::singular($belongsToTable["referenced_table_name"]);
             if (array_search($singular . '_id', \array_keys($tableDescription)) !== false) {
                 $description["tables"][$table["table_name"]]["belongs_to"][] = $singular;
             } else {
                 $description["tables"][$table["table_name"]]["belongs_to"][] = array($singular, 'as' => $belongsToTable['column_name']);
             }
         }
         // Get the schemas which this one owns.
         $hasManyTables = $this->query(sprintf("select table_constraints.table_name\n                    from information_schema.table_constraints \n                    join information_schema.key_column_usage using(constraint_name) \n                    where \n                        table_constraints.table_schema = '%s' and \n                        constraint_type = 'FOREIGN KEY' and \n                        referenced_table_name = '%s' and\n                        referenced_column_name = 'id'", $schema, $table["table_name"]));
         //@todo Change nomenulature to support has_a both here and in pgsql
         foreach ($hasManyTables as $hasManyTable) {
             /*$unique = $this->query("select column_name from
                    information_schema.table_constraints pk
                    join information_schema.key_column_usage c on
                       c.table_name = pk.table_name and
                       c.constraint_name = pk.constraint_name and
                       c.table_schema = pk.table_schema
                    where pk.table_name = '{$table['table_name']}' and pk.table_schema='{$schema}' and column_name = '{$singular}_id'
                    and constraint_type = 'UNIQUE'"
               );
               if(count($unique) > 0)
               {
                   $description["tables"][$table["table_name"]]["has_a"][] = 
                       $hasManyTable["table_name"];
               }
               else
               {*/
             $description["tables"][$table["table_name"]]["has_many"][] = $hasManyTable["table_name"];
             //}
         }
     }
     return $description;
 }
Esempio n. 7
0
<?php

$modelSingular = strtolower(\ntentan\Ntentan::singular($entity));
if ($headings) {
    ?>
    <?php 
    if ($entity != '') {
        ?>
        <h<?php 
        echo $heading_level;
        ?>
><?php 
        echo $entity;
        ?>
</h<?php 
        echo $heading_level;
        ?>
>
    <?php 
    }
}
?>
        
<div id="item-actions-menu">
    <?php 
echo $this->widgets->menu(array(array('label' => "Add new {$modelSingular}", 'url' => u("{$route}/add"))))->alias('actions');
?>
</div>
<?php 
if ($notifications & is_numeric($notification_type)) {
    ?>
Esempio n. 8
0
 /**
  * (non-PHPdoc)
  * @see models/datastores/ntentan\models\datastores.SqlDatabase::describeSchema()
  */
 public function describeModel()
 {
     $description = array();
     $description["tables"] = array();
     $tables = $this->query(sprintf("SELECT table_name\n                 FROM information_schema.tables\n                 WHERE table_schema = '%s'", $this->schema));
     foreach ($tables as $table) {
         $description["tables"][$table["table_name"]] = array();
         $description["tables"][$table["table_name"]]["belongs_to"] = array();
         $description["tables"][$table["table_name"]]["has_many"] = array();
         $description["tables"][$table["table_name"]]["has_a"] = array();
         $tableDescription = $this->describeTable($table['table_name'], $this->schema);
         // Get the schemas which belong to this schema
         $belongsToTables = $this->query(sprintf("select constraint_column_usage.table_name, column_name\n                    from information_schema.table_constraints\n                    join information_schema.constraint_column_usage using(constraint_name)\n                    where\n                        table_constraints.table_schema = '%s' and\n                        constraint_type = 'FOREIGN KEY' and\n                        table_constraints.table_name = '%s' and\n                        constraint_column_usage.column_name = 'id'", $this->schema, $table["table_name"]));
         foreach ($belongsToTables as $belongsToTable) {
             $singular = Ntentan::singular($belongsToTable["table_name"]);
             if (array_search($singular . '_id', \array_keys($tableDescription)) !== false) {
                 $description["tables"][$table["table_name"]]["belongs_to"][] = $singular;
             } else {
                 $description["tables"][$table["table_name"]]["belongs_to"][] = array($singular, 'as' => $belongsToTable['column_name']);
             }
         }
         // Get the schemas which this one owns.
         $hasManyTables = $this->query(sprintf("select table_constraints.table_name\n                    from information_schema.table_constraints\n                    join information_schema.constraint_column_usage using(constraint_name)\n                    where\n                        table_constraints.table_schema = '%s' and\n                        constraint_type = 'FOREIGN KEY' and\n                        constraint_column_usage.table_name = '%s' and\n                        constraint_column_usage.column_name = 'id'", $this->schema, $table["table_name"]));
         foreach ($hasManyTables as $hasManyTable) {
             $description["tables"][$table["table_name"]]["has_many"][] = $hasManyTable["table_name"];
         }
     }
     return $description;
 }
Esempio n. 9
0
 protected function _put($data)
 {
     $fields = array_keys($data);
     $subData = array();
     if ($fields[0] == "0") {
         $fields = array_keys($data[0]);
         $quotedFields = array();
         foreach ($quotedFields as $field) {
             $quotedFields[] = $this->quote($field);
         }
         $query = "INSERT INTO " . ($this->schema != '' ? $this->quotedSchema . "." : '') . "{$this->quotedTable} (" . implode(", ", $quotedFields) . ") VALUES ";
         $baseQueries = array();
         foreach ($data as $row) {
             $values = array();
             foreach ($row as $value) {
                 $values[] = $value === "" || $value === null ? "NULL" : "'" . $this->escape($value) . "'";
             }
             $baseQueries[] = "( " . implode(", ", $values) . " )";
         }
         $query .= implode(",", $baseQueries);
         $this->query($query);
         $id = true;
     } else {
         $dataFields = array();
         $quotedDataFields = array();
         foreach ($data as $field => $value) {
             if (is_array($value)) {
                 $subData[$field] = $value;
             } else {
                 $values[] = $value === "" || $value === null ? "NULL" : "'" . $this->escape($value) . "'";
                 $dataFields[] = $field;
                 $quotedDataFields[] = $this->quote($field);
             }
         }
         $query = "INSERT INTO " . ($this->schema != '' ? $this->quotedSchema . "." : '') . "{$this->quotedTable} (" . implode(", ", $quotedDataFields) . ") VALUES (" . implode(", ", $values) . ")";
         $this->query($query);
         if (array_search('id', $dataFields) === false) {
             $id = $this->getLastInsertId();
         } else {
             $id = $data['id'];
         }
         foreach ($subData as $modelName => $data) {
             $model = Model::load($modelName);
             $table = $model->dataStore->table;
             $fields = array_keys($data[0]);
             $fields[] = Ntentan::singular($this->model->name) . "_id";
             $query = "INSERT INTO {$table} (" . implode(", ", $fields) . ") VALUES ";
             $dataQueries = array();
             foreach ($data as $newEntry) {
                 $values = array();
                 foreach ($newEntry as $value) {
                     $values[] = $value = "" ? "NULL" : "'" . $this->escape($value) . "'";
                 }
                 $values[] = $id;
                 $dataQueries[] = "(" . implode(", ", $values) . ")";
             }
             $query .= implode(", ", $dataQueries);
             $this->query($query);
         }
     }
     return $id;
 }