Avenir | Drupal Development | Joomla Development | Magento Development | OsCommerce Development

Tuesday, 21 February 2012

Create the FORM in magento front end with module (Example: gallery module)

1. Exceute the following SQL code in your PHPMYADMIN


CREATE TABLE IF NOT EXISTS `avenirgallery` (
  `avenirgallery_id` int(11) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL default '',
  `age` int(11) NOT NULL,
  `email` varchar(100) NOT NULL,
  `filename` varchar(255) NOT NULL default '',
  `cratetheme` varchar(100) NOT NULL,
  `yourname` varchar(50) NOT NULL,
  `location` varchar(50) NOT NULL,
  `content` text NOT NULL,
  `status` smallint(6) NOT NULL default '0',
  `created_time` datetime default NULL,
  `update_time` datetime default NULL,
  PRIMARY KEY  (`avenirgallery_id`)
);



2. create folder as "slideshow" (for saving the images)  inside the media folder

3.  create file as "Avenir_Avenirgallery.xml"  in the path app\etc\modules , and copy and paste following code and save it.


<?xml version="1.0"?>
<config>
    <modules>
        <Avenir_Avenirgallery>
            <active>true</active>
            <codePool>local</codePool>
        </Avenir_Avenirgallery>
    </modules>
</config>
4. Create app/code/local/Avenir/Avenirgallery/Block/Avenirgallery.php and paste the following code


<?php
class Avenir_Avenirgallery_Block_Avenirgallery extends Mage_Core_Block_Template
{
public function _prepareLayout()
    {
return parent::_prepareLayout();
    }
 
     public function getAvenirgallery()  
     {
        if (!$this->hasData('avenirgallery')) {
            $this->setData('avenirgallery', Mage::registry('avenirgallery'));
        }
        return $this->getData('avenirgallery');
     
    }
}

5. app/code/local/ Avenir/Avenirgallery/controllers/IndexController.php


<?php
class Avenir_Avenirgallery_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
   
    /*
    * Load an object by id
    * Request looking like:
    * http://site.com/avenirgallery?id=15
    *  or
    * http://site.com/avenirgallery/id/15
    */
   
$avenirgallery_id = $this->getRequest()->getParam('id');

  if($avenirgallery_id != null && $avenirgallery_id != '') {
$avenirgallery = Mage::getModel('avenirgallery/avenirgallery')->load($avenirgallery_id)->getData();
} else {
$avenirgallery = null;
}


/*
    * If no param we load a the last created item
    */
   
    if($avenirgallery == null) {
$resource = Mage::getSingleton('core/resource');
$read= $resource->getConnection('core_read');
$avenirgalleryTable = $resource->getTableName('avenirgallery');

$select = $read->select()
  ->from($avenirgalleryTable,array('avenirgallery_id','title','content','status'))
  ->where('status',1)
  ->order('created_time DESC') ;

$avenirgallery = $read->fetchRow($select);
}
Mage::register('avenirgallery', $avenirgallery);



$this->loadLayout();  
$this->renderLayout();
    }
public function sendemailAction()
    {
        //Fetch submited params
        $params = $this->getRequest()->getParams();
   $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$connection->beginTransaction();
$fields = array();
$fields['title']= $params['name'];
$fields['filename']=$_FILES['uploadfile4']['name'];
$fields['age']=$params['age'];
$fields['email']=$params['email'];
$fields['cratetheme']=$params['cratetheme'];
$fields['yourname']=$params['yourname'];
$fields['location']=$params['location'];
$fields['content']=$params['comment'];
$fields['status']='2';
$fields['created_time']=date ("Y-m-d H:m:s");
if(isset($_FILES['uploadfile4']['name']) && $_FILES['uploadfile4']['name'] != '') {
try {
/* Starting upload */
$uploader = new Varien_File_Uploader('uploadfile4');

// Any extention would work
          $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);

// Set the file upload mode
// false -> get the file directly in the specified folder
// true -> get the file in the product like folders
// (file.jpg will go in something like /media/f/i/file.jpg)
$uploader->setFilesDispersion(false);

// We set media as the upload dir
$path = Mage::getBaseDir('media') . DS .'slideshow' ;
$uploader->save($path, $_FILES['uploadfile4']['name'] );

} catch (Exception $e) {
   
       }
     
       //this way the name is saved in DB
  //$fields['content'] = $param $_FILES['image']['name'];
}


$connection->insert('avenirgallery', $fields);
$connection->commit();

$mail = new Zend_Mail();
        $mail->setBodyText($params['comment']);
        $mail->setFrom($params['email'], $params['name']);
        $mail->addTo('XXX.YYY@gmail.com', 'Kiwi Crate');
        $mail->setSubject('Test Avenir Gallery Module for Magento');
$filename = $_FILES['uploadfile4']['name'];
$fileContents = file_get_contents(Mage::getBaseDir('media').'/'.$filename); /*(Here put the filename with full path of file, which have to be send)*/
$attachment = $mail->createAttachment($fileContents);
$attachment->filename = $filename;

        try {
            $mail->send();
Mage::getSingleton('core/session')->addSuccess(Mage::helper('avenirgallery')->__("Thank you! Your photo was uploaded successfully. You'll see it on the site once it's been approved. If you wish, you may upload another photo."));
        }      
        catch(Exception $ex) {
            Mage::getSingleton('core/session')->addError('Unable to send email. Sample of a custom notification error from Avenir Gallery module.');

        }



        //Redirect back to index action of (this) activecodeline-simplecontact controller
        $this->_redirect('avenirgallery/');
    }
}

6.  app/code/local/ Avenir/Avenirgallery/etc/config.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Avenir_Avenirgallery>
            <version>0.1.0</version>
        </Avenir_Avenirgallery>
    </modules>
    <frontend>
        <routers>
            <avenirgallery>
                <use>standard</use>
                <args>
                    <module>Avenir_Avenirgallery</module>
                    <frontName>avenirgallery</frontName>
                </args>
            </avenirgallery>
        </routers>
        <layout>
            <updates>
                <avenirgallery>
                    <file>avenirgallery.xml</file>
                </avenirgallery>
            </updates>
        </layout>
    </frontend>
    <admin>
        <routers>
<avenirgallery>
<use>admin</use>
<args>
<module>Avenir_Avenirgallery</module>
<frontName>avenirgallery</frontName>
</args>
</avenirgallery>
        </routers>
    </admin>
    <adminhtml>
<menu>
<avenirgallery module="avenirgallery">
<title>Avenirgallery</title>
<sort_order>71</sort_order>            
<children>
<items module="avenirgallery">
<title>Manage Items</title>
<sort_order>0</sort_order>
<action>avenirgallery/adminhtml_avenirgallery</action>
</items>
</children>
</avenirgallery>
</menu>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<Avenir_Avenirgallery>
<title>Avenirgallery Module</title>
<sort_order>10</sort_order>
</Avenir_Avenirgallery>
</children>
</admin>
</resources>
</acl>
<layout>
<updates>
<avenirgallery>
<file>avenirgallery.xml</file>
</avenirgallery>
</updates>
</layout>
    </adminhtml>
    <global>
        <models>
            <avenirgallery>
                <class>Avenir_Avenirgallery_Model</class>
                <resourceModel>avenirgallery_mysql4</resourceModel>
            </avenirgallery>
            <avenirgallery_mysql4>
                <class>Avenir_Avenirgallery_Model_Mysql4</class>
                <entities>
                    <avenirgallery>
                        <table>avenirgallery</table>
                    </avenirgallery>
                </entities>
            </avenirgallery_mysql4>
        </models>
        <resources>
            <avenirgallery_setup>
                <setup>
                    <module>Avenir_Avenirgallery</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </avenirgallery_setup>
            <avenirgallery_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </avenirgallery_write>
            <avenirgallery_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </avenirgallery_read>
        </resources>
        <blocks>
            <avenirgallery>
                <class>Avenir_Avenirgallery_Block</class>
            </avenirgallery>
        </blocks>
        <helpers>
            <avenirgallery>
                <class>Avenir_Avenirgallery_Helper</class>
            </avenirgallery>
        </helpers>
    </global>
</config>

7. General file creattion steps

app/etc/modules/Samplemod_Samplemod.xml

app/code/local/Samplemod/Samplemod/Block/Samplemod.php

app/code/local/Samplemod/Samplemod/controllers/IndexController.php

app/code/local/Samplemod/Samplemod/etc/config.xml

app/code/local/Samplemod/Samplemod/Model/Samplemod.php

app/code/local/Samplemod/Samplemod/Model/Mysql4/Samplemod.php

app/code/local/Samplemod/Samplemod/Model/Mysql4/Samplemod/Collection.php

app/code/local/Samplemod/Samplemod/Model/Status.php

app/code/local/Samplemod/Samplemod/sql/samplemod_setup/mysql4-install-0.1.0.php

app/design/frontend/default/default/layout/samplemod.xml

app/design/frontend/default/default/template/samplemod/samplemod.phtml

app/code/local/Samplemod/Samplemod/Block/Adminhtml/Samplemod.php

app/code/local/Samplemod/Samplemod/Block/Adminhtml/Samplemod/Edit.php

app/code/local/Samplemod/Samplemod/Block/Adminhtml/Samplemod/Grid.php

app/code/local/Samplemod/Samplemod/Block/Adminhtml/Samplemod/Edit/Form.php

app/code/local/Samplemod/Samplemod/Block/Adminhtml/Samplemod/Edit/Tabs.php

app/code/local/Samplemod/Samplemod/Block/Adminhtml/Samplemod/Edit/Tab/Form.php

app/code/local/Samplemod/Samplemod/controllers/Adminhtml/SamplemodController.php

app/code/local/Samplemod/Samplemod/Helper/Data.php

app/design/adminhtml/default/default/layout/samplemod.xml





6 comments:

  1. very good tutorial
    thank's for uploding

    ReplyDelete
    Replies
    1. Can you please explain me how can i use this?

      Delete
  2. It’s really a cool and helpful piece of info. I’m happy that you just shared this helpful info with us. Please stay us informed like this. Thanks for sharing.

    -

    web development in sydney | website designer sydney | website design sydney

    ReplyDelete
  3. The article was really helpful, Thank you very much! - Nguyen Duy Phong 88

    ReplyDelete
  4. Can you please explain me how can i use this?

    ReplyDelete
  5. I have followed the steps as you mentioned here, but how can i use this in my page?

    ReplyDelete