Customizing Magento Module with Event-Observer

1. About the Module :
This module is for media sync when deploying magento as distributed application
Module located at : app/code/local/Kim/Syncmedia/
There are only 4 files list below to make that all :

2. app/code/local/Kim/Syncmedia/controllers/indexController.php

[codesyntax lang=”php”]

class Kim_Syncmedia_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $params = $this->getRequest()->getParams();
        $model = Mage::getModel('syncmedia/observer');
        var_dump($params);
        var_dump($model);
        exit();
    }
}

[/codesyntax]

3. app/code/local/Kim/Syncmedia/etc/config.xml
[codesyntax lang=”xml”]

<?xml version="1.0"?>
<config>

    <!-- base configurations -->
    <modules>
        <Kim_Syncmedia>
            <!-- module version -->
            <version>0.1.0</version>
        </Kim_Syncmedia>
    </modules>

    <!-- global configurations -->
    <global>
        <!-- enable models and resources -->
        <models>
            <syncmedia>
                <class>Kim_Syncmedia_Model</class>
                <!-- <resourceModel>weblog_mysql4</resourceModel> -->
            </syncmedia>
        </models>

        <!-- events hooks -->
        <events>
            <cms_page_save_after>
                <observers>
                    <kim_catalog_observer>
                        <type>singleton</type>
                        <class>syncmedia/observer</class>
                        <method>sync_cms_image</method>
                    </kim_catalog_observer>
                </observers>
            </cms_page_save_after>

            <catalog_category_save_after>
                <observers>
                    <kim_catalog_observer>
                        <type>singleton</type>
                        <class>syncmedia/observer</class>
                        <method>sync_category_image</method>
                    </kim_catalog_observer>
                </observers>
            </catalog_category_save_after>

            <catalog_product_save_after>
                <observers>
                    <kim_catalog_observer>
                        <type>singleton</type>
                        <class>syncmedia/observer</class>
                        <method>sync_product_image</method>
                    </kim_catalog_observer>
                </observers>
            </catalog_product_save_after>
        </events>
    </global>

    <!-- frontend configurations -->
    <frontend>
        <routers>
            <syncmedia>
                <!-- module route -->
                <use>standard</use>
                <args>
                    <module>Kim_Syncmedia</module>
                    <frontName>syncmedia</frontName>
                </args>
            </syncmedia>
        </routers>
    </frontend>

[/codesyntax]

4. app/code/local/Kim/Syncmedia/Model/Observer.php

[codesyntax lang=”php”]

class Kim_Syncmedia_Model_Observer
{
    protected function _construct()
    {
        $this->_init('syncmedia/observer');
    }

    public function sync_cms_image($observer)
    {
        system('cp -Ru /data/vhosts/magento/public_html/media/*[.a-z] /data/vhosts/magento2/public_html/media/ &');
        system('chmod -R g+w /data/vhosts/magento2/public_html/media/ &');
        return $this;
    }

    public function sync_product_image($observer)
    {
        system('cp -Ru /ddata/vhosts/magento/public_html/media/catalog/product/* /ddata/vhosts/magento2/public_html/media/catalog/product/ &');
        system('chmod -R g+w /data/vhosts/magento2/public_html/media/catalog/product/ &');
        return $this;
    }

    public function sync_category_image($observer)
    {
        system('cp -Ru /data/vhosts/magento/public_html/media/catalog/category/* /data/vhosts/magento2/public_html/media/catalog/category/ &');
        system('chmod -R g+w /data/vhosts/magento2/public_html/media/catalog/category/ &');
        return $this;
    }

}

[/codesyntax]

5. Finally you need to enable the module with : app/etc/modules/Kim_Syncmedia.xml

[codesyntax lang=”xml”]

<?xml version="1.0"?>
<config>
    <modules>
        <Kim_Syncmedia>
            <active>true</active>
            <codePool>local</codePool>
        </Kim_Syncmedia>
    </modules>
</config>

[/codesyntax]

Posted in Magento | Tagged | 2 Comments