Newer
Older
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;
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('typoScriptGroup', Attribute::NORMAL_GROUP);
$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(
$typeUrlName = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
try {
if ($typeUrlName !== NULL) {
$selectedType = $this->tsrefRestService->getTypeByUrlName($typeUrlName);
$this->session->putData('typoScriptGroup', $selectedType->typo3Group);
$properties = $this->tsrefRestService->getPropertiesByParentId(
$selectedType->id, $this->decodeTypo3Version($typo3Version), $selectedType->typo3Group
);
$this->view->assign('properties', $properties);
$this->view->assign('selectedType', $selectedType);
$this->view->assign('typo3Groups', $this->tsrefRestService->getAllTypo3Groups());
// 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);
}
} 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);
* Adds a new type or saves changes in edited type.
* @param string $typo3Version
* @throws \TYPO3\Flow\Mvc\Exception\ForwardException
public function submitTypeAction( //TODO: Use session for typo3Version
Attribute $theType, $editForm = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
if ($theType->getParent() === -1) {
$theType->setParent(NULL);
}
// Check if the type is unique
$theTypeIsUnique = FALSE;
try {
$this->tsrefRestService->getTypeByUrlName($theType->getUrlName());
} catch (\Exception $notExist) {
if ($notExist->getCode() === 404) {
$theTypeIsUnique = TRUE;
}
}
if (!$theTypeIsUnique) {
throw new \RuntimeException('The name of the type must be unique.');
}
if ($editForm) {
$result = $this->tsrefRestService->editAttribute($theType);
} else {
$result = $this->tsrefRestService->addNewAttribute($theType);
}
$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]
);
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => $theType->getUrlName(), 'typo3Version' => $typo3Version]
}
/**
* Prepares and opens the page for adding new / editing the type
public function editTypeAction($typeId = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) { //TODO: Use session for typo3Version
$typoScriptGroup = (int) $this->session->getData('typoScriptGroup');
if ($typeId !== NULL) {
// Edit type
$selectedTypeAsStdClass = $this->tsrefRestService->getAttributeById($typeId);
$theType->initialiseAttribute($selectedTypeAsStdClass);
} else {
$theType->setTypo3Group($typoScriptGroup);
}
$selectMenuTypes = $this->tsrefRestService->getTypesWithNull(
$this->decodeTypo3Version($typo3Version), $typoScriptGroup
);
$categories = $this->tsrefRestService->getCategories();
$selectBoxCategories = Conversion::categoriesToAssociativeNameArray($categories);
$this->view->assign('typo3Groups', $this->tsrefRestService->getAllTypo3Groups());
$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);
/**
* Adds a new property or saves the changes in edited property.
*
* @param Attribute $theProperty
* @param boolean $editForm
* @param string $typo3Version
* @throws \TYPO3\Flow\Mvc\Exception\ForwardException
* @return void
*/
public function submitPropertyAction( //TODO: Use session for typo3Version
Attribute $theProperty, $editForm, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
$parentType = NULL;
try {
$this->checkEditPermission($typo3Version);
if ($editForm) {
$result = $this->tsrefRestService->editAttribute($theProperty);
} else {
$result = $this->tsrefRestService->addNewAttribute($theProperty);
}
$this->addFlashMessage('The property is successfully submitted.', 'Success', Message::SEVERITY_OK);
$parentType = $this->tsrefRestService->getAttributeById($theProperty->getParent());
} catch (\Exception $exception) {
$this->addFlashMessage($exception->getMessage(), 'Error', Message::SEVERITY_ERROR);
$this->redirect(
'editProperty', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['parentTypeId' => $theProperty->getParent(), 'thePropertyId' => $theProperty->getId(),
'typo3Version' => $typo3Version]
);
$urlName = (isset($parentType) ? (isset($parentType->urlName) ? $parentType->urlName : NULL) : NULL);
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => $urlName, 'typo3Version' => $typo3Version]
);
}
/**
* 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
$parentType = NULL;
try {
$this->checkEditPermission($typo3Version);
$theProperty = new Attribute();
$typoScriptGroup = (int) $this->session->getData('typoScriptGroup');
if ($thePropertyId !== NULL) {
// Edit type
$selectedPropertyAsStdClass = $this->tsrefRestService->getAttributeById($thePropertyId);
$theProperty->initialiseAttribute($selectedPropertyAsStdClass);
} else {
$theProperty->setTypo3Group($typoScriptGroup);
}
$theProperty->setParent($parentTypeId);
$parentType = $this->tsrefRestService->getAttributeById($parentTypeId);
$selectMenuTypes = Conversion::attributesToAssociativeIdArray(
$this->tsrefRestService->getTypes(TRUE, $this->decodeTypo3Version($typo3Version), NULL)
);
$this->view->assign('typo3Groups', $this->tsrefRestService->getAllTypo3Groups());
$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);
/**
* Deletes attribute by $attributeId.
* If the attribute is a type, $parentTypeUrlName should be NULL.
* If the attribute is a property, $parentTypeUrlName should be specified,
* and then that type will be presented after deletion of the property.
*
* @param int $attributeId
* @param string $parentTypeUrlName
public function deleteAttributeAction( //TODO: Use session for typo3Version
$attributeId, $parentTypeUrlName = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
try {
$this->checkEditPermission($typo3Version, $parentTypeUrlName);
$result = $this->tsrefRestService->patchAttribute($attributeId, ['deleted' => TRUE]);
$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]
);
}
/**
* Submits typo3 version to be used in filtering.
*
* @param string $submittedTypo3Version
* @throws \TYPO3\Flow\Mvc\Exception\ForwardException
* @return void
*/
public function submitTypo3VersionAndGroupAction($submittedTypo3Version, $submittedTypoScriptGroup) {
$this->storeToSession($submittedTypo3Version, $submittedTypoScriptGroup);
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend', ['typo3Version' => $submittedTypo3Version]
/**
* Download action for tsref.xml
*
* @param string $typo3Version
*/
public function downloadTsrefAction(
$typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $typoScriptGroup = Attribute::NORMAL_GROUP
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]
);
}
/**
* Sets view variables needed for type menu.
*
* @param int $selectedTypeUrlName
* @param string $selectedTypo3Version
* @return void
protected function prepareTypeMenu($selectedTypeUrlName, $selectedTypo3Version) {
try {
$typo3VersionFilter = $this->decodeTypo3Version($selectedTypo3Version);
$types = $this->tsrefRestService->getTypes(
TRUE, $typo3VersionFilter, $this->session->getData('typoScriptGroup')
);
$typo3Versions = $this->tsrefRestService->getTypo3Versions();
$groupedTypes = Conversion::groupTypesForSidebar($types);
$this->view->assign('menuTypes', $groupedTypes);
$this->view->assign('selectedTypeUrlName', $selectedTypeUrlName);
$this->view->assign('typo3Versions', $typo3Versions);
$this->view->assign('selectedTypo3Version', $selectedTypo3Version);
$this->view->assign('typoScriptGroups', $this->tsrefRestService->getAllTypo3Groups());
$this->view->assign('selectedTypoScriptGroup', $this->session->getData('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;
}
/**
* Stores to session $submittedTypo3Version and $submittedTypoScriptGroup.
*
* @param $submittedTypo3Version
* @param $submittedTypoScriptGroup
* @return void
*/
protected function storeToSession($submittedTypo3Version, $submittedTypoScriptGroup) {
$this->session->putData('typo3Version', $submittedTypo3Version);
$this->session->putData('typoScriptGroup', $submittedTypoScriptGroup);
}
/**
* 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 $typo3Version
* @param $parentTypeUrlName
* @return void
*/
protected function checkEditPermission(
$typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $parentTypeUrlName = 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' => $parentTypeUrlName, 'typo3Version' => $typo3Version]
);
}
}