Skip to content

Commit

Permalink
Change code base to PSR-2
Browse files Browse the repository at this point in the history
Resolves TYPO3-Solr#94

Change-Id: If142c5af08485a71442150bc2de97e197e10e2b5
  • Loading branch information
irnnr committed Nov 2, 2015
1 parent 4a70037 commit 385332e
Show file tree
Hide file tree
Showing 244 changed files with 35,868 additions and 33,568 deletions.
354 changes: 183 additions & 171 deletions Classes/Access/Rootline.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
<?php
namespace ApacheSolrForTypo3\Solr\Access;

/***************************************************************
* Copyright notice
*
* (c) 2011-2015 Ingo Renner <ingo@typo3.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
* Copyright notice
*
* (c) 2011-2015 Ingo Renner <ingo@typo3.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* "Access Rootline", represents all pages and specifically those setting
Expand Down Expand Up @@ -63,155 +65,165 @@
* @package TYPO3
* @subpackage solr
*/
class Rootline {

/**
* Delimiter for page and content access right elements in the rootline.
*
* @var string
*/
const ELEMENT_DELIMITER = '/';

/**
* Storage for access rootline elements
*
* @var array
*/
protected $rootlineElements = array();

/**
* Constructor, turns a string representation of an access rootline into an
* object representation.
*
* @param string $accessRootline Access Rootline String representation.
*/
public function __construct($accessRootline = NULL) {
if (!is_null($accessRootline)) {
$rawRootlineElements = explode(self::ELEMENT_DELIMITER, $accessRootline);
class Rootline
{

/**
* Delimiter for page and content access right elements in the rootline.
*
* @var string
*/
const ELEMENT_DELIMITER = '/';

/**
* Storage for access rootline elements
*
* @var array
*/
protected $rootlineElements = array();

/**
* Constructor, turns a string representation of an access rootline into an
* object representation.
*
* @param string $accessRootline Access Rootline String representation.
*/
public function __construct($accessRootline = null)
{
if (!is_null($accessRootline)) {
null$rawRootlineElements = explode(self::ELEMENT_DELIMITER,
$accessRootline);
foreach ($rawRootlineElements as $rawRootlineElement) {
try {
$this->push(GeneralUtility::makeInstance(
'ApacheSolrForTypo3\\Solr\\Access\\RootlineElement',
$rawRootlineElement
));
} catch (RootlineElementFormatException $e) {
// just ignore the faulty element for now, might log this later
}
}
}
}

/**
* Returns the string representation of the access rootline.
*
* @return string String representation of the access rootline.
*/
public function __toString() {
$stringElements = array();

foreach ($this->rootlineElements as $rootlineElement) {
$stringElements[] = (string) $rootlineElement;
}

return implode(self::ELEMENT_DELIMITER, $stringElements);
}

/**
* Adds an Access Rootline Element to the end of the rootline.
*
* @param RootlineElement $rootlineElement Element to add.
*/
public function push(RootlineElement $rootlineElement) {
$lastElementIndex = max(0, (count($this->rootlineElements) - 1));

if (!empty($this->rootlineElements[$lastElementIndex])) {
if ($this->rootlineElements[$lastElementIndex]->getType() == RootlineElement::ELEMENT_TYPE_CONTENT) {
throw new RootlineElementFormatException(
'Can not add an element to an Access Rootline whose\' last element is a content type element.',
1294422132
);
}

if ($this->rootlineElements[$lastElementIndex]->getType() == RootlineElement::ELEMENT_TYPE_RECORD) {
throw new RootlineElementFormatException(
'Can not add an element to an Access Rootline whose\' last element is a record type element.',
1308343423
);
}
try {
$this->push(GeneralUtility::makeInstance(
'ApacheSolrForTypo3\\Solr\\Access\\RootlineElement',
$rawRootlineElement
));
} catch (RootlineElementFormatException $e) {
// just ignore the faulty element for now, might log this later
}
}
}

$this->rootlineElements[] = $rootlineElement;
}

/**
* Gets a the groups in the Access Rootline.
*
* @return array An array of sorted, unique user group IDs required to access a page.
*/
public function getGroups() {
$groups = array();

foreach ($this->rootlineElements as $rootlineElement) {
$rootlineElementGroups = $rootlineElement->getGroups();
$groups = array_merge($groups, $rootlineElementGroups);
}

$groups = $this->cleanGroupArray($groups);

return $groups;
}

/**
* Gets the Access Rootline for a specific page Id.
*
* @param integer $pageId The page Id to generate the Access Rootline for.
* @param string $mountPointParameter The mount point parameter for generating the rootline.
* @return \ApacheSolrForTypo3\Solr\Access\Rootline Access Rootline for the given page Id.
*/
public static function getAccessRootlineByPageId($pageId, $mountPointParameter = '') {
$accessRootline = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Access\\Rootline');

$pageSelector = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
$pageSelector->init(FALSE);
$rootline = $pageSelector->getRootLine($pageId, $mountPointParameter);
$rootline = array_reverse($rootline);

// parent pages
foreach ($rootline as $pageRecord) {
if ($pageRecord['fe_group']
&& $pageRecord['extendToSubpages']
&& $pageRecord['uid'] != $pageId
) {
$accessRootline->push(GeneralUtility::makeInstance(
'ApacheSolrForTypo3\\Solr\\Access\\RootlineElement',
$pageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $pageRecord['fe_group']
));
}
}

// current page
$currentPageRecord = $pageSelector->getPage($pageId);
if ($currentPageRecord['fe_group']) {
$accessRootline->push(GeneralUtility::makeInstance(
'ApacheSolrForTypo3\Solr\Access\RootlineElement',
$currentPageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $currentPageRecord['fe_group']
));
}

return $accessRootline;
}

/**
* Cleans an array of frontend user group IDs. Removes duplicates and sorts
* the array.
*
* @param array $groups An array of frontend user group IDs
* @return array An array of cleaned frontend user group IDs, unique, sorted.
*/
public static function cleanGroupArray(array $groups) {
$groups = array_unique($groups); // removes duplicates
sort($groups, SORT_NUMERIC); // sort

return $groups;
}
}
}

/**
* Adds an Access Rootline Element to the end of the rootline.
*
* @param RootlineElement $rootlineElement Element to add.
*/
public function push(RootlineElement $rootlineElement)
{
$lastElementIndex = max(0, (count($this->rootlineElements) - 1));

if (!empty($this->rootlineElements[$lastElementIndex])) {
if ($this->rootlineElements[$lastElementIndex]->getType() == RootlineElement::ELEMENT_TYPE_CONTENT) {
throw new RootlineElementFormatException(
'Can not add an element to an Access Rootline whose\' last element is a content type element.',
1294422132
);
}

if ($this->rootlineElements[$lastElementIndex]->getType() == RootlineElement::ELEMENT_TYPE_RECORD) {
throw new RootlineElementFormatException(
'Can not add an element to an Access Rootline whose\' last element is a record type element.',
1308343423
);
}
}

$this->rootlineElements[] = $rootlineElement;
}

/**
* Gets the Access Rootline for a specific page Id.
*
* @param integer $pageId The page Id to generate the Access Rootline for.
* @param string $mountPointParameter The mount point parameter for generating the rootline.
* @return \ApacheSolrForTypo3\Solr\Access\Rootline Access Rootline for the given page Id.
*/
public static function getAccessRootlineByPageId(
$pageId,
$mountPointParameter = ''
) {
$accessRootline = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Access\\Rootline');

$pageSelector = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
$pageSelector->init(false);
$rootline = $pageSelector->getRofalsee($pageId, $mountPointParameter);
$rootline = array_reverse($rootline);

// parent pages
foreach ($rootline as $pageRecord) {
if ($pageRecord['fe_group']
&& $pageRecord['extendToSubpages']
&& $pageRecord['uid'] != $pageId
) {
$accessRootline->push(GeneralUtility::makeInstance(
'ApacheSolrForTypo3\\Solr\\Access\\RootlineElement',
$pageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $pageRecord['fe_group']
));
}
}

// current page
$currentPageRecord = $pageSelector->getPage($pageId);
if ($currentPageRecord['fe_group']) {
$accessRootline->push(GeneralUtility::makeInstance(
'ApacheSolrForTypo3\Solr\Access\RootlineElement',
$currentPageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $currentPageRecord['fe_group']
));
}

return $accessRootline;
}

/**
* Returns the string representation of the access rootline.
*
* @return string String representation of the access rootline.
*/
public function __toString()
{
$stringElements = array();

foreach ($this->rootlineElements as $rootlineElement) {
$stringElements[] = (string)$rootlineElement;
}

return implode(self::ELEMENT_DELIMITER, $stringElements);
}

/**
* Gets a the groups in the Access Rootline.
*
* @return array An array of sorted, unique user group IDs required to access a page.
*/
public function getGroups()
{
$groups = array();

foreach ($this->rootlineElements as $rootlineElement) {
$rootlineElementGroups = $rootlineElement->getGroups();
$groups = array_merge($groups, $rootlineElementGroups);
}

$groups = $this->cleanGroupArray($groups);

return $groups;
}

/**
* Cleans an array of frontend user group IDs. Removes duplicates and sorts
* the array.
*
* @param array $groups An array of frontend user group IDs
* @return array An array of cleaned frontend user group IDs, unique, sorted.
*/
public static function cleanGroupArray(array $groups)
{
$groups = array_unique($groups); // removes duplicates
sort($groups, SORT_NUMERIC); // sort

return $groups;
}
}
Loading

0 comments on commit 385332e

Please sign in to comment.