model = $list; $schema->createTables(); $schema->model = $item; $schema->createTables(); // Now we need a simple page to provide the results. // First we create a context with the data to carry. $context = new Pluf_Template_Context( array('page_title' => 'Installation', 'message' => 'The installation was successfull')); // Then we load a template. $tmpl = new Pluf_Template('simple.html'); // and render the template as content of the response sent to the user return new Pluf_HTTP_Response($tmpl->render($context)); } /** * Display the uninstall 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 uninstall($request, $match) { // The uninstallation is the reverse of the installation. // We create the data models the same way, but instead of calling // createTables() we call dropTables() // You can see that without all the comments, you do not have a // lot of lines of code. $list = new Todo_List(); $item = new Todo_Item(); $db = Pluf::db(); $schema = Pluf::factory('Pluf_DB_Schema', $db); $schema->model = $list; $schema->dropTables(); $schema->model = $item; $schema->dropTables(); $context = new Pluf_Template_Context( array('page_title' => 'Uninstallation', 'message' => 'The uninstallation was successfull')); $tmpl = new Pluf_Template('simple.html'); return new Pluf_HTTP_Response($tmpl->render($context)); } /** * Display the main page of the application. * * The main page is only displaying data. So we load the categories * and for each category we display the corresponding items. * * @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 main($request, $match) { // In the main page we want a list of all the Todo lists with // a link to edit each of them, a link to see the content and // a link to create a new list. // Get the complete list of Todo_List object $lists = Pluf::factory('Todo_List')->getList(); // Create a context for the template $context = new Pluf_Template_Context(array('page_title' => 'Home', 'lists' => $lists) ); // Load a template $tmpl = new Pluf_Template('main.html'); // Render the template and send the response to the user return new Pluf_HTTP_Response($tmpl->render($context)); } /** * Display the viewItem 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 viewItem($request, $match) { // Basically the same as the viewList view but with a Todo_Item $item_id = $match[1]; // We now are loading the corresponding item $item = new Todo_Item($item_id); // And check that the item has been found if ($item->id != $item_id) { return new Pluf_HTTP_Response_NotFound('The item has not been found.'); } // Now we get the list in wich the item is $list = $item->get_list(); // We have the item and the list, just display them. Instead of // creating a context, then a template and then rendering it // within a response object, we are going to use a shortcut // function. Using shortcuts is better as you end up having a // cleaner code. return Pluf_Shortcuts_RenderToResponse('viewitem.html', array('page_title' => 'View Item', 'list' => $list, 'item' => $item)); } /** * Display the addItem 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 addItem($request, $match) { // The workflow of the addition 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 create the new item. // We create a Todo_Item item as we are creating one here $item = new Todo_Item(); $list = Pluf_Shortcuts_GetObjectOr404('Todo_List', $match[1]); if ($request->method == 'POST') { // We get the data submitted by the user and initialize // the form with. $form = Pluf_Shortcuts_GetFormForModel($item, $request->POST); if ($form->isValid()) { // If no errors, we can save the Todo_Item from the data $item = $form->save(); // We redirect the user to the page of the Todo_List in which // we have created the item. // 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 { // As we already now the list in which we are going to add // the item, we pass it as initial value. The user can // change it in the select box. $initial = array('list'=>$list->id); $form = Pluf_Shortcuts_GetFormForModel($item, $initial); } // Here we are with a GET request or a POST request with errors // So we create the rendering view of the form // We create a new rendering view return Pluf_Shortcuts_RenderToResponse('additem.html', array('page_title' => 'Add a todo item', 'list' => $list, 'form' => $form)); } /** * 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('edititem.html', array('page_title' => 'Edit a todo item', 'item' => $item, 'form' => $form)); } /** * Display the deleteItem 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 deleteItem($request, $match) { // A delete of an item is like an update. First you check that // the item is available, then you delete it if the request is // a POST request, else you provide a form to ask confirmation // before deletion. $item = Pluf_Shortcuts_GetObjectOr404('Todo_Item', $match[1]); if ($request->method == 'POST') { // Store the list id. $list_id = $item->list; // We can here directly delete the Todo_Item. Note that if // your object is linking to other objects you need to be // sure that you are taking into consideration the // foreignkey and manytomany relationships. $item->delete(); $url = Pluf_HTTP_URL_urlForView('Todo_Views::viewList', array($list_id)); return new Pluf_HTTP_Response_Redirect($url); } // Here we are with a GET request we show a form with a // confirmation button to delete the item. return Pluf_Shortcuts_RenderToResponse('deleteitem.html', array('page_title' => 'Delete a todo item', 'item' => $item)); } /** * Display the listLists 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 listLists($request, $match) { // The list of lists is like the home, so we just return // the same content. return $this->main($request, $match); } /** * Display the viewList 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 viewList($request, $match) { // We are showing the content of the list. // That is, all the items in the list. $list = Pluf_Shortcuts_GetObjectOr404('Todo_List', $match[1]); // Now we get the items in the list $items = $list->get_todo_item_list(); // We have the items and the list, just display them // Create a context for the template return Pluf_Shortcuts_RenderToResponse('viewlist.html', array('page_title' => 'View List', 'list' => $list, 'items' => $items)); } /** * Display the updateList 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 updateList($request, $match) { // Take a look at updateItem() to have explanations. Here you // can see that the code is quite compact without comments. $list = Pluf_Shortcuts_GetObjectOr404('Todo_List', $match[1]); if ($request->method == 'POST') { $form = new Todo_Form_List($request->POST); if ($form->isValid()) { $list->setFromFormData($form->cleaned_data); $list->update(); $url = Pluf_HTTP_URL_urlForView('Todo_Views::viewList', array($list->id)); return new Pluf_HTTP_Response_Redirect($url); } } else { $form = new Todo_Form_List($list->getData()); } return Pluf_Shortcuts_RenderToResponse('editlist.html', array('page_title' => 'Edit a todo list', 'list' => $list, 'form' => $form)); } /** * Display the deleteList 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 deleteList($request, $match) { // Here we show how a list can be delete with the associated // Todo_Item. $list = Pluf_Shortcuts_GetObjectOr404('Todo_List', $match[1]); if ($request->method == 'POST') { // We need first to delete all the Todo_Item in the list. $items = $list->get_todo_item_list(); foreach ($items as $item) { $item->delete(); } // Then we can delete the list $list->delete(); // You can also drop directly the list and the todo items // will be automatically dropped at the same time. $url = Pluf_HTTP_URL_urlForView('Todo_Views::main', array()); return new Pluf_HTTP_Response_Redirect($url); } // Here we are with a GET request we show a form with a // confirmation button to delete the list. // To show the items to be deleted, we get them $items = $list->get_todo_item_list(); return Pluf_Shortcuts_RenderToResponse('deletelist.html', array('page_title' => 'Delete a todo list', 'list' => $list, 'items' => $items)); } /** * Display the addList 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 addList($request, $match) { // The workflow of the addition 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 create the new list. // We create a Todo_List item as we are creating one here $list = new Todo_List(); if ($request->method == 'POST') { // We create the form bounded to the data submitted. $form = new Todo_Form_List($request->POST); if ($form->isValid()) { // If no errors, we can save the Todo_List from the data $list->setFromFormData($form->cleaned_data); $list->create(); // We redirect the user to the page of the Todo_List $url = Pluf_HTTP_URL_urlForView('Todo_Views::viewList', array($list->id)); return new Pluf_HTTP_Response_Redirect($url); } } else { $form = new Todo_Form_List(); } return Pluf_Shortcuts_RenderToResponse('addlist.html', array('page_title' => 'Add a todo list', 'form' => $form)); } }