diff --git a/config/merged/indicateur.config.php b/config/merged/indicateur.config.php index 91919efa9dd5e3aeadd05e379f1d4752b0f55013..bc1412d930cd7460de51cc004bf9029cef32a31f 100644 --- a/config/merged/indicateur.config.php +++ b/config/merged/indicateur.config.php @@ -9,6 +9,8 @@ use UnicaenIndicateur\Form\Indicateur\IndicateurHydratorFactory; use UnicaenIndicateur\Provider\Privilege\IndicateurPrivileges; use UnicaenIndicateur\Service\Indicateur\IndicateurService; use UnicaenIndicateur\Service\Indicateur\IndicateurServiceFactory; +use UnicaenIndicateur\View\Helper\DefaultItemViewHelper; +use UnicaenIndicateur\View\Helper\IndicateurViewHelperFactory; use UnicaenPrivilege\Guard\PrivilegeController; use Laminas\Mvc\Console\Router\Simple; use Laminas\Router\Http\Literal; @@ -200,6 +202,13 @@ return [ 'factories' => [ IndicateurHydrator::class => IndicateurHydratorFactory::class, ], - ] - + ], + 'view_helpers' => [ + 'invokables' => [ + 'defaultItem' => DefaultItemViewHelper::class, + ], + 'factories' => [ + 'indicateur' => IndicateurViewHelperFactory::class, + ], + ], ]; \ No newline at end of file diff --git a/src/UnicaenIndicateur/View/Helper/DefaultItemViewHelper.php b/src/UnicaenIndicateur/View/Helper/DefaultItemViewHelper.php new file mode 100644 index 0000000000000000000000000000000000000000..34960eb308398b56c167459b727e3a7c366d5ddb --- /dev/null +++ b/src/UnicaenIndicateur/View/Helper/DefaultItemViewHelper.php @@ -0,0 +1,36 @@ +<?php + +namespace UnicaenIndicateur\View\Helper; + +use Laminas\View\Helper\AbstractHelper; + +class DefaultItemViewHelper extends AbstractHelper implements ItemViewHelperInterface +{ + public function getHeader(array $data) : string + { + $headers = $data[0]; + $texte = "<thead>"; + $texte .= "<tr>"; + foreach ($headers as $header) { + $texte .= "<th>"; + $texte .= $header; + $texte .= "</th>"; + } + $texte .= "</tr>"; + $texte .= "</thead>"; + return $texte; + } + + public function getItem(array $data, int $position) : string + { + $values = $data[1][$position]; + $texte = "<tr>"; + foreach ($values as $value) { + $texte .= "<td>"; + $texte .= $value; + $texte .= "</td>"; + } + $texte .= "</tr>"; + return $texte; + } +} \ No newline at end of file diff --git a/src/UnicaenIndicateur/View/Helper/IndicateurViewHelper.php b/src/UnicaenIndicateur/View/Helper/IndicateurViewHelper.php new file mode 100644 index 0000000000000000000000000000000000000000..f0e87465ed8bed9c52e395c10732419544358946 --- /dev/null +++ b/src/UnicaenIndicateur/View/Helper/IndicateurViewHelper.php @@ -0,0 +1,31 @@ +<?php + +namespace UnicaenIndicateur\View\Helper; + +use Laminas\View\Helper\AbstractHelper; +use Laminas\View\Renderer\PhpRenderer; +use Laminas\View\Resolver\TemplatePathStack; +use UnicaenIndicateur\Entity\Db\Indicateur; +use UnicaenIndicateur\Service\Indicateur\IndicateurServiceAwareTrait; + +class IndicateurViewHelper extends AbstractHelper { + use IndicateurServiceAwareTrait; + + /** + * @param Indicateur $indicateur + * @param array $options + * @return \Laminas\View\Helper\Partial|string + * + * La clef $options['ViewHelper'] permet d'indiquer le ViewHelperpour réaliser l'affichage + * attention celui-ci doit implementer l'interface ItemViewHelperInterface + */ + public function __invoke(Indicateur $indicateur, array $options = []) + { + /** @var PhpRenderer $view */ + $view = $this->getView(); + $view->resolver()->attach(new TemplatePathStack(['script_paths' => [__DIR__ . "/partial"]])); + + $data = $this->getIndicateurService()->getIndicateurData($indicateur); + return $view->partial('indicateur', ['indicateur' => $indicateur, 'data' => $data, 'options' => $options]); + } +} \ No newline at end of file diff --git a/src/UnicaenIndicateur/View/Helper/IndicateurViewHelperFactory.php b/src/UnicaenIndicateur/View/Helper/IndicateurViewHelperFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..603ba1771e0b79047fae471ec24f3b6b169c156d --- /dev/null +++ b/src/UnicaenIndicateur/View/Helper/IndicateurViewHelperFactory.php @@ -0,0 +1,29 @@ +<?php + +namespace UnicaenIndicateur\View\Helper; + +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; +use UnicaenIndicateur\Service\Indicateur\IndicateurService; + +class IndicateurViewHelperFactory { + + /** + * @param ContainerInterface $container + * @return IndicateurViewHelper + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function __invoke(ContainerInterface $container) : IndicateurViewHelper + { + /** + * @var IndicateurService $indicateurService + */ + $indicateurService = $container->get(IndicateurService::class); + + $helper = new IndicateurViewHelper(); + $helper->setIndicateurService($indicateurService); + return $helper; + } +} \ No newline at end of file diff --git a/src/UnicaenIndicateur/View/Helper/ItemViewHelperInterface.php b/src/UnicaenIndicateur/View/Helper/ItemViewHelperInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..0fdd09601cbf35bf0557e75d469c7e15b582deab --- /dev/null +++ b/src/UnicaenIndicateur/View/Helper/ItemViewHelperInterface.php @@ -0,0 +1,12 @@ +<?php + +namespace UnicaenIndicateur\View\Helper; + +interface ItemViewHelperInterface +{ + /** Retourne le header du tableau */ + public function getHeader(array $data) : string ; + + /** Retourne la line du tableau associée avec l'item à la position $position */ + public function getItem(array $data, int $position) : string ; +} \ No newline at end of file diff --git a/src/UnicaenIndicateur/View/Helper/partial/indicateur.phtml b/src/UnicaenIndicateur/View/Helper/partial/indicateur.phtml new file mode 100644 index 0000000000000000000000000000000000000000..d1ebb1a0bd4b49479f5e16d5791efccfef6b8085 --- /dev/null +++ b/src/UnicaenIndicateur/View/Helper/partial/indicateur.phtml @@ -0,0 +1,32 @@ +<?php + +use UnicaenIndicateur\Entity\Db\Indicateur; + +/** + * @see \UnicaenIndicateur\View\Helper\IndicateurViewHelper + * @var Indicateur $indicateur + * @var array $data + * @var array $options + */ + +$viewhelper = 'defaultItem'; +if (isset($options['ViewHelper'])) $viewhelper = $options['ViewHelper'] + +?> + +<div> +<table + id="indicateur_<?php echo $indicateur->getId(); ?>" + class="datatable table table-condensed table-hover" +> + <?php echo $this->{$viewhelper}($indicateur)->getHeader($data); ?> + <tbody> + <?php $nbLine = count($data[1]); ?> + <?php + for($position = 0 ; $position < count($data[1]) ; $position++) { + echo $this->{$viewhelper}($indicateur)->getItem($data, $position); + } + ?> + </tbody> +</table> +</div> \ No newline at end of file diff --git a/view/unicaen-indicateur/tableau-de-bord/afficher.phtml b/view/unicaen-indicateur/tableau-de-bord/afficher.phtml index eb58556276100dcc38c3687acf8f1d1b17b36e78..caba53e110c162322fdb52731637612636d8bb6b 100644 --- a/view/unicaen-indicateur/tableau-de-bord/afficher.phtml +++ b/view/unicaen-indicateur/tableau-de-bord/afficher.phtml @@ -95,8 +95,6 @@ $width = 12 / ((int) ($tableau?$tableau->getNbColumn():1)); <?php endif; ?> </div> </div> - - <span class="todo">Faire le view helper des données des indicateurs </span> <?php $exists = $indicateurService->verifierExistanceMaterializedView($indicateur->getViewId()); if ($exists === true) { @@ -106,24 +104,7 @@ $width = 12 / ((int) ($tableau?$tableau->getNbColumn():1)); } ?> <?php if ($exists) : ?> - <table id="indicateur_<?php echo $indicateur->getId(); ?>" class="datatable"> - <thead> - <tr> - <?php foreach ($header as $key ) : ?> - <th> <?php echo $key; ?> </th> - <?php endforeach; ?> - </tr> - </thead> - <tbody> - <?php foreach ($data as $item) : ?> - <tr> - <?php foreach ($item as $i) : ?> - <td> <?php echo $i; ?> </td> - <?php endforeach; ?> - </tr> - <?php endforeach; ?> - </tbody> - </table> + <?php echo $this->indicateur($indicateur); ?> <?php else : ?> Problème détecté sur l'indicateur. <?php endif; ?>