* @version     3.1.0
 * @license     http://www.gnu.org/licenses/gpl-3.0.txt GNU License 3.0
 * @package     Heurist academic knowledge management system
 * @subpackage  XForms
 * @uses        HEURIST_DBNAME
 * @uses        HEURIST_BASE_URL
 */
require_once dirname(__FILE__) . '/../../common/connect/applyCredentials.php';
require_once dirname(__FILE__) . '/../../common/php/dbMySqlWrappers.php';
require_once dirname(__FILE__) . '/../../common/php/getRecordInfoLibrary.php';
require_once dirname(__FILE__) . '/../../admin/describe/rectypeXFormLibrary.php';
mysql_connection_select(DATABASE);
if (mysql_error()) {
    die("Could not get database structure from given database source, MySQL error - unable to connect to database.");
}
if (!is_logged_in()) {
    header('Location: ' . HEURIST_BASE_URL . 'common/connect/login.php?db=' . HEURIST_DBNAME);
    return;
}
$rtyID = @$_REQUEST['rtyID'] ? $_REQUEST['rtyID'] : null;
if (!$rtyID) {
    print "<html><head><link rel=stylesheet href='../../common/css/global.css'></head><body><div class=wrap><div id=errorMsg><span>You must supply a rectype ID</span><p></div></div></body></html>";
    return;
}
list($form, $rtName, $rtConceptID, $rtDescription, $report) = buildform($rtyID);
if (!$form) {
    echo $report;
} else {
    echo $form;
}
return;
/**
 * Creates xform for $rtyID, saves it into HEURIST_UPLOAD_DIR/xforms/ folder and adds an entry to the manifest lists
 * @global   string [$folder] path of where to store output form
 * @global   string [$formsList] manifest stream of forms oldstyle ODK
 * @global   string [$xformsList] manifest stream of xforms
 * @param    integer [$rtyID] the local id of the recType to creat the xform for
 * @return   string report about success or failure of the forms creation
 * @uses     buildform() from rectypeXFormLibrary to build the form
 * @uses     HEURIST_DBNAME
 */
function createform($rtyID)
{
    global $folder, $formsList, $xformsList;
    try {
        list($form, $rtName, $rtConceptID, $rtDescription, $report) = buildform($rtyID);
        if ($form) {
            $filename = preg_replace('/[^a-zA-Z0-9-_\\.]/', '', $rtName);
            //todo this is not international, need to strip only illegal filesys characters and perhaps trim spaces to single space
            $fullfilename = $folder . $filename . ".xml";
            file_put_contents($fullfilename, $form);
            //			chgrp($fullfilename,"acl");
            //TODO: update formlist for this form.
            $report = $report . "{$rtName} Form Saved ok ({$fullfilename})<br/>";
            $xformEntry = "<xform>\n" . "<downloadUrl>http://heuristscholar.org/hayes/xforms/{$filename}.xml</downloadUrl>\n" . "<formID>{$rtConceptID}</formID>\n" . "<name>{$rtName} Record Form</name>\n" . "<descriptionText>{$rtName} Record as defined in the \"" . HEURIST_DBNAME . "\" database described as \"{$rtDescription}\" </descriptionText>\n" . "<majorMinorVersion>" . date("Ymd") . "</majorMinorVersion>\n" . "<version>" . date("Ymd") . "</version>\n" . "<hash>md5:" . md5_file($fullfilename) . "</hash>\n" . "</xform>\n";
            $formEntry = "<form url=\"http://heuristscholar.org/hayes/xforms/{$filename}.xml\">{$rtName}</form>\n";
        }
    } catch (Exception $e) {
        $report = $report . 'Exception ' . $e->getMessage();
        $formEntry = $xformEntry = "";
    }
    $formsList .= $formEntry ? $formEntry : "";
    $xformsList .= $xformEntry ? $xformEntry : "";
    return "<h2>{$rtName}</h2>" . $report . "<br/>";
}