#!/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 .
*/
/**
* list_template.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('headerrows', 'listrows');
// Test arguments.
if (isset($_SERVER['argv'][2])) {
// Second arg is db table.
$db_tbl = $_SERVER['argv'][2];
} else {
die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s\n", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
}
// Test for operation.
if (isset($_SERVER['argv'][3])) {
// Optional third arg is op.
$op = $_SERVER['argv'][3];
// 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");
}
}
// 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 (!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)));
}
// Get DB table column info.
$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
while ($row = mysql_fetch_row($qid)) {
$cols[] = $row;
}
$primary_key = '__///__';
// Loop through columns
if (is_array($cols) && !empty($cols)) {
foreach ($cols as $col) {
// Human readable.
$field = $col[0];
$type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
$title = ucfirst(str_replace('_', ' ', $field));
switch ($field) {
case 'added_by_user_id' :
case 'modified_by_user_id' :
$title = "by";
break;
case 'added_datetime' :
case 'modified_datetime' :
$title = str_replace(' datetime', '', $title);
break;
}
// Get primary key.
if ('PRI' == $col[3]) {
$primary_key = $field;
}
// Column headers.
$headers[$field] = $title;
// Column data.
if (preg_match("/enum\('true'\)/", $col[1])) {
$listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '•' : ''; \x3f>";
} else if (
'tinytext' == $type ||
'text' == $type ||
'mediumtext' == $type ||
'longtext' == $type ||
'tinyblob' == $type ||
'blob' == $type ||
'mediumblob' == $type ||
'longblob' == $type
) {
$listrows[] = "<\x3fphp echo mb_strlen(\$list[\$i]['$field'])<50 \x3f oTxt(\$list[\$i]['$field']) : oTxt(trim(mb_substr(\$list[\$i]['$field'], 0, 50)) . '...'); \x3f>";
} else if (preg_match('/.*(begin|start).*date.*/i', $field)) {
$listrows[] = "<\x3fphp echo \$db->getParam('zero_date') == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
} else if (preg_match('/.*(end|expire).*date.*/i', $field)) {
$listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
} else if (preg_match('/datetime/i', $type)) {
$listrows[] = "<\x3fphp echo strtotime(\$list[\$i]['modified_datetime']) > 0 ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>";
} else if (preg_match('/date/i', $type)) {
$listrows[] = "<\x3fphp echo strtotime(\$list[\$i]['modified_datetime']) > 0 ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>";
} else if (preg_match('/(amount|_rate)/i', $field)) {
$listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>";
} else if (preg_match('/(added_by_user_id)/i', $field)) {
$listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>";
} else if (preg_match('/(modified_by_user_id)/i', $field)) {
$listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_username']); \x3f>";
} else if ('rank' == $field) {
$listrows[] = "]\" value=\"<\x3fphp echo \$list[\$i]['rank']; \x3f>\" size=\"5\" />";
} else {
$listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['$field']); \x3f>";
}
}
} else {
die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
}
// Print the template out.
echo isset($op) ? '' : <<printErrorMessages(); \x3f>