Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Contact / BAO / SearchCustom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
32 */
33class CRM_Contact_BAO_SearchCustom {
34
86538308 35 /**
c037736a 36 * Get details.
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) {
be2fb01f 46 $error = [NULL, NULL, NULL];
6a488035
TO
47
48 if (!$csID &&
49 !$ssID &&
50 !$gID
51 ) {
52 return $error;
53 }
54
55 $customSearchID = $csID;
be2fb01f 56 $formValues = [];
6a488035
TO
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
be2fb01f 74 $customSearchClass = civicrm_api3('OptionValue', 'getvalue', [
6a59e510 75 'option_group_id' => 'custom_search',
76 'return' => 'name',
77 'value' => $customSearchID,
be2fb01f 78 ]);
6a488035
TO
79
80 $ext = CRM_Extension_System::singleton()->getMapper();
81
82 if (!$ext->isExtensionKey($customSearchClass)) {
83 $customSearchFile = str_replace('_',
353ffa53
TO
84 DIRECTORY_SEPARATOR,
85 $customSearchClass
86 ) . '.php';
6a488035
TO
87 }
88 else {
89 $customSearchFile = $ext->keyToPath($customSearchClass);
90 $customSearchClass = $ext->keyToClass($customSearchClass);
91 }
92
d3e86119 93 $error = include_once $customSearchFile;
6a488035
TO
94 if ($error == FALSE) {
95 CRM_Core_Error::fatal('Custom search file: ' . $customSearchFile . ' does not exist. Please verify your custom search settings in CiviCRM administrative panel.');
96 }
97
be2fb01f 98 return [$customSearchID, $customSearchClass, $formValues];
6a488035
TO
99 }
100
86538308 101 /**
100fef9d
CW
102 * @param int $csID
103 * @param int $ssID
86538308
EM
104 *
105 * @return mixed
106 * @throws Exception
107 */
00be9182 108 public static function customClass($csID, $ssID) {
6a488035
TO
109 list($customSearchID, $customSearchClass, $formValues) = self::details($csID, $ssID);
110
111 if (!$customSearchID) {
112 CRM_Core_Error::fatal('Could not resolve custom search ID');
113 }
114
115 // instantiate the new class
481a74f4 116 $customClass = new $customSearchClass($formValues);
6a488035
TO
117
118 return $customClass;
119 }
120
86538308 121 /**
100fef9d
CW
122 * @param int $csID
123 * @param int $ssID
86538308
EM
124 *
125 * @return mixed
126 */
00be9182 127 public static function contactIDSQL($csID, $ssID) {
6a488035
TO
128 $customClass = self::customClass($csID, $ssID);
129 return $customClass->contactIDs();
130 }
131
86538308
EM
132 /**
133 * @param $args
134 *
135 * @return array
136 */
00be9182 137 public static function &buildFormValues($args) {
6a488035
TO
138 $args = trim($args);
139
140 $values = explode("\n", $args);
be2fb01f 141 $formValues = [];
6a488035
TO
142 foreach ($values as $value) {
143 list($n, $v) = CRM_Utils_System::explode('=', $value, 2);
144 if (!empty($v)) {
145 $formValues[$n] = $v;
146 }
147 }
148 return $formValues;
149 }
150
86538308 151 /**
100fef9d
CW
152 * @param int $csID
153 * @param int $ssID
86538308
EM
154 *
155 * @return array
156 */
00be9182 157 public static function fromWhereEmail($csID, $ssID) {
6a488035
TO
158 $customClass = self::customClass($csID, $ssID);
159
160 $from = $customClass->from();
161 $where = $customClass->where();
162
be2fb01f 163 return [$from, $where];
6a488035 164 }
96025800 165
6a488035 166}