1 | #!/usr/local/bin/php |
---|
2 | <?php |
---|
3 | /** |
---|
4 | * module.cli.php |
---|
5 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
6 | */ |
---|
7 | |
---|
8 | |
---|
9 | // Get module maker configuration. |
---|
10 | require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
11 | |
---|
12 | |
---|
13 | /****************************************************************************** |
---|
14 | * CONFIG |
---|
15 | *****************************************************************************/ |
---|
16 | |
---|
17 | // Test for a single argument. |
---|
18 | if ($_SERVER['argc'] == 5 && $_SERVER['argv'][4] == 'clean') { |
---|
19 | $module_name_singular = $_SERVER['argv'][2]; |
---|
20 | $module_name_plural = $_SERVER['argv'][3]; |
---|
21 | } else if ($_SERVER['argc'] == 4) { |
---|
22 | // Normal operation. |
---|
23 | $module_name_singular = $_SERVER['argv'][2]; |
---|
24 | $module_name_plural = $_SERVER['argv'][3]; |
---|
25 | } else if ($_SERVER['argc'] == 3 && $_SERVER['argv'][2] == '_default_') { |
---|
26 | // Hacked mode: use these module names: |
---|
27 | $module_name_singular = 'event'; |
---|
28 | $module_name_plural = 'events'; |
---|
29 | echo "Notice: using hacked module names.\n"; |
---|
30 | } else { |
---|
31 | die(basename($_SERVER['argv'][0]) . " Error: invalid arguments. Try like this: |
---|
32 | |
---|
33 | " . basename($_SERVER['argv'][0]) . " <sitedirectory> <namesingular> <nameplural> |
---|
34 | |
---|
35 | E.g.: " . basename($_SERVER['argv'][0]) . " ~/Sites/www.asdf.com category categories |
---|
36 | |
---|
37 | There is also a secret _default_ option that uses options hard-coded into the script. Hack the script for more info. |
---|
38 | The following files will be generated by this script: |
---|
39 | /admin/nameplural.php |
---|
40 | /admin/_templates/namesingular_list.ihtml |
---|
41 | /admin/_templates/namesingular_form.ihtml |
---|
42 | /html/nameplural.php |
---|
43 | /html/_templates/namesingular_list.ihtml |
---|
44 | /html/_templates/namesingular.ihtml |
---|
45 | |
---|
46 | "); |
---|
47 | } |
---|
48 | |
---|
49 | // Where deleted files go: |
---|
50 | $user_trash_folder = $_SERVER['HOME'] . '/.Trash'; |
---|
51 | |
---|
52 | // Directories |
---|
53 | $skel_dir = realpath(CODE_BASE . '/bin/module_maker/skel'); |
---|
54 | $admin_dir = realpath(CODE_BASE . '/../admin'); |
---|
55 | $admin_tpl_dir = realpath(CODE_BASE . '/../admin/_templates'); |
---|
56 | $public_dir = realpath(CODE_BASE . '/../html'); |
---|
57 | $public_tpl_dir = realpath(CODE_BASE . '/../html/_templates'); |
---|
58 | |
---|
59 | // Names |
---|
60 | $module_title = ucwords($module_name_plural); |
---|
61 | $item_title = ucwords($module_name_singular); |
---|
62 | $module_name_upper = strtoupper($module_name_plural); |
---|
63 | |
---|
64 | // Admin files |
---|
65 | $admin_script = $module_name_plural . '.php'; |
---|
66 | $admin_list_template = $module_name_singular . '_list.ihtml'; |
---|
67 | $admin_form_template = $module_name_singular . '_form.ihtml'; |
---|
68 | |
---|
69 | // Public files |
---|
70 | $public_script = $module_name_plural . '.php'; |
---|
71 | $public_list_template = $module_name_singular . '_list.ihtml'; |
---|
72 | $public_detail_template = $module_name_singular . '.ihtml'; |
---|
73 | |
---|
74 | // Databaes names |
---|
75 | $db_tbl = $module_name_singular . '_tbl'; |
---|
76 | $primary_key = $module_name_singular . '_id'; |
---|
77 | |
---|
78 | |
---|
79 | // Only after we've defined essential files can we clean. |
---|
80 | if ($_SERVER['argc'] == 5 && $_SERVER['argv'][4] == 'clean') { |
---|
81 | echo "Beginning file cleanup\n"; |
---|
82 | trashFile("$admin_dir/$admin_script"); |
---|
83 | trashFile("$admin_tpl_dir/$admin_list_template"); |
---|
84 | trashFile("$admin_tpl_dir/$admin_form_template"); |
---|
85 | trashFile("$public_dir/$public_script"); |
---|
86 | trashFile("$public_tpl_dir/$public_list_template"); |
---|
87 | trashFile("$public_tpl_dir/$public_detail_template"); |
---|
88 | echo "End file cleanup\n"; |
---|
89 | die; |
---|
90 | } |
---|
91 | |
---|
92 | // Go! |
---|
93 | echo 'Running Module Maker. Using database: ' . $CFG->database . "\n"; |
---|
94 | |
---|
95 | |
---|
96 | /****************************************************************************** |
---|
97 | * PREPROCESSING |
---|
98 | *****************************************************************************/ |
---|
99 | |
---|
100 | // Ensure skel files exist. |
---|
101 | if ( |
---|
102 | !file_exists("$skel_dir/admin.php") || |
---|
103 | !file_exists("$skel_dir/public.php") || |
---|
104 | !file_exists("$skel_dir/public.ihtml") || |
---|
105 | !file_exists("$skel_dir/public_list.ihtml") |
---|
106 | ) { |
---|
107 | die(basename($_SERVER['argv'][0]) . " Warning: one or more skeleton source files missing. Please check $skel_dir.\n"); |
---|
108 | } |
---|
109 | |
---|
110 | // Ensure essential directories exist. |
---|
111 | if (!is_dir("$admin_dir")) { |
---|
112 | die(basename($_SERVER['argv'][0]) . " Error: $admin_dir directory not found.\n"); |
---|
113 | } |
---|
114 | if (!is_dir("$admin_tpl_dir")) { |
---|
115 | die(basename($_SERVER['argv'][0]) . " Error: $admin_tpl_dir directory not found.\n"); |
---|
116 | } |
---|
117 | if (!is_dir("$public_dir")) { |
---|
118 | die(basename($_SERVER['argv'][0]) . " Error: $public_dir directory not found.\n"); |
---|
119 | } |
---|
120 | if (!is_dir("$public_tpl_dir")) { |
---|
121 | die(basename($_SERVER['argv'][0]) . " Error: $public_tpl_dir directory not found.\n"); |
---|
122 | } |
---|
123 | |
---|
124 | // Get DB tables. |
---|
125 | $qid = dbQuery("SHOW TABLES"); |
---|
126 | while (list($row) = mysql_fetch_row($qid)) { |
---|
127 | $tables[] = $row; |
---|
128 | } |
---|
129 | |
---|
130 | // Make sure requested table is in database. |
---|
131 | if (!in_array($db_tbl, $tables)) { |
---|
132 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not exist in database $CFG->database. Please select one of: \n\n" . join("\n", $tables) . "\n\n"); |
---|
133 | } |
---|
134 | |
---|
135 | // Ensure requested table contains columns. |
---|
136 | // Get DB table column info. |
---|
137 | $qid = dbQuery("DESCRIBE " . mysql_real_escape_string($db_tbl)); |
---|
138 | while ($row = mysql_fetch_row($qid)) { |
---|
139 | $cols[] = $row; |
---|
140 | } |
---|
141 | if (!is_array($cols) || empty($cols)) { |
---|
142 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | /****************************************************************************** |
---|
147 | * SETUP VARIABLES |
---|
148 | *****************************************************************************/ |
---|
149 | |
---|
150 | |
---|
151 | // Loop through columns |
---|
152 | $multipart_form_required = false; |
---|
153 | $headers = array(); |
---|
154 | $public_list_page_vars = array(); |
---|
155 | $public_detail_page_vars = array(); |
---|
156 | if (is_array($cols) && !empty($cols)) { |
---|
157 | foreach ($cols as $col) { |
---|
158 | |
---|
159 | // Human readable. |
---|
160 | $field = $col[0]; |
---|
161 | $field_title = ucfirst(str_replace('_', ' ', $field)); |
---|
162 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
163 | |
---|
164 | // Get primary key. |
---|
165 | // if ('PRI' == $col[3]) { |
---|
166 | // $primary_key = $field; |
---|
167 | // } |
---|
168 | |
---|
169 | // Our form will require type="multipart/form-data". |
---|
170 | if (preg_match('/file/i', $field)) { |
---|
171 | $multipart_form_required = false; |
---|
172 | } |
---|
173 | |
---|
174 | // Column headers. |
---|
175 | $headers[$field] = $field_title; |
---|
176 | |
---|
177 | // Get php code for printing variables. |
---|
178 | $public_list_page_vars[] = "<\x3fphp echo oTxt(\$" . $module_name_singular . "_list[\$i]['$field']); \x3f>"; |
---|
179 | $public_detail_page_vars[] = "<\x3fphp echo oTxt(\$item['$field']); \x3f>"; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | // -------------FILES------------- |
---|
184 | |
---|
185 | $skel_files['admin_script'] = file_get_contents($skel_dir . '/admin.php'); |
---|
186 | $skel_files['admin_list_template'] = file_get_contents($skel_dir . '/adm_list.ihtml'); |
---|
187 | $skel_files['admin_form_template'] = file_get_contents($skel_dir . '/adm_form.ihtml'); |
---|
188 | $skel_files['public_script'] = file_get_contents($skel_dir . '/public.php'); |
---|
189 | $skel_files['public_list_template'] = file_get_contents($skel_dir . '/public_list.ihtml'); |
---|
190 | $skel_files['public_detail_template'] = file_get_contents($skel_dir . '/public.ihtml'); |
---|
191 | |
---|
192 | |
---|
193 | // -------------VARS------------- |
---|
194 | |
---|
195 | // Simple... |
---|
196 | |
---|
197 | $search['date'] = '/%DATE%/'; |
---|
198 | $replace['date'] = date($CFG->date_format); |
---|
199 | |
---|
200 | $search['name_plural'] = '/%NAME_PLURAL%/'; |
---|
201 | $replace['name_plural'] = $module_name_plural; |
---|
202 | |
---|
203 | $search['name_singular'] = '/%NAME_SINGULAR%/'; |
---|
204 | $replace['name_singular'] = $module_name_singular; |
---|
205 | |
---|
206 | $search['title'] = '/%TITLE%/'; |
---|
207 | $replace['title'] = $module_title; |
---|
208 | |
---|
209 | $search['item_title'] = '/%ITEM_TITLE%/'; |
---|
210 | $replace['item_title'] = $item_title; |
---|
211 | |
---|
212 | $search['primary_key'] = '/%PRIMARY_KEY%/'; |
---|
213 | $replace['primary_key'] = $primary_key; |
---|
214 | |
---|
215 | $search['db_tbl'] = '/%DB_TBL%/'; |
---|
216 | $replace['db_tbl'] = $db_tbl; |
---|
217 | |
---|
218 | $search['name_upper'] = '/%NAME_UPPER%/'; |
---|
219 | $replace['name_upper'] = $module_name_upper; |
---|
220 | |
---|
221 | $search['admin_script'] = '/%ADMIN_SCRIPT%/'; |
---|
222 | $replace['admin_script'] = $admin_script; |
---|
223 | |
---|
224 | $search['admin_list_template'] = '/%ADMIN_LIST_TEMPLATE%/'; |
---|
225 | $replace['admin_list_template'] = $admin_list_template; |
---|
226 | |
---|
227 | $search['admin_form_template'] = '/%ADMIN_FORM_TEMPLATE%/'; |
---|
228 | $replace['admin_form_template'] = $admin_form_template; |
---|
229 | |
---|
230 | $search['public_script'] = '/%PUBLIC_SCRIPT%/'; |
---|
231 | $replace['public_script'] = $public_script; |
---|
232 | |
---|
233 | $search['public_list_template'] = '/%PUBLIC_LIST_TEMPLATE%/'; |
---|
234 | $replace['public_list_template'] = $public_list_template; |
---|
235 | |
---|
236 | $search['public_detail_template'] = '/%PUBLIC_DETAIL_TEMPLATE%/'; |
---|
237 | $replace['public_detail_template'] = $public_detail_template; |
---|
238 | |
---|
239 | $search['public_detail_page_vars'] = '/%PUBLIC_DETAIL_PAGE_VARS%/'; |
---|
240 | $replace['public_detail_page_vars'] = join("\n", $public_detail_page_vars); |
---|
241 | |
---|
242 | $search['public_list_page_vars'] = '/%PUBLIC_LIST_PAGE_VARS%/'; |
---|
243 | $replace['public_list_page_vars'] = join("\n", $public_list_page_vars); |
---|
244 | |
---|
245 | $search['search_fields'] = '/%SEARCH_FIELDS%/'; |
---|
246 | $replace['search_fields'] = join(", ", $headers); |
---|
247 | |
---|
248 | $search['admin_form_tag_init'] = '/%ADMIN_FORM_TAG_INIT%/'; |
---|
249 | if ($multipart_form_required) { |
---|
250 | $replace['admin_form_tag_init'] = "<form enctype=\"multipart/form-data\" method=\"post\" action=\"<\x3fphp echo \$_SERVER['PHP_SELF']; \x3f>\">\n<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"12000000\">"; |
---|
251 | } else { |
---|
252 | $replace['admin_form_tag_init'] = "<form method=\"post\" action=\"<\x3fphp echo \$_SERVER['PHP_SELF']; \x3f>\">"; |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | // Complex.... |
---|
257 | |
---|
258 | echo "Generating admin form table rows...\n"; |
---|
259 | $output = array(); |
---|
260 | exec(dirname($_SERVER['argv'][0]) . "/form_template.cli.php $db_tbl", $output, $return_val); |
---|
261 | if ($return_val == 0) { |
---|
262 | $search['adm_form_table_rows'] = '/%ADM_FORM_TABLE_ROWS%/'; |
---|
263 | $replace['adm_form_table_rows'] = join("\n", $output); |
---|
264 | } else { |
---|
265 | die(basename($_SERVER['argv'][0]) . " Error: could not execute form_template.cli.php.\n"); |
---|
266 | } |
---|
267 | |
---|
268 | echo "Generating admin list table header rows...\n"; |
---|
269 | $output = array(); |
---|
270 | exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php $db_tbl headerrows", $output, $return_val); |
---|
271 | if ($return_val == 0) { |
---|
272 | $search['adm_list_header_rows'] = '/%ADM_LIST_HEADER_ROWS%/'; |
---|
273 | $replace['adm_list_header_rows'] = join("\n", $output); |
---|
274 | } else { |
---|
275 | die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); |
---|
276 | } |
---|
277 | |
---|
278 | echo "Generating admin list table rows...\n"; |
---|
279 | $output = array(); |
---|
280 | exec(dirname($_SERVER['argv'][0]) . "/list_template.cli.php $db_tbl listrows", $output, $return_val); |
---|
281 | if ($return_val == 0) { |
---|
282 | $search['adm_list_rows'] = '/%ADM_LIST_ROWS%/'; |
---|
283 | $replace['adm_list_rows'] = join("\n", $output); |
---|
284 | } else { |
---|
285 | die(basename($_SERVER['argv'][0]) . " Error: could not execute list_template.cli.php.\n"); |
---|
286 | } |
---|
287 | |
---|
288 | echo "Generating sortorder...\n"; |
---|
289 | $output = array(); |
---|
290 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php $db_tbl sortorder", $output, $return_val); |
---|
291 | if ($return_val == 0) { |
---|
292 | $search['sort_order'] = '/%SORT_ORDER%/'; |
---|
293 | $replace['sort_order'] = join("\n", $output); |
---|
294 | } else { |
---|
295 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
296 | } |
---|
297 | |
---|
298 | echo "Generating insert data...\n"; |
---|
299 | $output = array(); |
---|
300 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php $db_tbl insert", $output, $return_val); |
---|
301 | if ($return_val == 0) { |
---|
302 | $search['insert'] = '/%INSERT%/'; |
---|
303 | $replace['insert'] = join("\n", $output); |
---|
304 | } else { |
---|
305 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
306 | } |
---|
307 | |
---|
308 | echo "Generating update data...\n"; |
---|
309 | $output = array(); |
---|
310 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php $db_tbl update", $output, $return_val); |
---|
311 | if ($return_val == 0) { |
---|
312 | $search['update'] = '/%UPDATE%/'; |
---|
313 | $replace['update'] = join("\n", $output); |
---|
314 | } else { |
---|
315 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
316 | } |
---|
317 | |
---|
318 | echo "Generating search data...\n"; |
---|
319 | $output = array(); |
---|
320 | exec(dirname($_SERVER['argv'][0]) . "/sql.cli.php $db_tbl search", $output, $return_val); |
---|
321 | if ($return_val == 0) { |
---|
322 | $search['search'] = '/%SEARCH%/'; |
---|
323 | $replace['search'] = join("\n", $output); |
---|
324 | } else { |
---|
325 | die(basename($_SERVER['argv'][0]) . " Error: could not execute sql.cli.php.\n"); |
---|
326 | } |
---|
327 | |
---|
328 | echo "Generating form validation data...\n"; |
---|
329 | $output = array(); |
---|
330 | exec(dirname($_SERVER['argv'][0]) . "/validation.cli.php $db_tbl", $output, $return_val); |
---|
331 | if ($return_val == 0) { |
---|
332 | $search['form_validation'] = '/%FORM_VALIDATION%/'; |
---|
333 | $replace['form_validation'] = join("\n", $output); |
---|
334 | } else { |
---|
335 | die(basename($_SERVER['argv'][0]) . " Error: could not execute validation.cli.php.\n"); |
---|
336 | } |
---|
337 | |
---|
338 | /****************************************************************************** |
---|
339 | * WRITE FILES |
---|
340 | *****************************************************************************/ |
---|
341 | |
---|
342 | // (1) Admin script. |
---|
343 | if (file_exists("$admin_dir/$admin_script")) { |
---|
344 | echo "Admin script already exists: $admin_dir/$admin_script\n"; |
---|
345 | } else { |
---|
346 | echo "Writing admin script: $admin_dir/$admin_script\n"; |
---|
347 | $fp = fopen("$admin_dir/$admin_script", "w"); |
---|
348 | fwrite($fp, preg_replace($search, $replace, $skel_files['admin_script'])); |
---|
349 | fclose($fp); |
---|
350 | } |
---|
351 | |
---|
352 | // (2) Admin list template. |
---|
353 | if (file_exists("$admin_tpl_dir/$admin_list_template")) { |
---|
354 | echo "Admin list template already exists: $admin_tpl_dir/$admin_list_template\n"; |
---|
355 | } else { |
---|
356 | echo "Writing admin list template: $admin_tpl_dir/$admin_list_template\n"; |
---|
357 | $fp = fopen("$admin_tpl_dir/$admin_list_template", "w"); |
---|
358 | fwrite($fp, preg_replace($search, $replace, $skel_files['admin_list_template'])); |
---|
359 | fclose($fp); |
---|
360 | } |
---|
361 | |
---|
362 | // (3) Admin form template. |
---|
363 | if (file_exists("$admin_tpl_dir/$admin_form_template")) { |
---|
364 | echo "Admin form template already exists: $admin_tpl_dir/$admin_form_template\n"; |
---|
365 | } else { |
---|
366 | echo "Writing admin form template: $admin_tpl_dir/$admin_form_template\n"; |
---|
367 | $fp = fopen("$admin_tpl_dir/$admin_form_template", "w"); |
---|
368 | fwrite($fp, preg_replace($search, $replace, $skel_files['admin_form_template'])); |
---|
369 | fclose($fp); |
---|
370 | } |
---|
371 | |
---|
372 | // (4) Public script. |
---|
373 | if (file_exists("$public_dir/$public_script")) { |
---|
374 | echo "Public script already exists: $public_dir/$public_script\n"; |
---|
375 | } else { |
---|
376 | echo "Writing public script: $public_dir/$public_script\n"; |
---|
377 | $fp = fopen("$public_dir/$public_script", "w"); |
---|
378 | fwrite($fp, preg_replace($search, $replace, $skel_files['public_script'])); |
---|
379 | fclose($fp); |
---|
380 | } |
---|
381 | |
---|
382 | // (5) Public list template. |
---|
383 | if (file_exists("$public_tpl_dir/$public_list_template")) { |
---|
384 | echo "Public list template already exists: $public_tpl_dir/$public_list_template\n"; |
---|
385 | } else { |
---|
386 | echo "Writing public list template: $public_tpl_dir/$public_list_template\n"; |
---|
387 | $fp = fopen("$public_tpl_dir/$public_list_template", "w"); |
---|
388 | fwrite($fp, preg_replace($search, $replace, $skel_files['public_list_template'])); |
---|
389 | fclose($fp); |
---|
390 | } |
---|
391 | |
---|
392 | // (6) Public detail template. |
---|
393 | if (file_exists("$public_tpl_dir/$public_detail_template")) { |
---|
394 | echo "Public detail template already exists: $public_tpl_dir/$public_detail_template\n"; |
---|
395 | } else { |
---|
396 | echo "Writing public detail template: $public_tpl_dir/$public_detail_template\n"; |
---|
397 | $fp = fopen("$public_tpl_dir/$public_detail_template", "w"); |
---|
398 | fwrite($fp, preg_replace($search, $replace, $skel_files['public_detail_template'])); |
---|
399 | fclose($fp); |
---|
400 | } |
---|
401 | |
---|
402 | echo "Done!\n"; |
---|
403 | |
---|
404 | |
---|
405 | /****************************************************************************** |
---|
406 | * FUNCTIONS |
---|
407 | *****************************************************************************/ |
---|
408 | |
---|
409 | function trashFile($file_path_name) |
---|
410 | { |
---|
411 | global $user_trash_folder; |
---|
412 | static $file_prefix; |
---|
413 | |
---|
414 | if (!isset($file_prefix)) { |
---|
415 | $file_prefix = time(); |
---|
416 | } else { |
---|
417 | $file_prefix++; |
---|
418 | } |
---|
419 | |
---|
420 | // Make user trash folder. |
---|
421 | if (!is_dir($user_trash_folder)) { |
---|
422 | echo "Attempting to create user trash folder: $user_trash_folder/\n"; |
---|
423 | mkdir($user_trash_folder); |
---|
424 | chmod($user_trash_folder, 0777); |
---|
425 | } |
---|
426 | if (!is_dir("$user_trash_folder") || !is_writable("$user_trash_folder")) { |
---|
427 | die("User trash directory not available: $user_trash_folder/\n"); |
---|
428 | } |
---|
429 | |
---|
430 | // Move file to trash. |
---|
431 | if (file_exists($file_path_name)) { |
---|
432 | rename($file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); |
---|
433 | printf("Moved: %s -> %s\n", $file_path_name, sprintf('%s/%s_%s', $user_trash_folder, $file_prefix, basename($file_path_name))); |
---|
434 | } else { |
---|
435 | printf("File not found: %s\n", $file_path_name); |
---|
436 | } |
---|
437 | } |
---|
438 | |
---|
439 | ?> |
---|