Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove a number of unused methods from Helper.
These methods were only ever used by FormHelper in the core. They are
complex and not useful not that FormHelper does not persist state.
I'm not aware of many userland Helpers that used this functionality
either, which makes their removal more straight-forward.
  • Loading branch information
markstory committed Mar 5, 2014
1 parent 4ce49fc commit 71c26e9
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 718 deletions.
287 changes: 0 additions & 287 deletions src/View/Helper.php
Expand Up @@ -112,41 +112,6 @@ class Helper extends Object implements EventListener {
*/
protected $_View;

/**
* A list of strings that should be treated as suffixes, or
* sub inputs for a parent input. This is used for date/time
* inputs primarily.
*
* @var array
*/
protected $_fieldSuffixes = array(
'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
);

/**
* The name of the current model entities are in scope of.
*
* @see Helper::setEntity()
* @var string
*/
protected $_modelScope;

/**
* The name of the current model association entities are in scope of.
*
* @see Helper::setEntity()
* @var string
*/
protected $_association;

/**
* The dot separated list of elements the current field entity is for.
*
* @see Helper::setEntity()
* @var string
*/
protected $_entityPath;

/**
* Minimized attributes
*
Expand Down Expand Up @@ -470,229 +435,6 @@ protected function _confirm($message, $okCode, $cancelCode = '', $options = arra
return $confirm;
}

/**
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
*
* @param string $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
* @param boolean $setScope Sets the view scope to the model specified in $tagValue
* @return void
*/
public function setEntity($entity, $setScope = false) {
if ($entity === null) {
$this->_modelScope = false;
}
if ($setScope === true) {
$this->_modelScope = $entity;
}
$parts = array_values(Hash::filter(explode('.', $entity)));
if (empty($parts)) {
return;
}
$count = count($parts);
$lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;

// Either 'body' or 'date.month' type inputs.
if (
($count === 1 && $this->_modelScope && !$setScope) ||
(
$count === 2 &&
in_array($lastPart, $this->_fieldSuffixes) &&
$this->_modelScope &&
$parts[0] !== $this->_modelScope
)
) {
$entity = $this->_modelScope . '.' . $entity;
}

// 0.name, 0.created.month style inputs. Excludes inputs with the modelScope in them.
if (
$count >= 2 &&
is_numeric($parts[0]) &&
!is_numeric($parts[1]) &&
$this->_modelScope &&
strpos($entity, $this->_modelScope) === false
) {
$entity = $this->_modelScope . '.' . $entity;
}

$this->_association = null;

$isHabtm = (
isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
);

// habtm models are special
if ($count === 1 && $isHabtm) {
$this->_association = $parts[0];
$entity = $parts[0] . '.' . $parts[0];
} else {
// check for associated model.
$reversed = array_reverse($parts);
foreach ($reversed as $i => $part) {
if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
$this->_association = $part;
break;
}
}
}
$this->_entityPath = $entity;
}

/**
* Returns the entity reference of the current context as an array of identity parts
*
* @return array An array containing the identity elements of an entity
*/
public function entity() {
return explode('.', $this->_entityPath);
}

/**
* Gets the currently-used model of the rendering context.
*
* @return string
*/
public function model() {
if ($this->_association) {
return $this->_association;
}
return $this->_modelScope;
}

/**
* Gets the currently-used model field of the rendering context.
* Strips off field suffixes such as year, month, day, hour, min, meridian
* when the current entity is longer than 2 elements.
*
* @return string
*/
public function field() {
$entity = $this->entity();
$count = count($entity);
$last = $entity[$count - 1];
if ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
$last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
}
return $last;
}

/**
* Gets the input field name for the current tag. Creates input name attributes
* using CakePHP's `Model[field]` formatting.
*
* @param array|string $options If an array, should be an array of attributes that $key needs to be added to.
* If a string or null, will be used as the View entity.
* @param string $field
* @param string $key The name of the attribute to be set, defaults to 'name'
* @return mixed If an array was given for $options, an array with $key set will be returned.
* If a string was supplied a string will be returned.
*/
protected function _name($options = array(), $field = null, $key = 'name') {
if ($options === null) {
$options = array();
} elseif (is_string($options)) {
$field = $options;
$options = 0;
}

if (!empty($field)) {
$this->setEntity($field);
}

if (is_array($options) && array_key_exists($key, $options)) {
return $options;
}

switch ($field) {
case '_method':
$name = $field;
break;
default:
$entity = $this->entity();
$first = array_shift($entity);
$name = $first . ($entity ? '[' . implode('][', $entity) . ']' : '');
break;
}

if (is_array($options)) {
$options[$key] = $name;
return $options;
}
return $name;
}

/**
* Gets the data for the current tag
*
* @param array|string $options If an array, should be an array of attributes that $key needs to be added to.
* If a string or null, will be used as the View entity.
* @param string $field
* @param string $key The name of the attribute to be set, defaults to 'value'
* @return mixed If an array was given for $options, an array with $key set will be returned.
* If a string was supplied a string will be returned.
*/
public function value($options = array(), $field = null, $key = 'value') {
if ($options === null) {
$options = array();
} elseif (is_string($options)) {
$field = $options;
$options = 0;
}

if (is_array($options) && isset($options[$key])) {
return $options;
}

if (!empty($field)) {
$this->setEntity($field);
}
$result = null;
$data = $this->request->data;

$entity = $this->entity();
if (!empty($data) && is_array($data) && !empty($entity)) {
$result = Hash::get($data, implode('.', $entity));
}

$habtmKey = $this->field();
if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
$result = $data[$habtmKey][$habtmKey];
}

if (is_array($options)) {
if ($result === null && isset($options['default'])) {
$result = $options['default'];
}
unset($options['default']);
}

if (is_array($options)) {
$options[$key] = $result;
return $options;
}
return $result;
}

/**
* Sets the defaults for an input tag. Will set the
* name, value, and id attributes for an array of html attributes.
*
* @param string $field The field name to initialize.
* @param array $options Array of options to use while initializing an input field.
* @return array Array options for the form input.
*/
protected function _initInputField($field, $options = array()) {
if ($field !== null) {
$this->setEntity($field);
}
$options = (array)$options;
$options = $this->_name($options);
$options = $this->value($options);
$options = $this->domId($options);
return $options;
}

/**
* Adds the given class to the element options
*
Expand Down Expand Up @@ -739,33 +481,4 @@ public function implementedEvents() {
return $events;
}

/**
* Transforms a recordset from a hasAndBelongsToMany association to a list of selected
* options for a multiple select element
*
* @param string|array $data
* @param string $key
* @return array
*/
protected function _selectedArray($data, $key = 'id') {
if (!is_array($data)) {
$model = $data;
if (!empty($this->request->data[$model][$model])) {
return $this->request->data[$model][$model];
}
if (!empty($this->request->data[$model])) {
$data = $this->request->data[$model];
}
}
$array = array();
if (!empty($data)) {
foreach ($data as $row) {
if (isset($row[$key])) {
$array[$row[$key]] = $row[$key];
}
}
}
return empty($array) ? null : $array;
}

}

0 comments on commit 71c26e9

Please sign in to comment.