5d3832974049baac708b3b0ecc6cec877c24a1e4
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Activity.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_Activity extends CRM_Contact_Form_Search_Custom_FullText_AbstractPartialQuery {
36
37 function __construct() {
38 parent::__construct('Activity', ts('Activities'));
39 }
40
41 function isActive() {
42 return CRM_Core_Permission::check('view all activities');
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function fillTempTable($queryText, $entityIDTableName, $toTable, $queryLimit, $detailLimit) {
49 $queries = $this->prepareQueries($queryText, $entityIDTableName);
50 $result = $this->runQueries($queryText, $queries, $entityIDTableName, $queryLimit);
51 $this->moveActivityIDs($entityIDTableName, $toTable, $detailLimit);
52 return $result;
53 }
54
55 /**
56 * @param string $queryText
57 * @param string $entityIDTableName
58 * @return array list tables/queries (for runQueries)
59 */
60 function prepareQueries($queryText, $entityIDTableName) {
61 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
62
63 $contactSQL = array();
64
65 $contactSQL[] = "
66 SELECT distinct ca.id
67 FROM civicrm_activity ca
68 INNER JOIN civicrm_activity_contact cat ON cat.activity_id = ca.id
69 INNER JOIN civicrm_contact c ON cat.contact_id = c.id
70 LEFT JOIN civicrm_email e ON cat.contact_id = e.contact_id
71 LEFT JOIN civicrm_option_group og ON og.name = 'activity_type'
72 LEFT JOIN civicrm_option_value ov ON ( ov.option_group_id = og.id )
73 WHERE (
74 ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
75 OR
76 ({$this->matchText('civicrm_email e', 'email', $queryText)} AND ca.activity_type_id = ov.value AND ov.name IN ('Inbound Email', 'Email') )
77 )
78 AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
79 AND (c.is_deleted = 0 OR c.is_deleted IS NULL)
80 ";
81
82 $contactSQL[] = "
83 SELECT et.entity_id
84 FROM civicrm_entity_tag et
85 INNER JOIN civicrm_tag t ON et.tag_id = t.id
86 INNER JOIN civicrm_activity ca ON et.entity_id = ca.id
87 WHERE et.entity_table = 'civicrm_activity'
88 AND et.tag_id = t.id
89 AND ({$this->matchText('civicrm_tag t', 'name', $queryText)})
90 AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
91 GROUP BY et.entity_id
92 ";
93
94 $contactSQL[] = "
95 SELECT distinct ca.id
96 FROM civicrm_activity ca
97 WHERE ({$this->matchText('civicrm_activity ca', array('subject', 'details'), $queryText)})
98 AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
99 ";
100
101 $final = array();
102
103 $tables = array(
104 'civicrm_activity' => array('fields' => array()),
105 'sql' => $contactSQL,
106 'final' => $final,
107 );
108
109 $this->fillCustomInfo($tables, "( 'Activity' )");
110 return $tables;;
111 }
112
113 public function moveActivityIDs($fromTable, $toTable, $limit) {
114 $sql = "
115 INSERT INTO {$toTable}
116 ( table_name, activity_id, subject, details, contact_id, sort_name, record_type,
117 activity_type_id, case_id, client_id )
118 SELECT 'Activity', ca.id, substr(ca.subject, 1, 50), substr(ca.details, 1, 250),
119 c1.id, c1.sort_name, cac.record_type_id,
120 ca.activity_type_id,
121 cca.case_id,
122 ccc.contact_id as client_id
123 FROM {$fromTable} eid
124 INNER JOIN civicrm_activity ca ON ca.id = eid.entity_id
125 INNER JOIN civicrm_activity_contact cac ON cac.activity_id = ca.id
126 INNER JOIN civicrm_contact c1 ON cac.contact_id = c1.id
127 LEFT JOIN civicrm_case_activity cca ON cca.activity_id = ca.id
128 LEFT JOIN civicrm_case_contact ccc ON ccc.case_id = cca.case_id
129 WHERE (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
130 GROUP BY ca.id
131 {$this->toLimit($limit)}
132 ";
133 CRM_Core_DAO::executeQuery($sql);
134 }
135
136 }