]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Tasks/Task.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Tasks / Task.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
51
52 // Task is used to store customer information.
53 class Task extends SugarBean {
54         var $field_name_map;
55
56         // Stored fields
57         var $id;
58         var $date_entered;
59         var $date_modified;
60         var $assigned_user_id;
61         var $modified_user_id;
62         var $created_by;
63         var $created_by_name;
64         var $modified_by_name;
65         var $description;
66         var $name;
67         var $status;
68         var $date_due_flag;
69         var $date_due;
70         var $time_due;
71         var $date_start_flag;
72         var $date_start;
73         var $time_start;
74         var $priority;
75         var $parent_type;
76         var $parent_id;
77         var $contact_id;
78
79         var $parent_name;
80         var $contact_name;
81         var $contact_phone;
82         var $contact_email;
83         var $assigned_user_name;
84
85 //bug 28138 todo
86 //      var $default_task_name_values = array('Assemble catalogs', 'Make travel arrangements', 'Send a letter', 'Send contract', 'Send fax', 'Send a follow-up letter', 'Send literature', 'Send proposal', 'Send quote', 'Call to schedule meeting', 'Setup evaluation', 'Get demo feedback', 'Arrange introduction', 'Escalate support request', 'Close out support request', 'Ship product', 'Arrange reference call', 'Schedule training', 'Send local user group information', 'Add to mailing list');
87
88         var $table_name = "tasks";
89
90         var $object_name = "Task";
91         var $module_dir = 'Tasks';
92
93         var $importable = true;
94         // This is used to retrieve related fields from form posts.
95         var $additional_column_fields = Array('assigned_user_name', 'assigned_user_id', 'contact_name', 'contact_phone', 'contact_email', 'parent_name');
96
97
98         function Task() {
99                 parent::SugarBean();
100         }
101
102         var $new_schema = true;
103
104     function save($check_notify = FALSE)
105     {
106         if (empty($this->status) ) {
107             $this->status = $this->getDefaultStatus();
108         }
109         return parent::save($check_notify);
110     }
111
112         function get_summary_text()
113         {
114                 return "$this->name";
115         }
116
117     function create_export_query(&$order_by, &$where, $relate_link_join='')
118     {
119         $custom_join = $this->custom_fields->getJOIN(true, true,$where);
120                 if($custom_join)
121                                 $custom_join['join'] .= $relate_link_join;
122                 $contact_required = stristr($where,"contacts");
123                 if($contact_required)
124                 {
125                         $query = "SELECT tasks.*, contacts.first_name, contacts.last_name";
126                         if($custom_join){
127                                                         $query .= $custom_join['select'];
128                                                 }
129                         $query .= " FROM contacts, tasks ";
130                         $where_auto = "tasks.contact_id = contacts.id AND tasks.deleted=0 AND contacts.deleted=0";
131                 }
132                 else
133                 {
134                         $query = 'SELECT tasks.*';
135                         if($custom_join){
136                                                         $query .= $custom_join['select'];
137                                                 }
138                         $query .= ' FROM tasks ';
139                         $where_auto = "tasks.deleted=0";
140                 }
141
142
143                                 if($custom_join){
144                                         $query .= $custom_join['join'];
145                                 }
146
147                 if($where != "")
148                         $query .= "where $where AND ".$where_auto;
149                 else
150                         $query .= "where ".$where_auto;
151
152                 if($order_by != "")
153                         $query .=  " ORDER BY ". $this->process_order_by($order_by, null);
154                 else
155                         $query .= " ORDER BY tasks.name";
156                 return $query;
157
158         }
159
160
161
162         function fill_in_additional_list_fields()
163         {
164
165         }
166
167         function fill_in_additional_detail_fields()
168         {
169         parent::fill_in_additional_detail_fields();
170                 global $app_strings;
171
172                 if (isset($this->contact_id)) {
173
174                         $contact = new Contact();
175                         $contact->retrieve($this->contact_id);
176
177                         if($contact->id != "") {
178                                 $this->contact_name = $contact->full_name;
179                                 $this->contact_name_owner = $contact->assigned_user_id;
180                                 $this->contact_name_mod = 'Contacts';
181                                 $this->contact_phone = $contact->phone_work;
182                                 $this->contact_email = $contact->emailAddress->getPrimaryAddress($contact);
183                         } else {
184                                 $this->contact_name_mod = '';
185                                 $this->contact_name_owner = '';
186                                 $this->contact_name='';
187                                 $this->contact_email = '';
188                                 $this->contact_id='';
189                         }
190
191                 }
192
193                 $this->fill_in_additional_parent_fields();
194         }
195
196         function fill_in_additional_parent_fields()
197         {
198
199                 $this->parent_name = '';
200                 global $app_strings, $beanFiles, $beanList, $locale;
201                 if ( ! isset($beanList[$this->parent_type]))
202                 {
203                         $this->parent_name = '';
204                         return;
205                 }
206
207             $beanType = $beanList[$this->parent_type];
208                 require_once($beanFiles[$beanType]);
209                 $parent = new $beanType();
210
211                 if (is_subclass_of($parent, 'Person')) {
212                         $query = "SELECT first_name, last_name, assigned_user_id parent_name_owner from $parent->table_name where id = '$this->parent_id'";
213                 }
214                 else if (is_subclass_of($parent, 'File')) {
215                         $query = "SELECT document_name, assigned_user_id parent_name_owner from $parent->table_name where id = '$this->parent_id'";
216                 }
217                 else {
218
219                         $query = "SELECT name ";
220                         if(isset($parent->field_defs['assigned_user_id'])){
221                                 $query .= " , assigned_user_id parent_name_owner ";
222                         }else{
223                                 $query .= " , created_by parent_name_owner ";
224                         }
225                         $query .= " from $parent->table_name where id = '$this->parent_id'";
226                 }
227                 $result = $this->db->query($query,true," Error filling in additional detail fields: ");
228
229                 // Get the id and the name.
230                 $row = $this->db->fetchByAssoc($result);
231
232                 if ($row && !empty($row['parent_name_owner'])){
233                         $this->parent_name_owner = $row['parent_name_owner'];
234                         $this->parent_name_mod = $this->parent_type;
235                 }
236                 if (is_subclass_of($parent, 'Person') and $row != null)
237                 {
238                         $this->parent_name = $locale->getLocaleFormattedName(stripslashes($row['first_name']), stripslashes($row['last_name']));
239                 }
240                 else if (is_subclass_of($parent, 'File') && $row != null) {
241                         $this->parent_name = $row['document_name'];
242                 }
243                 elseif($row != null)
244                 {
245                         $this->parent_name = stripslashes($row['name']);
246                 }
247                 else {
248                         $this->parent_name = '';
249                 }
250         }
251
252
253
254
255         function get_list_view_data(){
256                 global $action, $currentModule, $focus, $current_module_strings, $app_list_strings, $timedate;
257
258                 $override_date_for_subpanel = false;
259                 if(!empty($_REQUEST['module']) && $_REQUEST['module'] !='Calendar' && $_REQUEST['module'] !='Tasks' && $_REQUEST['module'] !='Home'){
260                         //this is a subpanel list view, so override the due date with start date so that collections subpanel works as expected
261                         $override_date_for_subpanel = true;
262                 }
263                 
264                 $today = $timedate->nowDbDate();
265                 $task_fields = $this->get_list_view_array();
266                 $dbtime = $timedate->to_db($task_fields['DATE_DUE']);
267                 if($override_date_for_subpanel){
268                         $dbtime = $timedate->to_db($task_fields['DATE_START']);
269                 }
270
271         $task_fields['TIME_DUE'] = $timedate->to_display_time($dbtime);
272         $task_fields['DATE_DUE'] = $timedate->to_display_date($dbtime);
273
274
275         $date_due = $task_fields['DATE_DUE'];
276
277                 if (!empty($this->priority))
278                         $task_fields['PRIORITY'] = $app_list_strings['task_priority_dom'][$this->priority];
279                 if (isset($this->parent_type))
280                         $task_fields['PARENT_MODULE'] = $this->parent_type;
281                 if ($this->status != "Completed" && $this->status != "Deferred" )
282                 {
283                         $setCompleteUrl = "<a onclick='SUGAR.util.closeActivityPanel.show(\"{$this->module_dir}\",\"{$this->id}\",\"Completed\",\"listview\",\"1\");'>";
284                     $task_fields['SET_COMPLETE'] = $setCompleteUrl . SugarThemeRegistry::current()->getImage("close_inline","title=".translate('LBL_LIST_CLOSE','Tasks')." border='0'")."</a>";
285                 }
286
287         $dd = $timedate->to_db_date($date_due, false);
288                 if ($dd < $today){
289                         $task_fields['DATE_DUE']= "<font class='overdueTask'>".$date_due."</font>";
290                         if($override_date_for_subpanel){
291                                 $task_fields['DATE_START']= "<font class='overdueTask'>".$date_due."</font>";
292                         }
293                 }else if( $dd   == $today ){
294                         $task_fields['DATE_DUE'] = "<font class='todaysTask'>".$date_due."</font>";
295                         if($override_date_for_subpanel){
296                                 $task_fields['DATE_START'] = "<font class='todaysTask'>".$date_due."</font>";
297                         }
298                 }else{
299                         $task_fields['DATE_DUE'] = "<font class='futureTask'>".$date_due."</font>";
300                         if($override_date_for_subpanel){
301                                 $task_fields['DATE_START'] = "<font class='futureTask'>".$date_due."</font>";
302                         }
303                 }
304
305                 //make sure we grab the localized version of the contact name, if a contact is provided
306                 if (!empty($this->contact_id)) {
307                         global $locale;
308                         $query  = "SELECT first_name, last_name, salutation, title FROM contacts ";
309                         $query .= "WHERE id='$this->contact_id' AND deleted=0";
310                         $result = $this->db->limitQuery($query,0,1,true," Error filling in contact name fields: ");
311
312                         // Get the contact name.
313                         $row = $this->db->fetchByAssoc($result);
314
315                         if($row != null)
316                         {
317                                 $this->contact_name = $locale->getLocaleFormattedName($row['first_name'], $row['last_name'], $row['salutation'], $row['title']);
318                         }
319                 }
320
321                 $task_fields['CONTACT_NAME']= $this->contact_name;
322                 $task_fields['TITLE'] = '';
323                 if (!empty($task_fields['CONTACT_NAME'])) {
324                         $task_fields['TITLE'] .= $current_module_strings['LBL_LIST_CONTACT'].": ".$task_fields['CONTACT_NAME'];
325                 }
326                 if (!empty($this->parent_name)) {
327                         $task_fields['TITLE'] .= "\n".$app_list_strings['parent_type_display'][$this->parent_type].": ".$this->parent_name;
328                         $task_fields['PARENT_NAME']=$this->parent_name;
329                 }
330
331                 return $task_fields;
332         }
333
334         function set_notification_body($xtpl, $task)
335         {
336                 global $app_list_strings;
337         global $timedate;
338         $notifyUser = $task->current_notify_user;
339         $prefDate = $notifyUser->getUserDateTimePreferences();
340                 $xtpl->assign("TASK_SUBJECT", $task->name);
341                 //MFH #13507
342                 $xtpl->assign("TASK_PRIORITY", (isset($task->priority)?$app_list_strings['task_priority_dom'][$task->priority]:""));
343                 $xtpl->assign("TASK_DUEDATE", $timedate->to_display_date_time($task->date_due . " " . $task->time_due,true,true,$notifyUser)." ".$prefDate['userGmt']);
344                 $xtpl->assign("TASK_STATUS", (isset($task->status)?$app_list_strings['task_status_dom'][$task->status]:""));
345                 $xtpl->assign("TASK_DESCRIPTION", $task->description);
346
347                 return $xtpl;
348         }
349
350         function bean_implements($interface){
351                 switch($interface){
352                         case 'ACL':return true;
353                 }
354                 return false;
355         }
356         function listviewACLHelper(){
357                 $array_assign = parent::listviewACLHelper();
358                 $is_owner = false;
359                 if(!empty($this->parent_name)){
360                         if(!empty($this->parent_name_owner)){
361                                 global $current_user;
362                                 $is_owner = $current_user->id == $this->parent_name_owner;
363                         }
364                 }
365
366                         if(!ACLController::moduleSupportsACL($this->parent_type) || ACLController::checkAccess($this->parent_type, 'view', $is_owner)){
367                                 $array_assign['PARENT'] = 'a';
368                         }else{
369                                 $array_assign['PARENT'] = 'span';
370                         }
371                 $is_owner = false;
372                 if(!empty($this->contact_name)){
373                         if(!empty($this->contact_name_owner)){
374                                 global $current_user;
375                                 $is_owner = $current_user->id == $this->contact_name_owner;
376                         }
377                 }
378
379                 if( ACLController::checkAccess('Contacts', 'view', $is_owner)){
380                                 $array_assign['CONTACT'] = 'a';
381                 }else{
382                                 $array_assign['CONTACT'] = 'span';
383                 }
384
385                 return $array_assign;
386         }
387
388         public function getDefaultStatus()
389     {
390          $def = $this->field_defs['status'];
391          if (isset($def['default'])) {
392              return $def['default'];
393          } else {
394             $app = return_app_list_strings_language($GLOBALS['current_language']);
395             if (isset($def['options']) && isset($app[$def['options']])) {
396                 $keys = array_keys($app[$def['options']]);
397                 return $keys[0];
398             }
399         }
400         return '';
401     }
402
403 }