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 = [];
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 = [];
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';
235 // get the data table params.
243 'name' => 'iDisplayStart',
248 'name' => 'iDisplayLength',
253 'name' => 'iSortCol_0',
255 'default' => 'sort_name',
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;
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] = ['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, [$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 = [];
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);
425 $surveyParams = ['id' => $activityParams['source_record_id']];
426 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
429 ['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 = [$activityParams['target_contact_id']];
455 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
457 $activityParams['source_contact_id'],
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(['status' => $status]);
497 public function campaignGroups() {
498 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
499 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
503 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
505 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
509 //CRM-7406 --If there is no campaign or no group associated with
510 //campaign of given survey, lets allow to search across all groups.
511 if (empty($campGroups)) {
512 $campGroups = CRM_Core_PseudoConstant
::group();
517 'title' => ts('- select -'),
520 foreach ($campGroups as $grpId => $title) {
527 'status' => 'success',
531 CRM_Utils_JSON
::output($results);
535 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
538 public static function campaignList() {
539 //get the search criteria params.
540 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
541 $searchParams = explode(',', $searchCriteria);
543 $params = $searchRows = [];
544 foreach ($searchParams as $param) {
545 if (isset($_POST[$param])) {
546 $params[$param] = $_POST[$param];
550 //this is sequence columns on datatable.
567 // get the data table params.
575 'name' => 'iDisplayStart',
580 'name' => 'iDisplayLength',
585 'name' => 'iSortCol_0',
587 'default' => 'start_date',
590 'name' => 'sSortDir_0',
595 foreach ($dataTableParams as $pName => $pValues) {
596 $
$pName = $pValues['default'];
597 if (!empty($_POST[$pValues['name']])) {
598 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
599 if ($pName == 'sort') {
600 $
$pName = $selectorCols[$
$pName];
610 $params[$sortParam] = $
$sortParam;
613 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
614 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
615 $iTotal = $searchCount;
617 if ($searchCount > 0) {
618 if ($searchCount < $offset) {
621 foreach ($campaigns as $campaignID => $values) {
622 foreach ($selectorCols as $col) {
623 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
628 $selectorElements = $selectorCols;
630 $iFilteredTotal = $iTotal;
632 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
633 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
634 CRM_Utils_System
::civiExit();
638 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
641 public function surveyList() {
642 //get the search criteria params.
643 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
644 $searchParams = explode(',', $searchCriteria);
646 $params = $searchRows = [];
647 foreach ($searchParams as $param) {
648 if (!empty($_POST[$param])) {
649 $params[$param] = $_POST[$param];
653 //this is sequence columns on datatable.
662 'default_number_of_contacts',
663 'max_number_of_contacts',
672 // get the data table params.
680 'name' => 'iDisplayStart',
685 'name' => 'iDisplayLength',
690 'name' => 'iSortCol_0',
692 'default' => 'created_date',
695 'name' => 'sSortDir_0',
700 foreach ($dataTableParams as $pName => $pValues) {
701 $
$pName = $pValues['default'];
702 if (!empty($_POST[$pValues['name']])) {
703 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
704 if ($pName == 'sort') {
705 $
$pName = $selectorCols[$
$pName];
715 $params[$sortParam] = $
$sortParam;
718 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
719 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
720 $iTotal = $searchCount;
722 if ($searchCount > 0) {
723 if ($searchCount < $offset) {
726 foreach ($surveys as $surveyID => $values) {
727 foreach ($selectorCols as $col) {
728 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
733 $selectorElements = $selectorCols;
735 $iFilteredTotal = $iTotal;
737 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
738 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
739 CRM_Utils_System
::civiExit();
743 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
746 public function petitionList() {
747 //get the search criteria params.
748 $searchCriteria = CRM_Utils_Request
::retrieve('searchCriteria', 'String', CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST');
749 $searchParams = explode(',', $searchCriteria);
751 $params = $searchRows = [];
752 foreach ($searchParams as $param) {
753 if (!empty($_POST[$param])) {
754 $params[$param] = $_POST[$param];
758 //this is sequence columns on datatable.
772 // get the data table params.
780 'name' => 'iDisplayStart',
785 'name' => 'iDisplayLength',
790 'name' => 'iSortCol_0',
792 'default' => 'created_date',
795 'name' => 'sSortDir_0',
800 foreach ($dataTableParams as $pName => $pValues) {
801 $
$pName = $pValues['default'];
802 if (!empty($_POST[$pValues['name']])) {
803 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
804 if ($pName == 'sort') {
805 $
$pName = $selectorCols[$
$pName];
815 $params[$sortParam] = $
$sortParam;
818 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
819 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
820 $iTotal = $searchCount;
822 if ($searchCount > 0) {
823 if ($searchCount < $offset) {
826 foreach ($petitions as $petitionID => $values) {
827 foreach ($selectorCols as $col) {
828 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
833 $selectorElements = $selectorCols;
835 $iFilteredTotal = $iTotal;
837 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
838 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
839 CRM_Utils_System
::civiExit();