#!/usr/bin/env php
* Copyright 2001-2012 Strangecode, LLC
*
* This file is part of The Strangecode Codebase.
*
* The Strangecode Codebase is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* The Strangecode Codebase is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* The Strangecode Codebase. If not, see .
*/
/**
* module.cli.php
*/
if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) {
// First arg is path to current site. Realpath removes trailing /s
define('COMMON_BASE', realpath($_SERVER['argv'][1]));
} else {
die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n");
}
include_once dirname(__FILE__) . '/../_config.inc.php';
$op = null;
$valid_ops = array('clean', 'var', 'test', 'admin', 'public');
// Test for arguments.
if (isset($_SERVER['argv'][2]) && isset($_SERVER['argv'][3])) {
$module_name_singular = $_SERVER['argv'][2];
$module_name_plural = $_SERVER['argv'][3];
} else {
die(basename($_SERVER['argv'][0]) . " error: missing arguments.
Usage: " . basename($_SERVER['argv'][0]) . " SITE_DIRECTORY NAME_SINGULAR NAME_PLURAL OPERATION
Example: export DB_NAME=mydb; " . basename($_SERVER['argv'][0]) . " . order orders admin
Valid operations include: " . join(', ', $valid_ops) . "
The following files will be generated by this script:
‘admin’ operation:
SITE_DIRECTORY/admin/nameplural.php
SITE_DIRECTORY/admin/_templates/namesingular_list.ihtml
SITE_DIRECTORY/admin/_templates/namesingular_form.ihtml
‘public’ operation:
SITE_DIRECTORY/html/nameplural.php
SITE_DIRECTORY/html/_templates/namesingular_list.ihtml
SITE_DIRECTORY/html/_templates/namesingular.ihtml
");
}
// Test for valid operation.
$op = $_SERVER['argv'][4];
// Make sure op is valid.
if (!in_array($op, $valid_ops)) {
die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n");
}
switch ($op) {
case 'test' :
echo "RUNNING MODULE MAKER IN TEST MODE.\n\n";
break;
case 'admin' :
case 'public' :
echo "Generating only $op files…\n\n";
break;
}
/********************************************************************
* CONFIG
********************************************************************/
// Where deleted files go:
$user_trash_folder = $_SERVER['HOME'] . '/.Trash';
// Directories
$skel_dir = realpath(dirname(__FILE__) . '/skel');
$admin_dir = realpath(COMMON_BASE . '/admin');
$admin_tpl_dir = realpath(COMMON_BASE . '/admin/_templates');
$public_dir = realpath(COMMON_BASE . '/html');
$public_tpl_dir = realpath(COMMON_BASE . '/html/_templates');
// Names
$module_title = ucwords(str_replace('_', ' ', $module_name_plural));
$item_title = ucwords(str_replace('_', ' ', $module_name_singular));
$module_name_upper = mb_strtoupper(str_replace('_', ' ', $module_name_plural));
// Admin files
$admin_script = $module_name_plural . '.php';
$admin_list_template = $module_name_singular . '_list.ihtml';
$admin_form_template = $module_name_singular . '_form.ihtml';
// Public files
$public_script = $module_name_plural . '.php';
$public_list_template = $module_name_singular . '_list.ihtml';
$public_detail_template = $module_name_singular . '_view.ihtml';
// Database names
$db_tbl = $module_name_singular . '_tbl';
$primary_key = $module_name_singular . '_id';
// Only after we've defined essential files can we clean.
if ('clean' == $op) {
echo "Beginning file cleanup\n";
trashFile("$admin_dir/$admin_script");
trashFile("$admin_tpl_dir/$admin_list_template");
trashFile("$admin_tpl_dir/$admin_form_template");
trashFile("$public_dir/$public_script");
trashFile("$public_tpl_dir/$public_list_template");
trashFile("$public_tpl_dir/$public_detail_template");
echo "End file cleanup\n";
die;
}
// Go!
echo 'Running Module Maker. Using database: ' . $app->getParam('db_name') . "\n";
/********************************************************************
* PREPROCESSING
********************************************************************/
// Ensure skel files exist.
if ('admin' == $op && (!file_exists("$skel_dir/admin.php") || !file_exists("$skel_dir/adm_list.ihtml") || !file_exists("$skel_dir/adm_form.ihtml"))) {
die(basename($_SERVER['argv'][0]) . " Warning: one or more admin skeleton source files missing. Please check directory: $skel_dir.\n");
}
if ('public' == $op && (!file_exists("$skel_dir/public.php") || !file_exists("$skel_dir/public.ihtml") || !file_exists("$skel_dir/public_list.ihtml"))) {
die(basename($_SERVER['argv'][0]) . " Warning: one or more public skeleton source files missing. Please check directory: $skel_dir.\n");
}
if (!isset($_SERVER['argv'][4]) || 'var' != $_SERVER['argv'][4]) {
// Ensure essential directories exist, except for 'var' op.
if ('admin' == $op && !is_dir("$admin_dir")) {
die(basename($_SERVER['argv'][0]) . " Error: admin_dir '$admin_dir' directory not found.\n");
}
if ('admin' == $op && !is_dir("$admin_tpl_dir")) {
die(basename($_SERVER['argv'][0]) . " Error: admin_tpl_dir '$admin_tpl_dir' directory not found.\n");
}
if ('public' == $op && !is_dir("$public_dir")) {
die(basename($_SERVER['argv'][0]) . " Error: public_dir '$public_dir' directory not found.\n");
}
if ('public' == $op && !is_dir("$public_tpl_dir")) {
die(basename($_SERVER['argv'][0]) . " Error: public_tpl_dir '$public_tpl_dir' directory not found.\n");
}
}
// Get DB tables.
$qid = $db->query("SHOW TABLES");
while (list($row) = mysql_fetch_row($qid)) {
$tables[] = $row;
}
// Make sure requested table is in database.
if (!isset($tables)) {
die(sprintf("%s Warning: %s does not exist in database %s. No DB tables found!", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name')));
}
if (!in_array($db_tbl, $tables)) {
die(sprintf("%s Warning: %s does not exist in database %s. Please select one of: \n\n%s\n\n", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name'), join("\n", $tables)));
}
// Ensure requested table contains columns.
// Get DB table column info.
$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
while ($row = mysql_fetch_row($qid)) {
$cols[] = $row;
}
if (!is_array($cols) || empty($cols)) {
die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
}
/******************************************************************************
* SETUP VARIABLES
*****************************************************************************/
// Loop through columns
$upload_file_capability = false;
$headers = array();
$public_list_page_vars = array();
$public_detail_page_vars = array();
$set_values_default = array();
if (is_array($cols) && !empty($cols)) {
foreach ($cols as $col) {
// Human readable.
$field = $col[0];
$field_title = ucfirst(str_replace('_', ' ', $field));
$type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
$default = $col[4];
if (is_null($default) || mb_strpos($default, '0000') !== false || '0' == $default) {
$default = '';
}
// Get primary key.
// if ('PRI' == $col[3]) {
// $primary_key = $field;
// }
// Our form will require type="multipart/form-data".
if (preg_match('/file|image/i', $field)) {
$upload_file_capability = true;
}
// Column headers.
$headers[$field] = $field_title;
// Get php code for printing variables.
$public_list_page_vars[] = "<\x3fphp echo oTxt(\$" . $module_name_singular . "_list[\$i]['$field']); \x3f>";
$public_detail_page_vars[] = "<\x3fphp echo oTxt(\$item['$field']); \x3f>";
$set_values_default[] = "'$field' => '$default'";
}
}
// -------------FILES-------------
$skel_files = array();
$skel_files['skel_admin_script'] = file_get_contents($skel_dir . '/admin.php');
$skel_files['skel_admin_list_template'] = file_get_contents($skel_dir . '/adm_list.ihtml');
$skel_files['skel_admin_form_template'] = file_get_contents($skel_dir . '/adm_form.ihtml');
$skel_files['skel_public_script'] = file_get_contents($skel_dir . '/public.php');
$skel_files['skel_public_list_template'] = file_get_contents($skel_dir . '/public_list.ihtml');
$skel_files['skel_public_detail_template'] = file_get_contents($skel_dir . '/public.ihtml');
// Search-replace variables ----------------------------------
// Admin script file upload replacement routines...
$search['admin_form_tag_init'] = '/%ADMIN_FORM_TAG_INIT%/';
$replace['admin_form_tag_init'] = "