Solving Magento

Solutions for Magento E-Commerce Platform

by Oleg Ishenko

Magento Global Functions

Reinventing the wheel – what a cliche! Of course you don’t do it, at least not intentionally. And you hate being reminded of code reuse on those rare occasions when someone points to a function you wrote, which allegedly does the same as some other function somewhere in the framework. Yet it happens. Sometimes looking for blueprints for the wheel takes longer than hacking it out yourself. Sometimes you might do it just for fun. And sometimes you simply don’t know a ready solution exists buried in some obscure location in your framework. Magento is no exception – it does have some functionality left there by the core programmers, which is used extensively by the system and yet often overlooked by us, shop maintainers and extension developers.

A few such gems can be found in app/code/core/Mage/Core/functions.php. This file contains no class definition but globally available functions. This file is included early in the application initialization stage in app/Mage.php. Some of its functions may be of little use when developing extensions, since they do core system work. But some can come in handy whenever a trivial task presents itself and you have to:

1. Find out if a Directory is Writable in Magento

It is a true rarity, a Magento shop running on Windows. Still, improbable not impossible. Assuming you are in a Linux environment, this task can be easily accomplished by calling is_dir($path) and is_writable($path). Not so simple with Windows hosts. The function is_dir_writeable($dir) does some additional checks for such cases – by literally trying to write a file into the directory being checked. This function is used by Magento when checking writability of certain paths during a shop installation before creating vartmp and cache directories. Obviously, it is a well-proven approach – why not use it?

2. Output a Current Date

A neat shortcut to generate a MySQL-compatible date string, meet now($dayOnly=false). Leave the $dayOnly empty or set to false to get a Y-m-d H:i:s-formatted string, otherwise it will output a short Y-m-d representation of the current date.

function now($dayOnly=false)
{
    return date($dayOnly ? 'Y-m-d' : 'Y-m-d H:i:s');
}

3. Recursively Delete a Directory in Magento

Ever needed to delete a deeply nested directory structure? Use mageDelTree($dir). Magento uses this function to, for instance, clear user sessions when they are stored on disk.

4. Find out if a Class exists in Magento

If you need to make sure an external dependency is available in your installation, check it with mageFindClassFile($class), where the $class parameter is a class name, e.g. ExtPackage_ExtModule_Model_Classname.

5. Use an Extended Version of ucwords

This nifty little function does the same as the regular ucwords. And more! If you need to uppercase words in a string that is separated by characters other than whitespace – you can save some extra lines of code here. Say, you have a string modulename_model_classname. Converting it into a valid Magento class name will require just one call:

$className = uc_words('modulename_model_classname');
//$className === Modulename_Model_Classname

This function can also swap separators. So if there is a string modulename/model/classname it can be converted into a valid class name by calling:

$className = uc_words('modulename/model/classname', '_', '/');
//$className === Modulename_Model_Classname

Neat, eh?

6. Parse a CSV String in Magento

Now this kind of task comes fairly often. This function will save you time writing a parser – even if such is simple and takes a minute. But why bother if the function already exists? And it can handle quoted values as well. Check it out:

mageParseCsv($string, $delimiter=',', $enclosure='"', $escape='\\')

The last parameter, however, is redundant and used nowhere in the function.

Leave a Reply

Your email address will not be published. Required fields are marked *

Theme: Esquire by Matthew Buchanan.