/**
 * returns an array of detailType structured information for all detailTypes
 *     detailTypes = {"groups" => {"groupIDToIndex":{detailTypeGroupID:index},
 *                                       index:{propName:val,...},...},
 *                    "names":{dtyID:name,...},
 *                    "rectypeUsage" => { dtyID => [rtyID,...], ...},
 *                    'usageCount' => {dtyID:nonzero-count,...},
 *                    "typedefs":{"commonFieldNames":[defDetailTypes Column names],
 *                                "commonNamesToIndex":{"defDetailTypes columnName":index,...},
 *                                dtyID:{"commonFields":[val,....]},
 *                                ...},
 *                    'lookups' => {basetype=>displayName);
 *
 * @global    int $dbID databse ID
 * @param     boolean $useCachedData if true does a lookup for the detailtypes structure in cache
 * @return    object information describing all the detailtypes defined in the database
 * @uses      getDetailTypeColNames()
 * @uses      getColumnNameToIndex()
 * @uses      getRectypeStructureFieldColNames()
 * @uses      getDetailTypeGroups()
 * @uses      getDetailTypeUsageCount()
 * @uses      getDetailTypeDefUsage()
 * @uses      getDtLookups()
 * @uses      getCachedData()
 * @uses      setCachedData()
 */
function getAllDetailTypeStructures($useCachedData = false)
{
    global $dbID;
    $cacheKey = DATABASE . ":AllDetailTypeInfo";
    if ($useCachedData) {
        $dtStructs = getCachedData($cacheKey);
        if ($dtStructs) {
            return $dtStructs;
        }
    }
    $dtG = getDetailTypeGroups();
    /*****DEBUG****/
    //error_log(print_r($dtG,true));
    $dtStructs = array('groups' => $dtG, 'names' => array(), 'rectypeUsage' => getDetailTypeDefUsage(), 'usageCount' => getDetailTypeUsageCount(), 'typedefs' => array('commonFieldNames' => getDetailTypeColNames(), 'fieldNamesToIndex' => getColumnNameToIndex(getDetailTypeColNames())), 'lookups' => getDtLookups());
    $query = "select dtg_ID, dtg_Name, " . join(",", getDetailTypeColNames());
    $query = preg_replace("/dty_ConceptID/", "", $query);
    if ($dbID) {
        //if(trm_OriginatingDBID,concat(cast(trm_OriginatingDBID as char(5)),'-',cast(trm_IDInOriginatingDB as char(5))),'null') as trm_ConceptID
        $query .= " if(dty_OriginatingDBID, concat(cast(dty_OriginatingDBID as char(5)),'-',cast(dty_IDInOriginatingDB as char(5))), concat('{$dbID}-',cast(dty_ID as char(5)))) as dty_ConceptID";
    } else {
        $query .= " if(dty_OriginatingDBID, concat(cast(dty_OriginatingDBID as char(5)),'-',cast(dty_IDInOriginatingDB as char(5))), '') as dty_ConceptID";
    }
    $query .= " from defDetailTypes left join defDetailTypeGroups  on dtg_ID = dty_DetailTypeGroupID" . " order by dtg_Order, dtg_Name, dty_OrderInGroup, dty_Name";
    $res = mysql_query($query);
    $dtStructs['sortedNames'] = mysql__select_assoc('defDetailTypes', 'dty_Name', 'dty_ID', '1 order by dty_Name');
    while ($row = mysql_fetch_row($res)) {
        array_push($dtStructs['groups'][$dtG['groupIDToIndex'][$row[0]]]['allTypes'], $row[2]);
        if ($row[17]) {
            // dty_ShowInLists
            array_push($dtStructs['groups'][$dtG['groupIDToIndex'][$row[0]]]['showTypes'], $row[2]);
        }
        $dtStructs['typedefs'][$row[2]]['commonFields'] = array_slice($row, 2);
        $dtStructs['names'][$row[2]] = $row[3];
    }
    setCachedData($cacheKey, $dtStructs);
    /*****DEBUG****/
    //error_log(print_r($dtStructs['typedefs'],true));
    return $dtStructs;
}
//10-rst_DisplayDetailTypeGroupID
//11-rst_FilteredJsonTermIDTree
//12-rst_PtrFilteredIDs
//13-rst_TermIDTreeNonSelectableIDs
//14-rst_CalcFunctionID
//15-rst_Status
//16-rst_OrderForThumbnailGeneration
//17-dty_TermIDTreeNonSelectableIDs
//18-dty_FieldSetRectypeID
//19-rst_NonOwnerVisibility]....]
//rst_DisplayName, rst_DisplayHelpText, rst_DisplayExtendedDescription,
// rst_DefaultValue, rst_RequirementType, rst_MaxValues, rst_MinValues, rst_DisplayWidth, rst_RecordMatchOrder,
// rst_DisplayOrder, rst_DisplayDetailTypeGroupID, rst_FilteredJsonTermIDTree, rst_PtrFilteredIDs,
// rst_TermIDTreeNonSelectableIDs, rst_CalcFunctionID, rst_Status, rst_OrderForThumbnailGeneration,
// dty_TermIDTreeNonSelectableIDs, dty_FieldSetRectypeID, rst_NonOwnerVisibility] ...]
$rstC2I = getColumnNameToIndex(getRectypeStructureFieldColNames());
foreach ($rec_types as $rec_type) {
    foreach (getRectypeFields($rec_type) as $dtyID => $rdr) {
        // saw TODO need to represent the trm ids  and rectype pointer ids that are valid for this rectype.detailtype.
        array_push($detailRequirements, array($rec_type, $dtyID, $rstC2I['rst_RequirementType'] ? $rdr[$rstC2I['rst_RequirementType']] : null, $rstC2I['rst_MaxValues'] && $rdr[$rstC2I['rst_MaxValues']] ? intval($rdr[$rstC2I['rst_MaxValues']]) : null, array_key_exists('rst_DisplayName', $rstC2I) ? $rdr[$rstC2I['rst_DisplayName']] : null, $rstC2I['rst_DisplayHelpText'] ? $rdr[$rstC2I['rst_DisplayHelpText']] : null, $rstC2I['rst_RecordMatchOrder'] ? intval($rdr[$rstC2I['rst_RecordMatchOrder']]) : 0, $rstC2I['rst_DisplayWidth'] ? intval($rdr[$rstC2I['rst_DisplayWidth']]) : 0, $rstC2I['rst_DisplayOrder'] ? intval($rdr[$rstC2I['rst_DisplayOrder']]) : 0, $rstC2I['rst_DisplayExtendedDescription'] ? $rdr[$rstC2I['rst_DisplayExtendedDescription']] : null, $rstC2I['rst_DefaultValue'] ? $rdr[$rstC2I['rst_DefaultValue']] : null, $rstC2I['rst_MinValues'] ? intval($rdr[$rstC2I['rst_MinValues']]) : 0, $rstC2I['rst_DisplayDetailTypeGroupID'] ? $rdr[$rstC2I['rst_DisplayDetailTypeGroupID']] : null, $rstC2I['rst_FilteredJsonTermIDTree'] ? $rdr[$rstC2I['rst_FilteredJsonTermIDTree']] : null, $rstC2I['rst_TermIDTreeNonSelectableIDs'] ? $rdr[$rstC2I['rst_TermIDTreeNonSelectableIDs']] : null, $rstC2I['dty_TermIDTreeNonSelectableIDs'] ? $rdr[$rstC2I['dty_TermIDTreeNonSelectableIDs']] : null, $rstC2I['rst_PtrFilteredIDs'] ? $rdr[$rstC2I['rst_PtrFilteredIDs']] : null, $rstC2I['rst_CalcFunctionID'] ? $rdr[$rstC2I['rst_CalcFunctionID']] : null, $rstC2I['rst_OrderForThumbnailGeneration'] ? $rdr[$rstC2I['rst_OrderForThumbnailGeneration']] : null, $rstC2I['rst_Status'] ? $rdr[$rstC2I['rst_Status']] : null, $rstC2I['rst_NonOwnerVisibility'] ? $rdr[$rstC2I['rst_NonOwnerVisibility']] : null));
        //20-Non-Owner Visibility
    }
}
$commonData = array("users" => $users, "workgroups" => $workgroups, "ratings" => array("0" => "not rated", "1" => "*", "2" => "**", "3" => "***", "4" => "****", "5" => "*****"), "recordTypes" => $recordTypes, "detailTypes" => $detailTypes, "detailRequirements" => $detailRequirements, 'max_post_size' => _get_config_bytes(ini_get('post_max_size')), 'max_file_size' => _get_config_bytes(ini_get('upload_max_filesize')));
if (!@$_REQUEST["json"]) {
    print "var HAPI_commonData = ";
}
print json_encode($commonData);
if (!@$_REQUEST["json"]) {
    print ";\n";
}