]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/views/view.saveactivity.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Calendar / views / view.saveactivity.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once('include/MVC/View/SugarView.php');
38
39 class CalendarViewSaveActivity extends SugarView {
40
41         function CalendarViewSave(){
42                 parent::SugarView();
43         }
44         
45         function process(){
46                 $this->display();
47         }
48         
49         function display(){
50                 require_once("modules/Calendar/CalendarUtils.php");
51                 
52                 $field_list = CalendarUtils::get_fields();
53
54                 global $beanFiles,$beanList;
55                 $module = $_REQUEST['current_module'];
56                 require_once($beanFiles[$beanList[$module]]);
57                 $bean = new $beanList[$module]();
58                 $type = strtolower($beanList[$module]);
59                 $table_name = $bean->table_name;
60                 
61
62                 if(!empty($_REQUEST['record']))
63                         $bean->retrieve($_REQUEST['record']);
64         
65                 if(!$bean->ACLAccess('Save')) {
66                         $json_arr = array(
67                                 'success' => 'no',
68                         );
69                         echo json_encode($json_arr);
70                         die;    
71                 }
72
73                 require_once('include/formbase.php');
74                 
75                 $bean = populateFromPost("",$bean);
76
77                 
78                 if(!$_REQUEST['reminder_checked'])
79                         $bean->reminder_time = -1;
80
81
82                 if(empty($_REQUEST['record']) && strpos($_POST['user_invitees'],$bean->assigned_user_id) === false)
83                         $_POST['user_invitees'] .= ",".$bean->assigned_user_id;
84                 // fill invites and save the entry
85                 $this->save_activity($bean); 
86
87
88                 if($r_id = $bean->id){
89
90                         $u = new User();
91                         $u->retrieve($bean->assigned_user_id);
92
93                         $arr_rec = array();
94
95                         $bean->retrieve($r_id);
96         
97                         if(isset($bean->parent_name))
98                                 $bean->parent_name = $_REQUEST['parent_name'];
99         
100                         $bean->fill_in_additional_parent_fields();
101         
102
103                         global $timedate;
104                         
105                         $date_field = "date_start";
106                         if($_REQUEST['current_module'] == "Tasks")
107                                 $date_field = "date_due";                       
108                 
109                         $timestamp = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$bean->$date_field,new DateTimeZone('UTC'))->format('U');
110                         
111                         if($_REQUEST['current_module'] == 'Calls')
112                                 $users = $bean->get_call_users();
113                         if($_REQUEST['current_module'] == 'Meetings')
114                                 $users = $bean->get_meeting_users();
115                         $user_ids = array();    
116                         foreach($users as $u)
117                                 $user_ids[] = $u->id;
118
119
120                         $field_arr = array();
121                         foreach($field_list[$_REQUEST['current_module']] as $field){
122                                 $field_arr[$field] = $bean->$field;
123                                 if($bean->field_defs[$field]['type'] == 'text'){                                                                        
124                                         $t = $field_arr[$field];        
125                                         if(strlen($t) > 300){
126                                                 $t = substr($t, 0, 300);
127                                                 $t .= "...";
128                                         }                       
129                                         $t = str_replace("\r\n","<br>",$t);
130                                         $t = str_replace("\r","<br>",$t);
131                                         $t = str_replace("\n","<br>",$t);
132                                         $t = html_entity_decode($t,ENT_QUOTES);
133                                         $field_arr[$field] = $t;
134                                 }
135                         }       
136
137                         $json_arr = array(
138                                 'success' => 'yes',
139                                 'type' => $type,
140                                 'module_name' => $bean->module_dir,
141                                 'user_id' => $GLOBALS['current_user']->id,
142
143                                 'detail' => 1,  
144                                 'edit' => 1,            
145                                 'record_name' => html_entity_decode($bean->name,ENT_QUOTES),
146                                 'record' => $bean->id,
147                                 
148                                 'users' => $user_ids,
149
150                         );
151         
152                         $json_arr = array_merge($json_arr,$field_arr);                  
153                         $json_arr = array_merge($json_arr,CalendarUtils::get_time_data($bean));
154
155                 }else{
156                         $json_arr = array(
157                                 'success' => 'no',
158                         );
159                 }
160
161                 ob_clean();
162                 echo json_encode($json_arr);            
163         
164         }
165
166         
167         private function save_activity(&$bean,$notify = true){  
168         
169
170                 if($_REQUEST['current_module'] == 'Meetings'){          
171
172
173                                 if(!empty($_POST['user_invitees'])) {
174                                         $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
175                                 } else {
176                                         $userInvitees = array();        
177                                 }
178                             
179                                 // Calculate which users to flag as deleted and which to add
180                                 $deleteUsers = array();
181                                 $bean->load_relationship('users');
182                                 // Get all users for the meeting
183                                 $q = 'SELECT mu.user_id, mu.accept_status FROM meetings_users mu WHERE mu.meeting_id = \''.$bean->id.'\'';
184                                 $r = $bean->db->query($q);
185                                 $acceptStatusUsers = array();
186                                 while($a = $bean->db->fetchByAssoc($r)) {
187                                           if(!in_array($a['user_id'], $userInvitees)) {
188                                                 $deleteUsers[$a['user_id']] = $a['user_id'];
189                                           } else {
190                                                 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];  
191                                           }
192                                 }
193                                         
194                                 if(count($deleteUsers) > 0) {
195                                         $sql = '';
196                                         foreach($deleteUsers as $u) {
197                                                 $sql .= ",'" . $u . "'";
198                                         }
199                                         $sql = substr($sql, 1);
200                                         // We could run a delete SQL statement here, but will just mark as deleted instead
201                                         $sql = "UPDATE meetings_users set deleted = 1 where user_id in ($sql) AND meeting_id = '". $bean->id . "'";
202                                         $bean->db->query($sql);
203                                 }         
204                             
205                                 // Get all contacts for the meeting
206                                 if(!empty($_POST['contact_invitees'])) {
207                                    $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
208                                 } else {
209                                    $contactInvitees = array();  
210                                 }
211                             
212                                 $deleteContacts = array();
213                                 $bean->load_relationship('contacts');
214                                 $q = 'SELECT mu.contact_id, mu.accept_status FROM meetings_contacts mu WHERE mu.meeting_id = \''.$bean->id.'\'';
215                                 $r = $bean->db->query($q);
216                                 $acceptStatusContacts = array();
217                                 while($a = $bean->db->fetchByAssoc($r)) {
218                                           if(!in_array($a['contact_id'], $contactInvitees)) {
219                                                  $deleteContacts[$a['contact_id']] = $a['contact_id'];
220                                           }     else {
221                                                  $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
222                                           }
223                                 }
224                                 
225                                 if(count($deleteContacts) > 0) {
226                                         $sql = '';
227                                         foreach($deleteContacts as $u) {
228                                                 $sql .= ",'" . $u . "'";
229                                         }
230                                         $sql = substr($sql, 1);
231                                         // We could run a delete SQL statement here, but will just mark as deleted instead
232                                         $sql = "UPDATE meetings_contacts set deleted = 1 where contact_id in ($sql) AND meeting_id = '". $bean->id . "'";
233                                         $bean->db->query($sql);
234                                 }
235                 
236                                 if(!empty($_POST['lead_invitees'])) {
237                                    $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
238                                 } else {
239                                    $leadInvitees = array();     
240                                 }
241                             
242                                 $deleteLeads = array();
243                                 $bean->load_relationship('leads');
244                                 $q = 'SELECT mu.lead_id, mu.accept_status FROM meetings_leads mu WHERE mu.meeting_id = \''.$bean->id.'\'';
245                                 $r = $bean->db->query($q);
246                                 $acceptStatusLeads = array();
247                                 while($a = $bean->db->fetchByAssoc($r)) {
248                                           if(!in_array($a['lead_id'], $leadInvitees)) {
249                                                  $deleteLeads[$a['lead_id']] = $a['lead_id'];
250                                           }     else {
251                                                  $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
252                                           }
253                                 }
254                                 
255                                 if(count($deleteLeads) > 0) {
256                                         $sql = '';
257                                         foreach($deleteLeads as $u) {
258                                                 $sql .= ",'" . $u . "'";
259                                         }
260                                         $sql = substr($sql, 1);
261                                         // We could run a delete SQL statement here, but will just mark as deleted instead
262                                         $sql = "UPDATE meetings_leads set deleted = 1 where lead_id in ($sql) AND meeting_id = '". $bean->id . "'";
263                                         $bean->db->query($sql);
264                                 }
265                                 ////    END REMOVE
266                                 ///////////////////////////////////////////////////////////////////////////
267                 
268                             
269                                 ///////////////////////////////////////////////////////////////////////////
270                                 ////    REBUILD INVITEE RELATIONSHIPS
271                                 $bean->users_arr = array();
272                                 $bean->users_arr = $userInvitees;
273                                 $bean->contacts_arr = array();
274                                 $bean->contacts_arr = $contactInvitees;
275                                 $bean->leads_arr = array();
276                                 $bean->leads_arr = $leadInvitees;
277                 
278                                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
279                                         $bean->contacts_arr[] = $_POST['parent_id'];
280                                 }
281                             
282                                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
283                                         $bean->leads_arr[] = $_POST['parent_id'];
284                                 }
285                                 
286                                 
287                                 $bean->save($notify);           
288
289                                 
290                                 // Process users
291                                 $existing_users = array();
292                                 if(!empty($_POST['existing_invitees'])) {
293                                         $existing_users =  explode(",", trim($_POST['existing_invitees'], ','));
294                                 }
295                             
296                                 foreach($bean->users_arr as $user_id) {
297                                         if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
298                                                 continue;
299                                         }
300                                         
301                                         if(!isset($acceptStatusUsers[$user_id])) {                                              
302                                                 $bean->users->add($user_id);
303                                         } else {
304                                                 // update query to preserve accept_status
305                                                 $qU  = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
306                                                 $qU .= 'WHERE meeting_id = \''.$bean->id.'\' ';
307                                                 $qU .= 'AND user_id = \''.$user_id.'\'';
308                                                 $bean->db->query($qU);
309                                         } 
310                                 }
311                                 
312                                 // Process contacts
313                                 $existing_contacts =  array();
314                                 if(!empty($_POST['existing_contact_invitees'])) {
315                                    $existing_contacts =  explode(",", trim($_POST['existing_contact_invitees'], ','));
316                                 }
317                                     
318                                 foreach($bean->contacts_arr as $contact_id) {
319                                         if(empty($contact_id) || isset($exiting_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
320                                                 continue;
321                                         }
322                                         
323                                         if(!isset($acceptStatusContacts[$contact_id])) {
324                                             $bean->contacts->add($contact_id);
325                                         } else {
326                                                 // update query to preserve accept_status
327                                                 $qU  = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
328                                                 $qU .= 'WHERE meeting_id = \''.$bean->id.'\' ';
329                                                 $qU .= 'AND contact_id = \''.$contact_id.'\'';
330                                                 $bean->db->query($qU);
331                                         }
332                                 }
333                 
334                                 // Process leads
335                                 $existing_leads =  array();
336                                 if(!empty($_POST['existing_lead_invitees'])) {
337                                    $existing_leads =  explode(",", trim($_POST['existing_lead_invitees'], ','));
338                                 }
339                                     
340                                 foreach($bean->leads_arr as $lead_id) {
341                                         if(empty($lead_id) || isset($exiting_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
342                                                 continue;
343                                         }
344                                         
345                                         if(!isset($acceptStatusLeads[$lead_id])) {
346                                             $bean->leads->add($lead_id);
347                                         } else {
348                                                 // update query to preserve accept_status
349                                                 $qU  = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
350                                                 $qU .= 'WHERE meeting_id = \''.$bean->id.'\' ';
351                                                 $qU .= 'AND lead_id = \''.$lead_id.'\'';
352                                                 $bean->db->query($qU);
353                                         }
354                                 }
355                 
356                 }else if($_REQUEST['current_module'] == 'Calls'){
357         
358                                 if(!empty($_POST['user_invitees'])) {
359                                         $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
360                                 }else {
361                                         $userInvitees = array();        
362                                 }
363                             
364                                 // Calculate which users to flag as deleted and which to add
365                                 $deleteUsers = array();
366                                 $bean->load_relationship('users');
367                                 // Get all users for the call
368                                 $q = 'SELECT mu.user_id, mu.accept_status FROM calls_users mu WHERE mu.call_id = \''.$bean->id.'\'';
369                                 $r = $bean->db->query($q);
370                                 $acceptStatusUsers = array();
371                                 while($a = $bean->db->fetchByAssoc($r)) {
372                                           if(!in_array($a['user_id'], $userInvitees)) {
373                                                  $deleteUsers[$a['user_id']] = $a['user_id'];
374                                           } else {
375                                              $acceptStatusUsers[$a['user_id']] = $a['accept_status'];  
376                                           }
377                                 }
378                                         
379                                 if(count($deleteUsers) > 0) {
380                                         $sql = '';
381                                         foreach($deleteUsers as $u) {
382                                         // make sure we don't delete the assigned user
383                                         if ( $u != $bean->assigned_user_id )
384                                                 $sql .= ",'" . $u . "'";
385                                         }
386                                         $sql = substr($sql, 1);
387                                         // We could run a delete SQL statement here, but will just mark as deleted instead
388                                         $sql = "UPDATE calls_users set deleted = 1 where user_id in ($sql) AND call_id = '". $bean->id . "'";
389                                         $bean->db->query($sql);
390                                 }         
391                             
392                                 // Get all contacts for the call
393                                 if(!empty($_POST['contact_invitees'])) {
394                                    $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
395                                 } else {
396                                    $contactInvitees = array();  
397                                 }
398                             
399                                 $deleteContacts = array();
400                                 $bean->load_relationship('contacts');
401                                 $q = 'SELECT mu.contact_id, mu.accept_status FROM calls_contacts mu WHERE mu.call_id = \''.$bean->id.'\'';
402                                 $r = $bean->db->query($q);
403                                 $acceptStatusContacts = array();
404                                 while($a = $bean->db->fetchByAssoc($r)) {
405                                           if(!in_array($a['contact_id'], $contactInvitees)) {
406                                                  $deleteContacts[$a['contact_id']] = $a['contact_id'];
407                                           }else{
408                                                  $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
409                                           }
410                                 }
411                                 
412                                 if(count($deleteContacts) > 0) {
413                                         $sql = '';
414                                         foreach($deleteContacts as $u) {
415                                                 $sql .= ",'" . $u . "'";
416                                         }
417                                         $sql = substr($sql, 1);
418                                         // We could run a delete SQL statement here, but will just mark as deleted instead
419                                         $sql = "UPDATE calls_contacts set deleted = 1 where contact_id in ($sql) AND call_id = '". $bean->id . "'";
420                                         $bean->db->query($sql);
421                                 }
422                 
423                                 if(!empty($_POST['lead_invitees'])) {
424                                    $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
425                                 } else {
426                                    $leadInvitees = array();     
427                                 }
428                             
429                                 // Calculate which leads to flag as deleted and which to add
430                                 $deleteLeads = array();
431                                 $bean->load_relationship('leads');
432                                 // Get all leads for the call
433                                 $q = 'SELECT mu.lead_id, mu.accept_status FROM calls_leads mu WHERE mu.call_id = \''.$bean->id.'\'';
434                                 $r = $bean->db->query($q);
435                                 $acceptStatusLeads = array();
436                                 while($a = $bean->db->fetchByAssoc($r)) {
437                                           if(!in_array($a['lead_id'], $leadInvitees)) {
438                                                  $deleteLeads[$a['lead_id']] = $a['lead_id'];
439                                           } else {
440                                              $acceptStatusLeads[$a['user_id']] = $a['accept_status'];  
441                                           }
442                                 }
443                                         
444                                 if(count($deleteLeads) > 0) {
445                                         $sql = '';
446                                         foreach($deleteLeads as $u) {
447                                         // make sure we don't delete the assigned user
448                                         if ( $u != $bean->assigned_user_id )
449                                                 $sql .= ",'" . $u . "'";
450                                         }
451                                         $sql = substr($sql, 1);
452                                         // We could run a delete SQL statement here, but will just mark as deleted instead
453                                         $sql = "UPDATE calls_leads set deleted = 1 where lead_id in ($sql) AND call_id = '". $bean->id . "'";
454                                         $bean->db->query($sql);
455                                 }
456                                 ////    END REMOVE
457                                 ///////////////////////////////////////////////////////////////////////////
458                 
459                             
460                                 ///////////////////////////////////////////////////////////////////////////
461                                 ////    REBUILD INVITEE RELATIONSHIPS
462                                 $bean->users_arr = array();
463                                 $bean->users_arr = $userInvitees;
464                                 $bean->contacts_arr = array();
465                                 $bean->contacts_arr = $contactInvitees;
466                                 $bean->leads_arr = array();
467                                 $bean->leads_arr = $leadInvitees;
468                 
469                                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
470                                         $bean->contacts_arr[] = $_POST['parent_id'];
471                                 }
472                             
473                                 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
474                                         $bean->leads_arr[] = $_POST['parent_id'];
475                                 }       
476                                 
477                                 
478                                 $bean->save($notify);
479                                 
480                             
481                                 // Process users
482                                 $existing_users = array();
483                                 if(!empty($_POST['existing_invitees'])) {
484                                    $existing_users =  explode(",", trim($_POST['existing_invitees'], ','));
485                                 }
486                             
487                                 foreach($bean->users_arr as $user_id) {
488                                     if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
489                                                 continue;
490                                         }
491                                         
492                                         if(!isset($acceptStatusUsers[$user_id])) {
493                                                 $bean->users->add($user_id);
494                                         } else {
495                                                 // update query to preserve accept_status
496                                                 $qU  = 'UPDATE calls_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' ';
497                                                 $qU .= 'WHERE call_id = \''.$bean->id.'\' ';
498                                                 $qU .= 'AND user_id = \''.$user_id.'\'';
499                                                 $bean->db->query($qU);
500                                         } 
501                                 }
502                                 
503                                 // Process contacts
504                                 $existing_contacts =  array();
505                                 if(!empty($_POST['existing_contact_invitees'])) {
506                                    $existing_contacts =  explode(",", trim($_POST['existing_contact_invitees'], ','));
507                                 }
508                                     
509                                 foreach($bean->contacts_arr as $contact_id) {
510                                         if(empty($contact_id) || isset($exiting_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
511                                                 continue;
512                                         }
513                                         
514                                         if(!isset($acceptStatusContacts[$contact_id])) {
515                                             $bean->contacts->add($contact_id);
516                                         } else {
517                                                 // update query to preserve accept_status
518                                                 $qU  = 'UPDATE calls_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' ';
519                                                 $qU .= 'WHERE call_id = \''.$bean->id.'\' ';
520                                                 $qU .= 'AND contact_id = \''.$contact_id.'\'';
521                                                 $bean->db->query($qU);
522                                         }
523                                 }
524                 
525                                 // Process leads
526                                 $existing_leads =  array();
527                                 if(!empty($_POST['existing_lead_invitees'])) {
528                                    $existing_contacts =  explode(",", trim($_POST['existing_lead_invitees'], ','));
529                                 }
530                                     
531                                 foreach($bean->leads_arr as $lead_id) {
532                                         if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
533                                                 continue;
534                                         }
535                                         
536                                         if(!isset($acceptStatusLeads[$lead_id])) {
537                                             $bean->leads->add($lead_id);
538                                         } else {
539                                                 // update query to preserve accept_status
540                                                 $qU  = 'UPDATE calls_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' ';
541                                                 $qU .= 'WHERE call_id = \''.$bean->id.'\' ';
542                                                 $qU .= 'AND lead_id = \''.$lead_id.'\'';
543                                                 $bean->db->query($qU);
544                                         }
545                                 } 
546                                 
547                 }else if($_REQUEST['current_module'] == 'Tasks'){
548                         if (!isset($GLOBALS['check_notify'])){
549                                 $GLOBALS['check_notify'] = false;
550                         }
551                         $bean->save($GLOBALS['check_notify']);
552                 }       
553         }
554         
555
556 }
557
558 ?>