source: trunk/bin/module_maker/list_template.cli.php @ 20

Last change on this file since 20 was 20, checked in by scdev, 19 years ago

Tons of little updates and bugfixes. CSS updates to templates and core css files. File upload ability to module_maker. Remade Upload interface to use setParam/getParam.

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