Skip to content

Commit

Permalink
add crud
Browse files Browse the repository at this point in the history
  • Loading branch information
hws47a committed Nov 27, 2012
1 parent a1e1eea commit aa6b8b8
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Mtool/Codegen/Entity/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function lookupOriginEntityClass($namespace, $configs, $field = 'class')
* @param string $template
* @param Mtool_Codegen_Entity_Module $module
* @param array $params
* @return resulting class name
* @return string resulting class name
*/
public function createClass($path, $template, $module, $params = array())
{
Expand Down
7 changes: 5 additions & 2 deletions Mtool/Codegen/Entity/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ class Mtool_Codegen_Entity_Controller extends Mtool_Codegen_Entity_Abstract
* @param string $path
* @param Mtool_Codegen_Entity_Module $module
*/
public function create($namespace, $path, Mtool_Codegen_Entity_Module $module)
public function create($namespace, $path, Mtool_Codegen_Entity_Module $module, $template = null, $param = array())
{
if (!$template) {
$template = $this->_createTemplate;
}
// Create class file
$resultingClassName = $this->createClass($namespace, $path, $this->_createTemplate, $module);
$resultingClassName = $this->createClass($namespace, $path, $template, $module, $param);

// Create router in config if not exist
$config = new Mtool_Codegen_Config($module->getConfigPath('config.xml'));
Expand Down
85 changes: 85 additions & 0 deletions Mtool/Codegen/Entity/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,30 @@ class Mtool_Codegen_Entity_Crud extends Mtool_Codegen_Entity_Abstract
*/
protected $_gridContainerTemplate = 'block_grid_container';

/**
* Form container template name
* @var string
*/
protected $_formContainerTemplate = 'block_form_container';

/**
* Grid template name
* @var string
*/
protected $_gridTemplate = 'block_grid';

/**
* Form template name
* @var string
*/
protected $_formTemplate = 'block_form';

/**
* Controller template name
* @var string
*/
protected $_controllerTemplate = 'controller';

/**
* Block Entity name
* @var string
Expand Down Expand Up @@ -111,6 +129,73 @@ public function createGridBlock(Mtool_Codegen_Entity_Module $module, $blockNS, $
$params
);

$this->_createNamespaces($module, $blockNS, $modelNS);
}

/**
* Create new entity
*
* @param string $namespace
* @param string $path
* @param Mtool_Codegen_Entity_Module $module
*/
public function createFormBlock(Mtool_Codegen_Entity_Module $module, $blockNS, $blockName, $modelNS, $modelName)
{
$params = array(
'block_ns' => $blockNS,
'block_path' => $blockName,
'model_ns' => $modelNS,
'model_path' => $modelName,
);

// Create form container class file
$this->createClass(
$blockName . '_edit',
$this->_entityTemplateDir . DIRECTORY_SEPARATOR . $this->_formContainerTemplate,
$module,
$params
);

// Create form class file
$this->createClass(
$blockName . '_edit_form',
$this->_entityTemplateDir . DIRECTORY_SEPARATOR . $this->_formTemplate,
$module,
$params
);

$this->_createNamespaces($module, $blockNS, $modelNS);
}

/**
* Create new entity
*
* @param string $namespace
* @param string $path
* @param Mtool_Codegen_Entity_Module $module
*/
public function createController(Mtool_Codegen_Entity_Module $module, $controllerNS, $controllerName, $modelNS, $modelName)
{
$controllerEntity = new Mtool_Codegen_Entity_Controller();

$params = array(
'model_ns' => $modelNS,
'model_path' => $modelName,
'header_text' => $module->getModuleName() . ' ' . str_replace('_', ' ', ucfirst($modelName))
);

// Create controller class file
$controllerEntity->create(
$controllerNS,
$controllerName,
$module,
$this->_entityTemplateDir . DIRECTORY_SEPARATOR . $this->_controllerTemplate,
$params
);
}

protected function _createNamespaces(Mtool_Codegen_Entity_Module $module, $blockNS, $modelNS)
{
// Create block namespace in config if not exist
$config = new Mtool_Codegen_Config($module->getConfigPath('config.xml'));
$configPath = "global/{$this->_blockConfigNamespace}/{$blockNS}/class";
Expand Down
65 changes: 65 additions & 0 deletions Mtool/Codegen/Template/Crud/block_form.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* #{module_owner} extension for Magento
*
* Long description of this file (if any...)
*
* NOTICE OF LICENSE
*
#{license}
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade
* the #{company_name} #{module_name} module to newer versions in the future.
* If you wish to customize the #{company_name} #{module_name} module for your needs
* please refer to http://www.magentocommerce.com for more information.
*
* @category #{company_name}
* @package #{company_name}_#{module_name}
* @copyright Copyright (C) #{year} #{copyright_company}
* @license #{license_short}
*/

/**
* Short description of the class
*
* Long description of the class (if any...)
*
* @category #{company_name}
* @package #{company_name}_#{module_name}
* @subpackage Block
* @author #{author}
*/
class #{class_name} extends Mage_Adminhtml_Block_Widget_Form
{
/**
* Prepare the form
*
* @return Mage_Adminhtml_Block_Widget_Form|void
*/
protected function _prepareForm()
{
//add form
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id', null))),
'method' => 'post'
));
$form->setUseContainer(true);
$this->setForm($form);
//add fieldset
$fieldSet = $form->addFieldset(
'main_field_set',
array('legend' => $this->__('Main Content'))
);
$data = Mage::registry('current_#{model_ns}_#{model_path}');
if ($data) {
$form->setValues($data->getData());
}

parent::_prepareForm();
}
}
61 changes: 61 additions & 0 deletions Mtool/Codegen/Template/Crud/block_form_container.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* #{module_owner} extension for Magento
*
* Long description of this file (if any...)
*
* NOTICE OF LICENSE
*
#{license}
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade
* the #{company_name} #{module_name} module to newer versions in the future.
* If you wish to customize the #{company_name} #{module_name} module for your needs
* please refer to http://www.magentocommerce.com for more information.
*
* @category #{company_name}
* @package #{company_name}_#{module_name}
* @copyright Copyright (C) #{year} #{copyright_company}
* @license #{license_short}
*/

/**
* Short description of the class
*
* Long description of the class (if any...)
*
* @category #{company_name}
* @package #{company_name}_#{module_name}
* @subpackage Block
* @author #{author}
*/
class #{class_name} extends Mage_Adminhtml_Block_Widget_Form_Container
{
/**
* Init grid container
*/
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_controller = '#{block_path}';
$this->_blockGroup = '#{block_ns}';
}

/**
* Get container header text
*
* @return string
*/
public function getHeaderText()
{
$data = Mage::registry('current_#{model_ns}_#{model_path}');
if ($data) {
return $this->__('Edit Item #%d', $this->escapeHtml($data->getId()));
} else {
return $this->__('New Item');
}
}
}
2 changes: 1 addition & 1 deletion Mtool/Codegen/Template/Crud/block_grid.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class #{class_name} extends Mage_Adminhtml_Block_Widget_Grid
protected function _prepareMassaction()
{
$this->setMassactionIdField($this->getIdFieldName());
$this->getMassactionBlock()->setFormFieldName('menu');
$this->getMassactionBlock()->setFormFieldName('item');
$this->getMassactionBlock()->addItem('delete', array(
'label' => $this->__('Delete'),
Expand Down
Loading

0 comments on commit aa6b8b8

Please sign in to comment.