2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
37 require_once('include/MVC/View/SugarView.php');
39 class CalendarViewSaveActivity extends SugarView {
41 function CalendarViewSave(){
50 require_once("modules/Calendar/CalendarUtils.php");
52 $field_list = CalendarUtils::get_fields();
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;
62 if(!empty($_REQUEST['record']))
63 $bean->retrieve($_REQUEST['record']);
65 if(!$bean->ACLAccess('Save')) {
69 echo json_encode($json_arr);
73 require_once('include/formbase.php');
75 $bean = populateFromPost("",$bean);
78 if(!$_REQUEST['reminder_checked'])
79 $bean->reminder_time = -1;
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);
88 if($r_id = $bean->id){
91 $u->retrieve($bean->assigned_user_id);
95 $bean->retrieve($r_id);
97 if(isset($bean->parent_name))
98 $bean->parent_name = $_REQUEST['parent_name'];
100 $bean->fill_in_additional_parent_fields();
105 $date_field = "date_start";
106 if($_REQUEST['current_module'] == "Tasks")
107 $date_field = "date_due";
109 $timestamp = SugarDateTime::createFromFormat($GLOBALS['timedate']->get_date_time_format(),$bean->$date_field,new DateTimeZone('UTC'))->format('U');
111 if($_REQUEST['current_module'] == 'Calls')
112 $users = $bean->get_call_users();
113 if($_REQUEST['current_module'] == 'Meetings')
114 $users = $bean->get_meeting_users();
116 foreach($users as $u)
117 $user_ids[] = $u->id;
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);
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;
140 'module_name' => $bean->module_dir,
141 'user_id' => $GLOBALS['current_user']->id,
145 'record_name' => html_entity_decode($bean->name,ENT_QUOTES),
146 'record' => $bean->id,
148 'users' => $user_ids,
152 $json_arr = array_merge($json_arr,$field_arr);
153 $json_arr = array_merge($json_arr,CalendarUtils::get_time_data($bean));
162 echo json_encode($json_arr);
167 private function save_activity(&$bean,$notify = true){
170 if($_REQUEST['current_module'] == 'Meetings'){
173 if(!empty($_POST['user_invitees'])) {
174 $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
176 $userInvitees = array();
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'];
190 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
194 if(count($deleteUsers) > 0) {
196 foreach($deleteUsers as $u) {
197 $sql .= ",'" . $u . "'";
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);
205 // Get all contacts for the meeting
206 if(!empty($_POST['contact_invitees'])) {
207 $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
209 $contactInvitees = array();
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'];
221 $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
225 if(count($deleteContacts) > 0) {
227 foreach($deleteContacts as $u) {
228 $sql .= ",'" . $u . "'";
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);
236 if(!empty($_POST['lead_invitees'])) {
237 $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
239 $leadInvitees = array();
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'];
251 $acceptStatusLeads[$a['lead_id']] = $a['accept_status'];
255 if(count($deleteLeads) > 0) {
257 foreach($deleteLeads as $u) {
258 $sql .= ",'" . $u . "'";
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);
266 ///////////////////////////////////////////////////////////////////////////
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;
278 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
279 $bean->contacts_arr[] = $_POST['parent_id'];
282 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
283 $bean->leads_arr[] = $_POST['parent_id'];
287 $bean->save($notify);
291 $existing_users = array();
292 if(!empty($_POST['existing_invitees'])) {
293 $existing_users = explode(",", trim($_POST['existing_invitees'], ','));
296 foreach($bean->users_arr as $user_id) {
297 if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
301 if(!isset($acceptStatusUsers[$user_id])) {
302 $bean->users->add($user_id);
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);
313 $existing_contacts = array();
314 if(!empty($_POST['existing_contact_invitees'])) {
315 $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ','));
318 foreach($bean->contacts_arr as $contact_id) {
319 if(empty($contact_id) || isset($exiting_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
323 if(!isset($acceptStatusContacts[$contact_id])) {
324 $bean->contacts->add($contact_id);
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);
335 $existing_leads = array();
336 if(!empty($_POST['existing_lead_invitees'])) {
337 $existing_leads = explode(",", trim($_POST['existing_lead_invitees'], ','));
340 foreach($bean->leads_arr as $lead_id) {
341 if(empty($lead_id) || isset($exiting_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
345 if(!isset($acceptStatusLeads[$lead_id])) {
346 $bean->leads->add($lead_id);
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);
356 }else if($_REQUEST['current_module'] == 'Calls'){
358 if(!empty($_POST['user_invitees'])) {
359 $userInvitees = explode(',', trim($_POST['user_invitees'], ','));
361 $userInvitees = array();
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'];
375 $acceptStatusUsers[$a['user_id']] = $a['accept_status'];
379 if(count($deleteUsers) > 0) {
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 . "'";
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);
392 // Get all contacts for the call
393 if(!empty($_POST['contact_invitees'])) {
394 $contactInvitees = explode(',', trim($_POST['contact_invitees'], ','));
396 $contactInvitees = array();
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'];
408 $acceptStatusContacts[$a['contact_id']] = $a['accept_status'];
412 if(count($deleteContacts) > 0) {
414 foreach($deleteContacts as $u) {
415 $sql .= ",'" . $u . "'";
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);
423 if(!empty($_POST['lead_invitees'])) {
424 $leadInvitees = explode(',', trim($_POST['lead_invitees'], ','));
426 $leadInvitees = array();
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'];
440 $acceptStatusLeads[$a['user_id']] = $a['accept_status'];
444 if(count($deleteLeads) > 0) {
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 . "'";
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);
457 ///////////////////////////////////////////////////////////////////////////
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;
469 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') {
470 $bean->contacts_arr[] = $_POST['parent_id'];
473 if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') {
474 $bean->leads_arr[] = $_POST['parent_id'];
478 $bean->save($notify);
482 $existing_users = array();
483 if(!empty($_POST['existing_invitees'])) {
484 $existing_users = explode(",", trim($_POST['existing_invitees'], ','));
487 foreach($bean->users_arr as $user_id) {
488 if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) {
492 if(!isset($acceptStatusUsers[$user_id])) {
493 $bean->users->add($user_id);
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);
504 $existing_contacts = array();
505 if(!empty($_POST['existing_contact_invitees'])) {
506 $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ','));
509 foreach($bean->contacts_arr as $contact_id) {
510 if(empty($contact_id) || isset($exiting_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) {
514 if(!isset($acceptStatusContacts[$contact_id])) {
515 $bean->contacts->add($contact_id);
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);
526 $existing_leads = array();
527 if(!empty($_POST['existing_lead_invitees'])) {
528 $existing_contacts = explode(",", trim($_POST['existing_lead_invitees'], ','));
531 foreach($bean->leads_arr as $lead_id) {
532 if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) {
536 if(!isset($acceptStatusLeads[$lead_id])) {
537 $bean->leads->add($lead_id);
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);
547 }else if($_REQUEST['current_module'] == 'Tasks'){
548 if (!isset($GLOBALS['check_notify'])){
549 $GLOBALS['check_notify'] = false;
551 $bean->save($GLOBALS['check_notify']);