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.
160 'group', 'contact_type') as $param) {
161 $paramValue = CRM_Utils_Array
::value($param, $params);
163 unset($params[$param]);
164 $paramValue = explode(',', $paramValue);
165 foreach ($paramValue as $key => $value) {
166 $params[$param][$value] = 1;
171 $voterClauseParams = array();
173 'campaign_survey_id', 'survey_interviewer_id', 'campaign_search_voter_for') as $fld) {
174 $voterClauseParams[$fld] = CRM_Utils_Array
::value($fld, $params);
177 $interviewerId = $surveyTypeId = $surveyId = NULL;
178 $searchVoterFor = $params['campaign_search_voter_for'];
179 if ($searchVoterFor == 'reserve') {
180 if (!empty($params['campaign_survey_id'])) {
181 $survey = new CRM_Campaign_DAO_Survey();
182 $survey->id
= $surveyId = $params['campaign_survey_id'];
183 $survey->selectAdd('campaign_id, activity_type_id');
185 $campaignId = $survey->campaign_id
;
186 $surveyTypeId = $survey->activity_type_id
;
188 //allow voter search in sub-part of given constituents,
189 //but make sure in case user does not select any group.
190 //get all associated campaign groups in where filter, CRM-7406
191 $groups = CRM_Utils_Array
::value('group', $params);
192 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
193 $campaignGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
194 foreach ($campaignGroups as $id => $group) { $params['group'][$id] = 1;
198 //apply filter of survey contact type for search.
199 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
201 $params['contact_type'][$contactType] = 1;
204 unset($params['campaign_survey_id']);
206 unset($params['survey_interviewer_id']);
209 //get the survey status in where clause.
210 $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant
::activityStatus('name'));
211 if ($scheduledStatusId) {
212 $params['survey_status_id'] = $scheduledStatusId;
214 //BAO/Query knows reserve/release/interview processes.
215 if ($params['campaign_search_voter_for'] == 'gotv') {
216 $params['campaign_search_voter_for'] = 'release';
220 $selectorCols = array(
228 // get the data table params.
229 $dataTableParams = array(
236 'name' => 'iDisplayStart',
241 'name' => 'iDisplayLength',
246 'name' => 'iSortCol_0',
248 'default' => 'sort_name',
250 'sortOrder' => array(
251 'name' => 'sSortDir_0',
256 foreach ($dataTableParams as $pName => $pValues) {
257 $
$pName = $pValues['default'];
258 if (!empty($_POST[$pValues['name']])) {
259 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
260 if ($pName == 'sort') { $
$pName = $selectorCols[$
$pName];
265 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params);
266 $query = new CRM_Contact_BAO_Query($queryParams,
267 NULL, NULL, FALSE, FALSE,
268 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
272 //get the voter clause to restrict and validate search.
273 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
275 $searchCount = $query->searchQuery(0, 0, NULL,
279 CRM_Utils_Array
::value('whereClause', $voterClause),
281 CRM_Utils_Array
::value('fromClause', $voterClause)
284 $iTotal = $searchCount;
286 $selectorCols = array(
287 'contact_type', 'sort_name', 'street_address',
288 'street_name', 'street_number', 'street_unit',
291 $extraVoterColName = 'is_interview_conducted';
292 if ($params['campaign_search_voter_for'] == 'reserve') {
293 $extraVoterColName = 'reserve_voter';
296 if ($searchCount > 0) {
297 if ($searchCount < $offset) {
301 $config = CRM_Core_Config
::singleton();
303 // get the result of the search
304 $result = $query->searchQuery($offset, $rowCount, $sort,
308 CRM_Utils_Array
::value('whereClause', $voterClause),
310 CRM_Utils_Array
::value('fromClause', $voterClause)
312 while ($result->fetch()) {
313 $contactID = $result->contact_id
;
314 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
,
319 $searchRows[$contactID] = array('id' => $contactID);
320 foreach ($selectorCols as $col) {
321 $val = $result->$col;
322 if ($col == 'contact_type') {
325 $searchRows[$contactID][$col] = $val;
327 if ($searchVoterFor == 'reserve') {
328 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />';
329 $msg = ts('Respondent Reserved.');
330 $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
332 elseif ($searchVoterFor == 'gotv') {
333 $surveyActId = $result->survey_activity_id
;
334 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
335 $msg = ts('Vote Recorded.');
336 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
339 $surveyActId = $result->survey_activity_id
;
340 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
341 $msg = ts('Vote Recorded.');
342 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
344 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
348 $selectorElements = array_merge($selectorCols, array($extraVoterColName));
350 $iFilteredTotal = $iTotal;
352 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
353 CRM_Utils_System
::civiExit();
356 public function processVoterData() {
358 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
359 if ($operation == 'release') {
360 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
361 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
363 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
372 elseif ($operation == 'reserve') {
374 $createActivity = TRUE;
375 if (!empty($_POST['activity_id'])) {
376 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
378 $createActivity = FALSE;
379 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
384 if ($activityUpdated) {
389 if ($createActivity) {
394 'assignee_contact_id',
396 $activityParams = array();
397 foreach ($ids as $id) {
398 $val = CRM_Utils_Array
::value($id, $_POST);
400 $createActivity = FALSE;
403 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
406 if ($createActivity) {
407 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
408 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
409 $scheduledStatusId = array_search('Scheduled', $activityStatus);
411 $surveyValues = array();
412 $surveyParams = array('id' => $activityParams['source_record_id']);
413 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
416 array('title', 'activity_type_id', 'campaign_id')
419 $activityTypeId = $surveyValues['activity_type_id'];
421 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
423 $surveytitle = $surveyValues['title'];
426 $subject = ts('%1', array(1 => $surveytitle)) . ' - ' . ts('Respondent Reservation');
427 $activityParams['subject'] = $subject;
428 $activityParams['status_id'] = $scheduledStatusId;
429 $activityParams['skipRecentView'] = 1;
430 $activityParams['activity_date_time'] = date('YmdHis');
431 $activityParams['activity_type_id'] = $activityTypeId;
432 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
434 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
440 //delete reserved activity for given voter.
441 $voterIds = array($activityParams['target_contact_id']);
442 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
444 $activityParams['source_contact_id'],
445 array($scheduledStatusId)
447 foreach ($activities as $voterId => $values) {
448 $activityId = CRM_Utils_Array
::value('activity_id', $values);
449 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
450 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
462 elseif ($operation == 'gotv') {
463 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
464 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
472 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
481 CRM_Utils_JSON
::output(array('status' => $status));
484 public function allActiveCampaigns() {
485 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
486 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
490 'title' => ts('- select -'),
492 foreach ($campaigns as $value => $title) {
494 if (!array_key_exists($value, $currentCampaigns)) {
495 $class = 'status-past';
504 if (count($options) > 1) {
510 'campaigns' => $options,
513 CRM_Utils_JSON
::output($results);
516 public function campaignGroups() {
517 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
518 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
520 $campGroups = array();
522 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
524 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
528 //CRM-7406 --If there is no campaign or no group associated with
529 //campaign of given survey, lets allow to search across all groups.
530 if (empty($campGroups)) {
531 $campGroups = CRM_Core_PseudoConstant
::group();
536 'title' => ts('- select -'),
538 foreach ($campGroups as $grpId => $title) {
545 'status' => 'success',
549 CRM_Utils_JSON
::output($results);
553 * Retrieve campaigns as for campaign dashboard.
556 public function campaignList() {
557 //get the search criteria params.
558 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
560 $params = $searchRows = array();
561 foreach ($searchParams as $param) {
562 if (!empty($_POST[$param])) {
563 $params[$param] = $_POST[$param];
567 //this is sequence columns on datatable.
568 $selectorCols = array(
584 // get the data table params.
585 $dataTableParams = array(
592 'name' => 'iDisplayStart',
597 'name' => 'iDisplayLength',
602 'name' => 'iSortCol_0',
604 'default' => 'start_date',
606 'sortOrder' => array(
607 'name' => 'sSortDir_0',
612 foreach ($dataTableParams as $pName => $pValues) {
613 $
$pName = $pValues['default'];
614 if (!empty($_POST[$pValues['name']])) {
615 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
616 if ($pName == 'sort') {
617 $
$pName = $selectorCols[$
$pName];
622 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
623 $params[$sortParam] = $
$sortParam;
626 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
627 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
628 $iTotal = $searchCount;
630 if ($searchCount > 0) {
631 if ($searchCount < $offset) {
634 foreach ($campaigns as $campaignID => $values) {
635 foreach ($selectorCols as $col) {
636 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
641 $selectorElements = $selectorCols;
643 $iFilteredTotal = $iTotal;
645 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
646 CRM_Utils_System
::civiExit();
650 * Retrieve survey for survey dashboard.
653 public function surveyList() {
654 //get the search criteria params.
655 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
657 $params = $searchRows = array();
658 foreach ($searchParams as $param) {
659 if (!empty($_POST[$param])) {
660 $params[$param] = $_POST[$param];
664 //this is sequence columns on datatable.
665 $selectorCols = array(
673 'default_number_of_contacts',
674 'max_number_of_contacts',
683 // get the data table params.
684 $dataTableParams = array(
691 'name' => 'iDisplayStart',
696 'name' => 'iDisplayLength',
701 'name' => 'iSortCol_0',
703 'default' => 'created_date',
705 'sortOrder' => array(
706 'name' => 'sSortDir_0',
711 foreach ($dataTableParams as $pName => $pValues) {
712 $
$pName = $pValues['default'];
713 if (!empty($_POST[$pValues['name']])) {
714 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
715 if ($pName == 'sort') {
716 $
$pName = $selectorCols[$
$pName];
721 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
722 $params[$sortParam] = $
$sortParam;
725 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
726 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
727 $iTotal = $searchCount;
729 if ($searchCount > 0) {
730 if ($searchCount < $offset) {
733 foreach ($surveys as $surveyID => $values) {
734 foreach ($selectorCols as $col) {
735 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
740 $selectorElements = $selectorCols;
742 $iFilteredTotal = $iTotal;
744 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
745 CRM_Utils_System
::civiExit();
749 * Retrieve petitions for petition dashboard.
752 public function petitionList() {
753 //get the search criteria params.
754 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
756 $params = $searchRows = array();
757 foreach ($searchParams as $param) {
758 if (!empty($_POST[$param])) {
759 $params[$param] = $_POST[$param];
763 //this is sequence columns on datatable.
764 $selectorCols = array(
777 // get the data table params.
778 $dataTableParams = array(
785 'name' => 'iDisplayStart',
790 'name' => 'iDisplayLength',
795 'name' => 'iSortCol_0',
797 'default' => 'created_date',
799 'sortOrder' => array(
800 'name' => 'sSortDir_0',
805 foreach ($dataTableParams as $pName => $pValues) {
806 $
$pName = $pValues['default'];
807 if (!empty($_POST[$pValues['name']])) {
808 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
809 if ($pName == 'sort') {
810 $
$pName = $selectorCols[$
$pName];
815 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
816 $params[$sortParam] = $
$sortParam;
819 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
820 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
821 $iTotal = $searchCount;
823 if ($searchCount > 0) {
824 if ($searchCount < $offset) {
827 foreach ($petitions as $petitionID => $values) {
828 foreach ($selectorCols as $col) {
829 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
834 $selectorElements = $selectorCols;
836 $iFilteredTotal = $iTotal;
838 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
839 CRM_Utils_System
::civiExit();