00001 <?php
00013 $genpath = dirname(__FILE__);
00014 if(!file_exists('profiles/cmdline.php')) {
00015 die("Run this script from your top-level app directory (eg, /var/www/html/app)\n");
00016 }
00017 require_once('profiles/cmdline.php');
00018
00019 $db = Registry::get('pronto:db:main');
00020
00021 if($_SERVER['argc'] < 3) {
00022 echo "usage: php generate.php <entity> <db_table> [basename] [module]\n\n";
00023 echo "If set, [basename] will be the basename for all files generated.\n";
00024 echo "For example, if basename is 'foo', then the generator will create:\n";
00025 echo " * pages/foo.php\n";
00026 echo " * models/foo.php\n";
00027 echo " * templates/foo/create.php\n";
00028 echo " * templates/foo/list.php\n\n";
00029 echo "If not specified, [basename] will default to <entity>\n\n";
00030 echo "If [module] is set, then the generated files will be placed in\n";
00031 echo "modules/<module>/ instead of your top-level app directory.\n\n";
00032 exit(0);
00033 }
00034
00035 $entity_name = $_SERVER['argv'][1];
00036 $human_name = str_replace(' ','_',ucwords(str_replace('_',' ',$entity_name)));
00037 $db_table = $_SERVER['argv'][2];
00038 $fnbase = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : $entity_name;
00039 $module = isset($_SERVER['argv'][4]) ? $_SERVER['argv'][4] : '';
00040 $db_columns = array();
00041 $basepath = $module ? DIR_FS_APP.DS.'modules'.DS.$module : DIR_FS_APP;
00042
00043 echo "Entity: $entity_name\n";
00044 echo "DB Table: $db_table\n\n";
00045
00046 echo "Examining DB table for data introspection...\n";
00047 $has_id = false;
00048 foreach($db->get_all("EXPLAIN $db_table") as $r) {
00049 if($r['Field'] == 'id') {
00050 $has_id = true;
00051 continue;
00052 }
00053 if(preg_match('|_id$|', $r['Field'])) {
00054 echo "Warning: Ignoring {$r['Field']} - handle foreign keys manually\n";
00055 continue;
00056 }
00057 $col = array('name' => $r['Field']);
00058
00059 $type = array_shift(explode('(', $r['Type']));
00060 switch($type) {
00061 case 'date':
00062 case 'datetime':
00063 $col['type'] = 'date';
00064 break;
00065 case 'enum':
00066 preg_match('/\((.*)\)$/', $r['Type'], $m);
00067 $col['options'] = explode(',', $m[1]);
00068 $col['type'] = 'select';
00069 break;
00070 default:
00071 $col['type'] = 'text';
00072 }
00073 $db_columns[] = $col;
00074 }
00075 if(!$has_id) {
00076 echo "Warning: Table does not contain an 'id' field -- this is usually needed\n";
00077 }
00078 echo "\n";
00079
00080
00081
00082
00083 $longest = 0;
00084 foreach($db_columns as $col) {
00085 if(strlen($col['name']) > $longest) $longest = strlen($col['name']);
00086 }
00087
00088 $list_items = '';
00089 foreach($db_columns as $col) {
00090 $n = ucwords(str_replace('_',' ',$col['name']));
00091 $list_items .= "\t\t'{$col['name']}' ";
00092 for($i = 0; $i < $longest-strlen($col['name']); $i++) $list_items .= ' ';
00093 $list_items.= "=> array('label'=>__('$n'), 'type'=>'{$col['type']}'";
00094
00095 if(isset($col['options'])) {
00096 $list_items .= ", 'options'=>array(";
00097 foreach($col['options'] as $k) {
00098 $list_items .= "$k=>$k,";
00099 }
00100 $list_items = substr($list_items, 0, strlen($list_items)-1).')';
00101 }
00102 $list_items .= "),\n";
00103 }
00104
00105
00106
00107
00108 $create_items = '';
00109 foreach($db_columns as $col) {
00110 $n = ucwords(str_replace('_',' ',$col['name']));
00111 $create_items .= "\t\t\t'{$col['name']}' ";
00112 for($i = 0; $i < $longest-strlen($col['name']); $i++) $create_items .= ' ';
00113
00114 $create_items .= "=> array(";
00115 $create_items .= "'prompt'=>__('$n').':', ";
00116 $create_items .= "'type'=>'{$col['type']}'";
00117 if(isset($col['options'])) {
00118 $create_items .= ", 'options'=>array(";
00119 foreach($col['options'] as $k) {
00120 $create_items .= "$k=>$k,";
00121 }
00122 $create_items = substr($create_items, 0, strlen($create_items)-1).")";
00123 }
00124 $create_items .= "),\n";
00125 }
00126
00127 $keys = array('_ENTITY_', '_UENTITY_', '_DEFAULT_SORT_',
00128 '_DB_TABLE_','_LIST_ITEMS_','_CREATE_ITEMS_',
00129 '_MODULE_DESIGNATION_');
00130 $vals = array($entity_name, $human_name, $db_columns[0]['name'],
00131 $db_table, $list_items, $create_items,
00132 $module ? "\n\t\t".'$this->set_module('."'$module');\n" : '');
00133
00134
00135 function generate($template, $dest, $keys, $vals)
00136 {
00137 if(file_exists($dest)) {
00138 echo "$dest already exists. Overwrite? [Y/n] ";
00139 $fp = fopen('php://stdin', 'r');
00140 $answer = trim(fgets($fp));
00141 fclose($fp);
00142 if(strcasecmp($answer,'Y') && $answer) return false;
00143 }
00144 $tmpl = implode('', file($template));
00145 $tmpl = str_replace($keys, $vals, $tmpl);
00146 $fp = fopen($dest, "w") or exit(1);
00147 fputs($fp, $tmpl);
00148 fclose($fp);
00149 return true;
00150 }
00151
00152
00153 if(generate($genpath.'/templates/model.tpl.php', "$basepath/models/$fnbase.php", $keys, $vals)) {
00154 echo "New Model: $basepath/models/$fnbase.php\n";
00155 }
00156
00157 if(generate($genpath.'/templates/page.tpl.php', "$basepath/pages/$fnbase.php", $keys, $vals)) {
00158 echo "New Page Controller: $basepath/pages/$fnbase.php\n";
00159 }
00160
00161 @mkdir("$basepath/templates/$fnbase");
00162
00163 if(generate($genpath.'/templates/t_list.tpl.php', "$basepath/templates/$fnbase/list.php", $keys, $vals)) {
00164 echo "New Template: $basepath/templates/$fnbase/list.php\n";
00165 }
00166
00167 if(generate($genpath.'/templates/t_create.tpl.php', "$basepath/templates/$fnbase/create.php", $keys, $vals)) {
00168 echo "New Template: $basepath/templates/$fnbase/create.php\n";
00169 }
00170
00171 echo "\n";
00172 exit(0);
00173
00174 ?>