Skip to content
Snippets Groups Projects
Commit 4a1bb03e authored by damjan's avatar damjan
Browse files

[FEATURE] Flash messages

- Catching exceptions and reporting them via flash messages in each controller action
- Reporting success via flash messages
- Checking response of typoScriptBackend web resource for errors and handling the errors
- Fixing AccessArrayViewHelper not to throw error if given array is null
- A try to convert Fluid flashMessages html to html which can be styled by Bootstrap
parent 5557d039
No related branches found
No related tags found
No related merge requests found
......@@ -58,24 +58,30 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
public function indexAction(
$typeUrlName = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) {
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);
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);
}
$this->prepareTypeMenu((isset($selectedType) ? $selectedType->urlName : NULL), $typo3Version);
$urlName = isset($selectedType) ? (isset($selectedType->urlName) ? $selectedType->urlName : NULL) : NULL;
$this->prepareTypeMenu($urlName, $typo3Version);
}
/**
......@@ -88,20 +94,32 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
* @return void
*/
public function submitTypeAction( //TODO: Use session for typo3Version
Attribute $theType, $editForm, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
Attribute $theType, $editForm = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) {
$this->checkEditPermission($typo3Version);
try {
$this->checkEditPermission($typo3Version);
if ($theType->getParent() === -1) {
$theType->setParent(NULL);
}
if ($editForm) {
$result = $this->tsrefRestService->editAttribute($theType);
} else {
$result = $this->tsrefRestService->addNewAttribute($theType);
if ($theType->getParent() === -1) {
$theType->setParent(NULL);
}
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]
);
}
// TODO: get edited type and use its urlName to redirect to
// TODO: Return error message. Or success message.
$this->redirect(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => $theType->getUrlName(), 'typo3Version' => $typo3Version]
......@@ -117,32 +135,37 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
*/
public function editTypeAction($typeId = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) { //TODO: Use session for typo3Version
$this->checkEditPermission($typo3Version);
$theType = new Attribute();
$typoScriptGroup = (int) $this->session->getData('typoScriptGroup');
if ($typeId !== NULL) {
// Edit type
$selectedTypeAsStdClass = $this->tsrefRestService->getAttributeById($typeId);
$theType->initialiseAttribute($selectedTypeAsStdClass);
} else {
$theType->setTypo3Group($typoScriptGroup);
}
try {
$this->checkEditPermission($typo3Version);
$this->prepareTypeMenu($theType->getUrlName(), $typo3Version);
$selectMenuTypes = $this->tsrefRestService->getTypesWithNull(
$this->decodeTypo3Version($typo3Version), $typoScriptGroup
);
$typoScriptGroup = (int) $this->session->getData('typoScriptGroup');
$categories = $this->tsrefRestService->getCategories();
$selectBoxCategories = Conversion::categoriesToAssociativeNameArray($categories);
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->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);
$this->prepareTypeMenu($theType->getUrlName(), $typo3Version);
}
/**
......@@ -157,19 +180,33 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
public function submitPropertyAction( //TODO: Use session for typo3Version
Attribute $theProperty, $editForm, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) {
$this->checkEditPermission($typo3Version);
$parentType = NULL;
try {
$this->checkEditPermission($typo3Version);
if ($editForm) {
$result = $this->tsrefRestService->editAttribute($theProperty);
} else {
$result = $this->tsrefRestService->addNewAttribute($theProperty);
}
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]
);
}
// TODO: Return error message. Or success message.
$parentType = $this->tsrefRestService->getAttributeById($theProperty->getParent());
$urlName = (isset($parentType) ? (isset($parentType->urlName) ? $parentType->urlName : NULL) : NULL);
$this->redirect(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => ($parentType ? $parentType->urlName : NULL), 'typo3Version' => $typo3Version]
['typeUrlName' => $urlName, 'typo3Version' => $typo3Version]
);
}
......@@ -184,31 +221,38 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
public function editPropertyAction( //TODO: Use session for typo3Version
$parentTypeId, $thePropertyId = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) {
$this->checkEditPermission($typo3Version);
$theProperty = new Attribute();
$typoScriptGroup = (int) $this->session->getData('typoScriptGroup');
$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);
}
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);
$theProperty->setParent($parentTypeId);
$parentType = $this->tsrefRestService->getAttributeById($parentTypeId);
$selectMenuTypes = Conversion::attributesToAssociativeIdArray(
$this->tsrefRestService->getTypes(TRUE, $this->decodeTypo3Version($typo3Version), NULL)
);
$this->prepareTypeMenu(($parentType ? $parentType->urlName : NULL), $typo3Version);
$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);
}
$this->view->assign('typo3Groups', $this->tsrefRestService->getAllTypo3Groups());
$this->view->assign('theProperty', $theProperty);
$this->view->assign('editForm', ($thePropertyId !== NULL));
$this->view->assign('selectMenuTypes', $selectMenuTypes); // TODO: Better name is selectBoxTypes
$urlName = isset($parentType) ? (isset($parentType->urlName) ? $parentType->urlName : NULL) : NULL;
$this->prepareTypeMenu($urlName, $typo3Version);
}
/**
......@@ -225,10 +269,15 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
public function deleteAttributeAction( //TODO: Use session for typo3Version
$attributeId, $parentTypeUrlName = NULL, $typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL
) {
$this->checkEditPermission($typo3Version, $parentTypeUrlName);
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);
}
$result = $this->tsrefRestService->patchAttribute($attributeId, ['deleted' => TRUE]);
// TODO: Return error message. Or success message.
$this->redirect(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend',
['typeUrlName' => $parentTypeUrlName, 'typo3Version' => $typo3Version]
......@@ -260,13 +309,20 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
public function downloadTsrefAction(
$typo3Version = TsrefRestService::TYPO3_CURRENT_VERSION_LABEL, $typoScriptGroup = Attribute::NORMAL_GROUP
) {
$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;
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]
);
}
}
/**
......@@ -277,20 +333,24 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
* @return void
*/
protected function prepareTypeMenu($selectedTypeUrlName, $selectedTypo3Version) {
$typo3VersionFilter = $this->decodeTypo3Version($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'));
$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);
}
}
/**
......@@ -325,6 +385,7 @@ class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
/**
* 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
......
......@@ -94,9 +94,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -112,9 +114,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -130,9 +134,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -171,9 +177,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -242,12 +250,14 @@ class TsrefRestService {
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $attributeJson);
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$outputJson = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $outputJson);
return $output;
return $outputJson;
}
/**
......@@ -276,9 +286,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -310,9 +322,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -339,9 +353,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -356,9 +372,11 @@ class TsrefRestService {
// Download the given URL, and return output
$output = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
$this->checkForErrors($statusCode, $output);
return $output;
}
......@@ -414,4 +432,18 @@ class TsrefRestService {
$typesAssociative = [-1 => 'No type (NULL)'] + $typesAssociative;
return $typesAssociative;
}
/**
* Checks if there was en error in response and trows exception.
*
* @param $statusCode
* @param $outputJson
*/
protected function checkForErrors($statusCode, $outputJson) {
if (gettype($statusCode) === 'integer' && $statusCode >= 400) {
$outputObject = json_decode($outputJson);
$message = isset($outputObject->message) ? $outputObject->message : NULL;
throw new \RuntimeException($message, $statusCode);
}
}
}
\ No newline at end of file
......@@ -15,8 +15,12 @@ class AccessArrayViewHelper extends AbstractViewHelper {
*
* @param array $array
* @param string|int $index
* @return string
*/
public function render(array $array, $index) {
public function render($array, $index) {
if (gettype($array) !== 'array') {
return '';
}
return $array[$index];
}
}
......@@ -13,7 +13,49 @@
<f:render partial="NavigationSidebar" arguments="{_all}" />
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<f:flashMessages class="flashmessages" />
<f:flashMessages as="flashMessages">
<f:for each="{flashMessages}" as="flashMessage">
<f:if condition="{flashMessage.severity} == 'OK'">
<div class="alert alert-success">
<a class="close" data-dismiss="alert" href="#">×</a>
<f:if condition="{flashMessage.title}">
<h4 class="alert-heading">{flashMessage.title}</h4>
</f:if>
{flashMessage}
</div>
</f:if>
<f:if condition="{flashMessage.severity} == 'Notice'">
<div class="alert alert-notice">
<a class="close" data-dismiss="alert" href="#">×</a>
<f:if condition="{flashMessage.title}">
<h4 class="alert-heading">{flashMessage.title}</h4>
</f:if>
{flashMessage}
</div>
</f:if>
<f:if condition="{flashMessage.severity} == 'Warning'">
<div class="alert alert-warning">
<a class="close" data-dismiss="alert" href="#">×</a>
<f:if condition="{flashMessage.title}">
<h4 class="alert-heading">{flashMessage.title}</h4>
</f:if>
{flashMessage}
</div>
</f:if>
<f:if condition="{flashMessage.severity} == 'Error'">
<div class="alert alert-error">
<a class="close" data-dismiss="alert" href="#">×</a>
<f:if condition="{flashMessage.title}">
<h4 class="alert-heading">{flashMessage.title}</h4>
</f:if>
{flashMessage}
</div>
</f:if>
</f:for>
</f:flashMessages>
<f:if condition="{selectedTypeUrlName}">
<f:then>
<h1 class="page-header">{selectedType.name}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment