Merge pull request #2308 from civicrm/4.4
[civicrm-core.git] / CRM / Custom / Form / Option.php
index fb34d50ad0e7407990ee57f4448202802ccd2eef..15760f5aae9697ff78043cb73c651b3fcf8a555a 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -79,19 +79,27 @@ class CRM_Custom_Form_Option extends CRM_Core_Form {
     $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this);
 
     $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this);
+
     if (!isset($this->_gid) && $this->_fid) {
-      $this->_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
+      $this->_gid = CRM_Core_DAO::getFieldValue(
+        'CRM_Core_DAO_CustomField',
         $this->_fid,
         'custom_group_id'
       );
     }
+
     if ($this->_fid) {
-      $this->_optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
+      $this->_optionGroupID = CRM_Core_DAO::getFieldValue(
+        'CRM_Core_DAO_CustomField',
         $this->_fid,
         'option_group_id'
       );
     }
 
+    if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
+      CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
+    }
+
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
   }
 
@@ -154,6 +162,8 @@ class CRM_Custom_Form_Option extends CRM_Core_Form {
    */
   public function buildQuickForm() {
     if ($this->_action == CRM_Core_Action::DELETE) {
+      $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id));
+      $this->assign('label', $option['label']);
       $this->addButtons(array(
           array(
             'type' => 'next',
@@ -242,19 +252,20 @@ class CRM_Custom_Form_Option extends CRM_Core_Form {
    * @access public
    */
   static function formRule($fields, $files, $form) {
-    $optionLabel   = CRM_Utils_Type::escape($fields['label'], 'String');
-    $optionValue   = CRM_Utils_Type::escape($fields['value'], 'String');
+    $optionLabel   = $fields['label'];
+    $optionValue   = $fields['value'];
     $fieldId       = $form->_fid;
     $optionGroupId = $form->_optionGroupID;
 
     $temp = array();
     if (empty($form->_id)) {
       $query = "
-SELECT count(*) 
+SELECT count(*)
   FROM civicrm_option_value
  WHERE option_group_id = %1
    AND label = %2";
-      $params = array(1 => array($optionGroupId, 'Integer'),
+      $params = array(
+        1 => array($optionGroupId, 'Integer'),
         2 => array($optionLabel, 'String'),
       );
       if (CRM_Core_DAO::singleValueQuery($query, $params) > 0) {
@@ -262,11 +273,12 @@ SELECT count(*)
       }
 
       $query = "
-SELECT count(*) 
+SELECT count(*)
   FROM civicrm_option_value
  WHERE option_group_id = %1
    AND value = %2";
-      $params = array(1 => array($optionGroupId, 'Integer'),
+      $params = array(
+        1 => array($optionGroupId, 'Integer'),
         2 => array($optionValue, 'String'),
       );
       if (CRM_Core_DAO::singleValueQuery($query, $params) > 0) {
@@ -279,12 +291,13 @@ SELECT count(*)
 
       //check label duplicates within a custom field
       $query = "
-SELECT count(*) 
+SELECT count(*)
   FROM civicrm_option_value
  WHERE option_group_id = %1
    AND id != %2
    AND label = %3";
-      $params = array(1 => array($optionGroupId, 'Integer'),
+      $params = array(
+        1 => array($optionGroupId, 'Integer'),
         2 => array($optionId, 'Integer'),
         3 => array($optionLabel, 'String'),
       );
@@ -294,12 +307,13 @@ SELECT count(*)
 
       //check value duplicates within a custom field
       $query = "
-SELECT count(*) 
+SELECT count(*)
   FROM civicrm_option_value
  WHERE option_group_id = %1
    AND id != %2
    AND value = %3";
-      $params = array(1 => array($optionGroupId, 'Integer'),
+      $params = array(
+        1 => array($optionGroupId, 'Integer'),
         2 => array($optionId, 'Integer'),
         3 => array($optionValue, 'String'),
       );
@@ -309,7 +323,7 @@ SELECT count(*)
     }
 
     $query = "
-SELECT data_type 
+SELECT data_type
   FROM civicrm_custom_field
  WHERE id = %1";
     $params = array(1 => array($fieldId, 'Integer'));
@@ -363,7 +377,7 @@ SELECT data_type
           if (!empty($fields["value"])) {
             $params = array(1 => array($fields['value'], 'String'));
             $query = "
-SELECT count(*) 
+SELECT count(*)
   FROM civicrm_state_province
  WHERE name = %1
     OR abbreviation = %1";
@@ -391,10 +405,11 @@ SELECT count(*)
     $params = $this->controller->exportValues('Option');
 
     if ($this->_action == CRM_Core_Action::DELETE) {
+      $option = civicrm_api3('option_value', 'getsingle', array('id' => $this->_id));
       $fieldValues = array('option_group_id' => $this->_optionGroupID);
-      $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues);
+      CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues);
       CRM_Core_BAO_CustomOption::del($this->_id);
-      CRM_Core_Session::setStatus(ts('Your multiple choice option has been deleted'), ts('Deleted'), 'success');
+      CRM_Core_Session::setStatus(ts('Option "%1" has been deleted.', array(1 => $option['label'])), ts('Deleted'), 'success');
       return;
     }
 
@@ -414,19 +429,27 @@ SELECT count(*)
     }
 
     $fieldValues = array('option_group_id' => $this->_optionGroupID);
-    $customOption->weight = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_OptionValue', $oldWeight, $params['weight'], $fieldValues);
+    $customOption->weight =
+      CRM_Utils_Weight::updateOtherWeights(
+        'CRM_Core_DAO_OptionValue',
+        $oldWeight,
+        $params['weight'],
+        $fieldValues);
 
     $customOption->option_group_id = $this->_optionGroupID;
 
     $customField = new CRM_Core_DAO_CustomField();
     $customField->id = $this->_fid;
-    if ($customField->find(TRUE) &&
-      ($customField->html_type == 'CheckBox' ||
+    if (
+      $customField->find(TRUE) &&
+      (
+        $customField->html_type == 'CheckBox' ||
         $customField->html_type == 'AdvMulti-Select' ||
         $customField->html_type == 'Multi-Select'
       )
     ) {
-      $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR,
+      $defVal = explode(
+        CRM_Core_DAO::VALUE_SEPARATOR,
         substr($customField->default_value, 1, -1)
       );
       if (CRM_Utils_Array::value('default_value', $params)) {
@@ -437,7 +460,10 @@ SELECT count(*)
           else {
             $defVal[] = $customOption->value;
           }
-          $customField->default_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defVal) . CRM_Core_DAO::VALUE_SEPARATOR;
+          $customField->default_value =
+            CRM_Core_DAO::VALUE_SEPARATOR .
+            implode(CRM_Core_DAO::VALUE_SEPARATOR, $defVal) .
+            CRM_Core_DAO::VALUE_SEPARATOR;
           $customField->save();
         }
       }
@@ -449,7 +475,10 @@ SELECT count(*)
           }
         }
 
-        $customField->default_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $tempVal) . CRM_Core_DAO::VALUE_SEPARATOR;
+        $customField->default_value =
+          CRM_Core_DAO::VALUE_SEPARATOR .
+          implode(CRM_Core_DAO::VALUE_SEPARATOR, $tempVal) .
+          CRM_Core_DAO::VALUE_SEPARATOR;
         $customField->save();
       }
     }
@@ -481,14 +510,18 @@ SELECT count(*)
 
     $customOption->save();
 
-    CRM_Core_Session::setStatus(ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label)), '', 'success');
+    $msg = ts('Your multiple choice option \'%1\' has been saved', array(1 => $customOption->label));
+    CRM_Core_Session::setStatus($msg, '', 'success');
     $buttonName = $this->controller->getButtonName();
     $session = CRM_Core_Session::singleton();
     if ($buttonName == $this->getButtonName('next', 'new')) {
-      CRM_Core_Session::setStatus(ts(' You can add another option.'), '', 'info');
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
+      CRM_Core_Session::setStatus(ts('You can add another option.'), '', 'info');
+      $session->replaceUserContext(
+        CRM_Utils_System::url(
+          'civicrm/admin/custom/group/field/option',
           'reset=1&action=add&fid=' . $this->_fid . '&gid=' . $this->_gid
-        ));
+        )
+      );
     }
   }
 }