adding fix to FormHelper::create() for plugin path with action, see #2342. Adding default sizes, maxlengths, and values to input. FormHelper::inputs() will now return all the fields in the model without the need to pass anything from the controller, uses a blacklist so some fields can be skipped. Added PaginatorHelper::numbers(), will output current page numbers, uses a modulus to decide how many to show on each side of the current page. Paginator::options() now allows setting default paging params

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4762 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-04-04 20:38:26 +00:00
parent da35cf3193
commit 06d6bbdbae
2 changed files with 153 additions and 19 deletions

View file

@ -108,9 +108,12 @@ class FormHelper extends AppHelper {
$fields = $object->loadInfo();
$data = array(
'fields' => array_combine($fields->extract('{n}.name'), $fields->extract('{n}.type')),
'sizes' => array_combine($fields->extract('{n}.name'), $fields->extract('{n}.length')),
'values' => array_combine($fields->extract('{n}.name'), $fields->extract('{n}.default')),
'key' => $object->primaryKey,
'validates' => (ife(empty($object->validate), array(), array_keys($object->validate)))
);
$this->fieldset = $data;
}
if (isset($this->data[$model]) && isset($this->data[$model][$data['key']]) && !empty($this->data[$model][$data['key']])) {
@ -128,7 +131,9 @@ class FormHelper extends AppHelper {
if (empty($options['url']) || is_array($options['url'])) {
$options = (array)$options;
if (!empty($model) && $model != $defaultModel) {
if(!empty($this->plugin)) {
$controller = $this->plugin;
} elseif (!empty($model) && $model != $defaultModel) {
$controller = Inflector::underscore(Inflector::pluralize($model));
} else {
$controller = Inflector::underscore($this->params['controller']);
@ -270,12 +275,20 @@ class FormHelper extends AppHelper {
* replaces generateFields
*
* @access public
* @param array $fields works well with Controller::generateFieldNames();
* @param array $fields works well with Controller::generateFields() or on its own;
* @param array $blacklist a simple array of fields to skip
* @return output
*/
function inputs($fields) {
function inputs($fields = null, $blacklist = null) {
if(!is_array($fields)) {
$fields = array_keys($this->fieldset['fields']);
}
$out = null;
foreach($fields as $name => $options) {
if(is_array($blacklist) && in_array($name, $blacklist)) {
break;
}
if (is_numeric($name) && !is_array($options)) {
$name = $options;
$options = array();
@ -310,7 +323,6 @@ class FormHelper extends AppHelper {
$view =& ClassRegistry::getObject('view');
$varName = Inflector::variable(Inflector::pluralize(preg_replace('/_id$/', '', $this->field())));
$varOptions = $view->getVar($varName);
if (is_array($varOptions)) {
$options['type'] = 'select';
$options['options'] = $varOptions;
@ -324,9 +336,16 @@ class FormHelper extends AppHelper {
$options['type'] = 'select';
} elseif (in_array($this->field(), array('passwd', 'password'))) {
$options['type'] = 'password';
} elseif (ClassRegistry::isKeySet($this->model())) {
} else if(isset($this->fieldset['fields'][$this->field()])) {
$type = $this->fieldset['fields'][$this->field()];
$primaryKey = $this->fieldset['key'];
} else if (ClassRegistry::isKeySet($this->model())) {
$model =& ClassRegistry::getObject($this->model());
$type = $model->getColumnType($this->field());
$pimaryKey = $model->primaryKey;
}
if(isset($type)) {
$map = array(
'string' => 'text', 'datetime' => 'datetime',
'boolean' => 'checkbox', 'timestamp' => 'datetime',
@ -336,28 +355,50 @@ class FormHelper extends AppHelper {
if (isset($map[$type])) {
$options['type'] = $map[$type];
}
if($this->field() == $model->primaryKey) {
if($this->field() == $primaryKey) {
$options['type'] = 'hidden';
}
}
}
if(!array_key_exists('size', $options)) {
if(isset($this->fieldset['sizes'][$this->field()])) {
$options['size'] = $this->fieldset['sizes'][$this->field()];
}
}
if(!array_key_exists('maxlength', $options)) {
if(isset($this->fieldset['sizes'][$this->field()])) {
$options['maxlength'] = $this->fieldset['sizes'][$this->field()];
}
}
if(!array_key_exists('value', $options)) {
if(isset($this->fieldset['values'][$this->field()])) {
$options['value'] = $this->fieldset['values'][$this->field()];
}
}
$out = '';
$div = true;
if (isset($options['div'])) {
if (array_key_exists('div', $options)) {
$div = $options['div'];
unset($options['div']);
}
$divOptions = array();
if ($div === true) {
$divOptions['class'] = 'input';
} elseif ($div === false) {
unset($divOptions);
} elseif (is_string($div)) {
$divOptions['class'] = $div;
} elseif (is_array($div)) {
$divOptions = am(array('class' => 'input'), $div);
if(!empty($div)) {
$divOptions = array();
if ( !in_array($this->field(), $this->fieldset['validates'])) {
$divOptions['class'] = 'input';
} elseif (in_array($this->field(), $this->fieldset['validates'])) {
$divOptions['class'] = 'required';
}
if (is_string($div)) {
$divOptions['class'] = $div;
} elseif (is_array($div)) {
$divOptions = am($divOptions, $div);
}
}
$label = null;

View file

@ -93,6 +93,23 @@ class PaginatorHelper extends AppHelper {
if (is_string($options)) {
$options = array('update' => $options);
}
if(!empty($options['paging'])) {
if(!isset($this->params['paging'])) {
$this->params['paging'] = array();
}
$this->params['paging'] = am($this->params['paging'], $options['paging']);
unset($options['paging']);
}
$model = $this->defaultModel();
if(!empty($options[$model])) {
if(!isset($this->params['paging'][$model])) {
$this->params['paging'][$model] = array();
}
$this->params['paging'][$model] = am($this->params['paging'][$model], $options[$model]);
unset($options[$model]);
}
$this->options = array_filter(am($this->options, $options));
}
/**
@ -390,6 +407,82 @@ class PaginatorHelper extends AppHelper {
}
return $this->output($out);
}
}
/**
* Returns a set of numbers for the paged result set
* uses a modulus to decide how many numbers to show on each side of the current page (defautl: 8)
*
* @param mixed $options Options for the counter string. See #options for list of keys.
* @return string numbers string.
*/
function numbers($options = array()) {
$options = am(
array(
'before'=> null,
'after'=> null,
'model' => $this->defaultModel(),
'modulus' => '8',
'separator' => ' | '
),
$options);
$params = $this->params($options['model']);
unset($options['model']);
if($params['pageCount'] <= 1) {
return false;
}
$before = $options['before'];
unset($options['before']);
$after = $options['after'];
unset($options['after']);
$modulus = $options['modulus'];
unset($options['modulus']);
$separator = $options['separator'];
unset($options['separator']);
$out = $before;
if($modulus && $params['pageCount'] > $modulus) {
$half = intval($modulus / 2);
$end = $params['page'] + $half;
if($end > $params['pageCount']) {
$end = $params['pageCount'];
}
$start = $params['page'] - ($modulus - ($end - $params['page']));
if($start <= 1) {
$start = 1;
$end = $params['page'] + ($modulus - $params['page']) + 1;
}
for ($i = $start; $i < $params['page']; $i++) {
$out .= $this->link($i, am($options, array('page' => $i)));
$out .= $separator;
}
?>
$out .= $params['page'];
$out .= $separator;
$start = $params['page'] + 1;
for ($i = $start; $i <= $end; $i++) {
$out .= $this->link($i, am($options, array('page' => $i)));
$out .= $separator;
}
} else {
for ($i = 1; $i <= $params['pageCount']; $i++) {
if($i == $params['page']) {
$out .= $i;
} else {
$out .= $this->link($i, am($options, array('page' => $i)));
}
$out .= $separator;
}
}
$out .= $after;
return $this->output($out);
}
}
?>