]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Documents/Document.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Documents / Document.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry)
3         die('Not A Valid Entry Point');
4 /*********************************************************************************
5  * SugarCRM Community Edition 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 $assigned_user_id;
57         var $active_date;
58         var $exp_date;
59         var $document_revision_id;
60         var $filename;
61         var $doc_type;
62
63         var $img_name;
64         var $img_name_bare;
65         var $related_doc_id;
66         var $related_doc_name;
67         var $related_doc_rev_id;
68         var $related_doc_rev_number;
69         var $is_template;
70         var $template_type;
71
72         //additional fields.
73         var $revision;
74         var $last_rev_create_date;
75         var $last_rev_created_by;
76         var $last_rev_created_name;
77         var $file_url;
78         var $file_url_noimage;
79
80         var $table_name = "documents";
81         var $object_name = "Document";
82         var $user_preferences;
83
84         var $encodeFields = Array ();
85
86         // This is used to retrieve related fields from form posts.
87         var $additional_column_fields = Array ('revision');
88
89         var $new_schema = true;
90         var $module_dir = 'Documents';
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
105         if (empty($this->doc_type)) {
106                         $this->doc_type = 'Sugar';
107                 }
108         if (empty($this->id) || $this->new_with_id)
109                 {
110             if (empty($this->id)) {
111                 $this->id = create_guid();
112                 $this->new_with_id = true;
113             }
114
115             if ( isset($_REQUEST) && isset($_REQUEST['duplicateSave']) && $_REQUEST['duplicateSave'] == true && isset($_REQUEST['filename_old_doctype']) ) {
116                 $this->doc_type = $_REQUEST['filename_old_doctype'];
117                 $isDuplicate = true;
118             } else {
119                 $isDuplicate = false;
120             }
121
122             $Revision = new DocumentRevision();
123             //save revision.
124             $Revision->in_workflow = true;
125             $Revision->not_use_rel_in_req = true;
126             $Revision->new_rel_id = $this->id;
127             $Revision->new_rel_relname = 'Documents';
128             $Revision->change_log = translate('DEF_CREATE_LOG','Documents');
129             $Revision->revision = $this->revision;
130             $Revision->document_id = $this->id;
131             $Revision->filename = $this->filename;
132
133             if(isset($this->file_ext))
134             {
135                 $Revision->file_ext = $this->file_ext;
136             }
137
138             if(isset($this->file_mime_type))
139             {
140                 $Revision->file_mime_type = $this->file_mime_type;
141             }
142
143             $Revision->doc_type = $this->doc_type;
144             if ( isset($this->doc_id) ) {
145                 $Revision->doc_id = $this->doc_id;
146             }
147             if ( isset($this->doc_url) ) {
148                 $Revision->doc_url = $this->doc_url;
149             }
150
151             $Revision->id = create_guid();
152             $Revision->new_with_id = true;
153
154             $createRevision = false;
155             //Move file saved during populatefrompost to match the revision id rather than document id
156             if (!empty($_FILES['filename_file'])) {
157                 rename("upload://{$this->id}", "upload://{$Revision->id}");
158                 $createRevision = true;
159             } else if ( $isDuplicate && ( empty($this->doc_type) || $this->doc_type == 'Sugar' ) ) {
160                 // Looks like we need to duplicate a file, this is tricky
161                 $oldDocument = new Document();
162                 $oldDocument->retrieve($_REQUEST['duplicateId']);
163                 $old_name = "upload://{$oldDocument->document_revision_id}";
164                 $new_name = "upload://{$Revision->id}";
165                 $GLOBALS['log']->debug("Attempting to copy from $old_name to $new_name");
166                 copy($old_name, $new_name);
167                 $createRevision = true;
168             }
169
170             // For external documents, we just need to make sure we have a doc_id
171             if ( !empty($this->doc_id) && $this->doc_type != 'Sugar' ) {
172                 $createRevision = true;
173             }
174
175             if ( $createRevision ) {
176                 $Revision->save();
177                 //update document with latest revision id
178                 $this->process_save_dates=false; //make sure that conversion does not happen again.
179                 $this->document_revision_id = $Revision->id;
180             }
181
182
183             //set relationship field values if contract_id is passed (via subpanel create)
184             if (!empty($_POST['contract_id'])) {
185                 $save_revision['document_revision_id']=$this->document_revision_id;
186                 $this->load_relationship('contracts');
187                 $this->contracts->add($_POST['contract_id'],$save_revision);
188             }
189
190             if ((isset($_POST['load_signed_id']) and !empty($_POST['load_signed_id']))) {
191                 $query="update linked_documents set deleted=1 where id='".$_POST['load_signed_id']."'";
192                 $this->db->query($query);
193             }
194         }
195
196                 return parent :: save($check_notify);
197         }
198         function get_summary_text() {
199                 return "$this->document_name";
200         }
201
202         function is_authenticated() {
203                 return $this->authenticated;
204         }
205
206         function fill_in_additional_list_fields() {
207                 $this->fill_in_additional_detail_fields();
208         }
209
210         function fill_in_additional_detail_fields() {
211                 global $theme;
212                 global $current_language;
213                 global $timedate;
214                 global $locale;
215
216                 parent::fill_in_additional_detail_fields();
217
218                 $mod_strings = return_module_language($current_language, 'Documents');
219
220         if (!empty($this->document_revision_id)) {
221
222             $query = "SELECT users.first_name AS first_name, users.last_name AS last_name, document_revisions.date_entered AS rev_date,
223                  document_revisions.filename AS filename, document_revisions.revision AS revision,
224                  document_revisions.file_ext AS file_ext, document_revisions.file_mime_type AS file_mime_type
225                  FROM users, document_revisions
226                  WHERE users.id = document_revisions.created_by AND document_revisions.id = '$this->document_revision_id'";
227
228             $result = $this->db->query($query);
229             $row = $this->db->fetchByAssoc($result);
230
231             //populate name
232             if(isset($this->document_name))
233             {
234                 $this->name = $this->document_name;
235             }
236
237             if(isset($row['filename']))$this->filename = $row['filename'];
238             //$this->latest_revision = $row['revision'];
239             if(isset($row['revision']))$this->revision = $row['revision'];
240
241             //image is selected based on the extension name <ext>_icon_inline, extension is stored in document_revisions.
242             //if file is not found then default image file will be used.
243             global $img_name;
244             global $img_name_bare;
245             if (!empty ($row['file_ext'])) {
246                 $img_name = SugarThemeRegistry::current()->getImageURL(strtolower($row['file_ext'])."_image_inline.gif");
247                 $img_name_bare = strtolower($row['file_ext'])."_image_inline";
248             }
249         }
250
251                 //set default file name.
252                 if (!empty ($img_name) && file_exists($img_name)) {
253                         $img_name = $img_name_bare;
254                 } else {
255                         $img_name = "def_image_inline"; //todo change the default image.
256                 }
257                 if($this->ACLAccess('DetailView')) {
258                         if(!empty($this->doc_type) && $this->doc_type != 'Sugar' && !empty($this->doc_url)) {
259                 $file_url= "<a href='".$this->doc_url."' target='_blank'>".SugarThemeRegistry::current()->getImage($this->doc_type.'_image_inline', 'border="0"',null,null,'.png',$mod_strings['LBL_LIST_VIEW_DOCUMENT'])."</a>";
260                         } else {
261                             $file_url = "<a href='index.php?entryPoint=download&id={$this->document_revision_id}&type=Documents' target='_blank'>".SugarThemeRegistry::current()->getImage($img_name, 'border="0"', null,null,'.gif',$mod_strings['LBL_LIST_VIEW_DOCUMENT'])."</a>";
262                         }
263
264                 $this->file_url = $file_url;
265                 $this->file_url_noimage = "index.php?entryPoint=download&type=Documents&id={$this->document_revision_id}";
266                 }else{
267             $this->file_url = "";
268             $this->file_url_noimage = "";
269                 }
270
271                 //get last_rev_by user name.
272                 if (!empty ($row)) {
273                         $this->last_rev_created_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name']);
274
275                         $this->last_rev_create_date = $timedate->to_display_date_time($this->db->fromConvert($row['rev_date'], 'datetime'));
276                         $this->last_rev_mime_type = $row['file_mime_type'];
277                 }
278
279                 global $app_list_strings;
280             if(!empty($this->status_id)) {
281                //_pp($this->status_id);
282                $this->status = $app_list_strings['document_status_dom'][$this->status_id];
283             }
284         if (!empty($this->related_doc_id)) {
285             $this->related_doc_name = Document::get_document_name($this->related_doc_id);
286             $this->related_doc_rev_number = DocumentRevision::get_document_revision_name($this->related_doc_rev_id);
287         }
288         }
289
290         function list_view_parse_additional_sections(& $list_form, $xTemplateSection) {
291                 return $list_form;
292         }
293
294     function create_export_query(&$order_by, &$where, $relate_link_join='')
295     {
296         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
297                 if($custom_join)
298                                 $custom_join['join'] .= $relate_link_join;
299                 $query = "SELECT
300                                                 documents.*";
301                 if($custom_join){
302                         $query .=  $custom_join['select'];
303                 }
304                 $query .= " FROM documents ";
305                 if($custom_join){
306                         $query .=  $custom_join['join'];
307                 }
308
309                 $where_auto = " documents.deleted = 0";
310
311                 if ($where != "")
312                         $query .= " WHERE $where AND ".$where_auto;
313                 else
314                         $query .= " WHERE ".$where_auto;
315
316                 if ($order_by != "")
317                         $query .= " ORDER BY $order_by";
318                 else
319                         $query .= " ORDER BY documents.document_name";
320
321                 return $query;
322         }
323
324         function get_list_view_data() {
325                 global $current_language;
326                 $app_list_strings = return_app_list_strings_language($current_language);
327
328                 $document_fields = $this->get_list_view_array();
329
330         $this->fill_in_additional_list_fields();
331
332
333                 $document_fields['FILENAME'] = $this->filename;
334                 $document_fields['FILE_URL'] = $this->file_url;
335                 $document_fields['FILE_URL_NOIMAGE'] = $this->file_url_noimage;
336                 $document_fields['LAST_REV_CREATED_BY'] = $this->last_rev_created_name;
337                 $document_fields['CATEGORY_ID'] = empty ($this->category_id) ? "" : $app_list_strings['document_category_dom'][$this->category_id];
338                 $document_fields['SUBCATEGORY_ID'] = empty ($this->subcategory_id) ? "" : $app_list_strings['document_subcategory_dom'][$this->subcategory_id];
339         $document_fields['NAME'] = $this->document_name;
340                 $document_fields['DOCUMENT_NAME_JAVASCRIPT'] = $GLOBALS['db']->quote($document_fields['DOCUMENT_NAME']);
341                 return $document_fields;
342         }
343
344
345     /**
346      * mark_relationships_deleted
347      *
348      * Override method from SugarBean to handle deleting relationships associated with a Document.  This method will
349      * remove DocumentRevision relationships and then optionally delete Contracts depending on the version.
350      *
351      * @param $id String The record id of the Document instance
352      */
353         function mark_relationships_deleted($id)
354     {
355         $this->load_relationships('revisions');
356         $revisions= $this->get_linked_beans('revisions','DocumentRevision');
357
358         if (!empty($revisions) && is_array($revisions)) {
359                 foreach($revisions as $key=>$version) {
360                         UploadFile::unlink_file($version->id,$version->filename);
361                         //mark the version deleted.
362                         $version->mark_deleted($version->id);
363                 }
364         }
365
366         }
367
368
369         function bean_implements($interface) {
370                 switch ($interface) {
371                         case 'ACL' :
372                                 return true;
373                 }
374                 return false;
375         }
376
377         //static function.
378         function get_document_name($doc_id){
379                 if (empty($doc_id)) return null;
380
381                 $db = DBManagerFactory::getInstance();
382                 $query="select document_name from documents where id='$doc_id'";
383                 $result=$db->query($query);
384                 if (!empty($result)) {
385                         $row=$db->fetchByAssoc($result);
386                         if (!empty($row)) {
387                                 return $row['document_name'];
388                         }
389                 }
390                 return null;
391         }
392 }
393
394 require_once('modules/Documents/DocumentExternalApiDropDown.php');
395