Dialog & select2 improvements
[civicrm-core.git] / templates / CRM / Case / Form / ActivityToCase.tpl
1 {*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.5 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2014 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
8 | |
9 | CiviCRM is free software; you can copy, modify, and distribute it |
10 | under the terms of the GNU Affero General Public License |
11 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | |
13 | CiviCRM is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16 | See the GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public |
19 | License and the CiviCRM Licensing Exception along |
20 | with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 *}
26 {* CiviCase - assign activity to case form *}
27 {if !empty($buildCaseActivityForm)}
28 <div class="crm-block crm-form-block crm-case-activitytocase-form-block">
29 <table class="form-layout">
30 <tr class="crm-case-activitytocase-form-block-unclosed_case_id">
31 <td class="label">{$form.unclosed_case_id.label}</td>
32 <td>{$form.unclosed_case_id.html}</td>
33 </tr>
34 <tr class="crm-case-activitytocase-form-block-target_contact_id">
35 <td class="label">{$form.target_contact_id.label}</td>
36 <td>{$form.target_contact_id.html}</td>
37 </tr>
38 <tr class="crm-case-activitytocase-form-block-case_activity_subject">
39 <td class="label">{$form.case_activity_subject.label}</td>
40 <td>{$form.case_activity_subject.html}<br />
41 <span class="description">{ts}You can modify the activity subject before filing.{/ts}</span>
42 </td>
43 </tr>
44 </table>
45 </div>
46 {literal}
47 <script type="text/javascript">
48 var target_contact = '';
49 var target_contact_id = '';
50 var selectedCaseId = '';
51 var contactId = '';
52 CRM.$(function($) {
53 $('input[name=unclosed_case_id]', '#fileOnCaseDialog').crmSelect2({
54 placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
55 minimumInputLength: 1,
56 ajax: {
57 url: {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0}"{literal},
58 data: function(term) {
59 return {term: term, excludeCaseIds: "{/literal}{$currentCaseId}{literal}"};
60 },
61 results: function(response) {
62 return {results: response};
63 }
64 }
65 }).change(function() {
66 if ($(this).val()) {
67 selectedCaseId = $(this).val();
68 contactId = $(this).select2('data').extra.contact_id;
69 }
70 });
71 });
72 {/literal}
73 {if $targetContactValues}
74 {foreach from=$targetContactValues key=id item=name}
75 {literal}
76 target_contact += '{"name":"'+{/literal}"{$name}"{literal}+'","id":"'+{/literal}"{$id}"{literal}+'"},';
77 {/literal}
78 {/foreach}
79 {literal}
80 eval( 'target_contact = [' + target_contact + ']');
81 {/literal}
82 {/if}
83
84 {if $form.target_contact_id.value}
85 {literal}
86 var toDataUrl = "{/literal}{crmURL p='civicrm/ajax/checkemail' q='id=1&noemail=1' h=0 }{literal}";
87 var target_contact_id = cj.ajax({ url: toDataUrl + "&cid={/literal}{$form.$currentElement.value}{literal}", async: false }).responseText;
88 {/literal}
89 {/if}
90
91 {literal}
92 if ( target_contact_id ) {
93 eval( 'target_contact = ' + target_contact_id );
94 }
95
96 var tokenDataUrl = "{/literal}{$tokenUrl}{literal}";
97 var hintText = "{/literal}{ts escape='js'}Type in a partial or complete name or email address of an existing contact.{/ts}{literal}";
98 cj( "#target_contact_id" ).tokenInput(tokenDataUrl,{prePopulate: target_contact, theme: 'facebook', hintText: hintText });
99 cj( 'ul.token-input-list-facebook, div.token-input-dropdown-facebook' ).css( 'width', '450px' );
100
101 cj( "#fileOnCaseDialog" ).hide( );
102
103 </script>
104 {/literal}
105 {* main form end *}
106
107 {else}
108 {* Markup and js to go on the main page for loading the above form in a popup *}
109 <div id="fileOnCaseDialog"></div>
110 {literal}
111 <script type="text/javascript">
112 function fileOnCase( action, activityID, currentCaseId ) {
113 if ( action == "move" ) {
114 var dialogTitle = "{/literal}{ts escape='js'}Move to Case{/ts}{literal}";
115 } else if ( action == "copy" ) {
116 var dialogTitle = "{/literal}{ts escape='js'}Copy to Case{/ts}{literal}";
117 } else if ( action == "file" ) {
118 var dialogTitle = "{/literal}{ts escape='js'}File On Case{/ts}{literal}";
119 }
120
121 var dataUrl = {/literal}"{crmURL p='civicrm/case/addToCase' q='reset=1&snippet=4' h=0}"{literal};
122 dataUrl += '&activityId=' + activityID + '&caseId=' + currentCaseId + '&cid=' + {/literal}"{$contactID}"{literal};
123
124 cj.ajax({
125 url : dataUrl,
126 success : function ( content ) {
127 cj("#fileOnCaseDialog").show( ).html( content ).dialog({
128 title: dialogTitle,
129 modal: true,
130 width: 600,
131 height: 'auto',
132 close: function( event, ui ) {
133 cj('input[name=unclosed_case_id]', '#fileOnCaseDialog').select2('destroy');
134 cj(this).hide().dialog("destroy");
135 },
136 open: function() {
137
138 },
139
140 buttons: {
141 "{/literal}{ts escape='js'}Save{/ts}{literal}": function() {
142 var subject = cj("#case_activity_subject").val( );
143 var targetContactId = cj("#target_contact_id").val( );
144
145 if ( !cj("#unclosed_cases").val( ) ) {
146 cj("#unclosed_cases").crmError('{/literal}{ts escape="js"}Please select a case from the list{/ts}{literal}.');
147 return false;
148 }
149
150 cj(this).dialog("close");
151
152 var postUrl = {/literal}"{crmURL p='civicrm/ajax/activity/convert' h=0 }"{literal};
153 cj.post( postUrl, { activityID: activityID, caseID: selectedCaseId, contactID: contactId, newSubject: subject, targetContactIds: targetContactId, mode: action, key: {/literal}"{crmKey name='civicrm/ajax/activity/convert'}"{literal} },
154 function( values ) {
155 if ( values.error_msg ) {
156 cj().crmError(values.error_msg, "{/literal}{ts escape='js'}Unable to file on case{/ts}{literal}.");
157 return false;
158 } else {
159 var destUrl = {/literal}"{crmURL p='civicrm/contact/view/case' q='reset=1&action=view&id=' h=0 }"{literal};
160 var context = '';
161 {/literal}{if !empty($fulltext)}{literal}
162 context = '&context={/literal}{$fulltext}{literal}';
163 {/literal}{/if}{literal}
164 var caseUrl = destUrl + selectedCaseId + '&cid=' + contactId + context;
165 var redirectToCase = false;
166 var reloadWindow = false;
167 if ( action == 'move' ) redirectToCase = true;
168 if ( action == 'file' ) {
169 var curPath = document.location.href;
170 if ( curPath.indexOf( 'civicrm/contact/view' ) != -1 ) {
171 //hide current activity row.
172 cj( "#crm-activity_" + activityID ).hide( );
173 var visibleRowCount = 0;
174 cj('[id^="'+ 'crm-activity' +'"]:visible').each(function() {
175 visibleRowCount++;
176 } );
177 if ( visibleRowCount < 1 ) {
178 reloadWindow = true;
179 }
180 }
181 if ( ( curPath.indexOf( 'civicrm/contact/view/activity' ) != -1 ) ||
182 ( curPath.indexOf( 'civicrm/activity' ) != -1 ) ) {
183 redirectToCase = true;
184 }
185 }
186
187 if ( redirectToCase ) {
188 window.location.href = caseUrl;
189 } else if ( reloadWindow ) {
190 window.location.reload( );
191 } else {
192 var activitySubject = cj("#case_activity_subject").val( );
193 var statusMsg = activitySubject + '" has been filed to selected case: <a href="' + caseUrl + '">' + cj("#unclosed_cases").val( ) + '</a>.';
194 CRM.alert(statusMsg, '{/literal}{ts escape="js"}Activity Filed{/ts}{literal}', 'success');
195
196 }
197 }
198 }
199 );
200 },
201 "{/literal}{ts escape='js'}Cancel{/ts}{literal}": function() {
202 cj(this).dialog("close");
203 }
204 }
205
206 });
207 }
208 });
209 }
210 </script>
211 {/literal}
212 {/if}