mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding optgroup support for 2D arrays in FormHelper::select(); adding grouping ability to Model::generateList()
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3326 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
ab419f3fe1
commit
a75f33f430
3 changed files with 29 additions and 4 deletions
|
@ -70,6 +70,10 @@ selectoption = "<option value="%s" %s>%s</option>"
|
|||
; Tag template for a closing select tag.
|
||||
selectend = "</select>"
|
||||
|
||||
; Tag template for a option group tag.
|
||||
optiongroup = "<optgroup label="%s"%s>"
|
||||
optiongroupend = "</optgroup>"
|
||||
|
||||
; Tag template for a password tag.
|
||||
password = "<input type="password" name="data[%s][%s]" %s/>"
|
||||
|
||||
|
|
|
@ -1506,12 +1506,13 @@ class Model extends Overloadable {
|
|||
* @param int $limit SQL LIMIT clause, for calculating items per page
|
||||
* @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
|
||||
* @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
|
||||
* @param string $groupPath A string path to a value to group the elements by, i.e. "{n}.Post.category_id"
|
||||
* @return array An associative array of records, where the id is the key, and the display field is the value
|
||||
*/
|
||||
function generateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null) {
|
||||
function generateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $groupPath = null) {
|
||||
$db =& ConnectionManager::getDataSource($this->useDbConfig);
|
||||
|
||||
if ($keyPath == null && $valuePath == null && $this->hasField($this->displayField)) {
|
||||
if ($keyPath == null && $valuePath == null && $groupPath == null && $this->hasField($this->displayField)) {
|
||||
$fields = array($this->primaryKey, $this->displayField);
|
||||
} else {
|
||||
$fields = null;
|
||||
|
@ -1531,9 +1532,26 @@ class Model extends Overloadable {
|
|||
$vals = $db->getFieldValue($result, $valuePath);
|
||||
|
||||
if (!empty($keys) && !empty($vals)) {
|
||||
$out = array();
|
||||
|
||||
if ($groupPath != null) {
|
||||
$group = $db->getFieldValue($result, $groupPath);
|
||||
if (!empty($group)) {
|
||||
$c = count($keys);
|
||||
for ($i = 0; $i < $c; $i++) {
|
||||
if (!isset($out[$group[$i]])) {
|
||||
$out[$group[$i]] = array();
|
||||
}
|
||||
$out[$group[$i]][$keys[$i]] = $vals[$i];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
$return = array_combine($keys, $vals);
|
||||
return $return;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Escapes the field name and prepends the model name. Escaping will be done according to the current database driver's rules.
|
||||
|
|
|
@ -240,10 +240,13 @@ class FormHelper extends Helper {
|
|||
|
||||
foreach($elements as $name => $title) {
|
||||
$htmlOptions = array();
|
||||
|
||||
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
|
||||
$select[] = sprintf($this->tags['optiongroup'], $name, '');
|
||||
$select = am($select, $this->selectOptions($title, $selected));
|
||||
$select[] = $this->tags['optiongroupend'];
|
||||
$name = null;
|
||||
} elseif (is_array($title)) {
|
||||
$select = am($select, $this->selectOptions());
|
||||
$htmlOptions = $title;
|
||||
$name = $title['value'];
|
||||
$title = $title['name'];
|
||||
|
|
Loading…
Add table
Reference in a new issue