#!/usr/local/bin/php -q * Copyright 2001-2010 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 */ include_once dirname(__FILE__) . '/_config.inc.php'; $op = null; $valid_ops = array('clean', 'var', 'test'); // 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: invalid arguments. Try like this: " . basename($_SERVER['argv'][0]) . " site_directory name_singular name_plural [operation] Valid operations include: " . join(', ', $valid_ops) . " The following files will be generated by this script: /admin/nameplural.php /admin/_templates/namesingular_list.ihtml /admin/_templates/namesingular_form.ihtml /html/nameplural.php /html/_templates/namesingular_list.ihtml /html/_templates/namesingular.ihtml "); } // Test for operation. if (isset($_SERVER['argv'][4])) { // Optional fourth arg is op. $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; default : } /******************************************************************** * 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 ( !file_exists("$skel_dir/admin.php") || !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 skeleton source files missing. Please check directory: $skel_dir.\n"); } // Ensure essential directories exist. if (!is_dir("$admin_dir")) { die(basename($_SERVER['argv'][0]) . " Error: admin_dir '$admin_dir' directory not found.\n"); } if (!is_dir("$admin_tpl_dir")) { die(basename($_SERVER['argv'][0]) . " Error: admin_tpl_dir '$admin_tpl_dir' directory not found.\n"); } if (!is_dir("$public_dir")) { die(basename($_SERVER['argv'][0]) . " Error: public_dir '$public_dir' directory not found.\n"); } if (!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 (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['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'] = "
\" class=\"sc-form\">"; $search['admin_upload_include'] = '/%ADMIN_UPLOAD_INCLUDE%/'; $replace['admin_upload_include'] = ''; $search['admin_upload_config'] = '/%ADMIN_UPLOAD_CONFIG%/'; $replace['admin_upload_config'] = ''; $search['admin_upload_init'] = '/%ADMIN_UPLOAD_INIT%/'; $replace['admin_upload_init'] = ''; $search['admin_upload_del'] = '/%ADMIN_UPLOAD_DEL%/'; $replace['admin_upload_del'] = ''; $search['admin_upload_insert'] = '/%ADMIN_UPLOAD_INSERT%/'; $replace['admin_upload_insert'] = ''; $search['admin_upload_update'] = '/%ADMIN_UPLOAD_UPDATE%/'; $replace['admin_upload_update'] = ''; if ($upload_file_capability) { // Form arguments $replace['admin_form_tag_init'] = "\" class=\"sc-form\">\n\" />"; // Include statement. $replace['admin_upload_include'] = "require_once 'codebase/lib/Upload.inc.php';\n"; // Config $replace['admin_upload_config'] = <<setParam(array( 'upload_path' => COMMON_BASE . '/html/_db_files/__///__', 'dest_file_perms' => 0666, 'allow_overwriting' => false, 'valid_file_extensions' => array('jpg', 'gif', 'png', 'jpeg'), )); E_O_F; // Main init. $replace['admin_upload_init'] = <<getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')) { \$upload->deleteFile(\$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')); } E_O_F; // Insert 1. $replace['admin_upload_insert'] = <<process('__///__', \$%PRIMARY_KEY% . '_' . getFormData('__///__')); // If file upload errors, redirect to edit operation for the inserted record. if (\$upload->anyErrors() || false === \$new_file) { \$app->dieURL(\$_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . \$%PRIMARY_KEY%); } E_O_F; // Update. $replace['admin_upload_update'] = <<getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*'); // Process new file upload with prepended primary key. \$new_file = \$upload->process('__///__', getFormData('%PRIMARY_KEY%') . '_' . getFormData('__///__')); if (false === \$new_file || \$upload->anyErrors()) { // Upload failed. Reload form. Display errors. \$frm =& editRecordForm(getFormData('%PRIMARY_KEY%')); \$frm = array_merge(\$frm, getFormData()); \$nav->add(_("Edit %ITEM_TITLE%")); \$main_template = '%ADMIN_FORM_TEMPLATE%'; break; } else { // Upload succeeded. Delete old files. if ('' != \$old_file && \$old_file != \$new_file[0]['name']) { \$upload->deleteFile(\$old_file); } } } E_O_F; } // End upload_file_capability. // Simple... $search['date'] = '/%DATE%/'; $replace['date'] = date($app->getParam('date_format')); $search['name_plural'] = '/%NAME_PLURAL%/'; $replace['name_plural'] = $module_name_plural; $search['name_singular'] = '/%NAME_SINGULAR%/'; $replace['name_singular'] = $module_name_singular; $search['title'] = '/%TITLE%/'; $replace['title'] = $module_title; $search['item_title'] = '/%ITEM_TITLE%/'; $replace['item_title'] = $item_title; $search['primary_key'] = '/%PRIMARY_KEY%/'; $replace['primary_key'] = $primary_key; $search['db_tbl'] = '/%DB_TBL%/'; $replace['db_tbl'] = $db_tbl; $search['name_upper'] = '/%NAME_UPPER%/'; $replace['name_upper'] = $module_name_upper; $search['admin_script'] = '/%ADMIN_SCRIPT%/'; $replace['admin_script'] = $admin_script; $search['admin_list_template'] = '/%ADMIN_LIST_TEMPLATE%/'; $replace['admin_list_template'] = $admin_list_template; $search['admin_form_template'] = '/%ADMIN_FORM_TEMPLATE%/'; $replace['admin_form_template'] = $admin_form_template; $search['public_script'] = '/%PUBLIC_SCRIPT%/'; $replace['public_script'] = $public_script; $search['public_list_template'] = '/%PUBLIC_LIST_TEMPLATE%/'; $replace['public_list_template'] = $public_list_template; $search['public_detail_template'] = '/%PUBLIC_DETAIL_TEMPLATE%/'; $replace['public_detail_template'] = $public_detail_template; $search['public_detail_page_vars'] = '/%PUBLIC_DETAIL_PAGE_VARS%/'; $replace['public_detail_page_vars'] = join("\n", $public_detail_page_vars); $search['public_list_page_vars'] = '/%PUBLIC_LIST_PAGE_VARS%/'; $replace['public_list_page_vars'] = join("\n", $public_list_page_vars); $search['set_values_default'] = '/%SET_VALUES_DEFAULT%/'; $replace['set_values_default'] = join(",\n ", $set_values_default); $search['search_fields'] = '/%SEARCH_FIELDS%/'; $replace['search_fields'] = join(", ", $headers); // Complex.... echo "Generating admin form table rows...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/form_template.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val); if ($return_val == 0) { $search['adm_form_table_rows'] = '/%ADM_FORM_TABLE_ROWS%/'; $replace['adm_form_table_rows'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute form_template.cli.php.\n"); } echo "Generating admin list table header rows...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl headerrows", $output, $return_val); if ($return_val == 0) { $search['adm_list_header_rows'] = '/%ADM_LIST_HEADER_ROWS%/'; $replace['adm_list_header_rows'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); } echo "Generating admin list table rows...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl listrows", $output, $return_val); if ($return_val == 0) { $search['adm_list_rows'] = '/%ADM_LIST_ROWS%/'; $replace['adm_list_rows'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); } echo "Generating sortorder...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl sortorder", $output, $return_val); if ($return_val == 0) { $search['sort_order'] = '/%SORT_ORDER%/'; $replace['sort_order'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); } echo "Generating insert data...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl insert", $output, $return_val); if ($return_val == 0) { $search['insert'] = '/%INSERT%/'; $replace['insert'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); } echo "Generating update data...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl update", $output, $return_val); if ($return_val == 0) { $search['update'] = '/%UPDATE%/'; $replace['update'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); } echo "Generating search data...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl search", $output, $return_val); if ($return_val == 0) { $search['search'] = '/%SEARCH%/'; $replace['search'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); } echo "Generating form validation data...\n"; $output = array(); exec(dirname($_SERVER['argv'][0]) . "/validation.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val); if ($return_val == 0) { $search['form_validation'] = '/%FORM_VALIDATION%/'; $replace['form_validation'] = join("\n", $output); } else { die(basename($_SERVER['argv'][0]) . " Error: could not execute validation.cli.php.\n"); } /****************************************************************************** * PRINT VAR INSTEAD. *****************************************************************************/ if ('var' == $op) { if (isset($_SERVER['argv'][5]) && isset($replace[$_SERVER['argv'][5]])) { echo "\n\n" . $replace[$_SERVER['argv'][5]] . "\n\n"; } else if (isset($_SERVER['argv'][5]) && isset($skel_files[$_SERVER['argv'][5]])) { echo "\n\n" . preg_replace($search, $replace, $skel_files[$_SERVER['argv'][5]]) . "\n\n"; } else { die(basename($_SERVER['argv'][0]) . " Error: variable " . (isset($_SERVER['argv'][5]) ? $_SERVER['argv'][5] : '') . " not defined. Please choose one of:\n" . join(', ', array_keys(array_merge($replace, $skel_files))) . "\n"); } die; } /****************************************************************************** * WRITE FILES *****************************************************************************/ // (1) Admin script. if (file_exists("$admin_dir/$admin_script")) { echo "Admin script already exists: $admin_dir/$admin_script\n"; } else { echo "Writing admin script: $admin_dir/$admin_script\n"; if ('test' != $op) { $fp = fopen("$admin_dir/$admin_script", "w"); fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_script'])); fclose($fp); } } // (2) Admin list template. if (file_exists("$admin_tpl_dir/$admin_list_template")) { echo "Admin list template already exists: $admin_tpl_dir/$admin_list_template\n"; } else { echo "Writing admin list template: $admin_tpl_dir/$admin_list_template\n"; if ('test' != $op) { $fp = fopen("$admin_tpl_dir/$admin_list_template", "w"); fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_list_template'])); fclose($fp); } } // (3) Admin form template. if (file_exists("$admin_tpl_dir/$admin_form_template")) { echo "Admin form template already exists: $admin_tpl_dir/$admin_form_template\n"; } else { echo "Writing admin form template: $admin_tpl_dir/$admin_form_template\n"; if ('test' != $op) { $fp = fopen("$admin_tpl_dir/$admin_form_template", "w"); fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_form_template'])); fclose($fp); } } // (4) Public script. if (file_exists("$public_dir/$public_script")) { echo "Public script already exists: $public_dir/$public_script\n"; } else { echo "Writing public script: $public_dir/$public_script\n"; if ('test' != $op) { $fp = fopen("$public_dir/$public_script", "w"); fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_script'])); fclose($fp); } } // (5) Public list template. if (file_exists("$public_tpl_dir/$public_list_template")) { echo "Public list template already exists: $public_tpl_dir/$public_list_template\n"; } else { echo "Writing public list template: $public_tpl_dir/$public_list_template\n"; if ('test' != $op) { $fp = fopen("$public_tpl_dir/$public_list_template", "w"); fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_list_template'])); fclose($fp); } } // (6) Public detail template. if (file_exists("$public_tpl_dir/$public_detail_template")) { echo "Public detail template already exists: $public_tpl_dir/$public_detail_template\n"; } else { echo "Writing public detail template: $public_tpl_dir/$public_detail_template\n"; if ('test' != $op) { $fp = fopen("$public_tpl_dir/$public_detail_template", "w"); fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_detail_template'])); fclose($fp); } } echo "Done!\n"; /******************************************************************** * FUNCTIONS ********************************************************************/ function trashFile($file_path_name) { global $user_trash_folder; static $file_prefix; if (!isset($file_prefix)) { $file_prefix = time(); } else { $file_prefix++; } // Make user trash folder. if (!is_dir($user_trash_folder)) { echo "Attempting to create user trash folder: $user_trash_folder/\n"; mkdir($user_trash_folder); chmod($user_trash_folder, 0700); } if (!is_dir("$user_trash_folder/") || !is_writable("$user_trash_folder/")) { die("User trash directory not available: $user_trash_folder/\n"); } // Move file to trash. if (file_exists($file_path_name)) { rename($file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); printf("Moved to trash: %s -> %s\n", $file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); } else { printf("File not found: %s\n", $file_path_name); } } ?>