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