Merge pull request #16967 from wmortada/wp#46-2
[civicrm-core.git] / templates / CRM / Campaign / Form / Gotv.tpl
CommitLineData
6a488035
TO
1{*
2 +--------------------------------------------------------------------+
1188c7a8 3 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 4 | |
1188c7a8
TO
5 | This work is published under the GNU AGPLv3 license with some |
6 | permitted exceptions and without any warranty. For full license |
7 | and copyright information, see https://civicrm.org/licensing |
6a488035
TO
8 +--------------------------------------------------------------------+
9*}
10
11{if $errorMessages}
12 <div class="messages status crm-error no-popup">
13 <div class="icon inform-icon"></div>
14 <ul>
15 {foreach from=$errorMessages item=errorMsg}
16 <li>{ts}{$errorMsg}{/ts}</li>
17 {/foreach}
18 </ul>
19 </div>
20 </div>
21
22{elseif $buildSelector}
23
24 {* load voter selector for reserve/release *}
25
26 {literal}
27 <script type="text/javascript">
3cc60a06 28 CRM.$(function($) {
6a488035
TO
29 loadVoterList( );
30 });
31 </script>
32 {/literal}
33
24e7a504 34 <table class="gotvVoterRecords">
6a488035
TO
35 <thead>
36 <tr class="columnheader">
37 <th></th>
38 <th>{ts}Name{/ts}</th>
39 <th>{ts}Street Address{/ts}</th>
40 <th>{ts}Street Name{/ts}</th>
41 <th>{ts}Street Number{/ts}</th>
42 <th>{ts}Street Unit{/ts}</th>
43 {if $searchVoterFor eq 'release'}
44 <th>{ts}Is Interview Conducted?{/ts}</th>
45 {elseif $searchVoterFor eq 'gotv'}
46 <th>{ts}Voted?{/ts}</th>
47 {else}
48 <th>{ts}Is Reserved?{/ts}</th>
49 {/if}
50 </tr>
51 </thead>
52 <tbody></tbody>
53 </table>
54
55{else}
56
57 {* build search form *}
58 {include file='CRM/Campaign/Form/Search/Common.tpl' context='gotv'}
611e4d8b
M
59 <div class="crm-block crm-form-block crm-search-form-block">
60 <div id='voterList'></div>
61 </div>
6a488035
TO
62
63{/if} {* end of search form build *}
64
65
66{literal}
67<script type="text/javascript">
68
69 {/literal}
70 {* load selector when force *}
71 {if $force and !$buildSelector}
72 {literal}
3cc60a06 73 CRM.$(function($) {
6a488035
TO
74 searchVoters( {/literal}'{$qfKey}'{literal} );
75 });
76
77 {/literal}
78 {/if}
79 {literal}
80
81function searchVoters( qfKey )
82{
83 var dataUrl = {/literal}"{crmURL h=0 q='search=1&snippet=4'}"{literal};
84
85 //carry survey and interviewer id,
86 //might be helpful if user jump from current tab to interview tab.
2c68626a
DJ
87 var surveyId = CRM.$( '#campaign_survey_id' ).val();
88 var interviewerId = CRM.$( '#survey_interviewer_id' ).val();
6a488035
TO
89 if ( surveyId ) dataUrl = dataUrl + '&sid=' + surveyId;
90 if ( interviewerId ) dataUrl = dataUrl + '&cid=' + interviewerId;
91
92 //lets carry qfKey to retain form session.
93 if ( qfKey ) dataUrl = dataUrl + '&qfKey=' + qfKey;
94
2c68626a
DJ
95 CRM.$.get( dataUrl, null, function( voterList ) {
96 CRM.$( '#voterList' ).html( voterList ).trigger('crmLoad');
6a488035
TO
97
98 //collapse the search form.
99 var searchFormName = '#search_form_' + {/literal}'{$searchVoterFor}'{literal};
2c68626a 100 CRM.$( searchFormName + '.crm-accordion-wrapper:not(.collapsed)').crmAccordionToggle();
6a488035
TO
101 }, 'html' );
102}
103
104function loadVoterList( )
105{
106 var sourceUrl = {/literal}"{crmURL p='civicrm/ajax/rest' h=0 q='snippet=4&className=CRM_Campaign_Page_AJAX&fnName=voterList' }"{literal};
107
108 var searchVoterFor = {/literal}'{$searchVoterFor}'{literal};
b50fdacc 109 CRM.$( 'table.gotvVoterRecords', 'form.{/literal}{$form.formClass}{literal}').dataTable({
6a488035
TO
110 "bFilter" : false,
111 "bAutoWidth" : false,
112 "bProcessing": true,
113 "bJQueryUI" : true,
114 "aaSorting": [[0,''],[1,'asc'], [2,'asc'], [3,'asc'], [4,'asc'], [5,'asc'] ],
115 "aoColumns":[{bSortable:false},{sClass:""},{sClass:""},{sClass:""},{sClass:""},{sClass:""},{bSortable:false}],
116 "sPaginationType": "full_numbers",
117 "sDom" : '<"crm-datatable-pager-top"lfp>rt<"crm-datatable-pager-bottom"ip>',
118 "bServerSide": true,
119 "sAjaxSource": sourceUrl,
6a488035
TO
120
121 "fnServerData": function ( sSource, aoData, fnCallback ) {
122 var dataLength = aoData.length;
123
124 var count = 1;
ae8f569f 125 var searchCriteria = ['campaign_search_voter_for'];
6a488035
TO
126
127 //get the search criteria.
128 var searchParams = {/literal}{$searchParams}{literal};
129 for ( param in searchParams ) {
2c68626a 130 if ( val = CRM.$( '#' + param ).val( ) ) {
6a488035
TO
131 aoData[dataLength++] = {name: param , value: val };
132 }
133 searchCriteria[count++] = param;
134 }
135
136 //do search to reserve voters.
137 aoData[dataLength++] = {name: 'campaign_search_voter_for', value: searchVoterFor};
138
139 //lets transfer search criteria.
140 aoData[dataLength++] = {name: 'searchCriteria', value:searchCriteria.join(',')};
141
2c68626a 142 CRM.$.ajax( {
6a488035
TO
143 "dataType": 'json',
144 "type": "POST",
145 "url": sSource,
146 "data": aoData,
147 "success": fnCallback
148 } ); }
149 });
150}
151
152function processVoterData( element, operation )
153{
154
155 var data = new Object;
156 if ( !operation ) return;
157
158 var data = new Object;
159 if ( operation == 'release' ) {
160 data['operation'] = operation;
2c68626a
DJ
161 data['activity_id'] = CRM.$( element ).val( );
162 data['isDelete'] = CRM.$( element ).prop('checked') ? 1:0;
6a488035 163 } else if ( operation == 'reserve' ) {
2c68626a 164 var interviewerId = CRM.$( '#survey_interviewer_id' ).val( );
6a488035 165 data['operation'] = operation;
2c68626a
DJ
166 data['source_record_id'] = CRM.$( '#campaign_survey_id' ).val( );
167 data['target_contact_id'] = CRM.$( element ).val( );
6a488035
TO
168 data['source_contact_id'] = interviewerId;
169 data['assignee_contact_id'] = interviewerId;
2c68626a 170 data['isReserved'] = CRM.$( element ).prop('checked') ? 1:0;
6a488035
TO
171 } else if ( operation == 'gotv' ) {
172 data['operation'] = operation;
2c68626a
DJ
173 data['activity_id'] = CRM.$( element ).val( );
174 data['hasVoted'] = CRM.$( element ).prop('checked') ? 1: 0;
6a488035
TO
175 }
176 data['surveyTitle'] = {/literal}'{$surveyTitle|escape:javascript}'{literal};
177
178 var actUrl = {/literal}
179 "{crmURL p='civicrm/ajax/rest' h=0 q='className=CRM_Campaign_Page_AJAX&fnName=processVoterData'}"
180 {literal};
181
182 //post data to save voter as voted/non voted.
2c68626a 183 CRM.$.post( actUrl,
6a488035
TO
184 data,
185 function( response ) {
186 if ( response.status == 'success' ) {
2c68626a
DJ
187 var msgId = '#success_msg_' + CRM.$( element ).val( );
188 CRM.$( msgId ).fadeIn('slow').fadeOut('slow');
6a488035
TO
189 if ( operation == 'release' ) {
190 msg = '{/literal}{ts escape='js'}Save as voted.{/ts}{literal}';
2c68626a 191 var isDeleted = CRM.$( element ).prop('checked') ? 1:0;
6a488035
TO
192 if ( !isDeleted ) msg = '{/literal}{ts escape='js'}Save as non voted.{/ts}{literal}';
193 } else if ( operation == 'gotv' ) {
194 msg = '{/literal}{ts escape='js'}Vote Recorded.{/ts}{literal}';
2c68626a
DJ
195 var hasVoted = CRM.$( element ).prop('checked') ? 1:0;
196 var trObject = CRM.$( '[id^="survey_activity['+ CRM.$( element ).val() +']"]' ).parents('tr' );
6a488035
TO
197 var methodName = 'addClass';
198 if ( !hasVoted ) {
199 msg = '{/literal}{ts escape='js'}Vote Cancelled.{/ts}{literal}';
200 methodName = 'removeClass';
201 }
202 eval( 'trObject.' + methodName + "( 'name disabled' )" );
203 } else if ( operation == 'reserve' ) {
2c68626a 204 if ( CRM.$( element ).prop('checked') ) {
6a488035
TO
205 msg = '{/literal}{ts escape='js'}Reserved.{/ts}{literal}';
206 } else {
207 msg = '{/literal}{ts escape='js'}Released.{/ts}{literal}';
208 }
209 }
2c68626a 210 CRM.$( msgId ).html( msg );
6a488035
TO
211 }
212 }, 'json' );
213
214}
215
216</script>
217{/literal}