Commit cf77eee8 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Filtre des tempaltes par types

parent 8f745d84
Loading
Loading
Loading
Loading
Loading
+10 −22
Changes for src/UnicaenRenderer/Controller/TemplateController.php: 10 added lines, 22 removed lines.
Original line number Diff line number Diff line
@@ -20,34 +20,22 @@ class TemplateController extends AbstractActionController

    public function indexAction(): ViewModel
    {
        $selected = $this->params()->fromQuery('namespace');
        $namespace = $this->params()->fromQuery('namespace');
        $namespace = ($namespace !== '')?$namespace:null;
        $type = $this->params()->fromQuery('type');
        $type = ($type !== '')?$type:null;

        $allTemplates = $this->getTemplateService()->getTemplates();

        $namespaces = [];
        foreach ($allTemplates as $template) {
            $namespace = $template->getNamespace();
            $namespaces[$namespace][] = $template;
        }

        $templates = [];
        foreach ($allTemplates as $template) {
            if ($selected === null or $selected === "") {
                $templates[] = $template;
            } else {
                if ($selected === TemplateController::SANS_NAMESPACE) {
                    if ($template->getNamespace() === null) $templates[] = $template;
                } else {
                    if ($template->getNamespace() === $selected) $templates[] = $template;
                }
            }
        }
        $templates = $this->getTemplateService()->getTemplatesByTypeAndNamespaces($type, $namespace);
        $namespaces = $this->getTemplateService()->getNamespaces();
        $types = $this->getTemplateService()->getTypes();

        return new ViewModel([
            'title' => 'Gestion des templates',
            'templates' => $templates,
            'namespaces' => $namespaces,
            'selected' => $selected,
            'types' => $types,
            'namespace' => $namespace,
            'type' => $type,
        ]);
    }

+6 −0
Changes for src/UnicaenRenderer/Entity/Db/Template.php: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -18,9 +18,15 @@ class Template implements ResourceInterface
        return self::RESOURCE_ID;
    }


    const TYPE_TXT = 'texte';
    const TYPE_PDF = 'pdf';
    const TYPE_MAIL = 'mail';
    const TYPES = [
        self::TYPE_MAIL => "Template de mail",
        self::TYPE_PDF => "Template de pdf",
        self::TYPE_TXT => "Template de texte",
    ];

    private ?int $id = null;
    private ?string $code = null;
+1 −1
Changes for src/UnicaenRenderer/Service/Rendu/RenduService.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -131,7 +131,7 @@ class RenduService
    /**
     * TODO >>>  faire mieux pour rappliquer
     */
    public function generateRenduByTemplateCode(string $code, array $variables, bool $save=true): Rendu
    public function generateRenduByTemplateCode(string $code, array $variables=[], bool $save=true): Rendu
    {
        $template = $this->getTemplateService()->getTemplateByCode($code);
        if ($template === null) throw new RuntimeException('Aucun template de trouvé avec le code [' . $code . ']');
+37 −0
Changes for src/UnicaenRenderer/Service/Template/TemplateService.php: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -84,6 +84,19 @@ class TemplateService {
        return $result;
    }

    /** @return Template[] */
    public function getTemplatesByTypeAndNamespaces(?string $type = null, ?string $namespace=null, string $champ = 'code', string $ordre = 'ASC') : array
    {
        $qb = $this->createQueryBuilder()
            ->orderBy('template.' . $champ, $ordre)
        ;
        if ($type !== null) $qb = $qb->andWhere('template.type = :type')->setParameter('type', $type);
        if ($namespace !== null) $qb = $qb->andWhere('template.namespace = :namespace')->setParameter('namespace', $namespace);

        $result = $qb->getQuery()->getResult();
        return $result;
    }

    /** @return string[] */
    public function getTemplatesAsOptions(string $champ = 'code', string $ordre = 'ASC', ?string $type = null) : array
    {
@@ -151,6 +164,28 @@ class TemplateService {
        return $result;
    }

    public function getNamespaces() : array
    {
        $templates = $this->getTemplates();
        $namespaces = [];
        foreach ($templates as $template) {
            $namespaces[$template->getNamespace()] = $template->getNamespace();
        }
        sort($namespaces);
        return $namespaces;
    }

    public function getTypes() : array
    {
        $templates = $this->getTemplates();
        $types = [];
        foreach ($templates as $template) {
            $types[$template->getType()] = $template->getType();
        }
        sort($types);
        return $types;
    }

    /** FACADE ********************************************************************************************************/

    /**
@@ -197,4 +232,6 @@ class TemplateService {
        return $this->replaceMacros($template->getSujet(), $variables);
    }



}
 No newline at end of file
+32 −16
Changes for view/unicaen-renderer/template/index.phtml: 32 added lines, 16 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @see TemplateController::indexAction
 * @see TemplateController::indexAction()
 * @var Template[] $templates
 * @var Template[][] $namespaces @desc namespace:string => Template[] (templates du namespace)
 * @var string $selected
 * @var string[] $namespaces
 * @var string[] $types
 * @var ?string $namespace
 * @var ?string $type
 * @var string $title
 */

@@ -66,21 +68,35 @@ if (isset($title)) $this->headTitle($title);
    <div class="card-body">
        <form method="get" action="">
            <div class="row">
                <div class="col-md-5">
                <div class="col-md-4">
                    <label for="namespace"> Namespace : </label>
                    <select id="namespace" name="namespace" class="selectpicker" data-live-search="true">
                        <option value=""> Tous les namespaces</option>
                        <?php foreach ($namespaces as $namespace => $t) : ?>
                            <?php if ($namespace === "") $namespace = TemplateController::SANS_NAMESPACE; ?>
                            <option value="<?php echo $namespace; ?>"
                                <?php if ($namespace === $selected) echo " selected "; ?>
                        <?php foreach ($namespaces as $key => $namespaceCurrent) : ?>
                            <?php if ($namespaceCurrent === null) $namespaceCurrent = TemplateController::SANS_NAMESPACE; ?>
                            <option value="<?php echo $namespaceCurrent; ?>"
                                <?php if (isset($namespace) && $namespaceCurrent === $namespace) echo " selected "; ?>
                            >
                                <?php echo $namespace; ?>
                                <?php echo $namespaceCurrent; ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-7">
                <div class="col-md-4">
                    <label for="type"> Type : </label>
                    <select id="type" name="type" class="selectpicker" data-live-search="true">
                        <option value=""> Tous les types </option>
                        <?php foreach ($types as $key => $typeCurrent) : ?>
                            <option value="<?php echo $typeCurrent; ?>"
                                <?php if (isset($type) && $typeCurrent === $type) echo " selected "; ?>
                            >
                                <?php echo $typeCurrent; ?>
                            </option>

                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <button class="btn btn-primary" id="filter">
                            <span class="icon icon-filtrer"></span>
@@ -160,18 +176,18 @@ if (isset($title)) $this->headTitle($title);
        if (jQuery().selectpicker) {
            $('#variable').selectpicker('render');
        }
        $("form#filtre").submit(function (e) {
        $('button#clear').on('click',function (e) {
            e.preventDefault();
            window.location.href = 'template?namespace=&type=';
        });
        $('button#clear').click(function () {
            window.location.href = 'template';
        });
        $('button#filter').click(function () {
        $('button#filter').on('click',function (e) {
            e.preventDefault();
            let namespaceId = $('select#namespace option:selected').val();
            console.log("namespace=" + namespaceId);
            let typeId = $('select#type option:selected').val();

            let query = "";
            query += "&namespace=" + namespaceId;
            query += "&type=" + typeId;
            window.location.href = 'template?' + query;
        });
    });