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