Commit e3b80945 authored by Dainis Abols's avatar Dainis Abols
Browse files

Initial composer version

parents
Pipeline #166 failed with stages
in 0 seconds
<?php
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 TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Class ContactsController
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0.0
* @since 20.06.2018
*
* @package Lu\LuContacts\Controller
*/
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
*
* Reads get parameter `saraksts`:
* units - unit list
* search - search (default)
* abc - alphabetical list
* addresses - address list
*
* @return void
*/
public function listAction()
{
// Get request params
$listType = $this->getListType();
$unitId = $this->getUnitId();
$addressId = $this->getAddressId();
$personLetter = $this->getPersonLetter();
// Create navigation
$NavigationModel = new Navigation($this->settings);
$NavigationModel->setActive($listType);
$NavigationModel->setUriBuilder($this->controllerContext->getUriBuilder()->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();
// Search option has different approach
if ($listType == 'search') {
$searchKeyword = $this->getRequest('keyword');
$searchKeyword = trim(strip_tags($searchKeyword));
$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);
// Reformat person data
$PersonModel = new Person($this->settings);
$PersonModel->setUnitParentId($unitId);
$PersonModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$data = [
'error' => $data['error'] ?? null,
'section' => 'search',
'previousSearch' => $searchKeyword,
'persons' => $PersonModel->filterPersonList($data['persons']),
];
}
} else {
$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->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']);
}
// Assign data to view
$this->view->assign('content', $data);
// Assign alphabet menu to view, if requested
$this->view->assign('alphabet', $this->alphabet);
// Assign navigation to view, if requested
if ($this->settings['showNavigation']) {
$this->view->assign('nav', $nav);
}
}
/**
* Person detail view
*/
public function personAction()
{
// Pre-set variables and models
$personId = $this->getRequest('personId');
$PersonModel = new Person();
// 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.');
}
}
// Set id and retrieve data
$PersonModel->setPersonId($personId);
$solrData = $PersonModel->getFullData();
// Redirect away, if no person data found. This will also happen if no ID found
if (!$solrData) {
// @TODO: REDIRECT
}
$data = [
'title' => 'Found Person Name',
'data' => $solrData,
];
// Assign data to view
$this->view->assign('content', $data);
// Create navigation
$NavigationModel = new Navigation($this->settings);
$NavigationModel->setUriBuilder($this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true));
$nav = $NavigationModel->getNavigation();
// Assign navigation to view, if requested
if ($this->settings['showNavigation']) {
$this->view->assign('nav', $nav);
}
}
}
\ No newline at end of file
<?php
namespace Lu\LuContacts\Helpers;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Lang\LanguageService;
/**
* Class DataHelper
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0.0
* @since 21.06.2018
*
* @package Lu\LuContacts\Helpers
*/
class DataHelper
{
/**
* Localisation prefix
*/
private $L10N_PREFIX = 'LLL:EXT:lu_contacts/Resources/Private/Language/locallang.xlf';
/**
* DataHelper constructor.
*
* Add additional data on init
*/
public function __construct()
{
@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
*/
public function APIDecode($jsonData)
{
// Decode result
$decoded = json_decode($jsonData, true);
if ($decoded['code'] == 200) {
$data = $decoded['data'];
} else {
$data = null;
}
return $data;
}
/**
* Retrieve language value for requested string
*
* @TODO: Implement different language requests
*
* @param string $string
*
* @return string
*/
public function getLangString(string $string): string
{
return $this->getLanguageService()->sL($this->L10N_PREFIX.':'.$string);
}
/**
* Retrieve LanguageService
*
* @return mixed|\TYPO3\CMS\Lang\LanguageService
*/
private function getLanguageService()
{
return $GLOBALS['LANG'] ?? new LanguageService();
}
}
\ No newline at end of file
<?php
namespace Lu\LuContacts\Hooks;
use Lu\LuApi\DataSources\LuisContacts;
use Lu\LuContacts\Helpers\DataHelper;
use Lu\LuContacts\Models\Navigation;
/**
* Class FlexFormHooks
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @version 3.0.0
* @since 28.06.2018
*
* @package Lu\LuContacts\Hooks
*/
class FlexFormHooks
{
/**
* Create navigation select menu
*
* @param array $config
*/
public function defaultNavigation(array &$config)
{
// Preset menu
$optionList = $this->newOptionsList();
// Get navigation items
$NavigationModel = new Navigation();
$navigtion = $NavigationModel->getNavigation();
// Create optionsList
foreach ($navigtion as $item) {
$optionList[] = [
$item['name'],
$item['id'],
];
}
// Merge into items
$config['items'] = array_merge($config['items'], $optionList);
}
/**
* Create address list select menu
*
* @param array $config
*/
public function addressList(array &$config)
{
// Preset menu and models
$optionList = $this->newOptionsList(true);
$DataHelper = new DataHelper();
// Get address list from Solr
$luisContacts = new LuisContacts();
$luisContacts->setRequestType('address');
$jsonData = $luisContacts->fetch();
$addresses = $DataHelper->APIDecode($jsonData);
// Create optionsList
foreach ($addresses as $item) {
$optionList[] = [
$item['name'],
str_replace(@SOLR_PREFIX_ADDRESS, null, $item['id']),
];
}
// Merge into items
$config['items'] = array_merge($config['items'], $optionList);
}
/**
* Create new empty options list with or without a first element
*
* @param bool $addFirstEmptyElement
*
* @return array
*/
private function newOptionsList(bool $addFirstEmptyElement = false)
{
if ($addFirstEmptyElement) {
return [['' => '']];
}
return [];
}
}
\ No newline at end of file
<?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]',
],
],
],
],
]
);
}
}
<?php
namespace Lu\LuContacts\Models;
use Lu\LuApi\DataSources\LuisContacts;
use Lu\LuContacts\Helpers\DataHelper;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
/**
* Class Alphabet
*
* @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
{
/**
* Currently active item name
*
* @var string
*/
private $active = '';
/**
* Pre-set uri builder
*
* @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
*/
private $uriBuilder;
/**
* Settings
*
* @var array
*/
private $settings;
/**
* Define alphabet
*
* @param bool $count
*
* @return array
*/
private function alphabet($count = false): array
{
// Pre-set full alphabet list
$luisContacts = new LuisContacts();
$DataHelper = new DataHelper();
$fullList = [
'a' => 'A, Ā',
'b' => 'B',
'c' => 'C, Č',
'd' => 'D',
'e' => 'E, Ē',
'f' => 'F',
'g' => 'G, Ģ',
'h' => 'H',
'i' => 'I, Ī',
'j' => 'J',
'k' => 'K, Ķ',
'l' => 'L, Ļ',
'm' => 'M',
'n' => 'N, Ņ',
'o' => 'O',
'p' => 'P',
'q' => 'Q',
'r' => 'R',
's' => 'S, Š',
't' => 'T',
'u' => 'U, Ū',
'v' => 'V',
'w' => 'W',
'x' => 'X',
'y' => 'Y',
'z' => 'Z, Ž',
];
// Set request
$luisContacts->setRequestType('person');
$luisContacts->setUnitParentId($this->settings['parentId']);
$luisContacts->setUnitSubParents();
if ($this->settings['addressId']) {
$luisContacts->setUnitAddressId(@SOLR_PREFIX_ADDRESS.$this->settings['addressId']);
}
foreach ($fullList as $letter => $letterCombo) {
$jsonData = $luisContacts->fetchLettersCount(explode(",", $letterCombo));
$data = $DataHelper->APIDecode($jsonData);
if (!$data['data']) {
unset($fullList[$letter]);
} else if ($count) {
// Check if count requested