]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/DocumentRevisions/DocumentRevision.php
Release 6.1.4
[Github/sugarcrm.git] / modules / DocumentRevisions / DocumentRevision.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description: TODO:  To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47
48
49 require_once('include/upload_file.php');
50
51 // User is used to store Forecast information.
52 class DocumentRevision extends SugarBean {
53
54         var $id;
55         var $document_id;
56         var $date_entered;
57         var $created_by;
58         var $filename;
59         var $file_mime_type;
60         var $revision;
61         var $change_log;
62         var $document_name;
63         var $latest_revision;
64         var $file_url;
65         var $file_ext;
66         var $created_by_name;
67
68         var $img_name;
69         var $img_name_bare;
70         
71         var $table_name = "document_revisions"; 
72         var $object_name = "DocumentRevision";
73         var $module_dir = 'DocumentRevisions';
74         var $new_schema = true;
75         var $latest_revision_id;
76         
77         /*var $column_fields = Array("id"
78                 ,"document_id"
79                 ,"date_entered"
80                 ,"created_by"
81                 ,"filename"     
82                 ,"file_mime_type"
83                 ,"revision"
84                 ,"change_log"
85                 ,"file_ext"
86                 );
87 */
88         var $encodeFields = Array();
89
90         // This is used to retrieve related fields from form posts.
91         var $additional_column_fields = Array('');
92
93         // This is the list of fields that are in the lists.
94         var $list_fields = Array("id"
95                 ,"document_id"
96                 ,"date_entered"
97                 ,"created_by"
98                 ,"filename"     
99                 ,"file_mime_type"
100                 ,"revision"
101                 ,"file_url"
102                 ,"change_log"
103                 ,"file_ext"
104                 ,"created_by_name"
105                 );
106                 
107         var $required_fields = Array("revision");
108         
109         
110
111         function DocumentRevision() {
112                 parent::SugarBean();
113                 $this->setupCustomFields('DocumentRevisions');  //parameter is module name
114                 $this->disable_row_level_security =true; //no direct access to this module. 
115         }
116
117         function save($check_notify = false){   
118                 parent::save($check_notify);
119                                 
120                 //update documents table.
121                 //$query = "UPDATE documents set document_version_id='$this->id' where id = '$this->document_id'";      
122                 //$this->db->query($query);
123         }
124         function get_summary_text()
125         {
126                 return "$this->filename";
127         }
128
129         function retrieve($id, $encode=false){
130                 $ret = parent::retrieve($id, $encode);  
131                 
132                 return $ret;
133         }
134
135         function is_authenticated()
136         {
137                 return $this->authenticated;
138         }
139
140         function fill_in_additional_list_fields() {
141                 $this->fill_in_additional_detail_fields();
142         }
143
144         function fill_in_additional_detail_fields()
145         {
146                 global $theme;
147                 global $current_language;
148                 
149                 parent::fill_in_additional_detail_fields();
150                 
151                 $mod_strings=return_module_language($current_language, 'Documents');
152                 
153                 //find the document name and current version.
154                 $query = "SELECT document_name, revision, document_revision_id FROM documents, document_revisions where documents.id = '$this->document_id' AND document_revisions.id = documents.document_revision_id";
155                 $result = $this->db->query($query,true,"Error fetching document details...:");
156                 $row = $this->db->fetchByAssoc($result);
157                 if ($row != null) {
158                         $this->document_name = $row['document_name'];
159                         $this->latest_revision = $row['revision'];      
160                         $this->latest_revision_id = $row['document_revision_id'];
161                 }
162                 //populate the file url. 
163                 //image is selected based on the extension name <ext>_image_inline, extension is stored in document_revisions.
164                 //if file is not found then default image file will be used.
165                 global $img_name;
166                 global $img_name_bare;
167                 if (!empty($this->file_ext)) {
168                         $img_name = SugarThemeRegistry::current()->getImageURL("{$this->file_ext}_image_inline.gif");   
169                         $img_name_bare = "{$this->file_ext}_image_inline";              
170                 }
171                 
172                 //set default file name.
173                 if (!empty($img_name) && file_exists($img_name)) {
174                         $img_name = $img_name_bare;                     
175                 }
176                 else {
177                         $img_name = "def_image_inline";  //todo change the default image.                                               
178                 }
179                 if($this->ACLAccess('DetailView')){
180                     $this->file_url = "<a href='".UploadFile::get_url($this->filename,$this->id)."' target='_blank'>".SugarThemeRegistry::current()->getImage($img_name,'alt="'.$mod_strings['LBL_LIST_VIEW_DOCUMENT'].'"  border="0"')."</a>";
181                 }else{
182                     $this->file_url = "";
183                 }
184         }
185         
186         /**
187          * Returns a filename based off of the logical (Sugar-side) Document name and combined with the revision. Tailor
188          * this to needs created by email RFCs, filesystem name conventions, charset conventions etc.
189          * @param string revId Revision ID if not latest
190          * @return string formatted name
191          */
192         function getDocumentRevisionNameForDisplay($revId='') {
193                 global $sugar_config;
194                 global $current_language;
195                 
196                 $localLabels = return_module_language($current_language, 'DocumentRevisions');
197                 
198                 // prep - get source Document
199                 if(!class_exists('Documents')) {
200                         
201                 }
202                 $document = new Document();
203                 
204                 // use passed revision ID
205                 if(!empty($revId)) {
206                         $tempDoc = new DocumentRevision();
207                         $tempDoc->retrieve($revId);
208                 } else {
209                         $tempDoc = $this;
210                 }
211                 
212                 // get logical name
213                 $document->retrieve($tempDoc->document_id);
214                 $logicalName = $document->document_name;
215                 
216                 // get revision string
217                 $revString = '';
218                 if(!empty($tempDoc->revision)) {
219                         $revString = "-{$localLabels['LBL_REVISION']}_{$tempDoc->revision}";
220                 }
221                 
222                 // get extension
223                 $realFilename = $tempDoc->filename;
224                 $fileExtension_beg = strrpos($realFilename, ".");
225                 $fileExtension = "";
226                 
227                 if($fileExtension_beg > 0) {
228                         $fileExtension = substr($realFilename, $fileExtension_beg + 1);
229                 }
230                 //check to see if this is a file with extension located in "badext"
231                 foreach($sugar_config['upload_badext'] as $badExt) {
232                 if(strtolower($fileExtension) == strtolower($badExt)) {
233                         //if found, then append with .txt to filename and break out of lookup
234                         //this will make sure that the file goes out with right extension, but is stored
235                         //as a text in db.
236                         $fileExtension .= ".txt";
237                         break; // no need to look for more
238                 }
239         }
240                 $fileExtension = ".".$fileExtension;
241                 
242                 $return = $logicalName.$revString.$fileExtension;
243                 
244                 // apply RFC limitations here
245                 if(mb_strlen($return) > 1024) {
246                         // do something if we find a real RFC issue
247                 }
248                 
249                 return $return;
250         }
251
252         function fill_document_name_revision($doc_id) {
253
254                 //find the document name and current version.
255                 $query = "SELECT documents.document_name, revision FROM documents, document_revisions where documents.id = '$doc_id'";
256                 $query .= " AND document_revisions.id = documents.document_revision_id";
257                 $result = $this->db->query($query,true,"Error fetching document details...:");
258                 $row = $this->db->fetchByAssoc($result);
259                 if ($row != null) {
260                         $this->name = $row['document_name'];
261                         $this->latest_revision = $row['revision'];      
262                 }       
263         }
264         
265         function list_view_parse_additional_sections(&$list_form, $xTemplateSection){
266                 return $list_form;
267         }
268         
269         function get_list_view_data(){
270                 $revision_fields = $this->get_list_view_array();
271
272                 $forecast_fields['FILE_URL'] = $this->file_url;                                         
273                 return $revision_fields;
274         }
275
276         //static function..
277         function get_document_revision_name($doc_revision_id){
278                 if (empty($doc_revision_id)) return null;
279                 
280                 $db = DBManagerFactory::getInstance();                          
281                 $query="select revision from document_revisions where id='$doc_revision_id'";
282                 $result=$db->query($query);
283                 if (!empty($result)) {
284                         $row=$db->fetchByAssoc($result);
285                         if (!empty($row)) {
286                                 return $row['revision'];
287                         }
288                 }
289                 return null;
290         }
291         
292         //static function.
293         function get_document_revisions($doc_id){
294                 $return_array= Array();
295                 if (empty($doc_id)) return $return_array;
296                 
297                 $db = DBManagerFactory::getInstance();                          
298                 $query="select id, revision from document_revisions where document_id='$doc_id' and deleted=0";
299                 $result=$db->query($query);
300                 if (!empty($result)) {
301                         while (($row=$db->fetchByAssoc($result)) != null) {
302                                 $return_array[$row['id']]=$row['revision'];
303                         }
304                 }
305                 return $return_array;
306         }       
307 }
308 ?>