293b728a6186820ff5b413246b934204d3347b8f
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Contribution.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Contact_Form_Search_Custom_FullText_Contribution extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct('Contribution', ts('Contributions'));
40 }
41
42 /**
43 * Check if search is permitted.
44 *
45 * @return bool
46 */
47 public function isActive() {
48 $config = CRM_Core_Config::singleton();
49 return in_array('CiviContribute', $config->enableComponents) &&
50 CRM_Core_Permission::check('access CiviContribute');
51 }
52
53 /**
54 * @inheritDoc
55 */
56 public function fillTempTable($queryText, $entityIDTableName, $toTable, $queryLimit, $detailLimit) {
57 $queries = $this->prepareQueries($queryText, $entityIDTableName);
58 $result = $this->runQueries($queryText, $queries, $entityIDTableName, $queryLimit);
59 $this->moveIDs($entityIDTableName, $toTable, $detailLimit);
60 if (!empty($result['files'])) {
61 $this->moveFileIDs($toTable, 'contribution_id', $result['files']);
62 }
63 return $result;
64 }
65
66 /**
67 * Get contribution ids in entity tables.
68 *
69 * @param string $queryText
70 * @param string $entityIDTableName
71 * @return array
72 * list tables/queries (for runQueries)
73 */
74 public function prepareQueries($queryText, $entityIDTableName) {
75 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
76
77 $contactSQL = [];
78 $contactSQL[] = "
79 SELECT distinct cc.id
80 FROM civicrm_contribution cc
81 INNER JOIN civicrm_contact c ON cc.contact_id = c.id
82 WHERE ({$this->matchText('civicrm_contact c', ['sort_name', 'display_name', 'nick_name'], $queryText)})
83 ";
84 $tables = [
85 'civicrm_contribution' => [
86 'id' => 'id',
87 'fields' => [
88 'source' => NULL,
89 'amount_level' => NULL,
90 'trxn_Id' => NULL,
91 'invoice_id' => NULL,
92 // Odd: This is really a VARCHAR, so why are we searching like an INT?
93 'check_number' => 'Int',
94 'total_amount' => 'Int',
95 ],
96 ],
97 'file' => [
98 'xparent_table' => 'civicrm_contribution',
99 ],
100 'sql' => $contactSQL,
101 'civicrm_note' => [
102 'id' => 'entity_id',
103 'entity_table' => 'civicrm_contribution',
104 'fields' => [
105 'subject' => NULL,
106 'note' => NULL,
107 ],
108 ],
109 ];
110
111 // get the custom data info
112 $this->fillCustomInfo($tables, "( 'Contribution' )");
113 return $tables;
114 }
115
116 /**
117 * Move IDs.
118 *
119 * @param string $fromTable
120 * @param string $toTable
121 * @param int $limit
122 */
123 public function moveIDs($fromTable, $toTable, $limit) {
124 $sql = "
125 INSERT 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
131 INNER JOIN civicrm_contribution cc ON cc.id = ct.entity_id
132 LEFT JOIN civicrm_contact c ON cc.contact_id = c.id
133 LEFT JOIN civicrm_financial_type cct ON cct.id = cc.financial_type_id
134 LEFT JOIN civicrm_contribution_page ccp ON ccp.id = cc.contribution_page_id
135 LEFT JOIN civicrm_option_group option_group_contributionStatus ON option_group_contributionStatus.name = 'contribution_status'
136 LEFT 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 )
138 {$this->toLimit($limit)}
139 ";
140 CRM_Core_DAO::executeQuery($sql);
141 }
142
143 }