/** * Checks auditing is enables and then carrys out an audit on the current bean. * * @param $isUpdate */ public function auditBean($isUpdate) { if ($this->is_AuditEnabled() && $isUpdate) { $auditDataChanges = $this->db->getAuditDataChanges($this); if (!empty($auditDataChanges)) { $this->createAuditRecord($auditDataChanges); } else { $GLOBALS['log']->debug('Auditing: createAuditRecord was not called, audit record will not be created.'); } } }
/** * Implements a generic insert and update logic for any SugarBean * This method only works for subclasses that implement the same variable names. * This method uses the presence of an id field that is not null to signify and update. * The id field should not be set otherwise. * * @param boolean $check_notify Optional, default false, if set to true assignee of the record is notified via email. * @todo Add support for field type validation and encoding of parameters. */ function save($check_notify = FALSE) { $this->in_save = true; // cn: SECURITY - strip XSS potential vectors $this->cleanBean(); // This is used so custom/3rd-party code can be upgraded with fewer issues, this will be removed in a future release $this->fixUpFormatting(); global $timedate; global $current_user, $action; $isUpdate = true; if (empty($this->id)) { $isUpdate = false; } if ($this->new_with_id == true) { $isUpdate = false; } if (empty($this->date_modified) || $this->update_date_modified) { $this->date_modified = $GLOBALS['timedate']->nowDb(); } $this->_checkOptimisticLocking($action, $isUpdate); if (!empty($this->modified_by_name)) { $this->old_modified_by_name = $this->modified_by_name; } if ($this->update_modified_by) { $this->modified_user_id = 1; if (!empty($current_user)) { $this->modified_user_id = $current_user->id; $this->modified_by_name = $current_user->user_name; } } if ($this->deleted != 1) { $this->deleted = 0; } if (!$isUpdate) { if (empty($this->date_entered)) { $this->date_entered = $this->date_modified; } if ($this->set_created_by == true) { // created by should always be this user $this->created_by = isset($current_user) ? $current_user->id : ""; } if ($this->new_with_id == false) { $this->id = create_guid(); } } require_once "data/BeanFactory.php"; BeanFactory::registerBean($this->module_name, $this); if (empty($GLOBALS['updating_relationships']) && empty($GLOBALS['saving_relationships']) && empty($GLOBALS['resavingRelatedBeans'])) { $GLOBALS['saving_relationships'] = true; // let subclasses save related field changes $this->save_relationship_changes($isUpdate); $GLOBALS['saving_relationships'] = false; } if ($isUpdate && !$this->update_date_entered) { unset($this->date_entered); } // call the custom business logic $custom_logic_arguments['check_notify'] = $check_notify; $this->call_custom_logic("before_save", $custom_logic_arguments); unset($custom_logic_arguments); // If we're importing back semi-colon separated non-primary emails if ($this->hasEmails() && !empty($this->email_addresses_non_primary) && is_array($this->email_addresses_non_primary)) { // Add each mail to the account foreach ($this->email_addresses_non_primary as $mail) { $this->emailAddress->addAddress($mail); } $this->emailAddress->save($this->id, $this->module_dir); } if (isset($this->custom_fields)) { $this->custom_fields->bean = $this; $this->custom_fields->save($isUpdate); } // use the db independent query generator $this->preprocess_fields_on_save(); //construct the SQL to create the audit record if auditing is enabled. $auditDataChanges = array(); if ($this->is_AuditEnabled()) { if ($isUpdate && !isset($this->fetched_row)) { $GLOBALS['log']->debug('Auditing: Retrieve was not called, audit record will not be created.'); } else { $auditDataChanges = $this->db->getAuditDataChanges($this); } } $this->_sendNotifications($check_notify); if ($isUpdate) { $this->db->update($this); } else { $this->db->insert($this); } if (!empty($auditDataChanges) && is_array($auditDataChanges)) { foreach ($auditDataChanges as $change) { $this->db->save_audit_records($this, $change); } } if (empty($GLOBALS['resavingRelatedBeans'])) { SugarRelationship::resaveRelatedBeans(); } // populate fetched row with current bean values foreach ($auditDataChanges as $change) { $this->fetched_row[$change['field_name']] = $change['after']; } //If we aren't in setup mode and we have a current user and module, then we track if (isset($GLOBALS['current_user']) && isset($this->module_dir)) { $this->track_view($current_user->id, $this->module_dir, 'save'); } $this->call_custom_logic('after_save', ''); //Now that the record has been saved, we don't want to insert again on further saves $this->new_with_id = false; $this->in_save = false; return $this->id; }