Archive for the 'howto' Category

Nov 9, 2009

Extracting email addresses from inbox

Author: zwee | Filed under: howto

We had to setup a EDM/newsletter system for a client. He wanted to consolidated his and his staff email addresses. He sent me a few outlook PST files. We imported it into outlook and found that besides the few thousand emails in the contact list, there’s another few thousand more emails in different folders which held email addresses. I figured it would be easy to extract those emails.

Unfortunately, there’s no built in way of doing it in Outlook or Thunderbird. There’s a couple of paid solutions to extract emails from the inbox folders but I had to try it a few more times. After tinkering around with Outlook and Thunderbird, I found a way to extract emails through exporting to CSV.

Simple steps to get email addresses from inbox. These steps are for Office 2007

  1. Import emails into Outlook
  2. Export to CSV(DOS) (See Figure 1)
  3. Open with Excel
  4. Select the email column and copy into a new file.
  5. Save as CSV.
  6. You have it. Use it anywhere you like. CSV is supported for most applications.

Figure 1

Here’s the instructions to export to CSV file.

File > Import Export

Step 1: File > Import and Export

Step 2: Export to file

Step 2: Export to file

Step 2: Export to file

Step 3: Comma Separated Values (DOS)

Step 2: Export to file

Step 4: Select folder

Step 2: Export to file

Step 5: Export to CSV

Something as simple as getting the sql dump of a database in Microsoft SQL Server (2000, 2005 et al) to a SQL file could be quite a bit complicated (atleast more so as compared to the way we do it in MYSQL). SQL Server Management Studio does a good job of creating scripts that can help you recreate Database Schema. But when it comes to recreating the real Database data through SQL scripting sadly it simply just doesn’t have any simple method to accomplish it (as far as I know it).

The best way to accomplish this MS SQL is by using Microsoft SQL Server Database Publishing Wizard 1.1. It seems to be a pretty good tool because of its simplicity, stability, accuracy and ease of use. Once you download & install it, you can choose either the tool’s GUI or the command line interface to generate a single SQL script file which can be used to recreate a database (both schema and data). What is mire, there is even a bonus feature of being integrable with Visual Studio!

Download it from the Microsoft Download Page for Microsoft SQL Server Database Publishing Wizard 1.1

An important component of my role as UI designer and Project Manager at Savant Degrees involves prototyping application concepts for clients, as well as in-house product ideas. Ever since Savant Degrees started out, we have been working with people from all walks of life – partners who are graphic designer, client who play an active role, new experience team members, etc. I have to admit that working with someone new can really be a pain at times. Especially when the different groups speak in different languages (the design team, the HCI team, the development team and the project managers, etc).

I’m sure many of us know what a wireframe is, but how many of us use a proper software to work on a wireframe? For the benefit of people who are new to the term, it simply meant a visual presentation used in web design. Previously, I build simple HTML or drew simple shapes in word document and run through the process with the web developers. After some time, I turn to mockup softwares. I was happy with some but not as much as with Axure.

Before I talk about the tool, I like to explain why it is so important to use mockups.

  1. Designing wireframes the right way the first time, cost no more than doing it the wrong way. Creating a paper prototype adds no cost, either- simply print out the wireframe diagrams for the pages a visitor will use to complete the tasks most closely related to achieving his goals and meeting the site’s business objectives.
  2. Web Prototyping saves costs of any marketing communication that may be necessary to undo brand damage resulting from poorly functioning web site and a frustrating user experience. Expensive site redesigns are often undertaken to correct those kinds of problems. Adding a few days to create a wireframe and test a prototype ensures that the redesigned site won’t introduce any new causes for user frustration or further erosion of brand equity.
  3. Using simple shapes lets you focus on the information design first, to make sure it meets users needs, before moving on to the pretty pictures. For an existing site, wireframes are abstracted from screen shots, with new content, functional, and navigational elements sketched in.

Usually, your client will want to see visual designs as early as possible in the project. If possible, try to delay giving them a design, not before them the mockup.

Why?

One very important reason is to save money and time. Why spend on graphics before the underlying information design is complete?

Equally important, colors, fonts and stylistic treatments of logos and other graphical elements can invoke emotional responses (“I hate this pink”; “I love that blue”; “That version of the logo makes me ill”) that distract from the task of determining the best information design for the page.

A wireframe diagrams lets everybody focus on what’s important at this step: clearly understanding what goes on each page, where it goes, and why it goes there, so you can achieve the best overall balance and structure for each page. A wireframe is a sketch of a page-layout idea. The sketch may be rendered in the computer using an illustration application or hand-drawn on paper. Either method is fine, as long as it clearly communicates your ideas.

The information design will evolve as wireframe diagrams change, incorporating results from the usability test at the end of this step, to reflect placement of page elements in ways that better meet user needs. Keeping the information design flexible at this stage will help you create a more positive brand experience over the long term, so you don’t want anyone to develop partisan attachments to a particular look this early in the game.

Despite all that was said. Its sometime hard to say no to your clients request when they have more power over you. Hence, how you manage your project depends on the type of project, your relationship with the client, and your company’s work culture.

Note for Visual Designers

At this point in the process, visual designers should conduct exploratory meetings to understand the client’s visual preferences and the visual elements of the client’s brand. Wait until wireframes are set before showing visual-design treatments of the pages to the client.

Note for Clients

For clients who insist on seeing visuals earlier rather than later, you can ask visual designers to design page mock-ups representing possible colors, imagery, and look and feel – possible styles of what is being considered – at this stage. However, you should do this only if absolutely necessary – that is, if they won’t take no for an answer – and be sure to emphasize that these mock-ups in no way reflect the final designs (repeat this warning early and often).

Caution the people in the site-owning organization against developing any attachments to these mock-ups, because such attachments will make it difficult it change the visual design later on. Chances are these people will not be happy when they see the final design with text, links and information added to it. Focusing only on wireframes at this stage helps you avoid building unrealistic expectations about the site’s final look and feel, guarding maximum flexibility to evolve the site’s information design as necessary.

What I use

Now, let’s go on to why I personally choose Axure and what it can do for you. Do check out their website, www.axure.com for some videos. Do note that there are many mockup software out there and you should check them out before making a decision. For a good list, check out http://www.sitepoint.com/print/tools-prototyping-wireframing/

Axure RP Pro

* Price: USD$589 – Single User License (Discounted to $539 for 5+ Licenses)

* OS Compatability: Windows 2000, XP, 2003 Server or Vista

Axure allows the tagging of elements with functional specifications, which is excellent when a wireframe itself will not provide adequate information to allow a programmer to code the feature. Having said that, a programmer will normally prefer to receive specifications whether they be tagged to an element or written in a wiki, rather than code based on an interpretation of a wireframe.

I use Axure when I am planning a medium to large scale application which requires documentation (Axure exports all tagged specifications into a neatly laid out document – a big time saver!), functional specifications and HTML prototype (Axure will generate an HTML prototype for you – another time saver, however if you take a look at the code you’ll realise that there is no way it could be used as a basis for the actual production application. Use the prototype to display UX concepts and provide clients with a realistic and tangible model for feedback.)

I could go on for pages about what the product accomplish, but I believe their website, www.axure.com, do a better job in selling their products than in I do. Furthermore, it is not the purpose of this article to review this particular software.

By the way, did I mention I got my copy of Axure for free back when I was a student? Axure gives out free licenses to “good student”. Just email Axure and show them your transcript. See http://www.axure.com/learnMoreBuy.aspx for more info.

Frontend – List
Let us revisit our frontend controller. Surely by now you’ve gotten a better grasp of the controller and models. So we’ll be revisiting those concepts here. So lets say you want to allow users to view you current Twits as well as create a new Twit.

  1. Some new file structure loving
    app/
      design/
        frontend/
          default/
            default/
              template/
                twit/
                  - twits_list.phtml
      etc/
        modules/
          - SavantDegrees_All.xml (Or what ever your company name might be)
      code/
        local/
          SavantDegrees/ (Or what ever your company name might be)
            Twit/ (Or whatever your module name might be)
              Block/
                Admin/
                  - Main.php
                  Main/
                    - Grid.php
                  - Edit.php
                  Edit/
                    - Form.php
                  - New.php
                  New/
                    - Form.php
                - Index.php
              controllers/
                - AdminController.php
                - IndexController.php
              etc/
                - config.xml
              Helper/
                - Data.php
              Model/
                - Twit.php
                Mysql4/
                  - Twit.php
                  Twit/
                    - Collection.php
              sql/
                twit_setup/
                  - mysql4-install-0.2.0.php
                  - mysql4-upgrade-0.1.0-0.2.0.php

    We’ve added a whole nasty branch of subdirectories under app/designs. Read on to understand what its all for…

  2. Let us return to our IndexController.php
    <?php
    class SavantDegrees_Twit_IndexController extends Mage_Core_Controller_Front_Action
    {
     
        public function indexAction()
        {
    	$this->loadLayout();
    	$this->getLayout()
    		->getBlock('content')->append(
    			$this->getLayout()->createBlock('twit/index')
        	);
            $this->renderLayout();
        }
    }

    Hold your horses, this code is EXACTLY the same as the Part 1. Its just a revision. However, that being said, we see that in Part 1, we made the system create a Block called index. That’s an opening…

  3. So let us modify our index.php block:
    <?php
    class SavantDegrees_Twit_Block_Index extends Mage_Core_Block_Template
    {
        public function __construct()
        {
            parent::__construct();
            $this->setTemplate('twit/twits_list.phtml');
        }
     
        public function getTwits()
        {
        	$model = Mage::getModel('twit/twit');
        	$collection = $model
        	    	->getCollection()
        	    	->load();
     
            return $collection->getItems();
        }
    }

    What the difference here? We made the block load the template from twits/twit_list.phtml. We can create/find this file in /app/design/frontend/default/default/template/twits/twit_list.phtml. Some explanation is due here:

    • When you say setTemplate in the block, it means, use this file as the presentation/view
    • This file exists in the some subdirectory of /app/design/frontend/default/default/template. That is the path to your default template. Even though you may have other templates, this is the default 1. So when Magento cant find your template file in the other template directories, it always reverts back to this folder. Its a directory form of inheritance/ancestory/precedence.
  4. Lastly, the twit_list.phtml file:
    <?php
    $_twits = $this->getTwits();
    if ($_twits) {
    	$count = 0;
    	foreach ($_twits as $i=>$twit) {
    ?>
    		<div class="twit">
    			<div class='name'><?= $twit->getName() ?></div>
    			<div class='summary'><?= $twit->getSummary() ?></div>
    		</div>
    <?php
    	}
    }
    ?>

    Since the whole purpose of twits_list.php is to prepare the view for the list of twits, it obviously needs to get the data from somewhere. It turns out, the Template is the extension of the Block. So by calling $this->getTwits(), we’re calling the Block's getTwits() method.

  5. There you have it! check your twits list at http://127.0.0.1/magento/index.php/twit
Jun 21, 2009

JQuery Progress Bar 2.0

Author: gaweee | Filed under: development, howto
jQuery progressBar screenshot

Hi all its us again, In the past few months we’ve been asked many a times to help improve the progressbar code. We’ve also been referenced by several websites as THE progressbar to use. In all, we felt we owed it to you guys to make it better.

So here we are, after all those emails and bug reports, we finally got down to it. Always good to take a couple of months to have a fresh prespective of things. We ripped out the old code and made it much better and more extensible than its predecessors. So much that we decided it was enough to qualify as a major revision.

 

Let see whats been done…

  • Cleaned up the code, yes its lighter, cleaner faster. (Still lacks documentation though) :|
  • Callbacks! Everyone’s favourite
  • Max values, you can now set it to be 150/2000 instead of just a percentage
  • Text formats. Show 75/100 or 75% by toggling the textFormat accordingly
  • Steps, how many steps to get to your target value
  • Step Duration, how long each step lasts
  • Webkit (Chrome/Safari) compatibility. More like, Webkit Hacks

Once again, many a thanks for all those who have so generously provided the feature requests as well as bug fixes every now and then. And thanks for all your patience in waiting for this new release. :D
If its working for you, drop us a comment and tell us where we can see your stuff!

Download the new jQuery progressbar here: jQuery progressbar
or view the demo here

Jun 14, 2009

HOWTO: Find icons for your new prototype system

Author: gaweee | Filed under: development, howto

Having a good UI is 30% of your customer won. No i dont know, 30% was completely arbitrary but absolutely believable. As such, please dont try to make those icons yourself! As all developers know, we try our best not to reinvent the wheel. Unless the wheel sucks and we have the budget, then we knock ourselves out. :D
So its time to share those little icon secrets! Where do you find nice icons!?

  • famfamfam. If you dont know this site, you should be SHOT. Mark James spent good time making a fantastic set of icons so that everyone else could use them. Kudos to him!
  • crystal iconsAnother awesome job done by Everaldo. Download link’s not working. Use this instead. Made primarily for the KDE workspace. Which brings us to my next point
  • Any other Linux icons sources. These includes kde-look, tango, interfacelift, deviantart
  • Iconfinder. Its new, its awesome and apparently they’re license-friendly! If everything’s as promised, they’re gonna kick some serious internet butt! We’re fans already!
  • Ok we admit, sometimes we’re really tempted to use existing packages icons. Joomla has a really nice set. :D

All in all, plenty of icons. And if that doesnt work out. Go buy some from istockphoto and stock.xchng! They’re the pros for a good reason!
And a final word in licensing. Respect it. Not so much for the hard work, but more for the fact that designers can do what we cannot. Even if we tried, we probably wouldn’t have as much flair. Just like magicians.

Jun 14, 2009

Google Maps Helper

Author: gaweee | Filed under: development, howto

In our work we like to create Google Maps links for our client’s (offices, stores, etc). However, the larger our clients presence, the more random links there are bound to be. When it comes to using Google Map’s javascript API to control the map, thats still perfectly fine. But how do I create a google maps link that only shows 1 entry? Turns out, there are several ways to do this:

  1. Manipulate the request such that it only shows your entryHow? For example, searching for “Holland, Singapore” will lead us to about 1,356 results. See here. So lets tweak it a little:
    • Append &mrt=yp to the url – That returns us all the business listings. (852 results)
    • Append &start=16 to the url – That skips the first 16 results (Shows results 17-26 of 852)
    • Append &num=2 to the url – That returns us only 2 results (Only shows 2 results)

    For a more complete listing of Google Maps Parameters, consult mapki.com. Kudos to those guys for compiling that list. Really useful stuff
    So there you have it, a simple way to link your business such that its the only entry there. However this method is relatively dangerous. Why? Cause you’ve got no idea of the exact ordering that Google maps may return. Today you could be entry 17, surely not in a year (hopefully for the better). As such, this is not the recommended solution.

  2. Use the address with your long/lat coordinates to generate your entry.
    • First get your GPS coordinates, there are a number of ways to do this. Check our this link
    • Then create your link via the following structure: http://www.google.com/maps?&ie=UTF8&hl=en&q=[urlescaped address]&ll=[GPS lat,long]&z=[Zoom]&iwloc=A
    • Still too troublesome? We’ve created a simple tool for our clients to use. Save yourself some time. Try it!
    • Done! Whats the downside to this? You can only show the address for the business. Because searching by business names returns us way too many results. So instead, we have to search for an address, Google interprets this as an address and only returns 1 result (which is the whole point of geocoding). At this point its easy to the map to what we need. We’d use this typically for this nifty little “Find us on a map” links (instead of embedding the actual map on your site).

Once again, if you’d rather embed Google maps directly into your site via the JS, then your options are much more open.
Good luck! Let us know if it works for you, of if you find a better way of doing things!
googleMapsHelper

Apr 13, 2009

HOWTO: Controlling your presentation with your phone

Author: gaweee | Filed under: howto, quick picks

Yeah i just love seeing the awe on my student’s faces when i use my phone and my geek geeky geekiest green laser pointer to teach a class. The software is really simple. After trying out Phonepoint and Wireless Presenter (comes preinstalled in my Nokia E66), I’ve come to pick out Phonepoint as the winner!

Its really simple. Bluetooth connect, open the software on ur PC, open the app on your mobile. Hey ho presto! you’re ready to go! Try it! its free to try but $19.99 for a license. I’d say thats worth the money if i get to walk the room looking good.

CRUD – Update
Very similar to the Create phase, this phase basically requires the Form and Form Container blocks, and the controller to save them. Thats all!

  1. Repeat the file structure goodness:
    app/
      etc/
        modules/
          - SavantDegrees_All.xml (Or what ever your company name might be)
      code/
        local/
          SavantDegrees/ (Or what ever your company name might be)
            Twit/ (Or whatever your module name might be)
              Block/
                Admin/
                  - Main.php
                  Main/
                    - Grid.php
                  - Edit.php
                  Edit/
                    - Form.php
                  - New.php
                  New/
                    - Form.php
                - Index.php
              controllers/
                - AdminController.php
                - IndexController.php
              etc/
                - config.xml
              Helper/
                - Data.php
              Model/
                - Twit.php
                Mysql4/
                  - Twit.php
                  Twit/
                    - Collection.php
              sql/
                twit_setup/
                  - mysql4-install-0.2.0.php
                  - mysql4-upgrade-0.1.0-0.2.0.php

    Here we’ve added the block for the Edit container and Form

  2. Looking at the Edit.php code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    <?php
    class SavantDegrees_Twit_Block_Admin_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
    {
        public function __construct()
        {
            parent::__construct();
     
            $this->_blockGroup = 'twit';
            $this->_mode = 'edit';
            $this->_controller = 'admin';
     
            if( $this->getRequest()->getParam($this->_objectId) ) {
                $twitData = Mage::getModel('twit/twit')
                    ->load($this->getRequest()->getParam($this->_objectId));
                Mage::register('frozen_twit', $twitData);
            }
        }
     
        public function getHeaderText()
        {
            return Mage::helper('twit')->__("Edit Twit'%s'", $this->htmlEscape(Mage::registry('frozen_twit')->getName()));
        }
    }

    The additional code loads the right Twit data based on the Request parameter, then stores that data in the Mage registry. We’ll be using that data on the Form page again later to populate the form.
    Notice that the saving code is Mage::register and the retrieving code is Mage::registry

  3. Then the accompanying Edit/Form.php code
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
    <?php
    class SavantDegrees_Twit_Block_Admin_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
    {
        protected function _prepareForm()
        {
            $form = new Varien_Data_Form(array(
                'id'        => 'edit_form',
                'action'    => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                'method'    => 'post'
            ));
     
            $fieldset = $form->addFieldset('edit_twit', array('legend' => Mage::helper('twit')->__('Twit Details')));
     
            $fieldset->addField('name', 'text', array(
                'name'      => 'name',
                'title'     => Mage::helper('twit')->__('Name'),
                'label'     => Mage::helper('twit')->__('Name'),
                'maxlength' => '50',
                'required'  => true,
            ));
     
            $fieldset->addField('tags', 'text', array(
                'name'      => 'tags',
                'title'     => Mage::helper('twit')->__('Tags'),
                'label'     => Mage::helper('twit')->__('Tags'),
                'maxlength' => '255',
                'required'  => true,
            ));
     
            $fieldset->addField('summary', 'textarea', array(
                'name'      => 'summary',
                'title'     => Mage::helper('twit')->__('Summary'),
                'label'     => Mage::helper('twit')->__('Summary'),
                'style'     => 'width: 98%; height: 200px;',
                'required'  => true,
            ));
     
            $form->setUseContainer(true);
            $form->setValues(Mage::registry('frozen_twit')->getData());
            $this->setForm($form);
            return parent::_prepareForm();
        }
    }
  4. Then lastly the adminController with its editAction and saveAction to load and save the form respectively

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    
    <?php
    class SavantDegrees_Twit_AdminController extends Mage_Adminhtml_Controller_Action
    {
    	public function indexAction()
        {
    		$this->loadLayout()
    			->_addContent($this->getLayout()->createBlock('twit/admin_main'))
    			->renderLayout();
        }
     
    	public function deleteAction()
        {
            $twitId = $this->getRequest()->getParam('id', false);
     
            try {
                Mage::getModel('twit/twit')->setId($twitId)->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twit')->__('Page successfully deleted'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));
     
                return;
            } catch (Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
     
            $this->_redirectReferer();
        }
     
        public function newAction()
        {
            $this->loadLayout()
            ->_addContent($this->getLayout()->createBlock('twit/admin_new'))
            ->renderLayout();
        }
     
        public function postAction()
        {
            if ($data = $this->getRequest()->getPost()) {
                $twit = Mage::getModel('twit/twit')->setData($data);
                try {
                    $twit->save();
                    Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twit')->__('Twit was successfully saved'));
                    $this->getResponse()->setRedirect($this->getUrl('*/*/'));
                    return;
                } catch (Exception $e){
                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                }
            }
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));
            return;
        }
     
        public function editAction()
        {
            $this->loadLayout();
            $this->_addContent($this->getLayout()->createBlock('twit/admin_edit'));
            $this->renderLayout();
        }
     
        public function saveAction()
        {
            $twitId = $this->getRequest()->getParam('id', false);
            if ($data = $this->getRequest()->getPost()) {
                $twit = Mage::getModel('twit/twit')->load($twitId)->addData($data);
                try {
                    $twit->setId($twitId)->save();
     
                    Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twit')->__('Twit was saved successfully'));
                    $this->getResponse()->setRedirect($this->getUrl('*/*/'));
                    return;
                } catch (Exception $e){
                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                }
            }
            $this->_redirectReferer();
        }
    }
  5. Ok i admit the code is a tad inefficient. We have 2 separate functions that both does save. In fact, postAction and saveAction only differ that saveAction loads the right record via the ->load($twitId) function. Thats separates a CREATE from an UPDATE request.

    And we’re finally finally finally done! To test out the edit function, simply click on any row from the Grid! Thats assuming of course that you actually have some data.
    Magento Admin Form

For frontend stuff, carry on … Fontend – List

Otherwise, if it works for you, leave a comment! =)

Just to be sure, download the Magento tutorial files to see if you’ve made any mistakes here: Magento Tutorial

CRUD – Create

  1. More file structure goodness:
    app/
      etc/
        modules/
          - SavantDegrees_All.xml (Or what ever your company name might be)
      code/
        local/
          SavantDegrees/ (Or what ever your company name might be)
            Twit/ (Or whatever your module name might be)
              Block/
                Admin/
                  - Main.php
                  Main/
                    - Grid.php
                  - New.php
                  New/
                    - Form.php
                - Index.php
              controllers/
                - AdminController.php
                - IndexController.php
              etc/
                - config.xml
              Helper/
                - Data.php
              Model/
                - Twit.php
                Mysql4/
                  - Twit.php
                  Twit/
                    - Collection.php
              sql/
                twit_setup/
                  - mysql4-install-0.2.0.php
                  - mysql4-upgrade-0.1.0-0.2.0.php

    Here we’ve added the block for the New container and Form

  2. Looking at the New.php code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    <?php
    class SavantDegrees_Twit_Block_Admin_New extends Mage_Adminhtml_Block_Widget_Form_Container
    {
        public function __construct()
        {
            parent::__construct();
     
            $this->_blockGroup = 'twit';
            $this->_mode = 'new';
            $this->_controller = 'admin';
        }
     
        public function getHeaderText()
        {
            return Mage::helper('twit')->__('Add New Twit');
        }
    }
  3. Then the New/Form.php code
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    <?php
    class SavantDegrees_Twit_Block_Admin_New_Form extends Mage_Adminhtml_Block_Widget_Form
    {
        protected function _prepareForm()
        {
            $form = new Varien_Data_Form();
     
            $fieldset = $form->addFieldset('new_twit', array('legend' => Mage::helper('twit')->__('Twit Details')));
     
            $fieldset->addField('name', 'text', array(
                'name'      => 'name',
                'title'     => Mage::helper('twit')->__('Name'),
                'label'     => Mage::helper('twit')->__('Name'),
                'maxlength' => '50',
                'required'  => true,
            ));
     
            $fieldset->addField('tags', 'text', array(
                'name'      => 'tags',
                'title'     => Mage::helper('twit')->__('Tags'),
                'label'     => Mage::helper('twit')->__('Tags'),
                'maxlength' => '255',
                'required'  => true,
            ));
     
            $fieldset->addField('summary', 'textarea', array(
                'name'      => 'summary',
                'title'     => Mage::helper('twit')->__('Summary'),
                'label'     => Mage::helper('twit')->__('Summary'),
                'style'     => 'width: 98%; height: 200px;',
                'required'  => true,
            ));
     
            $form->setMethod('post');
            $form->setUseContainer(true);
            $form->setId('edit_form');
            $form->setAction($this->getUrl('*/*/post'));
     
            $this->setForm($form);
        }
    }

    Just like your listings/grid page, the New.php and Form.php classes are the Form Container and Form respectively. So the Form Container automagically creates the form. Did I mention why containers are good? They come with nice pretty buttons! (Save and Back).

  4. Then lastly the adminController with its newAction and postAction to load and save the form respectively

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    
     
    class SavantDegrees_Twit_AdminController extends Mage_Adminhtml_Controller_Action
    {
    	public function indexAction()
        {
    		$this->loadLayout()
    			->_addContent($this->getLayout()->createBlock('twit/admin_main'))
    			->renderLayout();
        }
     
    	public function deleteAction()
        {
            $twitId = $this->getRequest()->getParam('id', false);
     
            try {
                Mage::getModel('twit/twit')->setId($twitId)->delete();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twit')->__('Page successfully deleted'));
                $this->getResponse()->setRedirect($this->getUrl('*/*/'));
     
                return;
            } catch (Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
     
            $this->_redirectReferer();
        }
     
        public function newAction()
        {
            $this->loadLayout()
            ->_addContent($this->getLayout()->createBlock('twit/admin_new'))
            ->renderLayout();
        }
     
        public function postAction()
        {
            if ($data = $this->getRequest()->getPost()) {
                $twit = Mage::getModel('twit/twit')->setData($data);
                try {
                    $twit->save();
                    Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('twit')->__('Twit was successfully saved'));
                    $this->getResponse()->setRedirect($this->getUrl('*/*/'));
                    return;
                } catch (Exception $e){
                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                }
            }
            $this->getResponse()->setRedirect($this->getUrl('*/*/'));
            return;
        }
    }
  5. Reward yourself for your lightning fast copy-and-paste kung foo! Whats happening though?
    When the script receives a call newAction, it’ll show the form to enter data. The data is posted to the postAction method. That method creates an instance of the model, sets the post data into the model, saves it then redirects it back to the indexAction. Did i mention automagically? Yes, thats why we had to learn so much of the Magento code just to write this simple tutorial. The sql is automagically generated and run and the form is automagically created! *sheds a tear*

    Again, your well deserved create form can be found by clicking on the “Add New Twit” button from the Grid!

Just 1 more … CRUD – Update