Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-06-10-43-01
[civicrm-core.git] / CRM / Event / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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
36 /**
37 * This class contains all the function that are called using AJAX
38 */
39 class CRM_Event_Page_AJAX {
40
41 /**
42 * Function for building Event combo box
43 */
44 function event() {
45 $name = trim(CRM_Utils_Type::escape($_GET['s'], 'String'));
46 if (!$name) {
47 $name = '%';
48 }
49 $whereClause = " title LIKE '$name%' AND ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0 )";
50 $includeOld = CRM_Utils_Request::retrieve('includeOld', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, TRUE);
51 if (!$includeOld) {
52 $whereClause .= " AND ( end_date IS NULL OR end_date >= NOW() )";
53 }
54 $query = "
55 SELECT civicrm_event.title AS title,
56 civicrm_event.id AS id,
57 civicrm_address.city AS city,
58 civicrm_event.start_date
59 FROM civicrm_event
60 LEFT JOIN civicrm_loc_block ON
61 civicrm_event.loc_block_id = civicrm_loc_block.id
62 LEFT JOIN civicrm_address ON
63 civicrm_loc_block.address_id = civicrm_address.id
64 WHERE
65 {$whereClause}
66 ORDER BY
67 civicrm_event.title
68 ";
69 $dao = CRM_Core_DAO::executeQuery($query);
70 $results = array();
71 while ($dao->fetch()) {
72 $fields = array();
73 foreach (array('title', 'city') as $field) {
74 if (isset($dao->$field)) {
75 array_push($fields, $dao->$field);
76 }
77 }
78 if (isset($dao->start_date)) {
79 array_push($fields, CRM_Utils_Date::customFormat($dao->start_date));
80 }
81 $results[$dao->id] = implode(' - ', $fields);
82 }
83 CRM_Core_Page_AJAX::autocompleteResults($results);
84 }
85
86 /**
87 * Function for building Event Type combo box
88 */
89 function eventType() {
90 $name = trim(CRM_Utils_Type::escape($_GET['s'], 'String'));
91 if (!$name) {
92 $name = '%';
93 }
94 $whereClause = " v.label LIKE '$name%' ";
95
96 $query = "
97 SELECT v.label ,v.value
98 FROM civicrm_option_value v,
99 civicrm_option_group g
100 WHERE v.option_group_id = g.id
101 AND g.name = 'event_type'
102 AND v.is_active = 1
103 AND {$whereClause}
104 ORDER by v.weight";
105
106 $dao = CRM_Core_DAO::executeQuery($query);
107 $results = array();
108 while ($dao->fetch()) {
109 $results[$dao->value] = $dao->label;
110 }
111 CRM_Core_Page_AJAX::autocompleteResults($results);
112 }
113
114 /**
115 * Function for building EventFee combo box
116 */
117 function eventFee() {
118 $name = trim(CRM_Utils_Type::escape($_GET['s'], 'String'));
119
120 if (!$name) {
121 $name = '%';
122 }
123
124 $whereClause = "cv.label LIKE '$name%' ";
125
126 $query = "SELECT DISTINCT (
127 cv.label
128 ), cv.id
129 FROM civicrm_price_field_value cv
130 LEFT JOIN civicrm_price_field cf ON cv.price_field_id = cf.id
131 LEFT JOIN civicrm_price_set_entity ce ON ce.price_set_id = cf.price_set_id
132 WHERE ce.entity_table = 'civicrm_event' AND {$whereClause}
133 GROUP BY cv.label";
134 $dao = CRM_Core_DAO::executeQuery($query);
135 $results = array();
136 while ($dao->fetch()) {
137 $results[$dao->id] = $dao->label;
138 }
139 CRM_Core_Page_AJAX::autocompleteResults($results);
140 }
141
142 function eventList() {
143 $listparams = CRM_Utils_Array::value('listall', $_REQUEST, 1);
144 $events = CRM_Event_BAO_Event::getEvents($listparams);
145
146 $elements = array(array('name' => ts('- select -'),
147 'value' => '',
148 ));
149 foreach ($events as $id => $name) {
150 $elements[] = array(
151 'name' => $name,
152 'value' => $id,
153 );
154 }
155
156 echo json_encode($elements);
157 CRM_Utils_System::civiExit();
158 }
159
160 /**
161 * Function to get default participant role
162 */
163 function participantRole() {
164 $eventID = $_GET['eventId'];
165
166 $defaultRoleId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event',
167 $eventID,
168 'default_role_id',
169 'id'
170 );
171 $participantRole = array('role' => $defaultRoleId);
172 echo json_encode($participantRole);
173 CRM_Utils_System::civiExit();
174 }
175 }
176