Skip to content

Commit

Permalink
add Category and CategoryRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Neuhaus committed Apr 27, 2016
1 parent c8d237a commit 1d02970
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
109 changes: 109 additions & 0 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
namespace Mia3\Mia3Categories\Domain\Model;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Famelo\FameloCommon\ObjectStorageSorter;

/**
* This model represents a category (for anything).
*
* @api
*/
class Category extends \TYPO3\CMS\Extbase\Domain\Model\Category
{
/**
* @var integer
*/
protected $sorting;

/**
* categories
*
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Mia3\Mia3Categories\Domain\Model\Category>
* @lazy
*/
protected $children = null;

/**
* @var \Mia3\Mia3Categories\Domain\Model\Category|NULL
* @lazy
*/
protected $parent = null;

public function __toString()
{
return $this->title;
}

/**
* Returns the sorting
*
* @return integer $sorting
*/
public function getSorting()
{
return $this->sorting;
}

/**
* Sets the sorting
*
* @param integer $sorting
* @return void
*/
public function setSorting($sorting)
{
$this->sorting = $sorting;
}

/**
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Mia3\Mia3Categories\Domain\Model\Category> $children
*/
public function setChildren($children)
{
$this->children = $children;
}

/**
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Mia3\Mia3Categories\Domain\Model\Category>
*/
public function getChildren()
{
return ObjectStorageSorter::sort($this->children, 'sorting');
}

public function getChildrenRecursive($children = array())
{
foreach ($this->children as $child) {
$children[] = $child;
$children = $child->getChildrenRecursive($children);
}

return $children;
}

public function getHasPages()
{
$children = $this->getChildrenRecursive();
$uids = array($this->getUid());
foreach($children as $child) {
$uids[] = $child->getUid();
}
$where = ' tablenames = "pages"
AND fieldname = "categories"
AND uid_local IN (' . implode(',', $uids) . ')';
$result = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid_local', 'sys_category_record_mm', $where);
return $result > 0;
}
}
46 changes: 46 additions & 0 deletions Classes/Domain/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Mia3\Mia3Categories\Domain\Repository;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

/**
* Repository for Category models.
*
* @api
*/
class CategoryRepository extends \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository
{
/**
* Initializes the repository.
*
* @return void
*/
public function initializeObject()
{
/** @var $querySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
$querySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class);
$querySettings->setRespectStoragePage(FALSE);
$querySettings->setRespectSysLanguage(FALSE);
$this->setDefaultQuerySettings($querySettings);
}

public function findByUids($uids) {
if (is_string($uids)) {
$uids = explode(',', $uids);
}
$query = $this->createQuery();
$query->matching($query->in('uid', $uids));
return $query->execute();
}
}
29 changes: 29 additions & 0 deletions Configuration/TCA/Overrides/sys_category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$temporaryColumns = array(
'sorting' => array(
'exclude' => 1,
'label' => 'Sortierung',
'config' => array(
'type' => 'passthrough'
),
),
'children' => array(
'exclude' => 1,
'label' => 'Unterkategorien',
'config' => array(
'type' => 'select',
'renderType' => 'selectMultipleSideBySide',
'foreign_table' => 'sys_category',
'foreign_field' => 'parent',
'foreign_table_where' => ' AND (sys_category.sys_language_uid = 0 OR sys_category.l10n_parent = 0) ORDER BY sys_category.sorting',
'size' => 10,
'autoSizeMax' => 20,
'minitems' => 0,
'maxitems' => 20
),
)
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('sys_category', $temporaryColumns);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('sorting,children');
6 changes: 6 additions & 0 deletions ext_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Table structure for table 'sys_category'
#
CREATE TABLE sys_category (
children int(11) unsigned DEFAULT '0' NOT NULL
);
19 changes: 19 additions & 0 deletions ext_typoscript_setup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
config.tx_extbase{
persistence{
classes{

TYPO3\CMS\Extbase\Domain\Model\Category {
subclasses {
Tx_Mia3Categories_Category = Mia3\Mia3Categories\Domain\Model\Category

}
}
Mia3\Mia3Categories\Domain\Model\Category {
mapping {
tableName = sys_category
}
}

}
}
}

0 comments on commit 1d02970

Please sign in to comment.