diff --git a/app/views/layouts/default.thtml b/app/views/layouts/default.thtml index ffe562245..32b3e5e8b 100644 --- a/app/views/layouts/default.thtml +++ b/app/views/layouts/default.thtml @@ -10,6 +10,10 @@
+ +

+ +

diff --git a/config/tags.ini.php b/config/tags.ini.php index ba5e491a3..f94c1a50b 100644 --- a/config/tags.ini.php +++ b/config/tags.ini.php @@ -62,6 +62,9 @@ radio = "" ; Tag template for a select opening tag. selectStart = "" + ; Tag template for an empty select option tag. selectEmpty = "" diff --git a/libs/controller.php b/libs/controller.php index 1598e7b93..362a02916 100644 --- a/libs/controller.php +++ b/libs/controller.php @@ -695,7 +695,7 @@ class Controller extends Object $fieldNames[$tableName]['model'] = $tableName; $fieldNames[$tableName]['prompt'] = "Related ".Inflector::humanize($tableName); $fieldNames[$tableName]['type'] = "selectMultiple"; - $fieldNames[$tableName]['tagName'] = $tableName; + $fieldNames[$tableName]['tagName'] = $otherModelName.'/'.$tableName; foreach( $otherModel->findAll() as $pass ) { diff --git a/libs/helpers/html.php b/libs/helpers/html.php index 481a7f111..656fba22a 100644 --- a/libs/helpers/html.php +++ b/libs/helpers/html.php @@ -995,13 +995,19 @@ class HtmlHelper extends Helper if (!is_array($option_elements) || !count($option_elements)) return null; - $select[] = sprintf($this->tags['selectstart'], $this->model, $this->field, $this->parseHtmlOptions($select_attr)); + if( isset($select_attr) && array_key_exists( "multiple", $select_attr) ) + { + $select[] = sprintf($this->tags['selectmultiplestart'], $this->model, $this->field, $this->parseHtmlOptions($select_attr)); + } + else + { + $select[] = sprintf($this->tags['selectstart'], $this->model, $this->field, $this->parseHtmlOptions($select_attr)); + } $select[] = sprintf($this->tags['selectempty'], $this->parseHtmlOptions($optionAttr)); foreach ($option_elements as $name=>$title) { $options_here = $optionAttr; - if ($selected == $name) { $options_here['selected'] = 'selected'; diff --git a/libs/model.php b/libs/model.php index 1daf6f29e..cdf8e5b5e 100644 --- a/libs/model.php +++ b/libs/model.php @@ -804,7 +804,7 @@ class Model extends Object */ function setId ($id) { - $this->id = $id; + $this->id[0] = $id; if(!empty($this->_belongsToOther)) { @@ -866,9 +866,14 @@ class Model extends Object function read ($fields=null) { $this->validationErrors = null; - //return $this->id? $this->find("id = '{$this->id}'", $fields): false; + if(is_array($this->id)) + { return $this->id? $this->find("$this->table.id = '{$this->id[0]}'", $fields): false; - + } + else + { + return $this->id? $this->find("id = '{$this->id}'", $fields): false; + } } /** @@ -936,20 +941,45 @@ class Model extends Object } $fields = $values = array(); - + + if(count($this->data) > 1) + { + $weHaveMulti = true; + $count = 0; + $joined = false; + } + else + { + $weHaveMulti = false; + } + + foreach ($this->data as $n=>$v) { + if(isset($weHaveMulti) && $count > 0) + { + $joined = array($v); + } + else + { foreach ($v as $x => $y) { - - if ($this->hasField($x)) - { - $fields[] = $x; - $values[] = $this->db->prepare($y); - } - } + if ($this->hasField($x)) + { + $fields[] = $x; + $values[] = $this->db->prepare($y); + if($x == 'id' && !is_numeric($y)) + { + $newID = $y; + } + } + } + + $count++; + } } + if (empty($this->id) && $this->hasField('created') && !in_array('created', $fields)) { $fields[] = 'created'; @@ -976,9 +1006,13 @@ class Model extends Object } $sql = "UPDATE {$this->table} SET ".join(',', $sql)." WHERE id = '{$this->id}'"; - - if ($this->db->query($sql) && $this->db->lastAffected()) + + if ($this->db->query($sql)) // && $this->db->lastAffected()) { + if(!empty($joined)) + { + $this->saveMulti($joined, $this->id); + } $this->data = false; return true; } @@ -989,6 +1023,7 @@ class Model extends Object } else { + $fields = join(',', $fields); $values = join(',', $values); @@ -997,6 +1032,14 @@ class Model extends Object if($this->db->query($sql)) { $this->id = $this->db->lastInsertId($this->table, 'id'); + if(!empty($joined)) + { + if(!$this->id > 0) + { + $this->id = $newID; + } + $this->saveMulti($joined, $this->id); + } return true; } else @@ -1012,6 +1055,49 @@ class Model extends Object } +/** + * Saves model hasAndBelongsToMany data to the database. + * + * @param array $joined Data to save. + * @param string $id + * @return boolean success + */ + function saveMulti ($joined, $id) + { + $sql = array(); + + foreach ($joined as $x => $y) + { + foreach ($y as $name => $value) + { + $tableSort[0] = $this->table; + $tableSort[1] = $name; + sort($tableSort); + $joinTable = $tableSort[0] . '_' . $tableSort[1]; + $key1 = Inflector::singularize($this->table) . '_id'; + $key2 = Inflector::singularize($name) . '_id'; + foreach ($value as $update) + { + $fields1[] = $key1; + $values1[] = $this->db->prepare($id); + $fields1[] = $key2; + $values1[] = $this->db->prepare($update); + + $fields1 = join(',', $fields1); + $values1 = join(',', $values1); + $joinedSql[] = "INSERT INTO {$joinTable} ({$fields1}) VALUES ({$values1})"; + + unset($fields1); + unset($values1); + } + $this->db->query("DELETE FROM {$joinTable} WHERE $key1 = '{$id}'"); + } + foreach ($joinedSql as $insert){ + $this->db->query($insert); + } + } + } + /** * Synonym for del(). * diff --git a/public/css/forms.css b/public/css/forms.css index dba3867cb..314f8cabd 100644 --- a/public/css/forms.css +++ b/public/css/forms.css @@ -325,4 +325,8 @@ form div.wide small { div.date select { width:auto; +} + +option { +padding-left:1em; } \ No newline at end of file diff --git a/public/img/pbcake.gif b/public/img/pbcake.gif new file mode 100644 index 000000000..7739878e0 Binary files /dev/null and b/public/img/pbcake.gif differ