Merge pull request #4880 from totten/master-cs3
[civicrm-core.git] / CRM / Custom / Form / Group.php
index 940268bad28526d7c19e075438cd7bd87791af48..0e7ee54da2071c8b356c23d5d0e0423de10b5fc0 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
 class CRM_Custom_Form_Group extends CRM_Core_Form {
 
   /**
-   * the set id saved to the session for an update
+   * The set id saved to the session for an update
    *
    * @var int
-   * @access protected
    */
   protected $_id;
 
@@ -50,33 +49,29 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
    *  set is empty or not
    *
    * @var bool
-   * @access protected
    */
   protected $_isGroupEmpty = TRUE;
 
   /**
-   * array of existing subtypes set for a custom set
+   * Array of existing subtypes set for a custom set
    *
    * @var array
-   * @access protected
    */
   protected $_subtypes = array();
 
   /**
-   * array of default params
+   * Array of default params
    *
    * @var array
-   * @access protected
    */
   protected $_defaults = array();
 
   /**
-   * Function to set variables up before form is built
+   * Set variables up before form is built
    *
    * @param null
    *
    * @return void
-   * @access public
    */
   public function preProcess() {
     // current set id
@@ -110,28 +105,29 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
   }
 
   /**
-   * global form rule
+   * Global form rule
    *
-   * @param array $fields the input form values
-   * @param array $files the uploaded files if any
+   * @param array $fields
+   *   The input form values.
+   * @param array $files
+   *   The uploaded files if any.
    * @param $self
    *
-   * @internal param array $options additional user data
    *
    * @return true if no errors, else array of errors
-   * @access public
    * @static
    */
-  static function formRule($fields, $files, $self) {
+  public static function formRule($fields, $files, $self) {
     $errors = array();
 
     //validate group title as well as name.
     $title  = $fields['title'];
     $name   = CRM_Utils_String::munge($title, '_', 64);
     $query  = 'select count(*) from civicrm_custom_group where ( name like %1 OR title like %2 ) and id != %3';
-    $grpCnt = CRM_Core_DAO::singleValueQuery($query, array(1 => array($name, 'String'),
+    $grpCnt = CRM_Core_DAO::singleValueQuery($query, array(
+    1 => array($name, 'String'),
         2 => array($title, 'String'),
-        3 => array((int)$self->_id, 'Integer'),
+        3 => array((int) $self->_id, 'Integer'),
       ));
     if ($grpCnt) {
       $errors['title'] = ts('Custom group \'%1\' already exists in Database.', array(1 => $title));
@@ -154,7 +150,7 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
     }
 
     if (!empty($fields['is_multiple'])) {
-        $self->assign('showMultiple', TRUE);
+      $self->assign('showMultiple', TRUE);
     }
 
     if (empty($fields['is_multiple']) && $fields['style'] == 'Tab with table') {
@@ -167,7 +163,7 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
       // gives the ascii value
       $asciiValue = ord($title{0});
       if ($asciiValue >= 48 && $asciiValue <= 57) {
-        $errors['title'] = ts("Set's Name should not start with digit");
+        $errors['title'] = ts("Name cannot not start with a digit");
       }
     }
 
@@ -181,20 +177,18 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
    * @param null
    *
    * @return void
-   * @access public
    * @see valid_date
    */
-  function addRules() {
+  public function addRules() {
     $this->addFormRule(array('CRM_Custom_Form_Group', 'formRule'), $this);
   }
 
   /**
-   * Function to actually build the form
+   * Build the form object
    *
    * @param null
    *
    * @return void
-   * @access public
    */
   public function buildQuickForm() {
     $this->applyFilter('__ALL__', 'trim');
@@ -298,7 +292,7 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
       ts('Used For'),
       array(
         'name' => 'extends[0]',
-        'style' => 'vertical-align: top;'
+        'style' => 'vertical-align: top;',
       ),
       TRUE
     );
@@ -405,15 +399,14 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
   }
 
   /**
-   * This function sets the default values for the form. Note that in edit/view mode
+   * Set default values for the form. Note that in edit/view mode
    * the default values are retrieved from the database
    *
    * @param null
    *
    * @return array   array of default values
-   * @access public
    */
-  function setDefaultValues() {
+  public function setDefaultValues() {
     $defaults = &$this->_defaults;
     $this->assign('showMaxMultiple', TRUE);
     if ($this->_action == CRM_Core_Action::ADD) {
@@ -444,9 +437,6 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
         $defaults['extends'][1] = array(0 => '');
       }
 
-
-      $subName = CRM_Utils_Array::value('extends_entity_column_id', $defaults);
-
       if ($extends == 'Relationship' && !empty($this->_subtypes)) {
         $relationshipDefaults = array();
         foreach ($defaults['extends'][1] as $donCare => $rel_type_id) {
@@ -466,7 +456,6 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
    * @param null
    *
    * @return void
-   * @access public
    */
   public function postProcess() {
     // get the submitted form values.
@@ -532,33 +521,35 @@ class CRM_Custom_Form_Group extends CRM_Core_Form {
     }
   }
 
-  /*
-   * Function to return a formatted list of relationship name.
-   * @param $list array array of relationship name.
-   * @static
-   * return array array of relationship name.
-   */
   /**
-   * @param $list
+   * Return a formatted list of relationship name.
+   *
+   * @param array $list
+   *   Array of relationship name.
    *
-   * @return array
+   * @return array of relationship name.
    */
-  static function getFormattedList(&$list) {
+  public static function getFormattedList(&$list) {
     $relName = array();
 
-    foreach ($list as $k => $v) {
-      $key = substr($k, 0, strpos($k, '_'));
+    foreach ($list as $listItemKey => $itemValue) {
+      // Extract the relationship ID.
+      $key = substr($listItemKey, 0, strpos($listItemKey, '_'));
       if (isset($list["{$key}_b_a"])) {
+        $relName["$key"] = $list["{$key}_a_b"];
+        // Are the two labels different?
         if ($list["{$key}_a_b"] != $list["{$key}_b_a"]) {
           $relName["$key"] = $list["{$key}_a_b"] . ' / ' . $list["{$key}_b_a"];
         }
         unset($list["{$key}_b_a"]);
+        unset($list["{$key}_a_b"]);
       }
       else {
+        // If no '_b_a' label exists save the '_a_b' one and unset it from the list
         $relName["{$key}"] = $list["{$key}_a_b"];
+        unset($list["{$key}_a_b"]);
       }
     }
     return $relName;
   }
 }
-