3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This is base class for all ajax calls
39 class CRM_Core_Page_AJAX
{
42 * Call generic ajax forms
46 public static function run() {
47 $className = CRM_Utils_Type
::escape($_REQUEST['class_name'], 'String');
49 if (!empty($_REQUEST['type'])) {
50 $type = CRM_Utils_Type
::escape($_REQUEST['type'], 'String');
54 CRM_Core_Error
::fatal(ts('Invalid className: %1', array(1 => $className)));
58 if (isset($_REQUEST['fn_name'])) {
59 $fnName = CRM_Utils_Type
::escape($_REQUEST['fn_name'], 'String');
62 if (!self
::checkAuthz($type, $className, $fnName)) {
63 CRM_Utils_System
::civiExit();
68 call_user_func(array($className, $fnName));
74 // FIXME: This is done to maintain current wire protocol, but it might be
75 // simpler to just require different 'types' for pages and forms
76 if (preg_match('/^CRM_[a-zA-Z0-9]+_Page_Inline_/', $className)) {
77 $page = new $className;
81 $wrapper = new CRM_Utils_Wrapper();
82 $wrapper->run($className);
87 CRM_Core_Error
::debug_log_message('Unsupported inline request type: ' . var_export($type, TRUE));
89 CRM_Utils_System
::civiExit();
93 * Change is_quick_config priceSet to complex
97 public static function setIsQuickConfig() {
98 $id = $context = NULL;
99 if (!empty($_REQUEST['id'])) {
100 $id = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
103 if (!empty($_REQUEST['context'])) {
104 $context = CRM_Utils_Type
::escape($_REQUEST['context'], 'String');
106 // return false if $id is null and
107 // $context is not civicrm_event or civicrm_contribution_page
108 if (!$id ||
!in_array($context, array('civicrm_event', 'civicrm_contribution_page'))) {
111 $priceSetId = CRM_Price_BAO_PriceSet
::getFor($context, $id, NULL);
113 $result = CRM_Price_BAO_PriceSet
::setIsQuickConfig($priceSetId, 0);
114 if ($context == 'civicrm_event') {
116 civicrm_price_set cps
117 INNER JOIN civicrm_discount cd ON cd.price_set_id = cps.id
118 SET cps.is_quick_config = 0
119 WHERE cd.entity_id = (%1) AND cd.entity_table = 'civicrm_event' ";
120 $params = array(1 => array($id, 'Integer'));
121 CRM_Core_DAO
::executeQuery($sql, $params);
122 CRM_Core_BAO_Discount
::del($id, $context);
128 CRM_Utils_JSON
::output($priceSetId);
132 * Determine whether the request is for a valid class/method name.
134 * @param string $type
135 * 'method'|'class'|''.
136 * @param string $className
138 * @param string $fnName
143 public static function checkAuthz($type, $className, $fnName = NULL) {
146 if (!preg_match('/^CRM_[a-zA-Z0-9]+_Page_AJAX$/', $className)) {
149 if (!preg_match('/^[a-zA-Z0-9]+$/', $fnName)) {
153 // ensure that function exists
154 return method_exists($className, $fnName);
159 if (!preg_match('/^CRM_[a-zA-Z0-9]+_(Page|Form)_Inline_[a-zA-Z0-9]+$/', $className)) {
162 return class_exists($className);
170 * Outputs the CiviCRM standard json-formatted page/form response
171 * @param array|string $response
173 public static function returnJsonResponse($response) {
174 // Allow lazy callers to not wrap content in an array
175 if (is_string($response)) {
176 $response = array('content' => $response);
178 // Add session variables to response
179 $session = CRM_Core_Session
::singleton();
181 'status' => 'success',
182 'userContext' => htmlspecialchars_decode($session->readUserContext()),
183 'title' => CRM_Utils_System
::$title,
185 // crmMessages will be automatically handled by our ajax preprocessor
187 if ($session->getStatus(FALSE)) {
188 $response['crmMessages'] = $session->getStatus(TRUE);
190 $output = json_encode($response);
192 // CRM-11831 @see http://www.malsup.com/jquery/form/#file-upload
193 if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
194 header('Content-Type: application/json');
197 $output = "<textarea>$output</textarea>";
200 CRM_Utils_System
::civiExit();
204 * Set headers appropriate for a js file
206 public static function setJsHeaders() {
207 // Encourage browsers to cache for a long time - 1 year
208 $year = 60 * 60 * 24 * 364;
209 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() +
$year));
210 header('Content-Type: application/javascript');
211 header("Cache-Control: max-age=$year, public");
215 * Send autocomplete results to the client. Input can be a simple or nested array.
216 * @param array $results
217 * If nested array, also provide:.
219 * Array key to use as the value.
221 * Array key to use as the key.
224 public static function autocompleteResults($results, $val = 'label', $key = 'id') {
226 if (is_array($results)) {
227 foreach ($results as $k => $v) {
229 echo $v[$val] . '|' . $v[$key] . "\n";
236 CRM_Utils_System
::civiExit();