/** * Creates a new option group with the passed in values. * @TODO: Should update the group if it already exists intelligently, so multi-lingual is * not messed up. Currently deletes the old group * * @param string $groupName * The name of the option group - make sure there is no conflict. * @param array $values * The associative array that has information on the option values. * the keys of this array are: * string 'title' (required) * string 'value' (required) * string 'name' (optional) * string 'description' (optional) * int 'weight' (optional) - the order in which the value are displayed * bool 'is_default' (optional) - is this the default one to display when rendered in form * bool 'is_active' (optional) - should this element be rendered * @param int $defaultID * (reference) - the option value ID of the default element (if set) is returned else 'null'. * @param null $groupTitle * The optional label of the option group else set to group name. * * * @return int * the option group ID */ public static function createAssoc($groupName, &$values, &$defaultID, $groupTitle = NULL) { self::deleteAssoc($groupName); if (!empty($values)) { $group = new CRM_Core_DAO_OptionGroup(); $group->name = $groupName; $group->title = empty($groupTitle) ? $groupName : $groupTitle; $group->is_reserved = 1; $group->is_active = 1; $group->save(); foreach ($values as $v) { $value = new CRM_Core_DAO_OptionValue(); $value->option_group_id = $group->id; $value->label = $v['label']; $value->value = $v['value']; $value->name = CRM_Utils_Array::value('name', $v); $value->description = CRM_Utils_Array::value('description', $v); $value->weight = CRM_Utils_Array::value('weight', $v); $value->is_default = CRM_Utils_Array::value('is_default', $v); $value->is_active = CRM_Utils_Array::value('is_active', $v); $value->save(); if ($value->is_default) { $defaultID = $value->id; } } } else { return $defaultID = 'null'; } return $group->id; }
/** * Takes an associative array and creates a custom 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 CRM_Core_DAO_CustomField */ public static function create(&$params) { $origParams = array_merge(array(), $params); if (!isset($params['id'])) { if (!isset($params['column_name'])) { // if add mode & column_name not present, calculate it. $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); } if (!isset($params['name'])) { $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); } } else { $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name'); } $columnName = $params['column_name']; $indexExist = FALSE; //as during create if field is_searchable we had created index. if (!empty($params['id'])) { $indexExist = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable'); } switch (CRM_Utils_Array::value('html_type', $params)) { case 'Select Date': if (empty($params['date_format'])) { $config = CRM_Core_Config::singleton(); $params['date_format'] = $config->dateInputFormat; } break; case 'CheckBox': case 'AdvMulti-Select': case 'Multi-Select': if (isset($params['default_checkbox_option'])) { $tempArray = array_keys($params['default_checkbox_option']); $defaultArray = array(); foreach ($tempArray as $k => $v) { if ($params['option_value'][$v]) { $defaultArray[] = $params['option_value'][$v]; } } if (!empty($defaultArray)) { // also add the separator before and after the value per new convention (CRM-1604) $params['default_value'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultArray) . CRM_Core_DAO::VALUE_SEPARATOR; } } else { if (!empty($params['default_option']) && isset($params['option_value'][$params['default_option']])) { $params['default_value'] = $params['option_value'][$params['default_option']]; } } break; } $transaction = new CRM_Core_Transaction(); // create any option group & values if required if ($params['html_type'] != 'Text' && in_array($params['data_type'], array('String', 'Int', 'Float', 'Money'))) { $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['custom_group_id'], 'table_name'); //CRM-16659: if option_value then create an option group for this custom field. if ($params['option_type'] == 1 && (empty($params['option_group_id']) || !empty($params['option_value']))) { // first create an option group for this custom group $optionGroup = new CRM_Core_DAO_OptionGroup(); $optionGroup->name = "{$columnName}_" . date('YmdHis'); $optionGroup->title = $params['label']; $optionGroup->is_active = 1; $optionGroup->save(); $params['option_group_id'] = $optionGroup->id; if (!empty($params['option_value']) && is_array($params['option_value'])) { foreach ($params['option_value'] as $k => $v) { if (strlen(trim($v))) { $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->option_group_id = $optionGroup->id; $optionValue->label = $params['option_label'][$k]; $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); switch ($params['data_type']) { case 'Money': $optionValue->value = CRM_Utils_Rule::cleanMoney($v); break; case 'Int': $optionValue->value = intval($v); break; case 'Float': $optionValue->value = floatval($v); break; default: $optionValue->value = trim($v); } $optionValue->weight = $params['option_weight'][$k]; $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], FALSE); $optionValue->save(); } } } } } // check for orphan option groups if (!empty($params['option_group_id'])) { if (!empty($params['id'])) { self::fixOptionGroups($params['id'], $params['option_group_id']); } // if we do not have a default value // retrieve it from one of the other custom fields which use this option group if (empty($params['default_value'])) { //don't insert only value separator as default value, CRM-4579 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], $params['html_type']); if (!CRM_Utils_System::isNull(explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaultValue))) { $params['default_value'] = $defaultValue; } } } // since we need to save option group id :) if (!isset($params['attributes']) && strtolower($params['html_type']) == 'textarea') { $params['attributes'] = 'rows=4, cols=60'; } $customField = new CRM_Core_DAO_CustomField(); $customField->copyValues($params); $customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE); $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, FALSE); $customField->in_selector = CRM_Utils_Array::value('in_selector', $params, FALSE); $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, FALSE); //CRM-15792 - Custom field gets disabled if is_active not set $customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE); $customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE); $customField->save(); // make sure all values are present in the object for further processing $customField->find(TRUE); $triggerRebuild = CRM_Utils_Array::value('triggerRebuild', $params, TRUE); //create/drop the index when we toggle the is_searchable flag if (!empty($params['id'])) { self::createField($customField, 'modify', $indexExist, $triggerRebuild); } else { if (!isset($origParams['column_name'])) { $columnName .= "_{$customField->id}"; $params['column_name'] = $columnName; } $customField->column_name = $columnName; $customField->save(); // make sure all values are present in the object $customField->find(TRUE); $indexExist = FALSE; self::createField($customField, 'add', $indexExist, $triggerRebuild); } // complete transaction $transaction->commit(); CRM_Utils_System::flushCache(); return $customField; }
/** * function to add the Option Group * * @param array $params reference array contains the values submitted by the form * @param array $ids reference array contains the id * * @access public * @static * * @return object */ static function add(&$params, $ids = array()) { if (empty($params['id'])) { $params['id'] = CRM_Utils_Array::value('optionGroup', $ids); } $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); // action is taken depending upon the mode $optionGroup = new CRM_Core_DAO_OptionGroup(); $optionGroup->copyValues($params); if ($params['is_default']) { $query = "UPDATE civicrm_option_group SET is_default = 0"; CRM_Core_DAO::executeQuery($query); } $optionGroup->save(); return $optionGroup; }
/** * Process the form * * @param null * * @return void * @access public */ public function postProcess() { // store the submitted values in an array $params = $this->controller->exportValues($this->_name); $session = CRM_Core_Session::singleton(); $params['last_modified_id'] = $session->get('userID'); $params['last_modified_date'] = date('YmdHis'); require_once 'CRM/Core/BAO/OptionValue.php'; require_once 'CRM/Core/BAO/OptionGroup.php'; $updateResultSet = false; if (CRM_Utils_Array::value('option_type', $params) == 2 && CRM_Utils_Array::value('option_group_id', $params)) { if ($params['option_group_id'] == CRM_Utils_Array::value('result_id', $this->_values)) { $updateResultSet = true; } } if ($this->_surveyId) { if ($this->_action & CRM_Core_Action::DELETE) { CRM_Campaign_BAO_Survey::del($this->_surveyId); CRM_Core_Session::setStatus(ts(' Survey has been deleted.')); $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey')); return; } $params['id'] = $this->_surveyId; } else { $params['created_id'] = $session->get('userID'); $params['created_date'] = date('YmdHis'); } $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0); $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0); $recontactInterval = array(); if ($updateResultSet) { $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->option_group_id = $this->_values['result_id']; $optionValue->delete(); $params['result_id'] = $this->_values['result_id']; } else { $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis'); $optionGroup = new CRM_Core_DAO_OptionGroup(); $optionGroup->name = $opGroupName; $optionGroup->label = $params['title'] . ' Response Set'; $optionGroup->is_active = 1; $optionGroup->save(); $params['result_id'] = $optionGroup->id; } foreach ($params['option_value'] as $k => $v) { if (strlen(trim($v))) { $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->option_group_id = $params['result_id']; $optionValue->label = $params['option_label'][$k]; $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); $optionValue->value = trim($v); $optionValue->weight = $params['option_weight'][$k]; $optionValue->is_active = 1; if (CRM_Utils_Array::value('default_option', $params) && $params['default_option'] == $k) { $optionValue->is_default = 1; } $optionValue->save(); if (CRM_Utils_Array::value($k, $params['option_interval'])) { $recontactInterval[$optionValue->label] = $params['option_interval'][$k]; } } } $params['recontact_interval'] = serialize($recontactInterval); $surveyId = CRM_Campaign_BAO_Survey::create($params); if (CRM_Utils_Array::value('result_id', $this->_values) && !$updateResultSet) { $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1"; $countSurvey = CRM_Core_DAO::singleValueQuery($query, array(1 => array($this->_values['result_id'], 'Integer'))); // delete option group if no any survey is using it. if (!($countSurvey >= 1)) { CRM_Core_BAO_OptionGroup::del($this->_values['result_id']); } } require_once 'CRM/Core/BAO/UFJoin.php'; // also update the ProfileModule tables $ufJoinParams = array('is_active' => 1, 'module' => 'CiviCampaign', 'entity_table' => 'civicrm_survey', 'entity_id' => $surveyId->id); // first delete all past entries if ($this->_surveyId) { CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams); } if (CRM_Utils_Array::value('profile_id', $params)) { $ufJoinParams['weight'] = 1; $ufJoinParams['uf_group_id'] = $params['profile_id']; CRM_Core_BAO_UFJoin::create($ufJoinParams); } if (!is_a($surveyId, 'CRM_Core_Error')) { CRM_Core_Session::setStatus(ts('Survey %1 has been saved.', array(1 => $params['title']))); } if ($this->_context == 'dialog') { $returnArray = array('returnSuccess' => true); echo json_encode($returnArray); CRM_Utils_System::civiExit(); } $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->getButtonName('next', 'new')) { CRM_Core_Session::setStatus(ts(' You can add another Survey.')); $session->replaceUserContext(CRM_Utils_System::url('civicrm/survey/add', 'reset=1&action=add')); } else { $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey')); } }
/** * Process the form. */ public function postProcess() { // store the submitted values in an array $status = ''; $params = $this->controller->exportValues($this->_name); $params['id'] = $this->_surveyId; $updateResultSet = FALSE; $resultSetOptGrpId = NULL; if (CRM_Utils_Array::value('option_type', $params) == 2 && !empty($params['option_group_id'])) { $updateResultSet = TRUE; $resultSetOptGrpId = $params['option_group_id']; } $recontactInterval = array(); if ($updateResultSet) { $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->option_group_id = $resultSetOptGrpId; $optionValue->delete(); $params['result_id'] = $resultSetOptGrpId; } else { $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis'); $optionGroup = new CRM_Core_DAO_OptionGroup(); $optionGroup->name = $opGroupName; $optionGroup->title = $this->_values['title'] . ' Result Set'; $optionGroup->is_active = 1; $optionGroup->save(); $params['result_id'] = $optionGroup->id; } foreach ($params['option_value'] as $k => $v) { if (strlen(trim($v))) { $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->option_group_id = $params['result_id']; $optionValue->label = $params['option_label'][$k]; $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); $optionValue->value = trim($v); $optionValue->weight = $params['option_weight'][$k]; $optionValue->is_active = 1; if (!empty($params['default_option']) && $params['default_option'] == $k) { $optionValue->is_default = 1; } $optionValue->save(); // using is_numeric since 0 is a valid value for option_interval if (is_numeric($params['option_interval'][$k])) { $recontactInterval[$optionValue->label] = $params['option_interval'][$k]; } } } $params['recontact_interval'] = serialize($recontactInterval); $survey = CRM_Campaign_BAO_Survey::create($params); // create report if required. if (!$this->_reportId && $survey->id && !empty($params['create_report'])) { $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); $activityStatus = array_flip($activityStatus); $this->_params = array('name' => "survey_{$survey->id}", 'title' => $params['report_title'] ? $params['report_title'] : $this->_values['title'], 'status_id_op' => 'eq', 'status_id_value' => $activityStatus['Scheduled'], 'survey_id_value' => array($survey->id), 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.')); //Default value of order by $this->_params['order_bys'] = array(1 => array('column' => 'sort_name', 'order' => 'ASC')); // for WalkList or default $displayFields = array('id', 'sort_name', 'result', 'street_number', 'street_name', 'street_unit', 'survey_response'); if (CRM_Core_OptionGroup::getValue('activity_type', 'WalkList') == $this->_values['activity_type_id']) { $this->_params['order_bys'] = array(1 => array('column' => 'street_name', 'order' => 'ASC'), 2 => array('column' => 'street_number_odd_even', 'order' => 'ASC'), 3 => array('column' => 'street_number', 'order' => 'ASC'), 4 => array('column' => 'sort_name', 'order' => 'ASC')); } elseif (CRM_Core_OptionGroup::getValue('activity_type', 'PhoneBank') == $this->_values['activity_type_id']) { array_push($displayFields, 'phone'); } elseif (CRM_Core_OptionGroup::getValue('activity_type', 'Survey') == $this->_values['activity_type_id'] || CRM_Core_OptionGroup::getValue('activity_type', 'Canvass') == $this->_values['activity_type_id']) { array_push($displayFields, 'phone', 'city', 'state_province_id', 'postal_code', 'email'); } foreach ($displayFields as $key) { $this->_params['fields'][$key] = 1; } $this->_createNew = TRUE; $this->_id = CRM_Report_Utils_Report::getInstanceIDForValue('survey/detail'); CRM_Report_Form_Instance::postProcess($this, FALSE); $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1"; $reportID = CRM_Core_DAO::singleValueQuery($query, array(1 => array("survey_{$survey->id}", 'String'))); if ($reportID) { $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1'); $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.", array(1 => $url, 2 => $this->_params['title'])); } } if ($status) { // reset status as we don't want status set by Instance::postProcess $session = CRM_Core_Session::singleton(); $session->getStatus(TRUE); // set new status CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); } parent::endPostProcess(); }
/** * takes an associative array and creates a custom 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_Core_DAO_CustomField object * @access public * @static */ static function create(&$params) { if (!isset($params['id']) && !isset($params['column_name'])) { // if add mode & column_name not present, calculate it. require_once 'CRM/Utils/String.php'; $params['column_name'] = strtolower(CRM_Utils_String::munge($params['label'], '_', 32)); $params['name'] = CRM_Utils_String::munge($params['label'], '_', 64); } else { if (isset($params['id'])) { $params['column_name'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'column_name'); } } $indexExist = false; //as during create if field is_searchable we had created index. if (CRM_Utils_Array::value('id', $params)) { $indexExist = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $params['id'], 'is_searchable'); } if (($params['html_type'] == 'CheckBox' || $params['html_type'] == 'AdvMulti-Select' || $params['html_type'] == 'Multi-Select') && isset($params['default_checkbox_option'])) { $tempArray = array_keys($params['default_checkbox_option']); $defaultArray = array(); foreach ($tempArray as $k => $v) { if ($params['option_value'][$v]) { $defaultArray[] = $params['option_value'][$v]; } } if (!empty($defaultArray)) { // also add the seperator before and after the value per new conventio (CRM-1604) $params['default_value'] = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $defaultArray) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR; } } else { if (CRM_Utils_Array::value('default_option', $params) && isset($params['option_value'][$params['default_option']])) { $params['default_value'] = $params['option_value'][$params['default_option']]; } } require_once 'CRM/Core/Transaction.php'; $transaction = new CRM_Core_Transaction(); // create any option group & values if required if ($params['html_type'] != 'Text' && in_array($params['data_type'], array('String', 'Int', 'Float', 'Money')) && !empty($params['option_value']) && is_array($params['option_value'])) { $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['custom_group_id'], 'table_name'); if ($params['option_type'] == 1) { // first create an option group for this custom group require_once 'CRM/Core/BAO/OptionGroup.php'; $optionGroup = new CRM_Core_DAO_OptionGroup(); $optionGroup->name = "{$params['column_name']}_" . date('YmdHis'); $optionGroup->label = $params['label']; $optionGroup->is_active = 1; $optionGroup->save(); $params['option_group_id'] = $optionGroup->id; require_once 'CRM/Core/BAO/OptionValue.php'; require_once 'CRM/Utils/String.php'; foreach ($params['option_value'] as $k => $v) { if (strlen(trim($v))) { $optionValue = new CRM_Core_DAO_OptionValue(); $optionValue->option_group_id = $optionGroup->id; $optionValue->label = $params['option_label'][$k]; $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); switch ($params['data_type']) { case 'Money': require_once 'CRM/Utils/Rule.php'; $optionValue->value = number_format(CRM_Utils_Rule::cleanMoney($v), 2); break; case 'Int': $optionValue->value = intval($v); break; case 'Float': $optionValue->value = floatval($v); break; default: $optionValue->value = trim($v); } $optionValue->weight = $params['option_weight'][$k]; $optionValue->is_active = CRM_Utils_Array::value($k, $params['option_status'], false); $optionValue->save(); } } } } // check for orphan option groups if (CRM_Utils_Array::value('option_group_id', $params)) { if (CRM_Utils_Array::value('id', $params)) { self::fixOptionGroups($params['id'], $params['option_group_id']); } // if we dont have a default value // retrive it from one of the other custom fields which use this option group if (!CRM_Utils_Array::value('default_value', $params)) { //don't insert only value separator as default value, CRM-4579 $defaultValue = self::getOptionGroupDefault($params['option_group_id'], $params['html_type']); if (!CRM_Utils_System::isNull(explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $defaultValue))) { $params['default_value'] = $defaultValue; } } } // since we need to save option group id :) if (!isset($params['attributes']) && strtolower($params['html_type']) == 'textarea') { $params['attributes'] = 'rows=4, cols=60'; } $customField = new CRM_Core_DAO_CustomField(); $customField->copyValues($params); $customField->is_required = CRM_Utils_Array::value('is_required', $params, false); $customField->is_searchable = CRM_Utils_Array::value('is_searchable', $params, false); $customField->is_search_range = CRM_Utils_Array::value('is_search_range', $params, false); $customField->is_active = CRM_Utils_Array::value('is_active', $params, false); $customField->is_view = CRM_Utils_Array::value('is_view', $params, false); $customField->save(); // make sure all values are present in the object for further processing $customField->find(true); //create/drop the index when we toggle the is_searchable flag if (CRM_Utils_Array::value('id', $params)) { self::createField($customField, 'modify', $indexExist); } else { $customField->column_name .= "_{$customField->id}"; $customField->save(); // make sure all values are present in the object $customField->find(true); self::createField($customField, 'add'); } // complete transaction $transaction->commit(); // reset the cache require_once 'CRM/Core/BAO/Cache.php'; CRM_Core_BAO_Cache::deleteGroup('contact fields'); // reset various static arrays used here require_once 'CRM/Contact/BAO/Contact.php'; CRM_Contact_BAO_Contact::$_importableFields = CRM_Contact_BAO_Contact::$_exportableFields = self::$_importFields = null; return $customField; }