00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2006-2007 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 /* $Id: BaseEditor.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00016 abstract class BaseEditor implements Controller 00017 { 00018 const COMMAND_SUCCEEDED = 'success'; 00019 const COMMAND_FAILED = 'error'; 00020 00021 // to be redefined in __construct 00022 protected $commandMap = array(); 00023 00024 protected $map = null; 00025 protected $subject = null; 00026 00027 public function __construct(Prototyped $subject) 00028 { 00029 $this->subject = $subject; 00030 00031 $this->map = 00032 MappedForm::create( 00033 $this->subject->proto()->makeForm()->add( 00034 Primitive::choice('action')->setList($this->commandMap)-> 00035 setDefault('edit') 00036 ) 00037 )-> 00038 addSource('id', RequestType::get())-> 00039 addSource('action', RequestType::get())-> 00040 setDefaultType(RequestType::post()); 00041 } 00042 00046 public function postHandleRequest(ModelAndView $mav, HttpRequest $request) 00047 { 00048 $form = $this->map->getForm(); 00049 00050 if ($mav->getView() == self::COMMAND_SUCCEEDED) { 00051 00052 $mav->setView(new RedirectToView(get_class($this))); 00053 00054 $mav->getModel()-> 00055 drop('id'); 00056 00057 } else { 00058 $mav->setView(get_class($this)); 00059 00060 if ($command = $form->getValue('action')) 00061 $mav->getModel()->set('action', $command); 00062 else 00063 $form->dropAllErrors(); 00064 00065 $mav->getModel()->set('form', $form); 00066 } 00067 00068 return $mav; 00069 } 00070 00074 public function getForm() 00075 { 00076 return $this->map->getForm(); 00077 } 00078 } 00079 ?>