00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 abstract class PrototypedEditor extends MethodMappedController
00017 {
00018 const COMMAND_SUCCEEDED = 'success';
00019 const COMMAND_FAILED = 'error';
00020
00021 protected $subject = null;
00022 protected $map = null;
00023
00024 public function __construct(Prototyped $subject)
00025 {
00026 $this->subject = $subject;
00027 $this->map =
00028 MappedForm::create(
00029 $this->subject->proto()->makeForm()
00030 )->
00031 addSource('id', RequestType::get())->
00032 setDefaultType(RequestType::post());
00033
00034 $this->
00035 setMethodMapping('drop', 'doDrop')->
00036 setMethodMapping('take', 'doTake')->
00037 setMethodMapping('save', 'doSave')->
00038 setMethodMapping('edit', 'doEdit')->
00039 setMethodMapping('add', 'doAdd');
00040
00041 $this->setDefaultAction('edit');
00042 }
00043
00047 public function doDrop(HttpRequest $request)
00048 {
00049 $this->map->import($request);
00050 $form = $this->map->getForm();
00051
00052 if ($object = $form->getValue('id')) {
00053 if ($object instanceof Identifiable) {
00054
00055 $this->dropObject($request, $form, $object);
00056
00057 return ModelAndView::create()->setModel(
00058 Model::create()->
00059 set('editorResult', self::COMMAND_SUCCEEDED)
00060 );
00061
00062 } else {
00063
00064
00065 $form->markMissing('id');
00066
00067 return ModelAndView::create()->setModel(
00068 Model::create()->
00069 set('editorResult', self::COMMAND_FAILED)->
00070 set('form', $form)
00071 );
00072 }
00073 } else {
00074 return ModelAndView::create()->setModel(
00075 Model::create()->
00076 set('editorResult', self::COMMAND_FAILED)->
00077 set('form', $form)
00078 );
00079 }
00080
00081 Assert::isUnreachable();
00082 }
00083
00084 protected function dropObject(HttpRequest $request, Form $form, Identifiable $object)
00085 {
00086 $object->dao()->drop($object);
00087 }
00088
00092 public function doTake(HttpRequest $request)
00093 {
00094 $this->map->import($request);
00095 $form = $this->map->getForm();
00096
00097 if (!$form->getRawValue('id')) {
00098
00099 $isAdd = true;
00100 $form->markGood('id');
00101 $object = clone $this->subject;
00102
00103 } else {
00104
00105 $object = $form->getValue('id');
00106 $isAdd = false;
00107 }
00108
00109 if (!$form->getErrors()) {
00110
00111 $object = $isAdd
00112 ? $this->addObject($request, $form, $object)
00113 : $this->saveObject($request, $form, $object);
00114
00115 $editorResult = $form->getErrors()
00116 ? self::COMMAND_FAILED
00117 : self::COMMAND_SUCCEEDED;
00118
00119 return
00120 ModelAndView::create()->
00121 setModel(
00122 Model::create()->
00123 set('id', $object->getId())->
00124 set('subject', $object)->
00125 set('form', $form)->
00126 set('editorResult', $editorResult)
00127 );
00128 } else {
00129 $model =
00130 Model::create()->
00131 set('form', $form)->
00132 set('editorResult', self::COMMAND_FAILED);
00133
00134 if ($object)
00135 $model->set('subject', $object);
00136
00137 return ModelAndView::create()->setModel($model);
00138 }
00139
00140 Assert::isUnreachable();
00141 }
00142
00146 public function doSave(HttpRequest $request)
00147 {
00148 $this->map->import($request);
00149 $form = $this->map->getForm();
00150
00151 $object = $form->getValue('id');
00152
00153 if (!$form->getErrors()) {
00154
00155 $object = $this->saveObject($request, $form, $object);
00156
00157 $editorResult = $form->getErrors()
00158 ? self::COMMAND_FAILED
00159 : self::COMMAND_SUCCEEDED;
00160
00161 return
00162 ModelAndView::create()->
00163 setModel(
00164 Model::create()->
00165 set('id', $object->getId())->
00166 set('subject', $object)->
00167 set('form', $form)->
00168 set('editorResult', $editorResult)
00169 );
00170 } else {
00171 $model =
00172 Model::create()->
00173 set('form', $form)->
00174 set('editorResult', self::COMMAND_FAILED);
00175
00176 if ($object)
00177 $model->set('subject', $object);
00178
00179 return ModelAndView::create()->setModel($model);
00180 }
00181
00182 Assert::isUnreachable();
00183 }
00184
00185 protected function saveObject(HttpRequest $request, Form $form, Identifiable $object)
00186 {
00187 FormUtils::form2object($form, $object, false);
00188 return $object->dao()->save($object);
00189 }
00190
00194 public function doEdit(HttpRequest $request)
00195 {
00196 $this->map->import($request);
00197 $form = $this->map->getForm();
00198
00199 if ($form->getValue('id'))
00200 $object = $form->getValue('id');
00201 else
00202 $object = clone $this->subject;
00203
00204 FormUtils::object2form($object, $form);
00205
00206 $form->dropAllErrors();
00207
00208 return ModelAndView::create()->setModel(
00209 Model::create()->
00210 set('subject', $object)->
00211 set('form', $form)
00212 );
00213 }
00214
00218 public function doAdd(HttpRequest $request)
00219 {
00220 $this->map->import($request);
00221 $form = $this->map->getForm();
00222
00223 $form->markGood('id');
00224 $object = clone $this->subject;
00225
00226 if (!$form->getErrors()) {
00227
00228 $object = $this->addObject($request, $form, $object);
00229
00230 $editorResult = $form->getErrors()
00231 ? self::COMMAND_FAILED
00232 : self::COMMAND_SUCCEEDED;
00233
00234 return
00235 ModelAndView::create()->
00236 setModel(
00237 Model::create()->
00238 set('id', $object->getId())->
00239 set('subject', $object)->
00240 set('form', $form)->
00241 set('editorResult', $editorResult)
00242 );
00243 } else {
00244 return
00245 ModelAndView::create()->
00246 setModel(
00247 Model::create()->
00248 set('form', $form)->
00249 set('subject', $object)->
00250 set('editorResult', self::COMMAND_FAILED)
00251 );
00252 }
00253
00254 Assert::isUnreachable();
00255 }
00256
00257 protected function addObject(HttpRequest $request, Form $form, Identifiable $object)
00258 {
00259 FormUtils::form2object($form, $object);
00260 return $object->dao()->add($object);
00261 }
00262 }
00263 ?>