| 162 | | } |
| 163 | | } |
| | 182 | return true; |
| | 183 | } |
| | 184 | return false; |
| | 185 | } |
| | 186 | |
| | 187 | function generate_scaffold($model_name, $controller_name, $views="") { |
| | 188 | if(!$model_exists = $this->generate_model($model_name)) { |
| | 189 | echo "Error - Can't create Model: $model_name.\n"; |
| | 190 | exit; |
| | 191 | } |
| | 192 | |
| | 193 | $GLOBALS['current_controller_object'] =& $this; |
| | 194 | $model_class_name = Inflector::classify($model_name); |
| | 195 | $singluar_model_name = Inflector::singularize($model_name); |
| | 196 | $plural_model_name = Inflector::pluralize($model_name); |
| | 197 | $human_model_name = Inflector::humanize($model_name); |
| | 198 | $this->{$singluar_model_name} = new $model_class_name(); |
| | 199 | if(!$controller_name) { |
| | 200 | $controller_name = Inflector::pluralize($model_name); |
| | 201 | } else { |
| | 202 | $controller_name = Inflector::underscore($controller_name); |
| | 203 | } |
| | 204 | $controller_file = "$this->controller_path/".$controller_name."_controller.php"; |
| | 205 | $non_scaffolded_actions = array(); |
| | 206 | $illegal_views = array("index","add","edit","show"); |
| | 207 | if(is_array($views)) { |
| | 208 | foreach($views as $view) { |
| | 209 | if(!in_array($view, $illegal_views)) { |
| | 210 | $non_scaffolded_actions[] = $view; |
| | 211 | } |
| | 212 | } |
| | 213 | } |
| | 214 | $this->generate_controller($controller_name, $non_scaffolded_actions, true); |
| | 215 | if(stristr($controller_name, "/")) { |
| | 216 | $controller_class_name = Inflector::classify(substr($controller_name,strrpos($controller_name, "/")+1)); |
| | 217 | $human_controller_name = Inflector::humanize(substr($controller_name,strrpos($controller_name, "/")+1)); |
| | 218 | } else { |
| | 219 | $controller_class_name = Inflector::classify($controller_name); |
| | 220 | $human_controller_name = Inflector::humanize($controller_name); |
| | 221 | } |
| | 222 | |
| | 223 | # Generate the controller |
| | 224 | ob_start(); |
| | 225 | include("$this->scaffold_template_path/controller.php"); |
| | 226 | $controller_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 227 | ob_end_clean(); |
| | 228 | if(!file_exists($controller_file)) { |
| | 229 | if(!file_put_contents($controller_file, $controller_contents)) { |
| | 230 | echo "error creating controller class file: $controller_file\n"; |
| | 231 | } else { |
| | 232 | echo "created $controller_file\n"; |
| | 233 | } |
| | 234 | } else { |
| | 235 | echo "exists $controller_file\n"; |
| | 236 | } |
| | 237 | |
| | 238 | # Generate the index.phtml view |
| | 239 | $view_file = "$this->view_path/index.".$this->view_file_extention; |
| | 240 | ob_start(); |
| | 241 | include("$this->scaffold_template_path/view_index.phtml"); |
| | 242 | $index_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 243 | ob_end_clean(); |
| | 244 | if(!file_exists($view_file)) { |
| | 245 | if(!file_put_contents($view_file, $index_contents)) { |
| | 246 | echo "error creating view file: $view_file\n"; |
| | 247 | } else { |
| | 248 | echo "created $view_file\n"; |
| | 249 | } |
| | 250 | } else { |
| | 251 | echo "exists $view_file\n"; |
| | 252 | } |
| | 253 | |
| | 254 | # Generate the add.phtml view |
| | 255 | $view_file = "$this->view_path/add.".$this->view_file_extention; |
| | 256 | ob_start(); |
| | 257 | include("$this->scaffold_template_path/view_add.phtml"); |
| | 258 | $add_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 259 | ob_end_clean(); |
| | 260 | if(!file_exists($view_file)) { |
| | 261 | if(!file_put_contents($view_file, $index_contents)) { |
| | 262 | echo "error creating view file: $view_file\n"; |
| | 263 | } else { |
| | 264 | echo "created $view_file\n"; |
| | 265 | } |
| | 266 | } else { |
| | 267 | echo "exists $view_file\n"; |
| | 268 | } |
| | 269 | |
| | 270 | # Generate the edit.phtml view |
| | 271 | $view_file = "$this->view_path/edit.".$this->view_file_extention; |
| | 272 | ob_start(); |
| | 273 | include("$this->scaffold_template_path/view_edit.phtml"); |
| | 274 | $edit_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 275 | ob_end_clean(); |
| | 276 | if(!file_exists($view_file)) { |
| | 277 | if(!file_put_contents($view_file, $index_contents)) { |
| | 278 | echo "error creating view file: $view_file\n"; |
| | 279 | } else { |
| | 280 | echo "created $view_file\n"; |
| | 281 | } |
| | 282 | } else { |
| | 283 | echo "exists $view_file\n"; |
| | 284 | } |
| | 285 | |
| | 286 | # Generate the show.phtml view |
| | 287 | $view_file = "$this->view_path/show.".$this->view_file_extention; |
| | 288 | ob_start(); |
| | 289 | include("$this->scaffold_template_path/view_show.phtml"); |
| | 290 | $show_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 291 | ob_end_clean(); |
| | 292 | if(!file_exists($view_file)) { |
| | 293 | if(!file_put_contents($view_file, $index_contents)) { |
| | 294 | echo "error creating view file: $view_file\n"; |
| | 295 | } else { |
| | 296 | echo "created $view_file\n"; |
| | 297 | } |
| | 298 | } else { |
| | 299 | echo "exists $view_file\n"; |
| | 300 | } |
| | 301 | |
| | 302 | # Generate the partial containing the form elments from the database |
| | 303 | $view_file = "$this->view_path/_form.".$this->view_file_extention; |
| | 304 | ob_start(); |
| | 305 | include("$this->scaffold_template_path/form_scaffolding.phtml"); |
| | 306 | $_form_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 307 | ob_end_clean(); |
| | 308 | if(!file_exists($view_file)) { |
| | 309 | if(!file_put_contents($view_file, $index_contents)) { |
| | 310 | echo "error creating view file: $view_file\n"; |
| | 311 | } else { |
| | 312 | echo "created $view_file\n"; |
| | 313 | } |
| | 314 | } else { |
| | 315 | echo "exists $view_file\n"; |
| | 316 | } |
| | 317 | |
| | 318 | # Generate the layout for the scaffolding |
| | 319 | $layout_file = "$this->layouts_path/$controller_name.".$this->view_file_extention; |
| | 320 | ob_start(); |
| | 321 | include("$this->scaffold_template_path/layout.phtml"); |
| | 322 | $layout_contents = $this->fix_php_brackets(ob_get_contents()); |
| | 323 | ob_end_clean(); |
| | 324 | if(!file_exists($layout_file)) { |
| | 325 | if(!file_put_contents($layout_file, $index_contents)) { |
| | 326 | echo "error creating layout file: $view_file\n"; |
| | 327 | } else { |
| | 328 | echo "created $view_file\n"; |
| | 329 | } |
| | 330 | } else { |
| | 331 | echo "exists $layout_file\n"; |
| | 332 | } |
| | 333 | } |
| | 456 | |
| | 457 | function scaffold_help() { |
| | 458 | echo "Usage: ./generate scaffold ModelName [ControllerName] [view1 view2 ...]\n\n"; |
| | 459 | echo "Description:\n"; |
| | 460 | echo "\tThe scaffold generator creates a controller to interact with a model.\n"; |
| | 461 | echo "\tIf the model does not exist, it creates the model as well. The generated\n"; |
| | 462 | echo "\tcode is equivalent to the ( public \$scaffold = \"model\"; ) declaration,\n"; |
| | 463 | echo "\tmaking it easy to migrate when you wish to customize your controller and views.\n\n"; |
| | 464 | echo "\tThe generator takes a model name, an optional controller name, and a\n"; |
| | 465 | echo "\tlist of views as arguments. Scaffolded actions and views are created\n"; |
| | 466 | echo "\tautomatically.\n\n"; |
| | 467 | echo "\tThe auto scaffolded actions and views are:\n"; |
| | 468 | echo "\t\tindex, show, add, edit, delete\n\n"; |
| | 469 | echo "\tIf a controller name is not given, the plural form of the model name\n"; |
| | 470 | echo "\twill be used. The model and controller names may be given in CamelCase\n"; |
| | 471 | echo "\tor under_score and should not be suffixed with 'Model' or 'Controller'.\n\n"; |
| | 472 | echo "Example:\n"; |
| | 473 | echo "\t./generate scaffold Account Bank debit credit\n\n"; |
| | 474 | echo "\tThis will generate an Account model and BankController with a basic user interface\n"; |
| | 475 | echo "\tNow create the accounts table in your database and browse to http://localhost/bank/\n"; |
| | 476 | echo "\tvoila, you're on Trax!\n\n"; |
| | 477 | echo "Module/Folders Example:\n"; |
| | 478 | echo "\t./generate scaffold CreditCard 'admin/credit_card' suspend late_fee\n\n"; |
| | 479 | echo "\tThis will generate a CreditCard model and CreditCardController controller\n"; |
| | 480 | echo "\tin the admin module.\n"; |
| | 481 | } |