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 header('Content-Type: application/json');
364 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
365 CRM_Utils_System
::civiExit();
368 public function processVoterData() {
370 $operation = CRM_Utils_Type
::escape($_POST['operation'], 'String');
371 if ($operation == 'release') {
372 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
373 $isDelete = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isDelete'], 'String'));
375 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
384 elseif ($operation == 'reserve') {
386 $createActivity = TRUE;
387 if (!empty($_POST['activity_id'])) {
388 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
390 $createActivity = FALSE;
391 $activityUpdated = CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
396 if ($activityUpdated) {
401 if ($createActivity) {
406 'assignee_contact_id',
408 $activityParams = array();
409 foreach ($ids as $id) {
410 $val = CRM_Utils_Array
::value($id, $_POST);
412 $createActivity = FALSE;
415 $activityParams[$id] = CRM_Utils_Type
::escape($val, 'Integer');
418 if ($createActivity) {
419 $isReserved = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['isReserved'], 'String'));
420 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
421 $scheduledStatusId = array_search('Scheduled', $activityStatus);
423 $surveyValues = array();
424 $surveyParams = array('id' => $activityParams['source_record_id']);
425 CRM_Core_DAO
::commonRetrieve('CRM_Campaign_DAO_Survey',
428 array('title', 'activity_type_id', 'campaign_id')
431 $activityTypeId = $surveyValues['activity_type_id'];
433 $surveytitle = CRM_Utils_Array
::value('surveyTitle', $_POST);
435 $surveytitle = $surveyValues['title'];
438 $subject = ts('%1', array(1 => $surveytitle)) . ' - ' . ts('Respondent Reservation');
439 $activityParams['subject'] = $subject;
440 $activityParams['status_id'] = $scheduledStatusId;
441 $activityParams['skipRecentView'] = 1;
442 $activityParams['activity_date_time'] = date('YmdHis');
443 $activityParams['activity_type_id'] = $activityTypeId;
444 $activityParams['campaign_id'] = isset($surveyValues['campaign_id']) ?
$surveyValues['campaign_id'] : NULL;
446 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
452 //delete reserved activity for given voter.
453 $voterIds = array($activityParams['target_contact_id']);
454 $activities = CRM_Campaign_BAO_Survey
::voterActivityDetails($activityParams['source_record_id'],
456 $activityParams['source_contact_id'],
457 array($scheduledStatusId)
459 foreach ($activities as $voterId => $values) {
460 $activityId = CRM_Utils_Array
::value('activity_id', $values);
461 if ($activityId && ($values['status_id'] == $scheduledStatusId)) {
462 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
474 elseif ($operation == 'gotv') {
475 $activityId = CRM_Utils_Type
::escape($_POST['activity_id'], 'Integer');
476 $hasVoted = CRM_Utils_String
::strtoboolstr(CRM_Utils_Type
::escape($_POST['hasVoted'], 'String'));
484 CRM_Core_DAO
::setFieldValue('CRM_Activity_DAO_Activity',
493 CRM_Utils_JSON
::output(array('status' => $status));
496 public function allActiveCampaigns() {
497 $currentCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns();
498 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
502 'title' => ts('- select -'),
505 foreach ($campaigns as $value => $title) {
507 if (!array_key_exists($value, $currentCampaigns)) {
508 $class = 'status-past';
517 if (count($options) > 1) {
523 'campaigns' => $options,
526 CRM_Utils_JSON
::output($results);
529 public function campaignGroups() {
530 $surveyId = CRM_Utils_Request
::retrieve('survey_id', 'Positive',
531 CRM_Core_DAO
::$_nullObject, FALSE, NULL, 'POST'
533 $campGroups = array();
535 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'campaign_id');
537 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
541 //CRM-7406 --If there is no campaign or no group associated with
542 //campaign of given survey, lets allow to search across all groups.
543 if (empty($campGroups)) {
544 $campGroups = CRM_Core_PseudoConstant
::group();
549 'title' => ts('- select -'),
552 foreach ($campGroups as $grpId => $title) {
559 'status' => 'success',
563 CRM_Utils_JSON
::output($results);
567 * Retrieve campaigns as for campaign dashboard.
570 public function campaignList() {
571 //get the search criteria params.
572 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
574 $params = $searchRows = array();
575 foreach ($searchParams as $param) {
576 if (!empty($_POST[$param])) {
577 $params[$param] = $_POST[$param];
581 //this is sequence columns on datatable.
582 $selectorCols = array(
598 // get the data table params.
599 $dataTableParams = array(
606 'name' => 'iDisplayStart',
611 'name' => 'iDisplayLength',
616 'name' => 'iSortCol_0',
618 'default' => 'start_date',
620 'sortOrder' => array(
621 'name' => 'sSortDir_0',
626 foreach ($dataTableParams as $pName => $pValues) {
627 $
$pName = $pValues['default'];
628 if (!empty($_POST[$pValues['name']])) {
629 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
630 if ($pName == 'sort') {
631 $
$pName = $selectorCols[$
$pName];
641 $params[$sortParam] = $
$sortParam;
644 $searchCount = CRM_Campaign_BAO_Campaign
::getCampaignSummary($params, TRUE);
645 $campaigns = CRM_Campaign_Page_DashBoard
::getCampaignSummary($params);
646 $iTotal = $searchCount;
648 if ($searchCount > 0) {
649 if ($searchCount < $offset) {
652 foreach ($campaigns as $campaignID => $values) {
653 foreach ($selectorCols as $col) {
654 $searchRows[$campaignID][$col] = CRM_Utils_Array
::value($col, $values);
659 $selectorElements = $selectorCols;
661 $iFilteredTotal = $iTotal;
663 header('Content-Type: application/json');
664 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
665 CRM_Utils_System
::civiExit();
669 * Retrieve survey for survey dashboard.
672 public function surveyList() {
673 //get the search criteria params.
674 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
676 $params = $searchRows = array();
677 foreach ($searchParams as $param) {
678 if (!empty($_POST[$param])) {
679 $params[$param] = $_POST[$param];
683 //this is sequence columns on datatable.
684 $selectorCols = array(
692 'default_number_of_contacts',
693 'max_number_of_contacts',
702 // get the data table params.
703 $dataTableParams = array(
710 'name' => 'iDisplayStart',
715 'name' => 'iDisplayLength',
720 'name' => 'iSortCol_0',
722 'default' => 'created_date',
724 'sortOrder' => array(
725 'name' => 'sSortDir_0',
730 foreach ($dataTableParams as $pName => $pValues) {
731 $
$pName = $pValues['default'];
732 if (!empty($_POST[$pValues['name']])) {
733 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
734 if ($pName == 'sort') {
735 $
$pName = $selectorCols[$
$pName];
745 $params[$sortParam] = $
$sortParam;
748 $surveys = CRM_Campaign_Page_DashBoard
::getSurveySummary($params);
749 $searchCount = CRM_Campaign_BAO_Survey
::getSurveySummary($params, TRUE);
750 $iTotal = $searchCount;
752 if ($searchCount > 0) {
753 if ($searchCount < $offset) {
756 foreach ($surveys as $surveyID => $values) {
757 foreach ($selectorCols as $col) {
758 $searchRows[$surveyID][$col] = CRM_Utils_Array
::value($col, $values);
763 $selectorElements = $selectorCols;
765 $iFilteredTotal = $iTotal;
767 header('Content-Type: application/json');
768 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
769 CRM_Utils_System
::civiExit();
773 * Retrieve petitions for petition dashboard.
776 public function petitionList() {
777 //get the search criteria params.
778 $searchParams = explode(',', CRM_Utils_Array
::value('searchCriteria', $_POST));
780 $params = $searchRows = array();
781 foreach ($searchParams as $param) {
782 if (!empty($_POST[$param])) {
783 $params[$param] = $_POST[$param];
787 //this is sequence columns on datatable.
788 $selectorCols = array(
801 // get the data table params.
802 $dataTableParams = array(
809 'name' => 'iDisplayStart',
814 'name' => 'iDisplayLength',
819 'name' => 'iSortCol_0',
821 'default' => 'created_date',
823 'sortOrder' => array(
824 'name' => 'sSortDir_0',
829 foreach ($dataTableParams as $pName => $pValues) {
830 $
$pName = $pValues['default'];
831 if (!empty($_POST[$pValues['name']])) {
832 $
$pName = CRM_Utils_Type
::escape($_POST[$pValues['name']], $pValues['type']);
833 if ($pName == 'sort') {
834 $
$pName = $selectorCols[$
$pName];
844 $params[$sortParam] = $
$sortParam;
847 $petitions = CRM_Campaign_Page_DashBoard
::getPetitionSummary($params);
848 $searchCount = CRM_Campaign_BAO_Petition
::getPetitionSummary($params, TRUE);
849 $iTotal = $searchCount;
851 if ($searchCount > 0) {
852 if ($searchCount < $offset) {
855 foreach ($petitions as $petitionID => $values) {
856 foreach ($selectorCols as $col) {
857 $searchRows[$petitionID][$col] = CRM_Utils_Array
::value($col, $values);
862 $selectorElements = $selectorCols;
864 $iFilteredTotal = $iTotal;
866 header('Content-Type: application/json');
867 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
868 CRM_Utils_System
::civiExit();