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