Merge pull request #5078 from eileenmcnaughton/comment-full-stops
[civicrm-core.git] / CRM / Core / Form.php
index 82b2d6dc8c2a727ca12897883aa58584fee83c52..1ecbbede88734d9c028a39123c08580e2f606b53 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * This is our base form. It is part of the Form/Controller/StateMachine
@@ -152,7 +152,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   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
@@ -167,7 +167,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    *
    * @return \CRM_Core_Form
    */
-  function __construct(
+  public function __construct(
     $state = NULL,
     $action = CRM_Core_Action::NONE,
     $method = 'post',
@@ -221,14 +221,31 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   public function registerRules() {
     static $rules = array(
-      'title', 'longTitle', 'variable', 'qfVariable',
-      'phone', 'integer', 'query',
-      'url', 'wikiURL',
-      'domain', 'numberOfDigit',
-      'date', 'currentDate',
-      'asciiFile', 'htmlFile', 'utf8File',
-      'objectExists', 'optionExists', 'postalCode', 'money', 'positiveInteger',
-      'xssString', 'fileExists', 'autocomplete', 'validContact',
+      'title',
+      'longTitle',
+      'variable',
+      'qfVariable',
+      'phone',
+      'integer',
+      'query',
+      'url',
+      'wikiURL',
+      'domain',
+      'numberOfDigit',
+      'date',
+      'currentDate',
+      'asciiFile',
+      'htmlFile',
+      'utf8File',
+      'objectExists',
+      'optionExists',
+      'postalCode',
+      'money',
+      'positiveInteger',
+      'xssString',
+      'fileExists',
+      'autocomplete',
+      'validContact',
     );
 
     foreach ($rules as $rule) {
@@ -250,7 +267,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    *
    * @return HTML_QuickForm_Element could be an error object
    */
-  function &add(
+  public function &add(
     $type, $name, $label = '',
     $attributes = '', $required = FALSE, $extra = NULL
   ) {
@@ -329,7 +346,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     $this->postProcessHook();
 
     // Respond with JSON if in AJAX context (also support legacy value '6')
-    if ($allowAjax && !empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], array(CRM_Core_Smarty::PRINT_JSON, 6))) {
+    if ($allowAjax && !empty($_REQUEST['snippet']) && in_array($_REQUEST['snippet'], array(
+          CRM_Core_Smarty::PRINT_JSON,
+          6,
+        ))
+    ) {
       $this->ajaxResponse['buttonName'] = str_replace('_qf_' . $this->getAttribute('id') . '_', '', $this->controller->getButtonName());
       $this->ajaxResponse['action'] = $this->_action;
       if (isset($this->_id) || isset($this->id)) {
@@ -340,7 +361,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   }
 
   /**
-   * 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
@@ -367,10 +388,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    *
    * access        public
    *
-   * @return array
+   * @return array|NULL
    *   reference to the array of default values
    */
   public function setDefaultValues() {
+    return NULL;
   }
 
   /**
@@ -387,7 +409,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Performs the server side validation
    * @since     1.0
-   * @return boolean
+   * @return bool
    *   true if no error found
    * @throws    HTML_QuickForm_Error
    */
@@ -469,10 +491,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
 
     //Set html data-attribute to enable warning user of unsaved changes
     if ($this->unsavedChangesWarn === TRUE
-        || (!isset($this->unsavedChangesWarn)
-           && ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE)
-           )
-        ) {
+      || (!isset($this->unsavedChangesWarn)
+        && ($this->_action & CRM_Core_Action::ADD || $this->_action & CRM_Core_Action::UPDATE)
+      )
+    ) {
       $this->setAttribute('data-warn-changes', 'true');
     }
   }
@@ -480,10 +502,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * 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
    */
@@ -492,6 +515,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
     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';
       }
@@ -598,7 +625,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Setter function for options
    *
-   * @param mixed
+   * @param mixed $options
    *
    * @return void
    */
@@ -621,7 +648,7 @@ class CRM_Core_Form extends HTML_QuickForm_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);
@@ -639,7 +666,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Setter function for Form Action
    *
-   * @param string
+   * @param string $action
    *
    * @return void
    */
@@ -688,9 +715,12 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       $tplname = $ext->getTemplatePath(CRM_Utils_System::getClassName($this)) . DIRECTORY_SEPARATOR . $filename;
     }
     else {
-      $tplname = str_replace('_',
-        DIRECTORY_SEPARATOR,
-        CRM_Utils_System::getClassName($this)
+      $tplname = strtr(
+        CRM_Utils_System::getClassName($this),
+        array(
+          '_' => DIRECTORY_SEPARATOR,
+          '\\' => DIRECTORY_SEPARATOR,
+        )
       ) . '.tpl';
     }
     return $tplname;
@@ -741,9 +771,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Store the variable with the value in the form scope
    *
-   * @param string name : name of the variable
-   * @param mixed value : value of the variable
-   *
+   * @param string $name
+   *   Name of the variable.
+   * @param mixed $value
+   *   Value of the variable.
    *
    * @return void
    */
@@ -754,8 +785,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Get the variable from the form scope
    *
-   * @param string name : name of the variable
-   *
+   * @param string $name
+   *   Name of the variable
    *
    * @return mixed
    */
@@ -873,7 +904,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   public function addYesNo($id, $title, $allowClear = FALSE, $required = NULL, $attributes = array()) {
     $attributes += array('id_suffix' => $id);
-    $choice   = array();
+    $choice = array();
     $choice[] = $this->createElement('radio', NULL, '11', ts('Yes'), '1', $attributes);
     $choice[] = $this->createElement('radio', NULL, '11', ts('No'), '0', $attributes);
 
@@ -897,7 +928,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    * @param string $separator
    * @param bool $flipValues
    */
-  function addCheckBox(
+  public function addCheckBox(
     $id, $title, $values, $other = NULL,
     $attributes = NULL, $required = NULL,
     $javascriptMethod = NULL,
@@ -1112,8 +1143,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       'editor_id'
     );
     $editor = strtolower(CRM_Utils_Array::value($editorID,
-        CRM_Core_OptionGroup::values('wysiwyg_editor')
-      ));
+      CRM_Core_OptionGroup::values('wysiwyg_editor')
+    ));
     if (!$editor || $forceTextarea) {
       $editor = 'textarea';
     }
@@ -1152,7 +1183,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   public function addCountry($id, $title, $required = NULL, $extra = NULL) {
     $this->addElement('select', $id, $title,
       array(
-        '' => ts('- select -')) + CRM_Core_PseudoConstant::country(), $extra
+        '' => ts('- select -'),
+      ) + CRM_Core_PseudoConstant::country(), $extra
     );
     if ($required) {
       $this->addRule($id, ts('Please select %1', array(1 => $title)), 'required');
@@ -1254,20 +1286,23 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
 
   /**
    * Add date
-   * @param string $name
-   *   Name of the element.
-   * @param string $label
-   *   Label of the element.
-   * @param array $attributes
-   *   Key / value pair.
    *
+   * @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'])) {
@@ -1365,7 +1400,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Add a currency and money element to the form
    */
-  function addMoney(
+  public function addMoney(
     $name,
     $label,
     $required = FALSE,
@@ -1388,7 +1423,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
   /**
    * Add currency element to the form
    */
-  function addCurrency(
+  public function addCurrency(
     $name = 'currency',
     $label = NULL,
     $required = TRUE,
@@ -1418,17 +1453,17 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    * @param string $label
    * @param array $props
    *   Mix of html and widget properties, including:.
-   *  - select - params to give to select2 widget
-   *  - entity - defaults to contact
-   *  - create - can the user create a new entity on-the-fly?
+   *   - select - params to give to select2 widget
+   *   - entity - defaults to contact
+   *   - create - can the user create a new entity on-the-fly?
    *             Set to TRUE if entity is contact and you want the default profiles,
    *             or pass in your own set of links. @see CRM_Core_BAO_UFGroup::getCreateLinks for format
    *             note that permissions are checked automatically
-   *  - api - array of settings for the getlist api wrapper
+   *   - api - array of settings for the getlist api wrapper
    *          note that it accepts a 'params' setting which will be passed to the underlying api
-   *  - placeholder - string
-   *  - multiple - bool
-   *  - class, etc. - other html properties
+   *   - placeholder - string
+   *   - multiple - bool
+   *   - class, etc. - other html properties
    * @param bool $required
    *
    * @return HTML_QuickForm_Element
@@ -1487,8 +1522,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       if (!empty($params[$fieldName])) {
         $params[$fieldName] = CRM_Utils_Date::isoToMysql(
           CRM_Utils_Date::processDate(
-          $params[$fieldName],
-          CRM_Utils_Array::value("{$fieldName}_time", $params), TRUE)
+            $params[$fieldName],
+            CRM_Utils_Array::value("{$fieldName}_time", $params), TRUE)
         );
       }
       else {
@@ -1537,14 +1572,14 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
 
   /**
    * Get contact if for a form object. Prioritise
-   *  - cid in URL if 0 (on behalf on someoneelse)
+   *   - cid in URL if 0 (on behalf on someoneelse)
    *      (@todo consider setting a variable if onbehalf for clarity of downstream 'if's
-   *  - logged in user id if it matches the one in the cid in the URL
-   *  - contact id validated from a checksum from a checksum
-   *  - 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?)
+   *   - logged in user id if it matches the one in the cid in the URL
+   *   - contact id validated from a checksum from a checksum
+   *   - 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 mixed NULL|integer
+   * @return NULL|int
    */
   public function getContactID() {
     $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
@@ -1615,29 +1650,32 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    *   Ids of profiles that are on the form (to be autofilled).
    * @param array $autoCompleteField
    *
-   *  - name_field
-   *  - id_field
-   *  - url (for ajax lookup)
+   *   - name_field
+   *   - id_field
+   *   - url (for ajax lookup)
    *
    * @todo add data attributes so we can deal with multiple instances on a form
    */
   public function addAutoSelector($profiles = array(), $autoCompleteField = array()) {
     $autoCompleteField = array_merge(array(
-        'id_field' => 'select_contact_id',
-        'placeholder' => ts('Select someone else ...'),
-        'show_hide' => TRUE,
-        'api' => array('params' => array('contact_type' => 'Individual')),
-      ), $autoCompleteField);
+      'id_field' => 'select_contact_id',
+      'placeholder' => ts('Select someone else ...'),
+      'show_hide' => TRUE,
+      'api' => array('params' => array('contact_type' => 'Individual')),
+    ), $autoCompleteField);
 
     if ($this->canUseAjaxContactLookups()) {
       $this->assign('selectable', $autoCompleteField['id_field']);
-      $this->addEntityRef($autoCompleteField['id_field'], NULL, array('placeholder' => $autoCompleteField['placeholder'], 'api' => $autoCompleteField['api']));
+      $this->addEntityRef($autoCompleteField['id_field'], NULL, array(
+          'placeholder' => $autoCompleteField['placeholder'],
+          'api' => $autoCompleteField['api'],
+        ));
 
       CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/AlternateContactSelector.js', 1, 'html-header')
-      ->addSetting(array(
-      'form' => array('autocompletes' => $autoCompleteField),
-      'ids' => array('profile' => $profiles),
-      ));
+        ->addSetting(array(
+          'form' => array('autocompletes' => $autoCompleteField),
+          'ids' => array('profile' => $profiles),
+        ));
     }
   }
 
@@ -1645,7 +1683,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   public function canUseAjaxContactLookups() {
     if (0 < (civicrm_api3('contact', 'getcount', array('check_permissions' => 1))) &&
-      CRM_Core_Permission::check(array(array('access AJAX API', 'access CiviCRM')))) {
+      CRM_Core_Permission::check(array(array('access AJAX API', 'access CiviCRM')))
+    ) {
       return TRUE;
     }
   }
@@ -1657,6 +1696,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    * 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);
@@ -1750,7 +1791,12 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
    */
   public function addChainSelect($elementName, $settings = array()) {
     $props = $settings += array(
-      'control_field' => str_replace(array('state_province', 'StateProvince', 'county', 'County'), array('country', 'Country', 'state_province', 'StateProvince'), $elementName),
+      'control_field' => str_replace(array('state_province', 'StateProvince', 'county', 'County'), array(
+          'country',
+          'Country',
+          'state_province',
+          'StateProvince',
+        ), $elementName),
       'data-callback' => strpos($elementName, 'rovince') ? 'civicrm/ajax/jqState' : 'civicrm/ajax/jqCounty',
       'label' => strpos($elementName, 'rovince') ? ts('State/Province') : ts('County'),
       'data-empty-prompt' => strpos($elementName, 'rovince') ? ts('Choose country first') : ts('Choose state first'),
@@ -1842,4 +1888,5 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       }
     }
   }
+
 }