Skip to content
Snippets Groups Projects
TsrefController.php 7.33 KiB
Newer Older
damjan's avatar
damjan committed
<?php
damjan's avatar
damjan committed
namespace SGalinski\TypoScriptReferenceFrontend\Controller;

/*                                                                        *
 * This script belongs to the TYPO3 Flow package "SGalinski.TypoScriptReferenceFrontend".*
 *                                                                        *
 *                                                                        */

use SGalinski\TypoScriptReferenceFrontend\Domain\Model\Attribute;
use SGalinski\TypoScriptReferenceFrontend\Service\TsrefRestService;
damjan's avatar
damjan committed
use SGalinski\TypoScriptReferenceFrontend\Utilities\Conversion;
damjan's avatar
damjan committed
use TYPO3\Flow\Annotations as Flow;

class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
damjan's avatar
damjan committed
	/**
	 * @var TsrefRestService
	 */
	protected $tsrefRestService;

	/**
	 * @var \TYPO3\Flow\Security\Context
	 */
	protected $securityContext;

	/**
	 * The constructor is used for dependency injection.
	 *
	 * @param TsrefRestService $service
	 */
	public function __construct(TsrefRestService $service) {
		$this->tsrefRestService = $service;
	}

	/**
	 * Injects the security context
	 *
	 * @param \TYPO3\Flow\Security\Context $securityContext The security context
	 * @return void
	 */
	public function injectSecurityContext(\TYPO3\Flow\Security\Context $securityContext) {
		$this->securityContext = $securityContext;
	}

	 * The action which fetches the data for typoScript reference page, and displays the page.
damjan's avatar
damjan committed
	 * @param string $typo3Version
damjan's avatar
damjan committed
	 */
damjan's avatar
damjan committed
	public function indexAction($typeId = NULL, $typo3Version = TsrefRestService::TYPO3_DEFAULT_VERSION) {
			$selectedType = $this->tsrefRestService->getAttributeById($typeId);
			$properties = $this->tsrefRestService->getPropertiesByParentId($typeId);
			$this->view->assign('properties', $properties);
			$this->view->assign('selectedType', $selectedType);

			// If the type extends a type (superType), the superType name is being fetched among other fields
			if (isset($selectedType->parent_id)) {
				$superType = $this->tsrefRestService->getAttributeById($selectedType->parent_id);
				$this->view->assign('superType', $superType);
			}
//		$account = $this->securityContext->getAccount(); // TODO: Check if user is admin, view helper can be used
damjan's avatar
damjan committed
		$this->prepareTypeMenu($typeId, $typo3Version);
damjan's avatar
damjan committed
	}
	 * Adds a new type or saves changes in edited type.
	 *
	 * @param Attribute $theType
damjan's avatar
damjan committed
	 * @param boolean $editForm
damjan's avatar
damjan committed
	 * @param string $typo3Version
	 * @throws \TYPO3\Flow\Mvc\Exception\ForwardException
	 * @return void
	 */
damjan's avatar
damjan committed
	public function submitTypeAction(
		Attribute $theType, $editForm, $typo3Version = TsrefRestService::TYPO3_DEFAULT_VERSION
	) {
damjan's avatar
damjan committed
		if ($editForm) {
			$result = $this->tsrefRestService->editAttribute($theType);
damjan's avatar
damjan committed
		} else {
			$result = $this->tsrefRestService->addNewAttribute($theType);
damjan's avatar
damjan committed
		}
damjan's avatar
damjan committed
		// TODO: Return error message. Or success message.
damjan's avatar
damjan committed
			'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
			['typeId' => $theType->getId(), 'typo3Version' => $typo3Version]
		);
	}

	/**
	 * Prepares and opens the page for adding new / editing the type
	 * @param int $typeId
damjan's avatar
damjan committed
	 * @param string $typo3Version
	 * @return void
	public function editTypeAction($typeId = NULL, $typo3Version = TsrefRestService::TYPO3_DEFAULT_VERSION) {
		$theType = new Attribute();
		if ($typeId !== NULL) {
			// Edit type
			$selectedTypeAsStdClass = $this->tsrefRestService->getAttributeById($typeId);
			$theType->initialiseAttribute($selectedTypeAsStdClass);
		}

		$this->prepareTypeMenu($typeId, $typo3Version);
damjan's avatar
damjan committed
		$typo3Groups = $this->tsrefRestService->getAllTypo3Groups();

		$this->view->assign('typo3Groups', $typo3Groups);
		$this->view->assign('theType', $theType);
		$this->view->assign('editForm', ($typeId !== NULL));
damjan's avatar
damjan committed

	/**
	 * Adds a new property or saves the changes in edited property.
	 *
	 * @param Attribute $theProperty
	 * @param boolean $editForm
damjan's avatar
damjan committed
	 * @param string $typo3Version
	 * @throws \TYPO3\Flow\Mvc\Exception\ForwardException
damjan's avatar
damjan committed
	public function submitPropertyAction(
		Attribute $theProperty, $editForm,
		$typo3Version = TsrefRestService::TYPO3_DEFAULT_VERSION
	) {
		if ($editForm) {
			$result = $this->tsrefRestService->editAttribute($theProperty);
		} else {
			$result = $this->tsrefRestService->addNewAttribute($theProperty);
		}
		// TODO: Return error message. Or success message.
damjan's avatar
damjan committed
			'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
			['typeId' => $theProperty->getParent(), 'typo3Version' => $typo3Version]
damjan's avatar
damjan committed
	/**
	 * Prepares and opens the page for adding new / editing the property
	 *
	 * @param int $parentTypeId
	 * @param int $thePropertyId
damjan's avatar
damjan committed
	 * @param string $typo3Version
	 * @return void
damjan's avatar
damjan committed
	 */
damjan's avatar
damjan committed
	public function editPropertyAction(
		$parentTypeId, $thePropertyId = NULL, $typo3Version = TsrefRestService::TYPO3_DEFAULT_VERSION
	) {
		$theProperty = new Attribute();

		if ($thePropertyId !== NULL) {
			// Edit type
			$selectedPropertyAsStdClass = $this->tsrefRestService->getAttributeById($thePropertyId);
			$theProperty->initialiseAttribute($selectedPropertyAsStdClass);
		}
		$theProperty->setParent($parentTypeId);

damjan's avatar
damjan committed
		$this->prepareTypeMenu($parentTypeId, $typo3Version);
		$typo3Groups = $this->tsrefRestService->getAllTypo3Groups();

		$this->view->assign('typo3Groups', $typo3Groups);
		$this->view->assign('theProperty', $theProperty);
		$this->view->assign('editForm', ($thePropertyId !== NULL));
damjan's avatar
damjan committed
	}
	/**
	 * Deletes attribute by $attributeId.
	 * If the attribute is a type, $parentTypeId should be NULL.
	 * If the attribute is a property, $parentTypeId should be specified,
	 * and then that type will be presented after deletion of the property.
	 *
	 * @param int $attributeId
	 * @param int $parentTypeId
damjan's avatar
damjan committed
	 * @param string $typo3Version
	 * @throws \TYPO3\Flow\Mvc\Exception\ForwardException
damjan's avatar
damjan committed
	 * @return void
damjan's avatar
damjan committed
	public function deleteAttributeAction(
		$attributeId, $parentTypeId = NULL, $typo3Version = TsrefRestService::TYPO3_DEFAULT_VERSION
	) {
		$result = $this->tsrefRestService->patchAttribute($attributeId, ['deleted' => TRUE]);
		// TODO: Return error message. Or success message.
damjan's avatar
damjan committed
			'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
			['typeId' => $parentTypeId, 'typo3Version' => $typo3Version]
		);
	}

	/**
	 * Submits typo3 version to be used in filtering.
	 *
	 * @param string $submittedTypo3Version
damjan's avatar
damjan committed
	 * @throws \TYPO3\Flow\Mvc\Exception\ForwardException
	 * @return void
	 */
	public function submitTypo3VersionAction($submittedTypo3Version) {
damjan's avatar
damjan committed

		$this->redirect(
			'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend', ['typo3Version' => $submittedTypo3Version]
	/**
	 * Sets view variables needed for type menu.
	 *
	 * @param int $typeId
damjan's avatar
damjan committed
	 * @param string $selectedTypo3Version
	 * @return void
	protected function prepareTypeMenu($typeId, $selectedTypo3Version) {
damjan's avatar
damjan committed
		$typo3VersionFilter = $selectedTypo3Version;
		if (strcasecmp($selectedTypo3Version, 'all') === 0) {
damjan's avatar
damjan committed
			$typo3VersionFilter = NULL;
		}
		$types = $this->tsrefRestService->getTypes(TRUE, $typo3VersionFilter);
		$typo3Versions = $this->tsrefRestService->getTypo3Versions();
		$associativeTypes = Conversion::toAssociativeIdNamesArray($types);
		$this->view->assign('types', $associativeTypes);
		$this->view->assign('selectedTypeId', $typeId);
damjan's avatar
damjan committed
		$this->view->assign('typo3Versions', $typo3Versions);
		$this->view->assign('selectedTypo3Version', $selectedTypo3Version);
damjan's avatar
damjan committed
}