/** * Update an object (Part of the CRUD series). * * The minimal extra parameter is the model class name. The list * of extra parameters is: * * 'model' - Class name string, required. * * 'model_id' - Id of of the current model to update * * 'extra_context' - Array of key/values to be added to the * context (array()) * * 'extra_form' - Array of key/values to be added to the * form generation (array()) * * 'login_required' - Do we require login (false) * * 'template' - Template to use ('"model class"_update_form.html') * * 'post_save_redirect' - View to redirect after saving (use * getAbsoluteUrl() method of the mode) * * 'post_save_redirect_keys' - Which keys of the model to pass to * the view (array('id')) * * @param Pluf_HTTP_Request Request object * @param array Match * @param array Extra parameters * @return Pluf_HTTP_Response Response object (can be a redirect) */ public function updateObject($request, $match, $p) { if (isset($p['login_required']) && true == $p['login_required']) { if ($request->user->isAnonymous()) { return new Pluf_HTTP_Response_RedirectToLogin($request); } } if (!isset($p['model'])) { throw new Exception('The model class was not provided in the parameters.'); } // Set the default $model = $p['model']; $model_id = $p['model_id']; $context = isset($p['extra_context']) ? $p['extra_context'] : array(); $template = isset($p['template']) ? $p['template'] : strtolower($model) . '_update_form.html'; $post_save_keys = isset($p['post_save_redirect_keys']) ? $p['post_save_redirect_keys'] : array('id'); $object = Pluf_Shortcuts_GetObjectOr404($model, $model_id); if ($request->method == 'POST') { $form = Pluf_Shortcuts_GetFormForModel($object, $request->POST, $p['extra_form']); if ($form->isValid()) { $object = $form->save(); if (isset($p['post_save_redirect'])) { $url = Pluf_HTTP_URL_urlForView($p['post_save_redirect'], $post_save_keys); } elseif (in_array('getAbsoluteUrl', get_class_methods($object))) { $url = $object->getAbsoluteUrl(); } else { throw new Exception('No URL to redirect to from generic create view.'); } if (!$request->user->isAnonymous()) { $request->user->setMessage(sprintf(__('The %s was created successfully.'), $object->_a['verbose'])); } return new Pluf_HTTP_Response_Redirect($url); } } else { $form = Pluf_Shortcuts_GetFormForModel($object, $object->getData(), $p['extra_form']); } return Pluf_Shortcuts_RenderToResponse($template, array_merge($context, array('form' => $form, 'object' => $object)), $request); }
public function admin($request, $match) { $prj = $request->project; $title = sprintf(__('%s Project Summary'), (string) $prj); $form_fields = array('fields' => array('name', 'shortdesc', 'description')); if ($request->method == 'POST') { $form = Pluf_Shortcuts_GetFormForModel($prj, $request->POST, $form_fields); if ($form->isValid()) { $prj = $form->save(); $request->user->setMessage(__('The project has been updated.')); $url = Pluf_HTTP_URL_urlForView('IDF_Views_Project::admin', array($prj->shortname)); return new Pluf_HTTP_Response_Redirect($url); } } else { $form = Pluf_Shortcuts_GetFormForModel($prj, $prj->getData(), $form_fields); } $form->fields['description']->widget->attrs['cols'] = 68; $form->fields['description']->widget->attrs['rows'] = 26; $form->fields['shortdesc']->widget->attrs['size'] = 67; return Pluf_Shortcuts_RenderToResponse('idf/admin/summary.html', array('page_title' => $title, 'form' => $form), $request); }
/** * Display the updateItem page of the application. * * @param Pluf_HTTP_Request Request object * @param array Matches against the regex of the dispatcher * @return Pluf_HTTP_Response or can throw Exception */ public function updateItem($request, $match) { // Updating an item is somehow like creating an object but you // need first to load it to populate the form The workflow of // the update of an item is simple If the request of GET // method a form is displayed If it is a POST method, the form // is submitted and the content is proceeded to update item. // We create a Todo_Item item as we are updating one here // Here we are going to use another shortcut to get the item // or return a 404 error page if failing. $item = Pluf_Shortcuts_GetObjectOr404('Todo_Item', $match[1]); $new_data = $item->getData(); if ($request->method == 'POST') { // We get the data submitted by the user $form = Pluf_Shortcuts_GetFormForModel($item, $request->POST); if ($form->isValid()) { // The form is valid, we save it. $item = $form->save(); // We redirect the user to the page of the Todo_List // in which we have updated the item. We are using a // shortcut to get the URL directly from the view name // of interest. This allows us to not hard code the // path to the view in the view itself. $url = Pluf_HTTP_URL_urlForView('Todo_Views::viewList', array($item->list)); return new Pluf_HTTP_Response_Redirect($url); } } else { $form = Pluf_Shortcuts_GetFormForModel($item, $item->getData()); } // We proceed the same way by creating a context for a template // and providing the results to the user. return Pluf_Shortcuts_RenderToResponse('todo/item/update.html', array('page_title' => 'Update a Todo Item', 'item' => $item, 'form' => $form)); }