]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/SugarFields/Fields/File/SugarFieldFile.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / SugarFields / Fields / File / SugarFieldFile.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36         
37 if ( typeof(SUGAR.field) == 'undefined' ) {
38     SUGAR.field = new Object();
39 }
40
41 if ( typeof(SUGAR.field.file) == 'undefined' ) {
42     SUGAR.field.file = {
43         deleteAttachment: function(elemBaseName,docTypeName,elem) {
44             ajaxStatus.showStatus(SUGAR.language.get("app_strings", "LBL_REMOVING_ATTACHMENT"));
45             elem.form.deleteAttachment.value=1;
46             elem.form.action.value="deleteattachment";
47
48             var callback =  SUGAR.field.file.deleteAttachmentCallbackGen(elemBaseName,docTypeName);
49             var success = function(data) {
50                 if(data) {
51                     callback(data.responseText);
52                 }
53             }
54             YAHOO.util.Connect.setForm(elem.form); 
55             var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', {success: success, failure: success});
56             
57             elem.form.deleteAttachment.value=0;
58             elem.form.action.value="";
59         },
60         deleteAttachmentCallbackGen: function(elemBaseName,docTypeName) {
61             return function(text) {
62                     if(text == 'true') {
63                             document.getElementById(elemBaseName+'_new').style.display = '';
64                             ajaxStatus.hideStatus();
65                             document.getElementById(elemBaseName+'_old').innerHTML = '';
66                     if(docTypeName){
67                         document.getElementById(docTypeName).disabled = false;
68                     }
69                     document.getElementById(elemBaseName).value = '';
70                     } else {
71                             document.getElementById(elemBaseName+'_new').style.display = 'none';
72                             ajaxStatus.flashStatus(SUGAR.language.get('app_strings', 'ERR_REMOVING_ATTACHMENT'), 2000);
73                     }
74             }
75         },
76         checkEapiLogin: function(res) {
77             var failedLogins = YAHOO.lang.JSON.parse(res.responseText);
78             if ( failedLogins.length == 0 ) { return; }
79
80             for ( var idx in failedLogins ) {
81                 if(confirm(failedLogins[idx].label)) {
82                     window.open(failedLogins[idx].checkURL,'EAPM_CHECK_'+idx);
83                 } else {
84                     document.getElementById(res.argument.docTypeName).value = 'Sugar';
85                     document.getElementById(res.argument.docTypeName).onchange();
86                 }
87             }
88         },
89         setupEapiShowHide: function(elemBaseName,docTypeName,formName) {
90             var externalSearchToggle = function() {
91                 var moreElem = document.getElementById(elemBaseName + "_more");
92                 var hideMore = (moreElem.style.display == 'none');
93                 if ( hideMore ) {
94                     // We're hiding the "more" element, so we clicked on the "less" element and want to hide stuff
95                     moreElem.style.display = '';
96                     document.getElementById(elemBaseName + '_less').style.display = 'none';
97                     document.getElementById(elemBaseName + '_remoteNameSpan').style.display = 'none';
98                     document.getElementById(elemBaseName + '_file').disabled = false;
99                 } else {
100                     // We're not hiding the "more" element, so we clicked on the "more" element and want to show stuff
101                     moreElem.style.display = 'none';
102                     document.getElementById(elemBaseName + '_less').style.display = '';
103                     document.getElementById(elemBaseName + '_remoteNameSpan').style.display = '';
104                     document.getElementById(elemBaseName + '_file').disabled = true;
105                 }
106             }
107
108             var showHideFunc = function() {
109                 var docShowHideElem = document.getElementById(elemBaseName + "_externalApiSelector");
110                 
111                 var dropdownValue = document.getElementById(docTypeName).value;
112                 if ( typeof(SUGAR.eapm) != 'undefined' 
113                      && typeof(SUGAR.eapm[dropdownValue]) != 'undefined' 
114                      && typeof(SUGAR.eapm[dropdownValue].docSearch) != 'undefined'
115                      && SUGAR.eapm[dropdownValue].docSearch ) {
116                     docShowHideElem.style.display = '';
117                     
118
119                     // Double check to make sure their login is valid
120                     YAHOO.util.Connect.asyncRequest('GET', 'index.php?module=EAPM&action=CheckLogins&to_pdf=1&api='+dropdownValue,{success:SUGAR.field.file.checkEapiLogin,argument:{'elemBaseName':elemBaseName,'docTypeName':docTypeName}});
121
122                     // Start a refresh of the document cache in the background. Thanks AJAX!
123                     YAHOO.util.Connect.asyncRequest('GET', 'index.php?module=EAPM&action=flushFileCache&to_pdf=1&api='+dropdownValue,{});
124                 } else {
125                     docShowHideElem.style.display = 'none';
126                     document.getElementById(elemBaseName + '_file').disabled = false;
127                 }
128                 // Update the quick search
129                 sqs_objects[formName+"_"+elemBaseName+"_remoteName"].api = dropdownValue;
130
131                 
132                 // Now time to see if we can select security options
133                 var secLevelBoxElem = document.getElementById(elemBaseName + '_securityLevelBox');
134                 var secLevelElem = document.getElementById(elemBaseName + '_securityLevel');
135                 
136                 secLevelElem.options.length = 0;
137                 
138                 if ( SUGAR.eapm[dropdownValue] && SUGAR.eapm[dropdownValue].sharingOptions ) {
139                     var opts = SUGAR.eapm[dropdownValue].sharingOptions;
140                     var i = 0;
141
142                     for ( idx in opts ) {
143                         secLevelElem.options[i] = new Option(SUGAR.language.get('app_strings',opts[idx]),idx,false,false);
144                         i++;
145                     }
146                     
147                     secLevelBoxElem.style.display='';
148                 } else {
149                     secLevelBoxElem.style.display='none';
150                 }
151                 
152             }
153             document.getElementById(docTypeName).onchange = showHideFunc;
154
155             document.getElementById(elemBaseName + '_externalApiLabel').onclick = externalSearchToggle;
156             showHideFunc();
157         },
158         
159 // Select button / popup related functions
160         openPopup: function(elemBaseName) {
161             window.open('index.php?module=Documents&action=extdoc&isPopup=1&elemBaseName='+elemBaseName+'&apiName='+document.getElementById('doc_type').value,'sugarPopup','width=600,height=400,menubar=no,toolbar=no,status=no,resizeable=yes,scrollbars=yes'); 
162         },
163         
164         clearRemote: function(elemBaseName) {
165             document.getElementById('doc_id').value = '';
166             document.getElementById(elemBaseName).value = '';
167             document.getElementById(elemBaseName + '_remoteName').value = '';
168             document.getElementById('doc_url').value = '';
169         },
170
171         populateFromPopup: function(elemBaseName, docId, docName, docUrl, docDirectUrl) {
172             document.getElementById('doc_id').value = docId;
173             document.getElementById(elemBaseName).value = docId;
174             document.getElementById(elemBaseName + '_remoteName').value = docName;
175             document.getElementById('doc_url').value = docUrl;
176         },
177         
178         getFileExtension:function(fileName) {
179             var lastindex = fileName.lastIndexOf(".");   
180             if(lastindex == -1)
181                 return '';
182             else
183                 return fileName.substr(++lastindex);
184         },
185         isFileExtensionValid: function(fileName) {
186             var docType = document.getElementById('doc_type').value;
187             var fileExtension = this.getFileExtension(fileName);
188             
189             if( typeof(SUGAR.eapm[docType]) == 'undefined' || ! SUGAR.eapm[docType].restrictUploadsByExtension ){
190                 return true;
191             }   
192             var whiteSuffixlist = SUGAR.eapm[docType]['restrictUploadsByExtension']; 
193             if(whiteSuffixlist.constructor == Array){
194                 var results = false;
195                 for(var i=0;i<whiteSuffixlist.length;i++){
196                     if( fileExtension.toLowerCase() == whiteSuffixlist[i].toLowerCase() ){
197                         return true;
198                     }
199                 }
200             }
201             return results;
202         },
203         
204         checkFileExtension: function(e,obj) {
205             var sff = SUGAR.field.file; //Scope is set to element.
206             var fileEl = document.getElementById(obj.fileEl);
207             var fileName = fileEl.value;
208             
209             var isValid = sff.isFileExtensionValid(fileName);
210             if( !isValid && fileName != '' ){
211                 var errorPannel = new YAHOO.widget.SimpleDialog('sugarMsgWindow', {
212                                 width: '240px',visible: true, fixedcenter: true,constraintoviewport: true,
213                         draggable: true,type:'alert',modal:true,id:'sugarMsgWindow',close:true
214                         });
215                         errorPannel.setBody(SUGAR.language.get("app_strings", "LBL_INVALID_FILE_EXTENSION"));
216                         errorPannel.render(document.body);
217                         errorPannel.show();
218                         fileEl.value = '';
219                         document.getElementById(obj.targEl).value = '';
220             }
221         }
222     }
223 }