Newer
Older
namespace SGalinski\TypoScriptReferenceFrontend\Controller;
/* *
* This script belongs to the TYPO3 Flow package "SGalinski.TypoScriptReferenceFrontend".*
* *
* */
use SGalinski\TypoScriptReferenceFrontend\Domain\Model\Property;
use SGalinski\TypoScriptReferenceFrontend\Domain\Model\Type;
use SGalinski\TypoScriptReferenceFrontend\Service\TsrefRestService;
use SGalinski\TypoScriptReferenceFrontend\Utilities\Conversion;
class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
* @var TsrefRestService
*/
protected $tsrefRestService;
*/
protected $securityContext;
/**
* @var SessionInterface
*/
protected $session;
/**
* The constructor is used for dependency injection.
*
* @param TsrefRestService $service
public function __construct(TsrefRestService $service, SessionInterface $session) {
$this->tsrefRestService = $service;
$this->session = $session;
if (!$session->isStarted()) {
$session->start();
$session->putData('typo3Version', TsrefRestService::TYPO3_CURRENT_VERSION_LABEL);
}
}
/**
* The action which fetches the data for typoScript reference page, and displays the page.
* @param string $typeUrlName - typeUrlName
public function indexAction(
$typoScriptGroup, $typeUrlName = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
$selectedType = $this->tsrefRestService->getTypeByUrlName($typeUrlName, $typoScriptGroup);
$properties = $this->tsrefRestService->getPropertiesByParentTypeId(
$selectedType->id, $this->decodeTypo3Version($typo3Version), $typoScriptGroup
);
$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->getTypeById($selectedType->parent_id, $typoScriptGroup);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
$urlName = isset($selectedType) ? (isset($selectedType->urlName) ? $selectedType->urlName : NULL) : NULL;
$this->prepareTypeMenu($urlName, $typo3Version, $typoScriptGroup);
* Adds a new type or saves changes in edited type.
public function submitTypeAction( //TODO: Use session for typo3Version
Type $theType, $editForm = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $typoScriptGroup
$this->checkEditPermission($typoScriptGroup, $typo3Version);
if ($theType->getExtends() === -1) {
$theType->setExtends(NULL);
$result = $this->tsrefRestService->editType($theType, $typoScriptGroup);
$result = $this->tsrefRestService->addNewType($theType, $typoScriptGroup);
}
$this->addFlashMessage('The type is successfully submitted.', 'Success', Message::SEVERITY_OK);
// TODO: get edited type and use its urlName to redirect to
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
$this->redirect(
'editType', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
[
'typeId' => $theType->getId(),
'typo3Version' => $typo3Version,
'typoScriptGroup' => $typoScriptGroup
]
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
[
'typeUrlName' => $theType->getUrlName(),
'typo3Version' => $typo3Version,
'typoScriptGroup' => $typoScriptGroup
]
}
/**
* Prepares and opens the page for adding new / editing the type
public function editTypeAction(
$typeId = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL,
$typoScriptGroup
$this->checkEditPermission($typoScriptGroup, $typo3Version);
$selectedTypeAsStdClass = $this->tsrefRestService->getTypeById($typeId, $typoScriptGroup);
$theType->initialise($selectedTypeAsStdClass);
$typoScriptGroupId = $this->tsrefRestService->getTypoScriptGroupId($typoScriptGroup);
$theType->setTypo3Group($typoScriptGroupId);
}
$selectMenuTypes = $this->tsrefRestService->getTypesWithNull(
$this->decodeTypo3Version($typo3Version), $typoScriptGroup
);
$categories = $this->tsrefRestService->getCategories();
$selectBoxCategories = Conversion::categoriesToAssociativeNameArray($categories);
$this->view->assign('theType', $theType);
$this->view->assign('editForm', ($typeId !== NULL));
$this->view->assign('selectMenuTypes', $selectMenuTypes);
$this->view->assign('selectBoxCategories', $selectBoxCategories);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
}
$this->prepareTypeMenu($theType->getUrlName(), $typo3Version, $typoScriptGroup);
/**
* Adds a new property or saves the changes in edited property.
*
* @param Property $theProperty
* @param boolean $editForm
* @return void
*/
public function submitPropertyAction( //TODO: Use session for typo3Version
Property $theProperty, $editForm, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL,
$typoScriptGroup
$this->checkEditPermission($typoScriptGroup, $typo3Version);
$result = $this->tsrefRestService->editProperty($theProperty, $typoScriptGroup);
$result = $this->tsrefRestService->addNewProperty($theProperty, $typoScriptGroup);
$this->addFlashMessage('The property is successfully submitted.', 'Success', Message::SEVERITY_OK);
$parentType = $this->tsrefRestService->getTypeById($theProperty->getParentType(), $typoScriptGroup);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
$this->redirect(
'editProperty', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
[
'parentTypeId' => $theProperty->getParentType(),
'thePropertyId' => $theProperty->getId(),
'typo3Version' => $typo3Version,
'typoScriptGroup' => $typoScriptGroup
]
$urlName = (isset($parentType) ? (isset($parentType->urlName) ? $parentType->urlName : NULL) : NULL);
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
[
'typeUrlName' => $urlName,
'typo3Version' => $typo3Version,
'typoScriptGroup' => $typoScriptGroup
]
);
}
/**
* Prepares and opens the page for adding new / editing the property
*
* @param int $parentTypeId
* @param int $thePropertyId
public function editPropertyAction( //TODO: Use session for typo3Version
$parentTypeId, $thePropertyId = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL,
$typoScriptGroup
$this->checkEditPermission($typoScriptGroup, $typo3Version);
$theProperty = new Property();
$selectedPropertyAsStdClass = $this->tsrefRestService->getPropertyById(
$thePropertyId, $typoScriptGroup
);
$theProperty->initialise($selectedPropertyAsStdClass);
$typoScriptGroupId = $this->tsrefRestService->getTypoScriptGroupId($typoScriptGroup);
$theProperty->setTypo3Group($typoScriptGroupId);
$theProperty->setParentType($parentTypeId);
$parentType = $this->tsrefRestService->getTypeById($parentTypeId, $typoScriptGroup);
$selectMenuTypes = Conversion::typesToAssociativeIdArray(
$this->tsrefRestService->getTypes(TRUE, $this->decodeTypo3Version($typo3Version), $typoScriptGroup)
$this->view->assign('theProperty', $theProperty);
$this->view->assign('editForm', ($thePropertyId !== NULL));
$this->view->assign('selectMenuTypes', $selectMenuTypes);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
}
$urlName = isset($parentType) ? (isset($parentType->urlName) ? $parentType->urlName : NULL) : NULL;
$this->prepareTypeMenu($urlName, $typo3Version, $typoScriptGroup);
* Deletes type by $attributeId.
*
* @param int $attributeId
* @param string $typo3Version
* @return void
*/
public function deleteTypeAction( //TODO: Use session for typo3Version
$attributeId, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $typoScriptGroup
$this->checkEditPermission($typoScriptGroup, $typo3Version);
$result = $this->tsrefRestService->patchType($attributeId, ['deleted' => TRUE], $typoScriptGroup);
$this->addFlashMessage('Successfully deleted.', 'Success', Message::SEVERITY_OK);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
}
$this->redirect(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => NULL, 'typo3Version' => $typo3Version, 'typoScriptGroup' => $typoScriptGroup]
);
}
/**
* Deletes property by $attributeId.
* $parentTypeUrlName should be specified,
* and then that type will be presented after deletion of the property.
*
* @param int $attributeId
* @param string $parentTypeUrlName
public function deletePropertyAction( //TODO: Use session for typo3Version
$attributeId, $parentTypeUrlName = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL,
$typoScriptGroup
$this->checkEditPermission($typoScriptGroup, $typo3Version, $parentTypeUrlName);
$result = $this->tsrefRestService->patchProperty($attributeId, ['deleted' => TRUE], $typoScriptGroup);
$this->addFlashMessage('Successfully deleted.', 'Success', Message::SEVERITY_OK);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
}
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => $parentTypeUrlName, 'typo3Version' => $typo3Version, 'typoScriptGroup' => $typoScriptGroup]
);
}
/**
* Submits typo3 version to be used in filtering.
*
* @param string $submittedTypo3Version
public function submitTypo3Version($submittedTypo3Version, $typoScriptGroup) {
$this->session->putData('typo3Version', $submittedTypo3Version);
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typo3Version' => $submittedTypo3Version, 'typoScriptGroup' => $typoScriptGroup]
/**
* Download action for tsref.xml
*
* @param string $typo3Version
*/
public function downloadTsrefAction(
$typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $typoScriptGroup
try {
$typo3DecodedVersion = $this->decodeTypo3Version($typo3Version);
$tsrefXml = $this->tsrefRestService->getTsrefXml($typo3DecodedVersion, $typoScriptGroup);
$this->response->setContent($tsrefXml);
$this->response->setHeader('Content-Type', 'xml');
$this->response->setHeader('Content-Disposition', 'attachment;filename="tsref.xml"');
$this->view = NULL;
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
$this->redirect(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typo3Version' => $typo3Version, 'typoScriptGroup' => $typoScriptGroup]
/**
* Sets view variables needed for type menu.
*
* @param int $selectedTypeUrlName
protected function prepareTypeMenu($selectedTypeUrlName, $selectedTypo3Version, $typoScriptGroup) {
try {
$typo3VersionFilter = $this->decodeTypo3Version($selectedTypo3Version);
TRUE, $typo3VersionFilter, $typoScriptGroup
);
$typo3Versions = $this->tsrefRestService->getTypo3Versions();
$categories = $this->tsrefRestService->getCategories();
$categories = Conversion::categoriesToAssociativeNameArray($categories);
$groupedTypes = Conversion::groupTypesForSidebar($types, $categories);
$this->view->assign('menuTypes', $groupedTypes);
$this->view->assign('selectedTypeUrlName', $selectedTypeUrlName);
$this->view->assign('typo3Versions', $typo3Versions);
$this->view->assign('selectedTypo3Version', $selectedTypo3Version);
$this->view->assign('selectedTypoScriptGroup', $typoScriptGroup);
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
}
/**
* Translates TYPO3 version selection from user-friendly value to actual value
* which is used in backend for filtering.
*
* @return null|string
*/
protected function decodeTypo3Version($selectedTypo3Version) {
if (strcasecmp($selectedTypo3Version, TsrefRestService::TYPO3_CURRENT_VERSION_LABEL) === 0) {
return TsrefRestService::TYPO3_DEFAULT_VERSION;
}
if (strcasecmp($selectedTypo3Version, 'all') === 0) {
return NULL;
}
return $selectedTypo3Version;
}
/**
* Checks if client is authenticated and if he has privileges to edit the data.
* Sets error message if client can't edit the data, and redirects to index action.
* @param string $typoScriptGroup
* @param string $typo3Version
* @param string $typeUrlName
* @return void
*/
protected function checkEditPermission(
$typoScriptGroup, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $typeUrlName = NULL
) {
if (!$this->securityContext->hasRole('SGalinski.TypoScriptReferenceFrontend:Admin')) {
$this->addFlashMessage('You don\'t have permission to edit the data.', 'Error', Message::SEVERITY_ERROR);
$this->redirect(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
[
'typeUrlName' => $typeUrlName,
'typo3Version' => $typo3Version,
'typoScriptGroup' => $typoScriptGroup
]