| Conditions | 92 |
| Paths | > 20000 |
| Total Lines | 337 |
| Code Lines | 195 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 173 | function handleSave($prefix,$redirect=true, $useRequired=false) { |
||
| 174 | |||
| 175 | |||
| 176 | require_once('include/formbase.php'); |
||
| 177 | |||
| 178 | global $current_user; |
||
| 179 | global $timedate; |
||
| 180 | |||
| 181 | $focus = new Meeting(); |
||
| 182 | |||
| 183 | if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) { |
||
| 184 | return null; |
||
| 185 | } |
||
| 186 | |||
| 187 | if( !isset($_POST['reminder_checked']) or ( isset($_POST['reminder_checked']) && $_POST['reminder_checked'] == '0')) { |
||
| 188 | $_POST['reminder_time'] = -1; |
||
| 189 | } |
||
| 190 | if(!isset($_POST['reminder_time'])) { |
||
| 191 | $_POST['reminder_time'] = $current_user->getPreference('reminder_time'); |
||
| 192 | $_POST['reminder_checked']=1; |
||
| 193 | } |
||
| 194 | |||
| 195 | if(!isset($_POST['email_reminder_checked']) || (isset($_POST['email_reminder_checked']) && $_POST['email_reminder_checked'] == '0')) { |
||
| 196 | $_POST['email_reminder_time'] = -1; |
||
| 197 | } |
||
| 198 | if(!isset($_POST['email_reminder_time'])){ |
||
| 199 | $_POST['email_reminder_time'] = $current_user->getPreference('email_reminder_time'); |
||
| 200 | $_POST['email_reminder_checked'] = 1; |
||
| 201 | } |
||
| 202 | |||
| 203 | // don't allow to set recurring_source from a form |
||
| 204 | unset($_POST['recurring_source']); |
||
| 205 | |||
| 206 | $time_format = $timedate->get_user_time_format(); |
||
| 207 | $time_separator = ":"; |
||
| 208 | if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) { |
||
| 209 | $time_separator = $match[1]; |
||
| 210 | } |
||
| 211 | |||
| 212 | if(!empty($_POST[$prefix.'time_hour_start']) && empty($_POST['time_start'])) { |
||
| 213 | $_POST[$prefix.'time_start'] = $_POST[$prefix.'time_hour_start']. $time_separator .$_POST[$prefix.'time_minute_start']; |
||
| 214 | } |
||
| 215 | |||
| 216 | if(isset($_POST[$prefix.'meridiem']) && !empty($_POST[$prefix.'meridiem'])) { |
||
| 217 | $_POST[$prefix.'time_start'] = $timedate->merge_time_meridiem($_POST[$prefix.'time_start'],$timedate->get_time_format(), $_POST[$prefix.'meridiem']); |
||
| 218 | } |
||
| 219 | |||
| 220 | if(isset($_POST[$prefix.'time_start']) && strlen($_POST[$prefix.'date_start']) == 10) { |
||
| 221 | $_POST[$prefix.'date_start'] = $_POST[$prefix.'date_start'] . ' ' . $_POST[$prefix.'time_start']; |
||
| 222 | } |
||
| 223 | |||
| 224 | // retrieve happens here |
||
| 225 | $focus = populateFromPost($prefix, $focus); |
||
| 226 | if(!$focus->ACLAccess('Save')) { |
||
| 227 | ACLController::displayNoAccess(true); |
||
| 228 | sugar_cleanup(true); |
||
| 229 | } |
||
| 230 | |||
| 231 | // if dates changed |
||
| 232 | if (!empty($focus->id)) { |
||
| 233 | $oldBean = new Meeting(); |
||
| 234 | $oldBean->retrieve($focus->id); |
||
| 235 | if (($focus->date_start != $oldBean->date_start) || ($focus->date_end != $oldBean->date_end)) { |
||
| 236 | $focus->date_changed = true; |
||
| 237 | } else { |
||
| 238 | $focus->date_changed = false; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | $newBean = true; |
||
| 243 | if (!empty($focus->id)) { |
||
| 244 | $newBean = false; |
||
| 245 | } |
||
| 246 | |||
| 247 | //add assigned user and current user if this is the first time bean is saved |
||
| 248 | if(empty($focus->id) && !empty($_REQUEST['return_module']) && $_REQUEST['return_module'] =='Meetings' && !empty($_REQUEST['return_action']) && $_REQUEST['return_action'] =='DetailView'){ |
||
| 249 | //if return action is set to detail view and return module to meeting, then this is from the long form, do not add the assigned user (only the current user) |
||
| 250 | //The current user is already added to UI and we want to give the current user the option of opting out of meeting. |
||
| 251 | //add current user if the assigned to user is different than current user. |
||
| 252 | if($current_user->id != $_POST['assigned_user_id']){ |
||
| 253 | $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', '; |
||
| 254 | $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']); |
||
| 255 | } |
||
| 256 | }elseif (empty($focus->id) ){ |
||
| 257 | //this is not from long form so add assigned and current user automatically as there is no invitee list UI. |
||
| 258 | //This call could be through an ajax call from subpanels or shortcut bar |
||
| 259 | if(!isset($_POST['user_invitees'])) |
||
| 260 | { |
||
| 261 | $_POST['user_invitees'] = ''; |
||
| 262 | } |
||
| 263 | |||
| 264 | $_POST['user_invitees'] .= ','.$_POST['assigned_user_id'].', '; |
||
| 265 | |||
| 266 | //add current user if the assigned to user is different than current user. |
||
| 267 | if($current_user->id != $_POST['assigned_user_id'] && $_REQUEST['module'] != "Calendar"){ |
||
| 268 | $_POST['user_invitees'] .= ','.$current_user->id.', '; |
||
| 269 | } |
||
| 270 | |||
| 271 | //remove any double comma's introduced during appending |
||
| 272 | $_POST['user_invitees'] = str_replace(',,', ',', $_POST['user_invitees']); |
||
| 273 | } |
||
| 274 | |||
| 275 | |||
| 276 | if( (isset($_POST['isSaveFromDetailView']) && $_POST['isSaveFromDetailView'] == 'true') || |
||
| 277 | (isset($_POST['is_ajax_call']) && !empty($_POST['is_ajax_call']) && !empty($focus->id) || |
||
| 278 | (isset($_POST['return_action']) && $_POST['return_action'] == 'SubPanelViewer') && !empty($focus->id))|| |
||
| 279 | !isset($_POST['user_invitees']) // we need to check that user_invitees exists before processing, it is ok to be empty |
||
| 280 | ){ |
||
| 281 | $focus->save(true); |
||
| 282 | $return_id = $focus->id; |
||
| 283 | }else{ |
||
| 284 | if($focus->status == 'Held' && $this->isEmptyReturnModuleAndAction() && !$this->isSaveFromDCMenu()){ |
||
| 285 | //if we are closing the meeting, and the request does not have a return module AND return action set and it is not a save |
||
| 286 | //being triggered by the DCMenu (shortcut bar) then the request is coming from a dashlet or subpanel close icon and there is no |
||
| 287 | //need to process user invitees, just save the current values. |
||
| 288 | $focus->save(true); |
||
| 289 | }else{ |
||
| 290 | /////////////////////////////////////////////////////////////////////////// |
||
| 291 | //// REMOVE INVITEE RELATIONSHIPS |
||
| 292 | if(!empty($_POST['user_invitees'])) { |
||
| 293 | $userInvitees = explode(',', trim($_POST['user_invitees'], ',')); |
||
| 294 | } else { |
||
| 295 | $userInvitees = array(); |
||
| 296 | } |
||
| 297 | |||
| 298 | // Calculate which users to flag as deleted and which to add |
||
| 299 | $deleteUsers = array(); |
||
| 300 | $focus->load_relationship('users'); |
||
| 301 | // Get all users for the meeting |
||
| 302 | $q = 'SELECT mu.user_id, mu.accept_status FROM meetings_users mu WHERE mu.meeting_id = \''.$focus->id.'\' AND mu.deleted=0'; |
||
| 303 | $r = $focus->db->query($q); |
||
| 304 | $acceptStatusUsers = array(); |
||
| 305 | while($a = $focus->db->fetchByAssoc($r)) { |
||
| 306 | if(!in_array($a['user_id'], $userInvitees)) { |
||
| 307 | $deleteUsers[$a['user_id']] = $a['user_id']; |
||
| 308 | } else { |
||
| 309 | $acceptStatusUsers[$a['user_id']] = $a['accept_status']; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | if(count($deleteUsers) > 0) { |
||
| 314 | $sql = ''; |
||
| 315 | foreach($deleteUsers as $u) { |
||
| 316 | $sql .= ",'" . $u . "'"; |
||
| 317 | } |
||
| 318 | $sql = substr($sql, 1); |
||
| 319 | // We could run a delete SQL statement here, but will just mark as deleted instead |
||
| 320 | $sql = "UPDATE meetings_users set deleted = 1 where user_id in ($sql) AND meeting_id = '". $focus->id . "'"; |
||
| 321 | $focus->db->query($sql); |
||
| 322 | } |
||
| 323 | |||
| 324 | // Get all contacts for the meeting |
||
| 325 | if(!empty($_POST['contact_invitees'])) { |
||
| 326 | $contactInvitees = explode(',', trim($_POST['contact_invitees'], ',')); |
||
| 327 | } else { |
||
| 328 | $contactInvitees = array(); |
||
| 329 | } |
||
| 330 | |||
| 331 | $deleteContacts = array(); |
||
| 332 | $focus->load_relationship('contacts'); |
||
| 333 | $q = 'SELECT mu.contact_id, mu.accept_status FROM meetings_contacts mu WHERE mu.meeting_id = \''.$focus->id.'\' AND mu.deleted=0'; |
||
| 334 | $r = $focus->db->query($q); |
||
| 335 | $acceptStatusContacts = array(); |
||
| 336 | while($a = $focus->db->fetchByAssoc($r)) { |
||
| 337 | if(!in_array($a['contact_id'], $contactInvitees)) { |
||
| 338 | $deleteContacts[$a['contact_id']] = $a['contact_id']; |
||
| 339 | } else { |
||
| 340 | $acceptStatusContacts[$a['contact_id']] = $a['accept_status']; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | |||
| 344 | if(count($deleteContacts) > 0) { |
||
| 345 | $sql = ''; |
||
| 346 | foreach($deleteContacts as $u) { |
||
| 347 | $sql .= ",'" . $u . "'"; |
||
| 348 | } |
||
| 349 | $sql = substr($sql, 1); |
||
| 350 | // We could run a delete SQL statement here, but will just mark as deleted instead |
||
| 351 | $sql = "UPDATE meetings_contacts set deleted = 1 where contact_id in ($sql) AND meeting_id = '". $focus->id . "'"; |
||
| 352 | $focus->db->query($sql); |
||
| 353 | } |
||
| 354 | if(!empty($_POST['lead_invitees'])) { |
||
| 355 | $leadInvitees = explode(',', trim($_POST['lead_invitees'], ',')); |
||
| 356 | } else { |
||
| 357 | $leadInvitees = array(); |
||
| 358 | } |
||
| 359 | |||
| 360 | $deleteLeads = array(); |
||
| 361 | $focus->load_relationship('leads'); |
||
| 362 | $q = 'SELECT mu.lead_id, mu.accept_status FROM meetings_leads mu WHERE mu.meeting_id = \''.$focus->id.'\' AND mu.deleted=0'; |
||
| 363 | $r = $focus->db->query($q); |
||
| 364 | $acceptStatusLeads = array(); |
||
| 365 | while($a = $focus->db->fetchByAssoc($r)) { |
||
| 366 | if(!in_array($a['lead_id'], $leadInvitees)) { |
||
| 367 | $deleteLeads[$a['lead_id']] = $a['lead_id']; |
||
| 368 | } else { |
||
| 369 | $acceptStatusLeads[$a['lead_id']] = $a['accept_status']; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | if(count($deleteLeads) > 0) { |
||
| 374 | $sql = ''; |
||
| 375 | foreach($deleteLeads as $u) { |
||
| 376 | $sql .= ",'" . $u . "'"; |
||
| 377 | } |
||
| 378 | $sql = substr($sql, 1); |
||
| 379 | // We could run a delete SQL statement here, but will just mark as deleted instead |
||
| 380 | $sql = "UPDATE meetings_leads set deleted = 1 where lead_id in ($sql) AND meeting_id = '". $focus->id . "'"; |
||
| 381 | $focus->db->query($sql); |
||
| 382 | } |
||
| 383 | //// END REMOVE |
||
| 384 | /////////////////////////////////////////////////////////////////////////// |
||
| 385 | |||
| 386 | |||
| 387 | /////////////////////////////////////////////////////////////////////////// |
||
| 388 | //// REBUILD INVITEE RELATIONSHIPS |
||
| 389 | $focus->users_arr = array(); |
||
| 390 | $focus->users_arr = $userInvitees; |
||
| 391 | $focus->contacts_arr = array(); |
||
| 392 | $focus->contacts_arr = $contactInvitees; |
||
| 393 | $focus->leads_arr = array(); |
||
| 394 | $focus->leads_arr = $leadInvitees; |
||
| 395 | |||
| 396 | if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Contacts') { |
||
| 397 | $focus->contacts_arr[] = $_POST['parent_id']; |
||
| 398 | } |
||
| 399 | if(!empty($_POST['parent_id']) && $_POST['parent_type'] == 'Leads') { |
||
| 400 | $focus->leads_arr[] = $_POST['parent_id']; |
||
| 401 | } |
||
| 402 | // Call the Meeting module's save function to handle saving other fields besides |
||
| 403 | // the users and contacts relationships |
||
| 404 | $focus->update_vcal = false; // Bug #49195 : don't update vcal b/s related users aren't saved yet, create vcal cache below |
||
| 405 | $focus->save(true); |
||
| 406 | $return_id = $focus->id; |
||
| 407 | if(empty($return_id)){ |
||
| 408 | //this is to handle the situation where the save fails, most likely because of a failure |
||
| 409 | //in the external api. bug: 42200 |
||
| 410 | $_REQUEST['action'] = 'EditView'; |
||
| 411 | $_REQUEST['return_action'] = 'EditView'; |
||
| 412 | handleRedirect('', 'Meetings'); |
||
| 413 | } |
||
| 414 | // Process users |
||
| 415 | $existing_users = array(); |
||
| 416 | if(!empty($_POST['existing_invitees'])) { |
||
| 417 | $existing_users = explode(",", trim($_POST['existing_invitees'], ',')); |
||
| 418 | } |
||
| 419 | |||
| 420 | foreach($focus->users_arr as $user_id) { |
||
| 421 | if(empty($user_id) || isset($existing_users[$user_id]) || isset($deleteUsers[$user_id])) { |
||
| 422 | continue; |
||
| 423 | } |
||
| 424 | |||
| 425 | if(!isset($acceptStatusUsers[$user_id])) { |
||
| 426 | $focus->users->add($user_id); |
||
| 427 | } else if (!$focus->date_changed) { |
||
| 428 | // update query to preserve accept_status |
||
| 429 | $qU = 'UPDATE meetings_users SET deleted = 0, accept_status = \''.$acceptStatusUsers[$user_id].'\' '; |
||
| 430 | $qU .= 'WHERE meeting_id = \''.$focus->id.'\' '; |
||
| 431 | $qU .= 'AND user_id = \''.$user_id.'\''; |
||
| 432 | $focus->db->query($qU); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | // Process contacts |
||
| 437 | $existing_contacts = array(); |
||
| 438 | if(!empty($_POST['existing_contact_invitees'])) { |
||
| 439 | $existing_contacts = explode(",", trim($_POST['existing_contact_invitees'], ',')); |
||
| 440 | } |
||
| 441 | |||
| 442 | foreach($focus->contacts_arr as $contact_id) { |
||
| 443 | if(empty($contact_id) || isset($existing_contacts[$contact_id]) || isset($deleteContacts[$contact_id])) { |
||
| 444 | continue; |
||
| 445 | } |
||
| 446 | |||
| 447 | if(!isset($acceptStatusContacts[$contact_id])) { |
||
| 448 | $focus->contacts->add($contact_id); |
||
| 449 | } else if (!$focus->date_changed) { |
||
| 450 | // update query to preserve accept_status |
||
| 451 | $qU = 'UPDATE meetings_contacts SET deleted = 0, accept_status = \''.$acceptStatusContacts[$contact_id].'\' '; |
||
| 452 | $qU .= 'WHERE meeting_id = \''.$focus->id.'\' '; |
||
| 453 | $qU .= 'AND contact_id = \''.$contact_id.'\''; |
||
| 454 | $focus->db->query($qU); |
||
| 455 | } |
||
| 456 | } |
||
| 457 | // Process leads |
||
| 458 | $existing_leads = array(); |
||
| 459 | if(!empty($_POST['existing_lead_invitees'])) { |
||
| 460 | $existing_leads = explode(",", trim($_POST['existing_lead_invitees'], ',')); |
||
| 461 | } |
||
| 462 | |||
| 463 | foreach($focus->leads_arr as $lead_id) { |
||
| 464 | if(empty($lead_id) || isset($existing_leads[$lead_id]) || isset($deleteLeads[$lead_id])) { |
||
| 465 | continue; |
||
| 466 | } |
||
| 467 | |||
| 468 | if(!isset($acceptStatusLeads[$lead_id])) { |
||
| 469 | $focus->leads->add($lead_id); |
||
| 470 | } else if (!$focus->date_changed) { |
||
| 471 | // update query to preserve accept_status |
||
| 472 | $qU = 'UPDATE meetings_leads SET deleted = 0, accept_status = \''.$acceptStatusLeads[$lead_id].'\' '; |
||
| 473 | $qU .= 'WHERE meeting_id = \''.$focus->id.'\' '; |
||
| 474 | $qU .= 'AND lead_id = \''.$lead_id.'\''; |
||
| 475 | $focus->db->query($qU); |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 | // Bug #49195 : update vcal |
||
| 480 | vCal::cache_sugar_vcal($current_user); |
||
| 481 | |||
| 482 | // CCL - Comment out call to set $current_user as invitee |
||
| 483 | // set organizer to auto-accept |
||
| 484 | if ($focus->assigned_user_id == $current_user->id && $newBean) { |
||
| 485 | $focus->set_accept_status($current_user, 'accept'); |
||
| 486 | } |
||
| 487 | |||
| 488 | //// END REBUILD INVITEE RELATIONSHIPS |
||
| 489 | /////////////////////////////////////////////////////////////////////////// |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | if(!empty($_POST['is_ajax_call'])) |
||
| 494 | { |
||
| 495 | $json = getJSONobj(); |
||
| 496 | echo $json->encode(array('status' => 'success', 'get' => '')); |
||
| 497 | exit; |
||
| 498 | } |
||
| 499 | |||
| 500 | if (isset($_REQUEST['return_module']) && $_REQUEST['return_module'] == 'Home'){ |
||
| 501 | header("Location: index.php?module=Home&action=index"); |
||
| 502 | } |
||
| 503 | else if($redirect) { |
||
| 504 | handleRedirect($return_id, 'Meetings'); |
||
| 505 | } else { |
||
| 506 | return $focus; |
||
| 507 | } |
||
| 508 | |||
| 509 | } // end handleSave(); |
||
| 510 | |||
| 513 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.