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

Last change on this file since 461 was 461, checked in by anonymous, 10 years ago

Empty strings are better than zero for default select options. Updated functionality of Nav::currentPage().

File size: 9.1 KB
Line 
1#!/usr/bin/php -q
2<?php
3/**
4 * The Strangecode Codebase - a general application development framework for PHP
5 * For details visit the project site: <http://trac.strangecode.com/codebase/>
6 * Copyright 2001-2012 Strangecode, LLC
7 *
8 * This file is part of The Strangecode Codebase.
9 *
10 * The Strangecode Codebase is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by the
12 * Free Software Foundation, either version 3 of the License, or (at your option)
13 * any later version.
14 *
15 * The Strangecode Codebase is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/**
25 * list_template.cli.php
26 */
27
28include_once dirname(__FILE__) . '/_config.inc.php';
29
30$op = null;
31$valid_ops = array('headerrows', 'listrows');
32
33// Test arguments.
34if (isset($_SERVER['argv'][2])) {
35    // Second arg is db table.
36    $db_tbl = $_SERVER['argv'][2];
37} else {
38    die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
39}
40
41// Test for operation.
42if (isset($_SERVER['argv'][3])) {
43    // Optional third arg is op.
44    $op = $_SERVER['argv'][3];
45    // Make sure op is valid.
46    if (!in_array($op, $valid_ops)) {
47        die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n");
48    }
49}
50
51// Get DB tables.
52$qid = $db->query("SHOW TABLES");
53while (list($row) = mysql_fetch_row($qid)) {
54    $tables[] = $row;
55}
56
57// Make sure requested table is in database.
58if (!in_array($db_tbl, $tables)) {
59    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)));
60}
61
62// Get DB table column info.
63$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
64while ($row = mysql_fetch_row($qid)) {
65    $cols[] = $row;
66}
67
68$primary_key = '__///__';
69
70// Loop through columns
71if (is_array($cols) && !empty($cols)) {
72    foreach ($cols as $col) {
73
74        // Human readable.
75        $field = $col[0];
76        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
77        $title = ucfirst(str_replace('_', ' ', $field));
78
79        switch ($field) {
80        case 'added_by_user_id' :
81        case 'modified_by_user_id' :
82            $title = "by";
83            break;
84
85        case 'added_datetime' :
86        case 'modified_datetime' :
87            $title = str_replace(' datetime', '', $title);
88            break;
89        }
90
91        // Get primary key.
92        if ('PRI' == $col[3]) {
93            $primary_key = $field;
94        }
95
96        // Column headers.
97        $headers[$field] = $title;
98
99        // Column data.
100        if (preg_match("/enum\('true'\)/", $col[1])) {
101            $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '&bull;' : ''; \x3f>";
102        } else if (
103            'tinytext' == $type ||
104            'text' == $type ||
105            'mediumtext' == $type ||
106            'longtext' == $type ||
107            'tinyblob' == $type ||
108            'blob' == $type ||
109            'mediumblob' == $type ||
110            'longblob' == $type
111        ) {
112            $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>";
113        } else if (preg_match('/.*(begin|start).*date.*/i', $field)) {
114            $listrows[] = "<\x3fphp echo '0000-00-00' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
115        } else if (preg_match('/.*(end|expire).*date.*/i', $field)) {
116            $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
117        } else if (preg_match('/datetime/i', $type)) {
118            $listrows[] = "<\x3fphp echo Validator::validateStrDate(\$list[\$i]['$field']) ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>";
119        } else if (preg_match('/date/i', $type)) {
120            $listrows[] = "<\x3fphp echo Validator::validateStrDate(\$list[\$i]['$field']) ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>";
121        } else if (preg_match('/(amount|_rate)/i', $field)) {
122            $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>";
123        } else if (preg_match('/(added_by_user_id)/i', $field)) {
124            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>";
125        } else if (preg_match('/(modified_by_user_id)/i', $field)) {
126            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_username']); \x3f>";
127        } else if ('rank' == $field) {
128            $listrows[] = "<input type=\"text\" name=\"rank[<\x3fphp echo \$list[\$i]['$primary_key']; \x3f>]\" value=\"<\x3fphp echo \$list[\$i]['rank']; \x3f>\" size=\"5\" />";
129        } else {
130            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['$field'], true); \x3f>";
131        }
132    }
133} else {
134    die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
135}
136
137// Print the template out.
138echo isset($op) ? '' : <<<E_O_F
139
140<\x3fphp \$fv->printErrorMessages(); \x3f>
141
142<div class="commandbox">
143<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get">
144<\x3fphp \$app->printHiddenSession(false); \x3f>
145    <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span>
146    <br />
147
148    <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>" />
149    <select name="filter___///__">
150        <\x3fphp // printSelectForm('__///___tbl', "CONCAT(__///___id, '&mdash;', city, '&mdash;', title)", '__///___id', getFormData('filter___///__'), array('' => 'Any __///__'), 'ORDER BY __///__ ASC'); \x3f>
151    </select>
152    <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" />
153</form>
154</div>
155
156<?php include 'list_info.ihtml'; \x3f>
157
158<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post">
159<table class="list">
160    <tr>
161        <th>&nbsp;</th>
162        <th>&nbsp;</th>
163
164E_O_F;
165
166
167// Print header rows.
168if (!isset($op) || 'headerrows' == $op) {
169    foreach ($headers as $field=>$title) {
170        if (preg_match('/(_date|_datetime|_time|price|value|quantity)/', $field)) {
171            $ordering = 'DESC';
172        } else {
173            $ordering = 'ASC';
174        }
175
176        if ($field == $primary_key) {
177            echo "        <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'DESC'); \x3f></th>\n";
178        } else {
179            echo "        <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), '$ordering'); \x3f></th>\n";
180        }
181    }
182}
183
184echo isset($op) ? '' : <<<E_O_F
185        <th>&nbsp;</th>
186    </tr>
187    <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f>
188    <tr>
189        <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>
190        <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>
191
192E_O_F;
193
194// Print List rows.
195if (!isset($op) || 'listrows' == $op) {
196    foreach ($listrows as $col_data) {
197?>
198        <td class="sc-nowrap"><?php echo $col_data; ?> &nbsp;</td>
199<?php
200    }
201}
202
203echo isset($op) ? '' : <<<E_O_F
204        <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>
205    </tr>
206    <\x3fphp } \x3f>
207</table>
208
209<\x3fphp if (\$page->total_pages > 1) { \x3f>
210<div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f>&nbsp;<\x3fphp \$page->printPageNumbers() \x3f></div>
211<\x3fphp } \x3f>
212</form>
213
214E_O_F;
215
216?>
Note: See TracBrowser for help on using the repository browser.