Location: PHPKode > projects > DIY Blog > diy-blog/classes/diy-blog/model/AdminPageListResource.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 AdminPageListResource extends BackEndResource
{

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

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

	public static function getInstance()
	{
		$c = new Criteria();
		$c->add(BackEndResourcePeer::TYPE, BackEndResourcePeer::CLASSKEY_ADMINPAGELISTRESOURCE);
		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 PageListView($this);

			if ($request->getParameter("view") == "new") $view = new CreatePageView($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 PageListView($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();

					$this->createPage($form->getURL(), $form->getTitle(), $form->getContent(), $form->getDateTime(), $request->getSession()->getAttribute("user"));

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

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

		}
		return $view;
	}

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

		if (FrontEndMapping::findByURI($url) != null) throw new ObjectExistsException();

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

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

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

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

} // AdminPageListResource
Return current item: DIY Blog