Namespace : Company
Module Name : Web
|–Web/
| |–Block/
| |–controllers/
| |–etc/
| |–Helper/
| |–sql/
|
Module Name : Web
Step 1: Declare your shell module and it’s code pool
Create an xml file /app/etc/modules/Company_Web.xml (You can use any
name, even you can use a single file to declare number of modules).true local
Step 2:
Create the basic directory structure under /app/code/core/local/ :-
Company/|–Web/
| |–Block/
| |–controllers/
| |–etc/
| |–Helper/
| |–sql/
|
Step 3:
Write the front controller in app\code\local\Company\Web\controllers\IndexController.php
loadLayout();
$this->renderLayout();
}
}
Step 4:
Write your backend module controller in app\code\local\Company\Web\controllers\Adminhtml\WebController.php
<loadLayout()
->_setActiveMenu('web/items')
->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
return $this;
}
public function indexAction() {
$this->_initAction()
->renderLayout();
}
public function editAction() {
$id = $this->getRequest()->getParam('id');
$model = Mage::getModel('web/web')->load($id);
if ($model->getId() || $id == 0) {
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
Mage::register('web_data', $model);
$this->loadLayout();
$this->_setActiveMenu('web/items');
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('web/adminhtml_web_edit'))
->_addLeft($this->getLayout()->createBlock('web/adminhtml_web_edit_tabs'));
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('web')->__('Item does not exist'));
$this->_redirect('*/*/');
}
}
public function newAction() {
$this->_forward('edit');
}
public function saveAction() {
if ($data = $this->getRequest()->getPost()) {
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('filename');
// Any extention would work
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);
// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS ;
$uploader->save($path, $_FILES['filename']['name'] );
} catch (Exception $e) {
}
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
}
$model = Mage::getModel('web/web');
$model->setData($data)
->setId($this->getRequest()->getParam('id'));
try {
if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
$model->setCreatedTime(now())
->setUpdateTime(now());
} else {
$model->setUpdateTime(now());
}
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('web')->__('Item was successfully saved'));
Mage::getSingleton('adminhtml/session')->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$this->_redirect('*/*/edit', array('id' => $model->getId()));
return;
}
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setFormData($data);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('web')->__('Unable to find item to save'));
$this->_redirect('*/*/');
}
public function deleteAction() {
if( $this->getRequest()->getParam('id') > 0 ) {
try {
$model = Mage::getModel('web/web');
$model->setId($this->getRequest()->getParam('id'))
->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
$this->_redirect('*/*/');
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
public function massDeleteAction() {
$webIds = $this->getRequest()->getParam('web');
if(!is_array($webIds)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
} else {
try {
foreach ($webIds as $webId) {
$web = Mage::getModel('web/web')->load($webId);
$web->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('adminhtml')->__(
'Total of %d record(s) were successfully deleted', count($webIds)
)
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massStatusAction()
{
$webIds = $this->getRequest()->getParam('web');
if(!is_array($webIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)'));
} else {
try {
foreach ($webIds as $webId) {
$web = Mage::getSingleton('web/web')
->load($webId)
->setStatus($this->getRequest()->getParam('status'))
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($webIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
{
$response = $this->getResponse();
$response->setHeader('HTTP/1.1 200 OK','');
$response->setHeader('Pragma', 'public', true);
$response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
$response->setHeader('Content-Disposition', 'attachment; filename='.$fileName);
$response->setHeader('Last-Modified', date('r'));
$response->setHeader('Accept-Ranges', 'bytes');
$response->setHeader('Content-Length', strlen($content));
$response->setHeader('Content-type', $contentType);
$response->setBody($content);
$response->sendResponse();
die;
}
}
Step 5:
Write the frontend block file in app\code\local\Company\Web\Block\Web.php
hasData('web')) {
$this->setData('web', Mage::registry('web'));
}
return $this->getData('web');
}
}
Step 6: Now write the following file- app\code\local\Company\Web\Block\Adminhtml\Web.php
_controller = 'adminhtml_web';
$this->_blockGroup = 'web';
$this->_headerText = Mage::helper('web')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('web')->__('Add Item');
parent::__construct();
}
}
Step 7: Now write the following file- app\code\local\Company\Web\Block\Adminhtml\Web\Grid.php
setId('webGrid');
$this->setDefaultSort('web_id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('web/web')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('web_id', array(
'header' => Mage::helper('web')->__('ID'),
'align' =>'right',
'width' => '50px',
'index' => 'web_id',
));
$this->addColumn('title', array(
'header' => Mage::helper('web')->__('Title'),
'align' =>'left',
'index' => 'title',
));
/*
$this->addColumn('content', array(
'header' => Mage::helper('web')->__('Item Content'),
'width' => '150px',
'index' => 'content',
));
*/
$this->addColumn('status', array(
'header' => Mage::helper('web')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
'options' => array(
1 => 'Enabled',
2 => 'Disabled',
),
));
$this->addColumn('action',
array(
'header' => Mage::helper('web')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('web')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
$this->addExportType('*/*/exportCsv', Mage::helper('web')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('web')->__('XML'));
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('web_id');
$this->getMassactionBlock()->setFormFieldName('web');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('web')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('web')->__('Are you sure?')
));
$statuses = Mage::getSingleton('web/status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('web')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('web')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}
Step 8: Now write the following file- app\code\local\Company\Web\Block\Adminhtml\Web\Edit.php
_objectId = 'id'; $this->_blockGroup = 'web'; $this->_controller = 'adminhtml_web'; $this->_updateButton('save', 'label', Mage::helper('web')->__('Save Item')); $this->_updateButton('delete', 'label', Mage::helper('web')->__('Delete Item')); $this->_addButton('saveandcontinue', array( 'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'), 'onclick' => 'saveAndContinueEdit()', 'class' => 'save', ), -100); $this->_formScripts[] = " function toggleEditor() { if (tinyMCE.getInstanceById('web_content') == null) { tinyMCE.execCommand('mceAddControl', false, 'web_content'); } else { tinyMCE.execCommand('mceRemoveControl', false, 'web_content'); } } function saveAndContinueEdit(){ editForm.submit($('edit_form').action+'back/edit/'); } "; } public function getHeaderText() { if( Mage::registry('web_data') && Mage::registry('web_data')->getId() ) { return Mage::helper('web')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('web_data')->getTitle())); } else { return Mage::helper('web')->__('Add Item'); } } }
Step 9:
Create the config file as app\code\local\Company\Web\etc\config.xml
0.1.0 Company_Web web web.xml Company_Web web Allow Everything Web Module 10 web.xml Company_Web_Model web_mysql4 Company_Web_Model_Mysql4 web
Company_Web Company_Web_Block Company_Web_Helper
Step 10: Now write the helper class app\code\local\Company\Web\Helper\Data.php
Step 11: Create the model class for your module app\code\local\Company\Web\Model\Web.php_init('web/web'); } }Step 12: Now create app\code\local\Company\Web\Model\Mysql4\Web.php_init('web/web', 'web_id'); } }Step 13: Now create the collection class app\code\local\Company\Web\Model\Mysql4\Web\Collection.php_init('web/web'); } }Step 14: Now add the mysql setup file as app\code\local\Company\Web\sql\web_setup\mysql4-install-0.1.0.phpstartSetup(); $installer->run(" -- DROP TABLE IF EXISTS {$this->getTable('web')}; CREATE TABLE {$this->getTable('web')} ( `web_id` int(11) unsigned NOT NULL auto_increment, `title` varchar(255) NOT NULL default '', `filename` varchar(255) NOT NULL default '', `content` text NOT NULL default '', `status` smallint(6) NOT NULL default '0', `created_time` datetime NULL, `update_time` datetime NULL, PRIMARY KEY (`web_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "); $installer->endSetup();Step 15: Add the layout.xml as app\design\frontend\default\default\layout\web.xml Step 16: Now write the following file- app\design\adminhtml\default\default\layout\web.xml< Step 17: Finally create the template file of your module app\design\frontend\default\default\template\web\web.phtml
No comments:
Post a Comment