Location: PHPKode > projects > DIY Blog > diy-blog/classes/diy-blog/model/AdminPageResource.php
<?php

require_once 'diy-blog/model/BackEndResource.php';

require_once 'diy-blog/model/om/BaseBackEndResource.php';

/**
 * Backend Resource for editing a single Page.
 * Originally generated by Propel.
 *
 * @package	diy-blog.model
 * @author	Martynas Jusevicius <hide@address.com>
 * @link	http://www.xml.lt
 */

class AdminPageResource extends BackEndResource
{

	/**
	 * Constructs a new AdminPageResource class, setting the Type column to BackEndResourcePeer::CLASSKEY_ADMINPAGERESOURCE.
	 */
	public function __construct()
	{

		$this->setType(BackEndResourcePeer::CLASSKEY_ADMINPAGERESOURCE);
	}

	public function doGet(Request $request, Response $response)
	{
		$view = null;
		$parent = parent::doGet($request, $response);

		if ($parent != null) $view = $parent;
		else $view = new EditPageView($this);

		return $view;
	}

	public function doPost(Request $request, Response $response)
	{
		$view = null;
		$parent = parent::doPost($request, $response);

		if ($parent != null) $view = $parent;
		else
		{
			$view = new EditPageView($this);

			if ($request->getParameter("action") == "update")
			{
				$errors = array();
				$form = new PageForm($request);
				$errors = $form->validate();

				try
				{
					if (count($errors) != 0) throw new InvalidFormException();

					if ($form->isURLEntered()) $url = $form->getURL();
					else $url = urlencode($form->getTitle());

					$this->updatePage($url, $form->getTitle(), $form->getContent(), $form->getDateTime(), $request->getSession()->getAttribute("user"));
					$request->setAttribute("post-result", "success");

					$response->sendRedirect(BackEndMapping::getHost().AdminPageListResource::getInstance()->getURI());
				}
				catch (InvalidFormException $e)
				{
					$view = new EditPageView($this);
					$request->setAttribute("page-result", "failure");
				}
				catch (ObjectExistsException $e)
				{
					$errors[] = new Error("alreadyExists");
					$view = new EditPageView($this);
					$request->setAttribute("page-result", "failure");
				}

				$request->setAttribute("page-form", $form);
				$request->setAttribute("page-errors", $errors);
			}

			if ($request->getParameter("action") == "delete")
			{
				$pages = $this->getPages();
				$page = $pages[0];

				$frontEndResource = $page->getFrontEndResource();
				$page->delete();
				$frontEndResource->delete();
				$this->delete();

				$response->sendRedirect(BackEndMapping::getHost().AdminPageListResource::getInstance()->getURI());
			}		
		}

		return $view;

	}

	private function updatePage($url, $title, $content, $dateTime, User $user)
	{
		$pages = $this->getPages();
		$page = clone $pages[0];

		$oldPageResource = null;
		$oldAdminPageResource = null;

		if ($url != $page->getFrontEndURI())
		{
			if (FrontEndMapping::findByURI($url) != null) throw new ObjectExistsException();

			$oldPageResource = $page->getFrontEndResource();

			$pageResource = new PageResource();
			$pageResource->setURI($url);
			$page->setFrontEndResource($pageResource);
		}

		$adminPageUrl = AdminPageListResource::getInstance()->getURI().$url;
		if ($adminPageUrl != $page->getBackEndURI())
		{
			if (BackEndMapping::findByURI($adminPageUrl) != null) throw new ObjectExistsException();

			$oldAdminPageResource = $page->getBackEndResource();

			$adminPageResource = new AdminPageResource();
			$adminPageResource->setURI($adminPageUrl);
			$page->setBackEndResource($adminPageResource);
		}

		$page->setDateTime($dateTime);
		//$page->setUser($user);
		$page->setTitle($title);
		$page->setContent($content);

		$page->save();
		
		if ($oldPageResource != null) $oldPageResource->delete();
		if ($oldAdminPageResource != null) $oldAdminPageResource->delete();
	}

} // AdminPageResource
Return current item: DIY Blog