/**
 * createRectypes - Function that inserts a new rectype into defRecTypes table.and use the rty_ID to insert any
 * fields into the defRecStructure table
 * @author Stephen White
 * @param $commonNames an array valid column names in the defRecTypes table which match the order of data in the $rt param
 * @param $dtFieldNames an array valid column names in the defRecStructure table
 * @param $rt astructured array of which can contain the column names and data for one or more rectypes with fields
 * @param $icon_filename - filename from icon library - for new record type ONLY
 * @return $ret an array of return values for the various data elements created or errors if they occurred
 **/
function createRectypes($commonNames, $rt, $isAddDefaultSetOfFields, $convertTitleMask = true, $icon_filename = null)
{
    global $mysqli, $rtyColumnNames;
    $ret = null;
    if (count($commonNames)) {
        $colNames = join(",", $commonNames);
        $parameters = array("");
        $titleMask = null;
        $query = "";
        $querycols = "";
        foreach ($commonNames as $colName) {
            $val = array_shift($rt[0]['common']);
            if (@$rtyColumnNames[$colName]) {
                //keep value of text title mask to create canonical one
                if ($convertTitleMask && $colName == "rty_TitleMask") {
                    $titleMask = $val;
                }
                if ($query != "") {
                    $query = $query . ",";
                    $querycols = $querycols . ",";
                }
                $querycols = $querycols . $colName;
                $query = $query . "?";
                $parameters = addParam($parameters, $rtyColumnNames[$colName], $val);
            }
        }
        $query = "insert into defRecTypes ({$querycols}) values ({$query})";
        $rows = execSQL($mysqli, $query, $parameters, true);
        if ($rows == "1062") {
            $ret = "Record type with specified name already exists in the database, please use the existing record type\nThis type may be hidden - turn it on through Database > Manage structure";
        } else {
            if ($rows == 0 || is_string($rows)) {
                $ret = "SQL error inserting data into table defRecTypes: " . $rows;
            } else {
                $rtyID = $mysqli->insert_id;
                $ret = -$rtyID;
                if ($isAddDefaultSetOfFields) {
                    //add default set of detail types
                    addDefaultFieldForNewRecordType($rtyID);
                }
                //create canonical title mask
                if ($titleMask) {
                    updateTitleMask($rtyID, $titleMask);
                }
                $need_create_icon = true;
                if ($icon_filename) {
                    $need_create_icon = copy_IconAndThumb_FromLibrary($rtyID, $icon_filename);
                }
                //create icon and thumbnail
                if ($need_create_icon) {
                    getRectypeIconURL($rtyID);
                    getRectypeThumbURL($rtyID);
                }
            }
        }
    }
    if ($ret == null) {
        $ret = "no data supplied for inserting record type";
    }
    return $ret;
}
    return;
}
$rt_name = @$_REQUEST['rty_Name'];
$image_icon = getRectypeIconURL($rt_id);
$image_thumbnail = getRectypeThumbURL($rt_id);
$success_msg = null;
$failure_msg = null;
/* TODO: ???????
   require_once(dirname(__FILE__).'/../../../common/php/dbMySqlWrappers.php');
   mysql_connection_select(DATABASE);
   $res = mysql_query('select * from defRecTypes where rty_ID = ' . $rt_id);
   $rt = mysql_fetch_assoc($res);
   */
if (@$_REQUEST['libicon']) {
    //take from  library
    if (copy_IconAndThumb_FromLibrary($rt_id, $_REQUEST['libicon'])) {
        //error
        list($success_msg, $failure_msg) = array('', "Library file: {$filename} couldn't be saved to upload path defined for db = " . HEURIST_DBNAME . " (" . HEURIST_ICON_DIR . "). Please ask your system administrator to correct the path and/or permissions for this directory");
    } else {
        list($success_msg, $failure_msg) = array('Icon and thumbnail have been set successfully', '');
    }
} else {
    //upload new one
    if (@$_FILES['new_thumb']['size'] > 0) {
        list($success_msg, $failure_msg) = upload_file($rt_id, 'new_thumb');
    } else {
        if (@$_FILES['new_icon']['size'] > 0) {
            list($success_msg, $failure_msg) = upload_file($rt_id, 'new_icon');
        }
    }
}