Fixing issues with the scaffold generator (custom actions did not work).
[akelos.git] / lib / utils / generators / scaffold / scaffold_generator.php
blob2e81ae0f0dde7c01439f06c96eddf6eb6a6ca8be
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 /**
12 * @package ActiveSupport
13 * @subpackage Generators
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
18 ak_define('ACTIVE_RECORD_VALIDATE_TABLE_NAMES', false);
20 class ScaffoldGenerator extends AkelosGenerator
22 var $command_values = array('model_name','controller_name','(array)actions');
24 function cast()
26 $this->model_name = AkInflector::camelize($this->model_name);
27 $this->model_file_path = AkInflector::toModelFilename($this->model_name);
28 $this->controller_name = empty($this->controller_name) ? $this->model_name : (AkInflector::camelize($this->controller_name));
29 $this->controller_file_path = AkInflector::toControllerFilename($this->controller_name);
30 $this->controller_class_name = str_replace(array('/','::'),'_', $this->controller_name.'Controller');
31 $this->controller_name = AkInflector::demodulize($this->controller_name);
32 $this->controller_human_name = AkInflector::humanize($this->controller_name);
33 $this->helper_var_name = '$'.AkInflector::underscore($this->controller_name).'_helper';
35 $this->singular_name = AkInflector::underscore($this->model_name);
36 $this->plural_name = AkInflector::pluralize($this->singular_name);
37 $this->singular_controller_name = AkInflector::underscore($this->controller_name);
39 $this->module_preffix = AkInflector::underscore(substr($this->controller_class_name, 0, strrpos($this->controller_class_name, '_')));
40 $this->module_preffix = empty($this->module_preffix) ? '' : DS.$this->module_preffix;
43 $this->files = array(
44 'controller.php' => $this->controller_file_path,
45 /**
46 * @todo Implement generic functional tests
48 // 'functional_test.php' => AK_TEST_DIR.DS.'functional'.DS.'test_'.$this->controller_class_name.'.php',
49 'helper.php' => AK_HELPERS_DIR.$this->module_preffix.DS.trim($this->helper_var_name,'$').'.php',
50 'layout' => AK_VIEWS_DIR.DS.'layouts'.DS.$this->singular_controller_name.'.tpl',
51 'view_add' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'add.tpl',
52 'view_destroy' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'destroy.tpl',
53 'view_edit' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'edit.tpl',
54 'view_listing' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'listing.tpl',
55 'view_show' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'show.tpl',
56 'form' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'_form.tpl',
59 $this->user_actions = array();
60 foreach ((array)@$this->actions as $action){
61 $this->user_actions[$action] = AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.$action.'.tpl';
66 function hasCollisions()
68 $this->collisions = array();
69 foreach (array_merge(array_values($this->files),array_values($this->user_actions)) as $file_name){
70 if(file_exists($file_name)){
71 $this->collisions[] = Ak::t('%file_name file already exists',array('%file_name'=>$file_name));
74 return count($this->collisions) > 0;
77 function generate()
79 //Generate models if they don't exist
80 $model_files = array(
81 'model'=>$this->model_file_path,
82 'installer'=>AK_APP_DIR.DS.'installers'.DS.$this->singular_name.'_installer.php',
83 'model_unit_test'=>AK_TEST_DIR.DS.'unit'.DS.'app'.DS.'models'.DS.$this->singular_name.'.php',
84 'model_fixture'=> AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'models'.DS.$this->singular_name.'.php',
85 'installer_fixture'=>AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'installers'.DS.$this->singular_name.'_installer.php'
88 $this->_template_vars = (array)$this;
89 foreach ($model_files as $template=>$file_path){
90 if(!file_exists($file_path)){
91 $this->save($file_path, $this->render($template, !empty($this->sintags)));
95 // We check for common testing files
96 $common_testing_files = array(
97 'fixtures'.DS.'config'.DS.'config.php',
98 'fixtures'.DS.'app'.DS.'shared_model.php'
100 foreach ($common_testing_files as $common_testing_file){
101 if(!file_exists(AK_TEST_DIR.DS.$common_testing_file)){
102 $this->save(AK_TEST_DIR.DS.$common_testing_file,
103 file_get_contents(AK_FRAMEWORK_DIR.DS.'test'.DS.$common_testing_file));
107 if(file_exists($this->model_file_path)){
108 require_once(AK_APP_DIR.DS.'shared_model.php');
109 require_once($this->model_file_path);
110 if(class_exists($this->model_name)){
111 $ModelInstance =& new $this->model_name;
112 $table_name = $ModelInstance->getTableName();
113 if(!empty($table_name)){
114 $this->content_columns = $ModelInstance->getContentColumns();
115 unset(
116 $this->content_columns['updated_at'],
117 $this->content_columns['updated_on'],
118 $this->content_columns['created_at'],
119 $this->content_columns['created_on']
122 $internationalized_columns = $ModelInstance->getInternationalizedColumns();
123 foreach ($internationalized_columns as $column_name=>$languages){
124 foreach ($languages as $lang){
125 $this->content_columns[$column_name] = $this->content_columns[$lang.'_'.$column_name];
126 $this->content_columns[$column_name]['name'] = $column_name;
127 unset($this->content_columns[$lang.'_'.$column_name]);
135 $this->_template_vars = (array)$this;
136 foreach ($this->files as $template=>$file_path){
137 $this->save($file_path, $this->render($template, !empty($this->sintags)));
139 foreach ($this->user_actions as $action=>$file_path){
140 $this->assignVarToTemplate('action',$action);
141 $this->save($file_path, $this->render('view', !empty($this->sintags)));
144 $unit_test_runner = AK_TEST_DIR.DS.'unit.php';
145 if(!file_exists($unit_test_runner)){
146 Ak::file_put_contents($unit_test_runner, file_get_contents(AK_FRAMEWORK_DIR.DS.'test'.DS.'app.php'));