make it compatible with only 5.7+
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Activity.php
CommitLineData
4f5de903
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
4f5de903 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
4f5de903
TO
32 */
33class CRM_Contact_Form_Search_Custom_FullText_Activity extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
34
cd5823ae
EM
35 /**
36 * Class constructor.
37 */
00be9182 38 public function __construct() {
4f5de903
TO
39 parent::__construct('Activity', ts('Activities'));
40 }
41
cd5823ae 42 /**
2e2605fe
EM
43 * Is search active for this user.
44 *
cd5823ae
EM
45 * @return bool
46 */
00be9182 47 public function isActive() {
4f5de903
TO
48 return CRM_Core_Permission::check('view all activities');
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, 'activity_id', $result['files']);
60 }
180ed6f5 61 return $result;
4f5de903
TO
62 }
63
64 /**
65 * @param string $queryText
180ed6f5 66 * @param string $entityIDTableName
5a409b50 67 *
a6c01b45
CW
68 * @return array
69 * list tables/queries (for runQueries)
4f5de903 70 */
00be9182 71 public function prepareQueries($queryText, $entityIDTableName) {
a04af6db
TO
72 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
73
4f5de903
TO
74 $contactSQL = array();
75
76 $contactSQL[] = "
77SELECT distinct ca.id
78FROM civicrm_activity ca
79INNER JOIN civicrm_activity_contact cat ON cat.activity_id = ca.id
80INNER JOIN civicrm_contact c ON cat.contact_id = c.id
81LEFT JOIN civicrm_email e ON cat.contact_id = e.contact_id
82LEFT JOIN civicrm_option_group og ON og.name = 'activity_type'
83LEFT JOIN civicrm_option_value ov ON ( ov.option_group_id = og.id )
a04af6db
TO
84WHERE (
85 ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
86 OR
87 ({$this->matchText('civicrm_email e', 'email', $queryText)} AND ca.activity_type_id = ov.value AND ov.name IN ('Inbound Email', 'Email') )
88 )
4f5de903
TO
89AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
90AND (c.is_deleted = 0 OR c.is_deleted IS NULL)
91";
92
93 $contactSQL[] = "
94SELECT et.entity_id
95FROM civicrm_entity_tag et
96INNER JOIN civicrm_tag t ON et.tag_id = t.id
97INNER JOIN civicrm_activity ca ON et.entity_id = ca.id
98WHERE et.entity_table = 'civicrm_activity'
99AND et.tag_id = t.id
a04af6db 100AND ({$this->matchText('civicrm_tag t', 'name', $queryText)})
4f5de903
TO
101AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
102GROUP BY et.entity_id
103";
104
105 $contactSQL[] = "
106SELECT distinct ca.id
107FROM civicrm_activity ca
a04af6db 108WHERE ({$this->matchText('civicrm_activity ca', array('subject', 'details'), $queryText)})
4f5de903
TO
109AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
110";
111
112 $final = array();
113
114 $tables = array(
115 'civicrm_activity' => array('fields' => array()),
cac9c01d
TO
116 'file' => array(
117 'xparent_table' => 'civicrm_activity',
118 ),
4f5de903
TO
119 'sql' => $contactSQL,
120 'final' => $final,
121 );
122
123 $this->fillCustomInfo($tables, "( 'Activity' )");
180ed6f5 124 return $tables;;
4f5de903
TO
125 }
126
2e2605fe
EM
127 /**
128 * Move IDs.
129 *
130 * @param string $fromTable
131 * @param string $toTable
132 * @param int $limit
133 */
613e5116 134 public function moveIDs($fromTable, $toTable, $limit) {
4f5de903
TO
135 $sql = "
136INSERT INTO {$toTable}
137( table_name, activity_id, subject, details, contact_id, sort_name, record_type,
138 activity_type_id, case_id, client_id )
3636b520 139SELECT 'Activity', ca.id, substr(ca.subject, 1, 50), substr(ca.details, 1, 250),
140 c1.id, c1.sort_name, cac.record_type_id,
141 ca.activity_type_id,
142 cca.case_id,
4f5de903
TO
143 ccc.contact_id as client_id
144FROM {$fromTable} eid
145INNER JOIN civicrm_activity ca ON ca.id = eid.entity_id
146INNER JOIN civicrm_activity_contact cac ON cac.activity_id = ca.id
147INNER JOIN civicrm_contact c1 ON cac.contact_id = c1.id
148LEFT JOIN civicrm_case_activity cca ON cca.activity_id = ca.id
149LEFT JOIN civicrm_case_contact ccc ON ccc.case_id = cca.case_id
150WHERE (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
7296606d 151{$this->toLimit($limit)}
4f5de903
TO
152";
153 CRM_Core_DAO::executeQuery($sql);
154 }
155
ef10e0b5 156}