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
| <?php
class CatForm extends BaseCatForm
{
protected $parentId = null;
public function configure()
{
unset($this['parent'], $this['lft'], $this['rgt'], $this['level']);
$this->widgetSchema['parent_id'] = new sfWidgetFormDoctrineChoice(array(
'model' => 'cat',
'add_empty' => '~ (object is at root level)',
'order_by' => array('parent, lft',''),
'method' => 'getIndentedName'
));
$this->validatorSchema['parent_id'] = new sfValidatorDoctrineChoice(array(
'required' => false,
'model' => 'cat'
));
$this->setDefault('parent_id', $this->object->getParent());
$this->widgetSchema->setLabel('parent_id', 'Child of');
}
public function updateParentIdColumn($parentId)
{
$this->parentId = $parentId;
// further action is handled in the save() method
}
protected function doSave($con = null) {
parent::doSave($con);
$node = $this->object->getNode();
if ($this->parentId != $this->object->getParent() || !$node->isValidNode()){
if (empty($this->parentId)) {
//save as a root
if ($node->isValidNode()) {
$node->makeRoot($this->object['id']);
$this->object->save($con);
}
else
$this->object->getTable()->getTree()->createRoot($this->object); //calls $this->object->save internally
}
else {
//form validation ensures an existing ID for $this->parentId
$parent = $this->object->getTable()->find($this->parentId);
$method = ($node->isValidNode() ? 'move' : 'insert') . 'AsLastChildOf';
$node->$method($parent); //calls $this->object->save internally
}
}
}
} |
Partager