function handleSave($prefix, $redirect = true, $useRequired = false)
 {
     global $theme, $current_user;
     require_once 'include/formbase.php';
     $galleria = $_POST['galleria_c'];
     $focus = new RealtyTemplates();
     if (!empty($_POST[$prefix . 'new_reports_to_id'])) {
         $focus->retrieve($_POST[$prefix . 'new_reports_to_id']);
         $focus->reports_to_id = $_POST[$prefix . 'record'];
     } else {
         $focus = populateFromPost($prefix, $focus);
     }
     if (isset($galleria)) {
         $focus->galleria_c = $galleria;
     }
     if ($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
         return null;
     }
     if (!$focus->ACLAccess('Save')) {
         ACLController::displayNoAccess(true);
         sugar_cleanup(true);
     }
     if (isset($GLOBALS['check_notify'])) {
         $check_notify = $GLOBALS['check_notify'];
     } else {
         $check_notify = FALSE;
     }
     if (empty($_POST['dup_checked'])) {
         $duplicateRealtyTemplates = $this->checkForDuplicates($prefix);
         if (isset($duplicateRealtyTemplates)) {
             $focus->possible_duplicate = 1;
             $_SESSION['duplicateRealtyTemplates'] = $duplicateRealtyTemplates;
             $location = 'module=RealtyTemplates&action=ShowDuplicates&record=' . $_POST['record'];
             $get = '';
             if (isset($_POST['inbound_email_id']) && !empty($_POST['inbound_email_id'])) {
                 $get .= '&inbound_email_id=' . $_POST['inbound_email_id'];
             }
             // Bug 25311 - Add special handling for when the form specifies many-to-many relationships
             if (isset($_POST['relate_to']) && !empty($_POST['relate_to'])) {
                 $get .= '&RealtyTemplatesrelate_to=' . $_POST['relate_to'];
             }
             if (isset($_POST['relate_id']) && !empty($_POST['relate_id'])) {
                 $get .= '&RealtyTemplatesrelate_id=' . $_POST['relate_id'];
             }
             //add all of the post fields to redirect get string
             foreach ($focus->column_fields as $field) {
                 if (!empty($focus->{$field}) && !is_object($focus->{$field})) {
                     $get .= "&RealtyTemplates{$field}=" . urlencode($focus->{$field});
                 }
             }
             foreach ($focus->additional_column_fields as $field) {
                 if (!empty($focus->{$field})) {
                     $get .= "&RealtyTemplates{$field}=" . urlencode($focus->{$field});
                 }
             }
             if ($focus->hasCustomFields()) {
                 foreach ($focus->field_defs as $name => $field) {
                     if (!empty($field['source']) && $field['source'] == 'custom_fields') {
                         $get .= "&RealtyTemplates{$name}=" . urlencode($focus->{$name});
                     }
                 }
             }
             $emailAddress = new SugarEmailAddress();
             $get .= $emailAddress->getFormBaseURL($focus);
             //create list of suspected duplicate realtytemplates id's in redirect get string
             $i = 0;
             foreach ($duplicateRealtyTemplates as $realtytemplates) {
                 $get .= "&duplicate[{$i}]=" . $realtytemplates['id'];
                 $i++;
             }
             //add return_module, return_action, and return_id to redirect get string
             $get .= "&return_module=";
             if (!empty($_POST['return_module'])) {
                 $get .= $_POST['return_module'];
             } else {
                 $get .= "RealtyTemplates";
             }
             $get .= "&return_action=";
             if (!empty($_POST['return_action'])) {
                 $get .= 'EditView';
             }
             //else $get .= "DetailView";
             if (!empty($_POST['return_id'])) {
                 $get .= "&return_id=" . $_POST['return_id'];
             }
             if (!empty($_POST['popup'])) {
                 $get .= '&popup=' . $_POST['popup'];
             }
             if (!empty($_POST['create'])) {
                 $get .= '&create=' . $_POST['create'];
             }
             // for InboundEmail flow
             if (!empty($_POST['start'])) {
                 $get .= '&start=' . $_POST['start'];
             }
             $_SESSION['SHOW_DUPLICATES'] = $get;
             //now redirect the post to modules/RealtyTemplates/ShowDuplicates.php
             if (!empty($_POST['is_ajax_call']) && $_POST['is_ajax_call'] == '1') {
                 ob_clean();
                 $json = getJSONobj();
                 echo $json->encode(array('status' => 'dupe', 'get' => $location));
             } else {
                 if (!empty($_REQUEST['ajax_load'])) {
                     echo "<script>SUGAR.ajaxUI.loadContent('index.php?{$location}');</script>";
                 } else {
                     if (!empty($_POST['to_pdf'])) {
                         $location .= '&to_pdf=' . $_POST['to_pdf'];
                     }
                     header("Location: index.php?{$location}");
                 }
             }
             return null;
         }
     }
     $focus->save($check_notify);
     $return_id = $focus->id;
     $GLOBALS['log']->debug("Saved record with id of " . $return_id);
     //    $focus->load_relationship('realtytemplates_realtytemplates_1');
     //    $focus->realtytemplates_realtytemplates_1->delete($focus->id);
     //    foreach ($_SESSION['duplicateRealtyTemplates'] as $realtytemplates)
     //    {
     //        $focus->realtytemplates_realtytemplates_1->add($realtytemplates['id']);
     //    }
     $_SESSION['duplicateRealtyTemplates'] = array();
     if (isset($_POST['popup']) && $_POST['popup'] == 'true') {
         $get = '&module=';
         if (!empty($_POST['return_module'])) {
             $get .= $_POST['return_module'];
         } else {
             $get .= 'RealtyTemplates';
         }
         $get .= '&action=';
         if (!empty($_POST['return_action'])) {
             $get .= $_POST['return_action'];
         } else {
             $get .= 'Popup';
         }
         if (!empty($_POST['return_id'])) {
             $get .= '&return_id=' . $_POST['return_id'];
         }
         if (!empty($_POST['popup'])) {
             $get .= '&popup=' . $_POST['popup'];
         }
         if (!empty($_POST['create'])) {
             $get .= '&create=' . $_POST['create'];
         }
         if (!empty($_POST['to_pdf'])) {
             $get .= '&to_pdf=' . $_POST['to_pdf'];
         }
         $get .= '&name=' . urlencode($focus->name);
         $get .= '&query=true';
         header("Location: index.php?{$get}");
         return;
     }
     if ($redirect) {
         $this->handleRedirect($return_id);
     } else {
         return $focus;
     }
 }
<?php

$tpl_id = $_REQUEST['tpl_id'];
$floor = $_REQUEST['floor'];
$building_id = $_REQUEST['building_id'];
$sec_id = "";
if (isset($_REQUEST['section_id']) && !empty($_REQUEST['section_id'])) {
    $sec_id = $_REQUEST['section_id'];
}
$db = DBManagerFactory::getInstance();
if (!empty($tpl_id) && !empty($floor) && !empty($building_id)) {
    $realty_new = new Realty();
    $realty_tpl = new RealtyTemplates();
    $realty_tpl->retrieve($tpl_id);
    foreach ($realty_new->field_defs as $key => $val) {
        if ($key != "id" && $key != "floor" && isset($realty_tpl->field_defs[$key])) {
            $realty_new->{$key} = $realty_tpl->{$key};
        } elseif ($key == "floor") {
            $realty_new->floor = $floor;
        } elseif ($key == "floor") {
            $realty_new->floor = $floor;
        }
    }
    if (!empty($sec_id)) {
        $realty_new->sections_exist = "yes";
        $realty_new->section_id = $sec_id;
    }
    $id = $realty_new->save();
    if (!empty($id)) {
        echo "Объект успешно создан!";
    }