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