[1] | 1 | #!/usr/local/bin/php -q |
---|
| 2 | <?php |
---|
| 3 | /** |
---|
| 4 | * list_template.cli.php |
---|
| 5 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 9 | |
---|
| 10 | $op = null; |
---|
| 11 | $valid_ops = array('headerrows', 'listrows'); |
---|
| 12 | |
---|
| 13 | // Test arguments. |
---|
| 14 | if (isset($_SERVER['argv'][2])) { |
---|
| 15 | // Second arg is db table. |
---|
| 16 | $db_tbl = $_SERVER['argv'][2]; |
---|
| 17 | } else { |
---|
[151] | 18 | die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops))); |
---|
[1] | 19 | } |
---|
| 20 | |
---|
| 21 | // Test for operation. |
---|
| 22 | if (isset($_SERVER['argv'][3])) { |
---|
| 23 | // Optional third arg is op. |
---|
| 24 | $op = $_SERVER['argv'][3]; |
---|
| 25 | // Make sure op is valid. |
---|
| 26 | if (!in_array($op, $valid_ops)) { |
---|
| 27 | die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n"); |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | // Get DB tables. |
---|
[136] | 32 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 33 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 34 | $tables[] = $row; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | // Make sure requested table is in database. |
---|
| 38 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 39 | 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] | 40 | } |
---|
| 41 | |
---|
| 42 | // Get DB table column info. |
---|
[136] | 43 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 44 | while ($row = mysql_fetch_row($qid)) { |
---|
| 45 | $cols[] = $row; |
---|
| 46 | } |
---|
| 47 | |
---|
[154] | 48 | $primary_key = '__///__'; |
---|
[44] | 49 | |
---|
[1] | 50 | // Loop through columns |
---|
| 51 | if (is_array($cols) && !empty($cols)) { |
---|
| 52 | foreach ($cols as $col) { |
---|
[42] | 53 | |
---|
[1] | 54 | // Human readable. |
---|
| 55 | $field = $col[0]; |
---|
| 56 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
| 57 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
[42] | 58 | |
---|
[1] | 59 | // Get primary key. |
---|
| 60 | if ('PRI' == $col[3]) { |
---|
| 61 | $primary_key = $field; |
---|
| 62 | } |
---|
[42] | 63 | |
---|
[1] | 64 | // Column headers. |
---|
| 65 | $headers[$field] = $title; |
---|
[42] | 66 | |
---|
[1] | 67 | // Column data. |
---|
| 68 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 69 | $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '•' : ''; \x3f>"; |
---|
| 70 | } else if ( |
---|
| 71 | 'tinytext' == $type || |
---|
| 72 | 'text' == $type || |
---|
| 73 | 'mediumtext' == $type || |
---|
| 74 | 'longtext' == $type || |
---|
| 75 | 'tinyblob' == $type || |
---|
| 76 | 'blob' == $type || |
---|
| 77 | 'mediumblob' == $type || |
---|
| 78 | 'longblob' == $type |
---|
| 79 | ) { |
---|
[247] | 80 | $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] | 81 | } else if (preg_match('/.*(begin|start).*date.*/i', $field)) { |
---|
[136] | 82 | $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 83 | } else if (preg_match('/.*(end|expire).*date.*/i', $field)) { |
---|
[136] | 84 | $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 85 | } else if (preg_match('/datetime/i', $type)) { |
---|
[136] | 86 | $listrows[] = "<\x3fphp echo '0000-00-00 00:00:00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 87 | } else if (preg_match('/date/i', $type)) { |
---|
[136] | 88 | $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 89 | } else if (preg_match('/(amount|_rate)/i', $field)) { |
---|
| 90 | $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>"; |
---|
[19] | 91 | } else if (preg_match('/(added_by_user_id)/i', $field)) { |
---|
[1] | 92 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_admin_username']); \x3f>"; |
---|
[19] | 93 | } else if (preg_match('/(modified_by_user_id)/i', $field)) { |
---|
[1] | 94 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_admin_username']); \x3f>"; |
---|
| 95 | } else if ('rank' == $field) { |
---|
| 96 | $listrows[] = "<input type=\"text\" name=\"rank[<\x3fphp echo \$list[\$i]['$primary_key']; \x3f>]\" value=\"<\x3fphp echo \$list[\$i]['rank']; \x3f>\" size=\"5\" />"; |
---|
| 97 | } else { |
---|
| 98 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['$field'], true); \x3f>"; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | } else { |
---|
| 102 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // Print the template out. |
---|
| 106 | echo isset($op) ? '' : <<<E_O_F |
---|
| 107 | |
---|
| 108 | <\x3fphp \$fv->printErrorMessages(); \x3f> |
---|
| 109 | |
---|
| 110 | <div id="commandbox"> |
---|
[185] | 111 | <form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get"> |
---|
[136] | 112 | <\x3fphp \$app->printHiddenSession(false); \x3f> |
---|
[154] | 113 | <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span> |
---|
[1] | 114 | <br /> |
---|
[42] | 115 | |
---|
[154] | 116 | <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>" /> |
---|
| 117 | <select name="filter___///__"> |
---|
| 118 | <\x3fphp // printSelectForm('__///___tbl', "CONCAT(__///___id, '—', city, '—', title)", '__///___id', getFormData('filter___///__'), array('Any __///__'), 'ORDER BY __///__ ASC'); \x3f> |
---|
[1] | 119 | </select> |
---|
| 120 | <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" /> |
---|
[51] | 121 | </form> |
---|
[1] | 122 | </div> |
---|
| 123 | |
---|
| 124 | <?php include 'list_info.ihtml'; \x3f> |
---|
| 125 | |
---|
[185] | 126 | <form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post"> |
---|
[1] | 127 | <table class="list"> |
---|
| 128 | <tr> |
---|
| 129 | <th> </th> |
---|
| 130 | <th> </th> |
---|
| 131 | |
---|
| 132 | E_O_F; |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | // Print header rows. |
---|
| 136 | if (!isset($op) || 'headerrows' == $op) { |
---|
| 137 | foreach ($headers as $field=>$title) { |
---|
| 138 | if ($field == $primary_key) { |
---|
| 139 | echo " <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'ASC'); \x3f></th>\n"; |
---|
| 140 | } else { |
---|
| 141 | echo " <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), 'ASC'); \x3f></th>\n"; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | echo isset($op) ? '' : <<<E_O_F |
---|
| 147 | <th> </th> |
---|
| 148 | </tr> |
---|
| 149 | <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f> |
---|
| 150 | <tr> |
---|
[154] | 151 | <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/_widgets/edit.gif" alt="Edit" width="14" height="18" border="0"></a> </td> |
---|
| 152 | <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/_widgets/subcategory.gif" alt="" width="18" height="14" border="0" /></a> </td> |
---|
[1] | 153 | |
---|
| 154 | E_O_F; |
---|
| 155 | |
---|
| 156 | // Print List rows. |
---|
| 157 | if (!isset($op) || 'listrows' == $op) { |
---|
| 158 | foreach ($listrows as $col_data) { |
---|
| 159 | ?> |
---|
[121] | 160 | <td class="sc-nowrap"><?php echo $col_data; ?> </td> |
---|
[1] | 161 | <?php |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | echo isset($op) ? '' : <<<E_O_F |
---|
[154] | 166 | <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/_widgets/delete.gif" alt="Delete" width="16" height="17" border="0"></a> </td> |
---|
[1] | 167 | </tr> |
---|
| 168 | <\x3fphp } \x3f> |
---|
| 169 | </table> |
---|
| 170 | |
---|
| 171 | <\x3fphp if (\$page->total_pages > 1) { \x3f> |
---|
[121] | 172 | <div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f> <\x3fphp \$page->printPageNumbers() \x3f></div> |
---|
[1] | 173 | <\x3fphp } \x3f> |
---|
| 174 | </form> |
---|
| 175 | |
---|
| 176 | E_O_F; |
---|
| 177 | |
---|
| 178 | ?> |
---|