Current path: home/webdevt/cryptoimpot.fr/module/Application/src/Mapper/
?? Go up: /home/webdevt/cryptoimpot.fr/module/Application/src
<?php
namespace Application\Mapper;
use Doctrine\ORM\EntityManager;
use TheSeer\Tokenizer\Exception;
use Laminas\Hydrator\HydratorInterface;
use Laminas\Paginator\Adapter\ArrayAdapter;
use Laminas\Paginator\Paginator;
class ReportMapper {
/**
* @var EntityManager
*/
protected $em;
protected $entityClass;
protected $orderAuthorized;
protected $orderByAuthorized;
public function __construct(EntityManager $em) {
$this->em = $em;
$this->entityClass = 'Application\Entity\Report';
$this->orderByAuthorized = array(
'dateTransaction'
// 'id',
// 'categorie',
// 'titre',
// 'statut',
// 'dateCreation'
);
$this->orderAuthorized = array(
'ASC',
'DESC'
);
}
public function findById($id) {
$er = $this->em->getRepository($this->entityClass);
return $er->findOneBy(array('id' => $id));
}
public function findByYearAndUser($year, $user) {
$er = $this->em->getRepository($this->entityClass);
return $er->findOneBy(array('user' => $user, 'year' => $year));
}
public function getListByIdUser($idUser, $parameters = array(), $pagination = false) {
$parameters['idUser'] = $idUser;
return $this->getList($parameters, $pagination);
}
public function getList($parameters, $pagination = true, $onlyPublished = true) {
$er = $this->em->getRepository($this->entityClass);
$query = $er->createQueryBuilder('d')
//->leftJoin('d.statut', 'st')
;
if (isset($parameters['sort_by'])) {
$sort_by = in_array($parameters['sort_by'], $this->orderByAuthorized) ? $parameters['sort_by'] : 'id';
if (isset($parameters['sort'])) {
$sort = in_array($parameters['sort'], $this->orderAuthorized) ? $parameters['sort'] : 'DESC';
if ($parameters['sort_by'] == 'statut') {
$query->orderBy('st.statut', $sort);
} else {
//var_dump($sort_by, $sort); exit;
// $query->orderBy('d.' . $sort_by, $sort);
$query->orderBy('d.datetimeTransaction', $sort);
}
}
}
// if (isset($parameters['user'])) {
// $user = $parameters['user'];
// $query->orWhere('d.user_id = :search')
// ->setParameter('search', $user->getId());
// }
// if (isset($parameters['search'])) {
// $search = $parameters['search'];
//
// // Gestion recherche phrase exacte si présence de " début et fin de chaine, ou un des mots présents
// if (substr($search, 0, 1) == '"' && substr($search, -1, 1) == '"') {
// $search = substr($search, 1, -1);
// } else {
// $search = explode(' ', $search);
// }
// if (is_array($search)) {
// $i = 1;
// foreach ($search as $searchElement) {
// $query->orWhere('d.categorie LIKE ?' . $i)
// ->orWhere('d.titre LIKE ?' . $i)
// ->orWhere('d.file LIKE ?' . $i)
// ->orWhere('st.statut LIKE ?' . $i)
// ->setParameter($i, '%' . $searchElement . '%');
// $i++;
// }
// } else {
// if ($search == 'publié') {
// $query->orWhere('d.categorie = :search')
// ->orWhere('d.titre = :search')
// ->orWhere('d.file = :search')
// ->orWhere('st.statut = :search')
// ->setParameter('search', $search);
// } else {
// $query->orWhere('d.categorie LIKE :search')
// ->orWhere('d.titre LIKE :search')
// ->orWhere('d.file LIKE :search')
// ->orWhere('st.statut LIKE :search')
// ->setParameter('search', '%' . $search . '%');
// }
// }
// }
if (isset($parameters['idUser'])) {
$query->where('d.user = (:idUser)')
->setParameter('idUser', $parameters['idUser']);
}
// if ($onlyPublished) {
// $query->andWhere('st.id = :publish')
// ->setParameter('publish', 1);
// }
$arrayOfTests = $query->getQuery()
->getResult();
if ($pagination === true) {
$paginatorAdapter = new ArrayAdapter($arrayOfTests);
return new Paginator($paginatorAdapter);
} else {
return $arrayOfTests;
}
}
public function insert($entity, $tableName = null, HydratorInterface $hydrator = null) {
return $this->persist($entity);
}
public function update($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null) {
return $this->persist($entity);
}
protected function persist($entity) {
$this->em->persist($entity);
$this->em->flush();
return $entity;
}
public function delete($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null) {
try {
$this->em->remove($entity);
$this->em->flush();
return true;
} catch (Exception $e) {
return false;
}
}
}