[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 |
---|
[457] | 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. |
---|
[457] | 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. |
---|
[457] | 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 | * list_template.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; |
---|
| 38 | $valid_ops = array('headerrows', 'listrows'); |
---|
| 39 | |
---|
| 40 | // Test arguments. |
---|
| 41 | if (isset($_SERVER['argv'][2])) { |
---|
| 42 | // Second arg is db table. |
---|
| 43 | $db_tbl = $_SERVER['argv'][2]; |
---|
| 44 | } else { |
---|
[468] | 45 | die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s\n", basename($_SERVER['argv'][0]), join(', ', $valid_ops))); |
---|
[1] | 46 | } |
---|
| 47 | |
---|
| 48 | // Test for operation. |
---|
| 49 | if (isset($_SERVER['argv'][3])) { |
---|
| 50 | // Optional third arg is op. |
---|
| 51 | $op = $_SERVER['argv'][3]; |
---|
| 52 | // Make sure op is valid. |
---|
| 53 | if (!in_array($op, $valid_ops)) { |
---|
| 54 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // Get DB tables. |
---|
[136] | 59 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 60 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 61 | $tables[] = $row; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // Make sure requested table is in database. |
---|
| 65 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 66 | 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] | 67 | } |
---|
| 68 | |
---|
| 69 | // Get DB table column info. |
---|
[136] | 70 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 71 | while ($row = mysql_fetch_row($qid)) { |
---|
| 72 | $cols[] = $row; |
---|
| 73 | } |
---|
| 74 | |
---|
[154] | 75 | $primary_key = '__///__'; |
---|
[44] | 76 | |
---|
[1] | 77 | // Loop through columns |
---|
| 78 | if (is_array($cols) && !empty($cols)) { |
---|
| 79 | foreach ($cols as $col) { |
---|
[42] | 80 | |
---|
[1] | 81 | // Human readable. |
---|
| 82 | $field = $col[0]; |
---|
[316] | 83 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
[1] | 84 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
[42] | 85 | |
---|
[316] | 86 | switch ($field) { |
---|
| 87 | case 'added_by_user_id' : |
---|
| 88 | case 'modified_by_user_id' : |
---|
| 89 | $title = "by"; |
---|
| 90 | break; |
---|
| 91 | |
---|
| 92 | case 'added_datetime' : |
---|
| 93 | case 'modified_datetime' : |
---|
| 94 | $title = str_replace(' datetime', '', $title); |
---|
| 95 | break; |
---|
| 96 | } |
---|
| 97 | |
---|
[1] | 98 | // Get primary key. |
---|
| 99 | if ('PRI' == $col[3]) { |
---|
| 100 | $primary_key = $field; |
---|
| 101 | } |
---|
[42] | 102 | |
---|
[1] | 103 | // Column headers. |
---|
| 104 | $headers[$field] = $title; |
---|
[42] | 105 | |
---|
[1] | 106 | // Column data. |
---|
| 107 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 108 | $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '•' : ''; \x3f>"; |
---|
| 109 | } else if ( |
---|
| 110 | 'tinytext' == $type || |
---|
| 111 | 'text' == $type || |
---|
| 112 | 'mediumtext' == $type || |
---|
| 113 | 'longtext' == $type || |
---|
| 114 | 'tinyblob' == $type || |
---|
| 115 | 'blob' == $type || |
---|
| 116 | 'mediumblob' == $type || |
---|
| 117 | 'longblob' == $type |
---|
| 118 | ) { |
---|
[247] | 119 | $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>"; |
---|
[1] | 120 | } else if (preg_match('/.*(begin|start).*date.*/i', $field)) { |
---|
[136] | 121 | $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 122 | } else if (preg_match('/.*(end|expire).*date.*/i', $field)) { |
---|
[136] | 123 | $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 124 | } else if (preg_match('/datetime/i', $type)) { |
---|
[457] | 125 | $listrows[] = "<\x3fphp echo Validator::validateStrDate(\$list[\$i]['$field']) ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>"; |
---|
[1] | 126 | } else if (preg_match('/date/i', $type)) { |
---|
[457] | 127 | $listrows[] = "<\x3fphp echo Validator::validateStrDate(\$list[\$i]['$field']) ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>"; |
---|
[1] | 128 | } else if (preg_match('/(amount|_rate)/i', $field)) { |
---|
| 129 | $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>"; |
---|
[19] | 130 | } else if (preg_match('/(added_by_user_id)/i', $field)) { |
---|
[295] | 131 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>"; |
---|
[19] | 132 | } else if (preg_match('/(modified_by_user_id)/i', $field)) { |
---|
[295] | 133 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_username']); \x3f>"; |
---|
[1] | 134 | } else if ('rank' == $field) { |
---|
| 135 | $listrows[] = "<input type=\"text\" name=\"rank[<\x3fphp echo \$list[\$i]['$primary_key']; \x3f>]\" value=\"<\x3fphp echo \$list[\$i]['rank']; \x3f>\" size=\"5\" />"; |
---|
| 136 | } else { |
---|
| 137 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['$field'], true); \x3f>"; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } else { |
---|
| 141 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | // Print the template out. |
---|
| 145 | echo isset($op) ? '' : <<<E_O_F |
---|
| 146 | |
---|
| 147 | <\x3fphp \$fv->printErrorMessages(); \x3f> |
---|
| 148 | |
---|
[457] | 149 | <div class="commandbox"> |
---|
[185] | 150 | <form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get"> |
---|
[136] | 151 | <\x3fphp \$app->printHiddenSession(false); \x3f> |
---|
[154] | 152 | <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span> |
---|
[1] | 153 | <br /> |
---|
[42] | 154 | |
---|
[154] | 155 | <input type="text" class="sc-small" size="20" name="search_query" value="<\x3fphp echo getFormData('search_query'); \x3f>" title="<\x3fphp echo oTxt(_("Fields searched: __///__.")); \x3f>" /> |
---|
| 156 | <select name="filter___///__"> |
---|
[461] | 157 | <\x3fphp // printSelectForm('__///___tbl', "CONCAT(__///___id, '—', city, '—', title)", '__///___id', getFormData('filter___///__'), array('' => 'Any __///__'), 'ORDER BY __///__ ASC'); \x3f> |
---|
[1] | 158 | </select> |
---|
| 159 | <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" /> |
---|
[51] | 160 | </form> |
---|
[1] | 161 | </div> |
---|
| 162 | |
---|
| 163 | <?php include 'list_info.ihtml'; \x3f> |
---|
| 164 | |
---|
[185] | 165 | <form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post"> |
---|
[1] | 166 | <table class="list"> |
---|
| 167 | <tr> |
---|
| 168 | <th> </th> |
---|
| 169 | <th> </th> |
---|
| 170 | |
---|
| 171 | E_O_F; |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | // Print header rows. |
---|
| 175 | if (!isset($op) || 'headerrows' == $op) { |
---|
| 176 | foreach ($headers as $field=>$title) { |
---|
[316] | 177 | if (preg_match('/(_date|_datetime|_time|price|value|quantity)/', $field)) { |
---|
| 178 | $ordering = 'DESC'; |
---|
| 179 | } else { |
---|
| 180 | $ordering = 'ASC'; |
---|
| 181 | } |
---|
| 182 | |
---|
[1] | 183 | if ($field == $primary_key) { |
---|
[316] | 184 | echo " <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'DESC'); \x3f></th>\n"; |
---|
[1] | 185 | } else { |
---|
[316] | 186 | echo " <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), '$ordering'); \x3f></th>\n"; |
---|
[1] | 187 | } |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | echo isset($op) ? '' : <<<E_O_F |
---|
| 192 | <th> </th> |
---|
| 193 | </tr> |
---|
| 194 | <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f> |
---|
| 195 | <tr> |
---|
[316] | 196 | <td class="sc-nowrap"><a title="<\x3fphp printf(_("Edit %s"), oTxt(\$list[\$i]['__///__'])) \x3f>" href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=edit&$primary_key=' . \$list[\$i]['$primary_key']); \x3f>"><img src="/admin/i/pen.gif" alt="Edit" width="12" height="12" border="0" /></a> </td> |
---|
| 197 | <td class="sc-nowrap"><a title="<\x3fphp printf(_("Versions of %s"), oTxt(\$list[\$i]['__///__'])) \x3f>" 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>"><img src="/admin/i/multiple.png" alt="" width="12" height="12" border="0" /></a> </td> |
---|
[1] | 198 | |
---|
| 199 | E_O_F; |
---|
| 200 | |
---|
| 201 | // Print List rows. |
---|
| 202 | if (!isset($op) || 'listrows' == $op) { |
---|
| 203 | foreach ($listrows as $col_data) { |
---|
| 204 | ?> |
---|
[121] | 205 | <td class="sc-nowrap"><?php echo $col_data; ?> </td> |
---|
[1] | 206 | <?php |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | echo isset($op) ? '' : <<<E_O_F |
---|
[320] | 211 | <td class="sc-nowrap" align="right"><a title="<\x3fphp printf(_("Delete %s"), oTxt(\$list[\$i]['__///__'])) \x3f>" 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>');"><img src="/admin/i/trash.gif" alt="Delete" width="10" height="10" border="0" /></a> </td> |
---|
[1] | 212 | </tr> |
---|
| 213 | <\x3fphp } \x3f> |
---|
| 214 | </table> |
---|
| 215 | |
---|
| 216 | <\x3fphp if (\$page->total_pages > 1) { \x3f> |
---|
[121] | 217 | <div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f> <\x3fphp \$page->printPageNumbers() \x3f></div> |
---|
[1] | 218 | <\x3fphp } \x3f> |
---|
| 219 | </form> |
---|
| 220 | |
---|
| 221 | E_O_F; |
---|