2 if(!defined('sugarEntry') || !sugarEntry)
3 die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
39 require_once ('include/upload_file.php');
42 // User is used to store Forecast information.
43 class Document extends SugarBean {
55 var $modified_user_id;
58 var $document_revision_id;
64 var $related_doc_name;
65 var $related_doc_rev_id;
66 var $related_doc_rev_number;
72 var $last_rev_create_date;
73 var $last_rev_created_by;
74 var $last_rev_created_name;
76 var $file_url_noimage;
78 var $table_name = "documents";
79 var $object_name = "Document";
80 var $user_preferences;
82 var $encodeFields = Array ();
84 // This is used to retrieve related fields from form posts.
85 var $additional_column_fields = Array ('revision');
87 var $new_schema = true;
88 var $module_dir = 'Documents';
92 var $relationship_fields = Array(
93 'contract_id'=>'contracts',
98 parent :: SugarBean();
99 $this->setupCustomFields('Documents'); //parameter is module name
100 $this->disable_row_level_security = false;
103 function save($check_notify = false) {
104 if (empty($this->id) || $this->new_with_id)
106 if (!empty($this->uploadfile))
108 $this->filename = $this->uploadfile;
109 if (empty($this->id)) {
110 $this->id = create_guid();
111 $this->new_with_id = true;
113 $Revision = new DocumentRevision();
115 $Revision->change_log = translate('DEF_CREATE_LOG','Documents');
116 $Revision->revision = $this->revision;
117 $Revision->document_id = $this->id;
118 $Revision->filename = $this->filename;
119 $Revision->file_ext = $this->file_ext;
120 $Revision->file_mime_type = $this->file_mime_type;
123 //Move file saved during populatefrompost to match the revision id rather than document id
124 rename(UploadFile :: get_url($this->filename, $this->id), UploadFile :: get_url($this->filename, $Revision->id));
126 //update document with latest revision id
127 $this->process_save_dates=false; //make sure that conversion does not happen again.
128 $this->document_revision_id = $Revision->id;
131 //set relationship field values if contract_id is passed (via subpanel create)
132 if (!empty($_POST['contract_id'])) {
133 $save_revision['document_revision_id']=$this->document_revision_id;
134 $this->load_relationship('contracts');
135 $this->contracts->add($_POST['contract_id'],$save_revision);
138 if ((isset($_POST['load_signed_id']) and !empty($_POST['load_signed_id']))) {
139 $query="update linked_documents set deleted=1 where id='".$_POST['load_signed_id']."'";
140 $this->db->query($query);
144 return parent :: save($check_notify);
146 function get_summary_text() {
147 return "$this->document_name";
150 function is_authenticated() {
151 return $this->authenticated;
154 function fill_in_additional_list_fields() {
155 $this->fill_in_additional_detail_fields();
158 function fill_in_additional_detail_fields() {
160 global $current_language;
164 parent::fill_in_additional_detail_fields();
166 $mod_strings = return_module_language($current_language, 'Documents');
168 $query = "SELECT filename,revision,file_ext FROM document_revisions WHERE id='$this->document_revision_id'";
170 $result = $this->db->query($query);
171 $row = $this->db->fetchByAssoc($result);
174 if(isset($row['filename']))$this->filename = $row['filename'];
175 //$this->latest_revision = $row['revision'];
176 if(isset($row['revision']))$this->revision = $row['revision'];
178 //populate the file url.
179 //image is selected based on the extension name <ext>_icon_inline, extension is stored in document_revisions.
180 //if file is not found then default image file will be used.
182 global $img_name_bare;
183 if (!empty ($row['file_ext'])) {
184 $img_name = SugarThemeRegistry::current()->getImageURL(strtolower($row['file_ext'])."_image_inline.gif");
185 $img_name_bare = strtolower($row['file_ext'])."_image_inline";
188 //set default file name.
189 if (!empty ($img_name) && file_exists($img_name)) {
190 $img_name = $img_name_bare;
192 $img_name = "def_image_inline"; //todo change the default image.
194 if($this->ACLAccess('DetailView')){
195 $this->file_url = "<a href='index.php?entryPoint=download&id=".basename(UploadFile :: get_url($this->filename, $this->document_revision_id))."&type=Documents' target='_blank'>".SugarThemeRegistry::current()->getImage($img_name, 'alt="'.$mod_strings['LBL_LIST_VIEW_DOCUMENT'].'" border="0"')."</a>";
196 $this->file_url_noimage = basename(UploadFile :: get_url($this->filename, $this->document_revision_id));
198 $this->file_url = "";
199 $this->file_url_noimage = "";
202 //get last_rev_by user name.
203 $query = "SELECT first_name,last_name, document_revisions.date_entered as rev_date FROM users, document_revisions WHERE users.id = document_revisions.created_by and document_revisions.id = '$this->document_revision_id'";
204 $result = $this->db->query($query, true, "Eror fetching user name: ");
205 $row = $this->db->fetchByAssoc($result);
207 $this->last_rev_created_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
209 $this->last_rev_create_date = $timedate->to_display_date_time($row['rev_date']);
212 global $app_list_strings;
213 if(!empty($this->status_id)) {
214 //_pp($this->status_id);
215 $this->status = $app_list_strings['document_status_dom'][$this->status_id];
217 $this->related_doc_name = Document::get_document_name($this->related_doc_id);
218 $this->related_doc_rev_number = DocumentRevision::get_document_revision_name($this->related_doc_rev_id);
219 $this->save_file = basename($this->file_url_noimage);
223 function list_view_parse_additional_sections(& $list_form, $xTemplateSection) {
227 function create_export_query(&$order_by, &$where, $relate_link_join='')
229 $custom_join = $this->custom_fields->getJOIN(true, true,$where);
231 $custom_join['join'] .= $relate_link_join;
235 $query .= $custom_join['select'];
237 $query .= " FROM documents ";
239 $query .= $custom_join['join'];
242 $where_auto = " documents.deleted = 0";
245 $query .= " WHERE $where AND ".$where_auto;
247 $query .= " WHERE ".$where_auto;
250 $query .= " ORDER BY $order_by";
252 $query .= " ORDER BY documents.document_name";
257 function get_list_view_data() {
258 global $current_language;
259 $app_list_strings = return_app_list_strings_language($current_language);
261 $document_fields = $this->get_list_view_array();
262 $document_fields['FILE_URL'] = $this->file_url;
263 $document_fields['FILE_URL_NOIMAGE'] = $this->file_url_noimage;
264 $document_fields['LAST_REV_CREATED_BY'] = $this->last_rev_created_name;
265 $document_fields['CATEGORY_ID'] = empty ($this->category_id) ? "" : $app_list_strings['document_category_dom'][$this->category_id];
266 $document_fields['SUBCATEGORY_ID'] = empty ($this->subcategory_id) ? "" : $app_list_strings['document_subcategory_dom'][$this->subcategory_id];
268 $document_fields['DOCUMENT_NAME_JAVASCRIPT'] = $GLOBALS['db']->helper->escape_quote($document_fields['DOCUMENT_NAME']);
269 return $document_fields;
271 function mark_relationships_deleted($id) {
272 //do nothing, this call is here to avoid default delete processing since
273 //delete.php handles deletion of document revisions.
276 function bean_implements($interface) {
277 switch ($interface) {
285 function get_document_name($doc_id){
286 if (empty($doc_id)) return null;
288 $db = DBManagerFactory::getInstance();
289 $query="select document_name from documents where id='$doc_id'";
290 $result=$db->query($query);
291 if (!empty($result)) {
292 $row=$db->fetchByAssoc($result);
294 return $row['document_name'];