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

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

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

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

class AdminPostResource extends BackEndResource
{

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

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

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

		if ($parent != null) $view = $parent;
		else $view = new EditPostView($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 EditPostView($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->updatePost($url, $form->getTitle(), $form->getContent(), $form->getDateTime(), $request->getSession()->getAttribute("user"));
					$request->setAttribute("post-result", "success");

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

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

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

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

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

		return $view;

	}

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

		$year = substr($dateTime, 0, 4);
		$month = substr($dateTime, 4, 2);
		$day = substr($dateTime, 6, 2);

		$oldPostResource = null;
		$oldAdminPostResource = null;

		$postListResource = PostListResource::getInstance();
		$postUrl = $postListResource->getURI().urlencode($year)."/".urlencode($month)."/".urlencode($day)."/".$url;
		if ($postUrl != $post->getFrontEndURI())
		{
			if (FrontEndMapping::findByURI($postUrl) != null) throw new ObjectExistsException();

			$oldPostResource = $post->getFrontEndResource();

			$postResource = new PostResource();
			$postResource->setURI($postUrl);
			$post->setFrontEndResource($postResource);
		}

		$adminPostUrl = AdminPostListResource::getInstance()->getURI().$url;
		if ($adminPostUrl != $post->getBackEndURI())
		{
			if (BackEndMapping::findByURI($adminPostUrl) != null) throw new ObjectExistsException();

			$oldAdminPostResource = $post->getBackEndResource();

			$adminPostResource = new AdminPostResource();
			$adminPostResource->setURI($adminPostUrl);
			$post->setBackEndResource($adminPostResource);
		}

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

		$post->save();

		if ($oldPostResource != null) $oldPostResource->delete();
		if ($oldAdminPostResource != null) $oldAdminPostResource->delete();
	}

} // AdminPostResource
Return current item: DIY Blog