Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | * |
33 | */ | |
34 | ||
35 | /** | |
36 | * This class contains all campaign related functions that are called using AJAX (jQuery) | |
37 | */ | |
38 | class CRM_Campaign_Page_AJAX { | |
39 | ||
00be9182 | 40 | public static function registerInterview() { |
be2fb01f | 41 | $fields = [ |
6a488035 TO |
42 | 'result', |
43 | 'voter_id', | |
44 | 'survey_id', | |
45 | 'activity_id', | |
46 | 'surveyTitle', | |
47 | 'interviewer_id', | |
48 | 'activity_type_id', | |
be2fb01f | 49 | ]; |
6a488035 | 50 | |
be2fb01f | 51 | $params = []; |
6a488035 TO |
52 | foreach ($fields as $fld) { |
53 | $params[$fld] = CRM_Utils_Array::value($fld, $_POST); | |
54 | } | |
55 | $params['details'] = CRM_Utils_Array::value('note', $_POST); | |
353ffa53 TO |
56 | $voterId = $params['voter_id']; |
57 | $activityId = $params['activity_id']; | |
6a488035 TO |
58 | |
59 | $customKey = "field_{$voterId}_custom"; | |
60 | foreach ($_POST as $key => $value) { | |
61 | if (strpos($key, $customKey) !== FALSE) { | |
62 | $customFieldKey = str_replace(str_replace(substr($customKey, -6), '', $customKey), '', $key); | |
63 | $params[$customFieldKey] = $value; | |
64 | } | |
65 | } | |
66 | ||
8cc574cf | 67 | if (isset($_POST['field']) && !empty($_POST['field'][$voterId]) && |
6a488035 TO |
68 | is_array($_POST['field'][$voterId]) |
69 | ) { | |
70 | foreach ($_POST['field'][$voterId] as $fieldKey => $value) { | |
71 | $params[$fieldKey] = $value; | |
72 | } | |
73 | } | |
74 | ||
75 | //lets pickup contat related fields. | |
76 | foreach ($_POST as $key => $value) { | |
77 | if (strpos($key, "field_{$voterId}_") !== FALSE && | |
78 | strpos($key, "field_{$voterId}_custom") === FALSE | |
79 | ) { | |
80 | $key = substr($key, strlen("field_{$voterId}_")); | |
81 | $params[$key] = $value; | |
82 | } | |
83 | } | |
84 | ||
be2fb01f | 85 | $result = [ |
6a488035 TO |
86 | 'status' => 'fail', |
87 | 'voter_id' => $voterId, | |
88 | 'activity_id' => $params['interviewer_id'], | |
be2fb01f | 89 | ]; |
6a488035 TO |
90 | |
91 | //time to validate custom data. | |
92 | $errors = CRM_Core_BAO_CustomField::validateCustomData($params); | |
93 | if (is_array($errors) && !empty($errors)) { | |
94 | $result['errors'] = $errors; | |
ecdef330 | 95 | CRM_Utils_JSON::output($result); |
6a488035 TO |
96 | } |
97 | ||
98 | //process the response/interview data. | |
99 | $activityId = CRM_Campaign_Form_Task_Interview::registerInterview($params); | |
100 | if ($activityId) { | |
101 | $result['status'] = 'success'; | |
102 | } | |
103 | ||
ecdef330 | 104 | CRM_Utils_JSON::output($result); |
6a488035 TO |
105 | } |
106 | ||
00be9182 | 107 | public static function loadOptionGroupDetails() { |
6a488035 | 108 | |
015bf0da | 109 | $id = CRM_Utils_Request::retrieve('option_group_id', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
353ffa53 | 110 | $status = 'fail'; |
be2fb01f | 111 | $opValues = []; |
6a488035 TO |
112 | |
113 | if ($id) { | |
114 | $groupParams['id'] = $id; | |
115 | CRM_Core_OptionValue::getValues($groupParams, $opValues); | |
116 | } | |
117 | ||
015bf0da | 118 | $surveyId = CRM_Utils_Request::retrieve('survey_id', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
6a488035 | 119 | if ($surveyId) { |
353ffa53 TO |
120 | $survey = new CRM_Campaign_DAO_Survey(); |
121 | $survey->id = $surveyId; | |
6a488035 TO |
122 | $survey->result_id = $id; |
123 | if ($survey->find(TRUE)) { | |
124 | if ($survey->recontact_interval) { | |
125 | $recontactInterval = unserialize($survey->recontact_interval); | |
126 | foreach ($opValues as $opValId => $opVal) { | |
127 | if (is_numeric($recontactInterval[$opVal['label']])) { | |
128 | $opValues[$opValId]['interval'] = $recontactInterval[$opVal['label']]; | |
129 | } | |
130 | } | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | if (!empty($opValues)) { | |
136 | $status = 'success'; | |
137 | } | |
138 | ||
be2fb01f | 139 | $result = [ |
6a488035 TO |
140 | 'status' => $status, |
141 | 'result' => $opValues, | |
be2fb01f | 142 | ]; |
6a488035 | 143 | |
ecdef330 | 144 | CRM_Utils_JSON::output($result); |
6a488035 TO |
145 | } |
146 | ||
00be9182 | 147 | public function voterList() { |
6a488035 | 148 | //get the search criteria params. |
015bf0da | 149 | $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
7fa9167d | 150 | $searchParams = explode(',', $searchCriteria); |
6a488035 | 151 | |
be2fb01f | 152 | $params = $searchRows = []; |
6a488035 | 153 | foreach ($searchParams as $param) { |
a7488080 | 154 | if (!empty($_POST[$param])) { |
6a488035 TO |
155 | $params[$param] = $_POST[$param]; |
156 | } | |
157 | } | |
158 | ||
159 | //format multi-select group and contact types. | |
be2fb01f | 160 | foreach ([ |
353ffa53 | 161 | 'group', |
608e6658 | 162 | 'contact_type', |
be2fb01f | 163 | ] as $param) { |
6a488035 TO |
164 | $paramValue = CRM_Utils_Array::value($param, $params); |
165 | if ($paramValue) { | |
166 | unset($params[$param]); | |
167 | $paramValue = explode(',', $paramValue); | |
168 | foreach ($paramValue as $key => $value) { | |
169 | $params[$param][$value] = 1; | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
be2fb01f CW |
174 | $voterClauseParams = []; |
175 | foreach ([ | |
353ffa53 TO |
176 | 'campaign_survey_id', |
177 | 'survey_interviewer_id', | |
608e6658 | 178 | 'campaign_search_voter_for', |
be2fb01f | 179 | ] as $fld) { |
6a488035 TO |
180 | $voterClauseParams[$fld] = CRM_Utils_Array::value($fld, $params); |
181 | } | |
182 | ||
183 | $interviewerId = $surveyTypeId = $surveyId = NULL; | |
184 | $searchVoterFor = $params['campaign_search_voter_for']; | |
185 | if ($searchVoterFor == 'reserve') { | |
a7488080 | 186 | if (!empty($params['campaign_survey_id'])) { |
6a488035 TO |
187 | $survey = new CRM_Campaign_DAO_Survey(); |
188 | $survey->id = $surveyId = $params['campaign_survey_id']; | |
189 | $survey->selectAdd('campaign_id, activity_type_id'); | |
190 | $survey->find(TRUE); | |
191 | $campaignId = $survey->campaign_id; | |
192 | $surveyTypeId = $survey->activity_type_id; | |
193 | ||
194 | //allow voter search in sub-part of given constituents, | |
195 | //but make sure in case user does not select any group. | |
196 | //get all associated campaign groups in where filter, CRM-7406 | |
197 | $groups = CRM_Utils_Array::value('group', $params); | |
198 | if ($campaignId && CRM_Utils_System::isNull($groups)) { | |
199 | $campaignGroups = CRM_Campaign_BAO_Campaign::getCampaignGroups($campaignId); | |
5c2ea586 TO |
200 | foreach ($campaignGroups as $id => $group) { |
201 | $params['group'][$id] = 1; | |
94880218 | 202 | } |
6a488035 TO |
203 | } |
204 | ||
205 | //apply filter of survey contact type for search. | |
206 | $contactType = CRM_Campaign_BAO_Survey::getSurveyContactType($surveyId); | |
207 | if ($contactType) { | |
208 | $params['contact_type'][$contactType] = 1; | |
209 | } | |
210 | ||
211 | unset($params['campaign_survey_id']); | |
212 | } | |
213 | unset($params['survey_interviewer_id']); | |
214 | } | |
215 | else { | |
216 | //get the survey status in where clause. | |
217 | $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant::activityStatus('name')); | |
218 | if ($scheduledStatusId) { | |
219 | $params['survey_status_id'] = $scheduledStatusId; | |
220 | } | |
221 | //BAO/Query knows reserve/release/interview processes. | |
222 | if ($params['campaign_search_voter_for'] == 'gotv') { | |
223 | $params['campaign_search_voter_for'] = 'release'; | |
224 | } | |
225 | } | |
226 | ||
be2fb01f | 227 | $selectorCols = [ |
6a488035 TO |
228 | 'sort_name', |
229 | 'street_address', | |
230 | 'street_name', | |
231 | 'street_number', | |
232 | 'street_unit', | |
be2fb01f | 233 | ]; |
6a488035 TO |
234 | |
235 | // get the data table params. | |
be2fb01f CW |
236 | $dataTableParams = [ |
237 | 'sEcho' => [ | |
353ffa53 | 238 | 'name' => 'sEcho', |
6a488035 TO |
239 | 'type' => 'Integer', |
240 | 'default' => 0, | |
be2fb01f CW |
241 | ], |
242 | 'offset' => [ | |
6a488035 TO |
243 | 'name' => 'iDisplayStart', |
244 | 'type' => 'Integer', | |
245 | 'default' => 0, | |
be2fb01f CW |
246 | ], |
247 | 'rowCount' => [ | |
6a488035 TO |
248 | 'name' => 'iDisplayLength', |
249 | 'type' => 'Integer', | |
250 | 'default' => 25, | |
be2fb01f CW |
251 | ], |
252 | 'sort' => [ | |
6a488035 TO |
253 | 'name' => 'iSortCol_0', |
254 | 'type' => 'Integer', | |
255 | 'default' => 'sort_name', | |
be2fb01f CW |
256 | ], |
257 | 'sortOrder' => [ | |
6a488035 TO |
258 | 'name' => 'sSortDir_0', |
259 | 'type' => 'String', | |
260 | 'default' => 'asc', | |
be2fb01f CW |
261 | ], |
262 | ]; | |
6a488035 TO |
263 | foreach ($dataTableParams as $pName => $pValues) { |
264 | $$pName = $pValues['default']; | |
a7488080 | 265 | if (!empty($_POST[$pValues['name']])) { |
6a488035 | 266 | $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']); |
5c2ea586 TO |
267 | if ($pName == 'sort') { |
268 | $$pName = $selectorCols[$$pName]; | |
94880218 | 269 | } |
6a488035 TO |
270 | } |
271 | } | |
272 | ||
273 | $queryParams = CRM_Contact_BAO_Query::convertFormValues($params); | |
274 | $query = new CRM_Contact_BAO_Query($queryParams, | |
275 | NULL, NULL, FALSE, FALSE, | |
276 | CRM_Contact_BAO_Query::MODE_CAMPAIGN, | |
277 | TRUE | |
278 | ); | |
279 | ||
280 | //get the voter clause to restrict and validate search. | |
281 | $voterClause = CRM_Campaign_BAO_Query::voterClause($voterClauseParams); | |
282 | ||
283 | $searchCount = $query->searchQuery(0, 0, NULL, | |
284 | TRUE, FALSE, | |
285 | FALSE, FALSE, | |
286 | FALSE, | |
287 | CRM_Utils_Array::value('whereClause', $voterClause), | |
288 | NULL, | |
289 | CRM_Utils_Array::value('fromClause', $voterClause) | |
290 | ); | |
291 | ||
292 | $iTotal = $searchCount; | |
293 | ||
be2fb01f | 294 | $selectorCols = [ |
353ffa53 TO |
295 | 'contact_type', |
296 | 'sort_name', | |
297 | 'street_address', | |
298 | 'street_name', | |
299 | 'street_number', | |
300 | 'street_unit', | |
be2fb01f | 301 | ]; |
6a488035 TO |
302 | |
303 | $extraVoterColName = 'is_interview_conducted'; | |
0aec9935 | 304 | if ($params['campaign_search_voter_for'] == 'reserve') { |
6a488035 TO |
305 | $extraVoterColName = 'reserve_voter'; |
306 | } | |
307 | ||
308 | if ($searchCount > 0) { | |
309 | if ($searchCount < $offset) { | |
310 | $offset = 0; | |
311 | } | |
312 | ||
313 | $config = CRM_Core_Config::singleton(); | |
314 | ||
315 | // get the result of the search | |
316 | $result = $query->searchQuery($offset, $rowCount, $sort, | |
317 | FALSE, FALSE, | |
318 | FALSE, FALSE, | |
319 | FALSE, | |
320 | CRM_Utils_Array::value('whereClause', $voterClause), | |
321 | $sortOrder, | |
322 | CRM_Utils_Array::value('fromClause', $voterClause) | |
323 | ); | |
324 | while ($result->fetch()) { | |
325 | $contactID = $result->contact_id; | |
94880218 | 326 | $typeImage = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, |
6a488035 TO |
327 | FALSE, |
328 | $result->contact_id | |
329 | ); | |
330 | ||
be2fb01f | 331 | $searchRows[$contactID] = ['id' => $contactID]; |
6a488035 TO |
332 | foreach ($selectorCols as $col) { |
333 | $val = $result->$col; | |
334 | if ($col == 'contact_type') { | |
335 | $val = $typeImage; | |
336 | } | |
337 | $searchRows[$contactID][$col] = $val; | |
338 | } | |
339 | if ($searchVoterFor == 'reserve') { | |
340 | $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />'; | |
341 | $msg = ts('Respondent Reserved.'); | |
342 | $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>"; | |
343 | } | |
344 | elseif ($searchVoterFor == 'gotv') { | |
353ffa53 | 345 | $surveyActId = $result->survey_activity_id; |
6a488035 | 346 | $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />'; |
353ffa53 | 347 | $msg = ts('Vote Recorded.'); |
6a488035 TO |
348 | $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>"; |
349 | } | |
350 | else { | |
353ffa53 | 351 | $surveyActId = $result->survey_activity_id; |
6a488035 | 352 | $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />'; |
353ffa53 | 353 | $msg = ts('Vote Recorded.'); |
6a488035 TO |
354 | $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>"; |
355 | } | |
356 | $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml; | |
357 | } | |
358 | } | |
359 | ||
be2fb01f | 360 | $selectorElements = array_merge($selectorCols, [$extraVoterColName]); |
6a488035 TO |
361 | |
362 | $iFilteredTotal = $iTotal; | |
363 | ||
d42a224c | 364 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
6a488035 TO |
365 | echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
366 | CRM_Utils_System::civiExit(); | |
367 | } | |
368 | ||
00be9182 | 369 | public function processVoterData() { |
6a488035 TO |
370 | $status = NULL; |
371 | $operation = CRM_Utils_Type::escape($_POST['operation'], 'String'); | |
372 | if ($operation == 'release') { | |
373 | $activityId = CRM_Utils_Type::escape($_POST['activity_id'], 'Integer'); | |
374 | $isDelete = CRM_Utils_String::strtoboolstr(CRM_Utils_Type::escape($_POST['isDelete'], 'String')); | |
375 | if ($activityId && | |
376 | CRM_Core_DAO::setFieldValue('CRM_Activity_DAO_Activity', | |
377 | $activityId, | |
378 | 'is_deleted', | |
379 | $isDelete | |
380 | ) | |
381 | ) { | |
382 | $status = 'success'; | |
383 | } | |
384 | } | |
385 | elseif ($operation == 'reserve') { | |
386 | $activityId = NULL; | |
387 | $createActivity = TRUE; | |
a7488080 | 388 | if (!empty($_POST['activity_id'])) { |
6a488035 TO |
389 | $activityId = CRM_Utils_Type::escape($_POST['activity_id'], 'Integer'); |
390 | if ($activityId) { | |
391 | $createActivity = FALSE; | |
392 | $activityUpdated = CRM_Core_DAO::setFieldValue('CRM_Activity_DAO_Activity', | |
393 | $activityId, | |
394 | 'is_deleted', | |
395 | 0 | |
396 | ); | |
397 | if ($activityUpdated) { | |
398 | $status = 'success'; | |
399 | } | |
400 | } | |
401 | } | |
402 | if ($createActivity) { | |
be2fb01f | 403 | $ids = [ |
6a488035 TO |
404 | 'source_record_id', |
405 | 'source_contact_id', | |
406 | 'target_contact_id', | |
407 | 'assignee_contact_id', | |
be2fb01f CW |
408 | ]; |
409 | $activityParams = []; | |
6a488035 TO |
410 | foreach ($ids as $id) { |
411 | $val = CRM_Utils_Array::value($id, $_POST); | |
412 | if (!$val) { | |
413 | $createActivity = FALSE; | |
414 | break; | |
415 | } | |
416 | $activityParams[$id] = CRM_Utils_Type::escape($val, 'Integer'); | |
417 | } | |
418 | } | |
419 | if ($createActivity) { | |
353ffa53 TO |
420 | $isReserved = CRM_Utils_String::strtoboolstr(CRM_Utils_Type::escape($_POST['isReserved'], 'String')); |
421 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); | |
6a488035 TO |
422 | $scheduledStatusId = array_search('Scheduled', $activityStatus); |
423 | if ($isReserved) { | |
be2fb01f CW |
424 | $surveyValues = []; |
425 | $surveyParams = ['id' => $activityParams['source_record_id']]; | |
6a488035 TO |
426 | CRM_Core_DAO::commonRetrieve('CRM_Campaign_DAO_Survey', |
427 | $surveyParams, | |
428 | $surveyValues, | |
be2fb01f | 429 | ['title', 'activity_type_id', 'campaign_id'] |
6a488035 TO |
430 | ); |
431 | ||
432 | $activityTypeId = $surveyValues['activity_type_id']; | |
433 | ||
434 | $surveytitle = CRM_Utils_Array::value('surveyTitle', $_POST); | |
435 | if (!$surveytitle) { | |
436 | $surveytitle = $surveyValues['title']; | |
437 | } | |
438 | ||
58438e5b | 439 | $subject = $surveytitle . ' - ' . ts('Respondent Reservation'); |
6a488035 TO |
440 | $activityParams['subject'] = $subject; |
441 | $activityParams['status_id'] = $scheduledStatusId; | |
442 | $activityParams['skipRecentView'] = 1; | |
443 | $activityParams['activity_date_time'] = date('YmdHis'); | |
444 | $activityParams['activity_type_id'] = $activityTypeId; | |
bef6ccc5 | 445 | $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ? $surveyValues['campaign_id'] : NULL; |
6a488035 TO |
446 | |
447 | $activity = CRM_Activity_BAO_Activity::create($activityParams); | |
448 | if ($activity->id) { | |
449 | $status = 'success'; | |
450 | } | |
451 | } | |
452 | else { | |
453 | //delete reserved activity for given voter. | |
be2fb01f | 454 | $voterIds = [$activityParams['target_contact_id']]; |
6a488035 TO |
455 | $activities = CRM_Campaign_BAO_Survey::voterActivityDetails($activityParams['source_record_id'], |
456 | $voterIds, | |
457 | $activityParams['source_contact_id'], | |
be2fb01f | 458 | [$scheduledStatusId] |
6a488035 TO |
459 | ); |
460 | foreach ($activities as $voterId => $values) { | |
461 | $activityId = CRM_Utils_Array::value('activity_id', $values); | |
462 | if ($activityId && ($values['status_id'] == $scheduledStatusId)) { | |
463 | CRM_Core_DAO::setFieldValue('CRM_Activity_DAO_Activity', | |
464 | $activityId, | |
465 | 'is_deleted', | |
466 | TRUE | |
467 | ); | |
468 | $status = 'success'; | |
469 | break; | |
470 | } | |
471 | } | |
472 | } | |
473 | } | |
474 | } | |
475 | elseif ($operation == 'gotv') { | |
476 | $activityId = CRM_Utils_Type::escape($_POST['activity_id'], 'Integer'); | |
477 | $hasVoted = CRM_Utils_String::strtoboolstr(CRM_Utils_Type::escape($_POST['hasVoted'], 'String')); | |
478 | if ($activityId) { | |
479 | if ($hasVoted) { | |
480 | $statusValue = 2; | |
481 | } | |
482 | else { | |
483 | $statusValue = 1; | |
484 | } | |
485 | CRM_Core_DAO::setFieldValue('CRM_Activity_DAO_Activity', | |
486 | $activityId, | |
487 | 'status_id', | |
488 | $statusValue | |
489 | ); | |
490 | $status = 'success'; | |
491 | } | |
492 | } | |
493 | ||
be2fb01f | 494 | CRM_Utils_JSON::output(['status' => $status]); |
6a488035 TO |
495 | } |
496 | ||
00be9182 | 497 | public function campaignGroups() { |
6a488035 TO |
498 | $surveyId = CRM_Utils_Request::retrieve('survey_id', 'Positive', |
499 | CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST' | |
500 | ); | |
be2fb01f | 501 | $campGroups = []; |
6a488035 TO |
502 | if ($surveyId) { |
503 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id'); | |
504 | if ($campaignId) { | |
505 | $campGroups = CRM_Campaign_BAO_Campaign::getCampaignGroups($campaignId); | |
506 | } | |
507 | } | |
508 | ||
509 | //CRM-7406 --If there is no campaign or no group associated with | |
510 | //campaign of given survey, lets allow to search across all groups. | |
511 | if (empty($campGroups)) { | |
512 | $campGroups = CRM_Core_PseudoConstant::group(); | |
513 | } | |
be2fb01f CW |
514 | $groups = [ |
515 | [ | |
353ffa53 | 516 | 'value' => '', |
6a488035 | 517 | 'title' => ts('- select -'), |
be2fb01f CW |
518 | ], |
519 | ]; | |
6a488035 | 520 | foreach ($campGroups as $grpId => $title) { |
be2fb01f | 521 | $groups[] = [ |
6a488035 TO |
522 | 'value' => $grpId, |
523 | 'title' => $title, | |
be2fb01f | 524 | ]; |
6a488035 | 525 | } |
be2fb01f | 526 | $results = [ |
6a488035 TO |
527 | 'status' => 'success', |
528 | 'groups' => $groups, | |
be2fb01f | 529 | ]; |
6a488035 | 530 | |
ecdef330 | 531 | CRM_Utils_JSON::output($results); |
6a488035 TO |
532 | } |
533 | ||
534 | /** | |
9d311c8f CW |
535 | * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353. |
536 | * @deprecated | |
608e6658 | 537 | */ |
ace6e8ec | 538 | public static function campaignList() { |
6a488035 | 539 | //get the search criteria params. |
015bf0da | 540 | $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
7fa9167d | 541 | $searchParams = explode(',', $searchCriteria); |
6a488035 | 542 | |
be2fb01f | 543 | $params = $searchRows = []; |
6a488035 | 544 | foreach ($searchParams as $param) { |
da6cc247 | 545 | if (isset($_POST[$param])) { |
6a488035 TO |
546 | $params[$param] = $_POST[$param]; |
547 | } | |
548 | } | |
549 | ||
550 | //this is sequence columns on datatable. | |
be2fb01f | 551 | $selectorCols = [ |
6a488035 TO |
552 | 'id', |
553 | 'name', | |
554 | 'title', | |
555 | 'description', | |
556 | 'start_date', | |
557 | 'end_date', | |
558 | 'campaign_type_id', | |
559 | 'campaign_type', | |
560 | 'status_id', | |
561 | 'status', | |
562 | 'is_active', | |
563 | 'isActive', | |
564 | 'action', | |
be2fb01f | 565 | ]; |
6a488035 TO |
566 | |
567 | // get the data table params. | |
be2fb01f CW |
568 | $dataTableParams = [ |
569 | 'sEcho' => [ | |
353ffa53 | 570 | 'name' => 'sEcho', |
6a488035 TO |
571 | 'type' => 'Integer', |
572 | 'default' => 0, | |
be2fb01f CW |
573 | ], |
574 | 'offset' => [ | |
6a488035 TO |
575 | 'name' => 'iDisplayStart', |
576 | 'type' => 'Integer', | |
577 | 'default' => 0, | |
be2fb01f CW |
578 | ], |
579 | 'rowCount' => [ | |
6a488035 TO |
580 | 'name' => 'iDisplayLength', |
581 | 'type' => 'Integer', | |
582 | 'default' => 25, | |
be2fb01f CW |
583 | ], |
584 | 'sort' => [ | |
6a488035 TO |
585 | 'name' => 'iSortCol_0', |
586 | 'type' => 'Integer', | |
587 | 'default' => 'start_date', | |
be2fb01f CW |
588 | ], |
589 | 'sortOrder' => [ | |
6a488035 TO |
590 | 'name' => 'sSortDir_0', |
591 | 'type' => 'String', | |
592 | 'default' => 'desc', | |
be2fb01f CW |
593 | ], |
594 | ]; | |
6a488035 TO |
595 | foreach ($dataTableParams as $pName => $pValues) { |
596 | $$pName = $pValues['default']; | |
a7488080 | 597 | if (!empty($_POST[$pValues['name']])) { |
6a488035 TO |
598 | $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']); |
599 | if ($pName == 'sort') { | |
600 | $$pName = $selectorCols[$$pName]; | |
601 | } | |
602 | } | |
603 | } | |
be2fb01f | 604 | foreach ([ |
353ffa53 TO |
605 | 'sort', |
606 | 'offset', | |
607 | 'rowCount', | |
608e6658 | 608 | 'sortOrder', |
be2fb01f | 609 | ] as $sortParam) { |
6a488035 TO |
610 | $params[$sortParam] = $$sortParam; |
611 | } | |
612 | ||
613 | $searchCount = CRM_Campaign_BAO_Campaign::getCampaignSummary($params, TRUE); | |
353ffa53 TO |
614 | $campaigns = CRM_Campaign_Page_DashBoard::getCampaignSummary($params); |
615 | $iTotal = $searchCount; | |
6a488035 TO |
616 | |
617 | if ($searchCount > 0) { | |
618 | if ($searchCount < $offset) { | |
619 | $offset = 0; | |
620 | } | |
621 | foreach ($campaigns as $campaignID => $values) { | |
622 | foreach ($selectorCols as $col) { | |
623 | $searchRows[$campaignID][$col] = CRM_Utils_Array::value($col, $values); | |
624 | } | |
625 | } | |
626 | } | |
627 | ||
628 | $selectorElements = $selectorCols; | |
629 | ||
630 | $iFilteredTotal = $iTotal; | |
631 | ||
d42a224c | 632 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
6a488035 TO |
633 | echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
634 | CRM_Utils_System::civiExit(); | |
635 | } | |
636 | ||
637 | /** | |
9d311c8f CW |
638 | * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353. |
639 | * @deprecated | |
608e6658 | 640 | */ |
00be9182 | 641 | public function surveyList() { |
6a488035 | 642 | //get the search criteria params. |
015bf0da | 643 | $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
7fa9167d | 644 | $searchParams = explode(',', $searchCriteria); |
6a488035 | 645 | |
be2fb01f | 646 | $params = $searchRows = []; |
6a488035 | 647 | foreach ($searchParams as $param) { |
a7488080 | 648 | if (!empty($_POST[$param])) { |
6a488035 TO |
649 | $params[$param] = $_POST[$param]; |
650 | } | |
651 | } | |
652 | ||
653 | //this is sequence columns on datatable. | |
be2fb01f | 654 | $selectorCols = [ |
6a488035 TO |
655 | 'id', |
656 | 'title', | |
657 | 'campaign_id', | |
658 | 'campaign', | |
659 | 'activity_type_id', | |
660 | 'activity_type', | |
661 | 'release_frequency', | |
662 | 'default_number_of_contacts', | |
663 | 'max_number_of_contacts', | |
664 | 'is_default', | |
665 | 'is_active', | |
666 | 'isActive', | |
667 | 'result_id', | |
668 | 'action', | |
669 | 'voterLinks', | |
be2fb01f | 670 | ]; |
6a488035 TO |
671 | |
672 | // get the data table params. | |
be2fb01f CW |
673 | $dataTableParams = [ |
674 | 'sEcho' => [ | |
353ffa53 | 675 | 'name' => 'sEcho', |
6a488035 TO |
676 | 'type' => 'Integer', |
677 | 'default' => 0, | |
be2fb01f CW |
678 | ], |
679 | 'offset' => [ | |
6a488035 TO |
680 | 'name' => 'iDisplayStart', |
681 | 'type' => 'Integer', | |
682 | 'default' => 0, | |
be2fb01f CW |
683 | ], |
684 | 'rowCount' => [ | |
6a488035 TO |
685 | 'name' => 'iDisplayLength', |
686 | 'type' => 'Integer', | |
687 | 'default' => 25, | |
be2fb01f CW |
688 | ], |
689 | 'sort' => [ | |
6a488035 TO |
690 | 'name' => 'iSortCol_0', |
691 | 'type' => 'Integer', | |
692 | 'default' => 'created_date', | |
be2fb01f CW |
693 | ], |
694 | 'sortOrder' => [ | |
6a488035 TO |
695 | 'name' => 'sSortDir_0', |
696 | 'type' => 'String', | |
697 | 'default' => 'desc', | |
be2fb01f CW |
698 | ], |
699 | ]; | |
6a488035 TO |
700 | foreach ($dataTableParams as $pName => $pValues) { |
701 | $$pName = $pValues['default']; | |
a7488080 | 702 | if (!empty($_POST[$pValues['name']])) { |
6a488035 TO |
703 | $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']); |
704 | if ($pName == 'sort') { | |
705 | $$pName = $selectorCols[$$pName]; | |
706 | } | |
707 | } | |
708 | } | |
be2fb01f | 709 | foreach ([ |
353ffa53 TO |
710 | 'sort', |
711 | 'offset', | |
712 | 'rowCount', | |
608e6658 | 713 | 'sortOrder', |
be2fb01f | 714 | ] as $sortParam) { |
6a488035 TO |
715 | $params[$sortParam] = $$sortParam; |
716 | } | |
717 | ||
353ffa53 | 718 | $surveys = CRM_Campaign_Page_DashBoard::getSurveySummary($params); |
6a488035 | 719 | $searchCount = CRM_Campaign_BAO_Survey::getSurveySummary($params, TRUE); |
353ffa53 | 720 | $iTotal = $searchCount; |
6a488035 TO |
721 | |
722 | if ($searchCount > 0) { | |
723 | if ($searchCount < $offset) { | |
724 | $offset = 0; | |
725 | } | |
726 | foreach ($surveys as $surveyID => $values) { | |
727 | foreach ($selectorCols as $col) { | |
728 | $searchRows[$surveyID][$col] = CRM_Utils_Array::value($col, $values); | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
733 | $selectorElements = $selectorCols; | |
734 | ||
735 | $iFilteredTotal = $iTotal; | |
736 | ||
d42a224c | 737 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
6a488035 TO |
738 | echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
739 | CRM_Utils_System::civiExit(); | |
740 | } | |
741 | ||
742 | /** | |
9d311c8f CW |
743 | * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353. |
744 | * @deprecated | |
608e6658 | 745 | */ |
00be9182 | 746 | public function petitionList() { |
6a488035 | 747 | //get the search criteria params. |
015bf0da | 748 | $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'); |
7fa9167d | 749 | $searchParams = explode(',', $searchCriteria); |
6a488035 | 750 | |
be2fb01f | 751 | $params = $searchRows = []; |
6a488035 | 752 | foreach ($searchParams as $param) { |
a7488080 | 753 | if (!empty($_POST[$param])) { |
6a488035 TO |
754 | $params[$param] = $_POST[$param]; |
755 | } | |
756 | } | |
757 | ||
758 | //this is sequence columns on datatable. | |
be2fb01f | 759 | $selectorCols = [ |
6a488035 TO |
760 | 'id', |
761 | 'title', | |
762 | 'campaign_id', | |
763 | 'campaign', | |
764 | 'activity_type_id', | |
765 | 'activity_type', | |
766 | 'is_default', | |
767 | 'is_active', | |
768 | 'isActive', | |
769 | 'action', | |
be2fb01f | 770 | ]; |
6a488035 TO |
771 | |
772 | // get the data table params. | |
be2fb01f CW |
773 | $dataTableParams = [ |
774 | 'sEcho' => [ | |
353ffa53 | 775 | 'name' => 'sEcho', |
6a488035 TO |
776 | 'type' => 'Integer', |
777 | 'default' => 0, | |
be2fb01f CW |
778 | ], |
779 | 'offset' => [ | |
6a488035 TO |
780 | 'name' => 'iDisplayStart', |
781 | 'type' => 'Integer', | |
782 | 'default' => 0, | |
be2fb01f CW |
783 | ], |
784 | 'rowCount' => [ | |
6a488035 TO |
785 | 'name' => 'iDisplayLength', |
786 | 'type' => 'Integer', | |
787 | 'default' => 25, | |
be2fb01f CW |
788 | ], |
789 | 'sort' => [ | |
6a488035 TO |
790 | 'name' => 'iSortCol_0', |
791 | 'type' => 'Integer', | |
792 | 'default' => 'created_date', | |
be2fb01f CW |
793 | ], |
794 | 'sortOrder' => [ | |
6a488035 TO |
795 | 'name' => 'sSortDir_0', |
796 | 'type' => 'String', | |
797 | 'default' => 'desc', | |
be2fb01f CW |
798 | ], |
799 | ]; | |
6a488035 TO |
800 | foreach ($dataTableParams as $pName => $pValues) { |
801 | $$pName = $pValues['default']; | |
a7488080 | 802 | if (!empty($_POST[$pValues['name']])) { |
6a488035 TO |
803 | $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']); |
804 | if ($pName == 'sort') { | |
805 | $$pName = $selectorCols[$$pName]; | |
806 | } | |
807 | } | |
808 | } | |
be2fb01f | 809 | foreach ([ |
353ffa53 TO |
810 | 'sort', |
811 | 'offset', | |
812 | 'rowCount', | |
608e6658 | 813 | 'sortOrder', |
be2fb01f | 814 | ] as $sortParam) { |
6a488035 TO |
815 | $params[$sortParam] = $$sortParam; |
816 | } | |
817 | ||
353ffa53 | 818 | $petitions = CRM_Campaign_Page_DashBoard::getPetitionSummary($params); |
6a488035 | 819 | $searchCount = CRM_Campaign_BAO_Petition::getPetitionSummary($params, TRUE); |
353ffa53 | 820 | $iTotal = $searchCount; |
6a488035 TO |
821 | |
822 | if ($searchCount > 0) { | |
823 | if ($searchCount < $offset) { | |
824 | $offset = 0; | |
825 | } | |
826 | foreach ($petitions as $petitionID => $values) { | |
827 | foreach ($selectorCols as $col) { | |
828 | $searchRows[$petitionID][$col] = CRM_Utils_Array::value($col, $values); | |
829 | } | |
830 | } | |
831 | } | |
832 | ||
833 | $selectorElements = $selectorCols; | |
834 | ||
835 | $iFilteredTotal = $iTotal; | |
836 | ||
d42a224c | 837 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
6a488035 TO |
838 | echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); |
839 | CRM_Utils_System::civiExit(); | |
840 | } | |
96025800 | 841 | |
6a488035 | 842 | } |