(NFC) Update CRM/Contact to match new coder style
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Contribution.php
CommitLineData
4f5de903
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
4f5de903 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
4f5de903
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
4f5de903
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
4f5de903
TO
32 */
33class CRM_Contact_Form_Search_Custom_FullText_Contribution extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
34
4b62bc4f
EM
35 /**
36 * Class constructor.
37 */
00be9182 38 public function __construct() {
4f5de903
TO
39 parent::__construct('Contribution', ts('Contributions'));
40 }
41
cd5823ae 42 /**
5a409b50 43 * Check if search is permitted.
44 *
cd5823ae
EM
45 * @return bool
46 */
00be9182 47 public function isActive() {
4f5de903
TO
48 $config = CRM_Core_Config::singleton();
49 return in_array('CiviContribute', $config->enableComponents) &&
353ffa53 50 CRM_Core_Permission::check('access CiviContribute');
4f5de903
TO
51 }
52
53 /**
e7c15cb6 54 * @inheritDoc
4f5de903
TO
55 */
56 public function fillTempTable($queryText, $entityIDTableName, $toTable, $queryLimit, $detailLimit) {
180ed6f5
TO
57 $queries = $this->prepareQueries($queryText, $entityIDTableName);
58 $result = $this->runQueries($queryText, $queries, $entityIDTableName, $queryLimit);
613e5116 59 $this->moveIDs($entityIDTableName, $toTable, $detailLimit);
cac9c01d
TO
60 if (!empty($result['files'])) {
61 $this->moveFileIDs($toTable, 'contribution_id', $result['files']);
62 }
180ed6f5 63 return $result;
4f5de903
TO
64 }
65
66 /**
100fef9d 67 * Get contribution ids in entity tables.
4f5de903
TO
68 *
69 * @param string $queryText
180ed6f5 70 * @param string $entityIDTableName
a6c01b45
CW
71 * @return array
72 * list tables/queries (for runQueries)
4f5de903 73 */
00be9182 74 public function prepareQueries($queryText, $entityIDTableName) {
a04af6db
TO
75 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
76
be2fb01f 77 $contactSQL = [];
4f5de903
TO
78 $contactSQL[] = "
79SELECT distinct cc.id
80FROM civicrm_contribution cc
81INNER JOIN civicrm_contact c ON cc.contact_id = c.id
be2fb01f 82WHERE ({$this->matchText('civicrm_contact c', ['sort_name', 'display_name', 'nick_name'], $queryText)})
4f5de903 83";
be2fb01f
CW
84 $tables = [
85 'civicrm_contribution' => [
4f5de903 86 'id' => 'id',
be2fb01f 87 'fields' => [
4f5de903
TO
88 'source' => NULL,
89 'amount_level' => NULL,
90 'trxn_Id' => NULL,
91 'invoice_id' => NULL,
69078420
SL
92 // Odd: This is really a VARCHAR, so why are we searching like an INT?
93 'check_number' => 'Int',
a04af6db 94 'total_amount' => 'Int',
be2fb01f
CW
95 ],
96 ],
97 'file' => [
cac9c01d 98 'xparent_table' => 'civicrm_contribution',
be2fb01f 99 ],
4f5de903 100 'sql' => $contactSQL,
be2fb01f 101 'civicrm_note' => [
4f5de903
TO
102 'id' => 'entity_id',
103 'entity_table' => 'civicrm_contribution',
be2fb01f 104 'fields' => [
4f5de903
TO
105 'subject' => NULL,
106 'note' => NULL,
be2fb01f
CW
107 ],
108 ],
109 ];
4f5de903
TO
110
111 // get the custom data info
112 $this->fillCustomInfo($tables, "( 'Contribution' )");
180ed6f5 113 return $tables;
4f5de903
TO
114 }
115
b896fa44
EM
116 /**
117 * Move IDs.
118 *
119 * @param string $fromTable
120 * @param string $toTable
121 * @param int $limit
122 */
613e5116 123 public function moveIDs($fromTable, $toTable, $limit) {
4f5de903
TO
124 $sql = "
125INSERT INTO {$toTable}
126( table_name, contact_id, sort_name, contribution_id, financial_type, contribution_page, contribution_receive_date,
127 contribution_total_amount, contribution_trxn_Id, contribution_source, contribution_status, contribution_check_number )
128 SELECT 'Contribution', c.id, c.sort_name, cc.id, cct.name, ccp.title, cc.receive_date,
129 cc.total_amount, cc.trxn_id, cc.source, contribution_status.label, cc.check_number
130 FROM {$fromTable} ct
131INNER JOIN civicrm_contribution cc ON cc.id = ct.entity_id
132LEFT JOIN civicrm_contact c ON cc.contact_id = c.id
133LEFT JOIN civicrm_financial_type cct ON cct.id = cc.financial_type_id
134LEFT JOIN civicrm_contribution_page ccp ON ccp.id = cc.contribution_page_id
135LEFT JOIN civicrm_option_group option_group_contributionStatus ON option_group_contributionStatus.name = 'contribution_status'
136LEFT JOIN civicrm_option_value contribution_status ON
137( contribution_status.option_group_id = option_group_contributionStatus.id AND contribution_status.value = cc.contribution_status_id )
7296606d 138{$this->toLimit($limit)}
4f5de903
TO
139";
140 CRM_Core_DAO::executeQuery($sql);
141 }
142
ef10e0b5 143}