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-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 {
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;
80 /*var $column_fields = Array("id"
91 var $encodeFields = Array();
93 // This is used to retrieve related fields from form posts.
94 var $additional_column_fields = Array('');
96 // This is the list of fields that are in the lists.
97 var $list_fields = Array("id"
110 var $required_fields = Array("revision");
114 function DocumentRevision() {
116 $this->setupCustomFields('DocumentRevisions'); //parameter is module name
117 $this->disable_row_level_security =true; //no direct access to this module.
120 function save($check_notify = false){
121 $saveRet = parent::save($check_notify);
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'];
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);
136 function get_summary_text()
138 return "$this->filename";
141 function retrieve($id, $encode=false){
142 $ret = parent::retrieve($id, $encode);
147 function is_authenticated()
149 return $this->authenticated;
152 function fill_in_additional_list_fields() {
153 $this->fill_in_additional_detail_fields();
156 function fill_in_additional_detail_fields()
159 global $current_language;
161 parent::fill_in_additional_detail_fields();
163 if ( empty($this->id) && empty($this->document_id) && isset($_REQUEST['return_id']) && !empty($_REQUEST['return_id']) ) {
164 $this->document_id = $_REQUEST['return_id'];
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);
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'];
177 if ( empty($this->revision) ) {
178 $this->revision = $this->latest_revision + 1;
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
189 function getDocumentRevisionNameForDisplay($revId='') {
190 global $sugar_config;
191 global $current_language;
193 $localLabels = return_module_language($current_language, 'DocumentRevisions');
195 // prep - get source Document
196 $document = new Document();
198 // use passed revision ID
200 $tempDoc = new DocumentRevision();
201 $tempDoc->retrieve($revId);
207 $document->retrieve($tempDoc->document_id);
208 $logicalName = $document->document_name;
210 // get revision string
212 if(!empty($tempDoc->revision)) {
213 $revString = "-{$localLabels['LBL_REVISION']}_{$tempDoc->revision}";
217 $realFilename = $tempDoc->filename;
218 $fileExtension_beg = strrpos($realFilename, ".");
221 if($fileExtension_beg > 0) {
222 $fileExtension = substr($realFilename, $fileExtension_beg + 1);
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
230 $fileExtension .= ".txt";
231 break; // no need to look for more
234 $fileExtension = ".".$fileExtension;
236 $return = $logicalName.$revString.$fileExtension;
238 // apply RFC limitations here
239 if(mb_strlen($return) > 1024) {
240 // do something if we find a real RFC issue
246 function fill_document_name_revision($doc_id) {
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);
254 $this->name = $row['document_name'];
255 $this->latest_revision = $row['revision'];
259 function list_view_parse_additional_sections(&$list_form, $xTemplateSection){
263 function get_list_view_data(){
264 $revision_fields = $this->get_list_view_array();
266 $forecast_fields['FILE_URL'] = $this->file_url;
267 return $revision_fields;
271 function get_document_revision_name($doc_revision_id){
272 if (empty($doc_revision_id)) return null;
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);
280 return $row['revision'];
287 function get_document_revisions($doc_id){
288 $return_array= Array();
289 if (empty($doc_id)) return $return_array;
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'];
299 return $return_array;
303 require_once('modules/Documents/DocumentExternalApiDropDown.php');