357c2e3adb7674eb133426fd471c52dda6f8d939
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Membership.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33 class CRM_Contact_Form_Search_Custom_FullText_Membership extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct('Membership', ts('Memberships'));
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('CiviMember', $config->enableComponents) &&
50 CRM_Core_Permission::check('access CiviMember');
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, 'membership_id', $result['files']);
62 }
63 return $result;
64 }
65
66 /**
67 * Get membership ids in entity tables.
68 *
69 * @param string $queryText
70 * @param string $entityIDTableName
71 *
72 * @return array
73 * list tables/queries (for runQueries)
74 */
75 public function prepareQueries($queryText, $entityIDTableName) {
76 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
77
78 $contactSQL = array();
79 $contactSQL[] = "
80 SELECT distinct cm.id
81 FROM civicrm_membership cm
82 INNER JOIN civicrm_contact c ON cm.contact_id = c.id
83 WHERE ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
84 ";
85 $tables = array(
86 'civicrm_membership' => array(
87 'id' => 'id',
88 'fields' => array('source' => NULL),
89 ),
90 'file' => array(
91 'xparent_table' => 'civicrm_membership',
92 ),
93 'sql' => $contactSQL,
94 );
95
96 // get the custom data info
97 $this->fillCustomInfo($tables, "( 'Membership' )");
98 return $tables;
99 }
100
101 /**
102 * Move IDs.
103 *
104 * @param string $fromTable
105 * @param string $toTable
106 * @param int $limit
107 */
108 public function moveIDs($fromTable, $toTable, $limit) {
109 $sql = "
110 INSERT INTO {$toTable}
111 ( table_name, contact_id, sort_name, membership_id, membership_type, membership_fee, membership_start_date,
112 membership_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
115 INNER JOIN civicrm_membership cm ON cm.id = ct.entity_id
116 LEFT JOIN civicrm_contact c ON cm.contact_id = c.id
117 LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id
118 LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
119 LEFT JOIN civicrm_contribution cc ON cc.id = cmp.contribution_id
120 LEFT JOIN civicrm_membership_status cms ON cms.id = cm.status_id
121 {$this->toLimit($limit)}
122 ";
123 CRM_Core_DAO::executeQuery($sql);
124 }
125
126 }