Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure 'type' is always set for query field data. #12488

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ public static function &exportableFields($contactType = 'Individual', $status =
array(
'name' => 'organization_name',
'title' => ts('Current Employer'),
'type' => CRM_Utils_Type::T_STRING,
),
));

Expand All @@ -1545,6 +1546,7 @@ public static function &exportableFields($contactType = 'Individual', $status =
'name' => 'location_type',
'where' => 'civicrm_location_type.name',
'title' => ts('Location Type'),
'type' => CRM_Utils_Type::T_STRING,
),
);

Expand All @@ -1553,6 +1555,7 @@ public static function &exportableFields($contactType = 'Individual', $status =
'name' => 'im_provider',
'where' => 'civicrm_im.provider_id',
'title' => ts('IM Provider'),
'type' => CRM_Utils_Type::T_INT,
),
);

Expand Down Expand Up @@ -1610,14 +1613,17 @@ public static function &exportableFields($contactType = 'Individual', $status =
'groups' => array(
'title' => ts('Group(s)'),
'name' => 'groups',
'type' => CRM_Utils_Type::T_TEXT,
),
'tags' => array(
'title' => ts('Tag(s)'),
'name' => 'tags',
'type' => CRM_Utils_Type::T_TEXT,
),
'notes' => array(
'title' => ts('Note(s)'),
'name' => 'notes',
'type' => CRM_Utils_Type::T_TEXT,
),
));
}
Expand Down
45 changes: 32 additions & 13 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,19 +472,7 @@ public function __construct(
$this->_skipPermission = TRUE;
}
else {
$this->_fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE, TRUE, FALSE, !$skipPermission);

$fields = CRM_Core_Component::getQueryFields(!$this->_skipPermission);
unset($fields['note']);
$this->_fields = array_merge($this->_fields, $fields);

// add activity fields
$fields = CRM_Activity_BAO_Activity::exportableFields();
$this->_fields = array_merge($this->_fields, $fields);

// add any fields provided by hook implementers
$extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields();
$this->_fields = array_merge($this->_fields, $extFields);
$this->setAllFieldMetadata(!$skipPermission);
}

// basically do all the work once, and then reuse it
Expand Down Expand Up @@ -6613,4 +6601,35 @@ public function getSelect() {
return $select;
}

/**
* Get metadata for all fields accessible by the contact.
*
* @param bool $isCheckPermissions
*/
public function setAllFieldMetadata($isCheckPermissions) {

$this->_fields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE, TRUE, FALSE, $isCheckPermissions);

$fields = CRM_Core_Component::getQueryFields($isCheckPermissions);
unset($fields['note']);
$this->_fields = array_merge($this->_fields, $fields);

// add activity fields
$fields = CRM_Activity_BAO_Activity::exportableFields();
$this->_fields = array_merge($this->_fields, $fields);

// add any fields provided by hook implementers
$extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields();
$this->_fields = array_merge($this->_fields, $extFields);
}

/**
* Get array of field metadata.
*
* @return array
*/
public function getFieldMetadata() {
return $this->_fields;
}

}
10 changes: 10 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ public static function &exportableFields($checkPermission = TRUE) {
'name' => 'contribution_page',
'where' => 'civicrm_contribution_page.title',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
);

Expand All @@ -829,12 +830,14 @@ public static function &exportableFields($checkPermission = TRUE) {
'title' => ts('Contribution Note'),
'name' => 'contribution_note',
'data_type' => CRM_Utils_Type::T_TEXT,
'type' => CRM_Utils_Type::T_TEXT,
),
);

$extraFields = array(
'contribution_batch' => array(
'title' => ts('Batch Name'),
'type' => CRM_Utils_Type::T_STRING,
),
);

Expand All @@ -845,6 +848,7 @@ public static function &exportableFields($checkPermission = TRUE) {
'name' => 'campaign_title',
'where' => 'civicrm_campaign.title',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
);
$softCreditFields = array(
Expand All @@ -853,30 +857,35 @@ public static function &exportableFields($checkPermission = TRUE) {
'title' => ts('Soft Credit For'),
'where' => 'civicrm_contact_d.display_name',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
'contribution_soft_credit_amount' => array(
'name' => 'contribution_soft_credit_amount',
'title' => ts('Soft Credit Amount'),
'where' => 'civicrm_contribution_soft.amount',
'data_type' => CRM_Utils_Type::T_MONEY,
'type' => CRM_Utils_Type::T_MONEY,
),
'contribution_soft_credit_type' => array(
'name' => 'contribution_soft_credit_type',
'title' => ts('Soft Credit Type'),
'where' => 'contribution_softcredit_type.label',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
'contribution_soft_credit_contribution_id' => array(
'name' => 'contribution_soft_credit_contribution_id',
'title' => ts('Soft Credit For Contribution ID'),
'where' => 'civicrm_contribution_soft.contribution_id',
'data_type' => CRM_Utils_Type::T_INT,
'type' => CRM_Utils_Type::T_INT,
),
'contribution_soft_credit_contact_id' => array(
'name' => 'contribution_soft_credit_contact_id',
'title' => ts('Soft Credit For Contact ID'),
'where' => 'civicrm_contact_d.id',
'data_type' => CRM_Utils_Type::T_INT,
'type' => CRM_Utils_Type::T_INT,
),
);

Expand All @@ -887,6 +896,7 @@ public static function &exportableFields($checkPermission = TRUE) {
'name' => 'contribution_product_id',
'where' => 'civicrm_product.id',
'data_type' => CRM_Utils_Type::T_INT,
'type' => CRM_Utils_Type::T_INT,
),
);

Expand Down
4 changes: 4 additions & 0 deletions CRM/Event/BAO/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ public static function &importableFields($contactType = 'Individual', $status =
'name' => 'participant_note',
'headerPattern' => '/(participant.)?note$/i',
'data_type' => CRM_Utils_Type::T_TEXT,
'type' => CRM_Utils_Type::T_TEXT,
),
);

Expand All @@ -660,6 +661,7 @@ public static function &importableFields($contactType = 'Individual', $status =
'title' => ts('Participant Status'),
'name' => 'participant_status',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
);
$tmpFields['participant_status_id']['title'] = ts('Participant Status Id');
Expand All @@ -671,6 +673,7 @@ public static function &importableFields($contactType = 'Individual', $status =
'title' => ts('Participant Role'),
'name' => 'participant_role',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
);
$tmpFields['participant_role_id']['title'] = ts('Participant Role Id');
Expand All @@ -680,6 +683,7 @@ public static function &importableFields($contactType = 'Individual', $status =
'title' => ts('Event Type'),
'name' => 'event_type',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
);

Expand Down
3 changes: 2 additions & 1 deletion CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,8 @@ public static function sqlColumnDefn($query, &$sqlColumns, $field) {
else {
// set the sql columns for custom data
if (isset($queryFields[$field]['data_type'])) {

// @todo strip out this code.
CRM_Core_Error::deprecatedFunctionWarning('ensure type is always defined (this is believed to already be the case');
switch ($queryFields[$field]['data_type']) {
case 'String':
// May be option labels, which could be up to 512 characters
Expand Down
18 changes: 11 additions & 7 deletions CRM/Pledge/BAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,17 @@ public static function exportableFields($checkPermission = TRUE) {

// set title to calculated fields
$calculatedFields = array(
'pledge_total_paid' => array('title' => ts('Total Paid')),
'pledge_balance_amount' => array('title' => ts('Balance Amount')),
'pledge_next_pay_date' => array('title' => ts('Next Payment Date')),
'pledge_next_pay_amount' => array('title' => ts('Next Payment Amount')),
'pledge_payment_paid_amount' => array('title' => ts('Paid Amount')),
'pledge_payment_paid_date' => array('title' => ts('Paid Date')),
'pledge_total_paid' => array('title' => ts('Total Paid'), 'type' => CRM_Utils_Type::T_MONEY),
'pledge_balance_amount' => array('title' => ts('Balance Amount'), 'type' => CRM_Utils_Type::T_MONEY),
'pledge_next_pay_date' => array('title' => ts('Next Payment Date'), 'type' => CRM_Utils_Type::T_DATE),
'pledge_next_pay_amount' => array('title' => ts('Next Payment Amount'), 'type' => CRM_Utils_Type::T_MONEY),
'pledge_payment_paid_amount' => array('title' => ts('Paid Amount'), 'type' => CRM_Utils_Type::T_MONEY),
'pledge_payment_paid_date' => array('title' => ts('Paid Date'), 'type' => CRM_Utils_Type::T_DATE),
'pledge_payment_status' => array(
'title' => ts('Pledge Payment Status'),
'name' => 'pledge_payment_status',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
);

Expand All @@ -751,21 +752,24 @@ public static function exportableFields($checkPermission = TRUE) {
'title' => ts('Pledge Status'),
'name' => 'pledge_status',
'data_type' => CRM_Utils_Type::T_STRING,
'type' => CRM_Utils_Type::T_STRING,
),
'pledge_frequency_unit' => array(
'title' => ts('Pledge Frequency Unit'),
'name' => 'pledge_frequency_unit',
'data_type' => CRM_Utils_Type::T_ENUM,
'type' => CRM_Utils_Type::T_STRING,
),
'pledge_frequency_interval' => array(
'title' => ts('Pledge Frequency Interval'),
'name' => 'pledge_frequency_interval',
'data_type' => CRM_Utils_Type::T_INT,
'type' => CRM_Utils_Type::T_INT,
),
'pledge_contribution_page_id' => array(
'title' => ts('Pledge Contribution Page Id'),
'name' => 'pledge_contribution_page_id',
'data_type' => CRM_Utils_Type::T_INT,
'type' => CRM_Utils_Type::T_INT,
),
);

Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,4 +709,16 @@ public function testConvertFormValuesCRM21816() {
$this->assertEquals($modparams['member_is_primary'][2], $fv_orig['member_is_primary']);
}

/**
* Tests that 'type' is defined for all metadata fields.
*/
public function testFieldMetadata() {
$query = new CRM_Contact_BAO_Query();
$fields = $query->getFieldMetadata();
foreach ($fields as $field) {
$this->assertTrue(!empty($field['type']), print_r($field, 1));
$this->assertTrue(!empty(CRM_Utils_Type::typeToString($field['type'])));
}
}

}