aea55a1bbf6a00182b0d4a846f8e10b448e773ff
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Membership.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_Membership extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
36
37 /**
38 * Class constructor.
39 */
40 public function __construct() {
41 parent::__construct('Membership', ts('Memberships'));
42 }
43
44 public function isActive() {
45 $config = CRM_Core_Config::singleton();
46 return in_array('CiviMember', $config->enableComponents) &&
47 CRM_Core_Permission::check('access CiviMember');
48 }
49
50 /**
51 * @inheritDoc
52 */
53 public function fillTempTable($queryText, $entityIDTableName, $toTable, $queryLimit, $detailLimit) {
54 $queries = $this->prepareQueries($queryText, $entityIDTableName);
55 $result = $this->runQueries($queryText, $queries, $entityIDTableName, $queryLimit);
56 $this->moveIDs($entityIDTableName, $toTable, $detailLimit);
57 if (!empty($result['files'])) {
58 $this->moveFileIDs($toTable, 'membership_id', $result['files']);
59 }
60 return $result;
61 }
62
63 /**
64 * Get membership ids in entity tables.
65 *
66 * @param string $queryText
67 * @param string $entityIDTableName
68 * @return array
69 * list tables/queries (for runQueries)
70 */
71 public function prepareQueries($queryText, $entityIDTableName) {
72 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
73
74 $contactSQL = array();
75 $contactSQL[] = "
76 SELECT distinct cm.id
77 FROM civicrm_membership cm
78 INNER JOIN civicrm_contact c ON cm.contact_id = c.id
79 WHERE ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
80 ";
81 $tables = array(
82 'civicrm_membership' => array(
83 'id' => 'id',
84 'fields' => array('source' => NULL),
85 ),
86 'file' => array(
87 'xparent_table' => 'civicrm_membership',
88 ),
89 'sql' => $contactSQL,
90 );
91
92 // get the custom data info
93 $this->fillCustomInfo($tables, "( 'Membership' )");
94 return $tables;
95 }
96
97 public function moveIDs($fromTable, $toTable, $limit) {
98 $sql = "
99 INSERT INTO {$toTable}
100 ( table_name, contact_id, sort_name, membership_id, membership_type, membership_fee, membership_start_date,
101 membership_end_date, membership_source, membership_status )
102 SELECT 'Membership', c.id, c.sort_name, cm.id, cmt.name, cc.total_amount, cm.start_date, cm.end_date, cm.source, cms.name
103 FROM {$fromTable} ct
104 INNER JOIN civicrm_membership cm ON cm.id = ct.entity_id
105 LEFT JOIN civicrm_contact c ON cm.contact_id = c.id
106 LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id
107 LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
108 LEFT JOIN civicrm_contribution cc ON cc.id = cmp.contribution_id
109 LEFT JOIN civicrm_membership_status cms ON cms.id = cm.status_id
110 {$this->toLimit($limit)}
111 ";
112 CRM_Core_DAO::executeQuery($sql);
113 }
114
115 }