Skip to content

Commit

Permalink
add CategoryHasItemsViewHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Neuhaus committed May 3, 2016
1 parent 1d02970 commit 1ee37b6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ public function getHasPages()
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);
$where = ' sys_category_record_mm.tablenames = "pages"
AND sys_category_record_mm.fieldname = "categories"
AND sys_category_record_mm.uid_foreign = pages.uid
AND sys_category_record_mm.uid_local IN (' . implode(',', $uids) . ')
' . $GLOBALS["TSFE"]->sys_page->enableFields('pages');
$result = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid_local', 'sys_category_record_mm, pages', $where);
return $result > 0;
}
}
61 changes: 61 additions & 0 deletions Classes/ViewHelpers/CategoryHasItemsViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Mia3\Mia3Categories\ViewHelpers;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use Mia3\Mia3Categories\Domain\Model\Category;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
*
*/
class CategoryHasItemsViewHelper extends AbstractViewHelper {

/**
* @param mixed $categories
* @param string $tableName
* @param string $fieldName
*/
public function render($categories, $tableName = "pages", $fieldName = "categories") {
if (!is_array($categories)) {
$categories = explode(',', $categories);
}
$categoryUids = array();
foreach ($categories as $key => $category) {
if ($category instanceof Category) {
$groupCategories = array($category->getUid());
foreach ($category->getChildrenRecursive() as $childCategory) {
$groupCategories[] = $childCategory->getUid();
}
$categoryUids[] = $groupCategories;
} else {
$categoryUids[] = explode(',', $category);
}
}

$from = array($tableName);
$where = array();
foreach($categoryUids as $key => $categoryGroup) {
$mmTableName = 'mm' . $key;
$foreignTableName = $tableName;
$where[] = $mmTableName . '.tablenames = "' . $tableName . '"
AND ' . $mmTableName . '.fieldname = "' . $fieldName . '"
AND ' . $mmTableName . '.uid_local IN (' . implode(',', $categoryGroup) . ')
AND ' . $mmTableName . '.uid_foreign = ' . $foreignTableName . '.uid
';
$from[] = 'sys_category_record_mm as ' . $mmTableName;
}
// $GLOBALS['TYPO3_DB']->store_lastBuiltQuery = 1;
$result = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', implode(', ', $from), implode(' AND ', $where) . $GLOBALS["TSFE"]->sys_page->enableFields('pages'));
// echo $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
// exit();
return $result > 0;
}

}

0 comments on commit 1ee37b6

Please sign in to comment.