Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This is base class for all ajax calls | |
38 | */ | |
39 | class CRM_Core_Page_AJAX { | |
40 | ||
41 | /** | |
42 | * function to call generic ajax forms | |
43 | * | |
44 | * @static | |
45 | * @access public | |
46 | */ | |
47 | static function run() { | |
48 | $className = CRM_Utils_Type::escape($_REQUEST['class_name'], 'String'); | |
49 | $type = ''; | |
50 | if (!empty($_REQUEST['type'])) { | |
51 | $type = CRM_Utils_Type::escape($_REQUEST['type'], 'String'); | |
52 | } | |
53 | ||
54 | if (!$className) { | |
55 | CRM_Core_Error::fatal(ts('Invalid className: %1', array(1 => $className))); | |
56 | } | |
57 | ||
58 | $fnName = NULL; | |
59 | if (isset($_REQUEST['fn_name'])) { | |
60 | $fnName = CRM_Utils_Type::escape($_REQUEST['fn_name'], 'String'); | |
61 | } | |
62 | ||
63 | if (!self::checkAuthz($type, $className, $fnName)) { | |
64 | CRM_Utils_System::civiExit(); | |
65 | } | |
66 | ||
67 | switch ($type) { | |
68 | case 'method': | |
69 | call_user_func(array($className, $fnName)); | |
70 | break; | |
71 | ||
72 | case 'page': | |
73 | case 'class': | |
74 | case '': | |
75 | // FIXME: This is done to maintain current wire protocol, but it might be | |
76 | // simpler to just require different 'types' for pages and forms | |
77 | if (preg_match('/^CRM_[a-zA-Z0-9]+_Page_Inline_/', $className)) { | |
78 | $page = new $className; | |
79 | $page->run(); | |
80 | } | |
81 | else { | |
82 | $wrapper = new CRM_Utils_Wrapper(); | |
83 | $wrapper->run($className); | |
84 | } | |
85 | break; | |
86 | default: | |
87 | CRM_Core_Error::debug_log_message('Unsupported inline request type: ' . var_export($type, TRUE)); | |
88 | } | |
89 | CRM_Utils_System::civiExit(); | |
90 | } | |
91 | ||
92 | /** | |
93 | * function to change is_quick_config priceSet to complex | |
94 | * | |
95 | * @static | |
96 | * @access public | |
97 | */ | |
98 | static function setIsQuickConfig() { | |
99 | $id = $context = NULL; | |
a7488080 | 100 | if (!empty($_REQUEST['id'])) { |
6a488035 TO |
101 | $id = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer'); |
102 | } | |
cbb7c7e0 | 103 | |
a7488080 | 104 | if (!empty($_REQUEST['context'])) { |
6a488035 TO |
105 | $context = CRM_Utils_Type::escape($_REQUEST['context'], 'String'); |
106 | } | |
cbb7c7e0 | 107 | // return false if $id is null and |
6a488035 TO |
108 | // $context is not civicrm_event or civicrm_contribution_page |
109 | if (!$id || !in_array($context, array('civicrm_event', 'civicrm_contribution_page'))) { | |
110 | return false; | |
111 | } | |
9da8dc8c | 112 | $priceSetId = CRM_Price_BAO_PriceSet::getFor($context, $id, NULL); |
6a488035 | 113 | if ($priceSetId) { |
9da8dc8c | 114 | $result = CRM_Price_BAO_PriceSet::setIsQuickConfig($priceSetId, 0); |
6a488035 TO |
115 | if ($context == 'civicrm_event') { |
116 | $sql = "UPDATE | |
117 | civicrm_price_set cps | |
118 | INNER JOIN civicrm_discount cd ON cd.price_set_id = cps.id | |
119 | SET cps.is_quick_config = 0 | |
120 | WHERE cd.entity_id = (%1) AND cd.entity_table = 'civicrm_event' "; | |
121 | $params = array(1 => array($id, 'Integer')); | |
122 | CRM_Core_DAO::executeQuery($sql, $params); | |
123 | CRM_Core_BAO_Discount::del($id, $context); | |
124 | } | |
125 | } | |
126 | if (!$result) { | |
127 | $priceSetId = null; | |
128 | } | |
129 | echo json_encode($priceSetId); | |
130 | ||
131 | CRM_Utils_System::civiExit(); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Determine whether the request is for a valid class/method name. | |
136 | * | |
137 | * @param string $type 'method'|'class'|'' | |
138 | * @param string $className 'Class_Name' | |
139 | * @param string $fnName method name | |
77b97be7 EM |
140 | * |
141 | * @return bool | |
6a488035 TO |
142 | */ |
143 | static function checkAuthz($type, $className, $fnName = null) { | |
144 | switch ($type) { | |
145 | case 'method': | |
146 | if (!preg_match('/^CRM_[a-zA-Z0-9]+_Page_AJAX$/', $className)) { | |
147 | return FALSE; | |
148 | } | |
149 | if (!preg_match('/^[a-zA-Z0-9]+$/', $fnName)) { | |
150 | return FALSE; | |
151 | } | |
152 | ||
153 | // ensure that function exists | |
154 | return method_exists($className, $fnName); | |
155 | ||
156 | case 'page': | |
157 | case 'class': | |
158 | case '': | |
159 | if (!preg_match('/^CRM_[a-zA-Z0-9]+_(Page|Form)_Inline_[a-zA-Z0-9]+$/', $className)) { | |
160 | return FALSE; | |
161 | } | |
162 | return class_exists($className); | |
163 | default: | |
164 | return FALSE; | |
165 | } | |
166 | } | |
03a7ec8f CW |
167 | |
168 | /** | |
169 | * Outputs the CiviCRM standard json-formatted page/form response | |
170 | * @param array|string $response | |
171 | */ | |
172 | static function returnJsonResponse($response) { | |
173 | // Allow lazy callers to not wrap content in an array | |
174 | if (is_string($response)) { | |
175 | $response = array('content' => $response); | |
176 | } | |
0e017a41 | 177 | // Add session variables to response |
03a7ec8f CW |
178 | $session = CRM_Core_Session::singleton(); |
179 | $response += array( | |
180 | 'status' => 'success', | |
181 | 'userContext' => htmlspecialchars_decode($session->readUserContext()), | |
0e017a41 | 182 | 'title' => CRM_Utils_System::$title, |
03a7ec8f | 183 | ); |
0e017a41 CW |
184 | // crmMessages will be automatically handled by our ajax preprocessor |
185 | // @see js/Common.js | |
03a7ec8f CW |
186 | if ($session->getStatus(FALSE)) { |
187 | $response['crmMessages'] = $session->getStatus(TRUE); | |
188 | } | |
5d76705d | 189 | $output = json_encode($response); |
03a7ec8f CW |
190 | |
191 | // CRM-11831 @see http://www.malsup.com/jquery/form/#file-upload | |
5d76705d CW |
192 | if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { |
193 | header('Content-Type: application/json'); | |
03a7ec8f | 194 | } |
5d76705d CW |
195 | else { |
196 | $output = "<textarea>$output</textarea>"; | |
03a7ec8f | 197 | } |
5d76705d | 198 | echo $output; |
03a7ec8f CW |
199 | CRM_Utils_System::civiExit(); |
200 | } | |
d6408252 CW |
201 | |
202 | /** | |
203 | * Send autocomplete results to the client. Input can be a simple or nested array. | |
204 | * @param array $results - If nested array, also provide: | |
205 | * @param string $val - array key to use as the value | |
206 | * @param string $key - array key to use as the key | |
06508628 | 207 | * @deprecated |
d6408252 CW |
208 | */ |
209 | static function autocompleteResults($results, $val='label', $key='id') { | |
210 | $output = array(); | |
211 | if (is_array($results)) { | |
212 | foreach ($results as $k => $v) { | |
213 | if (is_array($v)) { | |
214 | echo $v[$val] . '|' . $v[$key] . "\n"; | |
215 | } | |
216 | else { | |
217 | echo "$v|$k\n"; | |
218 | } | |
219 | } | |
220 | } | |
221 | CRM_Utils_System::civiExit(); | |
222 | } | |
6a488035 TO |
223 | } |
224 |