input->get("show"); if ($show) { $index = $album->get_position($show); $page = ceil($index / $page_size); if ($page == 1) { url::redirect("albums/$album->id"); } else { url::redirect("albums/$album->id?page=$page"); } } $page = $this->input->get("page", "1"); $children_count = $album->viewable()->children_count(); $offset = ($page - 1) * $page_size; // Make sure that the page references a valid offset if ($page < 1 || $page > max(ceil($children_count / $page_size), 1)) { Kohana::show_404(); } $template = new Theme_View("page.html", "album"); $template->set_global("page_size", $page_size); $template->set_global("item", $album); $template->set_global("children", $album->viewable()->children($page_size, $offset)); $template->set_global("children_count", $children_count); $template->set_global("parents", $album->parents()); $template->content = new View("album.html"); $album->view_count++; $album->save(); print $template; } /** * @see REST_Controller::_create($resource) */ public function _create($album) { access::required("edit", $album); switch ($this->input->post("type")) { case "album": return $this->_create_album($album); case "photo": return $this->_create_photo($album); default: access::forbidden(); } } private function _create_album($album) { access::required("edit", $album); $form = album::get_add_form($album); if ($form->validate()) { $new_album = album::create( $album, $this->input->post("name"), $this->input->post("title", $this->input->post("name")), $this->input->post("description"), user::active()->id); log::success("content", "Created an album", html::anchor("albums/$new_album->id", "view album")); message::success(t("Created album %album_title", array("album_title" => $new_album->title))); print json_encode( array("result" => "success", "location" => url::site("albums/$new_album->id"), "resource" => url::site("albums/$new_album->id"))); } else { print json_encode( array("result" => "error", "form" => $form->__toString())); } } private function _create_photo($album) { access::required("edit", $album); // If we set the content type as JSON, it triggers saving the result as // a document in the browser (well, in Chrome at least). // @todo figure out why and fix this. $form = photo::get_add_form($album); if ($form->validate()) { $photo = photo::create( $album, $this->input->post("file"), $_FILES["file"]["name"], $this->input->post("title", $this->input->post("name")), $this->input->post("description"), user::active()->id); log::success("content", "Added a photo", html::anchor("photos/$photo->id", "view photo")); message::success(t("Added photo %photo_title", array("photo_title" => $photo->title))); print json_encode( array("result" => "success", "resource" => url::site("photos/$photo->id"), "location" => url::site("photos/$photo->id"))); } else { print json_encode( array("result" => "error", "form" => $form->__toString())); } } /** * @see REST_Controller::_update($resource) */ public function _update($album) { access::required("edit", $album); $form = album::get_edit_form($album); if ($form->validate()) { // @todo implement changing the name. This is not trivial, we have // to check for conflicts and rename the album itself, etc. Needs an // api method. $orig = clone $album; $album->title = $form->edit_album->title->value; $album->description = $form->edit_album->description->value; $album->save(); module::event("item_updated", $orig, $album); log::success("content", "Updated album", "id\">view"); message::success(t("Saved album %album_title", array("album_title" => $album->title))); print json_encode( array("result" => "success", "location" => url::site("albums/$album->id"))); } else { print json_encode( array("result" => "error", "form" => $form->__toString())); } } /** * @see REST_Controller::_form_add($parameters) */ public function _form_add($album_id) { $album = ORM::factory("item", $album_id); access::required("edit", $album); switch ($this->input->get("type")) { case "album": print album::get_add_form($album); break; case "photo": print photo::get_add_form($album); break; default: kohana::show_404(); } } /** * @see REST_Controller::_form_add($parameters) */ public function _form_edit($album) { access::required("edit", $album); print album::get_edit_form($album); } }