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