<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Krishna Sunuwar &#187; eCommrce</title>
	<atom:link href="http://www.krishnasunuwar.com.np/tag/ecommrce/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krishnasunuwar.com.np</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 04:39:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>eCommerce Software Magento &#8211; Shwoing Image in Grid</title>
		<link>http://www.krishnasunuwar.com.np/2009/07/ecommerce-software-magento-showing-image-in-grid/</link>
		<comments>http://www.krishnasunuwar.com.np/2009/07/ecommerce-software-magento-showing-image-in-grid/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 02:33:53 +0000</pubDate>
		<dc:creator>Krish</dc:creator>
				<category><![CDATA[Magento]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[eCommrce]]></category>

		<guid isPermaLink="false">http://www.krishnasunuwar.com.np/?p=123</guid>
		<description><![CDATA[As I said in my last post, Magento Code is awesome. Awesome that drive to hell if you don&#8217;t know, awesome like honey once you start knowing. Today I am going to write how to show images in grid by overriding Mage Core Grid Column Class. If you do not know about extension development go [...]]]></description>
			<content:encoded><![CDATA[<p>As I said in my <a title="Learning Magento" href="http://www.krishnasunuwar.com.np/2009/07/learning-magento/">last post</a>, Magento Code is awesome. Awesome that drive to hell if you don&#8217;t know, awesome like honey once you start knowing. Today I am going to write how to show images in grid by overriding Mage Core Grid Column Class. If you do not know about extension development go <a href="http://t.wits.sg/2009/03/31/howto-repackageable-custom-extension-development-in-magento/">here</a>, these guys covered pretty much about extension development. I have copied idea from them <img src='http://www.krishnasunuwar.com.np/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>Make sure you have following file structure. Only portion inside red-rectangle is applicable for images. This is to override Mage Core Grid Column Class.<br />
<img title="Magento Extension Structure to show image in grid" src="http://i70.photobucket.com/albums/i88/s2krish/blug_new/magento-module-image.jpg" alt="Magento Extension Structure to show image in grid" /><br />
<em>I assume &#8216;krish&#8217; as namespace and &#8216;timg&#8217; as extension (module) name</em></p>
<p>We also need to add option in config.xml (located at krish/timg/etc), which is as follows:</p>
<pre><code>
<blocks>
<adminhtml>
<rewrite>
<widget_grid_column>Krish_Timg_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml>
</blocks>
</code></pre>
<p>Showing above is part of code, which means that we are going to override Widget_Grid_Column class. Full config.xml looks like below:</p>
<pre>
<code> <?xml version="1.0"?>
<config>
<modules>
<Krish_Timg>
<version>0.2.0</version>
</Krish_Timg>
</modules>
<global>
<models>
<timg>
<class>Krish_Timg_Model</class>
<resourceModel>timg_mysql4</resourceModel>
</timg>
<timg_mysql4>
<class>Krish_Timg_Model_Mysql4</class>
<entities>
<timg>
<table>timg</table>

</timg>
</entities>
</timg_mysql4>
</models>
<blocks>
<timg>
<class>Krish_Timg_Block</class>
</timg>
</blocks>
<helpers>
<timg><class>Krish_Timg_Helper</class></timg>
</helpers>
<blocks>
<adminhtml>
<rewrite>
<widget_grid_column>Krish_Timg_Block_Widget_Grid_Column</widget_grid_column>
</rewrite>
</adminhtml>
</blocks>
<resources>
<timg_setup>
<setup>
<module>Krish_Timg</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</timg_setup>
<timg_write>
<connection>
<use>core_write</use>
</connection>
</timg_write>
<timg_read>
<connection>
<use>core_read</use>
</connection>
</timg_read>
</resources>
</global>
<adminhtml>
<menu>
<timg translate="title" module="timg">

<sort_order>101</sort_order>
<action>timg/admin</action>
</timg>
</menu>

</adminhtml>
<frontend>
<routers>
<Krish_Timg>
<use>standard</use>
<args>
<module>Krish_Timg</module>
<frontName>timg</frontName>
</args>
</Krish_Timg>
</routers>
</frontend>
</config></code></pre>
<p>Now, let&#8217;s see how we override Widget_Grid_Column. We have to keep those files inside Block folder (located at krish/timg/block). See Folder structure above, inside red rectangle.</p>
<p>Add Column.php file in krish/timg/block/widget/grid, which looks below:</p>
<pre><code>
class Krish_Timg_Block_Widget_Grid_Column extends Mage_Adminhtml_Block_Widget_Grid_Column
{
protected function _getRendererByType()
{
switch (strtolower($this-&gt;getType())) {
case 'image':
$rendererClass = 'timg/widget_grid_column_renderer_image';
break;
default:
$rendererClass = parent::_getRendererByType();
break;
}
return $rendererClass;
}
protected function _getFilterByType()
{
switch (strtolower($this-&gt;getType())) {
case 'image':
$filterClass = 'timg/widget_grid_column_filter_image';
break;
default:
$filterClass = parent::_getFilterByType();
break;
}
return $filterClass;
}
}
</code></pre>
<p>If column type is image, it call local class (not Core) from our renderer. Let&#8217;s define local renderers. Add Image.php file in krish/timg/block/widget/grid/column/renderer:</p>
<pre><code>
class Krish_Timg_Block_Widget_Grid_Column_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function __construct() {
}
public function render(Varien_Object $row)
{
return $this-&gt;_getValue($row);
}
protected function _getValue(Varien_Object $row)
{
$dored = false;
if ($getter = $this-&gt;getColumn()-&gt;getGetter()) {
$val = $row-&gt;$getter();
}
$val = $val2 = $row-&gt;getData($this-&gt;getColumn()-&gt;getIndex());
$url = Mage::helper('timg')-&gt;getImageUrl($val);
$size = Mage::helper('timg')-&gt;getImageThumbSize($val);
$file_extis = Mage::helper('timg')-&gt;getFileExists($val);
if($file_extis)
$out = "<img src='". $url ."' width='" .$size['width'] ."' height='" . $size['height'] ."' /> ";
return $out;
}
}
</code></pre>
<p>With help of krish_timg helper, it will display image. Let&#8217;s define action, this is also located at krish/timg/block/widget/grid/column/renderer. It is really not used so far, I&#8217;ll come to it later:</p>
<pre><code> class Krish_Timg_Block_Widget_Grid_Column_Renderer_Action extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Action
{
protected function _transformActionData(&amp;$action, &amp;$actionCaption, Varien_Object $row)
{
foreach ( $action as $attibute =&gt; $value ) {
if(isset($action[$attibute]) &amp;&amp; !is_array($action[$attibute])) {
$this-&gt;getColumn()-&gt;setFormat($action[$attibute]);
$action[$attibute] = parent::render($row);
} else {
$this-&gt;getColumn()-&gt;setFormat(null);
}
switch ($attibute) {
case 'caption':
$actionCaption = $action['caption'];
unset($action['caption']);
break;
case 'url':
if(is_array($action['url'])) {
$params = array($action['field']=&gt;$this-&gt;_getValue($row));
if(isset($action['url']['params'])) {
$params = array_merge($action['url']['params'], $params);
}
$action['href'] = $this-&gt;getUrl($action['url']['base'], $params);
unset($action['field']);
} else {
$action['href'] = $action['url'];
}
unset($action['url']);
break;
case 'popup':
$action['onclick'] = 'popWin(this.href, \'windth=800,height=700,resizable=1,scrollbars=1\');return false;';
break;
}
}
return $this;
}
}
</code></pre>
<p>Now, let&#8217;s define filter which is located at krish/timg/block/widget/grid/column/filter. We just need to inherit it:</p>
<pre><code> class Krish_Timg_Block_Widget_Grid_Column_Filter_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Text
{
}
</code></pre>
<p>Now, we have to write couple of function in helper which is located at krish/timg/helper:</p>
<pre><code>
class Krish_Timg_Helper_Data extends Mage_Core_Helper_Abstract
{
protected static $timgImgDir = null;
protected static $timgImgURL = null;
protected static $timgImgThumb = null;
protected static $timgImgThumbWidth = null;
protected $_allowedExtensions = Array();
public function KrishTimg_Helper_Data()
{
self::$timgImgDir = Mage::getBaseDir('media') .'/krish/timg/';
self::$timgImgURL = Mage::getBaseUrl('media').'/krish/timg/';
self::$timgImgThumb = "thumb/";
self::$timgImgThumbWidth = 100;
}
public function getImageUrl($image_file)
{
$url = false;
if(file_exists(self::$timgImgDir . self::$timgImgThumb . $image_file))
$url = self::$timgImgURL . self::$timgImgThumb . $image_file;
else
$url = self::$timgImgURL . $image_file;
return $url;
}
public function getFileExists($image_file)
{
$file_exists = false;
$file_exists = file_exists(self::$timgImgDir . $image_file);
return $file_exists;
}
public function getImageThumbSize($image_file)
{
$img_file = self::$timgImgDir . $image_file;
if(!file_exists($img_file)) return false;
list($width, $height, $type, $attr) = getimagesize($img_file);
$a_height = (int) ((self::$timgImgThumbWidth / $width) * $height);
return Array('width'=>self::$timgImgThumbWidth, 'height'=>$a_height);
}
function deleteFiles($image_file)
{
$pass = true;
if(!unlink(self::$timgImgDir . $image_file))
$pass = false;
if(!unlink(self::$timgImgDir . self::$timgImgThumb . $image_file))
$pass = false;
return $pass;
}
}
</code></pre>
<p>When ever you want to show image, you have to write following code in Grid.php (or wherever if grid code is located)</p>
<pre><code> $this-&gt;addColumn('file_name', array(
'header'        =&gt; Mage::helper('timg')-&gt;__('Name'),
'align'         =&gt; 'left',
'index'         =&gt; 'file_name',
'type'         =&gt; 'image',
'escape'        =&gt; true,
'sortable'  =&gt; false
));
</code></pre>
<p>That&#8217;s all, now you should be able to display image in grid.</p>
<p>Assumptions:</p>
<ol>
<li>Krish is namespace</li>
<li>timg is module name (sometime I called it extension)</li>
<li>/media/krish/timg is image location and media/krish/timg/thumb is thumbnail location.</li>
</ol>
<p>Sorry folks for messy code. I try to find better Code Escape plugin for WordPress. I tried with codeautoescape but it doesn&#8217;t work proper way. If you know better one, suggest me. Also some of code must have been modified by WordPress editor. I&#8217;ll provide full download in next post, including front-end part.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krishnasunuwar.com.np/2009/07/ecommerce-software-magento-showing-image-in-grid/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

