# BaseEditor.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2006-2008 by Konstantin V. Arkhipov                     *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00015     abstract class BaseEditor implements Controller
    00016     {
    00017         const COMMAND_SUCCEEDED = 'success';
    00018         const COMMAND_FAILED    = 'error';
    00019
    00020         // to be redefined in __construct
    00021         protected $commandMap       = array();
    00022         protected $defaultAction    = 'edit';
    00023
    00024         protected $map      = null;
    00025         protected $subject  = null;
    00026
    00027         protected $idFieldName = null;
    00028
    00029         public function __construct(Prototyped $subject)
    00030         {
    00031             $this->subject = $subject;
    00032
    00033             $form =
    00034                 $this->subject->proto()->makeForm()->add(
    00035                     Primitive::choice('action')->setList($this->commandMap)->
    00036                     setDefault($this->defaultAction)
    00037                 );
    00038
    00039             if ($this->idFieldName)
    00040                 $form->add(
    00041                     Primitive::alias($this->idFieldName, $form->get('id'))
    00042                 );
    00043
    00044             $this->map =
    00045                 MappedForm::create($form)->
    00046                 addSource('id', RequestType::get())->
    00047                 addSource('action', RequestType::get())->
    00048                 setDefaultType(RequestType::post());
    00049
    00050             if ($this->idFieldName)
    00051                 $this->map->addSource($this->idFieldName, RequestType::get());
    00052         }
    00053
    00057         public function postHandleRequest(ModelAndView $mav, HttpRequest $request)
    00058         {
    00059             $form = $this->getForm();
    00060
    00061             if ($mav->getView() == self::COMMAND_SUCCEEDED) {
    00062
    00063                 $mav->setView(new RedirectToView(get_class($this)));
    00064
    00065                 $mav->getModel()->
    00066                     drop('id');
    00067
    00068             } else {
    00069                 $mav->setView(get_class($this));
    00070
    00071                 if ($command = $form->getValue('action'))
    00072                     $mav->getModel()->set('action', $command);
    00073                 else
    00074                     $form->dropAllErrors();
    00075
    00076                 $mav->getModel()->set('form', $form);
    00077             }
    00078
    00079             return $mav;
    00080         }
    00081
    00085         public function getForm()
    00086         {
    00087             return $this->map->getForm();
    00088         }
    00089     }
    00090 ?>