Symfony’s documentation on the input_in_place_editor_tag is a little lacking. The input_in_place_editor_tag allows for in-line editing of any piece of text within your site. It uses AJAX to post the altered information back to your system. It’s a great tool, but I found myself wondering how to go about making use of it from within my Action methods. It didn’t take too much digging to figure out the basics you need to know.
Let’s assume you are working on a Wiki for your company website. And that any user signed into your site can edit information within a Wiki page. For this example, let’s assume all wiki pages have a ‘heading’ attribute and we want to allow the user to edit that heading for any page. The following code makes some assumptions about how your system may be setup, but should provide a general example of how to achieve what you need to do.
Within your template:
<?php use_helper('Javascript') ?>
<?php if ($sf_user->isAuthenticated()): ?>
<div id="edit_heading"><?php echo $page->getHeading() ?></div>
<?php echo input_in_place_editor_tag('edit_heading,
'page/editInLine?attribute=Heading&id='.$page->getId(),
array('cols' => '60', 'rows' => '3', )) ?>
<?php else: ?>
<?php echo $page->getHeading() ?>
<?php endif; ?>
This snippet of template is checking to see if the user is signed in. If they are, it is allowing them to edit the heading of the page. The editing will call a method named executeEditInLine within the actions.class.php file of the page module. So now let’s create the Action method. Note that this example is simplified. You should do some data cleansing as well as perhaps some user authentication. But here we go:
public function executeEditInLine(sfWebRequest $request)
{
//load the proper page to work with
$page = PagePeer::retrieveByPk($request->getParameter('id'));
//determine the proper get and set methods to be used
$set_func = 'set'.$request->getParameter('attribute');
$get_func = 'get'.$request->getParameter('attribute');
//same the current version of your page before performing the update
$page->saveCurrentVersion();
//set the new value, save it and return it back for page display
$page->$set_func($request->getParameter('value'));
$page->save();
die($page->$get_func());
}
Notice how the actual data being sent is done so within the parameter named ‘value.’