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