/** * Takes an associative array and creates a custom group 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_CustomGroup */ public static function create(&$params) { // create custom group dao, populate fields and then save. $group = new CRM_Core_DAO_CustomGroup(); if (isset($params['title'])) { $group->title = $params['title']; } if (in_array($params['extends'][0], array('ParticipantRole', 'ParticipantEventName', 'ParticipantEventType'))) { $group->extends = 'Participant'; } else { $group->extends = $params['extends'][0]; } $group->extends_entity_column_id = 'null'; if ($params['extends'][0] == 'ParticipantRole' || $params['extends'][0] == 'ParticipantEventName' || $params['extends'][0] == 'ParticipantEventType') { $group->extends_entity_column_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $params['extends'][0], 'value', 'name'); } //this is format when form get submit. $extendsChildType = CRM_Utils_Array::value(1, $params['extends']); //lets allow user to pass direct child type value, CRM-6893 if (!empty($params['extends_entity_column_value'])) { $extendsChildType = $params['extends_entity_column_value']; } if (!CRM_Utils_System::isNull($extendsChildType)) { $extendsChildType = implode(CRM_Core_DAO::VALUE_SEPARATOR, $extendsChildType); if (CRM_Utils_Array::value(0, $params['extends']) == 'Relationship') { $extendsChildType = str_replace(array('_a_b', '_b_a'), array('', ''), $extendsChildType); } if (substr($extendsChildType, 0, 1) != CRM_Core_DAO::VALUE_SEPARATOR) { $extendsChildType = CRM_Core_DAO::VALUE_SEPARATOR . $extendsChildType . CRM_Core_DAO::VALUE_SEPARATOR; } } else { $extendsChildType = 'null'; } $group->extends_entity_column_value = $extendsChildType; if (isset($params['id'])) { $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['id'], 'weight', 'id'); } else { $oldWeight = 0; } $group->weight = CRM_Utils_Weight::updateOtherWeights('CRM_Core_DAO_CustomGroup', $oldWeight, CRM_Utils_Array::value('weight', $params, FALSE)); $fields = array('style', 'collapse_display', 'collapse_adv_display', 'help_pre', 'help_post', 'is_active', 'is_multiple'); foreach ($fields as $field) { if (isset($params[$field]) || $field == 'is_multiple') { $group->{$field} = CRM_Utils_Array::value($field, $params, FALSE); } } $group->max_multiple = isset($params['is_multiple']) ? isset($params['max_multiple']) && $params['max_multiple'] >= '0' ? $params['max_multiple'] : 'null' : 'null'; $tableName = $oldTableName = NULL; if (isset($params['id'])) { $group->id = $params['id']; //check whether custom group was changed from single-valued to multiple-valued $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['id'], 'is_multiple'); if ((!empty($params['is_multiple']) || $isMultiple) && $params['is_multiple'] != $isMultiple) { $oldTableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['id'], 'table_name'); } } else { $group->created_id = CRM_Utils_Array::value('created_id', $params); $group->created_date = CRM_Utils_Array::value('created_date', $params); // we do this only once, so name never changes if (isset($params['name'])) { $group->name = CRM_Utils_String::munge($params['name'], '_', 64); } else { $group->name = CRM_Utils_String::munge($group->title, '_', 64); } if (isset($params['table_name'])) { $tableName = $params['table_name']; if (CRM_Core_DAO_AllCoreTables::isCoreTable($tableName)) { // Bad idea. Prevent group creation because it might lead to a broken configuration. CRM_Core_Error::fatal(ts("Cannot create custom table because %1 is already a core table.", array('1' => $tableName))); } } } if (array_key_exists('is_reserved', $params)) { $group->is_reserved = $params['is_reserved'] ? 1 : 0; } $op = isset($params['id']) ? 'edit' : 'create'; CRM_Utils_Hook::pre($op, 'CustomGroup', CRM_Utils_Array::value('id', $params), $params); // enclose the below in a transaction $transaction = new CRM_Core_Transaction(); $group->save(); if (!isset($params['id'])) { if (!isset($params['table_name'])) { $munged_title = strtolower(CRM_Utils_String::munge($group->title, '_', 42)); $tableName = "civicrm_value_{$munged_title}_{$group->id}"; } $group->table_name = $tableName; CRM_Core_DAO::setFieldValue('CRM_Core_DAO_CustomGroup', $group->id, 'table_name', $tableName); // now create the table associated with this group self::createTable($group); } elseif ($oldTableName) { CRM_Core_BAO_SchemaHandler::changeUniqueToIndex($oldTableName, CRM_Utils_Array::value('is_multiple', $params)); } if (CRM_Utils_Array::value('overrideFKConstraint', $params) == 1) { $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $params['id'], 'table_name'); CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($params['extends'][0])); } $transaction->commit(); // reset the cache CRM_Utils_System::flushCache(); if ($tableName) { CRM_Utils_Hook::post('create', 'CustomGroup', $group->id, $group); } else { CRM_Utils_Hook::post('edit', 'CustomGroup', $group->id, $group); } return $group; }