Merge pull request #19232 from eileenmcnaughton/friend
[civicrm-core.git] / CRM / Contact / BAO / SearchCustom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contact_BAO_SearchCustom {
18
86538308 19 /**
c037736a 20 * Get details.
21 *
100fef9d
CW
22 * @param int $csID
23 * @param int $ssID
24 * @param int $gID
86538308
EM
25 *
26 * @return array
27 * @throws Exception
28 */
00be9182 29 public static function details($csID, $ssID = NULL, $gID = NULL) {
be2fb01f 30 $error = [NULL, NULL, NULL];
6a488035
TO
31
32 if (!$csID &&
33 !$ssID &&
34 !$gID
35 ) {
36 return $error;
37 }
38
39 $customSearchID = $csID;
be2fb01f 40 $formValues = [];
6a488035
TO
41 if ($ssID || $gID) {
42 if ($gID) {
43 $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $gID, 'saved_search_id');
44 }
45
46 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($ssID);
47 $customSearchID = CRM_Utils_Array::value('customSearchID',
48 $formValues
49 );
50 }
51
52 if (!$customSearchID) {
53 return $error;
54 }
55
56 // check that the csid exists in the db along with the right file
57 // and implements the right interface
be2fb01f 58 $customSearchClass = civicrm_api3('OptionValue', 'getvalue', [
6a59e510 59 'option_group_id' => 'custom_search',
60 'return' => 'name',
61 'value' => $customSearchID,
be2fb01f 62 ]);
6a488035
TO
63
64 $ext = CRM_Extension_System::singleton()->getMapper();
65
66 if (!$ext->isExtensionKey($customSearchClass)) {
67 $customSearchFile = str_replace('_',
353ffa53
TO
68 DIRECTORY_SEPARATOR,
69 $customSearchClass
70 ) . '.php';
6a488035
TO
71 }
72 else {
73 $customSearchFile = $ext->keyToPath($customSearchClass);
74 $customSearchClass = $ext->keyToClass($customSearchClass);
75 }
76
d3e86119 77 $error = include_once $customSearchFile;
6a488035 78 if ($error == FALSE) {
7980012b 79 throw new CRM_Core_Exception('Custom search file: ' . $customSearchFile . ' does not exist. Please verify your custom search settings in CiviCRM administrative panel.');
6a488035
TO
80 }
81
be2fb01f 82 return [$customSearchID, $customSearchClass, $formValues];
6a488035
TO
83 }
84
86538308 85 /**
100fef9d
CW
86 * @param int $csID
87 * @param int $ssID
86538308 88 *
f29d74c4 89 * @return CRM_Contact_Form_Search_Custom_Base
86538308
EM
90 * @throws Exception
91 */
00be9182 92 public static function customClass($csID, $ssID) {
6a488035
TO
93 list($customSearchID, $customSearchClass, $formValues) = self::details($csID, $ssID);
94
95 if (!$customSearchID) {
7980012b 96 throw new CRM_Core_Exception('Could not resolve custom search ID');
6a488035
TO
97 }
98
99 // instantiate the new class
481a74f4 100 $customClass = new $customSearchClass($formValues);
6a488035
TO
101
102 return $customClass;
103 }
104
86538308 105 /**
100fef9d
CW
106 * @param int $csID
107 * @param int $ssID
86538308
EM
108 *
109 * @return mixed
110 */
00be9182 111 public static function contactIDSQL($csID, $ssID) {
6a488035
TO
112 $customClass = self::customClass($csID, $ssID);
113 return $customClass->contactIDs();
114 }
115
86538308
EM
116 /**
117 * @param $args
118 *
119 * @return array
120 */
00be9182 121 public static function &buildFormValues($args) {
6a488035
TO
122 $args = trim($args);
123
124 $values = explode("\n", $args);
be2fb01f 125 $formValues = [];
6a488035
TO
126 foreach ($values as $value) {
127 list($n, $v) = CRM_Utils_System::explode('=', $value, 2);
128 if (!empty($v)) {
129 $formValues[$n] = $v;
130 }
131 }
132 return $formValues;
133 }
134
86538308 135 /**
100fef9d
CW
136 * @param int $csID
137 * @param int $ssID
86538308
EM
138 *
139 * @return array
140 */
00be9182 141 public static function fromWhereEmail($csID, $ssID) {
6a488035
TO
142 $customClass = self::customClass($csID, $ssID);
143
144 $from = $customClass->from();
145 $where = $customClass->where();
146
be2fb01f 147 return [$from, $where];
6a488035 148 }
96025800 149
6a488035 150}