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

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

Updated example config file. Added admin2.inc.css and minor corrections into HTML. Module maker fixes.

File size: 7.8 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        $title = ucfirst(str_replace('_', ' ', $field));
57        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
58
59        // Get primary key.
60        if ('PRI' == $col[3]) {
61            $primary_key = $field;
62        }
63
64        // Column headers.
65        $headers[$field] = $title;
66
67        // Column data.
68        if (preg_match("/enum\('true'\)/", $col[1])) {
69            $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '&bull;' : ''; \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        ) {
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>";
81        } else if (preg_match('/.*(begin|start).*date.*/i', $field)) {
82            $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
83        } else if (preg_match('/.*(end|expire).*date.*/i', $field)) {
84            $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
85        } else if (preg_match('/datetime/i', $type)) {
86            $listrows[] = "<\x3fphp echo '0000-00-00 00:00:00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
87        } else if (preg_match('/date/i', $type)) {
88            $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
89        } else if (preg_match('/(amount|_rate)/i', $field)) {
90            $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>";
91        } else if (preg_match('/(added_by_user_id)/i', $field)) {
92            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>";
93        } else if (preg_match('/(modified_by_user_id)/i', $field)) {
94            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_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.
106echo isset($op) ? '' : <<<E_O_F
107
108<\x3fphp \$fv->printErrorMessages(); \x3f>
109
110<div id="commandbox">
111<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get">
112<\x3fphp \$app->printHiddenSession(false); \x3f>
113    <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span>
114    <br />
115
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, '&mdash;', city, '&mdash;', title)", '__///___id', getFormData('filter___///__'), array('Any __///__'), 'ORDER BY __///__ ASC'); \x3f>
119    </select>
120    <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" />
121</form>
122</div>
123
124<?php include 'list_info.ihtml'; \x3f>
125
126<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post">
127<table class="list">
128    <tr>
129        <th>&nbsp;</th>
130        <th>&nbsp;</th>
131
132E_O_F;
133
134
135// Print header rows.
136if (!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
146echo isset($op) ? '' : <<<E_O_F
147        <th>&nbsp;</th>
148    </tr>
149    <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f>
150    <tr>
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> &nbsp;</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> &nbsp;</td>
153
154E_O_F;
155
156// Print List rows.
157if (!isset($op) || 'listrows' == $op) {
158    foreach ($listrows as $col_data) {
159?>
160        <td class="sc-nowrap"><?php echo $col_data; ?> &nbsp;</td>
161<?php
162    }
163}
164
165echo isset($op) ? '' : <<<E_O_F
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> &nbsp;</td>
167    </tr>
168    <\x3fphp } \x3f>
169</table>
170
171<\x3fphp if (\$page->total_pages > 1) { \x3f>
172<div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f>&nbsp;<\x3fphp \$page->printPageNumbers() \x3f></div>
173<\x3fphp } \x3f>
174</form>
175
176E_O_F;
177
178?>
Note: See TracBrowser for help on using the repository browser.