Fix relationships permissions (CRM-17045) for 4.6 branch
[civicrm-core.git] / CRM / Contact / BAO / SearchCustom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
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 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_BAO_SearchCustom {
36
86538308 37 /**
100fef9d
CW
38 * @param int $csID
39 * @param int $ssID
40 * @param int $gID
86538308
EM
41 *
42 * @return array
43 * @throws Exception
44 */
00be9182 45 public static function details($csID, $ssID = NULL, $gID = NULL) {
6a488035
TO
46 $error = array(NULL, NULL, NULL);
47
48 if (!$csID &&
49 !$ssID &&
50 !$gID
51 ) {
52 return $error;
53 }
54
55 $customSearchID = $csID;
56 $formValues = array();
57 if ($ssID || $gID) {
58 if ($gID) {
59 $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $gID, 'saved_search_id');
60 }
61
62 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($ssID);
63 $customSearchID = CRM_Utils_Array::value('customSearchID',
64 $formValues
65 );
66 }
67
68 if (!$customSearchID) {
69 return $error;
70 }
71
72 // check that the csid exists in the db along with the right file
73 // and implements the right interface
74 $customSearchClass = CRM_Core_OptionGroup::getLabel('custom_search',
75 $customSearchID
76 );
77 if (!$customSearchClass) {
78 return $error;
79 }
80
81 $ext = CRM_Extension_System::singleton()->getMapper();
82
83 if (!$ext->isExtensionKey($customSearchClass)) {
84 $customSearchFile = str_replace('_',
353ffa53
TO
85 DIRECTORY_SEPARATOR,
86 $customSearchClass
87 ) . '.php';
6a488035
TO
88 }
89 else {
90 $customSearchFile = $ext->keyToPath($customSearchClass);
91 $customSearchClass = $ext->keyToClass($customSearchClass);
92 }
93
d3e86119 94 $error = include_once $customSearchFile;
6a488035
TO
95 if ($error == FALSE) {
96 CRM_Core_Error::fatal('Custom search file: ' . $customSearchFile . ' does not exist. Please verify your custom search settings in CiviCRM administrative panel.');
97 }
98
99 return array($customSearchID, $customSearchClass, $formValues);
100 }
101
86538308 102 /**
100fef9d
CW
103 * @param int $csID
104 * @param int $ssID
86538308
EM
105 *
106 * @return mixed
107 * @throws Exception
108 */
00be9182 109 public static function customClass($csID, $ssID) {
6a488035
TO
110 list($customSearchID, $customSearchClass, $formValues) = self::details($csID, $ssID);
111
112 if (!$customSearchID) {
113 CRM_Core_Error::fatal('Could not resolve custom search ID');
114 }
115
116 // instantiate the new class
481a74f4 117 $customClass = new $customSearchClass($formValues);
6a488035
TO
118
119 return $customClass;
120 }
121
86538308 122 /**
100fef9d
CW
123 * @param int $csID
124 * @param int $ssID
86538308
EM
125 *
126 * @return mixed
127 */
00be9182 128 public static function contactIDSQL($csID, $ssID) {
6a488035
TO
129 $customClass = self::customClass($csID, $ssID);
130 return $customClass->contactIDs();
131 }
132
86538308
EM
133 /**
134 * @param $args
135 *
136 * @return array
137 */
00be9182 138 public static function &buildFormValues($args) {
6a488035
TO
139 $args = trim($args);
140
141 $values = explode("\n", $args);
142 $formValues = array();
143 foreach ($values as $value) {
144 list($n, $v) = CRM_Utils_System::explode('=', $value, 2);
145 if (!empty($v)) {
146 $formValues[$n] = $v;
147 }
148 }
149 return $formValues;
150 }
151
86538308 152 /**
100fef9d
CW
153 * @param int $csID
154 * @param int $ssID
86538308
EM
155 *
156 * @return array
157 */
00be9182 158 public static function fromWhereEmail($csID, $ssID) {
6a488035
TO
159 $customClass = self::customClass($csID, $ssID);
160
161 $from = $customClass->from();
162 $where = $customClass->where();
163
6a488035
TO
164 return array($from, $where);
165 }
96025800 166
6a488035 167}