Commit 936f1fda authored by Dainis Abols's avatar Dainis Abols
Browse files

TYPO3 v11 remake

Added Type Classes, renamed Models, removed deprecations, reformatted most of the code, fixed unit limitations

Version 4.0.1
parent aa7a9693
......@@ -2,12 +2,18 @@
namespace Lu\LuContacts\Controller;
use Lu\LuContacts\Models\Alphabet;
use Lu\LuContacts\Models\Contacts;
use Lu\LuContacts\Models\Navigation;
use Lu\LuContacts\Models\Person;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use Lu\LuApi\Helpers\DataHelper;
use Lu\LuContacts\Models\AlphabetModel;
use Lu\LuContacts\Models\ContactsModel;
use Lu\LuContacts\Models\NavigationModel;
use Lu\LuContacts\Models\PersonModel;
use Lu\LuContacts\Models\RequestModel;
use Lu\LuContacts\Models\UnitModel;
use TYPO3\CMS\Core\Http\ImmediateResponseException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Frontend\Controller\ErrorController;
/**
* Class ContactsController
......@@ -21,93 +27,6 @@ use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
*/
class ContactsController extends ActionController
{
/**
* Define allowed request options
*
* @var array
*/
private $allowedOptions = ['listType', 'unit', 'address', 'letter', 'keyword', 'personId'];
/**
* Currently active alphabet
*
* @var array
*/
private $alphabet;
/**
* Set up the doc header properly here
*
* @param ViewInterface $view
*
* @return void
*/
protected function initializeView(ViewInterface $view)
{
parent::initializeView($view);
}
/**
* Retrieve request or return default, if none found
*
* @param $request
*
* @return mixed
*/
private function getRequest($request)
{
return $this->request->hasArgument($request) && in_array($request, $this->allowedOptions) ? $this->request->getArgument($request) : '';
}
/**
* Get List Type of requested contacts. If request not provided, check plugin settings.
*
* @return string
*/
private function getListType()
{
$unitId = !empty($this->getRequest('listType')) ? $this->getRequest('listType') : $this->settings['defaultNavigation'];
return (string)$unitId;
}
/**
* Get Unit Parent ID. If request not provided, check plugin settings.
*
* @return string
*/
private function getUnitId()
{
$unitId = !empty($this->getRequest('unit')) ? $this->getRequest('unit') : $this->settings['parentId'];
return (string)$unitId;
}
/**
* Get Address ID. If request not provided, check plugin settings for address name.
*
* @return string
*/
private function getAddressId()
{
$addressId = !empty($this->getRequest('address')) ? $this->getRequest('address') : $this->settings['addressId'];
return (string)$addressId;
}
/**
* Get person letter and return 1st character. If none present, return `a`
*
* @return mixed
*/
private function getPersonLetter()
{
$personLetter = $this->getRequest('letter');
$personLetter = (string)$personLetter;
return $personLetter ? mb_substr($personLetter, 0, 1, 'UTF8') : 'a';
}
/**
* List Action
*
......@@ -122,86 +41,66 @@ class ContactsController extends ActionController
public function listAction()
{
// Get request params
$listType = $this->getListType();
$unitId = $this->getUnitId();
$addressId = $this->getAddressId();
$personLetter = $this->getPersonLetter();
$request = new RequestModel();
$request->setSettings($this->settings);
$request->init($this->request);
// Create navigation
$NavigationModel = new Navigation($this->settings);
$NavigationModel->setActive($listType);
$NavigationModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$NavigationModel = new NavigationModel($this->settings);
$NavigationModel->setActive($request->getListType());
$NavigationModel->setUriBuilder(GeneralUtility::makeInstance(UriBuilder::class)->reset()->setCreateAbsoluteUri(true));
$nav = $NavigationModel->getNavigation();
// Create alphabet menu
$AlphabetMenu = new Alphabet($this->settings);
$AlphabetMenu->setActive($personLetter);
$AlphabetMenu->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$this->alphabet = $AlphabetMenu->getAlphabet();
$AlphabetModel = new AlphabetModel($this->settings);
$AlphabetModel->setActive($request->getLetter());
$AlphabetModel->setUriBuilder(GeneralUtility::makeInstance(UriBuilder::class)->reset()->setCreateAbsoluteUri(true));
$alphabet = $AlphabetModel->getAlphabet();
// Search option has different approach
if ($listType == 'search') {
$searchKeyword = $this->getRequest('keyword');
$searchKeyword = trim(strip_tags($searchKeyword));
$ContactsModel = new ContactsModel();
if ($request->getListType() === 'search') {
$data = [
'section' => 'search',
];
if ($searchKeyword) {
$ContactsModel = new Contacts();
$ContactsModel->setAlphabet($this->alphabet);
$ContactsModel->setRequestType($listType);
$ContactsModel->setPersonLetter($personLetter);
if ($unitId) {
$ContactsModel->setUnitParentId($unitId);
}
if ($addressId) {
$ContactsModel->setAddressId($addressId);
}
$ContactsModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$data = $ContactsModel->searchDataList($searchKeyword);
if ($request->getKeyword()) {
$data = $ContactsModel->getContactsModel($request, $alphabet)->searchDataList($request->getKeyword());
// Reformat person data
$PersonModel = new Person($this->settings);
$PersonModel->setUnitParentId($unitId);
$PersonModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$data = [
'error' => $data['error'] ?? null,
$PersonModel = new PersonModel($this->settings);
$data = [
'error' => $data['error'] ?? null,
'section' => 'search',
'previousSearch' => $searchKeyword,
'persons' => $PersonModel->filterPersonList($data['persons']),
'previousSearch' => $request->getKeyword(),
'persons' => $PersonModel->filterPersonList($data['persons'])->toList(),
];
}
} else {
$ContactsModel = new Contacts();
$ContactsModel->setAlphabet($this->alphabet);
$ContactsModel->setRequestType($listType);
$ContactsModel->setPersonLetter($personLetter);
if ($unitId) {
$ContactsModel->setUnitParentId($unitId);
$data = $ContactsModel->getContactsModel($request, $alphabet)->getDataList();
if (!empty($data['data'])) {
$data['data'] = array_values($data['data']);
}
if ($addressId) {
$ContactsModel->setAddressId($addressId);
}
$ContactsModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$data = $ContactsModel->getDataList();
// Reformat person data
$PersonModel = new Person($this->settings);
$PersonModel->setUnitParentId($unitId);
$PersonModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$data['persons'] = $PersonModel->filterPersonList($data['persons']);
$PersonModel = new PersonModel($this->settings);
$data['persons'] = $PersonModel->filterPersonList($data['persons'])->toList();
// If config has unitId, check if person can be viewed
if (!empty($this->settings['parentId'])) {
$UnitModel = new UnitModel();
if (!$UnitModel->validateUnit($data, $this->settings['parentId'], true)) {
$response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'Entity not found'
);
throw new ImmediateResponseException($response);
}
}
}
// Assign data to view
// Assign data
$this->view->assign('content', $data);
// Assign alphabet menu to view, if requested
$this->view->assign('alphabet', $this->alphabet);
// Assign navigation to view, if requested
$this->view->assign('alphabet', $alphabet);
if ($this->settings['showNavigation']) {
$this->view->assign('nav', $nav);
}
......@@ -212,24 +111,32 @@ class ContactsController extends ActionController
*/
public function personAction()
{
// Get request params
$request = new RequestModel();
$request->setSettings($this->settings);
$request->init($this->request);
// Pre-set variables and models
$personId = $this->getRequest('personId');
$PersonModel = new Person();
$PersonModel = new PersonModel();
// If config has unitId, check if person can be viewed
if (!empty($this->settings['parentId'])) {
if (!$PersonModel->validatePerson($personId, $this->settings['parentId'], true)) {
$GLOBALS['TSFE']->pageNotFoundAndExit('Entity not found.');
if (!$PersonModel->validatePerson($request->getPersonID(), $this->settings['parentId'], true)) {
$response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
$GLOBALS['TYPO3_REQUEST'],
'Entity not found'
);
throw new ImmediateResponseException($response);
}
}
// Set id and retrieve data
$PersonModel->setPersonId($personId);
$PersonModel->setPersonId($request->getPersonID());
$solrData = $PersonModel->getFullData();
// Redirect away, if no person data found. This will also happen if no ID found
if (!$solrData) {
// @TODO: REDIRECT
// @TODO: Show Nothing found notice
}
$data = [
......@@ -241,8 +148,8 @@ class ContactsController extends ActionController
$this->view->assign('content', $data);
// Create navigation
$NavigationModel = new Navigation($this->settings);
$NavigationModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$NavigationModel = new NavigationModel($this->settings);
$NavigationModel->setUriBuilder(GeneralUtility::makeInstance(UriBuilder::class)->reset()->setCreateAbsoluteUri(true));
$nav = $NavigationModel->getNavigation();
// Assign navigation to view, if requested
......
......@@ -4,6 +4,7 @@ namespace Lu\LuContacts\Helpers;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class DataHelper
......@@ -19,8 +20,10 @@ class DataHelper
{
/**
* Localisation prefix
*
* @var string
*/
private $L10N_PREFIX = 'LLL:EXT:lu_contacts/Resources/Private/Language/locallang.xlf';
private string $L10N_PREFIX = 'LLL:EXT:lu_contacts/Resources/Private/Language/locallang.xlf';
/**
* DataHelper constructor.
......@@ -32,22 +35,12 @@ class DataHelper
@include_once ExtensionManagementUtility::extPath('lu_api')."cli/convertLuisXmlToSolrJson.php";
}
/**
* Change language file location
*
* @param string $L10N_PREFIX
*/
public function setL10NPrefix(string $L10N_PREFIX)
{
$this->L10N_PREFIX = $L10N_PREFIX;
}
/**
* Decode API returned JSON or return null, if failed
*
* @param $jsonData
*
* @return string
* @return mixed|null
*/
public function APIDecode($jsonData)
{
......@@ -79,13 +72,17 @@ class DataHelper
/**
* Retrieve LanguageService
*
* @return mixed|\TYPO3\CMS\Core\Localization\LanguageService
*
* @return \TYPO3\CMS\Core\Localization\LanguageService
*/
private function getLanguageService()
protected function getLanguageService(): LanguageService
{
// Ensure language object is initialized
\TYPO3\CMS\Core\Core\Bootstrap::initializeLanguageObject();
$languageService = $languageService instanceof LanguageService ? $languageService : GeneralUtility::makeInstance(LanguageService::class);
if (!$GLOBALS['LANG']) {
$languageService->init('default');
$GLOBALS['LANG'] = $languageService;
}
return $GLOBALS['LANG'];
}
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ namespace Lu\LuContacts\Hooks;
use Lu\LuApi\DataSources\LuisContacts;
use Lu\LuContacts\Helpers\DataHelper;
use Lu\LuContacts\Models\Navigation;
use Lu\LuContacts\Models\NavigationModel;
/**
* Class FlexFormHooks
......@@ -29,7 +29,7 @@ class FlexFormHooks
$optionList = $this->newOptionsList();
// Get navigation items
$NavigationModel = new Navigation();
$NavigationModel = new NavigationModel();
$navigtion = $NavigationModel->getNavigation();
// Create optionsList
......
<?php
namespace Lu\LuContacts\Hooks;
/**
* Class RealUrlAutoConfiguration
* AutoConfiguration-Hook for RealURL
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0.1
* @since 04.07.2018
*
* @package Lu\LuContacts\Hooks
*/
class RealUrlAutoConfiguration
{
/**
* Generates additional RealURL configuration and merges it with provided configuration
*
* @param array $params Default configuration
*
* @return array Updated configuration
*/
public function addRealUrlConfig($params)
{
return array_merge_recursive(
$params['config'], [
'postVarSets' => [
'_DEFAULT' => [
// LU Contacts (3)
'meklesana' => [
[
'GETvar' => 'tx_lucontacts_pi1[action]',
'valueMap' => [
'saraksts' => 'list',
'persona' => 'person',
],
],
[
'GETvar' => 'tx_lucontacts_pi1[listType]',
'valueMap' => [
'abc' => 'surname',
'ekas' => 'address',
'strukturvienibas' => 'unit',
'meklesana' => 'search',
],
'noMatch' => 'bypass',
],
[
'GETvar' => 'tx_lucontacts_pi1[personId]',
],
],
'unit' => [
[
'GETvar' => 'tx_lucontacts_pi1[unit]',
],
],
'query' => [
[
'GETvar' => 'tx_lucontacts_pi1[keyword]',
],
],
'burts' => [
[
'GETvar' => 'tx_lucontacts_pi1[letter]',
],
],
'adrese' => [
[
'GETvar' => 'tx_lucontacts_pi1[address]',
],
],
],
],
]
);
}
}
......@@ -7,38 +7,31 @@ use Lu\LuContacts\Helpers\DataHelper;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
/**
* Class Alphabet
* Class AlphabetModel
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0.0
* @since 29.06.2018
*
* @package Lu\LuContacts\Models
*/
class Alphabet
class AlphabetModel
{
/**
* Currently active item name
*
* @var string
*/
private $active = '';
private string $active = '';
/**
* Pre-set uri builder
*
* @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
*/
private $uriBuilder;
private ?UriBuilder $uriBuilder = null;
/**
* Settings
*
* @var array
*/
private $settings;
private ?array $settings;
/**
* Define alphabet
......@@ -47,7 +40,7 @@ class Alphabet
*
* @return array
*/
private function alphabet($count = false): array
private function alphabet(bool $count = false): array
{
// Pre-set full alphabet list
$luisContacts = new LuisContacts();
......@@ -96,7 +89,7 @@ class Alphabet
unset($fullList[$letter]);
} else if ($count) {
// Check if count requested
$fullList[$letter] = $data['data'].":".$fullList[$letter];
$fullList[$letter] = $data['data'].":".$letterCombo;
}
}
......@@ -119,7 +112,7 @@ class Alphabet
*
* @param $active
*/
public function setActive($active)
public function setActive($active): void
{
$this->active = $active;
}
......@@ -129,7 +122,7 @@ class Alphabet
*
* @return string
*/
public function getActive()
public function getActive(): string
{
return $this->active;
}
......@@ -144,21 +137,6 @@ class Alphabet
$this->uriBuilder = $uriBuilder;
}
/**
* Return letter full values
*
* @param $letter
*
* @return mixed
*/
public function getValuesByLetter($letter)
{
// Set navigation options
$alphabet = $this->alphabet();
return @$alphabet[$letter];
}
/**
* Create alphabet navigation
*
......
......@@ -4,61 +4,59 @@ namespace Lu\LuContacts\Models;
use Lu\LuApi\DataSources\LuisContacts;
use Lu\LuContacts\Helpers\DataHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
/**
* Class Contacts
* Class ContactsModel
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0.0
* @since 21.06.2018
*
* @package Lu\LuContacts\Models
*/
class Contacts
class ContactsModel
{
/**
* The action request type
*
* @var string
*/
private $request_type = '';
private string $request_type = '';
/**
* Unit Parent ID
*
* @var string
* @var string|null
*/
private $unit_parent_id;
private ?string $unit_parent_id = null;
/**
* Unit Address ID
*
* @var string
* @var string|null
*/
private $unit_address_id;
private ?string $unit_address_id = null;
/**
* Person first letter filter
*
* @var string
*/
private $person_letter;
private string $person_letter;
/**