[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]; |
---|
[316] | 56 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
[1] | 57 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
[42] | 58 | |
---|
[316] | 59 | switch ($field) { |
---|
| 60 | case 'added_by_user_id' : |
---|
| 61 | case 'modified_by_user_id' : |
---|
| 62 | $title = "by"; |
---|
| 63 | break; |
---|
| 64 | |
---|
| 65 | case 'added_datetime' : |
---|
| 66 | case 'modified_datetime' : |
---|
| 67 | $title = str_replace(' datetime', '', $title); |
---|
| 68 | break; |
---|
| 69 | } |
---|
| 70 | |
---|
[1] | 71 | // Get primary key. |
---|
| 72 | if ('PRI' == $col[3]) { |
---|
| 73 | $primary_key = $field; |
---|
| 74 | } |
---|
[42] | 75 | |
---|
[1] | 76 | // Column headers. |
---|
| 77 | $headers[$field] = $title; |
---|
[42] | 78 | |
---|
[1] | 79 | // Column data. |
---|
| 80 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 81 | $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '•' : ''; \x3f>"; |
---|
| 82 | } else if ( |
---|
| 83 | 'tinytext' == $type || |
---|
| 84 | 'text' == $type || |
---|
| 85 | 'mediumtext' == $type || |
---|
| 86 | 'longtext' == $type || |
---|
| 87 | 'tinyblob' == $type || |
---|
| 88 | 'blob' == $type || |
---|
| 89 | 'mediumblob' == $type || |
---|
| 90 | 'longblob' == $type |
---|
| 91 | ) { |
---|
[247] | 92 | $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] | 93 | } else if (preg_match('/.*(begin|start).*date.*/i', $field)) { |
---|
[136] | 94 | $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 95 | } else if (preg_match('/.*(end|expire).*date.*/i', $field)) { |
---|
[136] | 96 | $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 97 | } else if (preg_match('/datetime/i', $type)) { |
---|
[136] | 98 | $listrows[] = "<\x3fphp echo '0000-00-00 00:00:00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 99 | } else if (preg_match('/date/i', $type)) { |
---|
[136] | 100 | $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>"; |
---|
[1] | 101 | } else if (preg_match('/(amount|_rate)/i', $field)) { |
---|
| 102 | $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>"; |
---|
[19] | 103 | } else if (preg_match('/(added_by_user_id)/i', $field)) { |
---|
[295] | 104 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>"; |
---|
[19] | 105 | } else if (preg_match('/(modified_by_user_id)/i', $field)) { |
---|
[295] | 106 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_username']); \x3f>"; |
---|
[1] | 107 | } else if ('rank' == $field) { |
---|
| 108 | $listrows[] = "<input type=\"text\" name=\"rank[<\x3fphp echo \$list[\$i]['$primary_key']; \x3f>]\" value=\"<\x3fphp echo \$list[\$i]['rank']; \x3f>\" size=\"5\" />"; |
---|
| 109 | } else { |
---|
| 110 | $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['$field'], true); \x3f>"; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } else { |
---|
| 114 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // Print the template out. |
---|
| 118 | echo isset($op) ? '' : <<<E_O_F |
---|
| 119 | |
---|
| 120 | <\x3fphp \$fv->printErrorMessages(); \x3f> |
---|
| 121 | |
---|
| 122 | <div id="commandbox"> |
---|
[185] | 123 | <form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get"> |
---|
[136] | 124 | <\x3fphp \$app->printHiddenSession(false); \x3f> |
---|
[154] | 125 | <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span> |
---|
[1] | 126 | <br /> |
---|
[42] | 127 | |
---|
[154] | 128 | <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>" /> |
---|
| 129 | <select name="filter___///__"> |
---|
| 130 | <\x3fphp // printSelectForm('__///___tbl', "CONCAT(__///___id, '—', city, '—', title)", '__///___id', getFormData('filter___///__'), array('Any __///__'), 'ORDER BY __///__ ASC'); \x3f> |
---|
[1] | 131 | </select> |
---|
| 132 | <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" /> |
---|
[51] | 133 | </form> |
---|
[1] | 134 | </div> |
---|
| 135 | |
---|
| 136 | <?php include 'list_info.ihtml'; \x3f> |
---|
| 137 | |
---|
[185] | 138 | <form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post"> |
---|
[1] | 139 | <table class="list"> |
---|
| 140 | <tr> |
---|
| 141 | <th> </th> |
---|
| 142 | <th> </th> |
---|
| 143 | |
---|
| 144 | E_O_F; |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | // Print header rows. |
---|
| 148 | if (!isset($op) || 'headerrows' == $op) { |
---|
| 149 | foreach ($headers as $field=>$title) { |
---|
[316] | 150 | if (preg_match('/(_date|_datetime|_time|price|value|quantity)/', $field)) { |
---|
| 151 | $ordering = 'DESC'; |
---|
| 152 | } else { |
---|
| 153 | $ordering = 'ASC'; |
---|
| 154 | } |
---|
| 155 | |
---|
[1] | 156 | if ($field == $primary_key) { |
---|
[316] | 157 | echo " <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'DESC'); \x3f></th>\n"; |
---|
[1] | 158 | } else { |
---|
[316] | 159 | echo " <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), '$ordering'); \x3f></th>\n"; |
---|
[1] | 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | echo isset($op) ? '' : <<<E_O_F |
---|
| 165 | <th> </th> |
---|
| 166 | </tr> |
---|
| 167 | <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f> |
---|
| 168 | <tr> |
---|
[316] | 169 | <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> |
---|
| 170 | <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] | 171 | |
---|
| 172 | E_O_F; |
---|
| 173 | |
---|
| 174 | // Print List rows. |
---|
| 175 | if (!isset($op) || 'listrows' == $op) { |
---|
| 176 | foreach ($listrows as $col_data) { |
---|
| 177 | ?> |
---|
[121] | 178 | <td class="sc-nowrap"><?php echo $col_data; ?> </td> |
---|
[1] | 179 | <?php |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | echo isset($op) ? '' : <<<E_O_F |
---|
[316] | 184 | <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] | 185 | </tr> |
---|
| 186 | <\x3fphp } \x3f> |
---|
| 187 | </table> |
---|
| 188 | |
---|
| 189 | <\x3fphp if (\$page->total_pages > 1) { \x3f> |
---|
[121] | 190 | <div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f> <\x3fphp \$page->printPageNumbers() \x3f></div> |
---|
[1] | 191 | <\x3fphp } \x3f> |
---|
| 192 | </form> |
---|
| 193 | |
---|
| 194 | E_O_F; |
---|
| 195 | |
---|
| 196 | ?> |
---|