]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Documents/Document.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Documents / Document.php
1 <?php
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.
7  * 
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.
14  * 
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
18  * details.
19  * 
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
23  * 02110-1301 USA.
24  * 
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.
27  * 
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.
31  * 
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  ********************************************************************************/
38
39 require_once ('include/upload_file.php');
40
41
42 // User is used to store Forecast information.
43 class Document extends SugarBean {
44
45         var $id;
46         var $document_name;
47         var $description;
48         var $category_id;
49         var $subcategory_id;
50         var $status_id;
51         var $status;
52         var $created_by;
53         var $date_entered;
54         var $date_modified;
55         var $modified_user_id;
56         var $active_date;
57         var $exp_date;
58         var $document_revision_id;
59         var $filename;
60
61         var $img_name;
62         var $img_name_bare;
63         var $related_doc_id;
64         var $related_doc_name;
65         var $related_doc_rev_id;
66         var $related_doc_rev_number;
67         var $is_template;
68         var $template_type;
69
70         //additional fields.
71         var $revision;
72         var $last_rev_create_date;
73         var $last_rev_created_by;
74         var $last_rev_created_name;
75         var $file_url;
76         var $file_url_noimage;
77
78         var $table_name = "documents";
79         var $object_name = "Document";
80         var $user_preferences;
81
82         var $encodeFields = Array ();
83
84         // This is used to retrieve related fields from form posts.
85         var $additional_column_fields = Array ('revision');
86         
87         var $new_schema = true;
88         var $module_dir = 'Documents';
89         
90         var $save_file;
91
92         var $relationship_fields = Array(
93                 'contract_id'=>'contracts',
94          );
95           
96
97         function Document() {
98                 parent :: SugarBean();
99                 $this->setupCustomFields('Documents'); //parameter is module name
100                 $this->disable_row_level_security = false;
101         }
102
103         function save($check_notify = false) {
104                 if (empty($this->id) || $this->new_with_id)
105                 {
106                         if (!empty($this->uploadfile))
107                         {
108                                 $this->filename = $this->uploadfile;
109                                 if (empty($this->id)) { 
110                                         $this->id = create_guid();
111                                         $this->new_with_id = true;
112                                 }
113                                 $Revision = new DocumentRevision();
114                                 //save revision.
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;
121                                 $Revision->save();
122                                 
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));
125                                 
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;    
129                         }
130                         
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);
136                         }
137                     
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);
141                         }
142                 }
143         
144                 return parent :: save($check_notify);
145         }
146         function get_summary_text() {
147                 return "$this->document_name";
148         }
149
150         function is_authenticated() {
151                 return $this->authenticated;
152         }
153
154         function fill_in_additional_list_fields() {
155                 $this->fill_in_additional_detail_fields();
156         }
157
158         function fill_in_additional_detail_fields() {
159                 global $theme;
160                 global $current_language;
161                 global $timedate;
162                 global $locale;
163
164                 parent::fill_in_additional_detail_fields();
165                 
166                 $mod_strings = return_module_language($current_language, 'Documents');
167
168                 $query = "SELECT filename,revision,file_ext FROM document_revisions WHERE id='$this->document_revision_id'";
169
170                 $result = $this->db->query($query);
171                 $row = $this->db->fetchByAssoc($result);
172
173                 //popuplate filename
174         if(isset($row['filename']))$this->filename = $row['filename'];
175         //$this->latest_revision = $row['revision'];
176         if(isset($row['revision']))$this->revision = $row['revision'];
177         
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.
181                 global $img_name;
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";
186                 }
187
188                 //set default file name.
189                 if (!empty ($img_name) && file_exists($img_name)) {
190                         $img_name = $img_name_bare;
191                 } else {
192                         $img_name = "def_image_inline"; //todo change the default image.                                                
193                 }
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));
197                 }else{
198             $this->file_url = "";
199             $this->file_url_noimage = "";
200                 }
201                 
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);
206                 if (!empty ($row)) {
207                         $this->last_rev_created_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
208
209                         $this->last_rev_create_date = $timedate->to_display_date_time($row['rev_date']);
210                 }
211                 
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];
216             }
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);
220         
221         }
222
223         function list_view_parse_additional_sections(& $list_form, $xTemplateSection) {
224                 return $list_form;
225         }
226
227     function create_export_query(&$order_by, &$where, $relate_link_join='')
228     {
229         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
230                 if($custom_join)
231                                 $custom_join['join'] .= $relate_link_join;
232                 $query = "SELECT
233                                                 documents.*";
234                 if($custom_join){
235                         $query .=  $custom_join['select'];
236                 }
237                 $query .= " FROM documents ";
238                 if($custom_join){
239                         $query .=  $custom_join['join'];
240                 }
241
242                 $where_auto = " documents.deleted = 0";
243
244                 if ($where != "")
245                         $query .= " WHERE $where AND ".$where_auto;
246                 else
247                         $query .= " WHERE ".$where_auto;
248
249                 if ($order_by != "")
250                         $query .= " ORDER BY $order_by";
251                 else
252                         $query .= " ORDER BY documents.document_name";
253
254                 return $query;
255         }
256
257         function get_list_view_data() {
258                 global $current_language;
259                 $app_list_strings = return_app_list_strings_language($current_language);
260
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];
267
268                 $document_fields['DOCUMENT_NAME_JAVASCRIPT'] = $GLOBALS['db']->helper->escape_quote($document_fields['DOCUMENT_NAME']);
269                 return $document_fields;
270         }
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.
274         }
275
276         function bean_implements($interface) {
277                 switch ($interface) {
278                         case 'ACL' :
279                                 return true;
280                 }
281                 return false;
282         }
283         
284         //static function.
285         function get_document_name($doc_id){
286                 if (empty($doc_id)) return null;
287                 
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);
293                         if (!empty($row)) {
294                                 return $row['document_name'];
295                         }
296                 }
297                 return null;
298         }
299 }
300 ?>