Drag & Drop Form Options with Zend Framework & Mootools

Posted January 31, 2008, 10:04am In: Zend Framework, SQL, Javascript, PHP

Recently I was working on a project that required the ability to define custom meta-data; this included categories and products, but also included a pre-determined list of countries and cities, etc.

Unfortunately, this hasn’t been tested on the latest version of the Zend Framework so it’s likely you’ll need to update your code accordingly.

We’re using version 1.2dev of MooTools

First, let’s create the controller:

I’ll skip the creation of fields and get right into the editing; I assume you’ve already added an index action and view. Our purpose is to effectively manage and modify field options, provide the ability to sort, edit and delete them, while preserving data related to the individual fields. For clarity, I will not use the view helpers for Form Elements.

Create your form and save it into your view, in my case edit-fields.php. I’ll leave it up to you to style and set form actions etc.

Then, pre-pend your javascript:

Now, before we get ahead of ourselves and worry about finding the fields, let’s the information and fields themselves - edit editFieldsAction() in your controller with the following:

Now let’s create our factory, in my case it’s MyFactory.php.

MyDomainObject::FetchCategory() validates that the requested categoryId exists. Then, the values assigned to that category. We could use $record->fetchValues() from within the rowClass instead of $obj->fetchValues($record->id), but it would more than likely just be an alias back to the factory.

Now, let’s open edit-fields.php again and add the following PHP between the UL.

An improvement to this would be to add an observer to all hyperlinks identified by class=”red”, but for simplicity, let’s leave that out. Now, our view should look something like this:

Assuming everything works, what should happen now is the list should pre-populate with previously created configuration options, we should be able to add, edit and delete those, and $_POST will override $this->options. Let’s start saving!

Edit editFieldsAction() to add some new functionality:

Now, back inside MyFactory:

It’s extra important that you pay attention to the usage of “ON DUPLICATE KEY UPDATE” - this is why we don’t have to do any fancy dancing. Additionally, we find the difference from $postArray versus what exists in the database to ensure that we delete anything that the user doesn’t want any more.

Additional steps might include the deletion of our user data when a config_value is deleted, either modifying the code itself or using the database.

This entry was posted on Thursday, January 31st, 2008 at 10:04 am and is filed under Zend Framework, SQL, Javascript, PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No Responses...

Leave a reply...

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>