/**
  * 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;
 }
示例#2
0
 /**
  * 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 object CRM_Core_DAO_CustomGroup object
  * @access public
  * @static
  */
 static function create(&$params)
 {
     // create custom group dao, populate fields and then save.
     $group = new CRM_Core_DAO_CustomGroup();
     $group->title = $params['title'];
     require_once 'CRM/Utils/String.php';
     if (isset($params['name'])) {
         $group->name = $params['name'];
     } else {
         $maxLength = CRM_Core_DAO::getAttribute('CRM_Core_DAO_CustomGroup', 'name');
         $group->name = CRM_Utils_String::titleToVar($params['title'], CRM_Utils_Array::value('maxlength', $maxLength));
     }
     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 (CRM_Utils_Array::value('extends_entity_column_value', $params)) {
         $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;
     }
     require_once 'CRM/Utils/Weight.php';
     $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) {
         $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 = 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 ($params['is_multiple'] != $isMultiple && (CRM_Utils_Array::value('is_multiple', $params) || $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);
         require_once 'CRM/Utils/String.php';
         // lets create the table associated with the group and save it
         $tableName = $group->table_name = "civicrm_value_" . strtolower(CRM_Utils_String::munge($group->title, '_', 32));
         // we do this only once, so name never changes
         $group->name = CRM_Utils_String::munge($params['title'], '_', 64);
     }
     // enclose the below in a transaction
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $group->save();
     if ($tableName) {
         // now append group id to table name, this prevent any name conflicts
         // like CRM-2742
         $tableName .= "_{$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) {
         require_once 'CRM/Core/BAO/SchemaHandler.php';
         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');
         require_once 'CRM/Core/BAO/SchemaHandler.php';
         CRM_Core_BAO_SchemaHandler::changeFKConstraint($table, self::mapTableName($params['extends'][0]));
     }
     $transaction->commit();
     // reset the cache
     require_once 'CRM/Core/BAO/Cache.php';
     CRM_Core_BAO_Cache::deleteGroup('contact fields');
     require_once 'CRM/Utils/Hook.php';
     if ($tableName) {
         CRM_Utils_Hook::post('create', 'CustomGroup', $group->id, $group);
     } else {
         CRM_Utils_Hook::post('edit', 'CustomGroup', $group->id, $group);
     }
     return $group;
 }