]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/CalendarActivity.php
Release 6.5.16
[Github/sugarcrm.git] / modules / Calendar / CalendarActivity.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-2013 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
41 require_once('include/utils/activity_utils.php');
42
43 class CalendarActivity {
44         var $sugar_bean;
45         var $start_time;
46         var $end_time;
47
48         function __construct($args){
49                 // if we've passed in an array, then this is a free/busy slot
50                 // and does not have a sugarbean associated to it
51                 global $timedate;
52
53                 if ( is_array ( $args )){
54                         $this->start_time = clone $args[0];
55                         $this->end_time = clone $args[1];
56                         $this->sugar_bean = null;
57                         $timedate->tzGMT($this->start_time);
58                         $timedate->tzGMT($this->end_time);
59                         return;
60                 }
61
62             // else do regular constructor..
63
64                 $sugar_bean = $args;
65                 $this->sugar_bean = $sugar_bean;
66
67
68         if ($sugar_bean->object_name == 'Task'){
69             if (!empty($this->sugar_bean->date_start))
70             {
71                 $this->start_time = $timedate->fromUser($this->sugar_bean->date_start);
72             }
73             else {
74                 $this->start_time = $timedate->fromUser($this->sugar_bean->date_due);
75             }
76             if ( empty($this->start_time)){
77                 return;
78             }
79             $this->end_time = $timedate->fromUser($this->sugar_bean->date_due);
80         }else{
81             $this->start_time = $timedate->fromUser($this->sugar_bean->date_start);
82             if ( empty($this->start_time)){
83                 return;
84             }
85                         $hours = $this->sugar_bean->duration_hours;
86                         if(empty($hours)){
87                             $hours = 0;
88                         }
89                         $mins = $this->sugar_bean->duration_minutes;
90                         if(empty($mins)){
91                             $mins = 0;
92                         }
93                         $this->end_time = $this->start_time->get("+$hours hours $mins minutes");
94                 }
95                 // Convert it back to database time so we can properly manage it for getting the proper start and end dates
96                 $timedate->tzGMT($this->start_time);
97                 $timedate->tzGMT($this->end_time);
98         }
99
100         /**
101          * Get where clause for fetching entries from DB for within two dates timespan
102          * @param string $table_name t
103          * @param string $rel_table table for accept status, not used in Tasks
104          * @param SugarDateTime $start_ts_obj start date
105          * @param SugarDateTime $end_ts_obj end date
106          * @param string $field_name date field in table
107          * @param string $view view; not used for now, left for compatibility
108          * @return string
109          */
110     function get_occurs_within_where_clause($table_name, $rel_table, $start_ts_obj, $end_ts_obj, $field_name = 'date_start', $view)
111     {
112         return self::getOccursWhereClauseGeneral($table_name, $rel_table, $start_ts_obj, $end_ts_obj, $field_name, array('self', 'within'));
113         }
114
115     /**
116      * Get where clause for fetching entries from DB for until certain date timespan
117      * @param string $table_name t
118      * @param string $rel_table table for accept status, not used in Tasks
119      * @param SugarDateTime $start_ts_obj start date
120      * @param SugarDateTime $end_ts_obj end date
121      * @param string $field_name date field in table
122      * @param string $view view; not used for now, left for compatibility
123      * @return string
124      */
125     public static function get_occurs_until_where_clause($table_name, $rel_table, $start_ts_obj, $end_ts_obj, $field_name = 'date_start', $view)
126     {
127         return self::getOccursWhereClauseGeneral($table_name, $rel_table, $start_ts_obj, $end_ts_obj, $field_name, array('self', 'until'));
128     }
129
130         function get_freebusy_activities($user_focus, $start_date_time, $end_date_time){
131                 $act_list = array();
132                 $vcal_focus = new vCal();
133                 $vcal_str = $vcal_focus->get_vcal_freebusy($user_focus);
134
135                 $lines = explode("\n",$vcal_str);
136                 $utc = new DateTimeZone("UTC");
137                 foreach ($lines as $line){
138                         if ( preg_match('/^FREEBUSY.*?:([^\/]+)\/([^\/]+)/i',$line,$matches)){
139                           $dates_arr = array(SugarDateTime::createFromFormat(vCal::UTC_FORMAT, $matches[1], $utc),
140                                               SugarDateTime::createFromFormat(vCal::UTC_FORMAT, $matches[2], $utc));
141                           $act_list[] = new CalendarActivity($dates_arr);
142                         }
143                 }
144                 return $act_list;
145         }
146
147         /**
148          * Get array of activities
149          * @param string $user_id
150          * @param boolean $show_tasks
151          * @param SugarDateTime $view_start_time start date
152          * @param SugarDateTime $view_end_time end date
153          * @param string $view view; not used for now, left for compatibility
154          * @param boolean $show_calls
155          * @param boolean $show_completed use to allow filtering completed events 
156          * @return array
157          */
158         function get_activities($user_id, $show_tasks, $view_start_time, $view_end_time, $view, $show_calls = true, $show_completed = true)
159         {
160                 global $current_user;
161                 $act_list = array();
162                 $seen_ids = array();
163                 
164                 $completedCalls = '';
165                 $completedMeetings = '';
166                 $completedTasks = '';
167                 if (!$show_completed)
168                 {
169                     $completedCalls = " AND calls.status = 'Planned' ";
170                     $completedMeetings = " AND meetings.status = 'Planned' ";
171                     $completedTasks = " AND tasks.status != 'Completed' ";
172                 }
173                 
174                 // get all upcoming meetings, tasks due, and calls for a user
175                 if(ACLController::checkAccess('Meetings', 'list', $current_user->id == $user_id)) {
176                         $meeting = new Meeting();
177
178                         if($current_user->id  == $user_id) {
179                                 $meeting->disable_row_level_security = true;
180                         }
181
182             $where = self::get_occurs_until_where_clause($meeting->table_name, $meeting->rel_users_table, $view_start_time, $view_end_time, 'date_start', $view);
183                         $where .= $completedMeetings;
184                         $focus_meetings_list = build_related_list_by_user_id($meeting, $user_id, $where);
185                         foreach($focus_meetings_list as $meeting) {
186                                 if(isset($seen_ids[$meeting->id])) {
187                                         continue;
188                                 }
189
190                                 $seen_ids[$meeting->id] = 1;
191                                 $act = new CalendarActivity($meeting);
192
193                                 if(!empty($act)) {
194                                         $act_list[] = $act;
195                                 }
196                         }
197                 }
198
199                 if($show_calls){
200                         if(ACLController::checkAccess('Calls', 'list',$current_user->id  == $user_id)) {
201                                 $call = new Call();
202
203                                 if($current_user->id  == $user_id) {
204                                         $call->disable_row_level_security = true;
205                                 }
206
207                                 $where = CalendarActivity::get_occurs_within_where_clause($call->table_name, $call->rel_users_table, $view_start_time, $view_end_time, 'date_start', $view);
208                                 $where .= $completedCalls;
209                                 $focus_calls_list = build_related_list_by_user_id($call, $user_id, $where);
210
211                                 foreach($focus_calls_list as $call) {
212                                         if(isset($seen_ids[$call->id])) {
213                                                 continue;
214                                         }
215                                         $seen_ids[$call->id] = 1;
216
217                                         $act = new CalendarActivity($call);
218                                         if(!empty($act)) {
219                                                 $act_list[] = $act;
220                                         }
221                                 }
222                         }
223                 }
224
225
226                 if($show_tasks){
227                         if(ACLController::checkAccess('Tasks', 'list',$current_user->id == $user_id)) {
228                                 $task = new Task();
229
230                                 $where = CalendarActivity::get_occurs_within_where_clause('tasks', '', $view_start_time, $view_end_time, 'date_due', $view);
231                                 $where .= " AND tasks.assigned_user_id='$user_id' ";
232                                 $where .= $completedTasks;
233
234                                 $focus_tasks_list = $task->get_full_list("", $where, true);
235
236                                 if(!isset($focus_tasks_list)) {
237                                         $focus_tasks_list = array();
238                                 }
239
240                                 foreach($focus_tasks_list as $task) {
241                                         $act = new CalendarActivity($task);
242                                         if(!empty($act)) {
243                                                 $act_list[] = $act;
244                                         }
245                                 }
246                         }
247                 }
248                 return $act_list;
249         }
250
251     /**
252      * Get where clause for fetching entries from DB (is used by certain get_occurs.. methods)
253      * @param string $table_name t
254      * @param string $rel_table table for accept status, not used in Tasks
255      * @param SugarDateTime $start_ts_obj start date
256      * @param SugarDateTime $end_ts_obj end date
257      * @param string $field_name date field in table
258      * @param array $callback callback function to generete specific SQL query-part
259      * @return string
260      */
261     protected static function getOccursWhereClauseGeneral($table_name, $rel_table, $start_ts_obj, $end_ts_obj, $field_name, $callback)
262     {
263         $start = clone $start_ts_obj;
264         $end = clone $end_ts_obj;
265
266         $field_date = $table_name . '.' . $field_name;
267
268         $start_day = $GLOBALS['db']->convert("'{$start->asDb()}'",'datetime');
269         $end_day = $GLOBALS['db']->convert("'{$end->asDb()}'",'datetime');
270
271         $where = '(';
272         $where .= call_user_func($callback, $field_date, $start_day, $end_day);
273
274         if ($rel_table != ''){
275             $where .= " AND $rel_table.accept_status != 'decline'";
276         }
277
278         $where .= ")";
279         return $where;
280     }
281
282     /**
283      * Helper-method to generate within two dates sql clause
284      * @param $field_date string table_name.field_name to compare
285      * @param $start_day string period start date
286      * @param $end_day string period end date
287      * @return string
288      */
289     protected static function within($field_date, $start_day, $end_day)
290     {
291         return "$field_date >= $start_day AND $field_date < $end_day";
292     }
293
294     /**
295      * Helper-method to generate until some date sql clause
296      * @param $field_date string table_name.field_name to compare
297      * @param $start_day string period start date
298      * @param $end_day string period end date
299      * @return string
300      */
301     protected static function until($field_date, $start_day, $end_day)
302     {
303         return "$field_date < $end_day";
304     }
305 }
306
307 ?>