Skip to content

Instantly share code, notes, and snippets.

@jamband
Created December 3, 2011 09:15
Show Gist options
  • Save jamband/1426639 to your computer and use it in GitHub Desktop.
Save jamband/1426639 to your computer and use it in GitHub Desktop.
<?php
class ItemController extends Controller
{
...
/**
* Creates a new item.
*/
public function actionCreate()
{
$item = new Item();
$this->commonEditAction($item);
}
/**
* Updates a particular item.
*/
public function actionUpdate()
{
$item = $this->loadModel();
$this->commonEditAction($item, array('title'));
}
/**
* Changes a particular item.
*/
public function actionChange()
{
$item = $this->loadModel();
$item->setScenario('change');
$this->commonEditAction($item, array('image'));
}
/**
* Edits a common action.
* @param Item $item item model
* @param array $attributes list of attributes that need to be saved.
*/
protected function commonEditAction($item, $attributes=null)
{
if (isset($_POST['confirm']))
{
$item->attributes = $_POST['Item'];
if ($item->validate())
{
if ($item->scenario !== 'update')
$_POST['Item']['image'] = $item->image;
$this->setPageState('x', $_POST['Item']);
$this->render('_formConfirm', compact('item'));
return;
}
}
else if (isset($_POST['back']))
$item->attributes = $this->getPageState('x');
else if (isset($_POST['finish']))
{
$item->attributes = $this->getPageState('x');
$item->save(false, $attributes);
$this->redirect(array('index'));
}
$this->render('_form', compact('item'));
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment