Merge pull request #7608 from colemanw/CRM-16353
[civicrm-core.git] / CRM / Campaign / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 = array(
42 'result',
43 'voter_id',
44 'survey_id',
45 'activity_id',
46 'surveyTitle',
47 'interviewer_id',
48 'activity_type_id',
49 );
50
51 $params = array();
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 = array(
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 = array();
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 = array(
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 = array();
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 (array(
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 = array();
175 foreach (array(
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 = array(
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 = array(
237 'sEcho' => array(
238 'name' => 'sEcho',
239 'type' => 'Integer',
240 'default' => 0,
241 ),
242 'offset' => array(
243 'name' => 'iDisplayStart',
244 'type' => 'Integer',
245 'default' => 0,
246 ),
247 'rowCount' => array(
248 'name' => 'iDisplayLength',
249 'type' => 'Integer',
250 'default' => 25,
251 ),
252 'sort' => array(
253 'name' => 'iSortCol_0',
254 'type' => 'Integer',
255 'default' => 'sort_name',
256 ),
257 'sortOrder' => array(
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 = array(
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] = array('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, array($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 = array(
404 'source_record_id',
405 'source_contact_id',
406 'target_contact_id',
407 'assignee_contact_id',
408 );
409 $activityParams = array();
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 = array();
425 $surveyParams = array('id' => $activityParams['source_record_id']);
426 CRM_Core_DAO::commonRetrieve('CRM_Campaign_DAO_Survey',
427 $surveyParams,
428 $surveyValues,
429 array('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 = array($activityParams['target_contact_id']);
455 $activities = CRM_Campaign_BAO_Survey::voterActivityDetails($activityParams['source_record_id'],
456 $voterIds,
457 $activityParams['source_contact_id'],
458 array($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(array('status' => $status));
495 }
496
497 public function allActiveCampaigns() {
498 $currentCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns();
499 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
500 $options = array(
501 array(
502 'value' => '',
503 'title' => ts('- select -'),
504 ),
505 );
506 foreach ($campaigns as $value => $title) {
507 $class = NULL;
508 if (!array_key_exists($value, $currentCampaigns)) {
509 $class = 'status-past';
510 }
511 $options[] = array(
512 'value' => $value,
513 'title' => $title,
514 'class' => $class,
515 );
516 }
517 $status = 'fail';
518 if (count($options) > 1) {
519 $status = 'success';
520 }
521
522 $results = array(
523 'status' => $status,
524 'campaigns' => $options,
525 );
526
527 CRM_Utils_JSON::output($results);
528 }
529
530 public function campaignGroups() {
531 $surveyId = CRM_Utils_Request::retrieve('survey_id', 'Positive',
532 CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'
533 );
534 $campGroups = array();
535 if ($surveyId) {
536 $campaignId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
537 if ($campaignId) {
538 $campGroups = CRM_Campaign_BAO_Campaign::getCampaignGroups($campaignId);
539 }
540 }
541
542 //CRM-7406 --If there is no campaign or no group associated with
543 //campaign of given survey, lets allow to search across all groups.
544 if (empty($campGroups)) {
545 $campGroups = CRM_Core_PseudoConstant::group();
546 }
547 $groups = array(
548 array(
549 'value' => '',
550 'title' => ts('- select -'),
551 ),
552 );
553 foreach ($campGroups as $grpId => $title) {
554 $groups[] = array(
555 'value' => $grpId,
556 'title' => $title,
557 );
558 }
559 $results = array(
560 'status' => 'success',
561 'groups' => $groups,
562 );
563
564 CRM_Utils_JSON::output($results);
565 }
566
567 /**
568 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
569 * @deprecated
570 */
571 public function campaignList() {
572 //get the search criteria params.
573 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
574 $searchParams = explode(',', $searchCriteria);
575
576 $params = $searchRows = array();
577 foreach ($searchParams as $param) {
578 if (isset($_POST[$param])) {
579 $params[$param] = $_POST[$param];
580 }
581 }
582
583 //this is sequence columns on datatable.
584 $selectorCols = array(
585 'id',
586 'name',
587 'title',
588 'description',
589 'start_date',
590 'end_date',
591 'campaign_type_id',
592 'campaign_type',
593 'status_id',
594 'status',
595 'is_active',
596 'isActive',
597 'action',
598 );
599
600 // get the data table params.
601 $dataTableParams = array(
602 'sEcho' => array(
603 'name' => 'sEcho',
604 'type' => 'Integer',
605 'default' => 0,
606 ),
607 'offset' => array(
608 'name' => 'iDisplayStart',
609 'type' => 'Integer',
610 'default' => 0,
611 ),
612 'rowCount' => array(
613 'name' => 'iDisplayLength',
614 'type' => 'Integer',
615 'default' => 25,
616 ),
617 'sort' => array(
618 'name' => 'iSortCol_0',
619 'type' => 'Integer',
620 'default' => 'start_date',
621 ),
622 'sortOrder' => array(
623 'name' => 'sSortDir_0',
624 'type' => 'String',
625 'default' => 'desc',
626 ),
627 );
628 foreach ($dataTableParams as $pName => $pValues) {
629 $$pName = $pValues['default'];
630 if (!empty($_POST[$pValues['name']])) {
631 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
632 if ($pName == 'sort') {
633 $$pName = $selectorCols[$$pName];
634 }
635 }
636 }
637 foreach (array(
638 'sort',
639 'offset',
640 'rowCount',
641 'sortOrder',
642 ) as $sortParam) {
643 $params[$sortParam] = $$sortParam;
644 }
645
646 $searchCount = CRM_Campaign_BAO_Campaign::getCampaignSummary($params, TRUE);
647 $campaigns = CRM_Campaign_Page_DashBoard::getCampaignSummary($params);
648 $iTotal = $searchCount;
649
650 if ($searchCount > 0) {
651 if ($searchCount < $offset) {
652 $offset = 0;
653 }
654 foreach ($campaigns as $campaignID => $values) {
655 foreach ($selectorCols as $col) {
656 $searchRows[$campaignID][$col] = CRM_Utils_Array::value($col, $values);
657 }
658 }
659 }
660
661 $selectorElements = $selectorCols;
662
663 $iFilteredTotal = $iTotal;
664
665 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
666 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
667 CRM_Utils_System::civiExit();
668 }
669
670 /**
671 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
672 * @deprecated
673 */
674 public function surveyList() {
675 //get the search criteria params.
676 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
677 $searchParams = explode(',', $searchCriteria);
678
679 $params = $searchRows = array();
680 foreach ($searchParams as $param) {
681 if (!empty($_POST[$param])) {
682 $params[$param] = $_POST[$param];
683 }
684 }
685
686 //this is sequence columns on datatable.
687 $selectorCols = array(
688 'id',
689 'title',
690 'campaign_id',
691 'campaign',
692 'activity_type_id',
693 'activity_type',
694 'release_frequency',
695 'default_number_of_contacts',
696 'max_number_of_contacts',
697 'is_default',
698 'is_active',
699 'isActive',
700 'result_id',
701 'action',
702 'voterLinks',
703 );
704
705 // get the data table params.
706 $dataTableParams = array(
707 'sEcho' => array(
708 'name' => 'sEcho',
709 'type' => 'Integer',
710 'default' => 0,
711 ),
712 'offset' => array(
713 'name' => 'iDisplayStart',
714 'type' => 'Integer',
715 'default' => 0,
716 ),
717 'rowCount' => array(
718 'name' => 'iDisplayLength',
719 'type' => 'Integer',
720 'default' => 25,
721 ),
722 'sort' => array(
723 'name' => 'iSortCol_0',
724 'type' => 'Integer',
725 'default' => 'created_date',
726 ),
727 'sortOrder' => array(
728 'name' => 'sSortDir_0',
729 'type' => 'String',
730 'default' => 'desc',
731 ),
732 );
733 foreach ($dataTableParams as $pName => $pValues) {
734 $$pName = $pValues['default'];
735 if (!empty($_POST[$pValues['name']])) {
736 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
737 if ($pName == 'sort') {
738 $$pName = $selectorCols[$$pName];
739 }
740 }
741 }
742 foreach (array(
743 'sort',
744 'offset',
745 'rowCount',
746 'sortOrder',
747 ) as $sortParam) {
748 $params[$sortParam] = $$sortParam;
749 }
750
751 $surveys = CRM_Campaign_Page_DashBoard::getSurveySummary($params);
752 $searchCount = CRM_Campaign_BAO_Survey::getSurveySummary($params, TRUE);
753 $iTotal = $searchCount;
754
755 if ($searchCount > 0) {
756 if ($searchCount < $offset) {
757 $offset = 0;
758 }
759 foreach ($surveys as $surveyID => $values) {
760 foreach ($selectorCols as $col) {
761 $searchRows[$surveyID][$col] = CRM_Utils_Array::value($col, $values);
762 }
763 }
764 }
765
766 $selectorElements = $selectorCols;
767
768 $iFilteredTotal = $iTotal;
769
770 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
771 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
772 CRM_Utils_System::civiExit();
773 }
774
775 /**
776 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
777 * @deprecated
778 */
779 public function petitionList() {
780 //get the search criteria params.
781 $searchCriteria = CRM_Utils_Request::retrieve('searchCriteria', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
782 $searchParams = explode(',', $searchCriteria);
783
784 $params = $searchRows = array();
785 foreach ($searchParams as $param) {
786 if (!empty($_POST[$param])) {
787 $params[$param] = $_POST[$param];
788 }
789 }
790
791 //this is sequence columns on datatable.
792 $selectorCols = array(
793 'id',
794 'title',
795 'campaign_id',
796 'campaign',
797 'activity_type_id',
798 'activity_type',
799 'is_default',
800 'is_active',
801 'isActive',
802 'action',
803 );
804
805 // get the data table params.
806 $dataTableParams = array(
807 'sEcho' => array(
808 'name' => 'sEcho',
809 'type' => 'Integer',
810 'default' => 0,
811 ),
812 'offset' => array(
813 'name' => 'iDisplayStart',
814 'type' => 'Integer',
815 'default' => 0,
816 ),
817 'rowCount' => array(
818 'name' => 'iDisplayLength',
819 'type' => 'Integer',
820 'default' => 25,
821 ),
822 'sort' => array(
823 'name' => 'iSortCol_0',
824 'type' => 'Integer',
825 'default' => 'created_date',
826 ),
827 'sortOrder' => array(
828 'name' => 'sSortDir_0',
829 'type' => 'String',
830 'default' => 'desc',
831 ),
832 );
833 foreach ($dataTableParams as $pName => $pValues) {
834 $$pName = $pValues['default'];
835 if (!empty($_POST[$pValues['name']])) {
836 $$pName = CRM_Utils_Type::escape($_POST[$pValues['name']], $pValues['type']);
837 if ($pName == 'sort') {
838 $$pName = $selectorCols[$$pName];
839 }
840 }
841 }
842 foreach (array(
843 'sort',
844 'offset',
845 'rowCount',
846 'sortOrder',
847 ) as $sortParam) {
848 $params[$sortParam] = $$sortParam;
849 }
850
851 $petitions = CRM_Campaign_Page_DashBoard::getPetitionSummary($params);
852 $searchCount = CRM_Campaign_BAO_Petition::getPetitionSummary($params, TRUE);
853 $iTotal = $searchCount;
854
855 if ($searchCount > 0) {
856 if ($searchCount < $offset) {
857 $offset = 0;
858 }
859 foreach ($petitions as $petitionID => $values) {
860 foreach ($selectorCols as $col) {
861 $searchRows[$petitionID][$col] = CRM_Utils_Array::value($col, $values);
862 }
863 }
864 }
865
866 $selectorElements = $selectorCols;
867
868 $iFilteredTotal = $iTotal;
869
870 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
871 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
872 CRM_Utils_System::civiExit();
873 }
874
875 }