76ed287cbf86ee120fb7dc4c04c67adfd82bb311
[civicrm-core.git] / CRM / Contact / BAO / SearchCustom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35 class CRM_Contact_BAO_SearchCustom {
36
37 static function details($csID, $ssID = NULL, $gID = NULL) {
38 $error = array(NULL, NULL, NULL);
39
40 if (!$csID &&
41 !$ssID &&
42 !$gID
43 ) {
44 return $error;
45 }
46
47 $customSearchID = $csID;
48 $formValues = array();
49 if ($ssID || $gID) {
50 if ($gID) {
51 $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $gID, 'saved_search_id');
52 }
53
54 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($ssID);
55 $customSearchID = CRM_Utils_Array::value('customSearchID',
56 $formValues
57 );
58 }
59
60 if (!$customSearchID) {
61 return $error;
62 }
63
64 // check that the csid exists in the db along with the right file
65 // and implements the right interface
66 $customSearchClass = CRM_Core_OptionGroup::getLabel('custom_search',
67 $customSearchID
68 );
69 if (!$customSearchClass) {
70 return $error;
71 }
72
73 $ext = CRM_Extension_System::singleton()->getMapper();
74
75 if (!$ext->isExtensionKey($customSearchClass)) {
76 $customSearchFile = str_replace('_',
77 DIRECTORY_SEPARATOR,
78 $customSearchClass
79 ) . '.php';
80 }
81 else {
82 $customSearchFile = $ext->keyToPath($customSearchClass);
83 $customSearchClass = $ext->keyToClass($customSearchClass);
84 }
85
86 $error = include_once ($customSearchFile);
87 if ($error == FALSE) {
88 CRM_Core_Error::fatal('Custom search file: ' . $customSearchFile . ' does not exist. Please verify your custom search settings in CiviCRM administrative panel.');
89 }
90
91 return array($customSearchID, $customSearchClass, $formValues);
92 }
93
94 static function customClass($csID, $ssID) {
95 list($customSearchID, $customSearchClass, $formValues) = self::details($csID, $ssID);
96
97 if (!$customSearchID) {
98 CRM_Core_Error::fatal('Could not resolve custom search ID');
99 }
100
101 // instantiate the new class
102 $customClass = new $customSearchClass( $formValues );
103
104 return $customClass;
105 }
106
107 static function contactIDSQL($csID, $ssID) {
108 $customClass = self::customClass($csID, $ssID);
109 return $customClass->contactIDs();
110 }
111
112 static function &buildFormValues($args) {
113 $args = trim($args);
114
115 $values = explode("\n", $args);
116 $formValues = array();
117 foreach ($values as $value) {
118 list($n, $v) = CRM_Utils_System::explode('=', $value, 2);
119 if (!empty($v)) {
120 $formValues[$n] = $v;
121 }
122 }
123 return $formValues;
124 }
125
126 static function fromWhereEmail($csID, $ssID) {
127 $customClass = self::customClass($csID, $ssID);
128
129 $from = $customClass->from();
130 $where = $customClass->where();
131
132
133 return array($from, $where);
134 }
135 }
136