Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .horde.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ dependencies:
optional:
composer:
horde/test: ^3
keywords: []
vendor: horde
109 changes: 68 additions & 41 deletions lib/Horde/Core/Form.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/**
* Horde_Core_Form Master Class.
*
* The Horde_Core_Form:: package provides form rendering, validation, and other
* functionality for the Horde Application Framework.
*
* Copyright 2001-2007 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2001-2017 Horde LLC (http://www.horde.org/)
* Copyright 2001-2026 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2001-2026 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
Expand All @@ -20,13 +21,13 @@ class Horde_Core_Form
protected $_name = '';
protected $_title = '';
protected $_vars;
protected $_errors = array();
protected $_errors = [];
protected $_submitted = null;
protected $_sections = array();
protected $_sections = [];
protected $_open_section = null;
protected $_currentSection = array();
protected $_variables = array();
protected $_hiddenVariables = array();
protected $_currentSection = [];
protected $_variables = [];
protected $_hiddenVariables = [];
protected $_useFormToken = true;
protected $_autofilled = false;
protected $_help = false;
Expand Down Expand Up @@ -103,19 +104,19 @@ public function useToken($token = null)
*
* @return object Horde_Core_Form_Renderer The form renderer.
*/
function getRenderer($params = array())
public function getRenderer($params = [])
{
return new Horde_Core_Form_Renderer_Xhtml($params);
}

function getType($type, $params = array())
public function getType($type, $params = [])
{
$type_class = 'Horde_Core_Form_Type_' . $type;
if (!class_exists($type_class)) {
throw new Horde_Exception(sprintf('Nonexistant class "%s" for field type "%s"', $type_class, $type));
}
$type_ob = new $type_class();
call_user_func_array(array($type_ob, 'init'), $params);
call_user_func_array([$type_ob, 'init'], $params);
return $type_ob;
}

Expand Down Expand Up @@ -170,39 +171,63 @@ public function getSectionExpandedState($section, $boolean = false)
}
}

public function add($varName, $type, $humanName, $required, $readonly = false, $description = null, $params = array())
public function add($varName, $type, $humanName, $required, $readonly = false, $description = null, $params = [])
{
return $this->addVariable($humanName, $varName, $type, $required, $readonly, $description, $params);
}

/**
* TODO
*/
public function addVariable($humanName, $varName, $type, $required,
$readonly = false, $description = null,
$params = array())
{
return $this->insertVariableBefore(null, $humanName, $varName, $type,
$required, $readonly, $description,
$params);
public function addVariable(
$humanName,
$varName,
$type,
$required,
$readonly = false,
$description = null,
$params = []
) {
return $this->insertVariableBefore(
null,
$humanName,
$varName,
$type,
$required,
$readonly,
$description,
$params
);
}

/**
* TODO
*/
public function insertVariableBefore($before, $humanName, $varName, $type,
$required, $readonly = false,
$description = null, $params = array())
{
public function insertVariableBefore(
$before,
$humanName,
$varName,
$type,
$required,
$readonly = false,
$description = null,
$params = []
) {
$type = $this->getType($type, $params);
$var = new Horde_Core_Form_Variable($humanName, $varName, $type,
$required, $readonly, $description);
$var = new Horde_Core_Form_Variable(
$humanName,
$varName,
$type,
$required,
$readonly,
$description
);

/* Set the form object reference in the var. */
$var->setFormOb($this);

if ($var->getType() instanceof Horde_Core_Form_Type_Enum &&
count($var->getValues()) == 1) {
if ($var->getType() instanceof Horde_Core_Form_Type_Enum
&& count($var->getValues()) == 1) {
$vals = array_keys($var->getValues());
$this->_vars->add($var->varName, $vals[0]);
$var->_autofilled = true;
Expand All @@ -215,17 +240,18 @@ public function insertVariableBefore($before, $humanName, $varName, $type,
$this->_variables[$this->_currentSection][] = &$var;
} else {
$num = 0;
while (isset($this->_variables[$this->_currentSection][$num]) &&
$this->_variables[$this->_currentSection][$num]->getVarName() != $before) {
while (isset($this->_variables[$this->_currentSection][$num])
&& $this->_variables[$this->_currentSection][$num]->getVarName() != $before) {
$num++;
}
if (!isset($this->_variables[$this->_currentSection][$num])) {
$this->_variables[$this->_currentSection][] = &$var;
} else {
$this->_variables[$this->_currentSection] = array_merge(
array_slice($this->_variables[$this->_currentSection], 0, $num),
array(&$var),
array_slice($this->_variables[$this->_currentSection], $num));
[&$var],
array_slice($this->_variables[$this->_currentSection], $num)
);
}
}

Expand All @@ -251,12 +277,13 @@ public function removeVariable($var)
{
foreach (array_keys($this->_variables) as $section) {
foreach (array_keys($this->_variables[$section]) as $i) {
if ((is_a($var, 'Horde_Core_Form_Variable') && $this->_variables[$section][$i] === $var) ||
($this->_variables[$section][$i]->getVarName() == $var)) {
if ((is_a($var, 'Horde_Core_Form_Variable') && $this->_variables[$section][$i] === $var)
|| ($this->_variables[$section][$i]->getVarName() == $var)) {
// Slice out the variable to be removed.
$this->_variables[$this->_currentSection] = array_merge(
array_slice($this->_variables[$this->_currentSection], 0, $i),
array_slice($this->_variables[$this->_currentSection], $i + 1));
array_slice($this->_variables[$this->_currentSection], $i + 1)
);

return true;
}
Expand All @@ -269,7 +296,7 @@ public function removeVariable($var)
/**
* TODO
*/
public function addHidden($varName, $type, $required, $params = array())
public function addHidden($varName, $type, $required, $params = [])
{
$type = $this->getType($type, $params);
$var = new Horde_Core_Form_Variable('', $varName, $type, $required);
Expand All @@ -281,7 +308,7 @@ public function addHidden($varName, $type, $required, $params = array())
public function getVariables($flat = true, $withHidden = false)
{
if ($flat) {
$vars = array();
$vars = [];
foreach ($this->_variables as $section) {
foreach ($section as $var) {
$vars[] = $var;
Expand Down Expand Up @@ -403,7 +430,7 @@ public function validate($canAutoFill = false)

public function clearValidation()
{
$this->_errors = array();
$this->_errors = [];
}

public function getError($var)
Expand All @@ -413,7 +440,7 @@ public function getError($var)
} else {
$name = $var;
}
return isset($this->_errors[$name]) ? $this->_errors[$name] : null;
return $this->_errors[$name] ?? null;
}

public function setError($var, $message)
Expand Down Expand Up @@ -482,13 +509,13 @@ protected function _getInfoFromVariables($variables, $info)
} else {
if (Horde_Array::getArrayParts($var->getVarName(), $base, $keys)) {
if (!isset($info[$base])) {
$info[$base] = array();
$info[$base] = [];
}
$pointer = &$info[$base];
while (count($keys)) {
$key = array_shift($keys);
if (!isset($pointer[$key])) {
$pointer[$key] = array();
$pointer[$key] = [];
}
$pointer = &$pointer[$key];
}
Expand Down Expand Up @@ -545,8 +572,8 @@ public function onSubmit()
* submitted if old value and new value differ. */
if ($var->getOption('trackchange')) {
$varname = $var->getVarName();
if (!is_null($this->_vars->get('formname')) &&
$this->_vars->get($varname) != $this->_vars->get('__old_' . $varname)) {
if (!is_null($this->_vars->get('formname'))
&& $this->_vars->get($varname) != $this->_vars->get('__old_' . $varname)) {
$this->_submitted = false;
}
}
Expand Down
35 changes: 18 additions & 17 deletions lib/Horde/Core/Form/Renderer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @package Form
*/
Expand All @@ -7,8 +8,8 @@
* The Horde_Core_Form_Renderer class provides HTML and other renderings of
* forms for the Horde_Core_Form package.
*
* Copyright 2001-2007 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2005-2007 Matt Warden <mwarden@gmail.com>
* Copyright 2001-2026 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2005-2026 Matt Warden <mwarden@gmail.com>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
Expand All @@ -19,16 +20,16 @@
*/
abstract class Horde_Core_Form_Renderer
{
var $_name;
var $_requiredLegend = false;
var $_helpMarker = '?';
var $_onLoadJS = array();
var $_showHeader = true;
var $_cols = 2;
var $_firstField = null;
var $_stripedRows = true;
public $_name;
public $_requiredLegend = false;
public $_helpMarker = '?';
public $_onLoadJS = [];
public $_showHeader = true;
public $_cols = 2;
public $_firstField = null;
public $_stripedRows = true;

protected $_submit = array();
protected $_submit = [];
protected $_reset = false;

/**
Expand All @@ -38,7 +39,7 @@ abstract class Horde_Core_Form_Renderer
*
* @var boolean
*/
var $_encodeTitle = true;
public $_encodeTitle = true;

/**
* Construct a new Horde_Form_Renderer::.
Expand All @@ -47,7 +48,7 @@ abstract class Horde_Core_Form_Renderer
* Possible keys:
* - 'encode_title': @see $_encodeTitle
*/
function __construct($params = array())
public function __construct($params = [])
{
if (isset($params['encode_title'])) {
$this->encodeTitle($params['encode_title']);
Expand All @@ -60,10 +61,10 @@ public function setButtons($submit, $reset = false)
{
if ($submit === true || is_null($submit) || empty($submit)) {
/* Default to 'Submit'. */
$submit = array(Horde_Model_Translation::t("Submit"));
$submit = [Horde_Model_Translation::t("Submit")];
} elseif (!is_array($submit)) {
/* Default to array if not passed. */
$submit = array($submit);
$submit = [$submit];
}
/* Only if $reset is strictly true insert default 'Reset'. */
if ($reset === true) {
Expand All @@ -79,7 +80,7 @@ public function setButtons($submit, $reset = false)
public function addButtons($buttons)
{
if (!is_array($buttons)) {
$buttons = array($buttons);
$buttons = [$buttons];
}

$this->_submit = array_merge($this->_submit, $buttons);
Expand All @@ -100,7 +101,7 @@ public function showHeader($bool)
*
* @return boolean Whether the form title should be encoded.
*/
function encodeTitle($encode = null)
public function encodeTitle($encode = null)
{
if (!is_null($encode)) {
$this->_encodeTitle = $encode;
Expand Down
Loading