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