more comment fixes
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Contact.php
CommitLineData
4f5de903
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
4f5de903
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
4f5de903
TO
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35class CRM_Contact_Form_Search_Custom_FullText_Contact extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
36
4b62bc4f
EM
37 /**
38 * Class constructor.
39 */
00be9182 40 public function __construct() {
4f5de903
TO
41 parent::__construct('Contact', ts('Contacts'));
42 }
43
cd5823ae
EM
44 /**
45 * @return bool
46 */
00be9182 47 public function isActive() {
4f5de903
TO
48 return CRM_Core_Permission::check('view all contacts');
49 }
50
51 /**
e7c15cb6 52 * @inheritDoc
4f5de903
TO
53 */
54 public function fillTempTable($queryText, $entityIDTableName, $toTable, $queryLimit, $detailLimit) {
180ed6f5
TO
55 $queries = $this->prepareQueries($queryText, $entityIDTableName);
56 $result = $this->runQueries($queryText, $queries, $entityIDTableName, $queryLimit);
613e5116 57 $this->moveIDs($entityIDTableName, $toTable, $detailLimit);
cac9c01d
TO
58 if (!empty($result['files'])) {
59 $this->moveFileIDs($toTable, 'contact_id', $result['files']);
60 }
180ed6f5 61 return $result;
4f5de903
TO
62 }
63
64 /**
65 * @param string $queryText
180ed6f5 66 * @param string $entityIDTableName
a6c01b45
CW
67 * @return array
68 * list tables/queries (for runQueries)
4f5de903 69 */
00be9182 70 public function prepareQueries($queryText, $entityIDTableName) {
a04af6db
TO
71 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
72
4f5de903
TO
73 $contactSQL = array();
74 $contactSQL[] = "
75SELECT et.entity_id
76FROM civicrm_entity_tag et
77INNER JOIN civicrm_tag t ON et.tag_id = t.id
78WHERE et.entity_table = 'civicrm_contact'
79AND et.tag_id = t.id
a04af6db 80AND ({$this->matchText('civicrm_tag t', 'name', $queryText)})
4f5de903
TO
81GROUP BY et.entity_id
82";
83
84 // lets delete all the deceased contacts from the entityID box
85 // this allows us to keep numbers in sync
86 // when we have acl contacts, the situation gets even more murky
87 $final = array();
88 $final[] = "DELETE FROM {$entityIDTableName} WHERE entity_id IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)";
89
90 $tables = array(
91 'civicrm_contact' => array(
92 'id' => 'id',
93 'fields' => array(
94 'sort_name' => NULL,
95 'nick_name' => NULL,
96 'display_name' => NULL,
97 ),
98 ),
99 'civicrm_address' => array(
100 'id' => 'contact_id',
101 'fields' => array(
102 'street_address' => NULL,
103 'city' => NULL,
104 'postal_code' => NULL,
105 ),
106 ),
107 'civicrm_email' => array(
108 'id' => 'contact_id',
109 'fields' => array('email' => NULL),
110 ),
111 'civicrm_phone' => array(
112 'id' => 'contact_id',
113 'fields' => array('phone' => NULL),
114 ),
115 'civicrm_note' => array(
116 'id' => 'entity_id',
117 'entity_table' => 'civicrm_contact',
118 'fields' => array(
119 'subject' => NULL,
120 'note' => NULL,
121 ),
122 ),
cac9c01d
TO
123 'file' => array(
124 'xparent_table' => 'civicrm_contact',
125 ),
4f5de903
TO
126 'sql' => $contactSQL,
127 'final' => $final,
128 );
129
130 // get the custom data info
131 $this->fillCustomInfo($tables,
132 "( 'Contact', 'Individual', 'Organization', 'Household' )"
133 );
134
180ed6f5 135 return $tables;
4f5de903
TO
136 }
137
2e2605fe
EM
138 /**
139 * Move IDs.
140 *
141 * @param $fromTable
142 * @param $toTable
143 * @param $limit
144 */
613e5116 145 public function moveIDs($fromTable, $toTable, $limit) {
4f5de903
TO
146 $sql = "
147INSERT INTO {$toTable}
148( id, contact_id, sort_name, display_name, table_name )
149SELECT c.id, ct.entity_id, c.sort_name, c.display_name, 'Contact'
150 FROM {$fromTable} ct
151INNER JOIN civicrm_contact c ON ct.entity_id = c.id
7296606d 152{$this->toLimit($limit)}
4f5de903
TO
153";
154 CRM_Core_DAO::executeQuery($sql);
155 }
156
ef10e0b5 157}