3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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_Array
::value('option_group_id', $_POST);
114 $groupParams['id'] = $id;
115 CRM_Core_OptionValue
::getValues($groupParams, $opValues);
118 $surveyId = CRM_Utils_Array
::value('survey_id', $_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 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
151 $params = $searchRows = array();
152 foreach ($searchParams as $param) {
153 if (!empty($_POST[$param])) {
154 $params[$param] = $_POST[$param];
158 //format multi-select group and contact types.
163 $paramValue = CRM_Utils_Array
::value($param, $params);
165 unset($params[$param]);
166 $paramValue = explode(',', $paramValue);
167 foreach ($paramValue as $key => $value) {
168 $params[$param][$value] = 1;
173 $voterClauseParams = array();
175 'campaign_survey_id',
176 'survey_interviewer_id',
177 'campaign_search_voter_for'
179 $voterClauseParams[$fld] = CRM_Utils_Array
::value($fld, $params);
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');
190 $campaignId = $survey->campaign_id
;
191 $surveyTypeId = $survey->activity_type_id
;
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;
204 //apply filter of survey contact type for search.
205 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
207 $params['contact_type'][$contactType] = 1;
210 unset($params['campaign_survey_id']);
212 unset($params['survey_interviewer_id']);
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;
220 //BAO/Query knows reserve/release/interview processes.
221 if ($params['campaign_search_voter_for'] == 'gotv') {
222 $params['campaign_search_voter_for'] = 'release';
226 $selectorCols = array(
234 // get the data table params.
235 $dataTableParams = array(
242 'name' => 'iDisplayStart',
247 'name' => 'iDisplayLength',
252 'name' => 'iSortCol_0',
254 'default' => 'sort_name',
256 'sortOrder' => array(
257 'name' => 'sSortDir_0',
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];
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
,
279 //get the voter clause to restrict and validate search.
280 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
282 $searchCount = $query->searchQuery(0, 0, NULL,
286 CRM_Utils_Array
::value('whereClause', $voterClause),
288 CRM_Utils_Array
::value('fromClause', $voterClause)
291 $iTotal = $searchCount;
293 $selectorCols = array(
302 $extraVoterColName = 'is_interview_conducted';
303 if ($params['campaign_search_voter_for'] == 'reserve') {
304 $extraVoterColName = 'reserve_voter';
307 if ($searchCount > 0) {
308 if ($searchCount < $offset) {
312 $config = CRM_Core_Config
::singleton();
314 // get the result of the search
315 $result = $query->searchQuery($offset, $rowCount, $sort,
319 CRM_Utils_Array
::value('whereClause', $voterClause),
321 CRM_Utils_Array
::value('fromClause', $voterClause)
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
,
330 $searchRows[$contactID] = array('id' => $contactID);
331 foreach ($selectorCols as $col) {
332 $val = $result->$col;
333 if ($col == 'contact_type') {
336 $searchRows[$contactID][$col] = $val;
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 .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
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 .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
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 .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
355 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
359 $selectorElements = array_merge($selectorCols, array($extraVoterColName));
361 $iFilteredTotal = $iTotal;
363 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
364 CRM_Utils_System
::civiExit();
367 public function processVoterData() {
369 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
370 if ($operation == 'release') {
371 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
372 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
374 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
383 elseif ($operation == 'reserve') {
385 $createActivity = TRUE;
386 if (!empty($_POST['activity_id'])) {
387 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
389 $createActivity = FALSE;
390 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
395 if ($activityUpdated) {
400 if ($createActivity) {
405 'assignee_contact_id',
407 $activityParams = array();
408 foreach ($ids as $id) {
409 $val = CRM_Utils_Array
::value($id, $_POST);
411 $createActivity = FALSE;
414 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
417 if ($createActivity) {
418 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
419 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
420 $scheduledStatusId = array_search('Scheduled', $activityStatus);
422 $surveyValues = array();
423 $surveyParams = array('id' => $activityParams['source_record_id']);
424 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
427 array('title', 'activity_type_id', 'campaign_id')
430 $activityTypeId = $surveyValues['activity_type_id'];
432 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
434 $surveytitle = $surveyValues['title'];
437 $subject = ts('%1', array(1 => $surveytitle)) . ' - ' . ts('Respondent Reservation');
438 $activityParams['subject'] = $subject;
439 $activityParams['status_id'] = $scheduledStatusId;
440 $activityParams['skipRecentView'] = 1;
441 $activityParams['activity_date_time'] = date('YmdHis');
442 $activityParams['activity_type_id'] = $activityTypeId;
443 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
445 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
451 //delete reserved activity for given voter.
452 $voterIds = array($activityParams['target_contact_id']);
453 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
455 $activityParams['source_contact_id'],
456 array($scheduledStatusId)
458 foreach ($activities as $voterId => $values) {
459 $activityId = CRM_Utils_Array
::value('activity_id', $values);
460 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
461 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
473 elseif ($operation == 'gotv') {
474 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
475 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
483 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
492 CRM_Utils_JSON
::output(array('status' => $status));
495 public function allActiveCampaigns() {
496 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
497 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
501 'title' => ts('- select -'),
504 foreach ($campaigns as $value => $title) {
506 if (!array_key_exists($value, $currentCampaigns)) {
507 $class = 'status-past';
516 if (count($options) > 1) {
522 'campaigns' => $options,
525 CRM_Utils_JSON
::output($results);
528 public function campaignGroups() {
529 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
530 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
532 $campGroups = array();
534 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
536 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
540 //CRM-7406 --If there is no campaign or no group associated with
541 //campaign of given survey, lets allow to search across all groups.
542 if (empty($campGroups)) {
543 $campGroups = CRM_Core_PseudoConstant
::group();
548 'title' => ts('- select -'),
551 foreach ($campGroups as $grpId => $title) {
558 'status' => 'success',
562 CRM_Utils_JSON
::output($results);
566 * Retrieve campaigns as for campaign dashboard.
569 public function campaignList() {
570 //get the search criteria params.
571 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
573 $params = $searchRows = array();
574 foreach ($searchParams as $param) {
575 if (!empty($_POST[$param])) {
576 $params[$param] = $_POST[$param];
580 //this is sequence columns on datatable.
581 $selectorCols = array(
597 // get the data table params.
598 $dataTableParams = array(
605 'name' => 'iDisplayStart',
610 'name' => 'iDisplayLength',
615 'name' => 'iSortCol_0',
617 'default' => 'start_date',
619 'sortOrder' => array(
620 'name' => 'sSortDir_0',
625 foreach ($dataTableParams as $pName => $pValues) {
626 $
$pName = $pValues['default'];
627 if (!empty($_POST[$pValues['name']])) {
628 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
629 if ($pName == 'sort') {
630 $
$pName = $selectorCols[$
$pName];
640 $params[$sortParam] = $
$sortParam;
643 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
644 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
645 $iTotal = $searchCount;
647 if ($searchCount > 0) {
648 if ($searchCount < $offset) {
651 foreach ($campaigns as $campaignID => $values) {
652 foreach ($selectorCols as $col) {
653 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
658 $selectorElements = $selectorCols;
660 $iFilteredTotal = $iTotal;
662 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
663 CRM_Utils_System
::civiExit();
667 * Retrieve survey for survey dashboard.
670 public function surveyList() {
671 //get the search criteria params.
672 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
674 $params = $searchRows = array();
675 foreach ($searchParams as $param) {
676 if (!empty($_POST[$param])) {
677 $params[$param] = $_POST[$param];
681 //this is sequence columns on datatable.
682 $selectorCols = array(
690 'default_number_of_contacts',
691 'max_number_of_contacts',
700 // get the data table params.
701 $dataTableParams = array(
708 'name' => 'iDisplayStart',
713 'name' => 'iDisplayLength',
718 'name' => 'iSortCol_0',
720 'default' => 'created_date',
722 'sortOrder' => array(
723 'name' => 'sSortDir_0',
728 foreach ($dataTableParams as $pName => $pValues) {
729 $
$pName = $pValues['default'];
730 if (!empty($_POST[$pValues['name']])) {
731 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
732 if ($pName == 'sort') {
733 $
$pName = $selectorCols[$
$pName];
743 $params[$sortParam] = $
$sortParam;
746 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
747 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
748 $iTotal = $searchCount;
750 if ($searchCount > 0) {
751 if ($searchCount < $offset) {
754 foreach ($surveys as $surveyID => $values) {
755 foreach ($selectorCols as $col) {
756 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
761 $selectorElements = $selectorCols;
763 $iFilteredTotal = $iTotal;
765 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
766 CRM_Utils_System
::civiExit();
770 * Retrieve petitions for petition dashboard.
773 public function petitionList() {
774 //get the search criteria params.
775 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
777 $params = $searchRows = array();
778 foreach ($searchParams as $param) {
779 if (!empty($_POST[$param])) {
780 $params[$param] = $_POST[$param];
784 //this is sequence columns on datatable.
785 $selectorCols = array(
798 // get the data table params.
799 $dataTableParams = array(
806 'name' => 'iDisplayStart',
811 'name' => 'iDisplayLength',
816 'name' => 'iSortCol_0',
818 'default' => 'created_date',
820 'sortOrder' => array(
821 'name' => 'sSortDir_0',
826 foreach ($dataTableParams as $pName => $pValues) {
827 $
$pName = $pValues['default'];
828 if (!empty($_POST[$pValues['name']])) {
829 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
830 if ($pName == 'sort') {
831 $
$pName = $selectorCols[$
$pName];
841 $params[$sortParam] = $
$sortParam;
844 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
845 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
846 $iTotal = $searchCount;
848 if ($searchCount > 0) {
849 if ($searchCount < $offset) {
852 foreach ($petitions as $petitionID => $values) {
853 foreach ($selectorCols as $col) {
854 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
859 $selectorElements = $selectorCols;
861 $iFilteredTotal = $iTotal;
863 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
864 CRM_Utils_System
::civiExit();