/**
* 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;
}
/**
* function to get the complete information for one or more events
*
* @param date $start get events with start date >= this date
* @param integer $type get events on the a specific event type (by event_type_id)
* @param integer $eventId return a single event - by event id
* @param date $end also get events with end date >= this date
*
* @return array $all array of all the events that are searched
* @static
* @access public
*/
static function &getCompleteInfo($start = null, $type = null, $eventId = null, $end = null)
{
// if start and end date are NOT passed, return all events with start_date OR end_date >= today CRM-5133
if ($start) {
// get events with start_date >= requested start
$startDate = CRM_Utils_Type::escape($start, 'Date');
} else {
// get events with start date >= today
$startDate = date("Ymd");
}
if ($end) {
// also get events with end_date >= requested end
$endDate = CRM_Utils_Type::escape($end, 'Date');
} else {
// OR also get events with end date >= today
$endDate = date("Ymd");
}
$dateCondition = "AND (civicrm_event.start_date >= {$startDate} OR civicrm_event.end_date >= {$endDate})";
if ($type) {
$typeCondition = " AND civicrm_event.event_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
}
// Get the Id of Option Group for Event Types
require_once 'CRM/Core/DAO/OptionGroup.php';
$optionGroupDAO = new CRM_Core_DAO_OptionGroup();
$optionGroupDAO->name = 'event_type';
$optionGroupId = null;
if ($optionGroupDAO->find(true)) {
$optionGroupId = $optionGroupDAO->id;
}
$query = "\nSELECT\n civicrm_event.id as event_id, \n civicrm_email.email as email, \n civicrm_event.title as title, \n civicrm_event.summary as summary, \n civicrm_event.start_date as start, \n civicrm_event.end_date as end, \n civicrm_event.description as description, \n civicrm_event.is_show_location as is_show_location, \n civicrm_event.is_online_registration as is_online_registration,\n civicrm_event.registration_link_text as registration_link_text,\n civicrm_event.registration_start_date as registration_start_date,\n civicrm_event.registration_end_date as registration_end_date,\n civicrm_option_value.label as event_type, \n civicrm_address.name as address_name, \n civicrm_address.street_address as street_address, \n civicrm_address.supplemental_address_1 as supplemental_address_1, \n civicrm_address.supplemental_address_2 as supplemental_address_2, \n civicrm_address.city as city, \n civicrm_address.postal_code as postal_code, \n civicrm_address.postal_code_suffix as postal_code_suffix, \n civicrm_state_province.abbreviation as state, \n civicrm_country.name AS country\nFROM civicrm_event\nLEFT JOIN civicrm_loc_block ON civicrm_event.loc_block_id = civicrm_loc_block.id\nLEFT JOIN civicrm_address ON civicrm_loc_block.address_id = civicrm_address.id\nLEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id\nLEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id\nLEFT JOIN civicrm_email ON civicrm_loc_block.email_id = civicrm_email.id\nLEFT JOIN civicrm_option_value ON (\n civicrm_event.event_type_id = civicrm_option_value.value AND\n civicrm_option_value.option_group_id = %1 )\nWHERE civicrm_event.is_active = 1 \n AND civicrm_event.is_public = 1\n AND (is_template = 0 OR is_template IS NULL)\n {$dateCondition}";
if (isset($typeCondition)) {
$query .= $typeCondition;
}
if (isset($eventId)) {
$query .= " AND civicrm_event.id ={$eventId} ";
}
$query .= " ORDER BY civicrm_event.start_date ASC";
$params = array(1 => array($optionGroupId, 'Integer'));
$dao =& CRM_Core_DAO::executeQuery($query, $params);
$all = array();
$config = CRM_Core_Config::singleton();
$baseURL = parse_url($config->userFrameworkBaseURL);
$url = "@" . $baseURL['host'];
if (CRM_Utils_Array::value('path', $baseURL)) {
$url .= substr($baseURL['path'], 0, -1);
}
// check 'view event info' permission
$permissions = CRM_Core_Permission::event(CRM_Core_Permission::VIEW);
require_once 'CRM/Utils/String.php';
while ($dao->fetch()) {
if (in_array($dao->event_id, $permissions)) {
$info = array();
$info['uid'] = "CiviCRM_EventID_{$dao->event_id}_" . md5($config->userFrameworkBaseURL) . $url;
$info['title'] = $dao->title;
$info['event_id'] = $dao->event_id;
$info['summary'] = $dao->summary;
$info['description'] = $dao->description;
$info['start_date'] = $dao->start;
$info['end_date'] = $dao->end;
$info['contact_email'] = $dao->email;
$info['event_type'] = $dao->event_type;
$info['is_show_location'] = $dao->is_show_location;
$info['is_online_registration'] = $dao->is_online_registration;
$info['registration_link_text'] = $dao->registration_link_text;
$info['registration_start_date'] = $dao->registration_start_date;
$info['registration_end_date'] = $dao->registration_end_date;
$address = '';
$addrFields = array('address_name' => $dao->address_name, 'street_address' => $dao->street_address, 'supplemental_address_1' => $dao->supplemental_address_1, 'supplemental_address_2' => $dao->supplemental_address_2, 'city' => $dao->city, 'state_province' => $dao->state, 'postal_code' => $dao->postal_code, 'postal_code_suffix' => $dao->postal_code_suffix, 'country' => $dao->country, 'county' => null);
require_once 'CRM/Utils/Address.php';
CRM_Utils_String::append($address, ', ', CRM_Utils_Address::format($addrFields));
$info['location'] = $address;
$info['url'] = CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $dao->event_id, true, null, false);
$all[] = $info;
}
}
return $all;
}
/**
* Get the values of all option values given an option group Name as a key => value pair
* Use above cached function to make it super efficient
*
* @param string $optionGroupName
* The option group name for which we want the values from.
*
* @return array
* an associative array of label, value pairs
*/
public static function getOptionValuesAssocArrayFromName($optionGroupName)
{
$dao = new CRM_Core_DAO_OptionGroup();
$dao->name = $optionGroupName;
$dao->selectAdd();
$dao->selectAdd('id');
$dao->find(TRUE);
$optionValues = self::getOptionValuesArray($dao->id);
$options = array();
foreach ($optionValues as $id => $value) {
$options[$value['value']] = $value['label'];
}
return $options;
}
/**
* function to get the complete information for one or more events
*
* @param date $start get events with start date >= this date
* @param integer $type get events on the a specific event type (by event_type_id)
* @param integer $eventId return a single event - by event id
* @param date $end also get events with end date >= this date
* @param boolean $onlyPublic include public events only, default TRUE
*
* @return array $all array of all the events that are searched
* @static
* @access public
*/
static function &getCompleteInfo($start = NULL, $type = NULL, $eventId = NULL, $end = NULL, $onlyPublic = TRUE)
{
$publicCondition = NULL;
if ($onlyPublic) {
$publicCondition = " AND civicrm_event.is_public = 1";
}
$dateCondition = '';
// if start and end date are NOT passed, return all events with start_date OR end_date >= today CRM-5133
if ($start) {
// get events with start_date >= requested start
$startDate = CRM_Utils_Type::escape($start, 'Date');
$dateCondition .= " AND ( civicrm_event.start_date >= {$startDate} )";
}
if ($end) {
// also get events with end_date <= requested end
$endDate = CRM_Utils_Type::escape($end, 'Date');
$dateCondition .= " AND ( civicrm_event.end_date <= '{$endDate}' ) ";
}
// CRM-9421 and CRM-8620 Default mode for ical/rss feeds. No start or end filter passed.
// Need to exclude old events with only start date
// and not exclude events in progress (start <= today and end >= today). DGG
if (empty($start) && empty($end)) {
// get events with end date >= today, not sure of this logic
// but keeping this for backward compatibility as per issue CRM-5133
$today = date("Y-m-d G:i:s");
$dateCondition .= " AND ( civicrm_event.end_date >= '{$today}' OR civicrm_event.start_date >= '{$today}' ) ";
}
if ($type) {
$typeCondition = " AND civicrm_event.event_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
}
// Get the Id of Option Group for Event Types
$optionGroupDAO = new CRM_Core_DAO_OptionGroup();
$optionGroupDAO->name = 'event_type';
$optionGroupId = NULL;
if ($optionGroupDAO->find(TRUE)) {
$optionGroupId = $optionGroupDAO->id;
}
$query = "\nSELECT\n civicrm_event.id as event_id,\n civicrm_email.email as email,\n civicrm_event.title as title,\n civicrm_event.summary as summary,\n civicrm_event.start_date as start,\n civicrm_event.end_date as end,\n civicrm_event.description as description,\n civicrm_event.is_show_location as is_show_location,\n civicrm_event.is_online_registration as is_online_registration,\n civicrm_event.registration_link_text as registration_link_text,\n civicrm_event.registration_start_date as registration_start_date,\n civicrm_event.registration_end_date as registration_end_date,\n civicrm_option_value.label as event_type,\n civicrm_address.name as address_name,\n civicrm_address.street_address as street_address,\n civicrm_address.supplemental_address_1 as supplemental_address_1,\n civicrm_address.supplemental_address_2 as supplemental_address_2,\n civicrm_address.city as city,\n civicrm_address.postal_code as postal_code,\n civicrm_address.postal_code_suffix as postal_code_suffix,\n civicrm_state_province.abbreviation as state,\n civicrm_country.name AS country\nFROM civicrm_event\nLEFT JOIN civicrm_loc_block ON civicrm_event.loc_block_id = civicrm_loc_block.id\nLEFT JOIN civicrm_address ON civicrm_loc_block.address_id = civicrm_address.id\nLEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id\nLEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id\nLEFT JOIN civicrm_email ON civicrm_loc_block.email_id = civicrm_email.id\nLEFT JOIN civicrm_option_value ON (\n civicrm_event.event_type_id = civicrm_option_value.value AND\n civicrm_option_value.option_group_id = %1 )\nWHERE civicrm_event.is_active = 1\n AND (is_template = 0 OR is_template IS NULL)\n {$publicCondition}\n {$dateCondition}";
if (isset($typeCondition)) {
$query .= $typeCondition;
}
if (isset($eventId)) {
$query .= " AND civicrm_event.id ={$eventId} ";
}
$query .= " ORDER BY civicrm_event.start_date ASC";
$params = array(1 => array($optionGroupId, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($query, $params);
$all = array();
$config = CRM_Core_Config::singleton();
$baseURL = parse_url($config->userFrameworkBaseURL);
$url = "@" . $baseURL['host'];
if (!empty($baseURL['path'])) {
$url .= substr($baseURL['path'], 0, -1);
}
// check 'view event info' permission
//@todo - per CRM-14626 we have resolved that 'view event info' means 'view ALL event info'
// and passing in the specific permission here will short-circuit the evaluation of permission to
// see specific events (doesn't seem relevant to this call
// however, since this function is accessed only by a convoluted call from a joomla block function
// it seems safer not to touch here. Suggestion is that CRM_Core_Permission::check(array or relevant permissions) would
// be clearer & safer here
$permissions = CRM_Core_Permission::event(CRM_Core_Permission::VIEW);
// check if we're in shopping cart mode for events
$enable_cart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, 'enable_cart');
if ($enable_cart) {
}
while ($dao->fetch()) {
if (!empty($permissions) && in_array($dao->event_id, $permissions)) {
$info = array();
$info['uid'] = "CiviCRM_EventID_{$dao->event_id}_" . md5($config->userFrameworkBaseURL) . $url;
$info['title'] = $dao->title;
$info['event_id'] = $dao->event_id;
$info['summary'] = $dao->summary;
$info['description'] = $dao->description;
$info['start_date'] = $dao->start;
$info['end_date'] = $dao->end;
$info['contact_email'] = $dao->email;
$info['event_type'] = $dao->event_type;
$info['is_show_location'] = $dao->is_show_location;
$info['is_online_registration'] = $dao->is_online_registration;
$info['registration_link_text'] = $dao->registration_link_text;
$info['registration_start_date'] = $dao->registration_start_date;
$info['registration_end_date'] = $dao->registration_end_date;
$address = '';
$addrFields = array('address_name' => $dao->address_name, 'street_address' => $dao->street_address, 'supplemental_address_1' => $dao->supplemental_address_1, 'supplemental_address_2' => $dao->supplemental_address_2, 'city' => $dao->city, 'state_province' => $dao->state, 'postal_code' => $dao->postal_code, 'postal_code_suffix' => $dao->postal_code_suffix, 'country' => $dao->country, 'county' => NULL);
CRM_Utils_String::append($address, ', ', CRM_Utils_Address::format($addrFields));
$info['location'] = $address;
//.........这里部分代码省略.........
function upgrade_3_3_beta1($rev)
{
$upgrade = new CRM_Upgrade_Form();
$upgrade->processSQL($rev);
// CRM-6902
// Add column price_field_value_id in civicrm_line_item.
// Do not drop option_group_id column now since we need it to
// update line items.
$updateLineItem1 = "ALTER TABLE civicrm_line_item ADD COLUMN price_field_value_id int(10) unsigned default NULL;";
CRM_Core_DAO::executeQuery($updateLineItem1);
$priceFieldDAO = new CRM_Price_DAO_Field();
$priceFieldDAO->find();
$ids = array();
while ($priceFieldDAO->fetch()) {
$opGroupDAO = new CRM_Core_DAO_OptionGroup();
$opGroupDAO->name = 'civicrm_price_field.amount.' . $priceFieldDAO->id;
if (!$opGroupDAO->find(TRUE)) {
$opGroupDAO->free();
continue;
}
$opValueDAO = new CRM_Core_DAO_OptionValue();
$opValueDAO->option_group_id = $opGroupDAO->id;
$opValueDAO->find();
while ($opValueDAO->fetch()) {
// FIX ME: not migrating description(?), there will
// be a field description for each option.
$fieldValue = array('price_field_id' => $priceFieldDAO->id, 'label' => $opValueDAO->label, 'name' => CRM_Utils_String::munge($opValueDAO->label, '_', 64), 'amount' => $opValueDAO->name, 'weight' => $opValueDAO->weight, 'is_default' => $opValueDAO->is_default, 'is_active' => $opValueDAO->is_active);
if ($priceFieldDAO->count) {
// Migrate Participant Counts on option level.
// count of each option will be the same
// as earlier field count.
$fieldValue['count'] = $priceFieldDAO->count;
}
$fieldValueDAO = CRM_Price_BAO_FieldValue::add($fieldValue, $ids);
$lineItemDAO = new CRM_Price_DAO_LineItem();
$lineItemDAO->option_group_id = $opGroupDAO->id;
$lineItemDAO->label = $opValueDAO->label;
$lineItemDAO->unit_price = $opValueDAO->name;
$labelFound = $priceFound = FALSE;
// check with label and amount
if (!$lineItemDAO->find(TRUE)) {
$lineItemDAO->free();
$lineItemDAO = new CRM_Price_DAO_LineItem();
$lineItemDAO->option_group_id = $opGroupDAO->id;
$lineItemDAO->label = $opValueDAO->label;
// check with label only
if ($lineItemDAO->find(TRUE)) {
$labelFound = TRUE;
}
} else {
$labelFound = TRUE;
$priceFound = TRUE;
}
$lineItemDAO->free();
// update civicrm_line_item for price_field_value_id.
// Used query to avoid line by line update.
if ($labelFound || $priceFound) {
$lineItemParams = array(1 => array($fieldValueDAO->id, 'Integer'), 2 => array($opValueDAO->label, 'String'));
$updateLineItems = "UPDATE civicrm_line_item SET price_field_value_id = %1 WHERE label = %2";
if ($priceFound) {
$lineItemParams[3] = array($opValueDAO->name, 'Float');
$updateLineItems .= " AND unit_price = %3";
}
CRM_Core_DAO::executeQuery($updateLineItems, $lineItemParams);
}
}
$opGroupDAO->delete();
$opValueDAO->free();
$opGroupDAO->free();
}
$priceFieldDAO->free();
// Now drop option_group_id column from civicrm_line_item
$updateLineItem2 = "ALTER TABLE civicrm_line_item DROP option_group_id,\n ADD CONSTRAINT `FK_civicrm_price_field_value_id` FOREIGN KEY (price_field_value_id) REFERENCES civicrm_price_field_value(id) ON DELETE SET NULL;";
CRM_Core_DAO::executeQuery($updateLineItem2, array(), TRUE, NULL, FALSE, FALSE);
$updatePriceField = "ALTER TABLE civicrm_price_field DROP count";
CRM_Core_DAO::executeQuery($updatePriceField, array(), TRUE, NULL, FALSE, FALSE);
// as the table 'civicrm_price_field' is localised and column 'count' is dropped
// after the views are rebuild, we need to rebuild views to avoid invalid refrence of table.
if ($upgrade->multilingual) {
CRM_Core_I18n_Schema::rebuildMultilingualSchema($upgrade->locales, $rev);
}
}
/**
* Function to get title of the option group
*
* @param int $optionGroupId Id of the Option Group.
*
* @return String title
*
* @access public
* @static
*/
static function getTitle($optionGroupId)
{
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->id = $optionGroupId;
$optionGroup->find(TRUE);
return $optionGroup->name;
}
/**
* 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();
}
}
}
}
}
//.........这里部分代码省略.........
/**
* Find a price_set_id associatied with the given option value or field ID
* @param array $params (reference) an assoc array of name/value pairs
* array may contain either option id or
* price field id
*
* @return price set id on success, null otherwise
* @static
* @access public
*/
public static function getSetId(&$params)
{
$fid = null;
require_once 'CRM/Utils/Array.php';
if ($oid = CRM_Utils_Array::value('oid', $params)) {
require_once 'CRM/Core/DAO/OptionGroup.php';
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $oid, 'option_group_id');
if ($optionGroup->find(true)) {
$groupName = explode(".", $optionGroup->name);
$fid = $groupName[2];
}
} else {
$fid = CRM_Utils_Array::value('fid', $params);
}
if (isset($fid)) {
return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Field', $fid, 'price_set_id');
}
return null;
}
请发表评论