]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/DocumentRevisions/DocumentRevision.php
Release 6.5.0
[Github/sugarcrm.git] / modules / DocumentRevisions / DocumentRevision.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 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 $doc_id;
57     var $doc_type;
58     var $doc_url;
59         var $date_entered;
60         var $created_by;
61         var $filename;
62         var $file_mime_type;
63         var $revision;
64         var $change_log;
65         var $document_name;
66         var $latest_revision;
67         var $file_url;
68         var $file_ext;
69         var $created_by_name;
70
71         var $img_name;
72         var $img_name_bare;
73         
74         var $table_name = "document_revisions"; 
75         var $object_name = "DocumentRevision";
76         var $module_dir = 'DocumentRevisions';
77         var $new_schema = true;
78         var $latest_revision_id;
79         
80         /*var $column_fields = Array("id"
81                 ,"document_id"
82                 ,"date_entered"
83                 ,"created_by"
84                 ,"filename"     
85                 ,"file_mime_type"
86                 ,"revision"
87                 ,"change_log"
88                 ,"file_ext"
89                 );
90 */
91         var $encodeFields = Array();
92
93         // This is used to retrieve related fields from form posts.
94         var $additional_column_fields = Array('');
95
96         // This is the list of fields that are in the lists.
97         var $list_fields = Array("id"
98                 ,"document_id"
99                 ,"date_entered"
100                 ,"created_by"
101                 ,"filename"     
102                 ,"file_mime_type"
103                 ,"revision"
104                 ,"file_url"
105                 ,"change_log"
106                 ,"file_ext"
107                 ,"created_by_name"
108                 );
109                 
110         var $required_fields = Array("revision");
111         
112         
113
114         function DocumentRevision() {
115                 parent::SugarBean();
116                 $this->setupCustomFields('DocumentRevisions');  //parameter is module name
117                 $this->disable_row_level_security =true; //no direct access to this module. 
118         }
119
120         function save($check_notify = false){   
121                 $saveRet = parent::save($check_notify);
122
123                 //update documents table. (not through save, because it causes a loop)
124         // If we don't have a document_id, find it.
125         if ( empty($this->document_id) ) {
126             $query = "SELECT document_id FROM document_revisions WHERE id = '".$this->db->quote($this->id)."'";
127             $ret = $this->db->query($query,true);
128             $row = $this->db->fetchByAssoc($ret);
129             $this->document_id = $row['document_id'];
130         }
131                 $query = "UPDATE documents set document_revision_id='".$this->db->quote($this->id)."', doc_type='".$this->db->quote($this->doc_type)."', doc_url='".$this->db->quote($this->doc_url)."', doc_id='".$this->db->quote($this->doc_id)."' where id = '".$this->db->quote($this->document_id)."'";
132                 $this->db->query($query,true);
133
134         return $saveRet;
135         }
136         function get_summary_text()
137         {
138                 return "$this->filename";
139         }
140
141         function retrieve($id, $encode=false, $deleted=true){
142                 $ret = parent::retrieve($id, $encode,$deleted); 
143                 
144                 return $ret;
145         }
146
147         function is_authenticated()
148         {
149                 return $this->authenticated;
150         }
151
152         function fill_in_additional_list_fields() {
153                 $this->fill_in_additional_detail_fields();
154         }
155
156         function fill_in_additional_detail_fields()
157         {
158                 global $theme;
159                 global $current_language;
160                 
161                 parent::fill_in_additional_detail_fields();
162
163         if ( empty($this->id) && empty($this->document_id) && isset($_REQUEST['return_id']) && !empty($_REQUEST['return_id']) ) {
164             $this->document_id = $_REQUEST['return_id'];
165         }
166                 
167                 //find the document name and current version.
168                 $query = "SELECT document_name, revision, document_revision_id FROM documents, document_revisions where documents.id = '".$this->db->quote($this->document_id)."' AND document_revisions.id = documents.document_revision_id";
169                 $result = $this->db->query($query,true,"Error fetching document details...:");
170                 $row = $this->db->fetchByAssoc($result);
171                 if ($row != null) {
172                         $this->document_name = $row['document_name'];
173             $this->document_name = '<a href="index.php?module=Documents&action=DetailView&record='.$this->document_id.'">'.$row['document_name'].'</a>';
174                         $this->latest_revision = $row['revision'];      
175                         $this->latest_revision_id = $row['document_revision_id'];
176
177             if ( empty($this->revision) ) {
178                 $this->revision = $this->latest_revision + 1;
179             }
180        }
181         }
182         
183         /**
184          * Returns a filename based off of the logical (Sugar-side) Document name and combined with the revision. Tailor
185          * this to needs created by email RFCs, filesystem name conventions, charset conventions etc.
186          * @param string revId Revision ID if not latest
187          * @return string formatted name
188          */
189         function getDocumentRevisionNameForDisplay($revId='') {
190                 global $sugar_config;
191                 global $current_language;
192                 
193                 $localLabels = return_module_language($current_language, 'DocumentRevisions');
194                 
195                 // prep - get source Document
196                 $document = new Document();
197                 
198                 // use passed revision ID
199                 if(!empty($revId)) {
200                         $tempDoc = new DocumentRevision();
201                         $tempDoc->retrieve($revId);
202                 } else {
203                         $tempDoc = $this;
204                 }
205                 
206                 // get logical name
207                 $document->retrieve($tempDoc->document_id);
208                 $logicalName = $document->document_name;
209                 
210                 // get revision string
211                 $revString = '';
212                 if(!empty($tempDoc->revision)) {
213                         $revString = "-{$localLabels['LBL_REVISION']}_{$tempDoc->revision}";
214                 }
215                 
216                 // get extension
217                 $realFilename = $tempDoc->filename;
218                 $fileExtension_beg = strrpos($realFilename, ".");
219                 $fileExtension = "";
220                 
221                 if($fileExtension_beg > 0) {
222                         $fileExtension = substr($realFilename, $fileExtension_beg + 1);
223                 }
224                 //check to see if this is a file with extension located in "badext"
225                 foreach($sugar_config['upload_badext'] as $badExt) {
226                 if(strtolower($fileExtension) == strtolower($badExt)) {
227                         //if found, then append with .txt to filename and break out of lookup
228                         //this will make sure that the file goes out with right extension, but is stored
229                         //as a text in db.
230                         $fileExtension .= ".txt";
231                         break; // no need to look for more
232                 }
233         }
234                 $fileExtension = ".".$fileExtension;
235                 
236                 $return = $logicalName.$revString.$fileExtension;
237                 
238                 // apply RFC limitations here
239                 if(mb_strlen($return) > 1024) {
240                         // do something if we find a real RFC issue
241                 }
242                 
243                 return $return;
244         }
245
246         function fill_document_name_revision($doc_id) {
247
248                 //find the document name and current version.
249                 $query = "SELECT documents.document_name, revision FROM documents, document_revisions where documents.id = '$doc_id'";
250                 $query .= " AND document_revisions.id = documents.document_revision_id";
251                 $result = $this->db->query($query,true,"Error fetching document details...:");
252                 $row = $this->db->fetchByAssoc($result);
253                 if ($row != null) {
254                         $this->name = $row['document_name'];
255                         $this->latest_revision = $row['revision'];      
256                 }       
257         }
258         
259         function list_view_parse_additional_sections(&$list_form, $xTemplateSection){
260                 return $list_form;
261         }
262         
263         function get_list_view_data(){
264                 $revision_fields = $this->get_list_view_array();
265
266                 $forecast_fields['FILE_URL'] = $this->file_url;                                         
267                 return $revision_fields;
268         }
269
270         //static function..
271         function get_document_revision_name($doc_revision_id){
272                 if (empty($doc_revision_id)) return null;
273                 
274                 $db = DBManagerFactory::getInstance();                          
275                 $query="select revision from document_revisions where id='$doc_revision_id'";
276                 $result=$db->query($query);
277                 if (!empty($result)) {
278                         $row=$db->fetchByAssoc($result);
279                         if (!empty($row)) {
280                                 return $row['revision'];
281                         }
282                 }
283                 return null;
284         }
285         
286         //static function.
287         function get_document_revisions($doc_id){
288                 $return_array= Array();
289                 if (empty($doc_id)) return $return_array;
290                 
291                 $db = DBManagerFactory::getInstance();                          
292                 $query="select id, revision from document_revisions where document_id='$doc_id' and deleted=0";
293                 $result=$db->query($query);
294                 if (!empty($result)) {
295                         while (($row=$db->fetchByAssoc($result)) != null) {
296                                 $return_array[$row['id']]=$row['revision'];
297                         }
298                 }
299                 return $return_array;
300         }       
301 }
302
303 require_once('modules/Documents/DocumentExternalApiDropDown.php');