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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: TODO: To be written.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
49 require_once('include/upload_file.php');
51 // User is used to store Forecast information.
52 class DocumentRevision extends SugarBean {
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;
77 /*var $column_fields = Array("id"
88 var $encodeFields = Array();
90 // This is used to retrieve related fields from form posts.
91 var $additional_column_fields = Array('');
93 // This is the list of fields that are in the lists.
94 var $list_fields = Array("id"
107 var $required_fields = Array("revision");
111 function DocumentRevision() {
113 $this->setupCustomFields('DocumentRevisions'); //parameter is module name
114 $this->disable_row_level_security =true; //no direct access to this module.
117 function save($check_notify = false){
118 parent::save($check_notify);
120 //update documents table.
121 //$query = "UPDATE documents set document_version_id='$this->id' where id = '$this->document_id'";
122 //$this->db->query($query);
124 function get_summary_text()
126 return "$this->filename";
129 function retrieve($id, $encode=false){
130 $ret = parent::retrieve($id, $encode);
135 function is_authenticated()
137 return $this->authenticated;
140 function fill_in_additional_list_fields() {
141 $this->fill_in_additional_detail_fields();
144 function fill_in_additional_detail_fields()
147 global $current_language;
149 parent::fill_in_additional_detail_fields();
151 $mod_strings=return_module_language($current_language, 'Documents');
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);
158 $this->document_name = $row['document_name'];
159 $this->latest_revision = $row['revision'];
160 $this->latest_revision_id = $row['document_revision_id'];
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.
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";
172 //set default file name.
173 if (!empty($img_name) && file_exists($img_name)) {
174 $img_name = $img_name_bare;
177 $img_name = "def_image_inline"; //todo change the default image.
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>";
182 $this->file_url = "";
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
192 function getDocumentRevisionNameForDisplay($revId='') {
193 global $sugar_config;
194 global $current_language;
196 $localLabels = return_module_language($current_language, 'DocumentRevisions');
198 // prep - get source Document
199 if(!class_exists('Documents')) {
202 $document = new Document();
204 // use passed revision ID
206 $tempDoc = new DocumentRevision();
207 $tempDoc->retrieve($revId);
213 $document->retrieve($tempDoc->document_id);
214 $logicalName = $document->document_name;
216 // get revision string
218 if(!empty($tempDoc->revision)) {
219 $revString = "-{$localLabels['LBL_REVISION']}_{$tempDoc->revision}";
223 $realFilename = $tempDoc->filename;
224 $fileExtension_beg = strrpos($realFilename, ".");
227 if($fileExtension_beg > 0) {
228 $fileExtension = substr($realFilename, $fileExtension_beg + 1);
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
236 $fileExtension .= ".txt";
237 break; // no need to look for more
240 $fileExtension = ".".$fileExtension;
242 $return = $logicalName.$revString.$fileExtension;
244 // apply RFC limitations here
245 if(mb_strlen($return) > 1024) {
246 // do something if we find a real RFC issue
252 function fill_document_name_revision($doc_id) {
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);
260 $this->name = $row['document_name'];
261 $this->latest_revision = $row['revision'];
265 function list_view_parse_additional_sections(&$list_form, $xTemplateSection){
269 function get_list_view_data(){
270 $revision_fields = $this->get_list_view_array();
272 $forecast_fields['FILE_URL'] = $this->file_url;
273 return $revision_fields;
277 function get_document_revision_name($doc_revision_id){
278 if (empty($doc_revision_id)) return null;
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);
286 return $row['revision'];
293 function get_document_revisions($doc_id){
294 $return_array= Array();
295 if (empty($doc_id)) return $return_array;
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'];
305 return $return_array;