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