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 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
351 CRM_Utils_System
::civiExit();
354 function processVoterData() {
356 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
357 if ($operation == 'release') {
358 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
359 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
361 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
370 elseif ($operation == 'reserve') {
372 $createActivity = TRUE;
373 if (!empty($_POST['activity_id'])) {
374 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
376 $createActivity = FALSE;
377 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
382 if ($activityUpdated) {
387 if ($createActivity) {
392 'assignee_contact_id',
394 $activityParams = array();
395 foreach ($ids as $id) {
396 $val = CRM_Utils_Array
::value($id, $_POST);
398 $createActivity = FALSE;
401 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
404 if ($createActivity) {
405 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
406 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
407 $scheduledStatusId = array_search('Scheduled', $activityStatus);
409 $surveyValues = array();
410 $surveyParams = array('id' => $activityParams['source_record_id']);
411 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
414 array('title', 'activity_type_id', 'campaign_id')
417 $activityTypeId = $surveyValues['activity_type_id'];
419 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
421 $surveytitle = $surveyValues['title'];
424 $subject = ts('%1', array(1 => $surveytitle)) . ' - ' . ts('Respondent Reservation');
425 $activityParams['subject'] = $subject;
426 $activityParams['status_id'] = $scheduledStatusId;
427 $activityParams['skipRecentView'] = 1;
428 $activityParams['activity_date_time'] = date('YmdHis');
429 $activityParams['activity_type_id'] = $activityTypeId;
430 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
432 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
438 //delete reserved activity for given voter.
439 $voterIds = array($activityParams['target_contact_id']);
440 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
442 $activityParams['source_contact_id'],
443 array($scheduledStatusId)
445 foreach ($activities as $voterId => $values) {
446 $activityId = CRM_Utils_Array
::value('activity_id', $values);
447 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
448 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
460 elseif ($operation == 'gotv') {
461 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
462 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
470 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
479 CRM_Utils_JSON
::output(array('status' => $status));
482 function allActiveCampaigns() {
483 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
484 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
487 'title' => ts('- select -'),
489 foreach ($campaigns as $value => $title) {
491 if (!array_key_exists($value, $currentCampaigns)) {
492 $class = 'status-past';
501 if (count($options) > 1) {
507 'campaigns' => $options,
510 CRM_Utils_JSON
::output($results);
513 function campaignGroups() {
514 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
515 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
517 $campGroups = array();
519 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
521 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
525 //CRM-7406 --If there is no campaign or no group associated with
526 //campaign of given survey, lets allow to search across all groups.
527 if (empty($campGroups)) {
528 $campGroups = CRM_Core_PseudoConstant
::group();
532 'title' => ts('- select -'),
534 foreach ($campGroups as $grpId => $title) {
541 'status' => 'success',
545 CRM_Utils_JSON
::output($results);
549 * Retrieve campaigns as for campaign dashboard.
552 function campaignList() {
553 //get the search criteria params.
554 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
556 $params = $searchRows = array();
557 foreach ($searchParams as $param) {
558 if (!empty($_POST[$param])) {
559 $params[$param] = $_POST[$param];
563 //this is sequence columns on datatable.
564 $selectorCols = array(
580 // get the data table params.
581 $dataTableParams = array(
582 'sEcho' => array('name' => 'sEcho',
587 'name' => 'iDisplayStart',
592 'name' => 'iDisplayLength',
597 'name' => 'iSortCol_0',
599 'default' => 'start_date',
601 'sortOrder' => array(
602 'name' => 'sSortDir_0',
607 foreach ($dataTableParams as $pName => $pValues) {
608 $
$pName = $pValues['default'];
609 if (!empty($_POST[$pValues['name']])) {
610 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
611 if ($pName == 'sort') {
612 $
$pName = $selectorCols[$
$pName];
617 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
618 $params[$sortParam] = $
$sortParam;
621 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
622 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
623 $iTotal = $searchCount;
625 if ($searchCount > 0) {
626 if ($searchCount < $offset) {
629 foreach ($campaigns as $campaignID => $values) {
630 foreach ($selectorCols as $col) {
631 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
636 $selectorElements = $selectorCols;
638 $iFilteredTotal = $iTotal;
640 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
641 CRM_Utils_System
::civiExit();
645 * Retrieve survey for survey dashboard.
648 function surveyList() {
649 //get the search criteria params.
650 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
652 $params = $searchRows = array();
653 foreach ($searchParams as $param) {
654 if (!empty($_POST[$param])) {
655 $params[$param] = $_POST[$param];
659 //this is sequence columns on datatable.
660 $selectorCols = array(
668 'default_number_of_contacts',
669 'max_number_of_contacts',
678 // get the data table params.
679 $dataTableParams = array(
680 'sEcho' => array('name' => 'sEcho',
685 'name' => 'iDisplayStart',
690 'name' => 'iDisplayLength',
695 'name' => 'iSortCol_0',
697 'default' => 'created_date',
699 'sortOrder' => array(
700 'name' => 'sSortDir_0',
705 foreach ($dataTableParams as $pName => $pValues) {
706 $
$pName = $pValues['default'];
707 if (!empty($_POST[$pValues['name']])) {
708 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
709 if ($pName == 'sort') {
710 $
$pName = $selectorCols[$
$pName];
715 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
716 $params[$sortParam] = $
$sortParam;
719 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
720 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
721 $iTotal = $searchCount;
723 if ($searchCount > 0) {
724 if ($searchCount < $offset) {
727 foreach ($surveys as $surveyID => $values) {
728 foreach ($selectorCols as $col) {
729 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
734 $selectorElements = $selectorCols;
736 $iFilteredTotal = $iTotal;
738 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
739 CRM_Utils_System
::civiExit();
743 * Retrieve petitions for petition dashboard.
746 function petitionList() {
747 //get the search criteria params.
748 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
750 $params = $searchRows = array();
751 foreach ($searchParams as $param) {
752 if (!empty($_POST[$param])) {
753 $params[$param] = $_POST[$param];
757 //this is sequence columns on datatable.
758 $selectorCols = array(
771 // get the data table params.
772 $dataTableParams = array(
773 'sEcho' => array('name' => 'sEcho',
778 'name' => 'iDisplayStart',
783 'name' => 'iDisplayLength',
788 'name' => 'iSortCol_0',
790 'default' => 'created_date',
792 'sortOrder' => array(
793 'name' => 'sSortDir_0',
798 foreach ($dataTableParams as $pName => $pValues) {
799 $
$pName = $pValues['default'];
800 if (!empty($_POST[$pValues['name']])) {
801 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
802 if ($pName == 'sort') {
803 $
$pName = $selectorCols[$
$pName];
808 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
809 $params[$sortParam] = $
$sortParam;
812 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
813 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
814 $iTotal = $searchCount;
816 if ($searchCount > 0) {
817 if ($searchCount < $offset) {
820 foreach ($petitions as $petitionID => $values) {
821 foreach ($selectorCols as $col) {
822 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
827 $selectorElements = $selectorCols;
829 $iFilteredTotal = $iTotal;
831 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
832 CRM_Utils_System
::civiExit();