#!/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 . */ /** * list_template.cli.php */ 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", 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'], true) : oTxt(trim(mb_substr(\$list[\$i]['$field'], 0, 50)) . '...'); \x3f>"; } else if (preg_match('/.*(begin|start).*date.*/i', $field)) { $listrows[] = "<\x3fphp echo '0000-00-00' == \$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 '0000-00-00 00:00:00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; } else if (preg_match('/date/i', $type)) { $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : 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'], true); \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>
<\x3fphp \$app->printHiddenSession(false); \x3f> <\x3fphp echo _("Add __///__"); \x3f>
" /> " />
E_O_F; // Print header rows. if (!isset($op) || 'headerrows' == $op) { foreach ($headers as $field=>$title) { if (preg_match('/(_date|_datetime|_time|price|value|quantity)/', $field)) { $ordering = 'DESC'; } else { $ordering = 'ASC'; } if ($field == $primary_key) { echo " \n"; } else { echo " \n"; } } } echo isset($op) ? '' : <<  <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f> E_O_F; // Print List rows. if (!isset($op) || 'listrows' == $op) { foreach ($listrows as $col_data) { ?> " href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . "?op=del&$primary_key=" . \$list[\$i]['$primary_key']); \x3f>" onclick="javascript:return confirm('<\x3fphp printf(_("Are you sure you want to delete the record %s? This action is permanent and cannot be undone."), oTxt(\$list[\$i]['__///__'])) \x3f>');">Delete   <\x3fphp } \x3f>
   <\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'DESC'); \x3f><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), '$ordering'); \x3f>
" href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=edit&$primary_key=' . \$list[\$i]['$primary_key']); \x3f>">Edit   " href="<\x3fphp echo \$app->oHREF("/admin/versions.php?record_table=$db_tbl&record_key=$primary_key&boomerang=true&record_val=" . \$list[\$i]['$primary_key']); \x3f>">    
<\x3fphp if (\$page->total_pages > 1) { \x3f>
<\x3fphp echo _("Pages:"); \x3f> <\x3fphp \$page->printPageNumbers() \x3f>
<\x3fphp } \x3f>
E_O_F; ?>