Monday , December 17 2018
Home >> All tutorial >> Magento tutorial: Magento Global Functions

Magento tutorial: Magento Global Functions

Today, Magento Tutorial will introduce you some functionality of Magento left there by core programmers, which is used extensively by the system and yet often overlooked by us, shop maintainers and extension developers. 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. Magento Tutorial –  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. Magento Tutorial- Output a current date 

A neat shortcut to generate a MySQL-compatible date string, meetnow($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.

  1. function now($dayOnly=false)
  2. {
  3.     return date($dayOnly ? ‘Y-m-d’ : ‘Y-m-d H:i:s’);
  4. }

3. Magento Tutorial – 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. Magento Tutorial – 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.Magento Tutorial – TUse 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 stringmodulename_model_classname. Converting it into a valid Magento class name will require just one call
  1. $className = uc_words(‘modulename_model_classname’);
  2. //$className === Modulename_Model_Classname
This function can also swap separators. So if there is a stringmodulename/model/classname it can be converted into a valid class name by calling:
  1. $className = uc_words(‘modulename/model/classname’, ‘_’, ‘/’);
  2. //$className === Modulename_Model_Classname

6. Magento Tutorial – Parse a CSV string in Mageto

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.

Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

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

Heap | Mobile and Web Analytics