StatsExtension.php 2.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php

namespace App\Twig;

use App\Entity\TypePrestataire;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;

class StatsExtension extends AbstractExtension
{
    protected $em;

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

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('showMlcStats', array($this, 'showMlcStats'))
        );
    }

    public function showMlcStats($stats)
    {
        $class = '';
35
        switch ($stats) {
36 37 38 39
            case 'user':
                $class = 'App\Entity\User';
                break;
            case 'prestataire':
40
                $class = null;
41 42 43
                $typepresta = $this->em->getRepository(TypePrestataire::class)->findOneBySlug('prestataire');
                $query = $this->em->createQuery(
                    'SELECT count(distinct p.id)
44 45 46
                    FROM App\\Entity\\Prestataire p
                    WHERE p.enabled = true AND p.typeprestataire = :typepresta'
                )->setParameter('typepresta', $typepresta);
47 48 49 50

                $val = $query->getSingleScalarResult();
                break;
            case 'partenaire':
51
                $class = null;
52 53 54
                $typepresta = $this->em->getRepository(TypePrestataire::class)->findOneBySlug('partenaire');
                $query = $this->em->createQuery(
                    'SELECT count(distinct p.id)
55 56 57
                    FROM App\\Entity\\Prestataire p
                    WHERE p.enabled = true AND p.typeprestataire = :typepresta'
                )->setParameter('typepresta', $typepresta);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

                $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)
                FROM '.$class.' u
75 76
                WHERE u.enabled = true'
            );
77 78 79 80 81 82 83

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

        return $val;
    }
}