]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Notes/Note.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Notes / Note.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  TODO: To be written.
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46
47
48
49
50
51 require_once('include/upload_file.php');
52
53 // Note is used to store customer information.
54 class Note extends SugarBean {
55         var $field_name_map;
56         // Stored fields
57         var $id;
58         var $date_entered;
59         var $date_modified;
60         var $modified_user_id;
61         var $assigned_user_id;
62         var $created_by;
63         var $created_by_name;
64         var $modified_by_name;
65         var $description;
66         var $name;
67         var $filename;
68         // handle to an upload_file object
69         // used in emails
70         var $file;
71         var $embed_flag; // inline image flag
72         var $parent_type;
73         var $parent_id;
74         var $contact_id;
75         var $portal_flag;
76
77         var $parent_name;
78         var $contact_name;
79         var $contact_phone;
80         var $contact_email;
81         var $file_mime_type;
82         var $module_dir = "Notes";
83         var $default_note_name_dom = array('Meeting notes', 'Reminder');
84         var $table_name = "notes";
85         var $new_schema = true;
86         var $object_name = "Note";
87         var $importable = true;
88     
89         // This is used to retrieve related fields from form posts.
90         var $additional_column_fields = Array('contact_name', 'contact_phone', 'contact_email', 'parent_name','first_name','last_name');
91         
92         
93
94         function Note() {
95                 parent::SugarBean();
96         }
97         
98         function safeAttachmentName() {
99                 global $sugar_config;
100                 
101                 //get position of last "." in file name
102                 $file_ext_beg = strrpos($this->filename, ".");
103                 $file_ext = "";
104                 
105                 //get file extension
106                 if($file_ext_beg !== false) {
107                         $file_ext = substr($this->filename, $file_ext_beg + 1);
108                 }
109                 
110                 //check to see if this is a file with extension located in "badext"
111                 foreach($sugar_config['upload_badext'] as $badExt) {
112                         if(strtolower($file_ext) == strtolower($badExt)) {
113                                 //if found, then append with .txt and break out of lookup
114                                 $this->name = $this->name . ".txt";
115                                 $this->file_mime_type = 'text/';
116                                 $this->filename = $this->filename . ".txt";
117                                 break; // no need to look for more
118                         }
119                 }
120                 
121         }
122         
123         /**
124          * overrides SugarBean's method.
125          * If a system setting is set, it will mark all related notes as deleted, and attempt to delete files that are
126          * related to those notes
127          * @param string id ID
128          */
129         function mark_deleted($id) {
130                 global $sugar_config;
131                 
132                 if($this->parent_type == 'Emails') {
133                         if(isset($sugar_config['email_default_delete_attachments']) && $sugar_config['email_default_delete_attachments'] == true) {
134                                 $removeFile = getcwd()."/{$sugar_config['upload_dir']}{$id}";
135                                 if(file_exists($removeFile)) {
136                                         if(!unlink($removeFile)) {
137                                                 $GLOBALS['log']->error("*** Could not unlink() file: [ {$removeFile} ]");
138                                         }
139                                 }
140                         }
141                 }
142                 
143                 // delete note
144                 parent::mark_deleted($id);
145         }
146         
147         function deleteAttachment($isduplicate="false"){
148                 if($this->ACLAccess('edit')){
149                         if($isduplicate=="true"){
150                                 return true;
151                         }
152             $removeFile = clean_path(getAbsolutePath("{$GLOBALS['sugar_config']['upload_dir']}{$this->id}"));
153                 }
154                 if(file_exists($removeFile)) {
155                         if(!unlink($removeFile)) {
156                                 $GLOBALS['log']->error("*** Could not unlink() file: [ {$removeFile} ]");
157                         }else{
158                                 $this->filename = '';
159                                 $this->file_mime_type = ''; 
160                                 $this->file = '';
161                                 $this->save();
162                                 return true;
163                         }
164                 }
165                 return false;
166         }       
167
168
169         function get_summary_text() {
170                 return "$this->name";
171         }
172     
173     function create_export_query(&$order_by, &$where, $relate_link_join='')
174     {
175         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
176                 if($custom_join)
177                                 $custom_join['join'] .= $relate_link_join;
178                 $query = "SELECT notes.*, contacts.first_name, contacts.last_name ";
179
180                 if($custom_join) {
181                         $query .= $custom_join['select'];
182                 }
183         
184         $query .= " FROM notes ";
185                 
186                 $query .= "     LEFT JOIN contacts ON notes.contact_id=contacts.id ";
187         
188                 if($custom_join) {
189                         $query .= $custom_join['join'];
190                 }
191         
192                 $where_auto = " notes.deleted=0 AND (contacts.deleted IS NULL OR contacts.deleted=0)";
193                                         
194         if($where != "")
195                         $query .= "where $where AND ".$where_auto;
196         else
197                         $query .= "where ".$where_auto;
198
199         if($order_by != "")
200                         $query .=  " ORDER BY ". $this->process_order_by($order_by, null);
201         else
202                         $query .= " ORDER BY notes.name";
203
204                 return $query;
205         }
206
207         function fill_in_additional_list_fields() {
208                 $this->fill_in_additional_detail_fields();
209         }
210
211         function fill_in_additional_detail_fields() {
212                 parent::fill_in_additional_detail_fields();
213                 //TODO:  Seems odd we need to clear out these values so that list views don't show the previous rows value if current value is blank
214                 $this->getRelatedFields('Contacts', $this->contact_id, array('name'=>'contact_name', 'phone_work'=>'contact_phone') );
215                 if(!empty($this->contact_name)){
216                         
217                         $emailAddress = new SugarEmailAddress();
218                         $this->contact_email = $emailAddress->getPrimaryAddress(false, 'Contacts', $this->contact_id);
219                 }
220                 
221                 if(isset($this->contact_id) && $this->contact_id != '') {
222                     $contact = new Contact();
223                     $contact->retrieve($this->contact_id);
224                     if(isset($contact->id)) {
225                         $this->contact_name = $contact->full_name;
226                     }
227                 }
228         }
229
230         
231         function get_list_view_data() 
232         {
233                 $note_fields = $this->get_list_view_array();
234                 global $app_list_strings, $focus, $action, $currentModule,$mod_strings, $sugar_config;
235                 
236                 if(isset($this->parent_type)) {
237                         $note_fields['PARENT_MODULE'] = $this->parent_type;
238                 }
239
240                 if(!isset($this->filename) || $this->filename != ''){  
241             $file_path = UploadFile::get_file_path($this->filename,$this->id);
242     
243             if(file_exists($file_path)){
244                 $save_file = urlencode(basename(UploadFile::get_url($this->filename,$this->id)));
245                 $note_fields['FILENAME'] = $this->filename; 
246                 $note_fields['FILE_URL'] = "index.php?entryPoint=download&id=".$save_file."&type=Notes";
247             }
248         }
249         if(isset($this->contact_id) && $this->contact_id != '') {
250                         $contact = new Contact();
251                         $contact->retrieve($this->contact_id);
252                         if(isset($contact->id)) {
253                             $this->contact_name = $contact->full_name;
254                         }
255                 }
256         if(isset($this->contact_name)){
257                 $note_fields['CONTACT_NAME'] = $this->contact_name; 
258         }
259
260                 global $current_language;
261                 $mod_strings = return_module_language($current_language, 'Notes');
262                 $note_fields['STATUS']=$mod_strings['LBL_NOTE_STATUS'];
263
264
265                 return $note_fields;
266         }
267         
268         function listviewACLHelper() {
269                 $array_assign = parent::listviewACLHelper();
270                 $is_owner = false;
271                 if(!empty($this->parent_name)) {
272                         if(!empty($this->parent_name_owner)) {
273                                 global $current_user;
274                                 $is_owner = $current_user->id == $this->parent_name_owner;
275                         }
276                 }
277                         
278                 if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)) {
279                         $array_assign['PARENT'] = 'a';
280                 } else {
281                         $array_assign['PARENT'] = 'span';
282                 }
283                 
284                 $is_owner = false;
285                 if(!empty($this->contact_name)) {
286                         if(!empty($this->contact_name_owner)) {
287                                 global $current_user;
288                                 $is_owner = $current_user->id == $this->contact_name_owner;
289                         }
290                 }
291                         
292                 if( ACLController::checkAccess('Contacts', 'view', $is_owner)) {
293                         $array_assign['CONTACT'] = 'a';
294                 } else {
295                         $array_assign['CONTACT'] = 'span';
296                 }
297                 
298                 return $array_assign;
299         }
300         
301         function bean_implements($interface) {
302                 switch($interface) {
303                         case 'ACL':return true;
304                 }
305                 return false;
306         }
307
308 }
309 ?>