3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 * This class contains all campaign related functions that are called using AJAX (jQuery)
22 class CRM_Campaign_Page_AJAX
{
24 public static function registerInterview() {
36 foreach ($fields as $fld) {
37 $params[$fld] = CRM_Utils_Array
::value($fld, $_POST);
39 $params['details'] = CRM_Utils_Array
::value('note', $_POST);
40 $voterId = $params['voter_id'];
41 $activityId = $params['activity_id'];
43 $customKey = "field_{$voterId}_custom";
44 foreach ($_POST as $key => $value) {
45 if (strpos($key, $customKey) !== FALSE) {
46 $customFieldKey = str_replace(str_replace(substr($customKey, -6), '', $customKey), '', $key);
47 $params[$customFieldKey] = $value;
51 if (isset($_POST['field']) && !empty($_POST['field'][$voterId]) &&
52 is_array($_POST['field'][$voterId])
54 foreach ($_POST['field'][$voterId] as $fieldKey => $value) {
55 $params[$fieldKey] = $value;
59 //lets pickup contat related fields.
60 foreach ($_POST as $key => $value) {
61 if (strpos($key, "field_{$voterId}_") !== FALSE &&
62 strpos($key, "field_{$voterId}_custom") === FALSE
64 $key = substr($key, strlen("field_{$voterId}_"));
65 $params[$key] = $value;
71 'voter_id' => $voterId,
72 'activity_id' => $params['interviewer_id'],
75 //time to validate custom data.
76 $errors = CRM_Core_BAO_CustomField
::validateCustomData($params);
77 if (is_array($errors) && !empty($errors)) {
78 $result['errors'] = $errors;
79 CRM_Utils_JSON
::output($result);
82 //process the response/interview data.
83 $activityId = CRM_Campaign_Form_Task_Interview
::registerInterview($params);
85 $result['status'] = 'success';
88 CRM_Utils_JSON
::output($result);
91 public static function loadOptionGroupDetails() {
93 $id = CRM_Utils_Request
::retrieve('option_group_id', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
98 $groupParams['id'] = $id;
99 CRM_Core_OptionValue
::getValues($groupParams, $opValues);
102 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
104 $survey = new CRM_Campaign_DAO_Survey();
105 $survey->id
= $surveyId;
106 $survey->result_id
= $id;
107 if ($survey->find(TRUE)) {
108 if ($survey->recontact_interval
) {
109 $recontactInterval = CRM_Utils_String
::unserialize($survey->recontact_interval
);
110 foreach ($opValues as $opValId => $opVal) {
111 if (is_numeric($recontactInterval[$opVal['label']])) {
112 $opValues[$opValId]['interval'] = $recontactInterval[$opVal['label']];
119 if (!empty($opValues)) {
125 'result' => $opValues,
128 CRM_Utils_JSON
::output($result);
131 public function voterList() {
132 //get the search criteria params.
133 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
134 $searchParams = explode(',', $searchCriteria);
136 $params = $searchRows = [];
137 foreach ($searchParams as $param) {
138 if (!empty($_POST[$param])) {
139 $params[$param] = $_POST[$param];
143 //format multi-select group and contact types.
148 $paramValue = CRM_Utils_Array
::value($param, $params);
150 unset($params[$param]);
151 $paramValue = explode(',', $paramValue);
152 foreach ($paramValue as $key => $value) {
153 $params[$param][$value] = 1;
158 $voterClauseParams = [];
160 'campaign_survey_id',
161 'survey_interviewer_id',
162 'campaign_search_voter_for',
164 $voterClauseParams[$fld] = CRM_Utils_Array
::value($fld, $params);
167 $interviewerId = $surveyTypeId = $surveyId = NULL;
168 $searchVoterFor = $params['campaign_search_voter_for'];
169 if ($searchVoterFor == 'reserve') {
170 if (!empty($params['campaign_survey_id'])) {
171 $survey = new CRM_Campaign_DAO_Survey();
172 $survey->id
= $surveyId = $params['campaign_survey_id'];
173 $survey->selectAdd('campaign_id, activity_type_id');
175 $campaignId = $survey->campaign_id
;
176 $surveyTypeId = $survey->activity_type_id
;
178 //allow voter search in sub-part of given constituents,
179 //but make sure in case user does not select any group.
180 //get all associated campaign groups in where filter, CRM-7406
181 $groups = CRM_Utils_Array
::value('group', $params);
182 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
183 $campaignGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
184 foreach ($campaignGroups as $id => $group) {
185 $params['group'][$id] = 1;
189 //apply filter of survey contact type for search.
190 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
192 $params['contact_type'][$contactType] = 1;
195 unset($params['campaign_survey_id']);
197 unset($params['survey_interviewer_id']);
200 //get the survey status in where clause.
201 $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant
::activityStatus('name'));
202 if ($scheduledStatusId) {
203 $params['survey_status_id'] = $scheduledStatusId;
205 //BAO/Query knows reserve/release/interview processes.
206 if ($params['campaign_search_voter_for'] == 'gotv') {
207 $params['campaign_search_voter_for'] = 'release';
219 // get the data table params.
227 'name' => 'iDisplayStart',
232 'name' => 'iDisplayLength',
237 'name' => 'iSortCol_0',
239 'default' => 'sort_name',
242 'name' => 'sSortDir_0',
247 foreach ($dataTableParams as $pName => $pValues) {
248 $
$pName = $pValues['default'];
249 if (!empty($_POST[$pValues['name']])) {
250 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
251 if ($pName == 'sort') {
252 $
$pName = $selectorCols[$
$pName];
257 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params);
258 $query = new CRM_Contact_BAO_Query($queryParams,
259 NULL, NULL, FALSE, FALSE,
260 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
264 //get the voter clause to restrict and validate search.
265 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
267 $searchCount = $query->searchQuery(0, 0, NULL,
271 CRM_Utils_Array
::value('whereClause', $voterClause),
273 CRM_Utils_Array
::value('fromClause', $voterClause)
276 $iTotal = $searchCount;
287 $extraVoterColName = 'is_interview_conducted';
288 if ($params['campaign_search_voter_for'] == 'reserve') {
289 $extraVoterColName = 'reserve_voter';
292 if ($searchCount > 0) {
293 if ($searchCount < $offset) {
297 $config = CRM_Core_Config
::singleton();
299 // get the result of the search
300 $result = $query->searchQuery($offset, $rowCount, $sort,
304 CRM_Utils_Array
::value('whereClause', $voterClause),
306 CRM_Utils_Array
::value('fromClause', $voterClause)
308 while ($result->fetch()) {
309 $contactID = $result->contact_id
;
310 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
,
315 $searchRows[$contactID] = ['id' => $contactID];
316 foreach ($selectorCols as $col) {
317 $val = $result->$col;
318 if ($col == 'contact_type') {
321 $searchRows[$contactID][$col] = $val;
323 if ($searchVoterFor == 'reserve') {
324 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />';
325 $msg = ts('Respondent Reserved.');
326 $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
328 elseif ($searchVoterFor == 'gotv') {
329 $surveyActId = $result->survey_activity_id
;
330 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
331 $msg = ts('Vote Recorded.');
332 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
335 $surveyActId = $result->survey_activity_id
;
336 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
337 $msg = ts('Vote Recorded.');
338 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
340 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
344 $selectorElements = array_merge($selectorCols, [$extraVoterColName]);
346 $iFilteredTotal = $iTotal;
348 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
349 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
350 CRM_Utils_System
::civiExit();
353 public function processVoterData() {
355 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
356 if ($operation == 'release') {
357 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
358 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
360 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
369 elseif ($operation == 'reserve') {
371 $createActivity = TRUE;
372 if (!empty($_POST['activity_id'])) {
373 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
375 $createActivity = FALSE;
376 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
381 if ($activityUpdated) {
386 if ($createActivity) {
391 'assignee_contact_id',
393 $activityParams = [];
394 foreach ($ids as $id) {
395 $val = CRM_Utils_Array
::value($id, $_POST);
397 $createActivity = FALSE;
400 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
403 if ($createActivity) {
404 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
405 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
406 $scheduledStatusId = array_search('Scheduled', $activityStatus);
409 $surveyParams = ['id' => $activityParams['source_record_id']];
410 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
413 ['title', 'activity_type_id', 'campaign_id']
416 $activityTypeId = $surveyValues['activity_type_id'];
418 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
420 $surveytitle = $surveyValues['title'];
423 $subject = $surveytitle . ' - ' . ts('Respondent Reservation');
424 $activityParams['subject'] = $subject;
425 $activityParams['status_id'] = $scheduledStatusId;
426 $activityParams['skipRecentView'] = 1;
427 $activityParams['activity_date_time'] = date('YmdHis');
428 $activityParams['activity_type_id'] = $activityTypeId;
429 $activityParams['campaign_id'] = $surveyValues['campaign_id'] ??
NULL;
431 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
437 //delete reserved activity for given voter.
438 $voterIds = [$activityParams['target_contact_id']];
439 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
441 $activityParams['source_contact_id'],
444 foreach ($activities as $voterId => $values) {
445 $activityId = CRM_Utils_Array
::value('activity_id', $values);
446 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
447 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
459 elseif ($operation == 'gotv') {
460 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
461 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
469 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
478 CRM_Utils_JSON
::output(['status' => $status]);
481 public function campaignGroups() {
482 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
483 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
487 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
489 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
493 //CRM-7406 --If there is no campaign or no group associated with
494 //campaign of given survey, lets allow to search across all groups.
495 if (empty($campGroups)) {
496 $campGroups = CRM_Core_PseudoConstant
::group();
501 'title' => ts('- select -'),
504 foreach ($campGroups as $grpId => $title) {
511 'status' => 'success',
515 CRM_Utils_JSON
::output($results);
519 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
522 public static function campaignList() {
523 //get the search criteria params.
524 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
525 $searchParams = explode(',', $searchCriteria);
527 $params = $searchRows = [];
528 foreach ($searchParams as $param) {
529 if (isset($_POST[$param])) {
530 $params[$param] = $_POST[$param];
534 //this is sequence columns on datatable.
551 // get the data table params.
559 'name' => 'iDisplayStart',
564 'name' => 'iDisplayLength',
569 'name' => 'iSortCol_0',
571 'default' => 'start_date',
574 'name' => 'sSortDir_0',
579 foreach ($dataTableParams as $pName => $pValues) {
580 $
$pName = $pValues['default'];
581 if (!empty($_POST[$pValues['name']])) {
582 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
583 if ($pName == 'sort') {
584 $
$pName = $selectorCols[$
$pName];
594 $params[$sortParam] = $
$sortParam;
597 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
598 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
599 $iTotal = $searchCount;
601 if ($searchCount > 0) {
602 if ($searchCount < $offset) {
605 foreach ($campaigns as $campaignID => $values) {
606 foreach ($selectorCols as $col) {
607 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
612 $selectorElements = $selectorCols;
614 $iFilteredTotal = $iTotal;
616 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
617 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
618 CRM_Utils_System
::civiExit();
622 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
625 public function surveyList() {
626 //get the search criteria params.
627 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
628 $searchParams = explode(',', $searchCriteria);
630 $params = $searchRows = [];
631 foreach ($searchParams as $param) {
632 if (!empty($_POST[$param])) {
633 $params[$param] = $_POST[$param];
637 //this is sequence columns on datatable.
646 'default_number_of_contacts',
647 'max_number_of_contacts',
656 // get the data table params.
664 'name' => 'iDisplayStart',
669 'name' => 'iDisplayLength',
674 'name' => 'iSortCol_0',
676 'default' => 'created_date',
679 'name' => 'sSortDir_0',
684 foreach ($dataTableParams as $pName => $pValues) {
685 $
$pName = $pValues['default'];
686 if (!empty($_POST[$pValues['name']])) {
687 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
688 if ($pName == 'sort') {
689 $
$pName = $selectorCols[$
$pName];
699 $params[$sortParam] = $
$sortParam;
702 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
703 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
704 $iTotal = $searchCount;
706 if ($searchCount > 0) {
707 if ($searchCount < $offset) {
710 foreach ($surveys as $surveyID => $values) {
711 foreach ($selectorCols as $col) {
712 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
717 $selectorElements = $selectorCols;
719 $iFilteredTotal = $iTotal;
721 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
722 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
723 CRM_Utils_System
::civiExit();
727 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
730 public function petitionList() {
731 //get the search criteria params.
732 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
733 $searchParams = explode(',', $searchCriteria);
735 $params = $searchRows = [];
736 foreach ($searchParams as $param) {
737 if (!empty($_POST[$param])) {
738 $params[$param] = $_POST[$param];
742 //this is sequence columns on datatable.
756 // get the data table params.
764 'name' => 'iDisplayStart',
769 'name' => 'iDisplayLength',
774 'name' => 'iSortCol_0',
776 'default' => 'created_date',
779 'name' => 'sSortDir_0',
784 foreach ($dataTableParams as $pName => $pValues) {
785 $
$pName = $pValues['default'];
786 if (!empty($_POST[$pValues['name']])) {
787 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
788 if ($pName == 'sort') {
789 $
$pName = $selectorCols[$
$pName];
799 $params[$sortParam] = $
$sortParam;
802 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
803 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
804 $iTotal = $searchCount;
806 if ($searchCount > 0) {
807 if ($searchCount < $offset) {
810 foreach ($petitions as $petitionID => $values) {
811 foreach ($selectorCols as $col) {
812 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
817 $selectorElements = $selectorCols;
819 $iFilteredTotal = $iTotal;
821 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
822 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
823 CRM_Utils_System
::civiExit();