CRM-14765 - FullText - Display file matches as paperclip icons
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / FullText / Activity.php
CommitLineData
4f5de903
TO
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 */
35class 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) {
180ed6f5
TO
49 $queries = $this->prepareQueries($queryText, $entityIDTableName);
50 $result = $this->runQueries($queryText, $queries, $entityIDTableName, $queryLimit);
613e5116 51 $this->moveIDs($entityIDTableName, $toTable, $detailLimit);
cac9c01d
TO
52 if (!empty($result['files'])) {
53 $this->moveFileIDs($toTable, 'activity_id', $result['files']);
54 }
180ed6f5 55 return $result;
4f5de903
TO
56 }
57
58 /**
59 * @param string $queryText
180ed6f5
TO
60 * @param string $entityIDTableName
61 * @return array list tables/queries (for runQueries)
4f5de903 62 */
180ed6f5 63 function prepareQueries($queryText, $entityIDTableName) {
a04af6db
TO
64 // Note: For available full-text indices, see CRM_Core_InnoDBIndexer
65
4f5de903
TO
66 $contactSQL = array();
67
68 $contactSQL[] = "
69SELECT distinct ca.id
70FROM civicrm_activity ca
71INNER JOIN civicrm_activity_contact cat ON cat.activity_id = ca.id
72INNER JOIN civicrm_contact c ON cat.contact_id = c.id
73LEFT JOIN civicrm_email e ON cat.contact_id = e.contact_id
74LEFT JOIN civicrm_option_group og ON og.name = 'activity_type'
75LEFT JOIN civicrm_option_value ov ON ( ov.option_group_id = og.id )
a04af6db
TO
76WHERE (
77 ({$this->matchText('civicrm_contact c', array('sort_name', 'display_name', 'nick_name'), $queryText)})
78 OR
79 ({$this->matchText('civicrm_email e', 'email', $queryText)} AND ca.activity_type_id = ov.value AND ov.name IN ('Inbound Email', 'Email') )
80 )
4f5de903
TO
81AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
82AND (c.is_deleted = 0 OR c.is_deleted IS NULL)
83";
84
85 $contactSQL[] = "
86SELECT et.entity_id
87FROM civicrm_entity_tag et
88INNER JOIN civicrm_tag t ON et.tag_id = t.id
89INNER JOIN civicrm_activity ca ON et.entity_id = ca.id
90WHERE et.entity_table = 'civicrm_activity'
91AND et.tag_id = t.id
a04af6db 92AND ({$this->matchText('civicrm_tag t', 'name', $queryText)})
4f5de903
TO
93AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
94GROUP BY et.entity_id
95";
96
97 $contactSQL[] = "
98SELECT distinct ca.id
99FROM civicrm_activity ca
a04af6db 100WHERE ({$this->matchText('civicrm_activity ca', array('subject', 'details'), $queryText)})
4f5de903
TO
101AND (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
102";
103
104 $final = array();
105
106 $tables = array(
107 'civicrm_activity' => array('fields' => array()),
cac9c01d
TO
108 'file' => array(
109 'xparent_table' => 'civicrm_activity',
110 ),
4f5de903
TO
111 'sql' => $contactSQL,
112 'final' => $final,
113 );
114
115 $this->fillCustomInfo($tables, "( 'Activity' )");
180ed6f5 116 return $tables;;
4f5de903
TO
117 }
118
613e5116 119 public function moveIDs($fromTable, $toTable, $limit) {
4f5de903
TO
120 $sql = "
121INSERT INTO {$toTable}
122( table_name, activity_id, subject, details, contact_id, sort_name, record_type,
123 activity_type_id, case_id, client_id )
124SELECT 'Activity', ca.id, substr(ca.subject, 1, 50), substr(ca.details, 1, 250),
125 c1.id, c1.sort_name, cac.record_type_id,
126 ca.activity_type_id,
127 cca.case_id,
128 ccc.contact_id as client_id
129FROM {$fromTable} eid
130INNER JOIN civicrm_activity ca ON ca.id = eid.entity_id
131INNER JOIN civicrm_activity_contact cac ON cac.activity_id = ca.id
132INNER JOIN civicrm_contact c1 ON cac.contact_id = c1.id
133LEFT JOIN civicrm_case_activity cca ON cca.activity_id = ca.id
134LEFT JOIN civicrm_case_contact ccc ON ccc.case_id = cca.case_id
135WHERE (ca.is_deleted = 0 OR ca.is_deleted IS NULL)
136GROUP BY ca.id
7296606d 137{$this->toLimit($limit)}
4f5de903
TO
138";
139 CRM_Core_DAO::executeQuery($sql);
140 }
141
142}