<?php

	namespace Mocha\UI\Pages;
	use Mocha\Core\InstanceKey;
	use Mocha\Core\KnownClassGuids;
	use Mocha\Core\KnownInstanceGuids;
	use Mocha\Core\KnownRelationshipGuids;
	use Mocha\Core\OmsContext;
	use Mocha\UI\Renderers\HTML\HTMLRenderer;
	use Phast\RenderingEventArgs;
	use Phast\WebPage;

	class RelatedTaskPage extends WebPage
	{

		protected function OnRendering(RenderingEventArgs $re)
		{
			parent::OnRendering($re);

			/**
			 * @var \Mocha\Oms\MySQLDatabaseOms
			 */
			$oms = mocha_get_oms();

			$instanceKey = InstanceKey::Parse($this->Page->GetPathVariableValue("instid"));
			$relatedTaskKey = InstanceKey::Parse($this->Page->GetPathVariableValue("reltaskid"));

			$instance = $oms->getInstanceByKey($instanceKey);
			$relatedTaskInstance = $oms->getInstanceByKey($relatedTaskKey);
			$initiatingElement = $oms->getRelatedInstance($relatedTaskInstance, KnownRelationshipGuids::Task__has_initiating__Element);

			$context = new OmsContext();

			$context->setWorkData($oms->normalizeInstanceReference(KnownInstanceGuids::WorkSet__RelatedInstance), $instance);
			$context->setWorkData($oms->getParentClass($instance), $instance);
			
			$taskRenderer = new HTMLRenderer($context);
			$taskRenderer->TargetInstance = $instance;
			$taskRenderer->IsPostback = $this->Page->IsPostback;
			if ($this->Page->IsPostback)
			{
				$taskRenderer->processPostback(element: $initiatingElement);
			}
			$taskRenderer->renderTask($relatedTaskInstance, $instance);
			exit();
		}

	}
?>