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