/**
     * Create form with repeat buttons.
     * Add is always displayed, delete displayed when > 1 instances.
     * Up displayed for all instances except the first.
     * Down displayed for all instances except the last.
     *
     * @param DOMNode $node the current node
     * @param int     $i    index of the current instance
     * @param int     $size the number of instances
     */
    private function addRepeatButtons($node, $i, $size)
    {
        $addButton = $this->htmlButton('add.png', 'add');
        $deleteButton = $size > 1 ? $this->htmlButton('delete.png', 'delete') : '';
        $upButton = $i > 0 ? $this->htmlButton('up.png', 'up') : '';
        $downButton = $i < $size - 1 ? $this->htmlButton('down.png', 'down') : '';
        $reference = new Reference($this->name, $i);
        $url = htmlspecialchars(new Common\PageURL(null, array('field' => (string) $reference) + $_GET));
        $id = htmlspecialchars($reference->toId());
        $this->addButtonHtml($node, <<<END
<form method="post" action="{$url}" id="{$id}">
    {$addButton}
    {$deleteButton}
    {$upButton}
    {$downButton}
</form>
END
);
    }
 protected function actionDisplay()
 {
     if (!isset($_POST['submit'])) {
         ob_end_clean();
         echo $this->mergeTemplateWithMessage(true);
         exit;
     }
     $mm = new MessageModel($_GET['id'], $this->dao);
     $messageAreas = $mm->messageAreas();
     $ref = Reference::decode(stripslashes($_REQUEST['field']));
     $idRef = clone $ref;
     switch ($_POST['submit']) {
         case 'save':
             $newValue = $_POST['content'];
             if ($ref->repeat) {
                 $messageAreas[$ref->repeat][$ref->instance][$ref->name] = $newValue;
             } else {
                 $messageAreas[$ref->name] = $newValue;
             }
             $mm->replaceMessageAreas($messageAreas);
             break;
         case 'cancel':
             break;
         case 'add':
             if (!isset($messageAreas[$ref->repeat])) {
                 $messageAreas[$ref->repeat] = array();
             }
             array_splice($messageAreas[$ref->repeat], $ref->instance + 1, 0, array(array()));
             $mm->replaceMessageAreas($messageAreas);
             $idRef->instance += 1;
             break;
         case 'delete':
             if (isset($messageAreas[$ref->repeat][$ref->instance])) {
                 unset($messageAreas[$ref->repeat][$ref->instance]);
                 $messageAreas[$ref->repeat] = array_values($messageAreas[$ref->repeat]);
                 $mm->replaceMessageAreas($messageAreas);
                 $idRef->instance = $idRef->instance > 0 ? --$idRef->instance : $idRef->instance;
             }
             break;
         case 'up':
             if (isset($messageAreas[$ref->repeat][$ref->instance]) && isset($messageAreas[$ref->repeat][$ref->instance - 1])) {
                 $temp = $messageAreas[$ref->repeat][$ref->instance - 1];
                 $messageAreas[$ref->repeat][$ref->instance - 1] = $messageAreas[$ref->repeat][$ref->instance];
                 $messageAreas[$ref->repeat][$ref->instance] = $temp;
                 $mm->replaceMessageAreas($messageAreas);
                 $idRef->instance -= 1;
             }
             break;
         case 'down':
             if (isset($messageAreas[$ref->repeat][$ref->instance]) && isset($messageAreas[$ref->repeat][$ref->instance + 1])) {
                 $temp = $messageAreas[$ref->repeat][$ref->instance + 1];
                 $messageAreas[$ref->repeat][$ref->instance + 1] = $messageAreas[$ref->repeat][$ref->instance];
                 $messageAreas[$ref->repeat][$ref->instance] = $temp;
                 $mm->replaceMessageAreas($messageAreas);
                 $idRef->instance += 1;
             }
             break;
         case 'hide':
             $messageAreas[$ref->name][0]['_hidden'] = true;
             $mm->replaceMessageAreas($messageAreas);
             break;
         case 'unhide':
             $messageAreas[$ref->name][0]['_hidden'] = false;
             $mm->replaceMessageAreas($messageAreas);
             break;
         default:
     }
     $query = $_GET;
     unset($query['field']);
     $redirect = new PageURL('message_page', $query, $idRef->toId());
     header('Location: ' . $redirect);
     exit;
 }