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

Last change on this file since 320 was 320, checked in by quinn, 16 years ago

Added a glob function to the ImageThumb? deleteThumbs method. Fixed some HTML errors. Minor admin2 CSS tweaks.

File size: 8.2 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]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
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 " . $db->escapeString($db_tbl));
44while ($row = mysql_fetch_row($qid)) {
45    $cols[] = $row;
46}
47
48$primary_key = '__///__';
49
50// Loop through columns
51if (is_array($cols) && !empty($cols)) {
52    foreach ($cols as $col) {
53
54        // Human readable.
55        $field = $col[0];
56        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
57        $title = ucfirst(str_replace('_', ' ', $field));
58
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
71        // Get primary key.
72        if ('PRI' == $col[3]) {
73            $primary_key = $field;
74        }
75
76        // Column headers.
77        $headers[$field] = $title;
78
79        // Column data.
80        if (preg_match("/enum\('true'\)/", $col[1])) {
81            $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '&bull;' : ''; \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        ) {
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>";
93        } else if (preg_match('/.*(begin|start).*date.*/i', $field)) {
94            $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
95        } else if (preg_match('/.*(end|expire).*date.*/i', $field)) {
96            $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
97        } else if (preg_match('/datetime/i', $type)) {
98            $listrows[] = "<\x3fphp echo '0000-00-00 00:00:00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
99        } else if (preg_match('/date/i', $type)) {
100            $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
101        } else if (preg_match('/(amount|_rate)/i', $field)) {
102            $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>";
103        } else if (preg_match('/(added_by_user_id)/i', $field)) {
104            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>";
105        } else if (preg_match('/(modified_by_user_id)/i', $field)) {
106            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_username']); \x3f>";
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.
118echo isset($op) ? '' : <<<E_O_F
119
120<\x3fphp \$fv->printErrorMessages(); \x3f>
121
122<div id="commandbox">
123<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get">
124<\x3fphp \$app->printHiddenSession(false); \x3f>
125    <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span>
126    <br />
127
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, '&mdash;', city, '&mdash;', title)", '__///___id', getFormData('filter___///__'), array('Any __///__'), 'ORDER BY __///__ ASC'); \x3f>
131    </select>
132    <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" />
133</form>
134</div>
135
136<?php include 'list_info.ihtml'; \x3f>
137
138<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post">
139<table class="list">
140    <tr>
141        <th>&nbsp;</th>
142        <th>&nbsp;</th>
143
144E_O_F;
145
146
147// Print header rows.
148if (!isset($op) || 'headerrows' == $op) {
149    foreach ($headers as $field=>$title) {
150        if (preg_match('/(_date|_datetime|_time|price|value|quantity)/', $field)) {
151            $ordering = 'DESC';
152        } else {
153            $ordering = 'ASC';
154        }
155
156        if ($field == $primary_key) {
157            echo "        <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'DESC'); \x3f></th>\n";
158        } else {
159            echo "        <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), '$ordering'); \x3f></th>\n";
160        }
161    }
162}
163
164echo isset($op) ? '' : <<<E_O_F
165        <th>&nbsp;</th>
166    </tr>
167    <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f>
168    <tr>
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> &nbsp;</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> &nbsp;</td>
171
172E_O_F;
173
174// Print List rows.
175if (!isset($op) || 'listrows' == $op) {
176    foreach ($listrows as $col_data) {
177?>
178        <td class="sc-nowrap"><?php echo $col_data; ?> &nbsp;</td>
179<?php
180    }
181}
182
183echo isset($op) ? '' : <<<E_O_F
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> &nbsp;</td>
185    </tr>
186    <\x3fphp } \x3f>
187</table>
188
189<\x3fphp if (\$page->total_pages > 1) { \x3f>
190<div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f>&nbsp;<\x3fphp \$page->printPageNumbers() \x3f></div>
191<\x3fphp } \x3f>
192</form>
193
194E_O_F;
195
196?>
Note: See TracBrowser for help on using the repository browser.