]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/File/SugarFieldFile.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Fields / File / SugarFieldFile.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once('include/SugarFields/Fields/Base/SugarFieldBase.php');
38
39 class SugarFieldFile extends SugarFieldBase {
40     private function fillInOptions(&$vardef,&$displayParams) {
41         if ( isset($vardef['allowEapm']) && $vardef['allowEapm'] == true ) {
42             if ( empty($vardef['docType']) ) {
43                 $vardef['docType'] = 'doc_type';
44             }
45             if ( empty($vardef['docId']) ) {
46                 $vardef['docId'] = 'doc_id';
47             }
48             if ( empty($vardef['docUrl']) ) {
49                 $vardef['docUrl'] = 'doc_url';
50             }
51         } else {
52             $vardef['allowEapm'] = false;
53         }
54
55         // Override the default module
56         if ( isset($vardef['linkModuleOverride']) ) {
57             $vardef['linkModule'] = $vardef['linkModuleOverride'];
58         } else {
59             $vardef['linkModule'] = '{$module}';
60         }
61
62         // This is needed because these aren't always filled out in the edit/detailview defs
63         if ( !isset($vardef['fileId']) ) {
64             if ( isset($displayParams['id']) ) {
65                 $vardef['fileId'] = $displayParams['id'];
66             } else {
67                 $vardef['fileId'] = 'id';
68             }
69         }
70     }
71
72
73         function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
74         $this->fillInOptions($vardef,$displayParams);
75         return parent::getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
76     }
77     
78         function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
79         $this->fillInOptions($vardef,$displayParams);
80
81         $keys = $this->getAccessKey($vardef,'FILE',$vardef['module']);
82         $displayParams['accessKeySelect'] = $keys['accessKeySelect'];
83         $displayParams['accessKeySelectLabel'] = $keys['accessKeySelectLabel'];
84         $displayParams['accessKeySelectTitle'] = $keys['accessKeySelectTitle'];
85         $displayParams['accessKeyClear'] = $keys['accessKeyClear'];
86         $displayParams['accessKeyClearLabel'] = $keys['accessKeyClearLabel'];
87         $displayParams['accessKeyClearTitle'] = $keys['accessKeyClearTitle'];
88         
89         return parent::getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
90     }
91     
92         public function save(&$bean, $params, $field, $vardef, $prefix = ''){
93         $fakeDisplayParams = array();
94         $this->fillInOptions($vardef,$fakeDisplayParams);
95
96                 require_once('include/upload_file.php');
97                 $upload_file = new UploadFile($prefix . $field . '_file');
98
99                 //remove file
100                 if (isset($_REQUEST['remove_file_' . $field]) && $params['remove_file_' . $field] == 1) {
101                         $upload_file->unlink_file($bean->$field);
102                         $bean->$field="";
103                 }
104                 
105                 $move=false;
106                 if (isset($_FILES[$prefix . $field . '_file']) && $upload_file->confirm_upload())
107                 {
108                 $bean->$field = $upload_file->get_stored_file_name();
109                 $bean->file_mime_type = $upload_file->mime_type;
110                         $bean->file_ext = $upload_file->file_ext;
111                         $move=true;
112                 }
113
114         if (!empty($params['isDuplicate']) && $params['isDuplicate'] == 'true' ) {
115             // This way of detecting duplicates is used in Notes
116             $old_id = $params['relate_id'];
117         }
118         if (!empty($params['duplicateSave']) && !empty($params['duplicateId']) ) {
119             // It's a duplicate
120             $old_id = $params['duplicateId'];
121         }
122
123         // Backwards compatibility for fields that still use customCode to handle the file uploads
124         if ( !$move && empty($old_id) && isset($_FILES['uploadfile']) ) {
125             $upload_file = new UploadFile('uploadfile');
126             if ( $upload_file->confirm_upload() ) {
127                 $bean->$field = $upload_file->get_stored_file_name();
128                 $bean->file_mime_type = $upload_file->mime_type;
129                 $bean->file_ext = $upload_file->file_ext;
130                 $move=true;
131                 
132             }
133         } else if ( !$move && !empty($old_id) && isset($_REQUEST['uploadfile']) && !isset($_REQUEST[$prefix . $field . '_file']) ) {
134             // I think we are duplicating a backwards compatibility module.
135             $upload_file = new UploadFile('uploadfile');
136         }
137
138
139         if (empty($bean->id)) { 
140             $bean->id = create_guid();
141             $bean->new_with_id = true;
142         }
143
144                 if ($move) {
145             $upload_file->final_move($bean->id);
146             $upload_file->upload_doc($bean, $bean->id, $params[$prefix . $vardef['docType']], $bean->$field, $upload_file->mime_type);
147         } else if ( ! empty($old_id) ) {
148             // It's a duplicate, I think
149
150             if ( empty($params[$prefix . $vardef['docUrl'] ]) ) {
151                 $upload_file->duplicate_file($old_id, $bean->id, $bean->$field);
152             } else {
153                 $docType = $vardef['docType'];
154                 $bean->$docType = $params[$prefix . $field . '_old_doctype'];
155             }
156                 } else if ( !empty($params[$prefix . $field . '_remoteName']) ) {
157             // We aren't moving, we might need to do some remote linking
158             $displayParams = array();
159             $this->fillInOptions($vardef,$displayParams);
160             
161             if ( isset($params[$prefix . $vardef['docId']])
162                  && ! empty($params[$prefix . $vardef['docId']])
163                  && isset($params[$prefix . $vardef['docType']]) 
164                  && ! empty($params[$prefix . $vardef['docType']])
165                 ) {
166                 $bean->$field = $params[$prefix . $field . '_remoteName'];
167                 
168                 require_once('include/utils/file_utils.php');
169                 $extension = get_file_extension($bean->$field);
170                 if(!empty($extension))
171                 {
172                         $bean->file_ext = $extension;
173                         $bean->file_mime_type = get_mime_content_type_from_filename($bean->$field);
174                 }
175             }
176         }
177         
178         if ( $vardef['allowEapm'] == true && empty($bean->$field) ) {
179             $GLOBALS['log']->info("The $field is empty, clearing out the lot");
180             // Looks like we are emptying this out
181             $clearFields = array('docId', 'docType', 'docUrl', 'docDirectUrl');
182             foreach ( $clearFields as $clearMe ) {
183                 if ( ! isset($vardef[$clearMe]) ) {
184                     continue;
185                 }
186                 $clearField = $vardef[$clearMe];
187                 $bean->$clearField = '';
188             }
189         }
190         }
191 }