function add($parent_id = null)
{
$this->set('parent_id', $parent_id);
# Is a post being sent?
if (!empty($this->data))
{
# if there is a parent, find ancestor, too
if (isset($this->data['Post']['parent_id']) &&
$parent_id = (int)$this->data['Post']['parent_id'])
{
$parent_post = $this->Post->find('id = '.$parent_id);
# if parent has no ancestor, parent *is* the ancestor, too
if ($parent_post['Post']['ancestor_id'] === null) {
$this->data['Post']['ancestor_id'] = $parent_id;
} else {
# otherwise, inherit parent's ancestor.
$this->data['Post']['ancestor_id'] =
$parent_post['Post']['ancestor_id'];
}
}
if ($this->Post->save($this->data))
{
$this->redirect('/');
exit;
}
}
# Otherwise, read in the parent post's data if necessary.
# This will be displayed on the "You're replying to..." page.
else
{
if ($parent_id !== null)
{
$this->Post->id = $parent_id;
$parent_post = $this->Post->read();
$this->set('parent_post', $parent_post);
}
else
{
$this->set('parent_post', null);
}
}
}