(NFC) Apply upcoming civicrm/coder policies (batch 2)
[civicrm-core.git] / CRM / Campaign / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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
40 public static function registerInterview() {
41 $fields = [
42 'result',
43 'voter_id',
44 'survey_id',
45 'activity_id',
46 'surveyTitle',
47 'interviewer_id',
48 'activity_type_id',
49 ];
50
51 $params = [];
52 foreach ($fields as $fld) {
53 $params[$fld] = CRM_Utils_Array::value($fld, $_POST);
54 }
55 $params['details'] = CRM_Utils_Array::value('note', $_POST);
56 $voterId = $params['voter_id'];
57 $activityId = $params['activity_id'];
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
67 if (isset($_POST['field']) && !empty($_POST['field'][$voterId]) &&
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
85 $result = [
86 'status' => 'fail',
87 'voter_id' => $voterId,
88 'activity_id' => $params['interviewer_id'],
89 ];
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;
95 CRM_Utils_JSON::output($result);
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
104 CRM_Utils_JSON::output($result);
105 }
106
107 public static function loadOptionGroupDetails() {
108
109 $id = CRM_Utils_Request::retrieve('option_group_id', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
110 $status = 'fail';
111 $opValues = [];
112
113 if ($id) {
114 $groupParams['id'] = $id;
115 CRM_Core_OptionValue::getValues($groupParams, $opValues);
116 }
117
118 $surveyId = CRM_Utils_Request::retrieve('survey_id', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
119 if ($surveyId) {
120 $survey = new CRM_Campaign_DAO_Survey();
121 $survey->id = $surveyId;
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
139 $result = [
140 'status' => $status,
141 'result' => $opValues,
142 ];
143
144 CRM_Utils_JSON::output($result);
145 }
146
147 public function voterList() {
148 //get the search criteria params.
149 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
150 $searchParams = explode(',', $searchCriteria);
151
152 $params = $searchRows = [];
153 foreach ($searchParams as $param) {
154 if (!empty($_POST[$param])) {
155 $params[$param] = $_POST[$param];
156 }
157 }
158
159 //format multi-select group and contact types.
160 foreach ([
161 'group',
162 'contact_type',
163 ] as $param) {
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
174 $voterClauseParams = [];
175 foreach ([
176 'campaign_survey_id',
177 'survey_interviewer_id',
178 'campaign_search_voter_for',
179 ] as $fld) {
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') {
186 if (!empty($params['campaign_survey_id'])) {
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);
200 foreach ($campaignGroups as $id => $group) {
201 $params['group'][$id] = 1;
202 }
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
227 $selectorCols = [
228 'sort_name',
229 'street_address',
230 'street_name',
231 'street_number',
232 'street_unit',
233 ];
234
235 // get the data table params.
236 $dataTableParams = [
237 'sEcho' => [
238 'name' => 'sEcho',
239 'type' => 'Integer',
240 'default' => 0,
241 ],
242 'offset' => [
243 'name' => 'iDisplayStart',
244 'type' => 'Integer',
245 'default' => 0,
246 ],
247 'rowCount' => [
248 'name' => 'iDisplayLength',
249 'type' => 'Integer',
250 'default' => 25,
251 ],
252 'sort' => [
253 'name' => 'iSortCol_0',
254 'type' => 'Integer',
255 'default' => 'sort_name',
256 ],
257 'sortOrder' => [
258 'name' => 'sSortDir_0',
259 'type' => 'String',
260 'default' => 'asc',
261 ],
262 ];
263 foreach ($dataTableParams as $pName => $pValues) {
264 $$pName = $pValues['default'];
265 if (!empty($_POST[$pValues['name']])) {
266 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
267 if ($pName == 'sort') {
268 $$pName = $selectorCols[$$pName];
269 }
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
294 $selectorCols = [
295 'contact_type',
296 'sort_name',
297 'street_address',
298 'street_name',
299 'street_number',
300 'street_unit',
301 ];
302
303 $extraVoterColName = 'is_interview_conducted';
304 if ($params['campaign_search_voter_for'] == 'reserve') {
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;
326 $typeImage = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type,
327 FALSE,
328 $result->contact_id
329 );
330
331 $searchRows[$contactID] = ['id' => $contactID];
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 .= "&nbsp;<span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
343 }
344 elseif ($searchVoterFor == 'gotv') {
345 $surveyActId = $result->survey_activity_id;
346 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
347 $msg = ts('Vote Recorded.');
348 $voterExtraColHtml .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
349 }
350 else {
351 $surveyActId = $result->survey_activity_id;
352 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
353 $msg = ts('Vote Recorded.');
354 $voterExtraColHtml .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
355 }
356 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
357 }
358 }
359
360 $selectorElements = array_merge($selectorCols, [$extraVoterColName]);
361
362 $iFilteredTotal = $iTotal;
363
364 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
365 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
366 CRM_Utils_System::civiExit();
367 }
368
369 public function processVoterData() {
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;
388 if (!empty($_POST['activity_id'])) {
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) {
403 $ids = [
404 'source_record_id',
405 'source_contact_id',
406 'target_contact_id',
407 'assignee_contact_id',
408 ];
409 $activityParams = [];
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) {
420 $isReserved = CRM_Utils_String::strtoboolstr(CRM_Utils_Type::escape($_POST['isReserved'], 'String'));
421 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
422 $scheduledStatusId = array_search('Scheduled', $activityStatus);
423 if ($isReserved) {
424 $surveyValues = [];
425 $surveyParams = ['id' => $activityParams['source_record_id']];
426 CRM_Core_DAO::commonRetrieve('CRM_Campaign_DAO_Survey',
427 $surveyParams,
428 $surveyValues,
429 ['title', 'activity_type_id', 'campaign_id']
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
439 $subject = $surveytitle . ' - ' . ts('Respondent Reservation');
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;
445 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ? $surveyValues['campaign_id'] : NULL;
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.
454 $voterIds = [$activityParams['target_contact_id']];
455 $activities = CRM_Campaign_BAO_Survey::voterActivityDetails($activityParams['source_record_id'],
456 $voterIds,
457 $activityParams['source_contact_id'],
458 [$scheduledStatusId]
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
494 CRM_Utils_JSON::output(['status' => $status]);
495 }
496
497 public function campaignGroups() {
498 $surveyId = CRM_Utils_Request::retrieve('survey_id', 'Positive',
499 CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'
500 );
501 $campGroups = [];
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 }
514 $groups = [
515 [
516 'value' => '',
517 'title' => ts('- select -'),
518 ],
519 ];
520 foreach ($campGroups as $grpId => $title) {
521 $groups[] = [
522 'value' => $grpId,
523 'title' => $title,
524 ];
525 }
526 $results = [
527 'status' => 'success',
528 'groups' => $groups,
529 ];
530
531 CRM_Utils_JSON::output($results);
532 }
533
534 /**
535 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
536 * @deprecated
537 */
538 public static function campaignList() {
539 //get the search criteria params.
540 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
541 $searchParams = explode(',', $searchCriteria);
542
543 $params = $searchRows = [];
544 foreach ($searchParams as $param) {
545 if (isset($_POST[$param])) {
546 $params[$param] = $_POST[$param];
547 }
548 }
549
550 //this is sequence columns on datatable.
551 $selectorCols = [
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',
565 ];
566
567 // get the data table params.
568 $dataTableParams = [
569 'sEcho' => [
570 'name' => 'sEcho',
571 'type' => 'Integer',
572 'default' => 0,
573 ],
574 'offset' => [
575 'name' => 'iDisplayStart',
576 'type' => 'Integer',
577 'default' => 0,
578 ],
579 'rowCount' => [
580 'name' => 'iDisplayLength',
581 'type' => 'Integer',
582 'default' => 25,
583 ],
584 'sort' => [
585 'name' => 'iSortCol_0',
586 'type' => 'Integer',
587 'default' => 'start_date',
588 ],
589 'sortOrder' => [
590 'name' => 'sSortDir_0',
591 'type' => 'String',
592 'default' => 'desc',
593 ],
594 ];
595 foreach ($dataTableParams as $pName => $pValues) {
596 $$pName = $pValues['default'];
597 if (!empty($_POST[$pValues['name']])) {
598 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
599 if ($pName == 'sort') {
600 $$pName = $selectorCols[$$pName];
601 }
602 }
603 }
604 foreach ([
605 'sort',
606 'offset',
607 'rowCount',
608 'sortOrder',
609 ] as $sortParam) {
610 $params[$sortParam] = $$sortParam;
611 }
612
613 $searchCount = CRM_Campaign_BAO_Campaign::getCampaignSummary($params, TRUE);
614 $campaigns = CRM_Campaign_Page_DashBoard::getCampaignSummary($params);
615 $iTotal = $searchCount;
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
632 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
633 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
634 CRM_Utils_System::civiExit();
635 }
636
637 /**
638 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
639 * @deprecated
640 */
641 public function surveyList() {
642 //get the search criteria params.
643 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
644 $searchParams = explode(',', $searchCriteria);
645
646 $params = $searchRows = [];
647 foreach ($searchParams as $param) {
648 if (!empty($_POST[$param])) {
649 $params[$param] = $_POST[$param];
650 }
651 }
652
653 //this is sequence columns on datatable.
654 $selectorCols = [
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',
670 ];
671
672 // get the data table params.
673 $dataTableParams = [
674 'sEcho' => [
675 'name' => 'sEcho',
676 'type' => 'Integer',
677 'default' => 0,
678 ],
679 'offset' => [
680 'name' => 'iDisplayStart',
681 'type' => 'Integer',
682 'default' => 0,
683 ],
684 'rowCount' => [
685 'name' => 'iDisplayLength',
686 'type' => 'Integer',
687 'default' => 25,
688 ],
689 'sort' => [
690 'name' => 'iSortCol_0',
691 'type' => 'Integer',
692 'default' => 'created_date',
693 ],
694 'sortOrder' => [
695 'name' => 'sSortDir_0',
696 'type' => 'String',
697 'default' => 'desc',
698 ],
699 ];
700 foreach ($dataTableParams as $pName => $pValues) {
701 $$pName = $pValues['default'];
702 if (!empty($_POST[$pValues['name']])) {
703 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
704 if ($pName == 'sort') {
705 $$pName = $selectorCols[$$pName];
706 }
707 }
708 }
709 foreach ([
710 'sort',
711 'offset',
712 'rowCount',
713 'sortOrder',
714 ] as $sortParam) {
715 $params[$sortParam] = $$sortParam;
716 }
717
718 $surveys = CRM_Campaign_Page_DashBoard::getSurveySummary($params);
719 $searchCount = CRM_Campaign_BAO_Survey::getSurveySummary($params, TRUE);
720 $iTotal = $searchCount;
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
737 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
738 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
739 CRM_Utils_System::civiExit();
740 }
741
742 /**
743 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
744 * @deprecated
745 */
746 public function petitionList() {
747 //get the search criteria params.
748 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
749 $searchParams = explode(',', $searchCriteria);
750
751 $params = $searchRows = [];
752 foreach ($searchParams as $param) {
753 if (!empty($_POST[$param])) {
754 $params[$param] = $_POST[$param];
755 }
756 }
757
758 //this is sequence columns on datatable.
759 $selectorCols = [
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',
770 ];
771
772 // get the data table params.
773 $dataTableParams = [
774 'sEcho' => [
775 'name' => 'sEcho',
776 'type' => 'Integer',
777 'default' => 0,
778 ],
779 'offset' => [
780 'name' => 'iDisplayStart',
781 'type' => 'Integer',
782 'default' => 0,
783 ],
784 'rowCount' => [
785 'name' => 'iDisplayLength',
786 'type' => 'Integer',
787 'default' => 25,
788 ],
789 'sort' => [
790 'name' => 'iSortCol_0',
791 'type' => 'Integer',
792 'default' => 'created_date',
793 ],
794 'sortOrder' => [
795 'name' => 'sSortDir_0',
796 'type' => 'String',
797 'default' => 'desc',
798 ],
799 ];
800 foreach ($dataTableParams as $pName => $pValues) {
801 $$pName = $pValues['default'];
802 if (!empty($_POST[$pValues['name']])) {
803 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
804 if ($pName == 'sort') {
805 $$pName = $selectorCols[$$pName];
806 }
807 }
808 }
809 foreach ([
810 'sort',
811 'offset',
812 'rowCount',
813 'sortOrder',
814 ] as $sortParam) {
815 $params[$sortParam] = $$sortParam;
816 }
817
818 $petitions = CRM_Campaign_Page_DashBoard::getPetitionSummary($params);
819 $searchCount = CRM_Campaign_BAO_Petition::getPetitionSummary($params, TRUE);
820 $iTotal = $searchCount;
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
837 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
838 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
839 CRM_Utils_System::civiExit();
840 }
841
842 }