/**
* Process the form when submitted.
*
* @return void
*/
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
CRM_Core_Session::setStatus(ts('The custom field \'%1\' has been deleted.', array(1 => $field->label)), '', 'success');
}
/**
* set up variables to build the form
*
* @return void
* @acess protected
*/
function preProcess()
{
$this->_id = $this->get('id');
$defaults = array();
$params = array('id' => $this->_id);
CRM_Core_BAO_CustomGroup::retrieve($params, $defaults);
$this->_title = $defaults['title'];
//check wheter this contain any custom fields
$customField = new CRM_Core_DAO_CustomField();
$customField->custom_group_id = $this->_id;
if ($customField->find(TRUE)) {
CRM_Core_Session::setStatus(ts("The Group '%1' cannot be deleted! You must Delete all custom fields in this group prior to deleting the group.", array(1 => $this->_title)), ts('Deletion Error'), 'error');
$url = CRM_Utils_System::url('civicrm/admin/custom/group', "reset=1");
CRM_Utils_System::redirect($url);
return TRUE;
}
$this->assign('title', $this->_title);
CRM_Utils_System::setTitle(ts('Confirm Custom Group Delete'));
}
/**
* Low-level option getter, rarely accessed directly.
* NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
*
* @param String $daoName
* @param String $fieldName
* @param Array $params
* - name string name of the option group
* - flip boolean results are return in id => label format if false
* if true, the results are reversed
* - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
* - localize boolean if true, localize the results before returning
* - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
* - keyColumn string the column to use for 'id'
* - labelColumn string the column to use for 'label'
* - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
* - onlyActive boolean return only the action option values
* - fresh boolean ignore cache entries and go back to DB
* @param String $context: Context string
*
* @return Array on success, FALSE on error.
*
* @static
*/
public static function get($daoName, $fieldName, $params = array(), $context = NULL)
{
CRM_Core_DAO::buildOptionsContext($context);
$flip = !empty($params['flip']);
// Merge params with defaults
$params += array('grouping' => FALSE, 'localize' => FALSE, 'onlyActive' => $context == 'validate' || $context == 'get' ? FALSE : TRUE, 'fresh' => FALSE);
// Custom fields are not in the schema
if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
$customField = new CRM_Core_DAO_CustomField();
$customField->id = (int) substr($fieldName, 7);
$customField->find(TRUE);
$options = FALSE;
if (!empty($customField->option_group_id)) {
$options = CRM_Core_OptionGroup::valuesByID($customField->option_group_id, $flip, $params['grouping'], $params['localize'], CRM_Utils_Array::value('labelColumn', $params, 'label'), $params['onlyActive'], $params['fresh']);
} else {
if ($customField->data_type === 'StateProvince') {
$options = self::stateProvince();
} elseif ($customField->data_type === 'Country') {
$options = $context == 'validate' ? self::countryIsoCode() : self::country();
} elseif ($customField->data_type === 'Boolean') {
$options = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
}
$options = $options && $flip ? array_flip($options) : $options;
}
if ($options !== FALSE) {
CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE);
}
$customField->free();
return $options;
}
// Core field: load schema
$dao = new $daoName();
$fields = $dao->fields();
$fieldKeys = $dao->fieldKeys();
$dao->free();
// Support "unique names" as well as sql names
$fieldKey = $fieldName;
if (empty($fields[$fieldKey])) {
$fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
}
// If neither worked then this field doesn't exist. Return false.
if (empty($fields[$fieldKey])) {
return FALSE;
}
$fieldSpec = $fields[$fieldKey];
// If the field is an enum, explode the enum definition and return the array.
if (isset($fieldSpec['enumValues'])) {
// use of a space after the comma is inconsistent in xml
$enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
$output = explode(',', $enumStr);
return array_combine($output, $output);
} elseif (!empty($fieldSpec['pseudoconstant'])) {
$pseudoconstant = $fieldSpec['pseudoconstant'];
// Merge params with schema defaults
$params += array('condition' => CRM_Utils_Array::value('condition', $pseudoconstant, array()), 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant), 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant));
// Fetch option group from option_value table
if (!empty($pseudoconstant['optionGroupName'])) {
if ($context == 'validate') {
$params['labelColumn'] = 'name';
}
// Call our generic fn for retrieving from the option_value table
return CRM_Core_OptionGroup::values($pseudoconstant['optionGroupName'], $flip, $params['grouping'], $params['localize'], $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL, $params['labelColumn'] ? $params['labelColumn'] : 'label', $params['onlyActive'], $params['fresh'], $params['keyColumn'] ? $params['keyColumn'] : 'value');
}
// Fetch options from other tables
if (!empty($pseudoconstant['table'])) {
// Normalize params so the serialized cache string will be consistent.
CRM_Utils_Array::remove($params, 'flip', 'fresh');
ksort($params);
$cacheKey = $daoName . $fieldName . serialize($params);
// Retrieve cached options
if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
$output = self::$cache[$cacheKey];
} else {
$daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']);
if (!class_exists($daoName)) {
return FALSE;
//.........这里部分代码省略.........
/**
* Process the form when submitted
*
* @return void
* @access public
*/
public function postProcess()
{
// step 1: copy and create dstField and column
require_once 'CRM/Core/BAO/CustomField.php';
$field = new CRM_Core_DAO_CustomField();
$field->id = $this->_srcFID;
if (!$field->find(true)) {
CRM_Core_Error::fatal();
}
// now change the field group ID and save it, also unset the id
unset($field->id);
// step 2: copy data from srcColumn to dstColumn
$query = "\nINSERT INTO {$dstTable} ( {$entityID}, {$dstColumn} )\nSELECT {$entityID}, {$srcColumn}\nFROM {$srcTable}\nON DUPLICATE KEY UPDATE {$dstColumn} = {$srcColumn}";
CRM_Core_DAO::query($query, CRM_Core_DAO::$_nullArray);
// step 3: remove srcField (which should also delete the srcColumn
require_once 'CRM/Core/BAO/CustomField.php';
$field = new CRM_Core_DAO_CustomField();
$field->id = $this->_srcFID;
CRM_Core_BAO_CustomField::deleteField($field);
}
/**
* global validation rules for the form
*
* @param array $fields posted values of the form
*
* @return array list of errors to be posted back to the form
* @static
* @access public
*/
static function formRule($fields, $files, $self)
{
$is_required = CRM_Utils_Array::value('is_required', $fields, false);
$is_registration = CRM_Utils_Array::value('is_registration', $fields, false);
$is_view = CRM_Utils_Array::value('is_view', $fields, false);
$in_selector = CRM_Utils_Array::value('in_selector', $fields, false);
$is_searchable = CRM_Utils_Array::value('is_searchable', $fields, false);
$visibility = CRM_Utils_Array::value('visibility', $fields, false);
$is_active = CRM_Utils_Array::value('is_active', $fields, false);
$errors = array();
if ($is_view && $is_registration) {
$errors['is_registration'] = ts('View Only cannot be selected if this field is to be included on the registration form');
}
if ($is_view && $is_required) {
$errors['is_view'] = ts('A View Only field cannot be required');
}
$fieldName = $fields['field_name'][0];
if (!$fieldName) {
$errors['field_name'] = ts('Please select a field name');
}
if ($in_selector && in_array($fieldName, array('Contribution', 'Participant', 'Membership', 'Activity'))) {
$errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $fieldName));
}
if (!empty($fields['field_id'])) {
//get custom field id
$customFieldId = explode('_', $fieldName);
if ($customFieldId[0] == 'custom') {
$customField = new CRM_Core_DAO_CustomField();
$customField->id = $customFieldId[1];
$customField->find(true);
if (!$customField->is_active && $is_active) {
$errors['field_name'] = ts('Cannot set this field "Active" since the selected custom field is disabled.');
}
}
}
//check profile is configured for double option process
//adding group field, email field should be present in the group
//fixed for issue CRM-2861 & CRM-4153
$config = CRM_Core_Config::singleton();
if ($config->profileDoubleOptIn) {
if ($fields['field_name'][1] == 'group') {
require_once 'CRM/Core/BAO/UFField.php';
$dao = new CRM_Core_BAO_UFField();
$dao->uf_group_id = $fields['group_id'];
$dao->find();
$emailField = false;
while ($dao->fetch()) {
//check email field is present in the group
if ($dao->field_name == 'email') {
$emailField = true;
}
}
if (!$emailField) {
$disableSetting = "define( 'CIVICRM_PROFILE_DOUBLE_OPTIN' , 0 );";
$errors['field_name'] = ts('Your site is currently configured to require double-opt in when users join (subscribe) to Group(s) via a Profile form. In this mode, you need to include an Email field in a Profile BEFORE you can add the Group(s) field. This ensures that an opt-in confirmation email can be sent. Your site administrator can disable double opt-in by adding this line to the CiviCRM settings file: <em>%1</em>', array(1 => $disableSetting));
}
}
}
//fix for CRM-3037
$fieldType = $fields['field_name'][0];
//get the group type.
$groupType = CRM_Core_BAO_UFGroup::calculateGroupType($self->_gid, CRM_Utils_Array::value('field_id', $fields));
switch ($fieldType) {
case 'Contact':
if (in_array('Activity', $groupType)) {
$errors['field_name'] = ts('Cannot add or update profile field type Contact with combination of Activity');
} else {
self::formRuleSubType($fieldType, $groupType, $errors);
}
break;
case 'Individual':
if (in_array('Activity', $groupType) || in_array('Household', $groupType) || in_array('Organization', $groupType)) {
$errors['field_name'] = ts('Cannot add or update profile field type Individual with combination of Household or Organization or Activity');
} else {
self::formRuleSubType($fieldType, $groupType, $errors);
}
break;
case 'Household':
if (in_array('Activity', $groupType) || in_array('Individual', $groupType) || in_array('Organization', $groupType)) {
$errors['field_name'] = ts('Cannot add or update profile field type Household with combination of Individual or Organization or Activity');
} else {
self::formRuleSubType($fieldType, $groupType, $errors);
}
break;
case 'Organization':
if (in_array('Activity', $groupType) || in_array('Household', $groupType) || in_array('Individual', $groupType)) {
$errors['field_name'] = ts('Cannot add or update profile field type Organization with combination of Household or Individual or Activity');
} else {
self::formRuleSubType($fieldType, $groupType, $errors);
}
break;
//.........这里部分代码省略.........
/**
* Check the status of custom field used in uf fields.
*
* @param int $UFFieldId
*
* @return bool
* false if custom field are disabled else true
*/
public static function checkUFStatus($UFFieldId)
{
$fieldName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $UFFieldId, 'field_name');
// return if field is not a custom field
if (!($customFieldId = CRM_Core_BAO_CustomField::getKeyID($fieldName))) {
return TRUE;
}
$customField = new CRM_Core_DAO_CustomField();
$customField->id = $customFieldId;
// if uf field is custom field
if ($customField->find(TRUE)) {
if (!$customField->is_active) {
return FALSE;
} else {
return TRUE;
}
}
}
开发者ID:kidaa30,项目名称:yes,代码行数:26,代码来源:UFField.php
示例9: formRule
/**
* global validation rules for the form
*
* @param array $fields (referance) posted values of the form
*
* @return array if errors then list of errors to be posted back to the form,
* true otherwise
* @static
* @access public
*/
static function formRule(&$fields, &$files, &$self)
{
$default = CRM_Utils_Array::value('default_value', $fields);
$errors = array();
// ensure that the label is not 'id'
if (strtolower($fields['label']) == 'id') {
$errors['label'] = ts("You cannot use 'id' as a field label.");
}
$customField = new CRM_Core_DAO_CustomField();
$customField->custom_group_id = $self->_gid;
$customField->label = $fields['label'];
$dupeLabel = false;
if ($customField->find(true) && $self->_id != $customField->id) {
$dupeLabel = true;
}
if ($dupeLabel) {
$errors['label'] = ts('Name already exists in Database.');
}
if (!isset($fields['data_type'][0]) || !isset($fields['data_type'][1])) {
$errors['_qf_default'] = ts('Please enter valid - Data and Input Field Type.');
}
if ($default) {
$dataType = self::$_dataTypeKeys[$fields['data_type'][0]];
switch ($dataType) {
case 'Int':
if (!CRM_Utils_Rule::integer($default)) {
$errors['default_value'] = ts('Please enter a valid integer as default value.');
}
break;
case 'Float':
if (!CRM_Utils_Rule::numeric($default)) {
$errors['default_value'] = ts('Please enter a valid number as default value.');
}
break;
case 'Money':
if (!CRM_Utils_Rule::money($default)) {
$errors['default_value'] = ts('Please enter a valid number value.');
}
break;
case 'Link':
if (!CRM_Utils_Rule::url($default)) {
$errors['default_value'] = ts('Please enter a valid link.');
}
break;
case 'Date':
if (!CRM_Utils_Rule::date($default)) {
$errors['default_value'] = ts('Please enter a valid date as default value using YYYY-MM-DD format. Example: 2004-12-31.');
}
break;
case 'Boolean':
if ($default != '1' && $default != '0') {
$errors['default_value'] = ts('Please enter 1 (for Yes) or 0 (for No) if you want to set a default value.');
}
break;
case 'Country':
if (!empty($default)) {
$query = "SELECT count(*) FROM civicrm_country WHERE name = %1 OR iso_code = %1";
$params = array(1 => array($fields['default_value'], 'String'));
if (CRM_Core_DAO::singleValueQuery($query, $params) <= 0) {
$errors['default_value'] = ts('Invalid default value for country.');
}
}
break;
case 'StateProvince':
if (!empty($default)) {
$query = "\nSELECT count(*) \n FROM civicrm_state_province\n WHERE name = %1\n OR abbreviation = %1";
$params = array(1 => array($fields['default_value'], 'String'));
if (CRM_Core_DAO::singleValueQuery($query, $params) <= 0) {
$errors['default_value'] = ts('The invalid default value for State/Province data type');
}
}
break;
case 'ContactReference':
//FIX ME
break;
}
}
if (self::$_dataTypeKeys[$fields['data_type'][0]] == 'Date') {
if (!$fields['date_format']) {
$errors['date_format'] = ts('Please select a date format.');
}
}
/** Check the option values entered
* Appropriate values are required for the selected datatype
* Incomplete row checking is also required.
*/
$_flagOption = $_rowError = 0;
$_showHide =& new CRM_Core_ShowHideBlocks('', '');
$dataType = self::$_dataTypeKeys[$fields['data_type'][0]];
if (isset($fields['data_type'][1])) {
//.........这里部分代码省略.........
开发者ID:ksecor,项目名称:civicrm,代码行数:101,代码来源:Field.php
示例10: formRule
/**
* Global validation rules for the form.
*
* @param array $fields
* Posted values of the form.
*
* @param $files
* @param $self
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($fields, $files, $self)
{
$is_required = CRM_Utils_Array::value('is_required', $fields, FALSE);
$is_registration = CRM_Utils_Array::value('is_registration', $fields, FALSE);
$is_view = CRM_Utils_Array::value('is_view', $fields, FALSE);
$in_selector = CRM_Utils_Array::value('in_selector', $fields, FALSE);
$is_active = CRM_Utils_Array::value('is_active', $fields, FALSE);
$errors = array();
if ($is_view && $is_registration) {
$errors['is_registration'] = ts('View Only cannot be selected if this field is to be included on the registration form');
}
if ($is_view && $is_required) {
$errors['is_view'] = ts('A View Only field cannot be required');
}
$entityName = $fields['field_name'][0];
if (!$entityName) {
$errors['field_name'] = ts('Please select a field name');
}
if ($in_selector && in_array($entityName, array('Contribution', 'Participant', 'Membership', 'Activity'))) {
$errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $entityName));
}
$isCustomField = FALSE;
$profileFieldName = CRM_Utils_Array::value(1, $fields['field_name']);
if ($profileFieldName) {
//get custom field id
$customFieldId = explode('_', $profileFieldName);
if ($customFieldId[0] == 'custom') {
$customField = new CRM_Core_DAO_CustomField();
$customField->id = $customFieldId[1];
$customField->find(TRUE);
$isCustomField = TRUE;
if (!empty($fields['field_id']) && !$customField->is_active && $is_active) {
$errors['field_name'] = ts('Cannot set this field "Active" since the selected custom field is disabled.');
}
//check if profile already has a different multi-record custom set field configured
$customGroupId = CRM_Core_BAO_CustomField::isMultiRecordField($profileFieldName);
if ($customGroupId) {
if ($profileMultiRecordCustomGid = CRM_Core_BAO_UFField::checkMultiRecordFieldExists($self->_gid)) {
if ($customGroupId != $profileMultiRecordCustomGid) {
$errors['field_name'] = ts("You cannot configure multi-record custom fields belonging to different custom sets in one profile");
}
}
}
}
}
// Get list of fields already in the group
$groupFields = CRM_Core_BAO_UFGroup::getFields($fields['group_id'], FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE);
// Check if we already added a primary field of the same communication type
self::formRulePrimaryCheck($fields, $profileFieldName, $groupFields, $errors);
//check profile is configured for double option process
//adding group field, email field should be present in the group
//fixed for issue CRM-2861 & CRM-4153
if (CRM_Core_BAO_UFGroup::isProfileDoubleOptin()) {
if (CRM_Utils_Array::value(1, $fields['field_name']) == 'group') {
$dao = new CRM_Core_BAO_UFField();
$dao->uf_group_id = $fields['group_id'];
$dao->find();
$emailField = FALSE;
while ($dao->fetch()) {
//check email field is present in the group
if ($dao->field_name == 'email') {
$emailField = TRUE;
break;
}
}
if (!$emailField) {
$disableSettingURL = CRM_Utils_System::url('civicrm/admin/setting/preferences/mailing', 'reset=1');
$errors['field_name'] = ts('Your site is currently configured to require double-opt in when users join (subscribe) to Group(s) via a Profile form. In this mode, you need to include an Email field in a Profile BEFORE you can add the Group(s) field. This ensures that an opt-in confirmation email can be sent. Your site administrator can disable double opt-in on the civimail admin settings: <em>%1</em>', array(1 => $disableSettingURL));
}
}
}
//fix for CRM-3037
$fieldType = $fields['field_name'][0];
//get the group type.
$groupType = CRM_Core_BAO_UFGroup::calculateGroupType($self->_gid, FALSE, CRM_Utils_Array::value('field_id', $fields));
switch ($fieldType) {
case 'Contact':
self::formRuleSubType($fieldType, $groupType, $errors);
break;
case 'Individual':
if (in_array('Activity', $groupType) || in_array('Household', $groupType) || in_array('Organization', $groupType)) {
//CRM-7603 - need to support activity + individual.
//$errors['field_name'] =
//ts( 'Cannot add or update profile field type Individual with combination of Household or Organization or Activity' );
if (in_array('Household', $groupType) || in_array('Organization', $groupType)) {
$errors['field_name'] = ts('Cannot add or update profile field type Individual with combination of Household or Organization');
}
} else {
//.........这里部分代码省略.........
开发者ID:kidaa30,项目名称:yes,代码行数:101,代码来源:Field.php
示例11: postProcess
/**
* 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');
}
/**
* Use this API to delete an existing custom group field.
*
* @param $params Array id of the field to be deleted
*
*
* @access public
**/
function civicrm_custom_field_delete($params)
{
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error('Params is not an array');
}
if (!CRM_Utils_Array::value('customFieldId', $params['result'])) {
return civicrm_create_error('Invalid or no value for Custom Field ID');
}
require_once 'CRM/Core/DAO/CustomField.php';
$field = new CRM_Core_DAO_CustomField();
$field->id = $params['result']['customFieldId'];
$field->find(true);
require_once 'CRM/Core/BAO/CustomField.php';
$customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field);
return $customFieldDelete ? civicrm_create_error('Error while deleting custom field') : civicrm_create_success();
}
/**
* Delete the Custom Group.
*
* @param CRM_Core_BAO_CustomGroup $group
* Custom group object.
* @param bool $force
* whether to force the deletion, even if there are custom fields.
*
* @return bool
* False if field exists for this group, true if group gets deleted.
*/
public static function deleteGroup($group, $force = FALSE)
{
//check whether this contain any custom fields
$customField = new CRM_Core_DAO_CustomField();
$customField->custom_group_id = $group->id;
$customField->find();
// return early if there are custom fields and we're not
// forcing the delete, otherwise delete the fields one by one
while ($customField->fetch()) {
if (!$force) {
return FALSE;
}
CRM_Core_BAO_CustomField::deleteField($customField);
}
// drop the table associated with this custom group
CRM_Core_BAO_SchemaHandler::dropTable($group->table_name);
//delete custom group
$group->delete();
CRM_Utils_Hook::post('delete', 'CustomGroup', $group->id, $group);
return TRUE;
}
请发表评论