StatsExtension.php 2.33 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php

namespace App\Twig;

use App\Entity\TypePrestataire;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;

class StatsExtension extends AbstractExtension
{
    protected $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
23 24 25
        return [
            new \Twig_SimpleFunction('showMlcStats', [$this, 'showMlcStats']),
        ];
Julien Jorry committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    }

    public function showMlcStats($stats)
    {
        $class = '';
        switch ($stats) {
            case 'user':
                $class = 'App\Entity\User';
                break;
            case 'prestataire':
                $class = null;
                $typepresta = $this->em->getRepository(TypePrestataire::class)->findOneBySlug('prestataire');
                $query = $this->em->createQuery(
                    'SELECT count(distinct p.id)
                    FROM App\\Entity\\Prestataire p
                    WHERE p.enabled = true AND p.typeprestataire = :typepresta'
                )->setParameter('typepresta', $typepresta);

                $val = $query->getSingleScalarResult();
                break;
            case 'partenaire':
                $class = null;
                $typepresta = $this->em->getRepository(TypePrestataire::class)->findOneBySlug('partenaire');
                $query = $this->em->createQuery(
                    'SELECT count(distinct p.id)
                    FROM App\\Entity\\Prestataire p
                    WHERE p.enabled = true AND p.typeprestataire = :typepresta'
                )->setParameter('typepresta', $typepresta);

                $val = $query->getSingleScalarResult();
                break;
            case 'adherent':
                $class = 'App\Entity\Adherent';
                break;
            case 'comptoir':
                $class = 'App\Entity\Comptoir';
                break;
            case 'groupe':
                $class = 'App\Entity\Groupe';
                break;
        }
        if (!empty($class)) {
            $query2 = $this->em->createQuery(
                'SELECT count(distinct u.id)
70
                FROM ' . $class . ' u
Julien Jorry committed
71 72 73 74 75 76 77 78 79
                WHERE u.enabled = true'
            );

            $val = $query2->getSingleScalarResult();
        }

        return $val;
    }
}