Hi,
Now i am creating a sub module(banner) under a module.The sub module would have functionally adding new banner,edit new banner,delete banner and listing with ajax pagination.
Step 1- First we have register action into the layout/module.xml file.
-
- <module_banner_list>
- <reference name=“root”>
- <action method=“setTemplate”>
- <template>page/module.phtml</template>
- </action>
- </reference>
- <reference name=“content”>
- <block type=“module/banner_list” name=“module_banner_list” template=“module/banner/list.phtml” />
- </reference>
- </module_banner_list>
-
-
- <module_banner_listajax>
- <reference name=“root”>
- <action method=“setTemplate”>
- <template>page/ajaxblank.phtml</template>
- </action>
- </reference>
- <reference name=“content”>
- <block type=“module/banner_list” name=“module_banner_list” template=“module/banner/ajaxlist.phtml” />
- </reference>
- </module_banner_listajax>
-
-
- <module_banner_edit>
- <reference name=“root”>
- <action method=“setTemplate”>
- <template>page/module.phtml</template>
- </action>
- </reference>
- <reference name=“content”>
- <block type=“module/banner_edit” name=“module_banner_edit” template=“module/banner/edit.phtml” />
- </reference>
- </module_banner_edit>
<!– Banner list for ajax –>
<module_banner_listajax>
<reference name=”root”>
<action method=”setTemplate”>
<template>page/ajaxblank.phtml</template>
</action>
</reference>
<reference name=”content”>
<block type=”module/banner_list” name=”module_banner_list” template=”module/banner/ajaxlist.phtml” />
</reference>
</module_banner_listajax>
<!– Banner console for add/edit –>
<module_banner_edit>
<reference name=”root”>
<action method=”setTemplate”>
<template>page/module.phtml</template>
</action>
</reference>
<reference name=”content”>
<block type=”module/banner_edit” name=”module_banner_edit” template=”module/banner/edit.phtml” />
</reference>
</module_banner_edit>
step 2- creating a class under jquery js file for listing with pagination and toggle of div.
-
-
-
- Module.Banner = function() {
-
- var that = {};
-
- params = {
- ‘url’: ‘/module/banner/listAjax/’,
- ‘containerId’: ‘banners-container’
- };
-
- jQuery(document).ready(function() {
- that.displayControl(jQuery(‘#type’).val());
- });
-
- jQuery(‘#type’).live(‘change’, function() {
- that.displayControl(jQuery(‘#type’).val());
- });
-
- jQuery(‘a.banners-list-page’).live(‘click’, function() {
- params['url'] = jQuery(this).attr(‘data-url’);
- params['containerId'] = jQuery(this).attr(‘data-active’);
- namespace.Class.Pagination().getPageData(params);
- namespace.Loader.show();
- });
-
- that.displayControl = function(type) {
- var data = jQuery(‘#data’).val();
- if(type == ‘image’) {
- jQuery(‘#image-div’).css(‘display’, ‘block’);
- } else {
- jQuery(‘#image-div’).css(‘display’, ‘none’);
- }
- that.addControl(type);
- jQuery(‘#data’).val(data);
- }
-
- that.addControl = function(type) {
- controlType = type == ‘image’ ? ‘input’ : ‘textarea’;
- var element = document.createElement(controlType);
- element.setAttribute(“name”, ‘data’);
- element.setAttribute(“id”, ‘data’);
- if(type == ‘image’) {
- element.setAttribute(“type”, ‘text’);
- element.setAttribute(“class”, ‘xlarge’);
- } else {
- element.setAttribute(“class”, ‘xxlarge’);
- element.setAttribute(“rows”, ’9′);
- element.setAttribute(“cols”, ’45′);
- }
- jQuery(‘.data’).html(element);
- }
- Module.Pagination().getPageData(params);
var that = {};
params = {
‘url’: ‘/module/banner/listAjax/’,
‘containerId’: ‘banners-container’
};
jQuery(document).ready(function() {
that.displayControl(jQuery(‘#type’).val());
});
jQuery(‘#type’).live(‘change’, function() {
that.displayControl(jQuery(‘#type’).val());
});
jQuery(‘a.banners-list-page’).live(‘click’, function() {
params['url'] = jQuery(this).attr(‘data-url’);
params['containerId'] = jQuery(this).attr(‘data-active’);
namespace.class.Pagination().getPageData(params);
namespace.Loader.show();
});
that.displayControl = function(type) {
var data = jQuery(‘#data’).val();
if(type == ‘image’) {
jQuery(‘#image-div’).css(‘display’, ‘block’);
} else {
jQuery(‘#image-div’).css(‘display’, ‘none’);
}
that.addControl(type);
jQuery(‘#data’).val(data);
}
that.addControl = function(type) {
controlType = type == ‘image’ ? ‘input’ : ‘textarea’;
var element = document.createElement(controlType);
element.setAttribute(“name”, ‘data’);
element.setAttribute(“id”, ‘data’);
if(type == ‘image’) {
element.setAttribute(“type”, ‘text’);
element.setAttribute(“class”, ‘xlarge’);
} else {
element.setAttribute(“class”, ‘xxlarge’);
element.setAttribute(“rows”, ’9′);
element.setAttribute(“cols”, ’45′);
}
jQuery(‘.data’).html(element);
}
Module.Pagination().getPageData(params);
Step 3- creating a class pagination common in js file.\
-
-
-
- Module.Pagination = function() {
-
- var that = {};
-
-
-
-
- that.getPageData = function(params) {
- Loader.show();
- params = params ? params : ”;
- url = params['url'];
- containerId = params['containerId'] ? params['containerId'] : ”;
- if (url == ”) {
- jQuery(‘#’ + containerId).html(‘<span class=”empty”>Invalid URL found.</span>’);
- } else {
- jQuery(‘#’ + containerId).empty();
- that.ajax = jQuery.ajax({
- url: url,
- dataType: ‘html’,
- type: ‘post’,
- data: params,
- success: function(response) {
- if (response == ”) {
- response = ‘<span class=”empty”>No record(s) found.</span>’;
- }
- jQuery(‘#’ + containerId).html(response);
- Loader.hide();
- }
- });
- }
- }
-
- return that;
- }
var that = {};
/**
* ajax function to bind page data for current page using pagination.
*/
that.getPageData = function(params) {
Loader.show();
params = params ? params : ”;
url = params['url'];
containerId = params['containerId'] ? params['containerId'] : ”;
if (url == ”) {
jQuery(‘#’ + containerId).html(‘<span class=”empty”>Invalid URL found.</span>’);
} else {
jQuery(‘#’ + containerId).empty();
that.ajax = jQuery.ajax({
url: url,
dataType: ‘html’,
type: ‘post’,
data: params,
success: function(response) {
if (response == ”) {
response = ‘<span class=”empty”>No record(s) found.</span>’;
}
jQuery(‘#’ + containerId).html(response);
Loader.hide();
}
});
}
}
return that;
}
step 4- creating list.phtml file for listing
- <h2>Banners List</h2>
-
- <?php
- $messages = Mage::getSingleton(‘customer/session’)->getData(‘status’, true);
- ?>
-
- <?php if ($messages[0]) { ?>
- <div class=“alert-message <?= $messages[0] ?>”><?= $messages[1] ?></div>
- <? } ?>
- <div class=“buttons pull-right” id=“add-div”>
- <a href=“/module/banner/edit/id/0″ class=“btn primary” id=“banner”>Add Banner</a>
- </div>
- <div id=“banners-container”></div>
-
- <script type=“text/javascript”>
- Module.Banner();
- </script>
<?php
$messages = Mage::getSingleton(‘customer/session’)->getData(‘status’, true);
?>
<?php if ($messages[0]) { ?>
<div class=”alert-message <?= $messages[0] ?>”><?= $messages[1] ?></div>
<? } ?>
<div class=”buttons pull-right” id=”add-div”>
<a href=”/module/banner/edit/id/0″ class=”btn primary” id=”banner”>Add Banner</a>
</div>
<div id=”banners-container”></div>
<script type=”text/javascript”>
Module.Banner();
</script>
step 5- creating listAjax.phtml file
- <?php
- $banners = $this->getBanners(array(‘id’, ‘type’, ‘data’, ‘brand’, ‘user_id’, ‘active’, ‘created_at’));
- ?>
-
- <div class=“result-count”>
- <span class=“pagination”><?= $this->getPaginationHtml(); ?></span>
- </div>
-
- <table>
- <thead>
- <tr>
- <th>Type</th>
- <th>Data</th>
- <th>Brand</th>
- <th>User</th>
- <th>Status</th>
- <th>Created At</th>
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- <?php if (count($banners) > 0): ?>
- <?php foreach ($banners as $banner): ?>
- <tr>
- <td><?= ucfirst($banner['type']) ?></td>
- <td><a href=“/module/banner/edit/id/<?= $banner['id'] ?>”><?= htmlentities(strlen($banner['data']) < 25 ? $banner['data'] : substr($banner['data'], 0 ,25) . ‘ …’) ?> </a></td>
- <td><?= ucfirst($banner['brand']) ?></td>
- <td><?= ucfirst($banner['email']) ?></td>
- <td><?= $banner['active'] ?></td>
- <td><?= $banner['created_at'] ?></td>
- <td><a class=“delete” data-url=“/module/banner/delete/id/<?= $banner['id'] ?>” data-redirect=“/module/banner/list/”>Delete</a></td>
- </tr>
- <?php endforeach ?>
- <?php else: ?>
- <tr>
- <td colspan=“4″>No record(s) found.</td>
- </tr>
- <?php endif ?>
- </tbody>
- </table>
<div class=”result-count”>
<span class=”pagination”><?= $this->getPaginationHtml(); ?></span>
</div>
<table>
<thead>
<tr>
<th>Type</th>
<th>Data</th>
<th>Brand</th>
<th>User</th>
<th>Status</th>
<th>Created At</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (count($banners) > 0): ?>
<?php foreach ($banners as $banner): ?>
<tr>
<td><?= ucfirst($banner['type']) ?></td>
<td><a href=”/module/banner/edit/id/<?= $banner['id'] ?>”><?= htmlentities(strlen($banner['data']) < 25 ? $banner['data'] : substr($banner['data'], 0 ,25) . ‘ …’) ?> </a></td>
<td><?= ucfirst($banner['brand']) ?></td>
<td><?= ucfirst($banner['email']) ?></td>
<td><?= $banner['active'] ?></td>
<td><?= $banner['created_at'] ?></td>
<td><a class=”delete” data-url=”/module/banner/delete/id/<?= $banner['id'] ?>” data-redirect=”/module/banner/list/”>Delete</a></td>
</tr>
<?php endforeach ?>
<?php else: ?>
<tr>
<td colspan=”4″>No record(s) found.</td>
</tr>
<?php endif ?>
</tbody>
</table>
step 6-creating edit.phtml file
- <?php
- $message = \Mage::getSingleton(‘customer/session’)->getData(‘errorMsg’, true);
- $type = $this->getData(‘type’);
- $id = $this->getId();
- $active = $this->getData(‘active’);
- ?>
-
- <h2><?php echo $id == 0 ? ‘Create’ : ‘Edit’; ?> Banner</h2>
-
- <?php if($message[1]) { ?>
- <div class=“alert-message <?= $message[0] ?>”><?= $message[1] ?></div>
- <?php } ?>
-
- <form action=“/module/banner/edit/id/<?= $id ?>” method=“post”>
- <div class=“clearfix”>
- <label>Type:</label>
- <div class=“input”>
- <select name=“type” id=“type” class=“medium”>
- <option value=“html” <?= $type == ‘html’ ? ‘selected=”selected”‘ : ” ?>>HTML</option>
- <option value=“image” <?= $type == ‘image’ ? ‘selected=”selected”‘ : ” ?>>Image</option>
- </select>
- </div>
- </div>
- <?= Mage::Helper(‘module/Product’)->getBrandsHtml($this->getData(‘brand’), true) ?>
- <div id=“image-div”>
- <div class=“clearfix”>
- <label>Link</label>
- <div class=“input”><input type=“text” class=“xlarge” name=“link” id=“link” value=“<?= $this->getData(‘link’) ?>” maxlength=“255″/></div>
- </div>
- </div>
- <div class=“clearfix”>
- <label>Width:</label>
- <div class=“input”><input type=“text” class=“xlarge” name=“width” id=“width” value=“<?= $this->getData(‘width’) ?>” maxlength=“4″/></div>
- </div>
- <div class=“clearfix”>
- <label>Height:</label>
- <div class=“input”><input type=“text” class=“xlarge” name=“height” id=“height” value=“<?= $this->getData(‘height’) ?>” maxlength=“4″/></div>
- </div>
- <div class=“clearfix”>
- <label>Data:</label>
- <div class=“input data”>
- <textarea cols=“45″ rows=“9″ class=“xxlarge” name=“data” id=“data”><?= $this->getData(‘data’) ?></textarea>
- </div>
- </div>
- <div class=“clearfix”>
- <label>Active:</label>
- <div class=“input”>
- <input type=“checkbox” value=“1″ <?= ($active == 1 || $active == ”) ? ‘checked=”checked”‘ : ” ?> name=“active” id=“active” />
- </div>
- </div>
- <div class=“actions”>
- <input type=“hidden” name=“id” value=“<?= $id ?>”/>
- <button type=“submit” class=“btn primary”>Submit</button>
- <a class=“btn primary” href=“/module/banner/list/” >Cancel</a>
- </div>
- </form>
-
- <script type=“text/javascript”>
- Module.Banner();
- </script>
<h2><?php echo $id == 0 ? ‘Create’ : ‘Edit’; ?> Banner</h2>
<?php if($message[1]) { ?>
<div class=”alert-message <?= $message[0] ?>”><?= $message[1] ?></div>
<?php } ?>
<form action=”/module/banner/edit/id/<?= $id ?>” method=”post”>
<div class=”clearfix”>
<label>Type:</label>
<div class=”input”>
<select name=”type” id=”type” class=”medium”>
<option value=”html” <?= $type == ‘html’ ? ‘selected=”selected”‘ : ” ?>>HTML</option>
<option value=”image” <?= $type == ‘image’ ? ‘selected=”selected”‘ : ” ?>>Image</option>
</select>
</div>
</div>
<?= Mage::Helper(‘module/Product’)->getBrandsHtml($this->getData(‘brand’), true) ?>
<div id=”image-div”>
<div class=”clearfix”>
<label>Link</label>
<div class=”input”><input type=”text” class=”xlarge” name=”link” id=”link” value=”<?= $this->getData(‘link’) ?>” maxlength=”255″/></div>
</div>
</div>
<div class=”clearfix”>
<label>Width:</label>
<div class=”input”><input type=”text” class=”xlarge” name=”width” id=”width” value=”<?= $this->getData(‘width’) ?>” maxlength=”4″/></div>
</div>
<div class=”clearfix”>
<label>Height:</label>
<div class=”input”><input type=”text” class=”xlarge” name=”height” id=”height” value=”<?= $this->getData(‘height’) ?>” maxlength=”4″/></div>
</div>
<div class=”clearfix”>
<label>Data:</label>
<div class=”input data”>
<textarea cols=”45″ rows=”9″ class=”xxlarge” name=”data” id=”data”><?= $this->getData(‘data’) ?></textarea>
</div>
</div>
<div class=”clearfix”>
<label>Active:</label>
<div class=”input”>
<input type=”checkbox” value=”1″ <?= ($active == 1 || $active == ”) ? ‘checked=”checked”‘ : ” ?> name=”active” id=”active” />
</div>
</div>
<div class=”actions”>
<input type=”hidden” name=”id” value=”<?= $id ?>”/>
<button type=”submit” class=”btn primary”>Submit</button>
<a class=”btn primary” href=”/module/banner/list/” >Cancel</a>
</div>
</form>
<script type=”text/javascript”>
Module.Banner();
</script>
step 7- creating controller for all action
- class Namespace_Module_BannerController extends Mage_Core_Controller_Front_Action
- {
-
-
-
- public function indexAction()
- {
- Mage::helper(‘module/acl’)->loadPagePermissions(‘banner’, ‘view’);
- $this->loadLayout();
- $this->renderLayout();
- }
-
-
-
-
- public function listAction()
- {
- Mage::helper(‘module/acl’)->loadPagePermissions(‘banner’, ‘view’);
- $this->loadLayout();
- $this->renderLayout();
- }
-
-
-
-
- public function listAjaxAction()
- {
- Mage::helper(‘module/Acl’)->loadPagePermissions(‘banner’, ‘view’);
- $this->loadLayout();
- $this->renderLayout();
- }
-
-
-
-
- public function editAction()
- {
- Mage::helper(‘module/Acl’)->loadPagePermissions(‘banner’, ‘view’);
- $params = $this->getRequest()->getPost();
- if (!emptyempty($params)) {
- try {
- if (Mage::getModel(‘module/Banner’)->setData($params)->validate()->save()) {
- Mage::getModel(‘customer/session’)->setData(‘status’, array(‘success’, ‘Record saved successfully.’));
- $this->_redirect(‘*/*/list/’);
- }
- } catch (\Exception $e) {
- Mage::getModel(‘customer/session’)->setData(‘errorMsg’, array(‘errors’, $e->getMessage()));
- }
- }
- $this->loadLayout();
- $this->renderLayout();
- }
-
- /**
- * delete the selected banner id
- */
- public function deleteAction()
- {
- Mage::helper(‘module/Acl’)->loadPagePermissions(‘banner’, ‘view’);
- try {
- if (Mage::getModel(‘module/Banner’)->delete($this->getRequest()->getParam(‘id’, 0))) {
- Mage::getModel(‘customer/session’)->setData(‘status’, array(‘success’, ‘Record deleted successfully.’));
- } else {
- Mage::getModel(‘customer/session’)->setData(‘status’, array(‘error’, ‘Record not found’));
- }
- } catch (\Exception $e) {
- Mage::getModel(‘customer/session’)->setData(‘status’, array(‘error’, $e->getMessage()));
- }
- }
- }
/**
* list to get all banners
*/
public function listAction()
{
Mage::helper(‘module/acl’)->loadPagePermissions(‘banner’, ‘view’);
$this->loadLayout();
$this->renderLayout();
}
/**
* list banners list for ajax call
*/
public function listAjaxAction()
{
Mage::helper(‘module/Acl’)->loadPagePermissions(‘banner’, ‘view’);
$this->loadLayout();
$this->renderLayout();
}
/**
* To edit/add banner
*/
public function editAction()
{
Mage::helper(‘module/Acl’)->loadPagePermissions(‘banner’, ‘view’);
$params = $this->getRequest()->getPost();
if (!empty($params)) {
try {
if (Mage::getModel(‘module/Banner’)->setData($params)->validate()->save()) {
Mage::getModel(‘customer/session’)->setData(‘status’, array(‘success’, ‘Record saved successfully.’));
$this->_redirect(‘*/*/list/’);
}
} catch (\Exception $e) {
Mage::getModel(‘customer/session’)->setData(‘errorMsg’, array(‘errors’, $e->getMessage()));
}
}
$this->loadLayout();
$this->renderLayout();
}
/**
* delete the selected banner id
*/
public function deleteAction()
{
Mage::helper(‘module/Acl’)->loadPagePermissions(‘banner’, ‘view’);
try {
if (Mage::getModel(‘module/Banner’)->delete($this->getRequest()->getParam(‘id’, 0))) {
Mage::getModel(‘customer/session’)->setData(‘status’, array(‘success’, ‘Record deleted successfully.’));
} else {
Mage::getModel(‘customer/session’)->setData(‘status’, array(‘error’, ‘Record not found’));
}
} catch (\Exception $e) {
Mage::getModel(‘customer/session’)->setData(‘status’, array(‘error’, $e->getMessage()));
}
}
}
step 8- creating model for banner management.
- <?php
-
- class Namespace_Module_Model_Banner extends Varien_Object
- {
-
-
-
- protected $_db;
-
-
-
-
-
- public function __construct()
- {
- $this->_db = Db::getInstance()->getDb();
- }
-
-
-
-
-
-
- public function save()
- {
- $id = $this->getData(‘id’);
- $this->setData(‘user_id’, Mage::registry(‘MODULE_USER’)->getId());
- if ($this->getData(‘type’) == ‘html’) {
- $this->setData(‘link’, ”);
- }
- $this->setData(‘active’, $this->getData(‘active’));
- try {
- if ($id == 0) {
- $this->setData(‘created_at’, date(‘Y-m-d H:i:s’));
- $this->setData(‘updated_at’, date(‘Y-m-d H:i:s’));
- $this->_db->insert(‘j_finder_banner’, $this->getData());
- } else {
- $this->setData(‘updated_at’, date(‘Y-m-d H:i:s’));
- $this->_db->update(‘j_finder_banner’, $this->getData(), $this->_db->quoteInto(‘id = ?’, $id));
- }
- } catch (\Exception $e) {
- throw new \Exception(‘Error in save operation’);
- }
-
- return $this;
- }
-
-
-
-
-
-
-
-
- public function load($id)
- {
- if ($id != 0) {
- $sql = $this->_db->select()
- ->from(‘j_finder_banner’)
- ->where(‘id = ?’, $id)
- ->limit(1);
- if($result = $sql->query()->fetch()) {
- $this->setData($result);
- }
- }
-
- return $this;
- }
-
-
-
-
-
-
-
- public function delete($id)
- {
- if ($id != 0) {
- try {
- $result = $this->_db->delete(‘j_finder_banner’, $this->_db->quoteInto(‘id = ?’, $id));
- } catch(\Exception $e) {
- throw new \Exception(‘Unable to deleted record’);
- }
-
- return $result;
- }
- }
-
-
-
-
-
-
- public function validate()
- {
- $errors = array();
- $data = $this->getData(‘data’);
- $link = $this->getData(‘link’);
- $width = $this->getData(‘width’);
- $height = $this->getData(‘height’);
- if ($this->getData(‘type’) == ‘image’) {
- if (emptyempty($link)) {
- $errors[] = ‘Link must not be blank.’;
- }
- if (emptyempty($width)) {
- $errors[] = ‘Width must not be blank.’;
- } elseif (!is_numeric($width) || $width < 0) {
- $errors[] = ‘Width must be a positive numeric value.’;
- }
- if (emptyempty($height)) {
- $errors[] = ‘Height must not be blank.’;
- } elseif (!is_numeric($height) || $height < 0) {
- $errors[] = ‘Height must be a positive numeric value.’;
- }
- }
- if (emptyempty($data)) {
- $errors[] = ‘Data must not be blank.’;
- }
- if (!emptyempty($errors)) {
- Mage::register(‘error’, $errors);
- throw new \Exception(implode(‘
- ’, $errors));
- }
-
- return $this;
- }
- }
class Namespace_Module_Model_Banner extends Varien_Object
{
/**
* @var Varien_Db_Adapter_Pdo_Mysql Singleton instance of finder db
*/
protected $_db;
/**
* Constructor of the class to initialize the database object
*
*/
public function __construct()
{
$this->_db = Db::getInstance()->getDb();
}
/**
* Save the Banner data into the database
*
* @return boolean
*/
public function save()
{
$id = $this->getData(‘id’);
$this->setData(‘user_id’, Mage::registry(‘MODULE_USER’)->getId());
if ($this->getData(‘type’) == ‘html’) {
$this->setData(‘link’, ”);
}
$this->setData(‘active’, $this->getData(‘active’));
try {
if ($id == 0) {
$this->setData(‘created_at’, date(‘Y-m-d H:i:s’));
$this->setData(‘updated_at’, date(‘Y-m-d H:i:s’));
$this->_db->insert(‘j_finder_banner’, $this->getData());
} else {
$this->setData(‘updated_at’, date(‘Y-m-d H:i:s’));
$this->_db->update(‘j_finder_banner’, $this->getData(), $this->_db->quoteInto(‘id = ?’, $id));
}
} catch (\Exception $e) {
throw new \Exception(‘Error in save operation’);
}
return $this;
}
/**
* Load the Banner details
*
* @param integer $id
*
* @return $data array details of Banner
*/
public function load($id)
{
if ($id != 0) {
$sql = $this->_db->select()
->from(‘j_finder_banner’)
->where(‘id = ?’, $id)
->limit(1);
if($result = $sql->query()->fetch()) {
$this->setData($result);
}
}
return $this;
}
/**
* Delete the selected Banner
*
* @param integer $id
*
*/
public function delete($id)
{
if ($id != 0) {
try {
$result = $this->_db->delete(‘j_finder_banner’, $this->_db->quoteInto(‘id = ?’, $id));
} catch(\Exception $e) {
throw new \Exception(‘Unable to deleted record’);
}
return $result;
}
}
/**
* validate all post Banner data
*
* @return boolean
*/
public function validate()
{
$errors = array();
$data = $this->getData(‘data’);
$link = $this->getData(‘link’);
$width = $this->getData(‘width’);
$height = $this->getData(‘height’);
if ($this->getData(‘type’) == ‘image’) {
if (empty($link)) {
$errors[] = ‘Link must not be blank.’;
}
if (empty($width)) {
$errors[] = ‘Width must not be blank.’;
} elseif (!is_numeric($width) || $width < 0) {
$errors[] = ‘Width must be a positive numeric value.’;
}
if (empty($height)) {
$errors[] = ‘Height must not be blank.’;
} elseif (!is_numeric($height) || $height < 0) {
$errors[] = ‘Height must be a positive numeric value.’;
}
}
if (empty($data)) {
$errors[] = ‘Data must not be blank.’;
}
if (!empty($errors)) {
Mage::register(‘error’, $errors);
throw new \Exception(implode(‘<br/> ‘, $errors));
}
return $this;
}
}
step 9- creating edit.php block file
- <?php
-
- class Namespace_Module_Block_Banner_Edit extends Mage_Core_Block_Template
- {
-
-
-
- protected $_data;
-
-
-
-
-
- public function __construct()
- {
- $this->_data = count($this->getPostData()) > 0
- ? $this->getPostData()
- : Mage::getModel(‘empire/Banner’)->load($this->getId())->getData();
- }
-
-
-
-
-
-
- public function getId()
- {
- return $this->getRequest()->getParam(‘id’, 0);
- }
-
-
-
-
-
-
- public function getPostData()
- {
- return $this->getRequest()->getPost();
- }
- }
class Namespace_Module_Block_Banner_Edit extends Mage_Core_Block_Template
{
/**
* @var array of data
*/
protected $_data;
/**
* Constructor of the class to set data variable
*
*/
public function __construct()
{
$this->_data = count($this->getPostData()) > 0
? $this->getPostData()
: Mage::getModel(‘empire/Banner’)->load($this->getId())->getData();
}
/**
* To return the current banner id
*
* @return integer
*/
public function getId()
{
return $this->getRequest()->getParam(‘id’, 0);
}
/**
* To return the post array data
*
* @return array
*/
public function getPostData()
{
return $this->getRequest()->getPost();
}
}
step 10- You will create list.php.This file would kept only banners() listing method.