Exemplo n.º 1
0
 /**
  * takes an associative array and creates a price field object
  *
  * This function is invoked from within the web form layer and also from the api layer
  *
  * @param array $params (reference) an assoc array of name/value pairs
  *
  * @return object CRM_Price_DAO_Field object
  * @access public
  * @static
  */
 static function create(&$params, $ids)
 {
     require_once 'CRM/Core/Transaction.php';
     require_once 'CRM/Price/BAO/FieldValue.php';
     $transaction = new CRM_Core_Transaction();
     $priceField =& self::add($params, $ids);
     if (is_a($priceField, 'CRM_Core_Error')) {
         $transaction->rollback();
         return $priceField;
     }
     $options = $optionsIds = array();
     require_once 'CRM/Price/Form/Field.php';
     $maxIndex = CRM_Price_Form_Field::NUM_OPTION;
     if ($priceField->html_type == 'Text') {
         $maxIndex = 1;
         require_once 'CRM/Price/BAO/FieldValue.php';
         $fieldValue = new CRM_Price_DAO_FieldValue();
         $fieldValue->price_field_id = $priceField->id;
         // update previous field values( if any )
         if ($fieldValue->find(true)) {
             $optionsIds['id'] = $fieldValue->id;
         }
     }
     $defaultArray = array();
     if ($params['html_type'] == 'CheckBox' && isset($params['default_checkbox_option'])) {
         $tempArray = array_keys($params['default_checkbox_option']);
         foreach ($tempArray as $v) {
             if ($params['option_amount'][$v]) {
                 $defaultArray[$v] = 1;
             }
         }
     } else {
         if (CRM_Utils_Array::value('default_option', $params) && isset($params['option_amount'][$params['default_option']])) {
             $defaultArray[$params['default_option']] = 1;
         }
     }
     for ($index = 1; $index <= $maxIndex; $index++) {
         if (CRM_Utils_Array::value($index, $params['option_label']) && !CRM_Utils_System::isNull($params['option_amount'][$index])) {
             $options = array('price_field_id' => $priceField->id, 'label' => trim($params['option_label'][$index]), 'name' => CRM_Utils_String::munge($params['option_label'][$index], '_', 64), 'amount' => CRM_Utils_Rule::cleanMoney(trim($params['option_amount'][$index])), 'count' => CRM_Utils_Array::value($index, $params['option_count'], null), 'max_value' => CRM_Utils_Array::value($index, $params['option_max_value'], null), 'description' => CRM_Utils_Array::value($index, $params['option_description'], null), 'weight' => $params['option_weight'][$index], 'is_active' => 1, 'is_default' => CRM_Utils_Array::value($index, $defaultArray));
             CRM_Price_BAO_FieldValue::add($options, $optionsIds);
         }
     }
     $transaction->commit();
     return $priceField;
 }
 function upgrade_3_3_beta1($rev)
 {
     $upgrade = new CRM_Upgrade_Form();
     $upgrade->processSQL($rev);
     // CRM-6902
     // Add column price_field_value_id in civicrm_line_item.
     // Do not drop option_group_id column now since we need it to
     // update line items.
     $updateLineItem1 = "ALTER TABLE civicrm_line_item ADD COLUMN price_field_value_id int(10) unsigned default NULL;";
     CRM_Core_DAO::executeQuery($updateLineItem1);
     $priceFieldDAO = new CRM_Price_DAO_Field();
     $priceFieldDAO->find();
     $ids = array();
     while ($priceFieldDAO->fetch()) {
         $opGroupDAO = new CRM_Core_DAO_OptionGroup();
         $opGroupDAO->name = 'civicrm_price_field.amount.' . $priceFieldDAO->id;
         if (!$opGroupDAO->find(TRUE)) {
             $opGroupDAO->free();
             continue;
         }
         $opValueDAO = new CRM_Core_DAO_OptionValue();
         $opValueDAO->option_group_id = $opGroupDAO->id;
         $opValueDAO->find();
         while ($opValueDAO->fetch()) {
             // FIX ME: not migrating description(?), there will
             // be a field description for each option.
             $fieldValue = array('price_field_id' => $priceFieldDAO->id, 'label' => $opValueDAO->label, 'name' => CRM_Utils_String::munge($opValueDAO->label, '_', 64), 'amount' => $opValueDAO->name, 'weight' => $opValueDAO->weight, 'is_default' => $opValueDAO->is_default, 'is_active' => $opValueDAO->is_active);
             if ($priceFieldDAO->count) {
                 // Migrate Participant Counts on option level.
                 // count of each option will be the same
                 // as earlier field count.
                 $fieldValue['count'] = $priceFieldDAO->count;
             }
             $fieldValueDAO = CRM_Price_BAO_FieldValue::add($fieldValue, $ids);
             $lineItemDAO = new CRM_Price_DAO_LineItem();
             $lineItemDAO->option_group_id = $opGroupDAO->id;
             $lineItemDAO->label = $opValueDAO->label;
             $lineItemDAO->unit_price = $opValueDAO->name;
             $labelFound = $priceFound = FALSE;
             // check with label and amount
             if (!$lineItemDAO->find(TRUE)) {
                 $lineItemDAO->free();
                 $lineItemDAO = new CRM_Price_DAO_LineItem();
                 $lineItemDAO->option_group_id = $opGroupDAO->id;
                 $lineItemDAO->label = $opValueDAO->label;
                 // check with label only
                 if ($lineItemDAO->find(TRUE)) {
                     $labelFound = TRUE;
                 }
             } else {
                 $labelFound = TRUE;
                 $priceFound = TRUE;
             }
             $lineItemDAO->free();
             // update civicrm_line_item for price_field_value_id.
             // Used query to avoid line by line update.
             if ($labelFound || $priceFound) {
                 $lineItemParams = array(1 => array($fieldValueDAO->id, 'Integer'), 2 => array($opValueDAO->label, 'String'));
                 $updateLineItems = "UPDATE civicrm_line_item SET price_field_value_id = %1 WHERE label = %2";
                 if ($priceFound) {
                     $lineItemParams[3] = array($opValueDAO->name, 'Float');
                     $updateLineItems .= " AND unit_price = %3";
                 }
                 CRM_Core_DAO::executeQuery($updateLineItems, $lineItemParams);
             }
         }
         $opGroupDAO->delete();
         $opValueDAO->free();
         $opGroupDAO->free();
     }
     $priceFieldDAO->free();
     // Now drop option_group_id column from civicrm_line_item
     $updateLineItem2 = "ALTER TABLE civicrm_line_item DROP option_group_id,\n                           ADD CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (price_field_value_id) REFERENCES civicrm_price_field_value(id) ON DELETE SET NULL;";
     CRM_Core_DAO::executeQuery($updateLineItem2, array(), TRUE, NULL, FALSE, FALSE);
     $updatePriceField = "ALTER TABLE civicrm_price_field DROP count";
     CRM_Core_DAO::executeQuery($updatePriceField, array(), TRUE, NULL, FALSE, FALSE);
     // as the table 'civicrm_price_field' is localised and column 'count' is dropped
     // after the views are rebuild, we need to rebuild views to avoid invalid refrence of table.
     if ($upgrade->multilingual) {
         CRM_Core_I18n_Schema::rebuildMultilingualSchema($upgrade->locales, $rev);
     }
 }
 function createMembershipPriceField($params, $ids, $previousID, $membershipTypeId)
 {
     $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', 'default_membership_type_amount', 'id', 'name');
     if (CRM_Utils_Array::value('memberOfContact', $ids)) {
         $fieldName = $ids['memberOfContact'];
     } else {
         $fieldName = $previousID;
     }
     $fieldLabel = 'Membership Amount';
     $optionsIds = NULL;
     $fieldParams = array('price_set_id ' => $priceSetId, 'name' => $fieldName);
     $results = array();
     CRM_Price_BAO_Field::retrieve($fieldParams, $results);
     if (empty($results)) {
         $fieldParams = array();
         $fieldParams['label'] = $fieldLabel;
         $fieldParams['name'] = $fieldName;
         $fieldParams['price_set_id'] = $priceSetId;
         $fieldParams['html_type'] = 'Radio';
         $fieldParams['is_display_amounts'] = $fieldParams['is_required'] = 0;
         $fieldParams['weight'] = $fieldParams['option_weight'][1] = 1;
         $fieldParams['option_label'][1] = $params['name'];
         $fieldParams['membership_type_id'][1] = $membershipTypeId;
         $fieldParams['option_amount'][1] = empty($params['minimum_fee']) ? 0 : $params['minimum_fee'];
         if ($previousID) {
             CRM_Member_Form_MembershipType::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds);
             $fieldParams['option_id'] = CRM_Utils_Array::value('option_id', $optionsIds);
         }
         $priceField = CRM_Price_BAO_Field::create($fieldParams);
     } else {
         $fieldID = $results['id'];
         $fieldValueParams = array('price_field_id' => $fieldID, 'membership_type_id' => $membershipTypeId);
         $results = array();
         CRM_Price_BAO_FieldValue::retrieve($fieldValueParams, $results);
         if (!empty($results)) {
             $results['label'] = $results['name'] = $params['name'];
             $results['amount'] = empty($params['minimum_fee']) ? 0 : $params['minimum_fee'];
             $optionsIds['id'] = $results['id'];
         } else {
             $results = array('price_field_id' => $fieldID, 'name' => $params['name'], 'label' => $params['name'], 'amount' => empty($params['minimum_fee']) ? 0 : $params['minimum_fee'], 'membership_type_id' => $membershipTypeId, 'is_active' => 1);
         }
         if ($previousID) {
             CRM_Member_Form_MembershipType::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds);
             if (CRM_Utils_Array::value('option_id', $optionsIds)) {
                 $optionsIds['id'] = current(CRM_Utils_Array::value('option_id', $optionsIds));
             }
         }
         CRM_Price_BAO_FieldValue::add($results, $optionsIds);
     }
 }