<?php
namespace App\Controller;
use App\Entity\DataArt;
use App\Repository\DataArtRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
class ItemsController extends BaseController{
public $dataClass = 'DataArt::class';
public $dataRepository = 'dataArtRepository';
public $img = 'https://acnhcdn.com/latest/FtrIcon/';
public $dataArtRepository;
public $managerRegistry;
public function __construct(DataArtRepository $dataArtRepository, ManagerRegistry $managerRegistry)
{
parent::__construct();
$this->dataArtRepository=$dataArtRepository;
$this->managerRegistry = $managerRegistry;
}
/**
* @Route("/items/{type}/{currentPage}", name="items_type")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function itemsIndex(Request $request, $type, $currentPage=1){
$items=array();
if($type){
$em = $this->getDoctrine()->getManager();
$limit = 15;
$name=$this->returType($type);
if($name[3]!=''){
$items = $em->getRepository($name[0])->getAllByField($currentPage, $limit, 'tag',$name[3]);
}else{
$items = $em->getRepository($name[0])->getAll($currentPage, $limit);
}
$itemsResultado = $items['paginator'];
$itemsQueryCompleta = $items['query'];
$maxPages = ceil($items['paginator']->count() / $limit);
$typesRepository = new $name[1]($this->managerRegistry);
$typesRepository->findBy(array(), array('name' => 'ASC'));
}
return $this->render('items/index.html.twig', [
'typeName' => $type,
'types' => $typesRepository,
'items' => $itemsResultado,
'maxPages' => $maxPages,
'thisPage' => $currentPage,
'all_items' => $itemsQueryCompleta,
'rutaimg' => $name[2],
'type' => $type
]);
}
/**
* @Route("/items/{type}/{currentPage}/{category}", name="items_type_category")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function categoryIndex(Request $request, $type, $currentPage=1, $category=null){
$items=array();
if($type){
$em = $this->getDoctrine()->getManager();
$limit = 15;
$name=$this->returType($type);
$items = $em->getRepository($name[0])->getAllByCategory($category, $currentPage, $limit);
$maxPages = ceil($items['paginator']->count() / $limit);
if($maxPages<$currentPage){
$items = $em->getRepository($name[0])->getAllByCategory($category, $currentPage=1, $limit);
}
$itemsResultado = $items['paginator'];
$itemsQueryCompleta = $items['query'];
$typesRepository = new $name[1]($this->managerRegistry);
$typesRepository->findBy(array(), array('name' => 'ASC'));
}
return $this->render('items/index.html.twig', [
'typeName' => $type,
'types' => $typesRepository,
'items' => $itemsResultado,
'maxPages' => $maxPages,
'thisPage' => $currentPage,
'all_items' => $itemsQueryCompleta,
'rutaimg' => $name[2],
'category' => $category,
'type' => $type
]);
}
private function returType($type)
{
$type= ucfirst($type);
$tag='';
$img = '/resources/img/icons/';
switch ($type) {
case 'Bushes':
$type = 'Other';
$tag = 'bushes';
break;
case 'Flowers':
$type = 'Other';
$tag = 'Plants';
break;
default:
//$img = 'https://acnhcdn.com/latest/FtrIcon/';
break;
}
$dataClass = "App\Entity\Data{$type}";
$dataRepository = "\App\Repository\data{$type}Repository";
/*switch ($type) {
case 'art':
$dataClass = 'App\Entity\DataArt';
$dataRepository = 'dataArtRepository';
$img = 'https://acnhcdn.com/latest/FtrIcon/';
break;
case 'bushes':
$dataClass = 'App\Entity\DataBushes';
$dataRepository = 'App\Repository\dataBushesRepository';
$img = 'https://acnhcdn.com/latest/FtrIcon/';
break;
case 'bags':
$dataClass = 'App\Entity\DataBags';
$dataRepository = 'App\Repository\dataBagsRepository';
break;
}
*/
if(isset($dataClass) && class_exists($dataClass)){
return array("{$dataClass}","{$dataRepository}", "{$img}", "{$tag}");
}else{
return array($this->dataClass,$this->dataRepository, $this->img);
}
}
}