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
19 * This class contains all campaign related functions that are called using AJAX (jQuery)
21 class CRM_Campaign_Page_AJAX
{
23 public static function registerInterview() {
35 foreach ($fields as $fld) {
36 $params[$fld] = $_POST[$fld] ??
NULL;
38 $params['details'] = $_POST['note'] ??
NULL;
39 $voterId = $params['voter_id'];
40 $activityId = $params['activity_id'];
42 $customKey = "field_{$voterId}_custom";
43 foreach ($_POST as $key => $value) {
44 if (strpos($key, $customKey) !== FALSE) {
45 $customFieldKey = str_replace(str_replace(substr($customKey, -6), '', $customKey), '', $key);
46 $params[$customFieldKey] = $value;
50 if (isset($_POST['field']) && !empty($_POST['field'][$voterId]) &&
51 is_array($_POST['field'][$voterId])
53 foreach ($_POST['field'][$voterId] as $fieldKey => $value) {
54 $params[$fieldKey] = $value;
58 //lets pickup contat related fields.
59 foreach ($_POST as $key => $value) {
60 if (strpos($key, "field_{$voterId}_") !== FALSE &&
61 strpos($key, "field_{$voterId}_custom") === FALSE
63 $key = substr($key, strlen("field_{$voterId}_"));
64 $params[$key] = $value;
70 'voter_id' => $voterId,
71 'activity_id' => $params['interviewer_id'],
74 //time to validate custom data.
75 $errors = CRM_Core_BAO_CustomField
::validateCustomData($params);
76 if (is_array($errors) && !empty($errors)) {
77 $result['errors'] = $errors;
78 CRM_Utils_JSON
::output($result);
81 //process the response/interview data.
82 $activityId = CRM_Campaign_Form_Task_Interview
::registerInterview($params);
84 $result['status'] = 'success';
87 CRM_Utils_JSON
::output($result);
90 public static function loadOptionGroupDetails() {
92 $id = CRM_Utils_Request
::retrieve('option_group_id', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
97 $groupParams['id'] = $id;
98 CRM_Core_OptionValue
::getValues($groupParams, $opValues);
101 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
103 $survey = new CRM_Campaign_DAO_Survey();
104 $survey->id
= $surveyId;
105 $survey->result_id
= $id;
106 if ($survey->find(TRUE)) {
107 if ($survey->recontact_interval
) {
108 $recontactInterval = CRM_Utils_String
::unserialize($survey->recontact_interval
);
109 foreach ($opValues as $opValId => $opVal) {
110 if (is_numeric($recontactInterval[$opVal['label']])) {
111 $opValues[$opValId]['interval'] = $recontactInterval[$opVal['label']];
118 if (!empty($opValues)) {
124 'result' => $opValues,
127 CRM_Utils_JSON
::output($result);
130 public function voterList() {
131 //get the search criteria params.
132 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
133 $searchParams = explode(',', $searchCriteria);
135 $params = $searchRows = [];
136 foreach ($searchParams as $param) {
137 if (!empty($_POST[$param])) {
138 $params[$param] = $_POST[$param];
142 //format multi-select group and contact types.
147 $paramValue = $params[$param] ??
NULL;
149 unset($params[$param]);
150 $paramValue = explode(',', $paramValue);
151 foreach ($paramValue as $key => $value) {
152 $params[$param][$value] = 1;
157 $voterClauseParams = [];
159 'campaign_survey_id',
160 'survey_interviewer_id',
161 'campaign_search_voter_for',
163 $voterClauseParams[$fld] = $params[$fld] ??
NULL;
166 $interviewerId = $surveyTypeId = $surveyId = NULL;
167 $searchVoterFor = $params['campaign_search_voter_for'];
168 if ($searchVoterFor == 'reserve') {
169 if (!empty($params['campaign_survey_id'])) {
170 $survey = new CRM_Campaign_DAO_Survey();
171 $survey->id
= $surveyId = $params['campaign_survey_id'];
172 $survey->selectAdd('campaign_id, activity_type_id');
174 $campaignId = $survey->campaign_id
;
175 $surveyTypeId = $survey->activity_type_id
;
177 //allow voter search in sub-part of given constituents,
178 //but make sure in case user does not select any group.
179 //get all associated campaign groups in where filter, CRM-7406
180 $groups = $params['group'] ??
NULL;
181 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
182 $campaignGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
183 foreach ($campaignGroups as $id => $group) {
184 $params['group'][$id] = 1;
188 //apply filter of survey contact type for search.
189 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
191 $params['contact_type'][$contactType] = 1;
194 unset($params['campaign_survey_id']);
196 unset($params['survey_interviewer_id']);
199 //get the survey status in where clause.
200 $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant
::activityStatus('name'));
201 if ($scheduledStatusId) {
202 $params['survey_status_id'] = $scheduledStatusId;
204 //BAO/Query knows reserve/release/interview processes.
205 if ($params['campaign_search_voter_for'] == 'gotv') {
206 $params['campaign_search_voter_for'] = 'release';
218 // get the data table params.
226 'name' => 'iDisplayStart',
231 'name' => 'iDisplayLength',
236 'name' => 'iSortCol_0',
238 'default' => 'sort_name',
241 'name' => 'sSortDir_0',
246 foreach ($dataTableParams as $pName => $pValues) {
247 $
$pName = $pValues['default'];
248 if (!empty($_POST[$pValues['name']])) {
249 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
250 if ($pName == 'sort') {
251 $
$pName = $selectorCols[$
$pName];
256 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params);
257 $query = new CRM_Contact_BAO_Query($queryParams,
258 NULL, NULL, FALSE, FALSE,
259 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
263 //get the voter clause to restrict and validate search.
264 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
266 $searchCount = $query->searchQuery(0, 0, NULL,
270 CRM_Utils_Array
::value('whereClause', $voterClause),
272 CRM_Utils_Array
::value('fromClause', $voterClause)
275 $iTotal = $searchCount;
286 $extraVoterColName = 'is_interview_conducted';
287 if ($params['campaign_search_voter_for'] == 'reserve') {
288 $extraVoterColName = 'reserve_voter';
291 if ($searchCount > 0) {
292 if ($searchCount < $offset) {
296 $config = CRM_Core_Config
::singleton();
298 // get the result of the search
299 $result = $query->searchQuery($offset, $rowCount, $sort,
303 CRM_Utils_Array
::value('whereClause', $voterClause),
305 CRM_Utils_Array
::value('fromClause', $voterClause)
307 while ($result->fetch()) {
308 $contactID = $result->contact_id
;
309 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
,
314 $searchRows[$contactID] = ['id' => $contactID];
315 foreach ($selectorCols as $col) {
316 $val = $result->$col;
317 if ($col == 'contact_type') {
320 $searchRows[$contactID][$col] = $val;
322 if ($searchVoterFor == 'reserve') {
323 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />';
324 $msg = ts('Respondent Reserved.');
325 $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
327 elseif ($searchVoterFor == 'gotv') {
328 $surveyActId = $result->survey_activity_id
;
329 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
330 $msg = ts('Vote Recorded.');
331 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
334 $surveyActId = $result->survey_activity_id
;
335 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
336 $msg = ts('Vote Recorded.');
337 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
339 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
343 $selectorElements = array_merge($selectorCols, [$extraVoterColName]);
345 $iFilteredTotal = $iTotal;
347 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
348 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
349 CRM_Utils_System
::civiExit();
352 public function processVoterData() {
354 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
355 if ($operation == 'release') {
356 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
357 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
359 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
368 elseif ($operation == 'reserve') {
370 $createActivity = TRUE;
371 if (!empty($_POST['activity_id'])) {
372 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
374 $createActivity = FALSE;
375 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
380 if ($activityUpdated) {
385 if ($createActivity) {
390 'assignee_contact_id',
392 $activityParams = [];
393 foreach ($ids as $id) {
394 $val = $_POST[$id] ??
NULL;
396 $createActivity = FALSE;
399 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
402 if ($createActivity) {
403 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
404 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
405 $scheduledStatusId = array_search('Scheduled', $activityStatus);
408 $surveyParams = ['id' => $activityParams['source_record_id']];
409 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
412 ['title', 'activity_type_id', 'campaign_id']
415 $activityTypeId = $surveyValues['activity_type_id'];
417 $surveytitle = $_POST['surveyTitle'] ??
NULL;
419 $surveytitle = $surveyValues['title'];
422 $subject = $surveytitle . ' - ' . ts('Respondent Reservation');
423 $activityParams['subject'] = $subject;
424 $activityParams['status_id'] = $scheduledStatusId;
425 $activityParams['skipRecentView'] = 1;
426 $activityParams['activity_date_time'] = date('YmdHis');
427 $activityParams['activity_type_id'] = $activityTypeId;
428 $activityParams['campaign_id'] = $surveyValues['campaign_id'] ??
NULL;
430 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
436 //delete reserved activity for given voter.
437 $voterIds = [$activityParams['target_contact_id']];
438 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
440 $activityParams['source_contact_id'],
443 foreach ($activities as $voterId => $values) {
444 $activityId = $values['activity_id'] ??
NULL;
445 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
446 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
458 elseif ($operation == 'gotv') {
459 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
460 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
468 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
477 CRM_Utils_JSON
::output(['status' => $status]);
480 public function campaignGroups() {
481 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
482 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
486 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
488 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
492 //CRM-7406 --If there is no campaign or no group associated with
493 //campaign of given survey, lets allow to search across all groups.
494 if (empty($campGroups)) {
495 $campGroups = CRM_Core_PseudoConstant
::group();
500 'title' => ts('- select -'),
503 foreach ($campGroups as $grpId => $title) {
510 'status' => 'success',
514 CRM_Utils_JSON
::output($results);
518 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
521 public static function campaignList() {
522 //get the search criteria params.
523 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
524 $searchParams = explode(',', $searchCriteria);
526 $params = $searchRows = [];
527 foreach ($searchParams as $param) {
528 if (isset($_POST[$param])) {
529 $params[$param] = $_POST[$param];
533 //this is sequence columns on datatable.
550 // get the data table params.
558 'name' => 'iDisplayStart',
563 'name' => 'iDisplayLength',
568 'name' => 'iSortCol_0',
570 'default' => 'start_date',
573 'name' => 'sSortDir_0',
578 foreach ($dataTableParams as $pName => $pValues) {
579 $
$pName = $pValues['default'];
580 if (!empty($_POST[$pValues['name']])) {
581 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
582 if ($pName == 'sort') {
583 $
$pName = $selectorCols[$
$pName];
593 $params[$sortParam] = $
$sortParam;
596 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
597 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
598 $iTotal = $searchCount;
600 if ($searchCount > 0) {
601 if ($searchCount < $offset) {
604 foreach ($campaigns as $campaignID => $values) {
605 foreach ($selectorCols as $col) {
606 $searchRows[$campaignID][$col] = $values[$col] ??
NULL;
611 $selectorElements = $selectorCols;
613 $iFilteredTotal = $iTotal;
615 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
616 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
617 CRM_Utils_System
::civiExit();
621 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
624 public function surveyList() {
625 //get the search criteria params.
626 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
627 $searchParams = explode(',', $searchCriteria);
629 $params = $searchRows = [];
630 foreach ($searchParams as $param) {
631 if (!empty($_POST[$param])) {
632 $params[$param] = $_POST[$param];
636 //this is sequence columns on datatable.
645 'default_number_of_contacts',
646 'max_number_of_contacts',
655 // get the data table params.
663 'name' => 'iDisplayStart',
668 'name' => 'iDisplayLength',
673 'name' => 'iSortCol_0',
675 'default' => 'created_date',
678 'name' => 'sSortDir_0',
683 foreach ($dataTableParams as $pName => $pValues) {
684 $
$pName = $pValues['default'];
685 if (!empty($_POST[$pValues['name']])) {
686 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
687 if ($pName == 'sort') {
688 $
$pName = $selectorCols[$
$pName];
698 $params[$sortParam] = $
$sortParam;
701 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
702 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
703 $iTotal = $searchCount;
705 if ($searchCount > 0) {
706 if ($searchCount < $offset) {
709 foreach ($surveys as $surveyID => $values) {
710 foreach ($selectorCols as $col) {
711 $searchRows[$surveyID][$col] = $values[$col] ??
NULL;
716 $selectorElements = $selectorCols;
718 $iFilteredTotal = $iTotal;
720 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
721 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
722 CRM_Utils_System
::civiExit();
726 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
729 public function petitionList() {
730 //get the search criteria params.
731 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
732 $searchParams = explode(',', $searchCriteria);
734 $params = $searchRows = [];
735 foreach ($searchParams as $param) {
736 if (!empty($_POST[$param])) {
737 $params[$param] = $_POST[$param];
741 //this is sequence columns on datatable.
755 // get the data table params.
763 'name' => 'iDisplayStart',
768 'name' => 'iDisplayLength',
773 'name' => 'iSortCol_0',
775 'default' => 'created_date',
778 'name' => 'sSortDir_0',
783 foreach ($dataTableParams as $pName => $pValues) {
784 $
$pName = $pValues['default'];
785 if (!empty($_POST[$pValues['name']])) {
786 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
787 if ($pName == 'sort') {
788 $
$pName = $selectorCols[$
$pName];
798 $params[$sortParam] = $
$sortParam;
801 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
802 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
803 $iTotal = $searchCount;
805 if ($searchCount > 0) {
806 if ($searchCount < $offset) {
809 foreach ($petitions as $petitionID => $values) {
810 foreach ($selectorCols as $col) {
811 $searchRows[$petitionID][$col] = $values[$col] ??
NULL;
816 $selectorElements = $selectorCols;
818 $iFilteredTotal = $iTotal;
820 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
821 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
822 CRM_Utils_System
::civiExit();