Пример #1
0
 /**
  * @param array $customParams
  *
  * @throws Exception
  */
 public static function create(&$customParams)
 {
     if (empty($customParams) || !is_array($customParams)) {
         return;
     }
     foreach ($customParams as $tableName => $tables) {
         foreach ($tables as $index => $fields) {
             $sqlOP = NULL;
             $hookID = NULL;
             $hookOP = NULL;
             $entityID = NULL;
             $isMultiple = FALSE;
             $set = array();
             $params = array();
             $count = 1;
             foreach ($fields as $field) {
                 if (!$sqlOP) {
                     $entityID = $field['entity_id'];
                     $hookID = $field['custom_group_id'];
                     $isMultiple = $field['is_multiple'];
                     if (array_key_exists('id', $field)) {
                         $sqlOP = "UPDATE {$tableName} ";
                         $where = " WHERE  id = %{$count}";
                         $params[$count] = array($field['id'], 'Integer');
                         $count++;
                         $hookOP = 'edit';
                     } else {
                         $sqlOP = "INSERT INTO {$tableName} ";
                         $where = NULL;
                         $hookOP = 'create';
                     }
                 }
                 // fix the value before we store it
                 $value = $field['value'];
                 $type = $field['type'];
                 switch ($type) {
                     case 'StateProvince':
                         $type = 'Integer';
                         if (is_array($value)) {
                             $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR;
                             $type = 'String';
                         } elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
                             //fix for multi select state, CRM-3437
                             $mulValues = explode(',', $value);
                             $validStates = array();
                             foreach ($mulValues as $key => $stateVal) {
                                 $states = array();
                                 $states['state_province'] = trim($stateVal);
                                 CRM_Utils_Array::lookupValue($states, 'state_province', CRM_Core_PseudoConstant::stateProvince(), TRUE);
                                 if (empty($states['state_province_id'])) {
                                     CRM_Utils_Array::lookupValue($states, 'state_province', CRM_Core_PseudoConstant::stateProvinceAbbreviation(), TRUE);
                                 }
                                 $validStates[] = CRM_Utils_Array::value('state_province_id', $states);
                             }
                             $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, $validStates);
                             $type = 'String';
                         } elseif (!$value) {
                             // CRM-3415
                             // using type of timestamp allows us to sneak in a null into db
                             // gross but effective hack
                             $value = NULL;
                             $type = 'Timestamp';
                         } else {
                             $type = 'String';
                         }
                         break;
                     case 'Country':
                         $type = 'Integer';
                         $mulValues = explode(',', $value);
                         if (is_array($value)) {
                             $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR;
                             $type = 'String';
                         } elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) {
                             //fix for multi select country, CRM-3437
                             $mulValues = explode(',', $value);
                             $validCountries = array();
                             foreach ($mulValues as $key => $countryVal) {
                                 $countries = array();
                                 $countries['country'] = trim($countryVal);
                                 CRM_Utils_Array::lookupValue($countries, 'country', CRM_Core_PseudoConstant::country(), TRUE);
                                 if (empty($countries['country_id'])) {
                                     CRM_Utils_Array::lookupValue($countries, 'country', CRM_Core_PseudoConstant::countryIsoCode(), TRUE);
                                 }
                                 $validCountries[] = CRM_Utils_Array::value('country_id', $countries);
                             }
                             $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, $validCountries);
                             $type = 'String';
                         } elseif (!$value) {
                             // CRM-3415
                             // using type of timestamp allows us to sneak in a null into db
                             // gross but effective hack
                             $value = NULL;
                             $type = 'Timestamp';
                         } else {
                             $type = 'String';
                         }
                         break;
                     case 'File':
                         if (!$field['file_id']) {
                             CRM_Core_Error::fatal();
                         }
                         // need to add/update civicrm_entity_file
                         $entityFileDAO = new CRM_Core_DAO_EntityFile();
                         $entityFileDAO->file_id = $field['file_id'];
                         $entityFileDAO->find(TRUE);
                         $entityFileDAO->entity_table = $field['table_name'];
                         $entityFileDAO->entity_id = $field['entity_id'];
                         $entityFileDAO->file_id = $field['file_id'];
                         $entityFileDAO->save();
                         $entityFileDAO->free();
                         $value = $field['file_id'];
                         $type = 'String';
                         break;
                     case 'Date':
                         $value = CRM_Utils_Date::isoToMysql($value);
                         break;
                     case 'Int':
                         if (is_numeric($value)) {
                             $type = 'Integer';
                         } else {
                             $type = 'Timestamp';
                         }
                         break;
                     case 'ContactReference':
                         if ($value == NULL) {
                             $type = 'Timestamp';
                         } else {
                             $type = 'Integer';
                         }
                         break;
                     case 'RichTextEditor':
                         $type = 'String';
                         break;
                     case 'Boolean':
                         //fix for CRM-3290
                         $value = CRM_Utils_String::strtoboolstr($value);
                         if ($value === FALSE) {
                             $type = 'Timestamp';
                         }
                         break;
                     default:
                         break;
                 }
                 if (strtolower($value) === "null") {
                     // when unsetting a value to null, we don't need to validate the type
                     // https://projectllr.atlassian.net/browse/VGQBMP-20
                     $set[$field['column_name']] = $value;
                 } else {
                     $set[$field['column_name']] = "%{$count}";
                     $params[$count] = array($value, $type);
                     $count++;
                 }
             }
             if (!empty($set)) {
                 $setClause = array();
                 foreach ($set as $n => $v) {
                     $setClause[] = "{$n} = {$v}";
                 }
                 $setClause = implode(',', $setClause);
                 if (!$where) {
                     // do this only for insert
                     $set['entity_id'] = "%{$count}";
                     $params[$count] = array($entityID, 'Integer');
                     $count++;
                     $fieldNames = implode(',', array_keys($set));
                     $fieldValues = implode(',', array_values($set));
                     $query = "{$sqlOP} ( {$fieldNames} ) VALUES ( {$fieldValues} )";
                     // for multiple values we dont do on duplicate key update
                     if (!$isMultiple) {
                         $query .= " ON DUPLICATE KEY UPDATE {$setClause}";
                     }
                 } else {
                     $query = "{$sqlOP} SET {$setClause} {$where}";
                 }
                 $dao = CRM_Core_DAO::executeQuery($query, $params);
                 CRM_Utils_Hook::custom($hookOP, $hookID, $entityID, $fields);
             }
         }
     }
 }
Пример #2
0
 /**
  * Delete option value give an option value and custom group id.
  *
  * @param int $customValueID
  *   Custom value ID.
  * @param int $customGroupID
  *   Custom group ID.
  *
  * @return void
  */
 public static function deleteCustomValue($customValueID, $customGroupID)
 {
     // first we need to find custom value table, from custom group ID
     $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name');
     // delete custom value from corresponding custom value table
     $sql = "DELETE FROM {$tableName} WHERE id = {$customValueID}";
     CRM_Core_DAO::executeQuery($sql);
     CRM_Utils_Hook::custom('delete', $customGroupID, NULL, $customValueID);
 }