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;
/**
* The constructor is used for dependency injection.
*
* @param TsrefRestService $service
*/
public function __construct(TsrefRestService $service) {
$this->tsrefRestService = $service;
}
/**
* The action which fetches the data for typoScript reference page, and displays the page.
* @param int $typeId
* @return void
public function indexAction($typeId = NULL) {
if ($typeId !== NULL) {
$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);
}
$this->prepareTypeMenu($typeId);
* Adds a new type or saves changes in edited type.
public function submitTypeAction(Attribute $theType, $editForm) {
if ($editForm) {
$result = $this->tsrefRestService->editAttribute($theType);
$result = $this->tsrefRestService->addNewAttribute($theType);
$this->forward('index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend', ['typeId' => $theType->getId()]);
}
/**
* Prepares and opens the page for adding new / editing the type
* @param int $theTypeId
*/
public function editTypeAction($theTypeId = NULL) {
$theType = new Attribute();
if ($theTypeId !== NULL) {
// Edit type
$selectedTypeAsStdClass = $this->tsrefRestService->getAttributeById($theTypeId);
$theType->initialiseAttribute($selectedTypeAsStdClass);
}
$this->prepareTypeMenu($theTypeId);
$typo3Groups = $this->tsrefRestService->getAllTypo3Groups();
$this->view->assign('typo3Groups', $typo3Groups);
$this->view->assign('editForm', ($theTypeId !== NULL));
/**
* Adds a new property or saves the changes in edited property.
*
* @param Attribute $theProperty
* @param boolean $editForm
* @return void
*/
public function submitPropertyAction(Attribute $theProperty, $editForm) {
if ($editForm) {
$result = $this->tsrefRestService->editAttribute($theProperty);
} else {
$result = $this->tsrefRestService->addNewAttribute($theProperty);
}
// TODO: Return error message. Or success message.
$this->forward(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend', ['typeId' => $theProperty->getParent()]
);
}
/**
* Prepares and opens the page for adding new / editing the property
*
* @param int $parentTypeId
* @param int $thePropertyId
*/
public function editPropertyAction($parentTypeId, $thePropertyId = NULL) {
$theProperty = new Attribute();
if ($thePropertyId !== NULL) {
// Edit type
$selectedPropertyAsStdClass = $this->tsrefRestService->getAttributeById($thePropertyId);
$theProperty->initialiseAttribute($selectedPropertyAsStdClass);
}
$theProperty->setParent($parentTypeId);
$this->prepareTypeMenu($parentTypeId);
$typo3Groups = $this->tsrefRestService->getAllTypo3Groups();
$this->view->assign('typo3Groups', $typo3Groups);
$this->view->assign('theProperty', $theProperty);
$this->view->assign('editForm', ($thePropertyId !== NULL));
/**
* 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
* @throws \TYPO3\Flow\Mvc\Exception\ForwardException
*/
public function deleteAttributeAction($attributeId, $parentTypeId = NULL) {
$result = $this->tsrefRestService->patchAttribute($attributeId, ['deleted' => TRUE]);
// TODO: Return error message. Or success message.
$this->forward(
'index', 'tsref', 'SGalinski.TypoScriptReferenceFrontend', ['typeId' => $parentTypeId]
);
}
/**
* Sets view variables needed for type menu.
*
* @param $theTypeId
*/
protected function prepareTypeMenu($theTypeId) {
$types = $this->tsrefRestService->getTypes();
$associativeTypes = Conversion::toAssociativeIdNamesArray($types);
$this->view->assign('types', $associativeTypes);
$this->view->assign('selectedTypeId', $theTypeId);
}