/** * Correct duplicate weight entries by putting them (duplicate weights) in sequence. * * @param string $daoName * Full name of the DAO. * @param array $fieldValues * Field => value to be used in the WHERE. * @param string $weightField * Field which contains the weight value,. * defaults to 'weight' * * @return bool */ public static function correctDuplicateWeights($daoName, $fieldValues = NULL, $weightField = 'weight') { $selectField = "MIN(id) AS dupeId, count(id) as dupeCount, {$weightField} as dupeWeight"; $groupBy = "{$weightField} having dupeCount>1"; $minDupeID = CRM_Utils_Weight::query('SELECT', $daoName, $fieldValues, $selectField, NULL, NULL, $groupBy); // return early if query returned empty // CRM-8043 if (!$minDupeID->fetch()) { return TRUE; } if ($minDupeID->dupeId) { $additionalWhere = "id !=" . $minDupeID->dupeId . " AND {$weightField} >= " . $minDupeID->dupeWeight; $update = "{$weightField} = {$weightField} + 1"; $status = CRM_Utils_Weight::query('UPDATE', $daoName, $fieldValues, $update, $additionalWhere); } if ($minDupeID->dupeId && $status) { //recursive call to correct all duplicate weight entries. return CRM_Utils_Weight::correctDuplicateWeights($daoName, $fieldValues, $weightField); } elseif (!$minDupeID->dupeId) { // case when no duplicate records are found. return TRUE; } elseif (!$status) { // case when duplicate records are found but update status is false. return FALSE; } }
/** * Save the PDF Page Format in the DB. * * @param array (reference) $values associative array of name/value pairs * @param int $id * Id of the database record (null = new record). * * @return void */ public function savePdfFormat(&$values, $id = NULL) { // get the Option Group ID for PDF Page Formats (create one if it doesn't exist) $group_id = self::_getGid(); // clear other default if this is the new default PDF Page Format if ($values['is_default']) { $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = {$group_id}"; CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); } if ($id) { // fetch existing record $this->id = $id; if ($this->find()) { $this->fetch(); } } // copy the supplied form values to the corresponding Option Value fields in the base class foreach ($this->fields() as $name => $field) { $this->{$name} = trim(CRM_Utils_Array::value($name, $values, $this->{$name})); if (empty($this->{$name})) { $this->{$name} = 'null'; } } $this->id = $id; $this->option_group_id = $group_id; $this->label = $this->name; $this->is_active = 1; // serialize PDF Page Format fields into a single string to store in the 'value' column of the Option Value table $v = json_decode($this->value, TRUE); foreach (self::$optionValueFields as $name => $field) { $v[$name] = self::getValue($name, $values, CRM_Utils_Array::value($name, $v)); } $this->value = json_encode($v); // make sure serialized array will fit in the 'value' column $attribute = CRM_Core_DAO::getAttribute('CRM_Core_BAO_PdfFormat', 'value'); if (strlen($this->value) > $attribute['maxlength']) { CRM_Core_Error::fatal(ts('PDF Page Format does not fit in database.')); } $this->save(); // fix duplicate weights $filter = array('option_group_id' => self::_getGid()); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter); }
/** * Save the Label Format in the DB * * @param array (reference) $values associative array of name/value pairs * @param int $id id of the database record (null = new record) * @param string $groupName group name of the label format * * @return void * @access public */ function saveLabelFormat(&$values, $id = NULL, $groupName = 'label_format') { // get the Option Group ID for Label Formats (create one if it doesn't exist) $group_id = self::_getGid($groupName); // clear other default if this is the new default label format if ($values['is_default']) { $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = {$group_id}"; CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); } if ($id) { // fetch existing record $this->id = $id; if ($this->find()) { $this->fetch(); } } else { // new record $list = self::getList(TRUE, $groupName); $cnt = 1; while (array_key_exists("custom_{$cnt}", $list)) { $cnt++; } $values['name'] = "custom_{$cnt}"; $values['grouping'] = self::customGroupName(); } // copy the supplied form values to the corresponding Option Value fields in the base class foreach ($this->fields() as $name => $field) { $this->{$name} = trim(CRM_Utils_Array::value($name, $values, $this->{$name})); if (empty($this->{$name})) { $this->{$name} = 'null'; } } $this->id = $id; $this->option_group_id = $group_id; $this->is_active = 1; // serialize label format fields into a single string to store in the 'value' column of the Option Value table $v = json_decode($this->value, TRUE); foreach (self::$optionValueFields as $name => $field) { if (!isset($v[$name])) { $v[$name] = NULL; } $v[$name] = self::getValue($name, $values, $v[$name]); } $this->value = json_encode($v); // make sure serialized array will fit in the 'value' column $attribute = CRM_Core_DAO::getAttribute('CRM_Core_BAO_LabelFormat', 'value'); if (strlen($this->value) > $attribute['maxlength']) { CRM_Core_Error::fatal(ts('Label Format does not fit in database.')); } $this->save(); // fix duplicate weights $filter = array('option_group_id' => self::_getGid()); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter); }
/** * Process the form submission. */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { $fieldValues = array('option_group_id' => $this->_gid); $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); if (CRM_Core_BAO_OptionValue::del($this->_id)) { if ($this->_gName == 'phone_type') { CRM_Core_BAO_Phone::setOptionToNull(CRM_Utils_Array::value('value', $this->_defaultValues)); } CRM_Core_Session::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_gLabel)), ts('Record Deleted'), 'success'); } else { CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_gLabel)), ts('Sorry'), 'error'); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); } } else { $params = $ids = array(); $params = $this->exportValues(); // allow multiple defaults within group. $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address'); if (in_array($this->_gName, $allowMultiDefaults)) { if ($this->_gName == 'from_email_address') { $params['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID()); } elseif ($filter = CRM_Utils_Array::value('contactOptions', $params)) { $params['filter'] = $filter; $params['reset_default_for'] = array('filter' => "0, " . $params['filter']); } //make sure we should has to have space, CRM-6977 if ($this->_gName == 'from_email_address') { $params['label'] = str_replace('"<', '" <', $params['label']); } } // set value of filter if not present in params if ($this->_id && !array_key_exists('filter', $params)) { if ($this->_gName == 'participant_role') { $params['filter'] = 0; } else { $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id'); } } $groupParams = array('name' => $this->_gName); $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id); // CRM-11516 if (!empty($params['financial_account_id'])) { $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); $params = array('entity_table' => 'civicrm_option_value', 'entity_id' => $optionValue->id, 'account_relationship' => $relationTypeId, 'financial_account_id' => $params['financial_account_id']); CRM_Financial_BAO_FinancialTypeAccount::add($params); } CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_gLabel, 2 => $optionValue->label)), ts('Saved'), 'success'); } }
/** * Delete the Custom Field. * * @param object $field * The field object. */ public static function deleteField($field) { CRM_Utils_System::flushCache(); // first delete the custom option group and values associated with this field if ($field->option_group_id) { //check if option group is related to any other field, if //not delete the option group and related option values self::checkOptionGroup($field->option_group_id); } // next drop the column from the custom value table self::createField($field, 'delete'); $field->delete(); CRM_Core_BAO_UFField::delUFField($field->id); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField'); }
/** * Function to process the form * * @access public * * @return None */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { if (CRM_Core_BAO_OptionValue::del($this->_id)) { CRM_Core_Session::setStatus(ts('Selected %1 Report has been deleted.', array(1 => $this->_GName))); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1")); } else { CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_GName))); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); } } else { // get the submitted form values. $params = $this->controller->exportValues($this->_name); $ids = array(); $groupParams = array('name' => 'report_template'); $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id); CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => 'Report Template', 2 => $optionValue->label))); CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/report/options/report_template', "reset=1")); } }
/** * Process the form when submitted * * @param null * * @return void * @access public */ public function postProcess() { $field =& new CRM_Core_DAO_CustomField(); $field->id = $this->_id; $field->find(true); CRM_Core_BAO_CustomField::deleteField($field); // also delete any profiles associted with this custom field require_once "CRM/Core/BAO/UFField.php"; CRM_Core_BAO_UFField::delUFField($this->_id); CRM_Core_Session::setStatus(ts('The custom field \'%1\' has been deleted.', array(1 => $field->label))); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField'); }
/** * Function to process the form * * @access public * @return None */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { $fieldValues = array('option_group_id' => $this->_gid); $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); if (CRM_Core_BAO_OptionValue::del($this->_id)) { if ($this->_gName == 'phone_type') { require_once 'CRM/Core/BAO/Phone.php'; CRM_Core_BAO_Phone::setOptionToNull(CRM_Utils_Array::value('value', $this->_defaultValues)); } CRM_Core_Session::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_GName))); } else { CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_GName))); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); } } else { $params = $ids = array(); $params = $this->exportValues(); // allow multiple defaults within group. $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address'); if (CRM_Utils_Array::value('is_default', $params) && in_array($this->_gName, $allowMultiDefaults)) { if ($this->_gName == 'from_email_address') { $params['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID()); } else { if ($filter = CRM_Utils_Array::value('contactOptions', $params)) { $params['filter'] = $filter; $params['reset_default_for'] = array('filter' => "0, " . $params['filter']); } } } $groupParams = array('name' => $this->_gName); require_once 'CRM/Core/OptionValue.php'; $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id); CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_GName, 2 => $optionValue->label))); } }
/** * Function to process the form * * @access public * @return None */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { $fieldValues = array('option_group_id' => $this->_gid); $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); if (CRM_Core_BAO_OptionValue::del($this->_id)) { if ($this->_gName == 'phone_type') { require_once 'CRM/Core/BAO/Phone.php'; CRM_Core_BAO_Phone::setOptionToNull(CRM_Utils_Array::value('value', $this->_defaultValues)); } CRM_Core_Session::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_GName))); } else { CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_GName))); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); } } else { $params = $ids = array(); $params = $this->exportValues(); //set defaultGreeting option in params as per contact type if (CRM_Utils_Array::value('contactOptions', $params)) { $params['filter'] = CRM_Utils_Array::value('contactOptions', $params); $params['defaultGreeting'] = 1; } $groupParams = array('name' => $this->_gName); require_once 'CRM/Core/OptionValue.php'; $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id); CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_GName, 2 => $optionValue->label))); } }
/** * Function to process the form * * @access public * * @return None */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { $fieldValues = array('option_group_id' => $this->_gid); $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); if (CRM_Core_BAO_OptionValue::del($this->_id)) { if ($this->_gName == 'phone_type') { CRM_Core_BAO_Phone::setOptionToNull(CRM_Utils_Array::value('value', $this->_defaultValues)); } CRM_Core_Session::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_GName))); } else { CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_GName))); CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); } } else { $params = $ids = array(); $params = $this->exportValues(); // allow multiple defaults within group. $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address'); if (in_array($this->_gName, $allowMultiDefaults)) { if ($this->_gName == 'from_email_address') { $params['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID()); } elseif ($filter = CRM_Utils_Array::value('contactOptions', $params)) { $params['filter'] = $filter; $params['reset_default_for'] = array('filter' => "0, " . $params['filter']); } //make sure we should has to have space, CRM-6977 if ($this->_gName == 'from_email_address') { $params['label'] = str_replace('"<', '" <', $params['label']); } } // set db value of filter in params if filter is non editable if ($this->_id && !array_key_exists('filter', $params) && !$this->_gName == 'participant_role') { $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id'); } $groupParams = array('name' => $this->_gName); $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id); CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_GName, 2 => $optionValue->label))); } }