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