Merge pull request #3453 from kenwest/CRM-14813
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Membership.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function __construct() {
38 parent::__construct('Membership', ts('Memberships'));
39 }
40
41 function isActive() {
42 $config = CRM_Core_Config::singleton();
43 return in_array('CiviMember', $config->enableComponents) &&
44 CRM_Core_Permission::check('access CiviMember');
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function fillTempTable($queryText, $entityIDTableName, $toTable, $queryLimit, $detailLimit) {
51 $count = $this->fillMembershipIDs($queryText, $entityIDTableName, $queryLimit);
52 $this->moveMembershipIDs($entityIDTableName, $toTable, $detailLimit);
53 return $count;
54 }
55
56 /**
57 * get membership ids in entity tables.
58 *
59 * @param string $queryText
60 * @return int the total number of matches
61 */
62 function fillMembershipIDs($queryText, $entityIDTableName, $limit) {
63 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
64
65 $contactSQL = array();
66 $contactSQL[] = "
67 SELECT distinct cm.id
68 FROM civicrm_membership cm
69 INNER JOIN civicrm_contact c ON cm.contact_id = c.id
70 WHERE ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
71 ";
72 $tables = array(
73 'civicrm_membership' => array(
74 'id' => 'id',
75 'fields' => array('source' => NULL),
76 ),
77 'sql' => $contactSQL,
78 );
79
80 // get the custom data info
81 $this->fillCustomInfo($tables, "( 'Membership' )");
82 return $this->runQueries($queryText, $tables, $entityIDTableName, $limit);
83 }
84
85 public function moveMembershipIDs($fromTable, $toTable, $limit) {
86 $sql = "
87 INSERT INTO {$toTable}
88 ( table_name, contact_id, sort_name, membership_id, membership_type, membership_fee, membership_start_date,
89 membership_end_date, membership_source, membership_status )
90 SELECT 'Membership', c.id, c.sort_name, cm.id, cmt.name, cc.total_amount, cm.start_date, cm.end_date, cm.source, cms.name
91 FROM {$fromTable} ct
92 INNER JOIN civicrm_membership cm ON cm.id = ct.entity_id
93 LEFT JOIN civicrm_contact c ON cm.contact_id = c.id
94 LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id
95 LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
96 LEFT JOIN civicrm_contribution cc ON cc.id = cmp.contribution_id
97 LEFT JOIN civicrm_membership_status cms ON cms.id = cm.status_id
98 {$this->toLimit($limit)}
99 ";
100 CRM_Core_DAO::executeQuery($sql);
101 }
102
103 }