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

Last change on this file since 768 was 768, checked in by anonymous, 2 years ago

Minor improvements

File size: 9.4 KB
Line 
1#!/usr/bin/env php
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
28if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) {
29    // First arg is path to current site. Realpath removes trailing /s
30    define('COMMON_BASE', realpath($_SERVER['argv'][1]));
31} else {
32    die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n");
33}
34
35include_once dirname(__FILE__) . '/../_config.inc.php';
36
37$op = null;
38$valid_ops = array('headerrows', 'listrows');
39
40// Test arguments.
41if (isset($_SERVER['argv'][2])) {
42    // Second arg is db table.
43    $db_tbl = $_SERVER['argv'][2];
44} else {
45    die(sprintf("Usage: %s site_directory db_table [operation]\nValid operations include: %s\n", basename($_SERVER['argv'][0]), join(', ', $valid_ops)));
46}
47
48// Test for operation.
49if (isset($_SERVER['argv'][3])) {
50    // Optional third arg is op.
51    $op = $_SERVER['argv'][3];
52    // Make sure op is valid.
53    if (!in_array($op, $valid_ops)) {
54        die(basename($_SERVER['argv'][0]) . " Warning: Operation '$op' is not something I know how to do Please select one of: " . join(", ", $valid_ops) . "\n");
55    }
56}
57
58// Get DB tables.
59$qid = $db->query("SHOW TABLES");
60while (list($row) = mysql_fetch_row($qid)) {
61    $tables[] = $row;
62}
63
64// Make sure requested table is in database.
65if (!in_array($db_tbl, $tables)) {
66    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)));
67}
68
69// Get DB table column info.
70$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
71while ($row = mysql_fetch_row($qid)) {
72    $cols[] = $row;
73}
74
75$primary_key = '__///__';
76
77// Loop through columns
78if (is_array($cols) && !empty($cols)) {
79    foreach ($cols as $col) {
80
81        // Human readable.
82        $field = $col[0];
83        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
84        $title = ucfirst(str_replace('_', ' ', $field));
85
86        switch ($field) {
87        case 'added_by_user_id' :
88        case 'modified_by_user_id' :
89            $title = "by";
90            break;
91
92        case 'added_datetime' :
93        case 'modified_datetime' :
94            $title = str_replace(' datetime', '', $title);
95            break;
96        }
97
98        // Get primary key.
99        if ('PRI' == $col[3]) {
100            $primary_key = $field;
101        }
102
103        // Column headers.
104        $headers[$field] = $title;
105
106        // Column data.
107        if (preg_match("/enum\('true'\)/", $col[1])) {
108            $listrows[] = "<\x3fphp echo (!empty(\$list[\$i]['$field'])) ? '&bull;' : ''; \x3f>";
109        } else if (
110            'tinytext' == $type ||
111            'text' == $type ||
112            'mediumtext' == $type ||
113            'longtext' == $type ||
114            'tinyblob' == $type ||
115            'blob' == $type ||
116            'mediumblob' == $type ||
117            'longblob' == $type
118        ) {
119            $listrows[] = "<\x3fphp echo mb_strlen(\$list[\$i]['$field'])<50 \x3f oTxt(\$list[\$i]['$field']) : oTxt(trim(mb_substr(\$list[\$i]['$field'], 0, 50)) . '...'); \x3f>";
120        } else if (preg_match('/.*(begin|start).*date.*/i', $field)) {
121            $listrows[] = "<\x3fphp echo \$db->getParam('zero_date') == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
122        } else if (preg_match('/.*(end|expire).*date.*/i', $field)) {
123            $listrows[] = "<\x3fphp echo '9999-12-31' == \$list[\$i]['$field'] ? '' : date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])); \x3f>";
124        } else if (preg_match('/datetime/i', $type)) {
125            $listrows[] = "<\x3fphp echo Validator::validateStrDate(\$list[\$i]['$field']) ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>";
126        } else if (preg_match('/date/i', $type)) {
127            $listrows[] = "<\x3fphp echo Validator::validateStrDate(\$list[\$i]['$field']) ? date(\$app->getParam('date_format'), strtotime(\$list[\$i]['$field'])) : ''; \x3f>";
128        } else if (preg_match('/(amount|_rate)/i', $field)) {
129            $listrows[] = "<\x3fphp printf('$%01.2f', \$list[\$i]['$field']); \x3f>";
130        } else if (preg_match('/(added_by_user_id)/i', $field)) {
131            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['added_by_username']); \x3f>";
132        } else if (preg_match('/(modified_by_user_id)/i', $field)) {
133            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['modified_by_username']); \x3f>";
134        } else if ('rank' == $field) {
135            $listrows[] = "<input type=\"text\" name=\"rank[<\x3fphp echo \$list[\$i]['$primary_key']; \x3f>]\" value=\"<\x3fphp echo \$list[\$i]['rank']; \x3f>\" size=\"5\" />";
136        } else {
137            $listrows[] = "<\x3fphp echo oTxt(\$list[\$i]['$field']); \x3f>";
138        }
139    }
140} else {
141    die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
142}
143
144// Print the template out.
145echo isset($op) ? '' : <<<E_O_F
146
147<\x3fphp \$fv->printErrorMessages(); \x3f>
148
149<div class="commandbox">
150<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="get">
151<\x3fphp \$app->printHiddenSession(false); \x3f>
152    <span class="sc-nowrap commandtext"><a href="<\x3fphp echo \$app->oHREF(\$_SERVER['PHP_SELF'] . '?op=add'); \x3f>"><\x3fphp echo _("Add __///__"); \x3f></a></span>
153    <br />
154
155    <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>" />
156    <select name="filter___///__">
157        <\x3fphp // printSelectForm('__///___tbl', "CONCAT(__///___id, '&mdash;', city, '&mdash;', title)", '__///___id', getFormData('filter___///__'), array('' => 'Any __///__'), 'ORDER BY __///__ ASC'); \x3f>
158    </select>
159    <input type="submit" name="list" value="<\x3fphp echo _("Search"); \x3f>" />
160</form>
161</div>
162
163<?php include 'list_info.ihtml'; \x3f>
164
165<form action="<\x3fphp echo oTxt(\$_SERVER['PHP_SELF']); \x3f>" method="post">
166<table class="list">
167    <tr>
168        <th>&nbsp;</th>
169        <th>&nbsp;</th>
170
171E_O_F;
172
173
174// Print header rows.
175if (!isset($op) || 'headerrows' == $op) {
176    foreach ($headers as $field=>$title) {
177        if (preg_match('/(_date|_datetime|_time|price|value|quantity)/', $field)) {
178            $ordering = 'DESC';
179        } else {
180            $ordering = 'ASC';
181        }
182
183        if ($field == $primary_key) {
184            echo "        <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"ID\"), 'DESC'); \x3f></th>\n";
185        } else {
186            echo "        <th><\x3fphp echo \$so->printSortHeader('$db_tbl.$field', _(\"$title\"), '$ordering'); \x3f></th>\n";
187        }
188    }
189}
190
191echo isset($op) ? '' : <<<E_O_F
192        <th>&nbsp;</th>
193    </tr>
194    <\x3fphp for (\$i = 0; \$i <= \$page->last_item - \$page->first_item && \$page->total_items > 0; \$i++) { \x3f>
195    <tr>
196        <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></td>
197        <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></td>
198
199E_O_F;
200
201// Print List rows.
202if (!isset($op) || 'listrows' == $op) {
203    foreach ($listrows as $col_data) {
204?>
205        <td class="sc-nowrap"><?php echo $col_data; ?></td>
206<?php
207    }
208}
209
210echo isset($op) ? '' : <<<E_O_F
211        <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></td>
212    </tr>
213    <\x3fphp } \x3f>
214</table>
215
216<\x3fphp if (\$page->total_pages > 1) { \x3f>
217<div class="sc-nowrap commandtext" style="float: right;"><\x3fphp echo _("Pages:"); \x3f>&nbsp;<\x3fphp \$page->printPageNumbers() \x3f></div>
218<\x3fphp } \x3f>
219</form>
220
221E_O_F;
Note: See TracBrowser for help on using the repository browser.