| | 404 | } |
| | 405 | |
| | 406 | function generate_mailer($name, $views = "") { |
| | 407 | if(stristr($name, "_")) { |
| | 408 | $model_file = $this->model_path."/".strtolower($name).".php"; |
| | 409 | } else { |
| | 410 | $model_file = $this->model_path."/".Inflector::underscore($name).".php"; |
| | 411 | } |
| | 412 | |
| | 413 | $model_class = Inflector::camelize($name); |
| | 414 | |
| | 415 | if(!file_exists($model_file)) { |
| | 416 | if(file_exists($this->mailer_model_template_file)) { |
| | 417 | $template = file_get_contents($this->mailer_model_template_file); |
| | 418 | $template = str_replace('[class_name]',$model_class,$template); |
| | 419 | # Add view methods |
| | 420 | if (!empty($views)) { |
| | 421 | # There are some views, add a method for each |
| | 422 | if(is_array($views)) { |
| | 423 | # Multiple views in an array |
| | 424 | foreach($views as $view) { |
| | 425 | $method = "\tfunction $view() {\n"; |
| | 426 | $method .= "\t\t\$this->subject = '".$model_class."->".$view."';\n"; |
| | 427 | $method .= "\t\t\$this->recipients = '';\n"; |
| | 428 | $method .= "\t\t\$this->from = '';\n"; |
| | 429 | $method .= "\t\t\$this->headers = array();\n"; |
| | 430 | $method .= "\t\t\$this->body = array();\n"; |
| | 431 | $method .= "\t}"; |
| | 432 | $class_methods[] = $method; |
| | 433 | } |
| | 434 | $class_methods = implode("\n\n",$class_methods); |
| | 435 | } else { |
| | 436 | $class_methods = "\tfunction $views() {\n"; |
| | 437 | $class_methods .= "\t\t\$this->subject = '".$model_class."->".$views."';\n"; |
| | 438 | $class_methods .= "\t\t\$this->recipients = '';\n"; |
| | 439 | $class_methods .= "\t\t\$this->from = '';\n"; |
| | 440 | $class_methods .= "\t\t\$this->headers = array();\n"; |
| | 441 | $class_methods .= "\t\t\$this->body = array();\n"; |
| | 442 | $class_methods .= "\t}"; |
| | 443 | } |
| | 444 | $template = str_replace('[class_methods]', $class_methods, $template); |
| | 445 | } else { |
| | 446 | # No view methods to add, so remove unneeded template |
| | 447 | $template = str_replace('[class_methods]', '', $template); |
| | 448 | } |
| | 449 | # Write the mailer model to disk |
| | 450 | if(!file_put_contents($model_file, $template)) { |
| | 451 | echo "error creating mailer model file: $model_file\n"; |
| | 452 | } else { |
| | 453 | echo "create $model_file\n"; |
| | 454 | } |
| | 455 | } else { |
| | 456 | echo "error mailer model template file doesn't exist: $this->mailer_model_template_file\n"; |
| | 457 | } |
| | 458 | } else { |
| | 459 | echo "exists $model_file\n"; |
| | 460 | } |
| | 461 | |
| | 462 | # Now create the view files |
| | 463 | $name = Inflector::underscore($name); |
| | 464 | $this->view_path .= "/$name"; |
| | 465 | $this->view_template_file = $this->mailer_view_template_file; |
| | 466 | $this->controller_class = $model_class; |
| | 467 | |
| | 468 | # Create the extra folders for View / Controller |
| | 469 | if(file_exists($this->view_path)) { |
| | 470 | echo "exists $this->view_path\n"; |
| | 471 | } else{ |
| | 472 | $this->exec("$this->mkdir_cmd $this->view_path"); |
| | 473 | echo "create $this->view_path\n"; |
| | 474 | } |
| | 475 | |
| | 476 | # Create view files if any |
| | 477 | if(is_array($views)) { |
| | 478 | foreach($views as $view) { |
| | 479 | $this->create_view($view, $name); |
| | 480 | } |
| | 481 | } elseif(!empty($views)) { |
| | 482 | $this->create_view($views, $name); |
| | 483 | } |
| | 484 | |
| | 485 | |
| | 939 | } |
| | 940 | |
| | 941 | /** |
| | 942 | * Output console help message for "generate mailer" |
| | 943 | */ |
| | 944 | function mailer_help() { |
| | 945 | echo "Usage: php script/generate.php mailer MailerName [view1 view2 ...]\n\n"; |
| | 946 | echo "Description:\n"; |
| | 947 | echo "\tThe mailer generator creates class methods for a new mailer and its views.\n\n"; |
| | 948 | echo "\tThe generator takes a mailer name and a list of views as arguments.\n"; |
| | 949 | echo "\tThe mailer name may be given in CamelCase or under_score.\n\n"; |
| | 950 | echo "\tThe generator creates a mailer class in app/models with view templates\n"; |
| | 951 | echo "\tin app/views/mailer_name.\n\n"; |
| | 952 | echo "Example:\n"; |
| | 953 | echo "\tphp script/generate.php mailer Notifications signup forgot_password invoice\n\n"; |
| | 954 | echo "\tThis will create a Notifications mailer class:\n"; |
| | 955 | echo "\t\tMailer: app/models/notifications.php\n"; |
| | 956 | echo "\t\tViews: app/views/notifications/signup.phtml [...]\n\n"; |