Search This Blog

Monday, February 28, 2011

To add a tab in the right side of admin module form

To add a tab in the right side of admin module form

app\code\local\Namespace\module\Block\Adminhtml\module\Edit \Tabs.php
in function _beforeToHtml
copy paste the addTab code and change the name

Sunday, February 27, 2011

Magento export porduct as text file with txt extension for feed or feeds

Hey there

Check the code below for Magento export porduct as text file with txt extension for feed or feeds

Magento - Export & Import Custom Product Options

The code

<?php
    // Load Magento
    require_once $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
    umask(0);
    Mage::app();
   
    // set Store ID
    $storeId         = Mage::app()->getStore()->getStoreId();
   
    // set loop start timer
    $starttime         = microtime();
    $startarray     = explode(" ", $starttime);
    $starttime         = $startarray[1] + $startarray[0];
   
    // get basic configuration
    $baseUrl         = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
    $baseMediaUrl     = Mage::getSingleton('catalog/product_media_config')->getBaseMediaUrl();
    $storeCharset    = Mage::getStoreConfig('design/head/default_charset');
   
    // set report email sender
    $from_email        = Mage::getStoreConfig('trans_email/ident_general/email');    // sender: general contact email
    $from_name        = Mage::getStoreConfig('trans_email/ident_general/name');    // sender: general contact name
   
    // set report email recipient from backend user "admin"
    Mage::getSingleton('core/session', array('name' => 'adminhtml'));
    $admin             = Mage::getModel('admin/user')->load(1);                     // load first administrator
    $to_email        = $admin->getEmail();                                        // recipient: admin email
    $to_name        = $admin->getFirstname() . ' ' . $admin->getLastname();        // recipient: admin name
   
    // set Google Base feed filename and directory
    $_feed_dir        = '/media/feed/';                                            // Feed directory
    $feedDir         = $_SERVER['DOCUMENT_ROOT'] . $_feed_dir;                    // Path to feed directory
    $gbaseFeed        = 'google_base_feed.txt';                                    // Google base feed filename
    $tmpCounter        = 'counter.txt';                                            // Temporary counter file
    $timer            = 'timer.txt';                                                // Temporary timer file
   
    // set log filename and directory
    $logDir            = $_SERVER['DOCUMENT_ROOT'] . '/var/log/';                    // Log directory
    $logFile        = $logDir . date('Y-m') . '-gbase-feed.log';                // Google base log filename
   
    // define basic file constants
    define('SAVE_FEED_LOCATION',     $feedDir . $gbaseFeed);                        // google base file
    define('SAVE_FEED_RELOAD',         $feedDir . $tmpCounter);                    // counter file
    define('SAVE_FEED_TIMER',        $feedDir . $timer);                            // timer file
               
    // make sure we don't time out
    @ini_set('max_execution_time', '0');
    @ini_set('max_input_time', '0');
    @set_time_limit(0);
    @ignore_user_abort(true);
   
    // set headers to Magento character set
    header('Content-Type: text/html; charset=' . $storeCharset);
       
    try
    {
       
        // create FEED directory in case it doesn't exist yet
        if (!file_exists($feedDir))
        {
            mkdir($feedDir, 0777);
        }
       
        // basic setup
        $reload_line         = (file_exists(SAVE_FEED_RELOAD)) ? file_get_contents(SAVE_FEED_RELOAD) : 0;
        $timer_line         = (file_exists(SAVE_FEED_TIMER)) ? file_get_contents(SAVE_FEED_TIMER) : 0;
        $handle                = ($reload_line == 0) ? fopen(SAVE_FEED_LOCATION, 'w') : fopen(SAVE_FEED_LOCATION, 'a+');
        $handle_reload         = fopen(SAVE_FEED_RELOAD, 'w');
        $handle_timer         = fopen(SAVE_FEED_TIMER, 'w');
       
        if ($reload_line == 0)
        {
            $heading         = array('id','title','description','link','image_link','price','brand','product_type','condition', 'c:product_code');
            $feed_line        = implode("\t", $heading) . "\r\n";
            fwrite($handle, $feed_line);
        }
       
        //---------------------- GET THE PRODUCTS   
        $visibility         =     array (
                                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                                    Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                                );
       
        $products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToFilter('status', 1);                     //enabled
        $products->addAttributeToFilter('visibility', $visibility);
        $products->addAttributeToSelect('sku');
        $prodIds = $products->getAllIds();
       
        foreach($prodIds as $productId)
        {
           
            if (($productId>=$reload_line) & ($productId<$reload_line+100))
            {
               
                // load product object
                $product                             = Mage::getModel('catalog/product');
                $product->load($productId);
               
                // populate product data array
                $product_data                         = array();   
                $product_data['sku']                = $product->getSku();   
                $product_data['title']                = $product->getName();
                $product_data['description']        = trim(ereg_replace("^[ \t\r\n]+|[ \t\r\n]+", " ", strip_tags($product->getShortDescription())));
                $product_data['link']                = $baseUrl . $product->getUrlKey() . Mage::getStoreConfig('catalog/seo/product_url_suffix') . '?source=googleps';
                $product_data['image_link']            = $baseMediaUrl . $product->getImage();
                $product_data['price']                = $product->getFinalPrice();
                $product_data['brand']                = $product->getAttributeText('manufacturer');   
                $product_data['product_type']         = '';
                $product_data['condition']            = 'new';
                $product_data['c:product_code']        = $product->getSku();
                   
                //get the product categories                   
                foreach($product->getCategoryIds() as $_categoryId)
                {
                    $category                         = Mage::getModel('catalog/category')->load($_categoryId);
                    $product_data['product_type']    .= $category->getName().', ';
                }
                $product_data['product_type']        = rtrim($product_data['product_type'],', ');       
   
                //sanitize data   
                foreach($product_data as $k=>$val)
                {
                    $bad                            = array('"',"\r\n","\n","\r","\t");
                    $good                            = array(""," "," "," ","");
                    $product_data[$k]                 = '"'.str_replace($bad,$good,$val).'"';
                }

                $feed_line                             = implode("\t", $product_data)."\r\n";
                fwrite($handle, $feed_line);
                fflush($handle);
                       
            }
        }

        //---------------------- WRITE THE FEED   
        fclose($handle);
        $feed_line_reload     = $reload_line + 100;
        fwrite($handle_reload, $feed_line_reload);
        fclose($handle_reload);
       
        // end of loop time
        $looptime             = microtime();
        $looparray             = explode(" ", $looptime);
        $looptime             = $looparray[1] + $looparray[0];
        $tmptime             = $looptime - $starttime;
       
        // sum of all passed loop times
        $feed_line_timer     = $timer_line + $tmptime;
        fwrite($handle_timer, $feed_line_timer);
        fclose($handle_timer);
    }
    catch(Exception $e)
    {
        //---------------------- LOG THE EXCEPTION   
        if (!file_exists($logDir))
        {
            mkdir($logDir, 0777);
        }
       
        if (!file_exists($logFile))
        {
            touch($logFile);
           
            $logHandle = fopen($logFile, 'w+');
           
            fwrite(              
                   $logHandle,
                   date('Y-m-d  H:i:s') . "  |  GOOGLE BASE FEED GENERATION PROBLEM\n\n" .
                   $e->getMessage() . "\n\n" .
                   "-------------------------------------------------------------------------------------------------------------------\n\n"
            );
           
            fclose($logHandle);        
        }
        else
        {
           
            $ini_handle     = fopen($logFile, "r");
            $ini_contents     = fread($ini_handle, filesize($logFile));
            fclose($ini_handle);
           
            $logHandle         = fopen($logFile, 'w+');
            fwrite(
                   $logHandle,
                   date('Y-m-d  H:i:s') . "  |  GOOGLE BASE FEED PROBLEM\n\n" .
                   $e->getMessage() . "\n\n" .
                   "-------------------------------------------------------------------------------------------------------------------\n\n" .
                   $ini_contents
            );
            fclose($logHandle);
           
        }
       
        //---------------------- SEND THE EXCEPTION    TO the ADMIN'S EMAIL
        $mail = new Zend_Mail($storeCharset);
        $mail->setBodyText($e->getMessage(), $storeCharset);
        $mail->setFrom($from_email, $from_name);
        $mail->addTo($to_email, $to_name);
        $mail->setSubject(date('Y-m-d') . ': Google base problem');
        try
        {
            $mail->send();
        }       
        catch(Exception $ex)
        {
            Mage::getSingleton('core/session')->addError('Cann\'t send the email. Try it later, please ...');
        }
       
        header('Content-Type: text/html; charset=' . $storeCharset);
        die($e->getMessage());
    }
    if (end($prodIds) < $reload_line)
    {
        // final feed generation time
        $totaltime         = round($feed_line_timer, 2); 
       
        // remove counter file
        @unlink(SAVE_FEED_RELOAD);
        @unlink(SAVE_FEED_TIMER);
       
        //---------------------- LOG THE SUCCESS   
        if (!file_exists($logDir))
        {
            mkdir($logDir, 0777);
        }
       
        if (!file_exists($logFile))
        {
            touch($logFile);
           
            $logHandle = fopen($logFile, 'w+');
           
            fwrite(              
                   $logHandle,
                   date('Y-m-d  H:i:s', filemtime ( SAVE_FEED_LOCATION ) ) . "  |  " . $gbaseFeed . "  |  " . count($prodIds) . " products exported  |  time: " . $totaltime . " seconds  |  size: " . format_bytes ( filesize ( SAVE_FEED_LOCATION ) ). "\n" .
                   "-------------------------------------------------------------------------------------------------------------------\n\n"
            );
           
            fclose($logHandle);        
        }
        else
        {
           
            $ini_handle     = fopen($logFile, "r");
            $ini_contents     = fread($ini_handle, filesize($logFile));
            fclose($ini_handle);
           
            $logHandle         = fopen($logFile, 'w+');
            fwrite(
                   $logHandle,
                   date('Y-m-d  H:i:s', filemtime ( SAVE_FEED_LOCATION ) ) . "  |  " . $gbaseFeed . "  |  " . count($prodIds) . " products exported  |  time: " . $totaltime . " seconds  |  size: " . format_bytes ( filesize ( SAVE_FEED_LOCATION ) ). "\n" .
                   "-------------------------------------------------------------------------------------------------------------------\n\n" .
                   $ini_contents
            );
            fclose($logHandle);
           
        }
       
        $log_handle     = fopen($logFile, "r");
        $log_contents     = fread($log_handle, filesize($logFile));
        fclose($log_handle);
       
        //---------------------- SEND THE LOG FILE TO ADMIN EVERY LAST DAY OF MONTH   
        if( date('d') == find_last_day(date('Y-m-d')) )
        {
           
            $mail = new Zend_Mail($storeCharset);
            $mail->setBodyText($log_contents, $storeCharset);
            $mail->setFrom($from_email, $from_name);
            $mail->addTo($to_email, $to_name);
            $mail->setSubject('Google base log from ' . date('m/Y') . ' period');
            try
            {
                $mail->send();
            }       
            catch(Exception $ex)
            {
                Mage::getSingleton('core/session')->addError('Cann\'t send the email. Try it later, please ...');
            }
           
        }
    }
    else
    {
        //---------------------- REFRESH SCRIPT TILL THE WHOLE FEED IS GENERATED
       
        curl_file_get_contents('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);

        // possible refresh variants

        //---------------------- script runs by web browser
        // header( 'refresh: 1; url=' . $_SERVER['PHP_SELF'] );
        // header('Location: '. $_SERVER['PHP_SELF']);
        // header("Location: http://$_SERVER[SERVER_NAME]" . substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], "/")) . "/");
        // echo '<META http-equiv="refresh" content="1; URL=' . $_SERVER['PHP_SELF'] . '">';
       
        //---------------------- script runs by cron
        // file_get_contents('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
        // curl_header_refresh('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
        // curl_file_get_contents('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);
       
    }
   
    function format_bytes($bytes, $precision = 2)
    {
        $units                 = array('B', 'KB', 'MB', 'GB', 'TB');
        $bytes                 = max($bytes, 0);
        $pow                 = floor(($bytes ? log($bytes) : 0) / log(1024));
        $pow                 = min($pow, count($units) - 1);
        $bytes                 /= pow(1024, $pow);
        return round($bytes, $precision) . ' ' . $units[$pow];
    }
   
    function find_last_day($anyDate)
    {
        //$anyDate            = '2009-08-25';                            // date format should be yyyy-mm-dd
        list($yr,$mn,$dt)    = split('-',$anyDate);                   
        $timeStamp            = mktime(0,0,0,$mn,1,$yr);               
        list($y,$m,$t)        = split('-',date('Y-m-t',$timeStamp));   
        $lastDayTimeStamp    = mktime(0,0,0,$m,$t,$y);               
        $lastDay            = date('d',$lastDayTimeStamp);           
        return $lastDay;
    }
   
    function curl_header_refresh($url)
    {
        $curl_init            = curl_init();
        curl_setopt($curl_init, CURLOPT_URL, $url);
        curl_setopt($curl_init, CURLOPT_HEADER, 0);
        $contents             = curl_exec($curl_init);
        curl_close($curl_init);
        return $contents;
    }
   
    function curl_file_get_contents($url)
    {
        $curl_init            = curl_init();                                            
        curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_init, CURLOPT_URL, $url);
        $contents             = curl_exec($curl_init);
        curl_close($curl_init);
        return $contents;
    }
   
?>


The Link
http://todd.be/notes/exporting-importing-magento-product-options
http://www.magentocommerce.com/boards/viewthread/153230/#t227283

Wednesday, February 23, 2011

Magento FConnect or magento facebook connect

Hear is the link for magento fconnect or magento facebook connect works gr8
smile
http://inchoo.net/ecommerce/magento/facebook-connect-magento-extension/

Php File upload for ckeditor 3.X

its good
http://download.cksource.com/CKFinder/CKFinder%20for%20PHP/2.0.2/ckfinder_php_2.0.2.zip

Magento insert update edit CRUD operations for dummies and beginers


Todo module for dummies: from the horse’s mouth :)

Some information  before we stare at the code:

Magento Path: phtmlàBlockàHelper->IndexControlleràphtml(updated)
Module naming: Modulename/Packagename e.g Shiva/Todo

Important  Folder info:

Adminhtml:contains config for admin menus like edit, grid, form, tab etc...

Block:contains front end outputs i.e the actions like getPostAction, editAction, prepareLayout etc..

Controllers:for interacting with the modules 

eg. indexcontroller : the action associated with the form like createAction, editAction, deleteAction

Adminhtml/XXXController : action for the backend like save edit,delete, massdelete etc... where XXX is your package name, here Todo

ETC contains the config directory
            config.xml default setting for module
            system.xml admininistration settings and admin form option ofr modules
Helper: helpers for module, covering areas such as translation e.g create, delete, edit functions are here Data.php

Model
Mysql4: (The top ORM model directory)---database related activity
 
Sql:mysql4-install-0.1.0.php contains database tables information required

Creating folder structure
To start with creating a Todo application in magento we need to build a module that becomes a part of the site that has its own URL. We will be creating our own module that will run on \todo URL structure.
Create the following folders in local directory [app\code\local\]
/Shiva
            /Todo
                        /Block
                        /etc
                        /Helper
/Model
/sql
/controllers
In etc/modules create Shiva_Todo.xml
etc/modules
- Shiva_Todo.xml
Tell magento that the new module is here
<?xml version="1.0"?>
<config>
    <modules>
        <Shiva_Todo>
            <active>true</active>
            <codePool>local</codePool>
        </Shiva_Todo>
    </modules>
</config>

Clear the cache and check in site->admin->system->Configuration->Advanced

Check if the module is “Enabled”.

For the frontend part

Your todo form is located in your theme
Path:-app\design\frontend\base_new\default\template\Todo\todo.phtml

Check the code in file
To view the formpage in address bar, check the form name front end by going to : Shiva/Todo/etc/config.xml and check the <frontend> tag

The form will be displayed as ‘todo’ as the name given in < frontName> tag

Some more file info…..

Helper class:

\Shiva\Todo\Helper

Helper:helpers for module, covering areas such as translation e.g create, delete, edit functions are here Data.php

Create a php file named Data.php

That file has a helper class named Shiva_Todo_Helper_Data in [Shiva/Todo/Helper/Data.php]

Check the code.

If you come across the code [‘todo/index/create’] it simply means in the todo module it has an Index controller and Create action [module / controller / action].

Check the necessary functions to get the id and perform respective actions for create, edit and delete.
Block class:
\Shiva\Todo\Block

Block: In Todo.php the class extend the template in the class and define the post Url actions
Grid.php
Location: \Adminhtml\Todo\Grid.php->define the display gird functions here

Edit.php
Location : \Adminhtml\Todo\ -> define the edit functions here
……..

Controllers:

IndexController.php
Location : \Shiva\Todo\controllers -> defines the actions here which are defined in Block and triggered from helper class
Admin side:
TodoController.php
Location : Shiva\Todo\controllers\Adminhtmlà for the admin actions like edit save etc….
Frontend part:
feedback.xml
Location: app\design\adminhtml\base_new\default\layout\Todo.xml
feedback.phtml
app\design\frontend\base_new\default\template\Todo\Todo.phtml
Do not forget to create a table name ‘todo’ in database
Form actions…………
To  create a record :
The flow

Magento Path: phtml-->Block-->Helper->IndexController-->phtml(updated)

Todo Path: todo.phtml---(on submit)--->(Todo.php)Block---$id-->Data.php)Helper->(indexcontroller.php)IndexController---redirect-->phtml(updated
)

Check the code in todo.phtml,Todo.php,Data.php,Indexcontroller.php
The screen shots:












Develop your custom module from the scratch with this demo and learn Magento
Download the source code here
The code shared is purely for education purpose only
# If you find any misleading info then tell us else tell others.





Monday, February 21, 2011

Writing a Custom Magento Module

The aim of this tutorial is to provide a good reference guide to keep with you when developing custom modules with ecommerce solution Magento. It details some of the key sections of how to build a custom Magento module, some of the concepts, assumptions and key points that that you are likely to encounter on a regular basis when building ecommerce installations with this software.

While Magento does not have a great reputation for documentation, there are some good resources available, including the Magento Wiki and php|architect’s Guide to Programming Magento. I used both of these when working for Session Digital as part of Ibuildings and I highly recommend them to you, along with the Magento Forums. Looking through these splintered resources can be time-consuming so in this article I give you the key details when building a module in Magento.

To accompany this article, you will find at the end of the page series of recommended resource links, and a download containing the example module that is built in the examples shown, available in a variety of formats. Hopefully these will help you continue to develop successfully with Magento!
The Magento Way

For those, seasoned in customising or maintaining other people’s software, you know that each has its own approach to things. You may not always agree with it or understand the logic behind it, but if you are going to work with it, you need to know what it is unless you want to go grey prematurely!

In this vein, before you make your next, or first, Magento module, it is helpful to understand some key concepts in how Magento is constructed. Magento is based on the Zend Framework and follows a simple code discovery, or resolution order, when compiling. There are three directories in which it finds code. These are core, community and local. Firstly, the core code modules are considered. Secondly, if code in the community modules are found that supercede it, they are used. Finally if code is found in the local directory, that supercedes the community directory, this is used. Said another way, code modules that you develop and place in the local directory, have priority over community and the Magento core.

This has a number of benefits from a developmental point of view. Chief amongst these is to allow for a seamless upgrade path. You can develop new modules, integrate third-party modules from the community in to your install and know, with a reasonable amount of confidence, that when you upgrade to the next release of Magento your hard work will survive intact. So remember never to touch the core code as that can break, or severely hamper, future upgrades. Too often I see in the forums, complaints about upgrades breaking, yet references to changing or hacking core code.

This philosophy of extending existing functionality means that you can take a community module to achieve the majority of your requirements and then supplement the missing functionality with a local module. You get a core of the work and only update where it’s missing what your project requires. Working in this way keeps development time and costs down, which is always good!

If you keep these things in mind whilst developing, it will help provide a simpler, more hassle-free development process as well as a more maintainable end-product.
Directory Structure

We have covered the ethos of Magento so we shall start getting our hands dirty and cover the filesystem structure of a standard module, by creating a our own module. Let’s say that you want to spruce up your Magento shop by combining your Flickr feed of customers using your products on the front page of your site. What we will do in this tutorial is create a simple module that links to an existing Flickr account and render a configurable set of images.

To start us off we will assume the following:

    * Your Company: Widgets Inc
    * Your Module: FlickrFeed
    * You have a Flickr account with the accompanying API key

Key Points

The most important parts of a module are these:

    * app/code/local/
    * app/etc/modules/

In app/code/local you will store all your modules. In app/etc/modules, you store your module specification file. The specification file is essential as it tells Magento that your module exists, making it available to use and configure. The local directory is where you will start to build your module repository. Analagous to PHP namespaces, C++ includes, or a Java package, the local directory provides a clear, professional and consistent method of organising and distributing your modules.
Key Files

app/Mage.php This file cannot be underestimated when developing with Magento. It provides a series of static methods that make working with Magento much more straightforward. There is some debate about the merits of this approach, but when working with it, these utility methods make life simpler. Some common methods you can find here are:

Mage::log Access the system logger

Mage::getModel Access a system model object

Mage::getStoreConfig Access the system configuration settings
Standard Module Layout

All modules follow the same structure. I’ll illustrate it using the company and module outlined above:

WidgetsInc/FlickrFeed/
  Adminhtml (Contains  the configuration for the admin menus)
    Model
      Block (Contains front-end output)
        Admin
  controllers (Controllers for interacting with your module)
  etc (The main configuration directory)
    config.xml (Configuration options and default settings for your module)
    system.xml (Administration settings and admin form options for the module)
  Helper (Helpers for the module, covering areas such as translation)
    Data.php
  Model
    Mysql4 (The top ORM model directory)
     
    Observer.php (Observers able to respond to system events)
  sql
    _setup (Contains setup and update scripts for your module)

In order to build the FlickrFeed module, we’ll need to add a number of files. The following sections will demonstrate each of these in turn.
config.xml

This is the core config file for your module. A minimalist version is available in the example download which you can find at the end of this post. Now let’s take you through what it means.

Here is the core definition of your module.

    * version is essential as it links in with the install and upgrade scripts.
    * depends lists the dependencies of your module. If the dependencies here are not satisfied, as you would assume, your module will not be enabled.
    * codePool specifies in what resource your module is located.

As you can assume, if these aren’t satisfied, the module will not load.

The models section sets up the data source connections and defines how to refer to it.

The resources section provides access to the system read, write and setup functionality for the module. If this section is not present, your module will be able to do very little.

The adminhtml section links the module in to the admin functionality, such as forms and configuration setups.

The routers section sets up the routes so that you can access the module. Unsurprisingly, default set up the defaults for your module that can be accessed as required.
system.xml

This file allows you to define an admin form for setting the key options for the feed. If you have a look at the one contained in the sample module, you’ll see an element groups. These enble you to combine the fields to display in to logical groupings, which will be rendered in a fieldsets containing the form field elements.

After the groups, the next element provides the initialisation of the section and then under the element fields, you list the fields that are to be displayed in this grouping. Fields and fieldsets can be disabled by default, or specifically in the website and/or store through show_in_default, show_in_website and show_in_store. To auto-translate fields, list them in the attribute translate for each group or selection element.
Managing Model Information

Next we need to look at the model files that will all us to work retrieve information from the module. These files are:

Model
  Feed.php
  Mysql4
    Feed.php
    Feed/
      Collection/
      Collection.php

In Model/Mysql4/Feed.php is the main model class, extending the core model class, making all of its functionality available. Model/Mysql4/Feed/Collection.php overrides the core model collection class, as with Feed.php, makes all the collection functionality available.
Working with the Magento Data Model

Now for the following example, let’s assume that we have a table structure similar to the following:
Field     Type     Null     Key     Default     Extra
address_id     int(10) unsigned     NO     PRI     NULL     auto_increment
address1     varchar(50)     NO         NULL    
address2     varchar(50)     NO         NULL    
city_id     int(10) unsigned     YES         NULL    
state_county     varchar(100)     YES         –    
post_code     varchar(30)     NO         NULL    
country     varchar(50)     YES         uk    
pobox     varchar(50)     YES         –    
created_date     datetime     NO         NULL    
last_modified     datetime     NO         NULL    

During a normal interaction with your model, you’re likely to retrieve, persist (save/update) and delete information from that model. So let’s look at how you can do this with Magento.
Retrieving Data

// get a handle on all the available feeds
$collection = Mage::getModel('flickrfeed/feed')-&gt;getCollection();
if ($collection-&gt;count() &gt;= 1) {
    // store the retrieved feeds
    $feedOutput = array();
    // iterate over the retrieved retrieved collection
    foreach ($collection as $flickrFeedItem) {
        $feedOutput[] = $flickrFeedItem-&gt;getData();
    }
}

Filtering the information

Now what if you did not require all the available data at once? There are so many situations where you do not want every record available. Magento has plenty of ways of filtering data but for this example we’ll use: addFieldToFilter(<field_name>, <field_value>).

$collection = Mage::getModel('flickrfeed/feed')->getCollection()
                               ->addFieldToFilter('post_code', 'SE11');

After adding to our previous call to getCollection, we have filtered down our returned records to just those with a post_code value of SE11. With the fluid interface we can call this as many times as we need to in order to meet our filtering requirements.
Persisting Data

We will also need to save data and update records, so here is a simple code example to illustrate:

Mage::getModel('flickrfeed/feed')->setData($data)->save();

In this instance, you’re calling the setData method on your model, which itself extends Mage_Core_Model_Mysql4_Abstract. The $data variable will be an associative array where the keys are the names of the columns in which you want the data saved. Then, using the fluent interface, further call the save() method and the data will then be saved by your model. Now, like all OOP classes, you don’t need to accept the parent setData() and save() methods; you can override these should you wish to. It is likely that the default may be quite acceptable in the majority of cases however.
Deleting Data

Now, let’s look at deleting records. Have a look at the code snippet below.

// get a handle on all the available feeds
$collection = Mage::getModel('flickrfeed/feed')->getCollection();
if ($collection->count() &gt;= 1) {
    // iterate over the retrieved retrieved collection
    foreach ($collection as $flickrFeedItem) {
        try {
            $flickrFeedItem->delete();
        } catch (Exception $e) {
            // note that the item couldn't be deleted.
            Mage::log(
            sprintf("Couldn't delete record. [%s]", var_export($_item, TRUE)),
            Zend_Log::ERR
            );
        }
    }
}

This example shows a simple way to delete records. After retrieving a collection, iterate over it using foreach and call delete on each item. Assuming that the method does not throw an exception, your records are deleted. We can also make things easier by creating a method for bulk deletion of records. In the model class, in our case FlickrFeed_Model_Mysql4_Feed, add a deleteAll() method similar to below:

      public function deleteAll()
      {
          // note that the item couldn't be deleted.
          Mage::log("Attempting to clear all records", Zend_Log::INFO);
          // get a handle on all the available feeds
          $collection = Mage::getModel('flickrfeed/feed')-&gt;getCollection();
          // attempt to delete all of them
          // not the most ideal way.
          foreach ($collection as $item) {
              try {
                  $item-&gt;delete();
              } catch (Exception $e) {
                // note that the item couldn't be deleted.
                Mage::log(
                  sprintf("Couldn't delete record. [%s]", var_export($_item, TRUE)),
                  Zend_Log::ERR
                  );
              }
          }
      }

Now all you need to do is call:

Mage::getModel('flickrfeed/feed')->deleteAll();

Logging

Since delete() can throw an exception if something goes wrong, this is a good time to show another Magento utility method: log. As you would assume, this gives quick access to the system log. Just pass in the message that you want logged and the log level, exactly as you would when working with Zend_Log, upon which this is based. Mage has a number of other methods, including getBaseUrl(), getStoreConfig(), getVersion() and dispatchEvent() that I highly encourage you to review.
Installing and Upgrading Modules

As mentioned earlier, one of the key aspects of modules is the value of version in /etc/config.xml. Whenever Magento looks at your module this value is checked; usually when you look at Admin -> system -> configuration -> advanced and click ‘Save Config‘.

The version value is compared against the value of core_resource.version in the database, where core_resource.code matches the name of your module. If the value in the file is higher than in the table column, then Magento looks for files in sql/flickrfeed_setup/ with a filename being equal to or higher than that of that found in config.xml.

If you want to re-run setup/upgrade routines during testing or migration, simply remove references to your modules in core_resource and then, in ‘advanced‘, click ‘Save Config‘ again. Magento will run all your scripts again.

We can now create the files for managing install and updates. Under your module, create the following file structure:

sql

  flickrfeed_setup

  mysql4-install-0.1.0.php

At this point you have the basis of your module. It will install and you will be able to call it!
Automation with Cron

Magento honours the tradition of using tools to use jobs so we you don’t have to by linking to Cron on UNIX based systems. For those unfamiliar with this utility, it is a UNIX daemon that wakes every minute, checks if it is time to run a job, if so, spawns them, and returns to sleep, waking a minute later to run all over again.

To get access to this automation in Magento, have a look at the snippet from config.xml listed below.

<crontab>
  <jobs>
    <publish_product_photos>
      <!-- run every 2 minutes for testing purposes -->
      <schedule><cron_expr>*/2 * * * *</cron_expr></schedule>
      <run><model>flickrfeed/observer::publishProductPhotos</model></run>
    </publish_product_photos >
  </jobs>
</crontab>

Place this snippet inside the root level of config.xml. This will cause flickrfeed/observer::publishProductPhotos to be executed every two minutes. For maintenance sake, we’ve labeled the task publish_product_photos. Inside that module, you can put in any Magento/PHP code that you need to automate. You can imagine that in this function, you could be performing actions such as checking for product photos that have not been published and publishing them.
Observing Application Events

Working with Magento can involve working with a lot of other code, whether that’s the Magento core, or third-party modules. To extend this is not always feasible and can be unecessary, leading to increased development overhead. Why not save your time and development budgets by simply listening for the events occurring and respond when they occur? Your module has the benefits of being self-contained and extremely loosely coupled. You are not necessarily bound to make changes when the module maintainer, or Varien, updates their code.

Now, let’s look at how we would observe an event. Magento calls events via the Mage::dispatchEvent. Given that there is not a comprehensively documented events list, here’s a few examples.

/app/code/core/mage/

    * ./Sales/Model/Order/Invoice.php:
          o sales_order_invoice_pay
          o sales_order_invoice_cancel
    * ./Sales/Model/Convert/Quote
          o sales_convert_quote_to_order
          o sales_convert_quote_address_to_order
          o sales_convert_quote_address_to_order_address
          o sales_convert_quote_payment_to_order_payment
          o sales_convert_quote_item_to_order_item
    * ./Sales/Model/Order.php
          o sales_order_place_after
    * ./Catalog/Model/Convert/Adapter/Product.php
          o catalog_product_import_after

To find a comprehensive list, you can run the following from the shell:

grep -rn Mage::dispatchEvent app/code/core/* --include='*.php'

You could refine this to make the output simpler to read if you wanted to. Alternatively, just search for Mage::dispatchEvent in your favourite editor. In our example we can observe catalog_product_import_after. Below is a snippet from config.xml.

<events>
  <!--listen for the product save action-->
  <catalog_product_import_after>
    <observers>
      <update_flickr_feed>
        <type>model</type>
        <class>FlickrFeed_Model_Observer</class>
        <method>publishCategoryPhotos</method>
      </update_flickr_feed>
    </observers>
  </catalog_product_import_after>
</events>

Put this section inside config -> adminhtml in /etc/config.xml of your module. When the catalog_product_import_after event is called, the system will call FlickrFeed::publishCategoryPhotos. What could be simpler? In your module’s method, you can put any valid Magento/PHP code that suits your needs. In this case, you could use access to the FlickrApi to publish a selection of photos from the latest category that you’ve just added.

All you need to do is determine the event you want to listen for, determine if there’s a suitable system event thrown and setup your module in the above fashion.
Deployment

When you are ready to deploy the module from your local development to your production server, just take the module directory you have built and copy it to the same directory on your production server. One word of caution, if you’re moving from one version of Magento to another, or from Community Edition to Enterprise, you may encounter some issues. So to minimise your deployment issues, aim to use for your development platform an edition and version of Magento as close as possible to your live version.
Download the Sample Code

We provide for you a zip file containing the FlickrFeed Codefor you to download and try out if you would like to.

Magento Connect

Provided by Magento, Magento Connect is where you can find a large and diverse range of modules, or extensions, to Magento. They allow you to quickly and simply extend your installation with new payment gateways, Google Maps integration and a lot more. Modules come in two flavours, free or commercially paid for, with varying support options available.

There is not much difference between the two formats. The modules are all hosted on magento-connect, but when a module is offered commercially, the seller, not Varien, handles payment. A magento-connect account is required, through which you can manage the details of the module.