[412] | 1 | #!/usr/bin/php -q |
---|
[1] | 2 | <?php |
---|
| 3 | /** |
---|
[362] | 4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 5 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 6 | * Copyright 2001-2012 Strangecode, LLC |
---|
[500] | 7 | * |
---|
[362] | 8 | * This file is part of The Strangecode Codebase. |
---|
| 9 | * |
---|
| 10 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 12 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 13 | * any later version. |
---|
[500] | 14 | * |
---|
[362] | 15 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 18 | * details. |
---|
[500] | 19 | * |
---|
[362] | 20 | * You should have received a copy of the GNU General Public License along with |
---|
| 21 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /** |
---|
[1] | 25 | * module.cli.php |
---|
| 26 | */ |
---|
| 27 | |
---|
[533] | 28 | if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) { |
---|
| 29 | // First arg is path to current site. Realpath removes trailing /s |
---|
| 30 | define('COMMON_BASE', realpath($_SERVER['argv'][1])); |
---|
| 31 | } else { |
---|
| 32 | die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n"); |
---|
| 33 | } |
---|
[1] | 34 | |
---|
[533] | 35 | include_once dirname(__FILE__) . '/../_config.inc.php'; |
---|
| 36 | |
---|
[1] | 37 | $op = null; |
---|
[556] | 38 | $valid_ops = array('clean', 'var', 'test', 'admin', 'public'); |
---|
[1] | 39 | |
---|
[259] | 40 | // Test for arguments. |
---|
[1] | 41 | if (isset($_SERVER['argv'][2]) && isset($_SERVER['argv'][3])) { |
---|
| 42 | $module_name_singular = $_SERVER['argv'][2]; |
---|
| 43 | $module_name_plural = $_SERVER['argv'][3]; |
---|
| 44 | } else { |
---|
[556] | 45 | die(basename($_SERVER['argv'][0]) . " error: missing arguments. |
---|
[42] | 46 | |
---|
[556] | 47 | Usage: " . basename($_SERVER['argv'][0]) . " SITE_DIRECTORY NAME_SINGULAR NAME_PLURAL OPERATION |
---|
[500] | 48 | |
---|
[556] | 49 | Example: export DB_NAME=mydb; " . basename($_SERVER['argv'][0]) . " . order orders admin |
---|
| 50 | |
---|
[151] | 51 | Valid operations include: " . join(', ', $valid_ops) . " |
---|
[1] | 52 | |
---|
| 53 | The following files will be generated by this script: |
---|
| 54 | |
---|
[556] | 55 | âadminâ operation: |
---|
| 56 | SITE_DIRECTORY/admin/nameplural.php |
---|
| 57 | SITE_DIRECTORY/admin/_templates/namesingular_list.ihtml |
---|
| 58 | SITE_DIRECTORY/admin/_templates/namesingular_form.ihtml |
---|
| 59 | |
---|
| 60 | âpublicâ operation: |
---|
| 61 | SITE_DIRECTORY/html/nameplural.php |
---|
| 62 | SITE_DIRECTORY/html/_templates/namesingular_list.ihtml |
---|
| 63 | SITE_DIRECTORY/html/_templates/namesingular.ihtml |
---|
| 64 | |
---|
[1] | 65 | "); |
---|
| 66 | } |
---|
| 67 | |
---|
[556] | 68 | // Test for valid operation. |
---|
| 69 | $op = $_SERVER['argv'][4]; |
---|
| 70 | // Make sure op is valid. |
---|
| 71 | if (!in_array($op, $valid_ops)) { |
---|
| 72 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
[1] | 73 | } |
---|
| 74 | |
---|
[25] | 75 | switch ($op) { |
---|
| 76 | case 'test' : |
---|
| 77 | echo "RUNNING MODULE MAKER IN TEST MODE.\n\n"; |
---|
| 78 | break; |
---|
[556] | 79 | case 'admin' : |
---|
| 80 | case 'public' : |
---|
| 81 | echo "Generating only $op filesâŠ\n\n"; |
---|
| 82 | break; |
---|
[25] | 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
[143] | 86 | /******************************************************************** |
---|
| 87 | * CONFIG |
---|
| 88 | ********************************************************************/ |
---|
[1] | 89 | |
---|
| 90 | // Where deleted files go: |
---|
| 91 | $user_trash_folder = $_SERVER['HOME'] . '/.Trash'; |
---|
| 92 | |
---|
| 93 | // Directories |
---|
| 94 | $skel_dir = realpath(dirname(__FILE__) . '/skel'); |
---|
| 95 | $admin_dir = realpath(COMMON_BASE . '/admin'); |
---|
| 96 | $admin_tpl_dir = realpath(COMMON_BASE . '/admin/_templates'); |
---|
| 97 | $public_dir = realpath(COMMON_BASE . '/html'); |
---|
| 98 | $public_tpl_dir = realpath(COMMON_BASE . '/html/_templates'); |
---|
| 99 | |
---|
| 100 | // Names |
---|
[20] | 101 | $module_title = ucwords(str_replace('_', ' ', $module_name_plural)); |
---|
| 102 | $item_title = ucwords(str_replace('_', ' ', $module_name_singular)); |
---|
[247] | 103 | $module_name_upper = mb_strtoupper(str_replace('_', ' ', $module_name_plural)); |
---|
[1] | 104 | |
---|
| 105 | // Admin files |
---|
| 106 | $admin_script = $module_name_plural . '.php'; |
---|
| 107 | $admin_list_template = $module_name_singular . '_list.ihtml'; |
---|
| 108 | $admin_form_template = $module_name_singular . '_form.ihtml'; |
---|
| 109 | |
---|
| 110 | // Public files |
---|
| 111 | $public_script = $module_name_plural . '.php'; |
---|
| 112 | $public_list_template = $module_name_singular . '_list.ihtml'; |
---|
| 113 | $public_detail_template = $module_name_singular . '_view.ihtml'; |
---|
| 114 | |
---|
[334] | 115 | // Database names |
---|
[1] | 116 | $db_tbl = $module_name_singular . '_tbl'; |
---|
| 117 | $primary_key = $module_name_singular . '_id'; |
---|
| 118 | |
---|
| 119 | // Only after we've defined essential files can we clean. |
---|
| 120 | if ('clean' == $op) { |
---|
[20] | 121 | echo "Beginning file cleanup\n"; |
---|
| 122 | trashFile("$admin_dir/$admin_script"); |
---|
| 123 | trashFile("$admin_tpl_dir/$admin_list_template"); |
---|
| 124 | trashFile("$admin_tpl_dir/$admin_form_template"); |
---|
| 125 | trashFile("$public_dir/$public_script"); |
---|
| 126 | trashFile("$public_tpl_dir/$public_list_template"); |
---|
[42] | 127 | trashFile("$public_tpl_dir/$public_detail_template"); |
---|
[20] | 128 | echo "End file cleanup\n"; |
---|
[1] | 129 | die; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // Go! |
---|
[238] | 133 | echo 'Running Module Maker. Using database: ' . $app->getParam('db_name') . "\n"; |
---|
[1] | 134 | |
---|
| 135 | |
---|
[143] | 136 | /******************************************************************** |
---|
| 137 | * PREPROCESSING |
---|
| 138 | ********************************************************************/ |
---|
[1] | 139 | |
---|
| 140 | // Ensure skel files exist. |
---|
[556] | 141 | if ('admin' == $op && (!file_exists("$skel_dir/admin.php") || !file_exists("$skel_dir/adm_list.ihtml") || !file_exists("$skel_dir/adm_form.ihtml"))) { |
---|
| 142 | die(basename($_SERVER['argv'][0]) . " Warning: one or more admin skeleton source files missing. Please check directory: $skel_dir.\n"); |
---|
[1] | 143 | } |
---|
[556] | 144 | if ('public' == $op && (!file_exists("$skel_dir/public.php") || !file_exists("$skel_dir/public.ihtml") || !file_exists("$skel_dir/public_list.ihtml"))) { |
---|
| 145 | die(basename($_SERVER['argv'][0]) . " Warning: one or more public skeleton source files missing. Please check directory: $skel_dir.\n"); |
---|
| 146 | } |
---|
[1] | 147 | |
---|
[413] | 148 | if (!isset($_SERVER['argv'][4]) || 'var' != $_SERVER['argv'][4]) { |
---|
| 149 | // Ensure essential directories exist, except for 'var' op. |
---|
[556] | 150 | if ('admin' == $op && !is_dir("$admin_dir")) { |
---|
[413] | 151 | die(basename($_SERVER['argv'][0]) . " Error: admin_dir '$admin_dir' directory not found.\n"); |
---|
| 152 | } |
---|
[556] | 153 | if ('admin' == $op && !is_dir("$admin_tpl_dir")) { |
---|
[413] | 154 | die(basename($_SERVER['argv'][0]) . " Error: admin_tpl_dir '$admin_tpl_dir' directory not found.\n"); |
---|
| 155 | } |
---|
[556] | 156 | if ('public' == $op && !is_dir("$public_dir")) { |
---|
[413] | 157 | die(basename($_SERVER['argv'][0]) . " Error: public_dir '$public_dir' directory not found.\n"); |
---|
| 158 | } |
---|
[556] | 159 | if ('public' == $op && !is_dir("$public_tpl_dir")) { |
---|
[413] | 160 | die(basename($_SERVER['argv'][0]) . " Error: public_tpl_dir '$public_tpl_dir' directory not found.\n"); |
---|
[500] | 161 | } |
---|
[1] | 162 | } |
---|
| 163 | |
---|
| 164 | // Get DB tables. |
---|
[136] | 165 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 166 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 167 | $tables[] = $row; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | // Make sure requested table is in database. |
---|
[295] | 171 | if (!isset($tables)) { |
---|
| 172 | 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'))); |
---|
| 173 | } |
---|
[1] | 174 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 175 | 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))); |
---|
[1] | 176 | } |
---|
| 177 | |
---|
| 178 | // Ensure requested table contains columns. |
---|
| 179 | // Get DB table column info. |
---|
[136] | 180 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 181 | while ($row = mysql_fetch_row($qid)) { |
---|
| 182 | $cols[] = $row; |
---|
| 183 | } |
---|
| 184 | if (!is_array($cols) || empty($cols)) { |
---|
| 185 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | /****************************************************************************** |
---|
| 190 | * SETUP VARIABLES |
---|
| 191 | *****************************************************************************/ |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | // Loop through columns |
---|
[20] | 195 | $upload_file_capability = false; |
---|
[1] | 196 | $headers = array(); |
---|
| 197 | $public_list_page_vars = array(); |
---|
| 198 | $public_detail_page_vars = array(); |
---|
| 199 | $set_values_default = array(); |
---|
| 200 | if (is_array($cols) && !empty($cols)) { |
---|
| 201 | foreach ($cols as $col) { |
---|
[42] | 202 | |
---|
[1] | 203 | // Human readable. |
---|
| 204 | $field = $col[0]; |
---|
| 205 | $field_title = ucfirst(str_replace('_', ' ', $field)); |
---|
| 206 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
| 207 | $default = $col[4]; |
---|
[500] | 208 | |
---|
[316] | 209 | if (mb_strpos($default, '0000') !== false || '0' == $default) { |
---|
| 210 | $default = ''; |
---|
| 211 | } |
---|
[42] | 212 | |
---|
[1] | 213 | // Get primary key. |
---|
| 214 | // if ('PRI' == $col[3]) { |
---|
| 215 | // $primary_key = $field; |
---|
| 216 | // } |
---|
[42] | 217 | |
---|
[1] | 218 | // Our form will require type="multipart/form-data". |
---|
[20] | 219 | if (preg_match('/file|image/i', $field)) { |
---|
| 220 | $upload_file_capability = true; |
---|
[1] | 221 | } |
---|
[42] | 222 | |
---|
[1] | 223 | // Column headers. |
---|
| 224 | $headers[$field] = $field_title; |
---|
[42] | 225 | |
---|
[1] | 226 | // Get php code for printing variables. |
---|
| 227 | $public_list_page_vars[] = "<\x3fphp echo oTxt(\$" . $module_name_singular . "_list[\$i]['$field']); \x3f>"; |
---|
| 228 | $public_detail_page_vars[] = "<\x3fphp echo oTxt(\$item['$field']); \x3f>"; |
---|
| 229 | $set_values_default[] = "'$field' => '$default'"; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // -------------FILES------------- |
---|
| 234 | |
---|
[557] | 235 | $skel_files = array(); |
---|
| 236 | $skel_files['skel_admin_script'] = file_get_contents($skel_dir . '/admin.php'); |
---|
| 237 | $skel_files['skel_admin_list_template'] = file_get_contents($skel_dir . '/adm_list.ihtml'); |
---|
| 238 | $skel_files['skel_admin_form_template'] = file_get_contents($skel_dir . '/adm_form.ihtml'); |
---|
| 239 | $skel_files['skel_public_script'] = file_get_contents($skel_dir . '/public.php'); |
---|
| 240 | $skel_files['skel_public_list_template'] = file_get_contents($skel_dir . '/public_list.ihtml'); |
---|
| 241 | $skel_files['skel_public_detail_template'] = file_get_contents($skel_dir . '/public.ihtml'); |
---|
[1] | 242 | |
---|
| 243 | |
---|
[20] | 244 | // Search-replace variables ---------------------------------- |
---|
[1] | 245 | |
---|
[20] | 246 | // Admin script file upload replacement routines... |
---|
| 247 | |
---|
| 248 | $search['admin_form_tag_init'] = '/%ADMIN_FORM_TAG_INIT%/'; |
---|
[295] | 249 | $replace['admin_form_tag_init'] = "<form method=\"post\" action=\"<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>\" class=\"sc-form\">"; |
---|
[20] | 250 | $search['admin_upload_include'] = '/%ADMIN_UPLOAD_INCLUDE%/'; |
---|
| 251 | $replace['admin_upload_include'] = ''; |
---|
| 252 | $search['admin_upload_config'] = '/%ADMIN_UPLOAD_CONFIG%/'; |
---|
| 253 | $replace['admin_upload_config'] = ''; |
---|
| 254 | $search['admin_upload_init'] = '/%ADMIN_UPLOAD_INIT%/'; |
---|
| 255 | $replace['admin_upload_init'] = ''; |
---|
| 256 | $search['admin_upload_del'] = '/%ADMIN_UPLOAD_DEL%/'; |
---|
| 257 | $replace['admin_upload_del'] = ''; |
---|
| 258 | $search['admin_upload_insert'] = '/%ADMIN_UPLOAD_INSERT%/'; |
---|
| 259 | $replace['admin_upload_insert'] = ''; |
---|
| 260 | $search['admin_upload_update'] = '/%ADMIN_UPLOAD_UPDATE%/'; |
---|
| 261 | $replace['admin_upload_update'] = ''; |
---|
| 262 | |
---|
| 263 | if ($upload_file_capability) { |
---|
[22] | 264 | // Form arguments |
---|
[319] | 265 | $replace['admin_form_tag_init'] = "<form enctype=\"multipart/form-data\" method=\"post\" action=\"<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>\" class=\"sc-form\">\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"<\x3fphp echo intval(ini_get('upload_max_filesize')) * 1024 * 1024; \x3f>\" />"; |
---|
[42] | 266 | |
---|
[22] | 267 | // Include statement. |
---|
[20] | 268 | $replace['admin_upload_include'] = "require_once 'codebase/lib/Upload.inc.php';\n"; |
---|
| 269 | |
---|
[22] | 270 | // Config |
---|
[20] | 271 | $replace['admin_upload_config'] = <<<E_O_F |
---|
| 272 | |
---|
| 273 | // This module has file upload capability. |
---|
| 274 | \$upload = new Upload(); |
---|
| 275 | \$upload->setParam(array( |
---|
[154] | 276 | 'upload_path' => COMMON_BASE . '/html/_db_files/__///__', |
---|
[20] | 277 | 'dest_file_perms' => 0666, |
---|
| 278 | 'allow_overwriting' => false, |
---|
| 279 | 'valid_file_extensions' => array('jpg', 'gif', 'png', 'jpeg'), |
---|
| 280 | )); |
---|
| 281 | |
---|
| 282 | E_O_F; |
---|
| 283 | |
---|
[22] | 284 | // Main init. |
---|
[20] | 285 | $replace['admin_upload_init'] = <<<E_O_F |
---|
| 286 | |
---|
| 287 | // Copy uploaded image name into form data. |
---|
[154] | 288 | \$_POST['__///__'] = isset(\$_FILES['__///__']) ? \$_FILES['__///__']['name'] : ''; |
---|
[20] | 289 | |
---|
[42] | 290 | |
---|
[20] | 291 | E_O_F; |
---|
| 292 | |
---|
[22] | 293 | // Delete. |
---|
[20] | 294 | $replace['admin_upload_del'] = <<<E_O_F |
---|
| 295 | |
---|
| 296 | // Delete file. |
---|
| 297 | if ('' != \$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')) { |
---|
| 298 | \$upload->deleteFile(\$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*')); |
---|
| 299 | } |
---|
| 300 | E_O_F; |
---|
| 301 | |
---|
[22] | 302 | // Insert 1. |
---|
[20] | 303 | $replace['admin_upload_insert'] = <<<E_O_F |
---|
| 304 | |
---|
[42] | 305 | // Upload files with prepended primary key. |
---|
[154] | 306 | \$new_file = \$upload->process('__///__', \$%PRIMARY_KEY% . '_' . getFormData('__///__')); |
---|
[22] | 307 | |
---|
| 308 | // If file upload errors, redirect to edit operation for the inserted record. |
---|
| 309 | if (\$upload->anyErrors() || false === \$new_file) { |
---|
[136] | 310 | \$app->dieURL(\$_SERVER['PHP_SELF'] . '?op=edit&%PRIMARY_KEY%=' . \$%PRIMARY_KEY%); |
---|
[20] | 311 | } |
---|
| 312 | E_O_F; |
---|
| 313 | |
---|
[22] | 314 | // Update. |
---|
[20] | 315 | $replace['admin_upload_update'] = <<<E_O_F |
---|
| 316 | |
---|
[22] | 317 | // Upload new files. |
---|
[154] | 318 | if (getFormData('__///__')) { |
---|
[22] | 319 | // Get old file names for deletion. |
---|
| 320 | \$old_file = \$upload->getFilenameGlob(getFormData('%PRIMARY_KEY%') . '_*'); |
---|
| 321 | // Process new file upload with prepended primary key. |
---|
[154] | 322 | \$new_file = \$upload->process('__///__', getFormData('%PRIMARY_KEY%') . '_' . getFormData('__///__')); |
---|
[22] | 323 | if (false === \$new_file || \$upload->anyErrors()) { |
---|
| 324 | // Upload failed. Reload form. Display errors. |
---|
[20] | 325 | \$frm =& editRecordForm(getFormData('%PRIMARY_KEY%')); |
---|
| 326 | \$frm = array_merge(\$frm, getFormData()); |
---|
[202] | 327 | \$nav->add(_("Edit %ITEM_TITLE%")); |
---|
[20] | 328 | \$main_template = '%ADMIN_FORM_TEMPLATE%'; |
---|
| 329 | break; |
---|
[22] | 330 | } else { |
---|
| 331 | // Upload succeeded. Delete old files. |
---|
| 332 | if ('' != \$old_file && \$old_file != \$new_file[0]['name']) { |
---|
| 333 | \$upload->deleteFile(\$old_file); |
---|
| 334 | } |
---|
| 335 | } |
---|
[20] | 336 | } |
---|
| 337 | E_O_F; |
---|
[22] | 338 | } // End upload_file_capability. |
---|
[20] | 339 | |
---|
| 340 | |
---|
[1] | 341 | // Simple... |
---|
| 342 | |
---|
| 343 | $search['date'] = '/%DATE%/'; |
---|
[136] | 344 | $replace['date'] = date($app->getParam('date_format')); |
---|
[1] | 345 | |
---|
| 346 | $search['name_plural'] = '/%NAME_PLURAL%/'; |
---|
| 347 | $replace['name_plural'] = $module_name_plural; |
---|
| 348 | |
---|
| 349 | $search['name_singular'] = '/%NAME_SINGULAR%/'; |
---|
| 350 | $replace['name_singular'] = $module_name_singular; |
---|
| 351 | |
---|
| 352 | $search['title'] = '/%TITLE%/'; |
---|
| 353 | $replace['title'] = $module_title; |
---|
| 354 | |
---|
| 355 | $search['item_title'] = '/%ITEM_TITLE%/'; |
---|
| 356 | $replace['item_title'] = $item_title; |
---|
| 357 | |
---|
| 358 | $search['primary_key'] = '/%PRIMARY_KEY%/'; |
---|
| 359 | $replace['primary_key'] = $primary_key; |
---|
| 360 | |
---|
| 361 | $search['db_tbl'] = '/%DB_TBL%/'; |
---|
| 362 | $replace['db_tbl'] = $db_tbl; |
---|
| 363 | |
---|
| 364 | $search['name_upper'] = '/%NAME_UPPER%/'; |
---|
| 365 | $replace['name_upper'] = $module_name_upper; |
---|
| 366 | |
---|
| 367 | $search['admin_script'] = '/%ADMIN_SCRIPT%/'; |
---|
| 368 | $replace['admin_script'] = $admin_script; |
---|
| 369 | |
---|
| 370 | $search['admin_list_template'] = '/%ADMIN_LIST_TEMPLATE%/'; |
---|
| 371 | $replace['admin_list_template'] = $admin_list_template; |
---|
| 372 | |
---|
| 373 | $search['admin_form_template'] = '/%ADMIN_FORM_TEMPLATE%/'; |
---|
| 374 | $replace['admin_form_template'] = $admin_form_template; |
---|
| 375 | |
---|
| 376 | $search['public_script'] = '/%PUBLIC_SCRIPT%/'; |
---|
| 377 | $replace['public_script'] = $public_script; |
---|
| 378 | |
---|
| 379 | $search['public_list_template'] = '/%PUBLIC_LIST_TEMPLATE%/'; |
---|
| 380 | $replace['public_list_template'] = $public_list_template; |
---|
| 381 | |
---|
| 382 | $search['public_detail_template'] = '/%PUBLIC_DETAIL_TEMPLATE%/'; |
---|
| 383 | $replace['public_detail_template'] = $public_detail_template; |
---|
| 384 | |
---|
| 385 | $search['public_detail_page_vars'] = '/%PUBLIC_DETAIL_PAGE_VARS%/'; |
---|
| 386 | $replace['public_detail_page_vars'] = join("\n", $public_detail_page_vars); |
---|
| 387 | |
---|
| 388 | $search['public_list_page_vars'] = '/%PUBLIC_LIST_PAGE_VARS%/'; |
---|
| 389 | $replace['public_list_page_vars'] = join("\n", $public_list_page_vars); |
---|
| 390 | |
---|
| 391 | $search['set_values_default'] = '/%SET_VALUES_DEFAULT%/'; |
---|
| 392 | $replace['set_values_default'] = join(",\n ", $set_values_default); |
---|
| 393 | |
---|
| 394 | $search['search_fields'] = '/%SEARCH_FIELDS%/'; |
---|
| 395 | $replace['search_fields'] = join(", ", $headers); |
---|
| 396 | |
---|
| 397 | |
---|
| 398 | |
---|
| 399 | // Complex.... |
---|
| 400 | |
---|
| 401 | echo "Generating admin form table rows...\n"; |
---|
| 402 | $output = array(); |
---|
| 403 | exec(dirname($_SERVER['argv'][0]) . "/form_template.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val); |
---|
| 404 | if ($return_val == 0) { |
---|
| 405 | $search['adm_form_table_rows'] = '/%ADM_FORM_TABLE_ROWS%/'; |
---|
| 406 | $replace['adm_form_table_rows'] = join("\n", $output); |
---|
| 407 | } else { |
---|
| 408 | die(basename($_SERVER['argv'][0]) . " Error: could not execute form_template.cli.php.\n"); |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | echo "Generating admin list table header rows...\n"; |
---|
| 412 | $output = array(); |
---|
| 413 | exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl headerrows", $output, $return_val); |
---|
| 414 | if ($return_val == 0) { |
---|
| 415 | $search['adm_list_header_rows'] = '/%ADM_LIST_HEADER_ROWS%/'; |
---|
| 416 | $replace['adm_list_header_rows'] = join("\n", $output); |
---|
| 417 | } else { |
---|
| 418 | die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | echo "Generating admin list table rows...\n"; |
---|
| 422 | $output = array(); |
---|
| 423 | exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php " . COMMON_BASE . " $db_tbl listrows", $output, $return_val); |
---|
| 424 | if ($return_val == 0) { |
---|
| 425 | $search['adm_list_rows'] = '/%ADM_LIST_ROWS%/'; |
---|
| 426 | $replace['adm_list_rows'] = join("\n", $output); |
---|
| 427 | } else { |
---|
| 428 | die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | echo "Generating sortorder...\n"; |
---|
| 432 | $output = array(); |
---|
| 433 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl sortorder", $output, $return_val); |
---|
| 434 | if ($return_val == 0) { |
---|
| 435 | $search['sort_order'] = '/%SORT_ORDER%/'; |
---|
| 436 | $replace['sort_order'] = join("\n", $output); |
---|
| 437 | } else { |
---|
| 438 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | echo "Generating insert data...\n"; |
---|
| 442 | $output = array(); |
---|
| 443 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl insert", $output, $return_val); |
---|
| 444 | if ($return_val == 0) { |
---|
| 445 | $search['insert'] = '/%INSERT%/'; |
---|
| 446 | $replace['insert'] = join("\n", $output); |
---|
| 447 | } else { |
---|
| 448 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | echo "Generating update data...\n"; |
---|
| 452 | $output = array(); |
---|
| 453 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl update", $output, $return_val); |
---|
| 454 | if ($return_val == 0) { |
---|
| 455 | $search['update'] = '/%UPDATE%/'; |
---|
| 456 | $replace['update'] = join("\n", $output); |
---|
| 457 | } else { |
---|
| 458 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | echo "Generating search data...\n"; |
---|
| 462 | $output = array(); |
---|
| 463 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php " . COMMON_BASE . " $db_tbl search", $output, $return_val); |
---|
| 464 | if ($return_val == 0) { |
---|
| 465 | $search['search'] = '/%SEARCH%/'; |
---|
| 466 | $replace['search'] = join("\n", $output); |
---|
| 467 | } else { |
---|
| 468 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | echo "Generating form validation data...\n"; |
---|
| 472 | $output = array(); |
---|
| 473 | exec(dirname($_SERVER['argv'][0]) . "/validation.cli.php " . COMMON_BASE . " $db_tbl", $output, $return_val); |
---|
| 474 | if ($return_val == 0) { |
---|
| 475 | $search['form_validation'] = '/%FORM_VALIDATION%/'; |
---|
| 476 | $replace['form_validation'] = join("\n", $output); |
---|
| 477 | } else { |
---|
| 478 | die(basename($_SERVER['argv'][0]) . " Error: could not execute validation.cli.php.\n"); |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | /****************************************************************************** |
---|
[10] | 482 | * PRINT VAR INSTEAD. |
---|
| 483 | *****************************************************************************/ |
---|
| 484 | |
---|
| 485 | if ('var' == $op) { |
---|
[119] | 486 | if (isset($_SERVER['argv'][5]) && isset($replace[$_SERVER['argv'][5]])) { |
---|
[42] | 487 | echo "\n\n" . $replace[$_SERVER['argv'][5]] . "\n\n"; |
---|
[119] | 488 | } else if (isset($_SERVER['argv'][5]) && isset($skel_files[$_SERVER['argv'][5]])) { |
---|
[10] | 489 | echo "\n\n" . preg_replace($search, $replace, $skel_files[$_SERVER['argv'][5]]) . "\n\n"; |
---|
| 490 | } else { |
---|
[119] | 491 | 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"); |
---|
[10] | 492 | } |
---|
| 493 | die; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | |
---|
| 497 | /****************************************************************************** |
---|
[1] | 498 | * WRITE FILES |
---|
| 499 | *****************************************************************************/ |
---|
| 500 | |
---|
[556] | 501 | if ('admin' == $op) { |
---|
| 502 | // (1) Admin script. |
---|
| 503 | if (file_exists("$admin_dir/$admin_script")) { |
---|
| 504 | echo "Admin script already exists: $admin_dir/$admin_script\n"; |
---|
| 505 | } else { |
---|
| 506 | echo "Writing admin script: $admin_dir/$admin_script\n"; |
---|
| 507 | if ('test' != $op) { |
---|
| 508 | $fp = fopen("$admin_dir/$admin_script", "w"); |
---|
| 509 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_script'])); |
---|
| 510 | fclose($fp); |
---|
| 511 | } |
---|
[25] | 512 | } |
---|
[1] | 513 | |
---|
[556] | 514 | // (2) Admin list template. |
---|
| 515 | if (file_exists("$admin_tpl_dir/$admin_list_template")) { |
---|
| 516 | echo "Admin list template already exists: $admin_tpl_dir/$admin_list_template\n"; |
---|
| 517 | } else { |
---|
| 518 | echo "Writing admin list template: $admin_tpl_dir/$admin_list_template\n"; |
---|
| 519 | if ('test' != $op) { |
---|
| 520 | $fp = fopen("$admin_tpl_dir/$admin_list_template", "w"); |
---|
| 521 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_list_template'])); |
---|
| 522 | fclose($fp); |
---|
| 523 | } |
---|
[25] | 524 | } |
---|
[1] | 525 | |
---|
[556] | 526 | // (3) Admin form template. |
---|
| 527 | if (file_exists("$admin_tpl_dir/$admin_form_template")) { |
---|
| 528 | echo "Admin form template already exists: $admin_tpl_dir/$admin_form_template\n"; |
---|
| 529 | } else { |
---|
| 530 | echo "Writing admin form template: $admin_tpl_dir/$admin_form_template\n"; |
---|
| 531 | if ('test' != $op) { |
---|
| 532 | $fp = fopen("$admin_tpl_dir/$admin_form_template", "w"); |
---|
| 533 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_admin_form_template'])); |
---|
| 534 | fclose($fp); |
---|
| 535 | } |
---|
[25] | 536 | } |
---|
[1] | 537 | } |
---|
| 538 | |
---|
[556] | 539 | if ('public' == $op) { |
---|
| 540 | // (4) Public script. |
---|
| 541 | if (file_exists("$public_dir/$public_script")) { |
---|
| 542 | echo "Public script already exists: $public_dir/$public_script\n"; |
---|
| 543 | } else { |
---|
| 544 | echo "Writing public script: $public_dir/$public_script\n"; |
---|
| 545 | if ('test' != $op) { |
---|
| 546 | $fp = fopen("$public_dir/$public_script", "w"); |
---|
| 547 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_script'])); |
---|
| 548 | fclose($fp); |
---|
| 549 | } |
---|
[25] | 550 | } |
---|
[1] | 551 | |
---|
[556] | 552 | // (5) Public list template. |
---|
| 553 | if (file_exists("$public_tpl_dir/$public_list_template")) { |
---|
| 554 | echo "Public list template already exists: $public_tpl_dir/$public_list_template\n"; |
---|
| 555 | } else { |
---|
| 556 | echo "Writing public list template: $public_tpl_dir/$public_list_template\n"; |
---|
| 557 | if ('test' != $op) { |
---|
| 558 | $fp = fopen("$public_tpl_dir/$public_list_template", "w"); |
---|
| 559 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_list_template'])); |
---|
| 560 | fclose($fp); |
---|
| 561 | } |
---|
[25] | 562 | } |
---|
[1] | 563 | |
---|
[556] | 564 | // (6) Public detail template. |
---|
| 565 | if (file_exists("$public_tpl_dir/$public_detail_template")) { |
---|
| 566 | echo "Public detail template already exists: $public_tpl_dir/$public_detail_template\n"; |
---|
| 567 | } else { |
---|
| 568 | echo "Writing public detail template: $public_tpl_dir/$public_detail_template\n"; |
---|
| 569 | if ('test' != $op) { |
---|
| 570 | $fp = fopen("$public_tpl_dir/$public_detail_template", "w"); |
---|
| 571 | fwrite($fp, preg_replace($search, $replace, $skel_files['skel_public_detail_template'])); |
---|
| 572 | fclose($fp); |
---|
| 573 | } |
---|
[25] | 574 | } |
---|
[556] | 575 | |
---|
[1] | 576 | } |
---|
| 577 | echo "Done!\n"; |
---|
| 578 | |
---|
| 579 | |
---|
[143] | 580 | /******************************************************************** |
---|
| 581 | * FUNCTIONS |
---|
| 582 | ********************************************************************/ |
---|
[1] | 583 | |
---|
[20] | 584 | function trashFile($file_path_name) |
---|
[1] | 585 | { |
---|
| 586 | global $user_trash_folder; |
---|
[20] | 587 | static $file_prefix; |
---|
[42] | 588 | |
---|
[20] | 589 | if (!isset($file_prefix)) { |
---|
| 590 | $file_prefix = time(); |
---|
| 591 | } else { |
---|
| 592 | $file_prefix++; |
---|
| 593 | } |
---|
[42] | 594 | |
---|
[20] | 595 | // Make user trash folder. |
---|
[87] | 596 | if (!is_dir($user_trash_folder)) { |
---|
[119] | 597 | echo "Attempting to create user trash folder: $user_trash_folder/\n"; |
---|
[1] | 598 | mkdir($user_trash_folder); |
---|
[119] | 599 | chmod($user_trash_folder, 0700); |
---|
[1] | 600 | } |
---|
[87] | 601 | if (!is_dir("$user_trash_folder/") || !is_writable("$user_trash_folder/")) { |
---|
[1] | 602 | die("User trash directory not available: $user_trash_folder/\n"); |
---|
| 603 | } |
---|
| 604 | |
---|
[20] | 605 | // Move file to trash. |
---|
| 606 | if (file_exists($file_path_name)) { |
---|
| 607 | rename($file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); |
---|
| 608 | printf("Moved to trash: %s -> %s\n", $file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); |
---|
[1] | 609 | } else { |
---|
[20] | 610 | printf("File not found: %s\n", $file_path_name); |
---|
[1] | 611 | } |
---|
| 612 | } |
---|