3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
36 * This class contains all campaign related functions that are called using AJAX (jQuery)
38 class CRM_Campaign_Page_AJAX
{
40 public static function registerInterview() {
52 foreach ($fields as $fld) {
53 $params[$fld] = CRM_Utils_Array
::value($fld, $_POST);
55 $params['details'] = CRM_Utils_Array
::value('note', $_POST);
56 $voterId = $params['voter_id'];
57 $activityId = $params['activity_id'];
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;
67 if (isset($_POST['field']) && !empty($_POST['field'][$voterId]) &&
68 is_array($_POST['field'][$voterId])
70 foreach ($_POST['field'][$voterId] as $fieldKey => $value) {
71 $params[$fieldKey] = $value;
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
80 $key = substr($key, strlen("field_{$voterId}_"));
81 $params[$key] = $value;
87 'voter_id' => $voterId,
88 'activity_id' => $params['interviewer_id'],
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);
98 //process the response/interview data.
99 $activityId = CRM_Campaign_Form_Task_Interview
::registerInterview($params);
101 $result['status'] = 'success';
104 CRM_Utils_JSON
::output($result);
107 public static function loadOptionGroupDetails() {
109 $id = CRM_Utils_Request
::retrieve('option_group_id', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
114 $groupParams['id'] = $id;
115 CRM_Core_OptionValue
::getValues($groupParams, $opValues);
118 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Integer', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
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']];
135 if (!empty($opValues)) {
141 'result' => $opValues,
144 CRM_Utils_JSON
::output($result);
147 public function voterList() {
148 //get the search criteria params.
149 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
150 $searchParams = explode(',', $searchCriteria);
152 $params = $searchRows = array();
153 foreach ($searchParams as $param) {
154 if (!empty($_POST[$param])) {
155 $params[$param] = $_POST[$param];
159 //format multi-select group and contact types.
164 $paramValue = CRM_Utils_Array
::value($param, $params);
166 unset($params[$param]);
167 $paramValue = explode(',', $paramValue);
168 foreach ($paramValue as $key => $value) {
169 $params[$param][$value] = 1;
174 $voterClauseParams = array();
176 'campaign_survey_id',
177 'survey_interviewer_id',
178 'campaign_search_voter_for',
180 $voterClauseParams[$fld] = CRM_Utils_Array
::value($fld, $params);
183 $interviewerId = $surveyTypeId = $surveyId = NULL;
184 $searchVoterFor = $params['campaign_search_voter_for'];
185 if ($searchVoterFor == 'reserve') {
186 if (!empty($params['campaign_survey_id'])) {
187 $survey = new CRM_Campaign_DAO_Survey();
188 $survey->id
= $surveyId = $params['campaign_survey_id'];
189 $survey->selectAdd('campaign_id, activity_type_id');
191 $campaignId = $survey->campaign_id
;
192 $surveyTypeId = $survey->activity_type_id
;
194 //allow voter search in sub-part of given constituents,
195 //but make sure in case user does not select any group.
196 //get all associated campaign groups in where filter, CRM-7406
197 $groups = CRM_Utils_Array
::value('group', $params);
198 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
199 $campaignGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
200 foreach ($campaignGroups as $id => $group) {
201 $params['group'][$id] = 1;
205 //apply filter of survey contact type for search.
206 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
208 $params['contact_type'][$contactType] = 1;
211 unset($params['campaign_survey_id']);
213 unset($params['survey_interviewer_id']);
216 //get the survey status in where clause.
217 $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant
::activityStatus('name'));
218 if ($scheduledStatusId) {
219 $params['survey_status_id'] = $scheduledStatusId;
221 //BAO/Query knows reserve/release/interview processes.
222 if ($params['campaign_search_voter_for'] == 'gotv') {
223 $params['campaign_search_voter_for'] = 'release';
227 $selectorCols = array(
235 // get the data table params.
236 $dataTableParams = array(
243 'name' => 'iDisplayStart',
248 'name' => 'iDisplayLength',
253 'name' => 'iSortCol_0',
255 'default' => 'sort_name',
257 'sortOrder' => array(
258 'name' => 'sSortDir_0',
263 foreach ($dataTableParams as $pName => $pValues) {
264 $
$pName = $pValues['default'];
265 if (!empty($_POST[$pValues['name']])) {
266 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
267 if ($pName == 'sort') {
268 $
$pName = $selectorCols[$
$pName];
273 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params);
274 $query = new CRM_Contact_BAO_Query($queryParams,
275 NULL, NULL, FALSE, FALSE,
276 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
280 //get the voter clause to restrict and validate search.
281 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
283 $searchCount = $query->searchQuery(0, 0, NULL,
287 CRM_Utils_Array
::value('whereClause', $voterClause),
289 CRM_Utils_Array
::value('fromClause', $voterClause)
292 $iTotal = $searchCount;
294 $selectorCols = array(
303 $extraVoterColName = 'is_interview_conducted';
304 if ($params['campaign_search_voter_for'] == 'reserve') {
305 $extraVoterColName = 'reserve_voter';
308 if ($searchCount > 0) {
309 if ($searchCount < $offset) {
313 $config = CRM_Core_Config
::singleton();
315 // get the result of the search
316 $result = $query->searchQuery($offset, $rowCount, $sort,
320 CRM_Utils_Array
::value('whereClause', $voterClause),
322 CRM_Utils_Array
::value('fromClause', $voterClause)
324 while ($result->fetch()) {
325 $contactID = $result->contact_id
;
326 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
,
331 $searchRows[$contactID] = array('id' => $contactID);
332 foreach ($selectorCols as $col) {
333 $val = $result->$col;
334 if ($col == 'contact_type') {
337 $searchRows[$contactID][$col] = $val;
339 if ($searchVoterFor == 'reserve') {
340 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />';
341 $msg = ts('Respondent Reserved.');
342 $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
344 elseif ($searchVoterFor == 'gotv') {
345 $surveyActId = $result->survey_activity_id
;
346 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
347 $msg = ts('Vote Recorded.');
348 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
351 $surveyActId = $result->survey_activity_id
;
352 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
353 $msg = ts('Vote Recorded.');
354 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
356 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
360 $selectorElements = array_merge($selectorCols, array($extraVoterColName));
362 $iFilteredTotal = $iTotal;
364 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
365 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
366 CRM_Utils_System
::civiExit();
369 public function processVoterData() {
371 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
372 if ($operation == 'release') {
373 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
374 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
376 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
385 elseif ($operation == 'reserve') {
387 $createActivity = TRUE;
388 if (!empty($_POST['activity_id'])) {
389 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
391 $createActivity = FALSE;
392 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
397 if ($activityUpdated) {
402 if ($createActivity) {
407 'assignee_contact_id',
409 $activityParams = array();
410 foreach ($ids as $id) {
411 $val = CRM_Utils_Array
::value($id, $_POST);
413 $createActivity = FALSE;
416 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
419 if ($createActivity) {
420 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
421 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
422 $scheduledStatusId = array_search('Scheduled', $activityStatus);
424 $surveyValues = array();
425 $surveyParams = array('id' => $activityParams['source_record_id']);
426 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
429 array('title', 'activity_type_id', 'campaign_id')
432 $activityTypeId = $surveyValues['activity_type_id'];
434 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
436 $surveytitle = $surveyValues['title'];
439 $subject = $surveytitle . ' - ' . ts('Respondent Reservation');
440 $activityParams['subject'] = $subject;
441 $activityParams['status_id'] = $scheduledStatusId;
442 $activityParams['skipRecentView'] = 1;
443 $activityParams['activity_date_time'] = date('YmdHis');
444 $activityParams['activity_type_id'] = $activityTypeId;
445 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
447 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
453 //delete reserved activity for given voter.
454 $voterIds = array($activityParams['target_contact_id']);
455 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
457 $activityParams['source_contact_id'],
458 array($scheduledStatusId)
460 foreach ($activities as $voterId => $values) {
461 $activityId = CRM_Utils_Array
::value('activity_id', $values);
462 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
463 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
475 elseif ($operation == 'gotv') {
476 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
477 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
485 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
494 CRM_Utils_JSON
::output(array('status' => $status));
497 public function allActiveCampaigns() {
498 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
499 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
503 'title' => ts('- select -'),
506 foreach ($campaigns as $value => $title) {
508 if (!array_key_exists($value, $currentCampaigns)) {
509 $class = 'status-past';
518 if (count($options) > 1) {
524 'campaigns' => $options,
527 CRM_Utils_JSON
::output($results);
530 public function campaignGroups() {
531 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
532 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
534 $campGroups = array();
536 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
538 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
542 //CRM-7406 --If there is no campaign or no group associated with
543 //campaign of given survey, lets allow to search across all groups.
544 if (empty($campGroups)) {
545 $campGroups = CRM_Core_PseudoConstant
::group();
550 'title' => ts('- select -'),
553 foreach ($campGroups as $grpId => $title) {
560 'status' => 'success',
564 CRM_Utils_JSON
::output($results);
568 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
571 public static function campaignList() {
572 //get the search criteria params.
573 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
574 $searchParams = explode(',', $searchCriteria);
576 $params = $searchRows = array();
577 foreach ($searchParams as $param) {
578 if (isset($_POST[$param])) {
579 $params[$param] = $_POST[$param];
583 //this is sequence columns on datatable.
584 $selectorCols = array(
600 // get the data table params.
601 $dataTableParams = array(
608 'name' => 'iDisplayStart',
613 'name' => 'iDisplayLength',
618 'name' => 'iSortCol_0',
620 'default' => 'start_date',
622 'sortOrder' => array(
623 'name' => 'sSortDir_0',
628 foreach ($dataTableParams as $pName => $pValues) {
629 $
$pName = $pValues['default'];
630 if (!empty($_POST[$pValues['name']])) {
631 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
632 if ($pName == 'sort') {
633 $
$pName = $selectorCols[$
$pName];
643 $params[$sortParam] = $
$sortParam;
646 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
647 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
648 $iTotal = $searchCount;
650 if ($searchCount > 0) {
651 if ($searchCount < $offset) {
654 foreach ($campaigns as $campaignID => $values) {
655 foreach ($selectorCols as $col) {
656 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
661 $selectorElements = $selectorCols;
663 $iFilteredTotal = $iTotal;
665 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
666 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
667 CRM_Utils_System
::civiExit();
671 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
674 public function surveyList() {
675 //get the search criteria params.
676 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
677 $searchParams = explode(',', $searchCriteria);
679 $params = $searchRows = array();
680 foreach ($searchParams as $param) {
681 if (!empty($_POST[$param])) {
682 $params[$param] = $_POST[$param];
686 //this is sequence columns on datatable.
687 $selectorCols = array(
695 'default_number_of_contacts',
696 'max_number_of_contacts',
705 // get the data table params.
706 $dataTableParams = array(
713 'name' => 'iDisplayStart',
718 'name' => 'iDisplayLength',
723 'name' => 'iSortCol_0',
725 'default' => 'created_date',
727 'sortOrder' => array(
728 'name' => 'sSortDir_0',
733 foreach ($dataTableParams as $pName => $pValues) {
734 $
$pName = $pValues['default'];
735 if (!empty($_POST[$pValues['name']])) {
736 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
737 if ($pName == 'sort') {
738 $
$pName = $selectorCols[$
$pName];
748 $params[$sortParam] = $
$sortParam;
751 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
752 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
753 $iTotal = $searchCount;
755 if ($searchCount > 0) {
756 if ($searchCount < $offset) {
759 foreach ($surveys as $surveyID => $values) {
760 foreach ($selectorCols as $col) {
761 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
766 $selectorElements = $selectorCols;
768 $iFilteredTotal = $iTotal;
770 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
771 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
772 CRM_Utils_System
::civiExit();
776 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
779 public function petitionList() {
780 //get the search criteria params.
781 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
782 $searchParams = explode(',', $searchCriteria);
784 $params = $searchRows = array();
785 foreach ($searchParams as $param) {
786 if (!empty($_POST[$param])) {
787 $params[$param] = $_POST[$param];
791 //this is sequence columns on datatable.
792 $selectorCols = array(
805 // get the data table params.
806 $dataTableParams = array(
813 'name' => 'iDisplayStart',
818 'name' => 'iDisplayLength',
823 'name' => 'iSortCol_0',
825 'default' => 'created_date',
827 'sortOrder' => array(
828 'name' => 'sSortDir_0',
833 foreach ($dataTableParams as $pName => $pValues) {
834 $
$pName = $pValues['default'];
835 if (!empty($_POST[$pValues['name']])) {
836 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
837 if ($pName == 'sort') {
838 $
$pName = $selectorCols[$
$pName];
848 $params[$sortParam] = $
$sortParam;
851 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
852 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
853 $iTotal = $searchCount;
855 if ($searchCount > 0) {
856 if ($searchCount < $offset) {
859 foreach ($petitions as $petitionID => $values) {
860 foreach ($selectorCols as $col) {
861 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
866 $selectorElements = $selectorCols;
868 $iFilteredTotal = $iTotal;
870 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
871 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
872 CRM_Utils_System
::civiExit();