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