Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Membership.php
CommitLineData
4f5de903
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
4f5de903 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
4f5de903
TO
32 */
33class CRM_Contact_Form_Search_Custom_FullText_Membership 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('Membership', ts('Memberships'));
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('CiviMember', $config->enableComponents) &&
353ffa53 50 CRM_Core_Permission::check('access CiviMember');
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, 'membership_id', $result['files']);
62 }
180ed6f5 63 return $result;
4f5de903
TO
64 }
65
66 /**
100fef9d 67 * Get membership ids in entity tables.
4f5de903
TO
68 *
69 * @param string $queryText
180ed6f5 70 * @param string $entityIDTableName
5a409b50 71 *
a6c01b45
CW
72 * @return array
73 * list tables/queries (for runQueries)
4f5de903 74 */
00be9182 75 public function prepareQueries($queryText, $entityIDTableName) {
a04af6db
TO
76 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
77
4f5de903
TO
78 $contactSQL = array();
79 $contactSQL[] = "
80SELECT distinct cm.id
81FROM civicrm_membership cm
82INNER JOIN civicrm_contact c ON cm.contact_id = c.id
a04af6db 83WHERE ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
4f5de903
TO
84";
85 $tables = array(
86 'civicrm_membership' => array(
87 'id' => 'id',
88 'fields' => array('source' => NULL),
89 ),
cac9c01d
TO
90 'file' => array(
91 'xparent_table' => 'civicrm_membership',
92 ),
4f5de903
TO
93 'sql' => $contactSQL,
94 );
95
96 // get the custom data info
97 $this->fillCustomInfo($tables, "( 'Membership' )");
180ed6f5 98 return $tables;
4f5de903
TO
99 }
100
0e2e76cf
EM
101 /**
102 * Move IDs.
103 *
104 * @param string $fromTable
105 * @param string $toTable
106 * @param int $limit
107 */
613e5116 108 public function moveIDs($fromTable, $toTable, $limit) {
4f5de903
TO
109 $sql = "
110INSERT INTO {$toTable}
111( table_name, contact_id, sort_name, membership_id, membership_type, membership_fee, membership_start_date,
112membership_end_date, membership_source, membership_status )
113 SELECT 'Membership', c.id, c.sort_name, cm.id, cmt.name, cc.total_amount, cm.start_date, cm.end_date, cm.source, cms.name
114 FROM {$fromTable} ct
115INNER JOIN civicrm_membership cm ON cm.id = ct.entity_id
116LEFT JOIN civicrm_contact c ON cm.contact_id = c.id
117LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id
118LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
119LEFT JOIN civicrm_contribution cc ON cc.id = cmp.contribution_id
120LEFT JOIN civicrm_membership_status cms ON cms.id = cm.status_id
7296606d 121{$this->toLimit($limit)}
4f5de903
TO
122";
123 CRM_Core_DAO::executeQuery($sql);
124 }
125
ef10e0b5 126}