3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 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 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;
197 //apply filter of survey contact type for search.
198 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
200 $params['contact_type'][$contactType] = 1;
203 unset($params['campaign_survey_id']);
205 unset($params['survey_interviewer_id']);
208 //get the survey status in where clause.
209 $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant
::activityStatus('name'));
210 if ($scheduledStatusId) {
211 $params['survey_status_id'] = $scheduledStatusId;
213 //BAO/Query knows reserve/release/interview processes.
214 if ($params['campaign_search_voter_for'] == 'gotv') {
215 $params['campaign_search_voter_for'] = 'release';
219 $selectorCols = array(
227 // get the data table params.
228 $dataTableParams = array(
229 'sEcho' => array('name' => 'sEcho',
234 'name' => 'iDisplayStart',
239 'name' => 'iDisplayLength',
244 'name' => 'iSortCol_0',
246 'default' => 'sort_name',
248 'sortOrder' => array(
249 'name' => 'sSortDir_0',
254 foreach ($dataTableParams as $pName => $pValues) {
255 $
$pName = $pValues['default'];
256 if (!empty($_POST[$pValues['name']])) {
257 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
258 if ($pName == 'sort')$
$pName = $selectorCols[$
$pName];
262 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params);
263 $query = new CRM_Contact_BAO_Query($queryParams,
264 NULL, NULL, FALSE, FALSE,
265 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
269 //get the voter clause to restrict and validate search.
270 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
272 $searchCount = $query->searchQuery(0, 0, NULL,
276 CRM_Utils_Array
::value('whereClause', $voterClause),
278 CRM_Utils_Array
::value('fromClause', $voterClause)
281 $iTotal = $searchCount;
283 $selectorCols = array(
284 'contact_type', 'sort_name', 'street_address',
285 'street_name', 'street_number', 'street_unit',
288 $extraVoterColName = 'is_interview_conducted';
289 if ($params['campaign_search_voter_for'] == 'reserve') {
290 $extraVoterColName = 'reserve_voter';
293 if ($searchCount > 0) {
294 if ($searchCount < $offset) {
298 $config = CRM_Core_Config
::singleton();
300 // get the result of the search
301 $result = $query->searchQuery($offset, $rowCount, $sort,
305 CRM_Utils_Array
::value('whereClause', $voterClause),
307 CRM_Utils_Array
::value('fromClause', $voterClause)
309 while ($result->fetch()) {
310 $contactID = $result->contact_id
;
311 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
312 $result->contact_sub_type
: $result->contact_type
,
317 $searchRows[$contactID] = array('id' => $contactID);
318 foreach ($selectorCols as $col) {
319 $val = $result->$col;
320 if ($col == 'contact_type') {
323 $searchRows[$contactID][$col] = $val;
325 if ($searchVoterFor == 'reserve') {
326 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />';
327 $msg = ts('Respondent Reserved.');
328 $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
330 elseif ($searchVoterFor == 'gotv') {
331 $surveyActId = $result->survey_activity_id
;
332 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
333 $msg = ts('Vote Recorded');
334 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
337 $surveyActId = $result->survey_activity_id
;
338 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
339 $msg = ts('Vote Recorded');
340 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
342 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
346 $selectorElements = array_merge($selectorCols, array($extraVoterColName));
348 $iFilteredTotal = $iTotal;
350 header('Content-Type: application/json');
351 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
352 CRM_Utils_System
::civiExit();
355 function processVoterData() {
357 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
358 if ($operation == 'release') {
359 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
360 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
362 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
371 elseif ($operation == 'reserve') {
373 $createActivity = TRUE;
374 if (!empty($_POST['activity_id'])) {
375 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
377 $createActivity = FALSE;
378 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
383 if ($activityUpdated) {
388 if ($createActivity) {
393 'assignee_contact_id',
395 $activityParams = array();
396 foreach ($ids as $id) {
397 $val = CRM_Utils_Array
::value($id, $_POST);
399 $createActivity = FALSE;
402 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
405 if ($createActivity) {
406 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
407 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
408 $scheduledStatusId = array_search('Scheduled', $activityStatus);
410 $surveyValues = array();
411 $surveyParams = array('id' => $activityParams['source_record_id']);
412 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
415 array('title', 'activity_type_id', 'campaign_id')
418 $activityTypeId = $surveyValues['activity_type_id'];
420 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
422 $surveytitle = $surveyValues['title'];
425 $subject = ts('%1', array(1 => $surveytitle)) . ' - ' . ts('Respondent Reservation');
426 $activityParams['subject'] = $subject;
427 $activityParams['status_id'] = $scheduledStatusId;
428 $activityParams['skipRecentView'] = 1;
429 $activityParams['activity_date_time'] = date('YmdHis');
430 $activityParams['activity_type_id'] = $activityTypeId;
431 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
433 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
439 //delete reserved activity for given voter.
440 $voterIds = array($activityParams['target_contact_id']);
441 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
443 $activityParams['source_contact_id'],
444 array($scheduledStatusId)
446 foreach ($activities as $voterId => $values) {
447 $activityId = CRM_Utils_Array
::value('activity_id', $values);
448 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
449 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
461 elseif ($operation == 'gotv') {
462 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
463 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
471 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
480 CRM_Utils_JSON
::output(array('status' => $status));
483 function allActiveCampaigns() {
484 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
485 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
488 'title' => ts('- select -'),
490 foreach ($campaigns as $value => $title) {
492 if (!array_key_exists($value, $currentCampaigns)) {
493 $class = 'status-past';
502 if (count($options) > 1) {
508 'campaigns' => $options,
511 CRM_Utils_JSON
::output($results);
514 function campaignGroups() {
515 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
516 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
518 $campGroups = array();
520 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
522 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
526 //CRM-7406 --If there is no campaign or no group associated with
527 //campaign of given survey, lets allow to search across all groups.
528 if (empty($campGroups)) {
529 $campGroups = CRM_Core_PseudoConstant
::group();
533 'title' => ts('- select -'),
535 foreach ($campGroups as $grpId => $title) {
542 'status' => 'success',
546 CRM_Utils_JSON
::output($results);
550 * Retrieve campaigns as for campaign dashboard.
553 function campaignList() {
554 //get the search criteria params.
555 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
557 $params = $searchRows = array();
558 foreach ($searchParams as $param) {
559 if (!empty($_POST[$param])) {
560 $params[$param] = $_POST[$param];
564 //this is sequence columns on datatable.
565 $selectorCols = array(
581 // get the data table params.
582 $dataTableParams = array(
583 'sEcho' => array('name' => 'sEcho',
588 'name' => 'iDisplayStart',
593 'name' => 'iDisplayLength',
598 'name' => 'iSortCol_0',
600 'default' => 'start_date',
602 'sortOrder' => array(
603 'name' => 'sSortDir_0',
608 foreach ($dataTableParams as $pName => $pValues) {
609 $
$pName = $pValues['default'];
610 if (!empty($_POST[$pValues['name']])) {
611 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
612 if ($pName == 'sort') {
613 $
$pName = $selectorCols[$
$pName];
618 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
619 $params[$sortParam] = $
$sortParam;
622 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
623 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
624 $iTotal = $searchCount;
626 if ($searchCount > 0) {
627 if ($searchCount < $offset) {
630 foreach ($campaigns as $campaignID => $values) {
631 foreach ($selectorCols as $col) {
632 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
637 $selectorElements = $selectorCols;
639 $iFilteredTotal = $iTotal;
641 header('Content-Type: application/json');
642 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
643 CRM_Utils_System
::civiExit();
647 * Retrieve survey for survey dashboard.
650 function surveyList() {
651 //get the search criteria params.
652 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
654 $params = $searchRows = array();
655 foreach ($searchParams as $param) {
656 if (!empty($_POST[$param])) {
657 $params[$param] = $_POST[$param];
661 //this is sequence columns on datatable.
662 $selectorCols = array(
670 'default_number_of_contacts',
671 'max_number_of_contacts',
680 // get the data table params.
681 $dataTableParams = array(
682 'sEcho' => array('name' => 'sEcho',
687 'name' => 'iDisplayStart',
692 'name' => 'iDisplayLength',
697 'name' => 'iSortCol_0',
699 'default' => 'created_date',
701 'sortOrder' => array(
702 'name' => 'sSortDir_0',
707 foreach ($dataTableParams as $pName => $pValues) {
708 $
$pName = $pValues['default'];
709 if (!empty($_POST[$pValues['name']])) {
710 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
711 if ($pName == 'sort') {
712 $
$pName = $selectorCols[$
$pName];
717 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
718 $params[$sortParam] = $
$sortParam;
721 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
722 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
723 $iTotal = $searchCount;
725 if ($searchCount > 0) {
726 if ($searchCount < $offset) {
729 foreach ($surveys as $surveyID => $values) {
730 foreach ($selectorCols as $col) {
731 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
736 $selectorElements = $selectorCols;
738 $iFilteredTotal = $iTotal;
740 header('Content-Type: application/json');
741 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
742 CRM_Utils_System
::civiExit();
746 * Retrieve petitions for petition dashboard.
749 function petitionList() {
750 //get the search criteria params.
751 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
753 $params = $searchRows = array();
754 foreach ($searchParams as $param) {
755 if (!empty($_POST[$param])) {
756 $params[$param] = $_POST[$param];
760 //this is sequence columns on datatable.
761 $selectorCols = array(
774 // get the data table params.
775 $dataTableParams = array(
776 'sEcho' => array('name' => 'sEcho',
781 'name' => 'iDisplayStart',
786 'name' => 'iDisplayLength',
791 'name' => 'iSortCol_0',
793 'default' => 'created_date',
795 'sortOrder' => array(
796 'name' => 'sSortDir_0',
801 foreach ($dataTableParams as $pName => $pValues) {
802 $
$pName = $pValues['default'];
803 if (!empty($_POST[$pValues['name']])) {
804 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
805 if ($pName == 'sort') {
806 $
$pName = $selectorCols[$
$pName];
811 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
812 $params[$sortParam] = $
$sortParam;
815 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
816 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
817 $iTotal = $searchCount;
819 if ($searchCount > 0) {
820 if ($searchCount < $offset) {
823 foreach ($petitions as $petitionID => $values) {
824 foreach ($selectorCols as $col) {
825 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
830 $selectorElements = $selectorCols;
832 $iFilteredTotal = $iTotal;
834 header('Content-Type: application/json');
835 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
836 CRM_Utils_System
::civiExit();