/**
 * 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;
}
function createRectypes($commonNames, $rt, $isAddDefaultSetOfFields)
{
    global $db, $rtyColumnNames;
    $ret = null;
    if (count($commonNames)) {
        $colNames = join(",", $commonNames);
        $parameters = array("");
        $titleMask = null;
        $query = "";
        foreach ($commonNames as $colName) {
            $val = array_shift($rt[0]['common']);
            if ($query != "") {
                $query = $query . ",";
            }
            $query = $query . "?";
            $parameters[0] = $parameters[0] . $rtyColumnNames[$colName];
            array_push($parameters, $val);
            //keep value of text title mask to create canonical one
            if ($colName == "rty_TitleMask") {
                $titleMask = $val;
            }
        }
        $query = "insert into defRecTypes ({$colNames}) values ({$query})";
        $rows = execSQL($db, $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 Designer view > Record types";
        } else {
            if ($rows == 0 || is_string($rows)) {
                $ret = "SQL error inserting data into table defRecTypes: " . $rows;
            } else {
                $rtyID = $db->insert_id;
                $ret = -$rtyID;
                if ($isAddDefaultSetOfFields) {
                    //add default set of detail types
                    addDefaultFieldForNewRecordType($rtyID);
                    //create canonical title mask
                    updateCanonicalTitleMask($rtyID, $titleMask);
                }
                //create icon and thumbnail
                getRectypeIconURL($rtyID);
                getRectypeThumbURL($rtyID);
            }
        }
    }
    if ($ret == null) {
        $ret = "no data supplied for inserting rectype";
    }
    return $ret;
}