+--------------------------------------------------------------------+
| CiviCRM version 4.6 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014 |
+ | Copyright CiviCRM LLC (c) 2004-2015 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
* machine. Each form can also operate in various modes
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
* $Id$
*
*/
private $_chainSelectFields = array();
/**
- * Constructor for the basic form page
+ * Constructor for the basic form page.
*
* We should not use QuickForm directly. This class provides a lot
* of default convenient functions, rules and buttons
}
/**
- * Register all the standard rules that most forms potentially use
+ * Register all the standard rules that most forms potentially use.
*
* @return void
*/
}
/**
- * The postProcess hook is typically called by the framework
+ * The postProcess hook is typically called by the framework.
* However in a few cases, the form exits or redirects early in which
* case it needs to call this function so other modules can do the needful
* Calling this function directly should be avoided if possible. In general a
*
* access public
*
- * @return array
+ * @return array|NULL
* reference to the array of default values
*/
public function setDefaultValues() {
+ return NULL;
}
/**
}
/**
- * Performs the server side validation
+ * Performs the server side validation.
* @since 1.0
- * @return boolean
+ * @return bool
* true if no error found
* @throws HTML_QuickForm_Error
*/
/**
* Add default Next / Back buttons
*
- * @param array array of associative arrays in the order in which the buttons should be
- * displayed. The associate array has 3 fields: 'type', 'name' and 'isDefault'
- * The base form class will define a bunch of static arrays for commonly used
- * formats
+ * @param array $params
+ * Array of associative arrays in the order in which the buttons should be
+ * displayed. The associate array has 3 fields: 'type', 'name' and 'isDefault'
+ * The base form class will define a bunch of static arrays for commonly used
+ * formats.
*
* @return void
*/
foreach ($params as $button) {
$attrs = array('class' => 'crm-form-submit') + (array) CRM_Utils_Array::value('js', $button);
+ if (!empty($button['class'])) {
+ $attrs['class'] .= ' ' . $button['class'];
+ }
+
if (!empty($button['isDefault'])) {
$attrs['class'] .= ' default';
}
}
/**
- * Getter function for Name
+ * Getter function for Name.
*
* @return string
*/
}
/**
- * Getter function for State
+ * Getter function for State.
*
* @return object
*/
}
/**
- * Getter function for StateType
+ * Getter function for StateType.
*
* @return int
*/
}
/**
- * Setter function for options
+ * Setter function for options.
*
- * @param mixed
+ * @param mixed $options
*
* @return void
*/
}
/**
- * Boolean function to determine if this is a one form page
+ * Boolean function to determine if this is a one form page.
*
- * @return boolean
+ * @return bool
*/
public function isSimpleForm() {
return $this->_state->getType() & (CRM_Core_State::START | CRM_Core_State::FINISH);
}
/**
- * Getter function for Form Action
+ * Getter function for Form Action.
*
* @return string
*/
}
/**
- * Setter function for Form Action
+ * Setter function for Form Action.
*
- * @param string
+ * @param string $action
*
* @return void
*/
}
/**
- * Render form and return contents
+ * Render form and return contents.
*
* @return string
*/
}
/**
- * Use the form name to create the tpl file name
+ * Use the form name to create the tpl file name.
*
* @return string
*/
$tplname = $ext->getTemplatePath(CRM_Utils_System::getClassName($this)) . DIRECTORY_SEPARATOR . $filename;
}
else {
- $tplname = str_replace('_',
- DIRECTORY_SEPARATOR,
- CRM_Utils_System::getClassName($this)
- ) . '.tpl';
+ $tplname = strtr(
+ CRM_Utils_System::getClassName($this),
+ array(
+ '_' => DIRECTORY_SEPARATOR,
+ '\\' => DIRECTORY_SEPARATOR,
+ )
+ ) . '.tpl';
}
return $tplname;
}
}
/**
- * Error reporting mechanism
+ * Error reporting mechanism.
*
* @param string $message
* Error Message.
}
/**
- * Store the variable with the value in the form scope
- *
- * @param string name : name of the variable
- * @param mixed value : value of the variable
+ * Store the variable with the value in the form scope.
*
+ * @param string $name
+ * Name of the variable.
+ * @param mixed $value
+ * Value of the variable.
*
* @return void
*/
}
/**
- * Get the variable from the form scope
- *
- * @param string name : name of the variable
+ * Get the variable from the form scope.
*
+ * @param string $name
+ * Name of the variable
*
* @return mixed
*/
}
/**
- * Getter for action
+ * Getter for action.
*
* @return int
*/
}
/**
- * Setter for action
+ * Setter for action.
*
* @param int $action
* The mode we want to set the form.
}
/**
- * Assign value to name in template
+ * Assign value to name in template.
*
* @param string $var
* Name of variable.
}
/**
- * Assign value to name in template by reference
+ * Assign value to name in template by reference.
*
* @param string $var
* Name of variable.
}
/**
- * Appends values to template variables
+ * Appends values to template variables.
*
* @param array|string $tpl_var the template variable name(s)
* @param mixed $value
}
/**
- * Returns an array containing template variables
+ * Returns an array containing template variables.
*
* @param string $name
*
$attributes = $attributes ? $attributes : array();
$allowClear = !empty($attributes['allowClear']);
unset($attributes['allowClear']);
- $attributes += array('id_suffix' => $name);
+ $attributes['id_suffix'] = $name;
foreach ($values as $key => $var) {
$options[] = $this->createElement('radio', NULL, NULL, $var, $key, $attributes);
}
}
/**
- * Adds a select based on field metadata
+ * Adds a select based on field metadata.
* TODO: This could be even more generic and widget type (select in this case) could also be read from metadata
* Perhaps a method like $form->bind($name) which would look up all metadata for named field
* @param $name
}
/**
- * Add date
- * @param string $name
- * Name of the element.
- * @param string $label
- * Label of the element.
- * @param array $attributes
- * Key / value pair.
+ * Add date.
*
+ * @code
* // if you need time
* $attributes = array(
* 'addTime' => true,
* 'formatType' => 'relative' or 'birth' etc check advanced date settings
* );
+ * @endcode
+ *
+ * @param string $name
+ * Name of the element.
+ * @param string $label
+ * Label of the element.
* @param bool $required
* True if required.
+ * @param array $attributes
+ * Key / value pair.
*/
public function addDate($name, $label, $required = FALSE, $attributes = NULL) {
if (!empty($attributes['formatType'])) {
}
/**
- * Function that will add date and time
+ * Function that will add date and time.
*/
public function addDateTime($name, $label, $required = FALSE, $attributes = NULL) {
$addTime = array('addTime' => TRUE);
}
/**
- * Add a currency and money element to the form
+ * Add a currency and money element to the form.
*/
public function addMoney(
$name,
}
/**
- * Add currency element to the form
+ * Add currency element to the form.
*/
public function addCurrency(
$name = 'currency',
}
/**
- * Create a single or multiple entity ref field
+ * Create a single or multiple entity ref field.
* @param string $name
* @param string $label
* @param array $props
}
/**
- * Function that can be defined in Form to override or
+ * Function that can be defined in Form to override or.
* perform specific action on cancel action
*/
public function cancelAction() {
}
/**
- * Helper function to verify that required fields have been filled
+ * Helper function to verify that required fields have been filled.
* Typically called within the scope of a FormRule function
*/
public static function validateMandatoryFields($fields, $values, &$errors) {
* - cid from the url if the caller has ACL permission to view
* - fallback is logged in user (or ? NULL if no logged in user) (@todo wouldn't 0 be more intuitive?)
*
- * @return NULL|integer
+ * @return NULL|int
*/
public function getContactID() {
$tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
// from that page
// we don't really need to set it when $tempID is set because the params have that stored
$this->set('cid', 0);
- return $tempID;
+ return (int) $tempID;
}
$userID = $this->getLoggedInUserContactID();
- if ($tempID == $userID) {
- return $userID;
+ if (!is_null($tempID) && $tempID === $userID) {
+ return (int) $userID;
}
//check if this is a checksum authentication
return $tempID;
}
- return $userID;
+ return is_numeric($userID) ? $userID : NULL;
}
/**
- * Get the contact id of the logged in user
+ * Get the contact id of the logged in user.
*/
public function getLoggedInUserContactID() {
// check if the user is logged in and has a contact ID
* that small pieces of duplication are not being refactored into separate functions because their only shared parent
* is this form. Inserting a class FrontEndForm.php between the contribution & event & this class would allow functions like this
* and a dozen other small ones to be refactored into a shared parent with the reduction of much code duplication
+ *
+ * @param $onlinePaymentProcessorEnabled
*/
public function addCIDZeroOptions($onlinePaymentProcessorEnabled) {
$this->assign('nocid', TRUE);
}
/**
- * Sets form attribute
+ * Sets form attribute.
* @see CRM.loadForm
*/
public function preventAjaxSubmit() {
}
/**
- * Sets form attribute
+ * Sets form attribute.
* @see CRM.loadForm
*/
public function allowAjaxSubmit() {
}
/**
- * Sets page title based on entity and action
+ * Sets page title based on entity and action.
* @param string $entityLabel
*/
public function setPageTitle($entityLabel) {
*/
private function preProcessChainSelectFields() {
foreach ($this->_chainSelectFields as $control => $target) {
- $targetField = $this->getElement($target);
- $targetType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ? 'county' : 'stateProvince';
- $options = array();
- // If the control field is on the form, setup chain-select and dynamically populate options
- if ($this->elementExists($control)) {
- $controlField = $this->getElement($control);
- $controlType = $targetType == 'county' ? 'stateProvince' : 'country';
-
- $targetField->setAttribute('class', $targetField->getAttribute('class') . ' crm-chain-select-target');
-
- $css = (string) $controlField->getAttribute('class');
- $controlField->updateAttributes(array(
- 'class' => ($css ? "$css " : 'crm-select2 ') . 'crm-chain-select-control',
- 'data-target' => $target,
- ));
- $controlValue = $controlField->getValue();
- if ($controlValue) {
- $options = CRM_Core_BAO_Location::getChainSelectValues($controlValue, $controlType, TRUE);
- if (!$options) {
- $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
+ // The 'target' might get missing if extensions do removeElement() in a form hook.
+ if ($this->elementExists($target)) {
+ $targetField = $this->getElement($target);
+ $targetType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ? 'county' : 'stateProvince';
+ $options = array();
+ // If the control field is on the form, setup chain-select and dynamically populate options
+ if ($this->elementExists($control)) {
+ $controlField = $this->getElement($control);
+ $controlType = $targetType == 'county' ? 'stateProvince' : 'country';
+
+ $targetField->setAttribute('class', $targetField->getAttribute('class') . ' crm-chain-select-target');
+
+ $css = (string) $controlField->getAttribute('class');
+ $controlField->updateAttributes(array(
+ 'class' => ($css ? "$css " : 'crm-select2 ') . 'crm-chain-select-control',
+ 'data-target' => $target,
+ ));
+ $controlValue = $controlField->getValue();
+ if ($controlValue) {
+ $options = CRM_Core_BAO_Location::getChainSelectValues($controlValue, $controlType, TRUE);
+ if (!$options) {
+ $targetField->setAttribute('placeholder', $targetField->getAttribute('data-none-prompt'));
+ }
+ }
+ else {
+ $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
+ $targetField->setAttribute('disabled', 'disabled');
}
}
+ // Control field not present - fall back to loading default options
else {
- $targetField->setAttribute('placeholder', $targetField->getAttribute('data-empty-prompt'));
- $targetField->setAttribute('disabled', 'disabled');
+ $options = CRM_Core_PseudoConstant::$targetType();
}
+ if (!$targetField->getAttribute('multiple')) {
+ $options = array('' => $targetField->getAttribute('placeholder')) + $options;
+ $targetField->removeAttribute('placeholder');
+ }
+ $targetField->_options = array();
+ $targetField->loadArray($options);
}
- // Control field not present - fall back to loading default options
- else {
- $options = CRM_Core_PseudoConstant::$targetType();
- }
- if (!$targetField->getAttribute('multiple')) {
- $options = array('' => $targetField->getAttribute('placeholder')) + $options;
- $targetField->removeAttribute('placeholder');
- }
- $targetField->_options = array();
- $targetField->loadArray($options);
}
}
*/
private function validateChainSelectFields() {
foreach ($this->_chainSelectFields as $control => $target) {
- if ($this->elementExists($control)) {
+ if ($this->elementExists($control) && $this->elementExists($target)) {
$controlValue = (array) $this->getElementValue($control);
$targetField = $this->getElement($target);
$controlType = $targetField->getAttribute('data-callback') == 'civicrm/ajax/jqCounty' ? 'stateProvince' : 'country';