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