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

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

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

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

class AdminPostListResource extends BackEndResource
{

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

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

	public static function getInstance()
	{
		$c = new Criteria();
		$c->add(BackEndResourcePeer::TYPE, BackEndResourcePeer::CLASSKEY_ADMINPOSTLISTRESOURCE);
		return BackEndResourcePeer::doSelectOne($c);
	}

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

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

			if ($request->getParameter("view") == "new") $view = new CreatePostView($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 PostListView($this);

			if ($request->getParameter("action") == "create")
			{
				if (!($request->getSession()->getAttribute("user") instanceof AdminUser))
				{
					$response->setStatus(Response::SC_FORBIDDEN);
					return new ForbiddenView($this);
				}

				$form = new PageForm($request);
				$errors = array();
				$errors = $form->validate();

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

					if ($form->isURLEntered()) $url = $form->getURL();
					else $url = urlencode($form->getTitle());
					$this->createPost($url, $form->getTitle(), $form->getContent(), $form->getDateTime(), $request->getSession()->getAttribute("user"));

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

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

		}
		return $view;
	}

	private function createPost($url, $title, $content, $dateTime, User $user)
	{
		$year = substr($dateTime, 0, 4);
		$month = substr($dateTime, 4, 2);
		$day = substr($dateTime, 6, 2);

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

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

		// add yearly/monthly resources for archive, if they do not exist
		$yearUrl = $postListResource->getURI().urlencode($year)."/";
		if (FrontEndMapping::findByURI($yearUrl) == null)
		{
			$yearlyResource = new YearlyPostListResource();
			$yearlyResource->setURI($yearUrl);
			$yearlyResource->save();
		}
		$monthUrl = $yearUrl.urlencode($month)."/";
		if (FrontEndMapping::findByURI($monthUrl) == null)
		{
			$monthlyResource = new MonthlyPostListResource();
			$monthlyResource->setURI($monthUrl);
			$monthlyResource->save();
		}

		$post = new PostPage();
		$post->setDateTime($dateTime);
		$post->setUser($user);
		$post->setTitle($title);
		$post->setContent($content);

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

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

		$post->save();
	}

} // AdminPostListResource
Return current item: DIY Blog