copyright and version fixes
[civicrm-core.git] / CRM / Group / Form / Edit.php
index 669f0b6faaec9fb5c18c626e5a53dd76fec69ed2..ff4a18e62acbd6d3825ea578dd39f78763497fc2 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -88,7 +88,6 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
    */
   function preProcess() {
     $this->_id = $this->get('id');
-
     if ($this->_id) {
       $breadCrumb = array(array('title' => ts('Manage Groups'),
           'url' => CRM_Utils_System::url('civicrm/group',
@@ -132,10 +131,15 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
             'search_custom_id'
           );
         }
-        if (CRM_Utils_Array::value('created_id', $this->_groupValues))
+        if (!empty($this->_groupValues['created_id']))
           $groupValues['created_by'] =
             CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['created_id'] , 'sort_name', 'id');
 
+        if (!empty($this->_groupValues['modified_id'])) {
+          $groupValues['modified_by'] =
+            CRM_Core_DAO::getFieldValue("CRM_Contact_DAO_Contact", $this->_groupValues['modified_id'] , 'sort_name', 'id');
+        }
+
         $this->assign_by_ref('group', $groupValues);
 
         CRM_Utils_System::setTitle(ts('Group Settings: %1', array(1 => $this->_title)));
@@ -153,14 +157,14 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
    * the default values are retrieved from the database
    *
    * @access public
-   * @return None
+   * @return void
    */
   function setDefaultValues() {
     $defaults = array();
 
     if (isset($this->_id)) {
       $defaults = $this->_groupValues;
-      if (CRM_Utils_Array::value('group_type', $defaults)) {
+      if (!empty($defaults['group_type'])) {
         $types = explode(CRM_Core_DAO::VALUE_SEPARATOR,
           substr($defaults['group_type'], 1, -1)
         );
@@ -170,17 +174,8 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
         }
       }
 
-      if (CRM_Core_Permission::check('administer Multiple Organizations') &&
-        CRM_Core_Permission::isMultisiteEnabled()
-      ) {
+      if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) {
         CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults);
-
-        if (CRM_Utils_Array::value('group_organization', $defaults)) {
-          //used in edit mode
-          $this->_groupOrganizationID = $defaults['group_organization'];
-        }
-
-        $this->assign('organizationID', CRM_Utils_Array::value('organization_id',$defaults));
       }
     }
 
@@ -198,7 +193,7 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
       }
     }
 
-    if (!CRM_Utils_Array::value('parents', $defaults)) {
+    if (empty($defaults['parents'])) {
       $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
     }
 
@@ -210,7 +205,7 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
   /**
    * Function to actually build the form
    *
-   * @return None
+   * @return void
    * @access public
    */
   public function buildQuickForm() {
@@ -230,6 +225,11 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
       return;
     }
 
+    // We want the "new group" form to redirect the user
+    if ($this->_action == CRM_Core_Action::ADD) {
+      $this->preventAjaxSubmit();
+    }
+
     $this->applyFilter('__ALL__', 'trim');
     $this->add('text', 'title', ts('Name') . ' ',
       CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE
@@ -241,9 +241,7 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
 
     $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
     $config = CRM_Core_Config::singleton();
-    if (isset($this->_id) &&
-      CRM_Utils_Array::value('saved_search_id', $this->_groupValues)
-    ) {
+    if (isset($this->_id) && !empty($this->_groupValues['saved_search_id'])) {
       unset($groupTypes['Access Control']);
     }
 
@@ -255,66 +253,15 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
       );
     }
 
-    $this->add('select', 'visibility', ts('Visibility'),
-      CRM_Core_SelectValues::ufVisibility(TRUE), TRUE
-    );
-
-    $groupNames = CRM_Core_PseudoConstant::group();
-
-    $parentGroups = $parentGroupElements = array();
-    if (isset($this->_id) &&
-      CRM_Utils_Array::value('parents', $this->_groupValues)
-    ) {
-      $parentGroupIds = explode(',', $this->_groupValues['parents']);
-      foreach ($parentGroupIds as $parentGroupId) {
-        $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
-        if (array_key_exists($parentGroupId, $groupNames)) {
-          $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
-          $this->addElement('checkbox', "remove_parent_group_$parentGroupId",
-            $groupNames[$parentGroupId]
-          );
-        }
-      }
-    }
-    $this->assign_by_ref('parent_groups', $parentGroupElements);
-
-    if (isset($this->_id)) {
-      $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($this->_id,
-        $groupNames
-      );
-    }
-    else {
-      $potentialParentGroupIds = array_keys($groupNames);
-    }
+    $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::groupVisibility(), TRUE);
 
-    $parentGroupSelectValues = array('' => '- ' . ts('select') . ' -');
-    foreach ($potentialParentGroupIds as $potentialParentGroupId) {
-      if (array_key_exists($potentialParentGroupId, $groupNames)) {
-        $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
-      }
-    }
+    //CRM-14190
+    $parentGroups = self::buildParentGroups($this);
 
-    if (count($parentGroupSelectValues) > 1) {
-      if (CRM_Core_Permission::isMultisiteEnabled()) {
-        $required = empty($parentGroups) ? TRUE : FALSE;
-        $required = (($this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id)) ||
-          !isset($this->_id)
-        ) ? FALSE : $required;
-      }
-      else {
-        $required = FALSE;
-      }
-      $this->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required);
-    }
-    if (CRM_Core_Permission::check('administer Multiple Organizations') &&
-      CRM_Core_Permission::isMultisiteEnabled()
-    ) {
+    if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) {
       //group organization Element
-      $groupOrgDataURL = CRM_Utils_System::url('civicrm/ajax/search', 'org=1', FALSE, NULL, FALSE);
-      $this->assign('groupOrgDataURL', $groupOrgDataURL);
-
-      $this->addElement('text', 'organization', ts('Organization'), '');
-      $this->addElement('hidden', 'organization_id', '', array('id' => 'organization_id'));
+      $props = array('api' => array('params' => array('contact_type' => 'Organization')));
+      $this->addEntityRef('organization_id', ts('Organization'), $props);
     }
 
     // is_reserved property CRM-9936
@@ -380,7 +327,7 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
       }
 
       $grpAdd = 0;
-      if (CRM_Utils_Array::value('parents', $fields)) {
+      if (!empty($fields['parents'])) {
         $grpAdd++;
       }
 
@@ -390,7 +337,7 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
     }
 
     // do check for both name and title uniqueness
-    if (CRM_Utils_Array::value('title', $fields)) {
+    if (!empty($fields['title'])) {
       $title = trim($fields['title']);
       $query = "
 SELECT count(*)
@@ -454,10 +401,10 @@ WHERE  title = %1
       $group = CRM_Contact_BAO_Group::create($params);
 
       /*
-             * Remove any parent groups requested to be removed
-             */
+       * Remove any parent groups requested to be removed
+       */
 
-      if (CRM_Utils_Array::value('parents', $this->_groupValues)) {
+      if (!empty($this->_groupValues['parents'])) {
         $parentGroupIds = explode(',', $this->_groupValues['parents']);
         foreach ($parentGroupIds as $parentGroupId) {
           if (isset($params["remove_parent_group_$parentGroupId"])) {
@@ -487,5 +434,65 @@ WHERE  title = %1
       CRM_Contact_BAO_GroupNestingCache::update();
     }
   }
+
+  /*
+   * Build parent groups form elements
+   *
+   * @obj form object passed by reference
+   *
+   * @return parent groups array
+   * @static
+   * @access public
+   */
+  static function buildParentGroups( &$obj ) {
+    $groupNames = CRM_Core_PseudoConstant::group();
+    $parentGroups = $parentGroupElements = array();
+    if (isset($obj->_id) &&
+      CRM_Utils_Array::value('parents', $obj->_groupValues)
+    ) {
+      $parentGroupIds = explode(',', $obj->_groupValues['parents']);
+      foreach ($parentGroupIds as $parentGroupId) {
+        $parentGroups[$parentGroupId] = $groupNames[$parentGroupId];
+        if (array_key_exists($parentGroupId, $groupNames)) {
+          $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId];
+          $obj->addElement('checkbox', "remove_parent_group_$parentGroupId",
+            $groupNames[$parentGroupId]
+          );
+        }
+      }
+    }
+    $obj->assign_by_ref('parent_groups', $parentGroupElements);
+
+    if (isset($obj->_id)) {
+      $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($obj->_id,
+        $groupNames
+      );
+    }
+    else {
+      $potentialParentGroupIds = array_keys($groupNames);
+    }
+
+    $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -');
+    foreach ($potentialParentGroupIds as $potentialParentGroupId) {
+      if (array_key_exists($potentialParentGroupId, $groupNames)) {
+        $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId];
+      }
+    }
+
+    if (count($parentGroupSelectValues) > 1) {
+      if (CRM_Core_Permission::isMultisiteEnabled()) {
+        $required = empty($parentGroups) ? TRUE : FALSE;
+        $required = (($obj->_id && CRM_Core_BAO_Domain::isDomainGroup($obj->_id)) ||
+          !isset($obj->_id)
+        ) ? FALSE : $required;
+      }
+      else {
+        $required = FALSE;
+      }
+      $obj->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2'));
+    }
+
+    return $parentGroups;
+  }
 }