PHP Magento Tips

Add Festival Background Image In Magento

Rate this posting:
{[['']]}

Upcoming Holiday season a Christmas, is one of the most popular Holiday season in U.S. Country. Many Ecommerce Store owners want to set some Catchy festival background for their store. In this article you will find how to set festival background image for different date in your Magento store.

Step 1: Create Extension Initialization file. Create file in app/etc/modules/Vijay_Festivalbackgroundimage.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Vijay_Festivalbackgroundimage>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Core />
             </depends>
        </Vijay_Festivalbackgroundimage>
    </modules>
</config>

Step 2: Create Extension Config.xml file. Create file in app/code/community/Vijay/Festivalbackgroundimage/etc/config.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Vijay_Festivalbackgroundimage>
            <version>1.0.0</version>
        </Vijay_Festivalbackgroundimage>
    </modules>
    <frontend>
        <routers>
            <vijay_festivalbackgroundimage>
                <use>standard</use>
                <args>
                    <module>Vijay_Festivalbackgroundimage</module>
                    <frontName>vijay_festivalbackgroundimage</frontName>
                </args>
            </vijay_festivalbackgroundimage>
        </routers>
        <layout>
            <updates>
                <vijay_festivalbackgroundimage>
                    <file>vijay_festivalbackgroundimage.xml</file>
                </vijay_festivalbackgroundimage>
            </updates>
        </layout>
        <translate>
            <modules>
                <Vijay_Festivalbackgroundimage>
                    <files>
                        <default>Vijay_Festivalbackgroundimage.csv</default>
                    </files>
                </Vijay_Festivalbackgroundimage>
            </modules>
        </translate>
    </frontend>
    <global>
        <resources>
            <vijay_festivalbackgroundimage_setup>
                <setup>
                    <module>Vijay_Festivalbackgroundimage</module>
                    <class>Vijay_Festivalbackgroundimage_Model_Resource_Setup</class>
                </setup>
            </vijay_festivalbackgroundimage_setup>
        </resources>
        <blocks>
            <vijay_festivalbackgroundimage>
                <class>Vijay_Festivalbackgroundimage_Block</class>
            </vijay_festivalbackgroundimage>
        </blocks>
        <helpers>
            <vijay_festivalbackgroundimage>
                <class>Vijay_Festivalbackgroundimage_Helper</class>
            </vijay_festivalbackgroundimage>
        </helpers>
        <models>
            <vijay_festivalbackgroundimage>
                <class>Vijay_Festivalbackgroundimage_Model</class>
                <resourceModel>vijay_festivalbackgroundimage_resource</resourceModel>
            </vijay_festivalbackgroundimage>
            <vijay_festivalbackgroundimage_resource>
                <class>Vijay_Festivalbackgroundimage_Model_Resource</class>
                <entities>
                    <festivalbackground>
                        <table>vijay_festivalbackgroundimage_festivalbackground</table>
                    </festivalbackground>
                </entities>
            </vijay_festivalbackgroundimage_resource>
        </models>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <vijay_festivalbackgroundimage>
                    <file>vijay_festivalbackgroundimage.xml</file>
                </vijay_festivalbackgroundimage>
            </updates>
        </layout>
        <translate>
            <modules>
                <Vijay_Festivalbackgroundimage>
                    <files>
                        <default>Vijay_Festivalbackgroundimage.csv</default>
                    </files>
                </Vijay_Festivalbackgroundimage>
            </modules>
        </translate>
        <global_search>
            <festivalbackground>
                <class>vijay_festivalbackgroundimage/adminhtml_search_festivalbackground</class>
                <acl>vijay_festivalbackgroundimage</acl>
            </festivalbackground>
        </global_search>
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Vijay_Festivalbackgroundimage before="Mage_Adminhtml">Vijay_Festivalbackgroundimage_Adminhtml</Vijay_Festivalbackgroundimage>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <default>
        <vijay_festivalbackgroundimage>
            <general>
                <is_enabled>1</is_enabled>
            </general>
            <frontend>
                <includejquery>1</includejquery>
            </frontend>
        </vijay_festivalbackgroundimage>
    </default>
</config>

Step 3: Create system.xml file. Create file in app/code/community/Vijay/Festivalbackgroundimage/etc/system.xml and add below code in it.

<?xml version="1.0"?>
<config>
 <tabs>
  <festivalbackground translate="label" module="vijay_festivalbackgroundimage">
   <label>Festival Bacground</label>
   <sort_order>320</sort_order>
  </festivalbackground>
 </tabs>
 <sections>
  <vijay_festivalbackgroundimage translate="label" module="vijay_festivalbackgroundimage">
   <label>Festival Bacground Image / Color</label>
   <tab>festivalbackground</tab>
   <frontend_type>text</frontend_type>
   <sort_order>150</sort_order>
   <show_in_default>1</show_in_default>
   <show_in_website>1</show_in_website>
   <show_in_store>1</show_in_store>
   <groups>
    <general translate="label">
     <label>Festival Bacground Settings</label>
     <frontend_type>text</frontend_type>
     <sort_order>10</sort_order>
     <show_in_default>1</show_in_default>
     <show_in_website>1</show_in_website>
     <show_in_store>1</show_in_store>
     <fields>
      <is_enabled translate="label">
       <label>Is Enabled</label>
       <frontend_type>select</frontend_type>
       <source_model>adminhtml/system_config_source_yesno</source_model>
       <sort_order>10</sort_order>
       <show_in_default>1</show_in_default>
       <show_in_website>1</show_in_website>
       <show_in_store>1</show_in_store>
      </is_enabled>
     </fields>
    </general>
    <frontend translate="label">
     <label>Frontend Settings</label>
     <frontend_type>text</frontend_type>
     <sort_order>20</sort_order>
     <show_in_default>1</show_in_default>
     <show_in_website>1</show_in_website>
     <show_in_store>1</show_in_store>
     <fields>
      <includejquery translate="label">
       <label>Include Jquery ?</label>
       <frontend_type>select</frontend_type>
       <source_model>adminhtml/system_config_source_yesno</source_model>
       <sort_order>20</sort_order>
       <show_in_default>1</show_in_default>
       <show_in_website>0</show_in_website>
       <show_in_store>0</show_in_store>
       <depends>
        <is_enabled>
         <fieldset>general</fieldset>
         <value>1</value>
        </is_enabled>
       </depends>
      </includejquery>
     </fields>
    </frontend>
   </groups>
  </vijay_festivalbackgroundimage>
 </sections>
</config>

Step 4: Create adminhtml.xml file. Create file in app/code/community/Vijay/Festivalbackgroundimage/etc/adminhtml.xml and add below code in it.

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <vijay_festivalbackgroundimage translate="title" module="vijay_festivalbackgroundimage">
                                        <title>Festivalbackgroundimage</title>
                                    </vijay_festivalbackgroundimage>
                                </children>
                            </config>
                        </children>
                    </system>
                    <vijay_festivalbackgroundimage translate="title" module="vijay_festivalbackgroundimage">
                        <title>Festival Background</title>
                        <children>
                            <festivalbackground translate="title" module="vijay_festivalbackgroundimage">
                                <title>Festivalbackground</title>
                                <sort_order>0</sort_order>
                            </festivalbackground>
                        </children>
                    </vijay_festivalbackgroundimage>
                </children>
            </admin>
        </resources>
    </acl>
    <menu>
        <vijay_festivalbackgroundimage translate="title" module="vijay_festivalbackgroundimage">
            <title>Festival Background</title>
            <sort_order>75</sort_order>
            <children>
                <festivalbackground translate="title" module="vijay_festivalbackgroundimage">
                    <title>Festivalbackground</title>
                    <action>adminhtml/festivalbackgroundimage_festivalbackground</action>
                    <sort_order>0</sort_order>
                </festivalbackground>
            </children>
        </vijay_festivalbackgroundimage>
    </menu>
</config>

Step 5: Create app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Festivalbackground.php file and add below code in it.

<?php
class Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        $this->_controller         = 'adminhtml_festivalbackground';
        $this->_blockGroup         = 'vijay_festivalbackgroundimage';
        parent::__construct();
        $this->_headerText         = Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground');
        $this->_updateButton('add', 'label', Mage::helper('vijay_festivalbackgroundimage')->__('Add Festivalbackground'));

    }
}


Step 6: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Festivalbackground/Edit.php and add below code in it.

Here, i added JavaScript File in _prepareLayout function. Here, first check my background type and added different JavaScript file according to layout type.

<?php

class Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        parent::__construct();
        $this->_blockGroup = 'vijay_festivalbackgroundimage';
        $this->_controller = 'adminhtml_festivalbackground';
        $this->_updateButton(
            'save',
            'label',
            Mage::helper('vijay_festivalbackgroundimage')->__('Save Festivalbackground')
        );
        $this->_updateButton(
            'delete',
            'label',
            Mage::helper('vijay_festivalbackgroundimage')->__('Delete Festivalbackground')
        );
        $this->_addButton(
            'saveandcontinue',
            array(
                'label'   => Mage::helper('vijay_festivalbackgroundimage')->__('Save And Continue Edit'),
                'onclick' => 'saveAndContinueEdit()',
                'class'   => 'save',
            ),
            -100
        );
        $this->_formScripts[] = "
            function saveAndContinueEdit() {
                editForm.submit($('edit_form').action+'back/edit/');
            }
        ";
    }
    protected function _prepareLayout() {
     if (Mage::registry('current_festivalbackground') && Mage::registry('current_festivalbackground')->getId()) {
      $type = Mage::registry('current_festivalbackground')->getType();
      if ($type == 1){
       $this->getLayout()->getBlock('head')->addJs('festivalbackgroundimage/customimage.js');
      } elseif ($type == 2){
       $this->getLayout()->getBlock('head')->addJs('festivalbackgroundimage/customcolor.js');
      } else {
       
      }
      
     } else {
      $this->getLayout()->getBlock('head')->addJs('festivalbackgroundimage/custom.js');
     }
     
     return parent::_prepareLayout();
    }

    /**
     * get the edit form header
     *
     * @access public
     * @return string
     * @author Ultimate Module Creator
     */
    public function getHeaderText()
    {
        if (Mage::registry('current_festivalbackground') && Mage::registry('current_festivalbackground')->getId()) {
            return Mage::helper('vijay_festivalbackgroundimage')->__(
                "Edit Festivalbackground '%s'",
                $this->escapeHtml(Mage::registry('current_festivalbackground')->getFestivalname())
            );
        } else {
            return Mage::helper('vijay_festivalbackgroundimage')->__('Add Festivalbackground');
        }
    }
}

Step 7: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Festivalbackground/Grid.php and add below code in it. Here, Check renderer for background type and its value. We will get its value by creating custom function.

<?php
class Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    /**
     * constructor
     *
     * @access public
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        parent::__construct();
        $this->setId('festivalbackgroundGrid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

    /**
     * prepare collection
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Grid
     * @author Ultimate Module Creator
     */
    protected function _prepareCollection()
    {
        $collection = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground')
            ->getCollection();
        
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    /**
     * prepare grid collection
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Grid
     * @author Ultimate Module Creator
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Id'),
                'index'  => 'entity_id',
                'type'   => 'number'
            )
        );
        $this->addColumn(
            'festivalname',
            array(
                'header'    => Mage::helper('vijay_festivalbackgroundimage')->__('Festival Name'),
                'align'     => 'left',
                'index'     => 'festivalname',
            )
        );
        
        $this->addColumn(
            'status',
            array(
                'header'  => Mage::helper('vijay_festivalbackgroundimage')->__('Status'),
                'index'   => 'status',
                'type'    => 'options',
                'options' => array(
                    '1' => Mage::helper('vijay_festivalbackgroundimage')->__('Enabled'),
                    '0' => Mage::helper('vijay_festivalbackgroundimage')->__('Disabled'),
                )
            )
        );
        $this->addColumn(
            'startdate',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Festival Start Date'),
                'index'  => 'startdate',
                'type'=> 'date',

            )
        );
        $this->addColumn(
            'enddate',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Festival End Date'),
                'index'  => 'enddate',
                'type'=> 'date',

            )
        );
  $this->addColumn(
            'background_target',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Background Target'),
                'index'  => 'background_target',
                'type'  => 'options',
                'options' => Mage::helper('vijay_festivalbackgroundimage')->convertOptions(
                    Mage::getModel('vijay_festivalbackgroundimage/background_attribute_source_backgroundtarget')->getAllOptions(false)
                )

            )
        );
  $this->addColumn(
            'background_custom_target',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Custom CSS selector'),
                'index'  => 'background_custom_target',
                'type'=> 'text',

            )
        );
        $this->addColumn(
            'type',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Background Type'),
                'index'  => 'type',
             'align'     =>'left',
                'type'=> 'number',
             'renderer'  => 'Vijay_Festivalbackgroundimage_Block_Adminhtml_Renderer_Value',

            )
        );
        $this->addColumn(
            'background',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Background'),
             'align'     =>'left',
                'index'  => 'background',
                'type'=> 'text',
             'renderer'  => 'Vijay_Festivalbackgroundimage_Block_Adminhtml_Renderer_Backgroundimg',

            )
        );
        $this->addColumn(
            'created_at',
            array(
                'header' => Mage::helper('vijay_festivalbackgroundimage')->__('Created at'),
                'index'  => 'created_at',
                'width'  => '120px',
                'type'   => 'datetime',
            )
        );
        $this->addColumn(
            'updated_at',
            array(
                'header'    => Mage::helper('vijay_festivalbackgroundimage')->__('Updated at'),
                'index'     => 'updated_at',
                'width'     => '120px',
                'type'      => 'datetime',
            )
        );
        $this->addColumn(
            'action',
            array(
                'header'  =>  Mage::helper('vijay_festivalbackgroundimage')->__('Action'),
                'width'   => '100',
                'type'    => 'action',
                'getter'  => 'getId',
                'actions' => array(
                    array(
                        'caption' => Mage::helper('vijay_festivalbackgroundimage')->__('Edit'),
                        'url'     => array('base'=> '*/*/edit'),
                        'field'   => 'id'
                    )
                ),
                'filter'    => false,
                'is_system' => true,
                'sortable'  => false,
            )
        );
        $this->addExportType('*/*/exportCsv', Mage::helper('vijay_festivalbackgroundimage')->__('CSV'));
        $this->addExportType('*/*/exportExcel', Mage::helper('vijay_festivalbackgroundimage')->__('Excel'));
        $this->addExportType('*/*/exportXml', Mage::helper('vijay_festivalbackgroundimage')->__('XML'));
        return parent::_prepareColumns();
    }

    /**
     * prepare mass action
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Grid
     * @author Ultimate Module Creator
     */
    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('festivalbackground');
        $this->getMassactionBlock()->addItem(
            'delete',
            array(
                'label'=> Mage::helper('vijay_festivalbackgroundimage')->__('Delete'),
                'url'  => $this->getUrl('*/*/massDelete'),
                'confirm'  => Mage::helper('vijay_festivalbackgroundimage')->__('Are you sure?')
            )
        );
        $this->getMassactionBlock()->addItem(
            'status',
            array(
                'label'      => Mage::helper('vijay_festivalbackgroundimage')->__('Change status'),
                'url'        => $this->getUrl('*/*/massStatus', array('_current'=>true)),
                'additional' => array(
                    'status' => array(
                        'name'   => 'status',
                        'type'   => 'select',
                        'class'  => 'required-entry',
                        'label'  => Mage::helper('vijay_festivalbackgroundimage')->__('Status'),
                        'values' => array(
                            '1' => Mage::helper('vijay_festivalbackgroundimage')->__('Enabled'),
                            '0' => Mage::helper('vijay_festivalbackgroundimage')->__('Disabled'),
                        )
                    )
                )
            )
        );
        return $this;
    }

    /**
     * get the row url
     *
     * @access public
     * @param Vijay_Festivalbackgroundimage_Model_Festivalbackground
     * @return string
     * @author Ultimate Module Creator
     */
    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }

    /**
     * get the grid url
     *
     * @access public
     * @return string
     * @author Ultimate Module Creator
     */
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }

    /**
     * after collection load
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Grid
     * @author Ultimate Module Creator
     */
    protected function _afterLoadCollection()
    {
        $this->getCollection()->walk('afterLoad');
        parent::_afterLoadCollection();
    }
}

Step 8: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Renderer/Backgroundimg.php and add below code in it.

<?php
class Vijay_Festivalbackgroundimage_Block_Adminhtml_Renderer_Backgroundimg extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{ 
 public function render(Varien_Object $row) 
 {
  $data =  $row->getData($this->getColumn()->getIndex());
  $id=$row->getData();
  if($id['type']==1)
  {
          $value='<center>  <img src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'/festivalbackgroundimage/'.$data.'" width="150" height="100" /> <br> <a href="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'/festivalbackgroundimage/'.$data.'" class="various" > Show Photo </a></center>';
  return $value;        
  }
  
  else
  {
   $value='<div><div style="background-color:'.$id['background'].';width:15px;height:15px;float:left"></div>&nbsp;&nbsp;'.$id['background'].'</div>';
      return $value;
  }
  
 }
}


Step 9: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Renderer/Value.php and add below code in it.

<?php
class Vijay_Festivalbackgroundimage_Block_Adminhtml_Renderer_Value extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
        public function render(Varien_Object $row) 
 {
         $data =  $row->getData($this->getColumn()->getIndex());
         $id=$row->getData();
         
         if($id['type']==1)
         {
                 return "Image";      
         }
         
         else
         {
                 return "Color";
         }
         
 }
}

Step 10: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Festivalbackground/Edit/Form.php and add below code in it.

<?php
class Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * prepare form
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit_Form
     * @author Ultimate Module Creator
     */
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(
            array(
                'id'         => 'edit_form',
                'action'     => $this->getUrl(
                    '*/*/save',
                    array(
                        'id' => $this->getRequest()->getParam('id')
                    )
                ),
                'method'     => 'post',
                'enctype'    => 'multipart/form-data'
            )
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}


Step 11: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Festivalbackground/Edit/Tabs.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    /**
     * Initialize Tabs
     *
     * @access public
     * @author Ultimate Module Creator
     */
    public function __construct()
    {
        parent::__construct();
        $this->setId('festivalbackground_tabs');
        $this->setDestElementId('edit_form');
        $this->setTitle(Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground'));
    }

    /**
     * before render html
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit_Tabs
     * @author Ultimate Module Creator
     */
    protected function _beforeToHtml()
    {
        $this->addTab(
            'form_festivalbackground',
            array(
                'label'   => Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground'),
                'title'   => Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground'),
                'content' => $this->getLayout()->createBlock(
                    'vijay_festivalbackgroundimage/adminhtml_festivalbackground_edit_tab_form'
                )
                ->toHtml(),
            )
        );
        return parent::_beforeToHtml();
    }

    /**
     * Retrieve festivalbackground entity
     *
     * @access public
     * @return Vijay_Festivalbackgroundimage_Model_Festivalbackground
     * @author Ultimate Module Creator
     */
    public function getFestivalbackground()
    {
        return Mage::registry('current_festivalbackground');
    }
}

Step 12: Create file in app/code/community/Vijay/Festivalbackgroundimage/Block/Adminhtml/Festivalbackground/Edit/Tab/Form.php and add below code in it.


<?php

class Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * prepare the form
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Block_Adminhtml_Festivalbackground_Edit_Tab_Form
     * @author Ultimate Module Creator
     */
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $form->setHtmlIdPrefix('festivalbackground_');
        $form->setFieldNameSuffix('festivalbackground');
        $this->setForm($form);
        $fieldset = $form->addFieldset(
            'festivalbackground_form',
            array('legend' => Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground'))
        );
  
        /* Custom Code Start */
        $URLID=$this->getRequest()->getParam('id');
        if (!empty($URLID) && $URLID != '') {
         $true = false;
        } else {
         $true = true;
        }
        $_edited_banner = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground')->load($URLID);
        $_edited_banner = ($_edited_banner->getdata());
        $type=$_edited_banner['type'];
        $bimg="";
        $bcolor="";
        if($type==1)
        {
         $bimg='<br/>&nbsp;<div style="padding-top:5px;padding-bottom:5px" id="imagetag"><img src="'.Mage::getBaseUrl('media') . 'festivalbackgroundimage'.DS.$_edited_banner['background'].'" width=250px height=250/></div>';
        }
        else
        {
         $bcolor='<br/>&nbsp;<div style="padding-top:5px;padding-bottom:5px;width:30px;height:30px; background-color:'.$_edited_banner['background'].'" id="colortag" ></div>';
        }
        /* Custom Code ends */
        
        $fieldset->addField(
            'festivalname',
            'text',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Festival Name'),
                'name'  => 'festivalname',
                'note' => $this->__('Enter Festival Name Here'),
                'required'  => true,
                'class' => 'required-entry',

           )
        );

        $fieldset->addField(
            'startdate',
            'date',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Festival Start Date'),
                'name'  => 'startdate',
                'note' => $this->__('Enter Festival Start Date Here'),
                'required'  => true,
                'class' => 'required-entry validate-date validate-date-range date-range-festivalbackground-from',

            'image' => $this->getSkinUrl('images/grid-cal.gif'),
            'format'  => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
           )
        );

        $fieldset->addField(
            'enddate',
            'date',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Festival End Date'),
                'name'  => 'enddate',
                'note' => $this->__('Enter Festival End Date Here'),
                'required'  => true,
                'class' => 'required-entry validate-date validate-date-range date-range-festivalbackground-to',

            'image' => $this->getSkinUrl('images/grid-cal.gif'),
            'format'  => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
           )
        );
  $typefield = $fieldset->addField(
            'background_target',
            'select',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Background Target'),
                'name'  => 'background_target',
                'required'  => true,
                'class' => 'required-entry',

                'values'=> Mage::getModel('vijay_festivalbackgroundimage/background_attribute_source_backgroundtarget')->getAllOptions(true),
                'onchange' => 'onchangeStyleShow(this.value)',
           )
        );
  $typefield->setAfterElementHtml("<script type=\"text/javascript\">
            function onchangeStyleShow(e){
                if (e == 'custom'){
     $('festivalbackground_background_custom_target').addClassName('required-entry');
                } else {
                $('festivalbackground_background_custom_target').removeClassName('required-entry');
                }
            }
        </script>");
  $fieldset->addField(
            'background_custom_target',
            'text',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Custom CSS selector'),
                'name'  => 'background_custom_target',
                'required'  => false,
                'note'=>'Input CSS selector, like .class or #id',
           )
        );
  $this->setChild(
  'form_after', 
  $this->getLayout()
   ->createBlock('adminhtml/widget_form_element_dependence')
   ->addFieldMap('festivalbackground_background_target', 'festivalbackground_background_target')
         ->addFieldMap('festivalbackground_background_custom_target', 'festivalbackground_background_custom_target')
         ->addFieldDependence('festivalbackground_background_custom_target', 'festivalbackground_background_target', 'custom')
  );
        /*$fieldset->addField(
            'type',
            'radios',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Background Type'),
                'name'  => 'type',
              'values' => array(
                array('id'=>'image','value'=>'1','label'=>'Image'),
                array('id'=>'color','value'=>'2','label'=>'Color'),
              ),
                'required'  => false,
                'class' => 'validate-one-required',

           )
        );*/
        
       $fieldset->addField('type', 'radios', array(
          'label'     => Mage::helper('vijay_festivalbackgroundimage')->__('Background Type'),
          'name'      => 'type',
    'onclick' => "",
    'onchange' => "",
    'value'  => '2',
    'values' => array(
         array('id'=>'image','value'=>'1','label'=>'Image'),
                array('id'=>'color','value'=>'2','label'=>'Color'),
          ),
    'disabled' => false,
    'readonly' => false,
    //'after_element_html' => '<small>Comments</small>',
    'tabindex' => 1,
    'class' => 'validate-one-required-by-name',
  ));
  
        $fieldset->addField('backgroundcolor', 'text', array(
          'label'     => Mage::helper('vijay_festivalbackgroundimage')->__('Background Color'),
          'name'      => 'backgroundcolor',
          'class'     => 'color {required:true, adjust:true, hash:true} validate-hex',
          'required'  => $true,
          'after_element_html' => $bcolor,
        ));
        
        
        $fieldset->addField('backgroundimage', 'file', array(
          'label'     => Mage::helper('vijay_festivalbackgroundimage')->__('Background Image'),
          'name'      => 'backgroundimage',
          'required'  => $true,
          'after_element_html' =>$bimg,
        ));
        
        /*$fieldset->addField(
            'background',
            'text',
            array(
                'label' => Mage::helper('vijay_festivalbackgroundimage')->__('background'),
                'name'  => 'background',
                'required'  => true,
                'class' => 'required-entry',

           )
        );*/
        $fieldset->addField(
            'status',
            'select',
            array(
                'label'  => Mage::helper('vijay_festivalbackgroundimage')->__('Status'),
                'name'   => 'status',
                'values' => array(
                    array(
                        'value' => 1,
                        'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Enabled'),
                    ),
                    array(
                        'value' => 0,
                        'label' => Mage::helper('vijay_festivalbackgroundimage')->__('Disabled'),
                    ),
                ),
            )
        );
        
        $formValues = Mage::registry('current_festivalbackground')->getDefaultValues();
        if (!is_array($formValues)) {
            $formValues = array();
        }
        if (Mage::getSingleton('adminhtml/session')->getFestivalbackgroundData()) {
            $formValues = array_merge($formValues, Mage::getSingleton('adminhtml/session')->getFestivalbackgroundData());
            Mage::getSingleton('adminhtml/session')->setFestivalbackgroundData(null);
        } elseif (Mage::registry('current_festivalbackground')) {
            $formValues = array_merge($formValues, Mage::registry('current_festivalbackground')->getData());
        }
        $form->setValues($formValues);
        return parent::_prepareForm();
    }
}

Step 13: Create file in app/code/community/Vijay/Festivalbackgroundimage/Controller/Adminhtml/Festivalbackgroundimage.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Controller_Adminhtml_Festivalbackgroundimage extends Mage_Adminhtml_Controller_Action
{
    /**
     * upload file and get the uploaded name
     *
     * @access public
     * @param string $input
     * @param string $destinationFolder
     * @param array $data
     * @return string
     * @author Ultimate Module Creator
     */
    protected function _uploadAndGetName($input, $destinationFolder, $data)
    {
        try {
            if (isset($data[$input]['delete'])) {
                return '';
            } else {
                $uploader = new Varien_File_Uploader($input);
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                $uploader->setAllowCreateFolders(true);
                $result = $uploader->save($destinationFolder);
                return $result['file'];
            }
        } catch (Exception $e) {
            if ($e->getCode() != Varien_File_Uploader::TMP_NAME_EMPTY) {
                throw $e;
            } else {
                if (isset($data[$input]['value'])) {
                    return $data[$input]['value'];
                }
            }
        }
        return '';
    }
}


Step 14: Create file in app/code/community/Vijay/Festivalbackgroundimage/controllers/Adminhtml/Festivalbackgroundimage/FestivalbackgroundController.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Adminhtml_Festivalbackgroundimage_FestivalbackgroundController extends Vijay_Festivalbackgroundimage_Controller_Adminhtml_Festivalbackgroundimage
{
    /**
     * init the festivalbackground
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Model_Festivalbackground
     */
    protected function _initFestivalbackground()
    {
        $festivalbackgroundId  = (int) $this->getRequest()->getParam('id');
        $festivalbackground    = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground');
        if ($festivalbackgroundId) {
            $festivalbackground->load($festivalbackgroundId);
        }
        Mage::register('current_festivalbackground', $festivalbackground);
        return $festivalbackground;
    }

    /**
     * default action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function indexAction()
    {
        $this->loadLayout();
        $this->_title(Mage::helper('vijay_festivalbackgroundimage')->__('Festival Background'))
             ->_title(Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackgrounds'));
        $this->renderLayout();
    }

    /**
     * grid action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function gridAction()
    {
        $this->loadLayout()->renderLayout();
    }

    /**
     * edit festivalbackground - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function editAction()
    {
        $festivalbackgroundId    = $this->getRequest()->getParam('id');
        $festivalbackground      = $this->_initFestivalbackground();
        if ($festivalbackgroundId && !$festivalbackground->getId()) {
            $this->_getSession()->addError(
                Mage::helper('vijay_festivalbackgroundimage')->__('This festivalbackground no longer exists.')
            );
            $this->_redirect('*/*/');
            return;
        }
        $data = Mage::getSingleton('adminhtml/session')->getFestivalbackgroundData(true);
        if (!empty($data)) {
            $festivalbackground->setData($data);
        }
        Mage::register('festivalbackground_data', $festivalbackground);
        $this->loadLayout();
        $this->_title(Mage::helper('vijay_festivalbackgroundimage')->__('Festival Background'))
             ->_title(Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackgrounds'));
        if ($festivalbackground->getId()) {
            $this->_title($festivalbackground->getFestivalname());
        } else {
            $this->_title(Mage::helper('vijay_festivalbackgroundimage')->__('Add festivalbackground'));
        }
        if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
            $this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
        }
        $this->renderLayout();
    }

    /**
     * new festivalbackground action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function newAction()
    {
        $this->_forward('edit');
    }

    /**
     * save festivalbackground - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function saveAction()
    {
     $id= $this->getRequest()->getParam('id');
      
        if ($data = $this->getRequest()->getPost('festivalbackground')) {
            try {
                $data = $this->_filterDates($data, array('startdate' ,'enddate'));     
    /* Custom Code Start */
    $postData = $this->getRequest()->getPost();
    
                $festivalbackgroundModel = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground');
             
                if($postData['type']==1)
                {
                    $background = time().$_FILES['festivalbackground']['name']['backgroundimage'];
                }
                else
                {
                 $background=$postData['festivalbackground']['backgroundcolor'];
                }
    
    if($postData['festivalbackground']['background_target']== 'custom')
                {
                    $backgroundcustomtarget = $postData['festivalbackground']['background_custom_target'];
                }
                else
                {
                 $backgroundcustomtarget='';
                }
    
     // Start date and enddate must be >= today
                $today= date("Y-m-d");
    
    if($data['startdate'] >= $today && $data['enddate'] >= $today)
                {
                 // Duplicate start date and enddate not allowed.... Code start... 
                 $collection = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground')->getCollection();
             $collection->getSelect()
             ->reset(Zend_Db_Select::COLUMNS)
             ->columns('startdate')
             ->columns('entity_id')
             ->columns('enddate');
       $collection=$collection->getData();
       foreach($collection as $row) {
         if($id!='' && $id==$row['entity_id'])
            {
             continue;
            }
      $dsd = strtotime($row['startdate']);
            $dsd = date('Y-m-d',$dsd);
            $ded = strtotime($row['enddate']);
            $ded = date('Y-m-d',$ded);
                        $psd = date('Y-m-d',strtotime($data['startdate']));
            $ped = date('Y-m-d',strtotime($data['enddate'])); 
      if((($psd >= $dsd && $psd<= $ded) || ($ped <= $ded && $ped >= $dsd)))
            {
             if($id!='' && $dsd==$psd && $ded==$ped) {
              
                } else {
                 Mage::getSingleton('adminhtml/session')->addError('Slot Booked..'); 
                   $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                   return false;
               }
            } elseif(($dsd >= $psd && $ded <= $ped)) {
                   if($id!='' && $dsd==$psd && $ded==$ped) {
                    
                   }
                   else {
                    Mage::getSingleton('adminhtml/session')->addError('Slot Booked..'); 
                       $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                       return false;
                   }
           } else {
            
           }
       }
       // Duplicate start date and enddate not allowed.... Code End...
       if(isset($_FILES['festivalbackground']['name']['backgroundimage']) && $_FILES['festivalbackground']['name']['backgroundimage'] != '') 
       {
        try {         
         if($this->getRequest()->getParam('id'))
                            {
                                $festivalbackgroundModel->load($this->getRequest()->getParam('id'));
                                if($festivalbackgroundModel->getImage())
                                {
                                 $this->removeRequiredImages($festivalbackgroundModel->getImage());
                                }
                            }
       /* Upload Image Code Start */
                            foreach($_FILES['festivalbackground']['name']  as $key =>$image){
       if (!empty($image)) {
        try {
         $filesize = $_FILES['festivalbackground']['size'][$key];
         $fileName = time().$_FILES['festivalbackground']['name'][$key];
                                 $uploader = new Varien_File_Uploader(array(
                                     'name' => $_FILES['festivalbackground']['name'][$key],
                                     'type' => $_FILES['festivalbackground']['type'][$key],
                                     'tmp_name' => $_FILES['festivalbackground']['tmp_name'][$key],
                                     'error' => $_FILES['festivalbackground']['error'][$key],
                                     'size' => $_FILES['festivalbackground']['size'][$key]
                                      ));
     
         $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'png'));
         $uploader->setAllowRenameFiles(false);
         $uploader->setFilesDispersion(false);
         $path = Mage::getBaseDir('media') . DS . 'festivalbackgroundimage';
         
         if(!is_dir($path))
                              {
                               mkdir($path, 0777, true);
                              }
         
         $id = $this->getRequest()->getParam('id'); 
         
         if ($id != '') {
          $remove = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground')->load($id);
                               $remove=$remove->getData();
                               $removeimg=$remove['background'];
         }
                              
        
         if (($filesize > 200000) && ($id == ''))
                               {
                                $uploader->save($path . DS, $fileName );
          
                               } elseif (($filesize > 200000) && ($id != '')){
                                $uploader->save($path . DS, $fileName );
                                //$this->removeFile($removeimg);
                                unlink($path . DS . $removeimg);
                               }
                               else
                               {
                                Mage::getSingleton('adminhtml/session')->addError("Image Size should be minimum 200 KB");
                                //$this->_redirect('*/*/');
                                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                                return;
                               }                           
                                     
        } catch (Exception $e) { 
                   Mage::log('Error in upload');                        
                  }
       }
             } 
                            /* End code for image upload */
         
        } catch (Exception $e)
           {
                            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                            //$this->_redirect('*/*/');
                            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                            return;
                        }
       }
    } else {
     Mage::getSingleton('adminhtml/session')->addError('Selected Date all ready passed');
                    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                    return; 
    }
    /* Custom Code Ends */
                $festivalbackground = $this->_initFestivalbackground();
    /* Set Custom Data Start*/
    
    if( $id =='') {
                    $data['background'] = $background;    
     $data['type'] = $postData['type'];                                
                } else {
                 $id = $this->getRequest()->getParam('id'); 
                 $remove = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground')->load($id);
                      $remove=$remove->getData();
                      $removeimg=$remove['background'];
                      //$this->removeFile($removeimg);  
                      $data['type'] = $postData['type'];
                      if ($postData['type'] == 2){
                       if (isset($postData['festivalbackground']['backgroundcolor']) && ($postData['festivalbackground']['backgroundcolor'] != '')){
                        $data['background'] = $background;
                       } else {
                        $data['background'] = $removeimg;
                       }
                       
                      } else {
                       if (isset($_FILES['festivalbackground']['name']['backgroundimage']) && $_FILES['festivalbackground']['name']['backgroundimage'] != ''){
                        $data['background'] = time().$_FILES['festivalbackground']['name']['backgroundimage'];
                        //unlink($path . DS . $removeimg);
                       } else {
                        $data['background'] = $removeimg;
                       }
                      }
                      
                      
      
                }
    
    $festivalbackground->setData('background_custom_target',$backgroundcustomtarget);
    /* Set Custom Data Ends*/
                $festivalbackground->addData($data);
                $festivalbackground->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground was successfully saved')
                );
                Mage::getSingleton('adminhtml/session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $festivalbackground->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFestivalbackgroundData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            } catch (Exception $e) {
                Mage::logException($e);
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('vijay_festivalbackgroundimage')->__('There was a problem saving the festivalbackground.')
                );
                Mage::getSingleton('adminhtml/session')->setFestivalbackgroundData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(
            Mage::helper('vijay_festivalbackgroundimage')->__('Unable to find festivalbackground to save.')
        );
        $this->_redirect('*/*/');
    }

    /**
     * delete festivalbackground - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function deleteAction()
    {
        if ( $this->getRequest()->getParam('id') > 0) {
            try {
                $festivalbackground = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground');
                $festivalbackground->setId($this->getRequest()->getParam('id'))->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground was successfully deleted.')
                );
                $this->_redirect('*/*/');
                return;
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('vijay_festivalbackgroundimage')->__('There was an error deleting festivalbackground.')
                );
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                Mage::logException($e);
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(
            Mage::helper('vijay_festivalbackgroundimage')->__('Could not find festivalbackground to delete.')
        );
        $this->_redirect('*/*/');
    }

    /**
     * mass delete festivalbackground - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function massDeleteAction()
    {
        $festivalbackgroundIds = $this->getRequest()->getParam('festivalbackground');
        if (!is_array($festivalbackgroundIds)) {
            Mage::getSingleton('adminhtml/session')->addError(
                Mage::helper('vijay_festivalbackgroundimage')->__('Please select festivalbackgrounds to delete.')
            );
        } else {
            try {
                foreach ($festivalbackgroundIds as $festivalbackgroundId) {
                    $festivalbackground = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground');
                    $festivalbackground->setId($festivalbackgroundId)->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('vijay_festivalbackgroundimage')->__('Total of %d festivalbackgrounds were successfully deleted.', count($festivalbackgroundIds))
                );
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('vijay_festivalbackgroundimage')->__('There was an error deleting festivalbackgrounds.')
                );
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }

    /**
     * mass status change - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function massStatusAction()
    {
        $festivalbackgroundIds = $this->getRequest()->getParam('festivalbackground');
        if (!is_array($festivalbackgroundIds)) {
            Mage::getSingleton('adminhtml/session')->addError(
                Mage::helper('vijay_festivalbackgroundimage')->__('Please select festivalbackgrounds.')
            );
        } else {
            try {
                foreach ($festivalbackgroundIds as $festivalbackgroundId) {
                $festivalbackground = Mage::getSingleton('vijay_festivalbackgroundimage/festivalbackground')->load($festivalbackgroundId)
                            ->setStatus($this->getRequest()->getParam('status'))
                            ->setIsMassupdate(true)
                            ->save();
                }
                $this->_getSession()->addSuccess(
                    $this->__('Total of %d festivalbackgrounds were successfully updated.', count($festivalbackgroundIds))
                );
            } catch (Mage_Core_Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(
                    Mage::helper('vijay_festivalbackgroundimage')->__('There was an error updating festivalbackgrounds.')
                );
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }

    /**
     * export as csv - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportCsvAction()
    {
        $fileName   = 'festivalbackground.csv';
        $content    = $this->getLayout()->createBlock('vijay_festivalbackgroundimage/adminhtml_festivalbackground_grid')
            ->getCsv();
        $this->_prepareDownloadResponse($fileName, $content);
    }

    /**
     * export as MsExcel - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportExcelAction()
    {
        $fileName   = 'festivalbackground.xls';
        $content    = $this->getLayout()->createBlock('vijay_festivalbackgroundimage/adminhtml_festivalbackground_grid')
            ->getExcelFile();
        $this->_prepareDownloadResponse($fileName, $content);
    }

    /**
     * export as xml - action
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function exportXmlAction()
    {
        $fileName   = 'festivalbackground.xml';
        $content    = $this->getLayout()->createBlock('vijay_festivalbackgroundimage/adminhtml_festivalbackground_grid')
            ->getXml();
        $this->_prepareDownloadResponse($fileName, $content);
    }

    /**
     * Check if admin has permissions to visit related pages
     *
     * @access protected
     * @return boolean
     * @author Ultimate Module Creator
     */
    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('vijay_festivalbackgroundimage/festivalbackground');
    }
}

Step 15: Create file in app/code/community/Vijay/Festivalbackgroundimage/Helper/Data.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * convert array to options
     *
     * @access public
     * @param $options
     * @return array
     * @author Ultimate Module Creator
     */
    public function convertOptions($options)
    {
        $converted = array();
        foreach ($options as $option) {
            if (isset($option['value']) && !is_array($option['value']) &&
                isset($option['label']) && !is_array($option['label'])) {
                $converted[$option['value']] = $option['label'];
            }
        }
        return $converted;
    }
    public function canShowjQuery()
    {
     if(Mage::getStoreConfig('vijay_festivalbackgroundimage/general/is_enabled') == true && Mage::getStoreConfig('vijay_festivalbackgroundimage/frontend/includejquery') == true) {
      return 'festivalbackgroundimage/jquery-3.1.1.min.js';
     }
    }
}

Step 16: Create file in app/code/community/Vijay/Festivalbackgroundimage/Model/Festivalbackground.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Model_Festivalbackground extends Mage_Core_Model_Abstract
{
    /**
     * Entity code.
     * Can be used as part of method name for entity processing
     */
    const ENTITY    = 'vijay_festivalbackgroundimage_festivalbackground';
    const CACHE_TAG = 'vijay_festivalbackgroundimage_festivalbackground';

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'vijay_festivalbackgroundimage_festivalbackground';

    /**
     * Parameter name in event
     *
     * @var string
     */
    protected $_eventObject = 'festivalbackground';

    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    public function _construct()
    {
        parent::_construct();
        $this->_init('vijay_festivalbackgroundimage/festivalbackground');
    }

    /**
     * before save festivalbackground
     *
     * @access protected
     * @return Vijay_Festivalbackgroundimage_Model_Festivalbackground
     * @author Ultimate Module Creator
     */
    protected function _beforeSave()
    {
        parent::_beforeSave();
        $now = Mage::getSingleton('core/date')->gmtDate();
        if ($this->isObjectNew()) {
            $this->setCreatedAt($now);
        }
        $this->setUpdatedAt($now);
        return $this;
    }

    /**
     * save festivalbackground relation
     *
     * @access public
     * @return Vijay_Festivalbackgroundimage_Model_Festivalbackground
     * @author Ultimate Module Creator
     */
    protected function _afterSave()
    {
        return parent::_afterSave();
    }

    /**
     * get default values
     *
     * @access public
     * @return array
     * @author Ultimate Module Creator
     */
    public function getDefaultValues()
    {
        $values = array();
        $values['status'] = 1;
        return $values;
    }
    
}

Step 17: Create file in app/code/community/Vijay/Festivalbackgroundimage/Model/Resource/Festivalbackground.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Model_Resource_Festivalbackground extends Mage_Core_Model_Resource_Db_Abstract
{

    /**
     * constructor
     *
     * @access public
     * @author Ultimate Module Creator
     */
    public function _construct()
    {
        $this->_init('vijay_festivalbackgroundimage/festivalbackground', 'entity_id');
    }
}

Step 18: Create file in app/code/community/Vijay/Festivalbackgroundimage/Model/Resource/Setup.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Model_Resource_Setup extends Mage_Core_Model_Resource_Setup
{
}

Step 19: Create file in app/code/community/Vijay/Festivalbackgroundimage/Model/Resource/Festivalbackground/Collection.php and add below code in it.

<?php
class Vijay_Festivalbackgroundimage_Model_Resource_Festivalbackground_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected $_joinedFields = array();

    /**
     * constructor
     *
     * @access public
     * @return void
     * @author Ultimate Module Creator
     */
    protected function _construct()
    {
        parent::_construct();
        $this->_init('vijay_festivalbackgroundimage/festivalbackground');
    }

    /**
     * get festivalbackgrounds as array
     *
     * @access protected
     * @param string $valueField
     * @param string $labelField
     * @param array $additional
     * @return array
     * @author Ultimate Module Creator
     */
    protected function _toOptionArray($valueField='entity_id', $labelField='festivalname', $additional=array())
    {
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }

    /**
     * get options hash
     *
     * @access protected
     * @param string $valueField
     * @param string $labelField
     * @return array
     * @author Ultimate Module Creator
     */
    protected function _toOptionHash($valueField='entity_id', $labelField='festivalname')
    {
        return parent::_toOptionHash($valueField, $labelField);
    }

    /**
     * Get SQL for get record count.
     * Extra GROUP BY strip added.
     *
     * @access public
     * @return Varien_Db_Select
     * @author Ultimate Module Creator
     */
    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(Zend_Db_Select::GROUP);
        return $countSelect;
    }
}

Step 20: Create file in app/code/community/Vijay/Festivalbackgroundimage/Model/Adminhtml/Search/Festivalbackground.php and add below code in it.

<?php

class Vijay_Festivalbackgroundimage_Model_Adminhtml_Search_Festivalbackground extends Varien_Object
{
    /**
     * Load search results
     *
     * @access public
     * @return Vijay_Festivalbackgroundimage_Model_Adminhtml_Search_Festivalbackground
     * @author Ultimate Module Creator
     */
    public function load()
    {
        $arr = array();
        if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
            $this->setResults($arr);
            return $this;
        }
        $collection = Mage::getResourceModel('vijay_festivalbackgroundimage/festivalbackground_collection')
            ->addFieldToFilter('festivalname', array('like' => $this->getQuery().'%'))
            ->setCurPage($this->getStart())
            ->setPageSize($this->getLimit())
            ->load();
        foreach ($collection->getItems() as $festivalbackground) {
            $arr[] = array(
                'id'          => 'festivalbackground/1/'.$festivalbackground->getId(),
                'type'        => Mage::helper('vijay_festivalbackgroundimage')->__('Festivalbackground'),
                'name'        => $festivalbackground->getFestivalname(),
                'description' => $festivalbackground->getFestivalname(),
                'url' => Mage::helper('adminhtml')->getUrl(
                    '*/festivalbackgroundimage_festivalbackground/edit',
                    array('id'=>$festivalbackground->getId())
                ),
            );
        }
        $this->setResults($arr);
        return $this;
    }
}

Step 21: Create file in app/code/community/Vijay/Festivalbackgroundimage/Model/Background/Attribute/Source/Backgroundtarget.php and add below code in it.

<?php
/**
 * vijay_festivalbackgroundimage extension
 * 
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 * 
 * @category       Vijay
 * @package        vijay_festivalbackgroundimage
 * @copyright      Copyright (c) 2016
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 */
/**
 * Admin source model for Background Target
 *
 * @category    Vijay
 * @package     vijay_festivalbackgroundimage
 * @author      Ultimate Module Creator
 */
class Vijay_Festivalbackgroundimage_Model_Background_Attribute_Source_Backgroundtarget extends Mage_Eav_Model_Entity_Attribute_Source_Table
{
    /**
     * get possible values
     *
     * @access public
     * @param bool $withEmpty
     * @param bool $defaultValues
     * @return array
     * @author Ultimate Module Creator
     */
    public function getAllOptions($withEmpty = true, $defaultValues = false)
    {
        $options =  array(
            array(
                'label' => Mage::helper('vijay_custombackground')->__('Wrapper'),
                'value' => '.wrapper'
            ),
            array(
                'label' => Mage::helper('vijay_custombackground')->__('Body'),
                'value' => 'body'
            ),
            array(
                'label' => Mage::helper('vijay_custombackground')->__('Custom CSS selector'),
                'value' => 'custom'
            ),
        );
        if ($withEmpty) {
            array_unshift($options, array('label'=>'', 'value'=>''));
        }
        return $options;

    }

    /**
     * get options as array
     *
     * @access public
     * @param bool $withEmpty
     * @return string
     * @author Ultimate Module Creator
     */
    public function getOptionsArray($withEmpty = true)
    {
        $options = array();
        foreach ($this->getAllOptions($withEmpty) as $option) {
            $options[$option['value']] = $option['label'];
        }
        return $options;
    }

    /**
     * get option text
     *
     * @access public
     * @param mixed $value
     * @return string
     * @author Ultimate Module Creator
     */
    public function getOptionText($value)
    {
        $options = $this->getOptionsArray();
        if (!is_array($value)) {
            $value = explode(',', $value);
        }
        $texts = array();
        foreach ($value as $v) {
            if (isset($options[$v])) {
                $texts[] = $options[$v];
            }
        }
        return implode(', ', $texts);
    }
}

Step 22: Create file in app/code/community/Vijay/Festivalbackgroundimage/sql/vijay_festivalbackgroundimage_setup/install-1.0.0.php and add below code in it.

<?php

$this->startSetup();
$table = $this->getConnection()
    ->newTable($this->getTable('vijay_festivalbackgroundimage/festivalbackground'))
    ->addColumn(
        'entity_id',
        Varien_Db_Ddl_Table::TYPE_INTEGER,
        null,
        array(
            'identity'  => true,
            'nullable'  => false,
            'primary'   => true,
        ),
        'Festivalbackground ID'
    )
    ->addColumn(
        'festivalname',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(
            'nullable'  => false,
        ),
        'Festival Name'
    )
    ->addColumn(
        'startdate',
        Varien_Db_Ddl_Table::TYPE_DATETIME, 255,
        array(
            'nullable'  => false,
        ),
        'Festival Start Date'
    )
    ->addColumn(
        'enddate',
        Varien_Db_Ddl_Table::TYPE_DATETIME, 255,
        array(
            'nullable'  => false,
        ),
        'Festival End Date'
    )
 ->addColumn(
        'background_target',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(
            'nullable'  => false,
        ),
        'Background Target'
    )
 ->addColumn(
        'background_custom_target',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(),
        'Background Custom Target'
    )
    ->addColumn(
        'type',
        Varien_Db_Ddl_Table::TYPE_SMALLINT, null,
        array(
         'unsigned'  => true,
            'nullable'  => false,
        ),
        'Background Type'
    )
    ->addColumn(
        'background',
        Varien_Db_Ddl_Table::TYPE_TEXT, 255,
        array(
            'nullable'  => false,
        ),
        'background'
    )
    ->addColumn(
        'status',
        Varien_Db_Ddl_Table::TYPE_SMALLINT, null,
        array(),
        'Enabled'
    )
    ->addColumn(
        'updated_at',
        Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
        null,
        array(),
        'Festivalbackground Modification Time'
    )
    ->addColumn(
        'created_at',
        Varien_Db_Ddl_Table::TYPE_TIMESTAMP,
        null,
        array(),
        'Festivalbackground Creation Time'
    ) 
    ->setComment('Festivalbackground Table');
$this->getConnection()->createTable($table);
$this->endSetup();

Step 23: Add frontend layout file. Create file in app/design/frontend/base/default/layout/vijay_festivalbackgroundimage.xml and add below code in it.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="head">
         <action method="addJs">
          <script helper="vijay_festivalbackgroundimage/data/canShowjQuery" />
         </action>
     <block type="core/template" name="festivalbackgroundimage" template="festivalbackgroundimage/festivalbackground.phtml" />
        </reference>
    </default>
</layout>

Step 24: Create frontend template file. Create file in app/design/frontend/base/default/template/festivalbackgroundimage/festivalbackground.phtml and add below code in it.

<?php if (Mage::getStoreConfigFlag('vijay_festivalbackgroundimage/general/is_enabled') == true ) :?>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(
function(jQuery) 
{      
  changeBG();
});

function changeBG()
{
 <?php 
        $today = date("Y-m-d");
        $collection = Mage::getModel('vijay_festivalbackgroundimage/festivalbackground')->getCollection()
        ->addFieldToFilter("status",1)
        ->addFieldToFilter("startdate",array('lteq'=> $today))
        ->addFieldToFilter("enddate",array('gteq'=> $today));
        
foreach($collection as $background)
 {
     $bg=$background->getBackground();
        $type=$background->getType();
     $backgroundtarget=$background->getBackgroundTarget();
     $backgroundcustomtarget=$background->getBackgroundCustomTarget();
     $startdate=$background->getStartdate();
     $enddate=$background->getEnddate();
     $startdate  = date('Y-m-d',strtotime($startdate));
     $enddate  = date('Y-m-d',strtotime($enddate));
                      
             if(($startdate<=$today) && ($enddate>=$today))
     {
    if($type==1)
    {
     $imageUrl=Mage::getBaseUrl('media').'festivalbackgroundimage/'.$bg;
    
    if ($backgroundtarget == 'custom') { ?>
     jQuery("<?php echo $backgroundcustomtarget; ?>").css("background", "url('<?php echo $imageUrl; ?>') no-repeat");
    <?php } else { ?>
     jQuery("<?php echo $backgroundtarget; ?>").css("background", "url('<?php echo $imageUrl; ?>') no-repeat");
    <?php } ?>
     
     jQuery(".footer-container").css("border-top-color", "transparent");
     
 
    <?php
    }
    else
    {
    
     if ($backgroundtarget == 'custom') { ?>
     jQuery("<?php echo $backgroundcustomtarget; ?>").css("background-color", "<?php echo $bg; ?>");
    <?php } else { ?>
     jQuery("<?php echo $backgroundtarget; ?>").css("background-color", "<?php echo $bg; ?>");
    <?php } ?> 
     jQuery(".footer-container").css("border-top-color", "transparent");
    
    <?php    
       }
          }    
  } 
     
?>

}
</script>
<?php endif;?>

Step 25: Create admin layout file. Create file in app/design/adminhtml/default/default/layout/vijay_festivalbackgroundimage.xml and add below code in it.

<?xml version="1.0"?> 
<layout>
 <default>        
     <reference name="head">
    <action method="addJs">
                        <script>festivalbackgroundimage/jquery-3.1.1.min.js</script><!--this is optional file // if not working then add this -->
                </action>
                <action method="addJs">
                        <script>jscolor/jscolor.js</script>
                </action>
                <action method="addJs">
                        <script>festivalbackgroundimage/jquery.fancybox.pack.js</script>
                </action>
                <action method="addJs">
                        <script>festivalbackgroundimage/showimg.js</script>
                </action>
                <action method="addJs">
                 <script>scriptaculous/effects.js</script>
                </action>
                <action method="addCss">
                        <stylesheet>festivalbackgroundimage/style.css</stylesheet>
                </action>
        </reference>      
    </default>
    <adminhtml_festivalbackgroundimage_festivalbackground_index>
        <reference name="menu">
            <action method="setActive">
                <menupath>vijay_festivalbackgroundimage/festivalbackground</menupath>
            </action>
        </reference>
        <reference name="content">
            <block type="vijay_festivalbackgroundimage/adminhtml_festivalbackground" name="festivalbackground" />

        </reference>
    </adminhtml_festivalbackgroundimage_festivalbackground_index>
    <adminhtml_festivalbackgroundimage_festivalbackground_grid>
        <block type="core/text_list" name="root" output="toHtml">
            <block type="vijay_festivalbackgroundimage/adminhtml_festivalbackground_grid" name="festivalbackground_grid"/>
        </block>
    </adminhtml_festivalbackgroundimage_festivalbackground_grid>
    <!-- Festivalbackground add/edit action -->
    <adminhtml_festivalbackgroundimage_festivalbackground_edit>
        <update handle="editor"/>
        <reference name="menu">
            <action method="setActive">
                <menupath>vijay_festivalbackgroundimage/festivalbackground</menupath>
            </action>
        </reference>
        <reference name="content">
            <block type="vijay_festivalbackgroundimage/adminhtml_festivalbackground_edit" name="festivalbackground_edit"></block>
        </reference>
        <reference name="left">
            <block type="vijay_festivalbackgroundimage/adminhtml_festivalbackground_edit_tabs" name="festivalbackground_tabs"></block>
        </reference>
    </adminhtml_festivalbackgroundimage_festivalbackground_edit>

</layout>


Step 26: Add JS files in js/festivalbackgroundimage folder. You can download JS files from https://github.com/vijayrami/Festival_Background_Image/tree/master/js/festivalbackgroundimage

Step 27: Add CSS files in skin/adminhtml/default/default/festivalbackgroundimage folder. You can download CSS files from https://github.com/vijayrami/Festival_Background_Image/tree/master/skin/adminhtml/default/default/festivalbackgroundimage

Step 28: Add Language Translation file. Create file in app/locale/en_US/Vijay_Festivalbackgroundimage.csv. You can download this file from
https://github.com/vijayrami/Festival_Background_Image/tree/master/app/locale/en_US

You can download whole module from Github

You will Found some catchy background on Front end as per below:


Tag : Magento
0 Comments On "Add Festival Background Image In Magento"

Back To Top