is_file() vs file_exists()

March 19, 2012  |  No Comments  |  by Raj (MagePsycho)  |  Latest, Magento, PHP

Introduction

If you do not know how to use is_file() and file_exists() properly, then you will end up tearing out your hair.
Here we will talk about a scenario which will clearify the usage of is_file() vs file_exists().

Case:

We are trying to show the gallery images from a table which has the field: ‘imagepath’ for storing the path of images.
And $images holds the array of gallery images.

1. Code using file_exists()

//first case: using file_exists()
foreach($images as $_image){
	$filePath = Mage::getBaseDir("media") . DS . str_replace("/", DS, $_image->getImagepath());
	if(file_exists($filePath)){
		echo '<img src="'.$filePath.'" alt="'.$_image->getTitle().'" border="0" />'; //display image
	}
}

Notes: In this case <img> tag will be displayed in every loop even if $_image->getImagepath() is empty. why?

file_exists โ€” Checks whether a file or directory exists

From the definition of file_exists(), it is clear that it checks whether a file or directory exists or not(don’t go with the name ‘file’ ;)).
In above case even if $_image->getImagepath() is empty (i.e. file doesn’t exists), directory does exist as it points to Mage::getBaseDir(“media”), file_exists($filePath) always return true.

2. Code using is_file()

//second case: using is_file()
foreach($images as $_image){
	$filePath = Mage::getBaseDir("media") . DS . str_replace("/", DS, $_image->getImagepath());
	if(is_file($filePath)){
		echo '<img src="'.$filePath.'" alt="'.$_image->getTitle().'" border="0" />';
	}
}

Notes: In this case <img> is displayed only if image file exists.

is_file โ€” Tells whether the filename is a regular file

From the definition of is_file(), it is clear that if you want to check whether a file exists or not, is_file() seems to be the right choice.

So beware of using file_exists() while checking the existence of a file, is_file() is the right choice.
Hope this article makes some sense ๐Ÿ™‚

Configuring Magento for Development / Debug Mode

March 10, 2012  |  11 Comments  |  by Raj (MagePsycho)  |  Apache, Latest, Magento

Configuring Magento for Development / Debug Mode:

1. Disable Cache
System > Cache Management > Select All [check-boxes] > Actions = Disable > Submit

2. Re-Index All
System > Index Management > Select All [check-boxes] > Actions = Reindex Data > Submit

3. Disable Compilation
System > Tools > Compilation > Disable

Note: By default compilation mode is disabled. So just check if the Compiler Status is Enabled or not.

4. Turn on Error Reporting
a> Open index.php and un-comment the following line

#ini_set('display_errors', 1);

b> Open .htaccess and add the following line at the end

SetEnv MAGE_IS_DEVELOPER_MODE "true"

5. Turn on Logging
System > Configuration > Advanced > Developer > Log Settings > Enabled => Yes

6. Configuring Mangeto Error Page
rename errors/local.xml.sample to errors/local.xml

7. Install ‘Easy Template Path Hints’
Install Easy Template Path Hints for turning on/off the template path hints for frontend and backend easily & securely.

That’s all. Let me know for any missing points.
Happy Debugging!

Invalid command ‘Header’, perhaps misspelled or defined by a module not included in the server configuration

March 7, 2012  |  4 Comments  |  by Raj (MagePsycho)  |  Apache, Latest, Linux, Magento

I got the following error for one of the Magento e-commerce website:

Invalid command โ€˜Headerโ€™, perhaps misspelled or defined by a module not included in the server configuration

After googling it was found that related headers module was not enabled in Apache.

I was able to fix the issue simply by following two commands:
1. Enabling headers Module

ln -sf /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load

2. Restarting Apache

/etc/init.d/apache2 restart

Note: Above commands were run from Ubuntu 10.04.2 server

Hope this’s helpful.
Cheers

Usage of Magento event: core_block_abstract_to_html_after

March 4, 2012  |  1 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

Introduction

There are lots of events in Magento which are dispatched by default. Among them ‘core_block_abstract_to_html_after’ is the one.

Recently we have re-developed the Easy Template Path Hints extension using this event as the previous version of the extension has some confliction issues with other modules.

In this article we will demonstrate the implementation of ‘core_block_abstract_to_html_after’ event in Easy Template Path Hints Extension:

1. Register the ‘core_block_abstract_to_html_after’ event

app/code/local/MagePsycho/Easypathhints/etc/config.xml

...
<global>
...		
	<events>
		<core_block_abstract_to_html_after>
			<observers>
				<easypathhints_core_block_abstract_to_html_after>
					<class>easypathhints/observer</class>
					<method>setTemplatePathHints</method>
				</easypathhints_core_block_abstract_to_html_after>
			</observers>
		</core_block_abstract_to_html_after>
	</events>
...
</global>
...

2. Implementing the Observer

<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Easypathhints
 * @author     magepsycho@gmail.com
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class MagePsycho_Easypathhints_Model_Observer {

	public function setTemplatePathHints(Varien_Event_Observer $observer) {

		/* @var $block Mage_Core_Block_Abstract */
		$block				= $observer->getBlock();
		$transport			= $observer->getTransport();

		$fileName			= $block->getTemplateFile();
		$thisClass			= get_class($block);
		if($fileName){
			$preHtml = '<div style="position:relative; border:1px dotted red; margin:6px 2px; padding:18px 2px 2px 2px; zoom:1;">
<div style="position:absolute; left:0; top:0; padding:2px 5px; background:red; color:white; font:normal 11px Arial;
text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'"
onmouseout="this.style.zIndex=\'998\'" title="'.$fileName.'">'.$fileName.'</div>';
			$preHtml .= '<div style="position:absolute; right:0; top:0; padding:2px 5px; background:red; color:blue; font:normal 11px Arial;
	text-align:left !important; z-index:998;" onmouseover="this.style.zIndex=\'999\'" onmouseout="this.style.zIndex=\'998\'"
	title="'.$thisClass.'">'.$thisClass.'</div>';

			$postHtml = '</div>';
		}else{
			$preHtml	= null;
			$postHtml	= null;
		}


		$html = $transport->getHtml();
		$html = $preHtml . $html . $postHtml;
		$transport->setHtml($html);
	}
}

Notes: From above code you can see that we can easily find the related $block object and $transport object which are very handy in order to find the block class, template file, modify the block output ($transport->setHtml()) etc.

Output of Easy Template Path Hints

Isn’t that cool way to modify the core code without touching it?
FYI, Event-Observer technique is always the best method for modifying core code.

Don’t forget to provide the feedback/comments if there’s any.
Happy E-commerce!!

How to change default sort order / dir in Magento search results?

March 4, 2012  |  6 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

Introduction

Today we will be discussing about how to change the defualt sort order / dir in magento search results (simple & advanced).

Regarding solution, some says you need to change the /app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php file in order to change the default sort order field and dir. But you don’t need to do so as there is an extremely easy way to do so.

Suppose say we are changing the default sort order field to ‘name’ and default sort direction to ‘asc’. Here goes the solution:

Changing default sort order / dir in simple search results:

1> Copy the following file to your custom theme folder
app/design/frontend/*/*/template/catalogsearch/form.mini.phtml
2> and insert the following two lines of code in between the

<form></form>

element of form.mini.phtml:

<input type="hidden" name="order" value="name" />
<input type="hidden" name="dir" value="asc" />

3> You’re done

Changing default sort order / dir in advanced search results:

1> Copy the following file to your custom theme folder
app/design/frontend/*/*/template/catalogsearch/advanced/form.phtml
2> and insert the following two lines of code in between the

<form></form>

element of form.phtml:

<input type="hidden" name="order" value="name" />
<input type="hidden" name="dir" value="asc" />

3> You’re done

Sort By Field: 'name', Dir: 'asc'

Hopefully this artice was useful.
Thanks for reading guys.