<?php
// *********************************************************************************************************************************
// fm_generate_field_constants.php
//
// Outputs define() statements for the database info, layout name, field names, and table occurence(s) on a layout.
//
//
// v2 July 29th, 2014 Mark DeNyse, Driftwood Interactive, Inc. mark@driftwoodinterative.com
// v1 <in the past> <unknown author>
//
// *********************************************************************************************************************************
require_once('FileMaker.php');
ini_set('display_errors', 'on');
ini_set('error_reporting', E_ALL & ~E_DEPRECATED & ~E_STRICT);
$php = fmGenerateFieldConstants('DATABASE_NAME', 'HOST_NAME', 'USERNAME', 'PASSWORD', 'LAYOUT_NAME', 'TABLE_NAME');
// *********************************************************************************************************************************
//
// fmGenerateFieldConstants
//
// Get the fields on a layout (along with any portals) as a series of PHP define() statements.
// This is useful when you are first creating the field constants for a new layout you're working with.
//
// This function is based on some earlier code I came across years ago. Unfortunately I don't remember
// who it was from. If you know please contact me and I'll note their original contribution!
//
// Mark DeNyse, Driftwood Interactive, Inc. mark@driftwoodinterative.com
//
// *********************************************************************************************************************************
function fmGenerateFieldConstants($dataBase, $hostName, $userName, $password, $layoutName, $tableName = '')
{
    // Tweak these values as per your personal/business preference
    $fieldPrefix = 'FM_';
    $fieldSuffix = '_FIELD';
    $keyValueSeparator = " ";    // Some people like tabs "\t"
    $newLine = "\n";
    // If anyone can find a way to programmatically determine the base table name from a layout please let me know!
    // In the meantime, we pass in our name or leave it blank and use the layout name as our 'table' name.
    $tableName = (($tableName != '') ? $tableName : $layoutName);
    $fm = new FileMaker($dataBase, $hostName, $userName, $password);
    $layoutObject = $fm->getLayout($layoutName);
    if (FileMaker::IsError($layoutObject)) {
        $php = 'Error - layout not found. Error code = '. $layoutObject->getCode() .' '. $layoutObject->getMessage() .'.';
    }
    else {
        $php = "<"."?"."php". $newLine;
        $php .= $newLine;
        $php .= "// Database definition". $newLine;
        $php .= "define('FM_". strtoupper(str_replace(array(':', '.'), '_', $hostName)) ."_HOST',". $keyValueSeparator. "'". $hostName ."');". $newLine;
        $php .= "define('FM_". strtoupper(str_replace(array(':', '.'), '_', $hostName)) ."_DATABASE',". $keyValueSeparator. "'". $dataBase ."');". $newLine;
        $php .= "define('FM_". strtoupper($dataBase) ."_USERNAME',". $keyValueSeparator. "'". $userName ."');". $newLine;
        $php .= "define('FM_". strtoupper($dataBase) ."_PASSWORD',". $keyValueSeparator. "'". $password ."');". $newLine;
        $php .= $newLine;
        $php .= "// '". $layoutName ."' Layout". $newLine;
        $php .= "define('FM_". strtoupper($layoutName) ."_LAYOUT',". $keyValueSeparator. "'". $layoutName ."');". $newLine;
        $php .= $newLine;
        $php .= "// '". (($tableName != '') ? $tableName : $layoutName) ."' Table". $newLine;
        // First dump out the fields on the layout
        $fieldObjects = $layoutObject->getFields();
        foreach ($fieldObjects as $fieldObject) {
            $fieldName = $fieldObject->getName();
            $php .= "define('". $fieldPrefix . strtoupper((($tableName != '') ? $tableName .'__' : '') . str_replace(array('::', '.', ' '), '_', $fieldName)) . $fieldSuffix ."',". $keyValueSeparator. "'". $fieldName ."');". $newLine;
        }
        // Now do any portals. A constant for the table occurence name is also generated.
        $relatedSets = $layoutObject->getRelatedSets();
        if (! FileMaker::IsError($relatedSets)) {
            foreach($relatedSets as $relatedSet) {
                $php .= $newLine;
                $relatedSetName = $relatedSet->getName();
                $php .= "// '". $relatedSetName ."' Table Occurrence". $newLine;
                $toStripped = str_replace(array(':', '.', ' '), '_', $relatedSetName);
                $php .= "define('FM_TO_". $toStripped ."',". $keyValueSeparator. "'". $relatedSetName ."');". $newLine;
                $php .= $newLine;
                $fieldNames = $relatedSet->listFields();
                foreach($fieldNames as $fieldName) {
                    $fieldName = str_replace($relatedSetName .'::', '', $fieldName);        // Strip off related set name and ::
                    $php .= "define('". $fieldPrefix. $toStripped .'_'. str_replace(array('.', ' '), '_', $fieldName) . $fieldSuffix ."',". $keyValueSeparator. "'". $fieldName ."');". $newLine;
                }
            }
        }
        $php .= $newLine;
        $php .= "?".">". $newLine;
    }
    return $php;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>FileMaker PHP Database Definitions File Generator</title>
    </head>
    <body>
        <?php echo highlight_string($php, true); ?>
    </body>
</html>