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 echo json_encode($result);
96 CRM_Utils_System
::civiExit();
99 //process the response/interview data.
100 $activityId = CRM_Campaign_Form_Task_Interview
::registerInterview($params);
102 $result['status'] = 'success';
105 echo json_encode($result);
107 CRM_Utils_System
::civiExit();
110 static function loadOptionGroupDetails() {
112 $id = CRM_Utils_Array
::value('option_group_id', $_POST);
117 $groupParams['id'] = $id;
118 CRM_Core_OptionValue
::getValues($groupParams, $opValues);
121 $surveyId = CRM_Utils_Array
::value('survey_id', $_POST);
123 $survey = new CRM_Campaign_DAO_Survey();
124 $survey->id
= $surveyId;
125 $survey->result_id
= $id;
126 if ($survey->find(TRUE)) {
127 if ($survey->recontact_interval
) {
128 $recontactInterval = unserialize($survey->recontact_interval
);
129 foreach ($opValues as $opValId => $opVal) {
130 if (is_numeric($recontactInterval[$opVal['label']])) {
131 $opValues[$opValId]['interval'] = $recontactInterval[$opVal['label']];
138 if (!empty($opValues)) {
144 'result' => $opValues,
147 echo json_encode($result);
148 CRM_Utils_System
::civiExit();
151 function voterList() {
152 //get the search criteria params.
153 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
155 $params = $searchRows = array();
156 foreach ($searchParams as $param) {
157 if (!empty($_POST[$param])) {
158 $params[$param] = $_POST[$param];
162 //format multi-select group and contact types.
164 'group', 'contact_type') as $param) {
165 $paramValue = CRM_Utils_Array
::value($param, $params);
167 unset($params[$param]);
168 $paramValue = explode(',', $paramValue);
169 foreach ($paramValue as $key => $value) {
170 $params[$param][$value] = 1;
175 $voterClauseParams = array();
177 'campaign_survey_id', 'survey_interviewer_id', 'campaign_search_voter_for') as $fld) {
178 $voterClauseParams[$fld] = CRM_Utils_Array
::value($fld, $params);
181 $interviewerId = $surveyTypeId = $surveyId = NULL;
182 $searchVoterFor = $params['campaign_search_voter_for'];
183 if ($searchVoterFor == 'reserve') {
184 if (!empty($params['campaign_survey_id'])) {
185 $survey = new CRM_Campaign_DAO_Survey();
186 $survey->id
= $surveyId = $params['campaign_survey_id'];
187 $survey->selectAdd('campaign_id, activity_type_id');
189 $campaignId = $survey->campaign_id
;
190 $surveyTypeId = $survey->activity_type_id
;
192 //allow voter search in sub-part of given constituents,
193 //but make sure in case user does not select any group.
194 //get all associated campaign groups in where filter, CRM-7406
195 $groups = CRM_Utils_Array
::value('group', $params);
196 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
197 $campaignGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
198 foreach ($campaignGroups as $id => $group) $params['group'][$id] = 1;
201 //apply filter of survey contact type for search.
202 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType($surveyId);
204 $params['contact_type'][$contactType] = 1;
207 unset($params['campaign_survey_id']);
209 unset($params['survey_interviewer_id']);
212 //get the survey status in where clause.
213 $scheduledStatusId = array_search('Scheduled', CRM_Core_PseudoConstant
::activityStatus('name'));
214 if ($scheduledStatusId) {
215 $params['survey_status_id'] = $scheduledStatusId;
217 //BAO/Query knows reserve/release/interview processes.
218 if ($params['campaign_search_voter_for'] == 'gotv') {
219 $params['campaign_search_voter_for'] = 'release';
223 $selectorCols = array(
231 // get the data table params.
232 $dataTableParams = array(
233 'sEcho' => array('name' => 'sEcho',
238 'name' => 'iDisplayStart',
243 'name' => 'iDisplayLength',
248 'name' => 'iSortCol_0',
250 'default' => 'sort_name',
252 'sortOrder' => array(
253 'name' => 'sSortDir_0',
258 foreach ($dataTableParams as $pName => $pValues) {
259 $
$pName = $pValues['default'];
260 if (!empty($_POST[$pValues['name']])) {
261 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
262 if ($pName == 'sort')$
$pName = $selectorCols[$
$pName];
266 $queryParams = CRM_Contact_BAO_Query
::convertFormValues($params);
267 $query = new CRM_Contact_BAO_Query($queryParams,
268 NULL, NULL, FALSE, FALSE,
269 CRM_Contact_BAO_Query
::MODE_CAMPAIGN
,
273 //get the voter clause to restrict and validate search.
274 $voterClause = CRM_Campaign_BAO_Query
::voterClause($voterClauseParams);
276 $searchCount = $query->searchQuery(0, 0, NULL,
280 CRM_Utils_Array
::value('whereClause', $voterClause),
282 CRM_Utils_Array
::value('fromClause', $voterClause)
285 $iTotal = $searchCount;
287 $selectorCols = array(
288 'contact_type', 'sort_name', 'street_address',
289 'street_name', 'street_number', 'street_unit',
292 $extraVoterColName = 'is_interview_conducted';
293 if ($params['campaign_search_voter_for'] == 'reserve') {
294 $extraVoterColName = 'reserve_voter';
297 if ($searchCount > 0) {
298 if ($searchCount < $offset) {
302 $config = CRM_Core_Config
::singleton();
304 // get the result of the search
305 $result = $query->searchQuery($offset, $rowCount, $sort,
309 CRM_Utils_Array
::value('whereClause', $voterClause),
311 CRM_Utils_Array
::value('fromClause', $voterClause)
313 while ($result->fetch()) {
314 $contactID = $result->contact_id
;
315 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
316 $result->contact_sub_type
: $result->contact_type
,
321 $searchRows[$contactID] = array('id' => $contactID);
322 foreach ($selectorCols as $col) {
323 $val = $result->$col;
324 if ($col == 'contact_type') {
327 $searchRows[$contactID][$col] = $val;
329 if ($searchVoterFor == 'reserve') {
330 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $contactID . ']" name="survey_activity[' . $contactID . ']" value=' . $contactID . ' onClick="processVoterData( this, \'reserve\' );" />';
331 $msg = ts('Respondent Reserved.');
332 $voterExtraColHtml .= " <span id='success_msg_{$contactID}' class='ok' style='display:none;'>$msg</span>";
334 elseif ($searchVoterFor == 'gotv') {
335 $surveyActId = $result->survey_activity_id
;
336 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'gotv\' );" />';
337 $msg = ts('Vote Recorded');
338 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
341 $surveyActId = $result->survey_activity_id
;
342 $voterExtraColHtml = '<input type="checkbox" id="survey_activity[' . $surveyActId . ']" name="survey_activity[' . $surveyActId . ']" value=' . $surveyActId . ' onClick="processVoterData( this, \'release\' );" />';
343 $msg = ts('Vote Recorded');
344 $voterExtraColHtml .= " <span id='success_msg_{$surveyActId}' class='ok' style='display:none;'>$msg</span>";
346 $searchRows[$contactID][$extraVoterColName] = $voterExtraColHtml;
350 $selectorElements = array_merge($selectorCols, array($extraVoterColName));
352 $iFilteredTotal = $iTotal;
354 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
355 CRM_Utils_System
::civiExit();
358 function processVoterData() {
360 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
361 if ($operation == 'release') {
362 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
363 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
365 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
374 elseif ($operation == 'reserve') {
376 $createActivity = TRUE;
377 if (!empty($_POST['activity_id'])) {
378 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
380 $createActivity = FALSE;
381 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
386 if ($activityUpdated) {
391 if ($createActivity) {
396 'assignee_contact_id',
398 $activityParams = array();
399 foreach ($ids as $id) {
400 $val = CRM_Utils_Array
::value($id, $_POST);
402 $createActivity = FALSE;
405 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
408 if ($createActivity) {
409 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
410 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
411 $scheduledStatusId = array_search('Scheduled', $activityStatus);
413 $surveyValues = array();
414 $surveyParams = array('id' => $activityParams['source_record_id']);
415 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
418 array('title', 'activity_type_id', 'campaign_id')
421 $activityTypeId = $surveyValues['activity_type_id'];
423 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
425 $surveytitle = $surveyValues['title'];
428 $subject = ts('%1', array(1 => $surveytitle)) . ' - ' . ts('Respondent Reservation');
429 $activityParams['subject'] = $subject;
430 $activityParams['status_id'] = $scheduledStatusId;
431 $activityParams['skipRecentView'] = 1;
432 $activityParams['activity_date_time'] = date('YmdHis');
433 $activityParams['activity_type_id'] = $activityTypeId;
434 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
436 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
442 //delete reserved activity for given voter.
443 $voterIds = array($activityParams['target_contact_id']);
444 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
446 $activityParams['source_contact_id'],
447 array($scheduledStatusId)
449 foreach ($activities as $voterId => $values) {
450 $activityId = CRM_Utils_Array
::value('activity_id', $values);
451 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
452 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
464 elseif ($operation == 'gotv') {
465 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
466 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
474 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
483 echo json_encode(array('status' => $status));
484 CRM_Utils_System
::civiExit();
487 function allActiveCampaigns() {
488 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
489 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
492 'title' => ts('- select -'),
494 foreach ($campaigns as $value => $title) {
496 if (!array_key_exists($value, $currentCampaigns)) {
497 $class = 'status-past';
506 if (count($options) > 1) {
512 'campaigns' => $options,
515 echo json_encode($results);
517 CRM_Utils_System
::civiExit();
520 function campaignGroups() {
521 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
522 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
524 $campGroups = array();
526 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
528 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
532 //CRM-7406 --If there is no campaign or no group associated with
533 //campaign of given survey, lets allow to search across all groups.
534 if (empty($campGroups)) {
535 $campGroups = CRM_Core_PseudoConstant
::group();
539 'title' => ts('- select -'),
541 foreach ($campGroups as $grpId => $title) {
548 'status' => 'success',
552 echo json_encode($results);
554 CRM_Utils_System
::civiExit();
558 * Retrieve campaigns as for campaign dashboard.
561 function campaignList() {
562 //get the search criteria params.
563 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
565 $params = $searchRows = array();
566 foreach ($searchParams as $param) {
567 if (!empty($_POST[$param])) {
568 $params[$param] = $_POST[$param];
572 //this is sequence columns on datatable.
573 $selectorCols = array(
589 // get the data table params.
590 $dataTableParams = array(
591 'sEcho' => array('name' => 'sEcho',
596 'name' => 'iDisplayStart',
601 'name' => 'iDisplayLength',
606 'name' => 'iSortCol_0',
608 'default' => 'start_date',
610 'sortOrder' => array(
611 'name' => 'sSortDir_0',
616 foreach ($dataTableParams as $pName => $pValues) {
617 $
$pName = $pValues['default'];
618 if (!empty($_POST[$pValues['name']])) {
619 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
620 if ($pName == 'sort') {
621 $
$pName = $selectorCols[$
$pName];
626 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
627 $params[$sortParam] = $
$sortParam;
630 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
631 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
632 $iTotal = $searchCount;
634 if ($searchCount > 0) {
635 if ($searchCount < $offset) {
638 foreach ($campaigns as $campaignID => $values) {
639 foreach ($selectorCols as $col) {
640 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
645 $selectorElements = $selectorCols;
647 $iFilteredTotal = $iTotal;
649 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
650 CRM_Utils_System
::civiExit();
654 * Retrieve survey for survey dashboard.
657 function surveyList() {
658 //get the search criteria params.
659 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
661 $params = $searchRows = array();
662 foreach ($searchParams as $param) {
663 if (!empty($_POST[$param])) {
664 $params[$param] = $_POST[$param];
668 //this is sequence columns on datatable.
669 $selectorCols = array(
677 'default_number_of_contacts',
678 'max_number_of_contacts',
687 // get the data table params.
688 $dataTableParams = array(
689 'sEcho' => array('name' => 'sEcho',
694 'name' => 'iDisplayStart',
699 'name' => 'iDisplayLength',
704 'name' => 'iSortCol_0',
706 'default' => 'created_date',
708 'sortOrder' => array(
709 'name' => 'sSortDir_0',
714 foreach ($dataTableParams as $pName => $pValues) {
715 $
$pName = $pValues['default'];
716 if (!empty($_POST[$pValues['name']])) {
717 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
718 if ($pName == 'sort') {
719 $
$pName = $selectorCols[$
$pName];
724 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
725 $params[$sortParam] = $
$sortParam;
728 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
729 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
730 $iTotal = $searchCount;
732 if ($searchCount > 0) {
733 if ($searchCount < $offset) {
736 foreach ($surveys as $surveyID => $values) {
737 foreach ($selectorCols as $col) {
738 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
743 $selectorElements = $selectorCols;
745 $iFilteredTotal = $iTotal;
747 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
748 CRM_Utils_System
::civiExit();
752 * Retrieve petitions for petition dashboard.
755 function petitionList() {
756 //get the search criteria params.
757 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
759 $params = $searchRows = array();
760 foreach ($searchParams as $param) {
761 if (!empty($_POST[$param])) {
762 $params[$param] = $_POST[$param];
766 //this is sequence columns on datatable.
767 $selectorCols = array(
780 // get the data table params.
781 $dataTableParams = array(
782 'sEcho' => array('name' => 'sEcho',
787 'name' => 'iDisplayStart',
792 'name' => 'iDisplayLength',
797 'name' => 'iSortCol_0',
799 'default' => 'created_date',
801 'sortOrder' => array(
802 'name' => 'sSortDir_0',
807 foreach ($dataTableParams as $pName => $pValues) {
808 $
$pName = $pValues['default'];
809 if (!empty($_POST[$pValues['name']])) {
810 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
811 if ($pName == 'sort') {
812 $
$pName = $selectorCols[$
$pName];
817 'sort', 'offset', 'rowCount', 'sortOrder') as $sortParam) {
818 $params[$sortParam] = $
$sortParam;
821 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
822 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
823 $iTotal = $searchCount;
825 if ($searchCount > 0) {
826 if ($searchCount < $offset) {
829 foreach ($petitions as $petitionID => $values) {
830 foreach ($selectorCols as $col) {
831 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
836 $selectorElements = $selectorCols;
838 $iFilteredTotal = $iTotal;
840 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
841 CRM_Utils_System
::civiExit();