diff --git a/Mtool/Codegen/Entity/Abstract.php b/Mtool/Codegen/Entity/Abstract.php index f699406..bceca67 100644 --- a/Mtool/Codegen/Entity/Abstract.php +++ b/Mtool/Codegen/Entity/Abstract.php @@ -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()) { diff --git a/Mtool/Codegen/Entity/Controller.php b/Mtool/Codegen/Entity/Controller.php index 607858f..96f5f67 100644 --- a/Mtool/Codegen/Entity/Controller.php +++ b/Mtool/Codegen/Entity/Controller.php @@ -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')); diff --git a/Mtool/Codegen/Entity/Crud.php b/Mtool/Codegen/Entity/Crud.php index 6d1264e..5a3fa41 100644 --- a/Mtool/Codegen/Entity/Crud.php +++ b/Mtool/Codegen/Entity/Crud.php @@ -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 @@ -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"; diff --git a/Mtool/Codegen/Template/Crud/block_form.tpl b/Mtool/Codegen/Template/Crud/block_form.tpl new file mode 100644 index 0000000..07ae424 --- /dev/null +++ b/Mtool/Codegen/Template/Crud/block_form.tpl @@ -0,0 +1,65 @@ + '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(); + } +} diff --git a/Mtool/Codegen/Template/Crud/block_form_container.tpl b/Mtool/Codegen/Template/Crud/block_form_container.tpl new file mode 100644 index 0000000..94deaef --- /dev/null +++ b/Mtool/Codegen/Template/Crud/block_form_container.tpl @@ -0,0 +1,61 @@ +_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'); + } + } +} diff --git a/Mtool/Codegen/Template/Crud/block_grid.tpl b/Mtool/Codegen/Template/Crud/block_grid.tpl index ff7747f..196af3a 100644 --- a/Mtool/Codegen/Template/Crud/block_grid.tpl +++ b/Mtool/Codegen/Template/Crud/block_grid.tpl @@ -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'), diff --git a/Mtool/Codegen/Template/Crud/controller.tpl b/Mtool/Codegen/Template/Crud/controller.tpl new file mode 100644 index 0000000..0d4ac7c --- /dev/null +++ b/Mtool/Codegen/Template/Crud/controller.tpl @@ -0,0 +1,231 @@ +loadLayout() + //->_setActiveMenu('custom_modules/scheduled_content') + ->_addBreadcrumb($this->__('#{header_text}'), $this->__('#{header_text}')); + $this->_title($this->__('#{header_text}')); + + return $this; + } + + /** + * Show grid + * + * @return void + */ + public function indexAction() + { + $this->_initAction() + ->renderLayout(); + } + + /** + * Edit item action + * + * @return void + */ + public function editAction() + { + $modelId = intval($this->getRequest()->getParam('id', 0)); + $error = false; + if ($modelId) { + $model = Mage::getModel($this->_model)->load($modelId); + if ($model->getId()) { + $data = Mage::getSingleton('adminhtml/session')->getFormData(true); + if ($data) { + $model->setData($data)->setId($modelId); + } + Mage::register('current_#{model_ns}_#{model_path}', $model); + } else { + $this->_getSession()->addError($this->__('Item doesn\'t exist')); + $error = true; + } + } + + if ($error) { + $this->_redirectError($this->_getRefererUrl()); + } else { + $this->_initAction(); + $this->getLayout()->getBlock('head')->setCanLoadExtJs(true); + $this->renderLayout(); + } + } + + /** + * New item action + * + * @return void + */ + public function newAction() + { + $this->_forward('edit'); + } + + /** + * save menu item action + * + * @return void + */ + public function saveAction() + { + $error = false; + + if ($data = $this->getRequest()->getPost()) { + $model = Mage::getModel($this->_model); + + $modelId = intval($this->getRequest()->getParam('id', 0)); + if ($modelId) { + $model->load($modelId); + } + + $this->_getSession()->setFormData($data); + + try { + $model->setData($data); + + if ($modelId) { + $model->setId($modelId); + } + + $model->save(); + + if (!$model->getId()) { + Mage::throwException($this->__('Error saving item')); + } + + $this->_getSession()->addSuccess($this->__('Item was successfully saved.')); + $this->_getSession()->setFormData(false); + + } catch (Mage_Core_Exception $e) { + $this->_getSession()->addError($e->getMessage()); + $error = true; + } catch (Exception $e) { + $this->_getSession()->addError($this->__('Error while saving item')); + Mage::logException($e); + $error = true; + } + } else { + $this->_getSession()->addError($this->__('No data found to save')); + } + + if (!$error && isset($model) && $model->getId()) { + // The following line decides if it is a "save" or "save and continue" + if ($this->getRequest()->getParam('back')) { + $this->_redirect('*/*/edit', array('id' => $model->getId())); + } else { + $this->_redirect('*/*/'); + } + } else { + $this->_redirectReferer(); + } + } + + /** + * Delete item action + * + * @return mixed + */ + public function deleteAction() + { + if ($modelId = $this->getRequest()->getParam('id')) { + try { + $model = Mage::getModel($this->_model); + $model->setId($modelId); + $model->delete(); + $this->_getSession()->addSuccess($this->__('Item has been deleted.')); + $this->_redirect('*/*/'); + + return; + } + catch (Exception $e) { + $this->_getSession()->addError($e->getMessage()); + $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); + + return; + } + } + $this->_getSession()->addError($this->__('Unable to find the item to delete.')); + $this->_redirect('*/*/'); + } + + /** + * Load grid for ajax action + * + * @return void + */ + public function gridAction() + { + $this->loadLayout() + ->renderLayout(); + } + + /** + * Mass delete items action + * + * @return void + */ + public function massDeleteAction() + { + $modelIds = $this->getRequest()->getParam('item'); + if (!is_array($modelIds)) { + $this->_getSession()->addError($this->__('Please select item(s).')); + } else { + try { + foreach ($modelIds as $modelId) { + Mage::getSingleton($this->_model) + ->load($modelId) + ->delete(); + } + $this->_getSession()->addSuccess( + $this->__('Total of %d record(s) were deleted.', count($modelIds)) + ); + } catch (Exception $e) { + $this->_getSession()->addError($e->getMessage()); + } + } + + $this->_redirect('*/*/'); + } +} diff --git a/Mtool/Codegen/Template/EntityTable/model_blank.tpl b/Mtool/Codegen/Template/EntityTable/model_blank.tpl index e935d57..add48f9 100644 --- a/Mtool/Codegen/Template/EntityTable/model_blank.tpl +++ b/Mtool/Codegen/Template/EntityTable/model_blank.tpl @@ -42,5 +42,5 @@ class #{class_name} extends Mage_Core_Model_Abstract { parent::_construct(); $this->_init('#{namespace}/#{model_path}'); -} + } } diff --git a/Mtool/Providers/Crud.php b/Mtool/Providers/Crud.php index 8c13aa8..1cc0f2f 100644 --- a/Mtool/Providers/Crud.php +++ b/Mtool/Providers/Crud.php @@ -42,7 +42,41 @@ public function getName() */ public function createGridBlock($targetModule = null, $blockPath = null, $modelPath = null) { - $this->_createGridBlock($targetModule, $blockPath, $modelPath); + $this->_createData('createGridBlock', $targetModule, $blockPath, $modelPath); + } + + /** + * Create form block + * + * @param string $targetModule in format of companyname/modulename + * @param string $blockPath in format of mymodule/block_path + * @param string $modelPath in format of mymodule/model_path + */ + public function createFormBlock($targetModule = null, $blockPath = null, $modelPath = null) + { + $this->_createData('createFormBlock', $targetModule, $blockPath, $modelPath); + } + + /** + * Create form block + * + * @param string $targetModule in format of companyname/modulename + * @param string $controllerPath in format of mymodule/block_path + * @param string $modelPath in format of mymodule/model_path + */ + public function createController($targetModule = null, $controllerPath = null, $modelPath = null) + { + if ($targetModule == null) { + $targetModule = $this->_ask('Enter the target module (in format of Mycompany/Mymodule)'); + } + if ($controllerPath == null) { + $entityPath = $this->_ask("Enter the controller path (in format of frontName/controller_path)"); + } + if ($modelPath == null) { + $entityPath = $this->_ask("Enter the model path (in format of mymodule/model_path)"); + } + + $this->_createData('createController', $targetModule, $controllerPath, $modelPath); } /** @@ -52,7 +86,7 @@ public function createGridBlock($targetModule = null, $blockPath = null, $modelP * @param string $blockPath in format of mymodule/block_path * @param string $modelPath in format of mymodule/model_path */ - protected function _createGridBlock($targetModule = null, $blockPath = null, $modelPath = null) + protected function _createData($action, $targetModule = null, $blockPath = null, $modelPath = null) { $entity = new Mtool_Codegen_Entity_Crud(); @@ -74,7 +108,7 @@ protected function _createGridBlock($targetModule = null, $blockPath = null, $mo list($modelNS, $modelName) = explode('/', $modelPath); - $entity->createGridBlock($module, $blockNS, $blockName, $modelNS, $modelName); + $entity->$action($module, $blockNS, $blockName, $modelNS, $modelName); $this->_answer('Done'); }