mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-04 22:04:59 +00:00
fix folder name
This commit is contained in:
72
src/Bundle/ChillMainBundle/Templating/CSVCellTwig.php
Normal file
72
src/Bundle/ChillMainBundle/Templating/CSVCellTwig.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
* Twig filter to transform a string in a safer way to be the content of a csv
|
||||
* cell.
|
||||
*
|
||||
* This filter replace the char " by ""
|
||||
*/
|
||||
class CSVCellTwig extends AbstractExtension
|
||||
{
|
||||
/*
|
||||
* Returns a list of filters to add to the existing list.
|
||||
*
|
||||
* (non-PHPdoc)
|
||||
* @see Twig_Extension::getFilters()
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new TwigFilter(
|
||||
'csv_cell',
|
||||
array($this, 'csvCellFilter'),
|
||||
array('is_safe' => array('html')))
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Replace into a string the char " by ""
|
||||
*
|
||||
* @param String $content The input string.
|
||||
* @return String The safe string.
|
||||
*/
|
||||
public function csvCellFilter($content)
|
||||
{
|
||||
return str_replace('"', '""', $content);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return The name of the extension.
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_main_csv_cell';
|
||||
}
|
||||
}
|
101
src/Bundle/ChillMainBundle/Templating/ChillTwigHelper.php
Normal file
101
src/Bundle/ChillMainBundle/Templating/ChillTwigHelper.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ChillTwigHelper extends AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_print_or_message', [$this, 'printOrMessage'], [
|
||||
'needs_environment' => true,
|
||||
'is_safe' => ['html', 'html_attrs']
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Print `value` inside a template, or, if $value is empty,
|
||||
* print $message.
|
||||
*
|
||||
* The template can be customized. The template is a full path to another
|
||||
* template, or one of the key belows:
|
||||
*
|
||||
* - 'default' ;
|
||||
* - 'blockquote' ;
|
||||
*
|
||||
* `DateTimeInterface are also rendered. The date and time format may be set
|
||||
* using those key in `$options´ parameter:
|
||||
*
|
||||
* - `date_format` (default to `'medium'`)
|
||||
* - `time_format` (default to `'none'`)
|
||||
*
|
||||
* @param Environment $twig
|
||||
* @param string $value Default to 'No value'. Fallback to default if null
|
||||
* @param string $message
|
||||
* @param string $template
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function printOrMessage(
|
||||
Environment $twig,
|
||||
$value,
|
||||
$message = 'No value',
|
||||
$template = 'default',
|
||||
array $options = []
|
||||
) {
|
||||
if ($value instanceof \DateTimeInterface) {
|
||||
$options = \array_merge([
|
||||
'date_format' => 'medium',
|
||||
'time_format' => 'none'
|
||||
], $options);
|
||||
switch ($template) {
|
||||
case 'default':
|
||||
case 'blockquote':
|
||||
$t = '@ChillMain/Extensions/PrintOrMessage/'.$template.'_date.html.twig';
|
||||
break;
|
||||
default:
|
||||
$t = $template;
|
||||
}
|
||||
} else {
|
||||
switch ($template) {
|
||||
case 'default':
|
||||
case 'blockquote':
|
||||
$t = '@ChillMain/Extensions/PrintOrMessage/'.$template.'.html.twig';
|
||||
break;
|
||||
default:
|
||||
$t = $template;
|
||||
}
|
||||
}
|
||||
|
||||
return $twig->render($t, \array_merge([
|
||||
'value' => $value,
|
||||
'message' => $message ?? 'No value'
|
||||
], $options));
|
||||
}
|
||||
}
|
152
src/Bundle/ChillMainBundle/Templating/ChillTwigRoutingHelper.php
Normal file
152
src/Bundle/ChillMainBundle/Templating/ChillTwigRoutingHelper.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\Node\Node;
|
||||
use Twig\TwigFunction;
|
||||
use Twig\TwigFilter;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Bridge\Twig\Extension\RoutingExtension;
|
||||
|
||||
/**
|
||||
* Provides function to build path with returnPath.
|
||||
*
|
||||
* The logic of the function is based on the original routing extension.
|
||||
*
|
||||
*/
|
||||
class ChillTwigRoutingHelper extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var RoutingExtension
|
||||
*/
|
||||
protected $originalExtension;
|
||||
|
||||
public function __construct(
|
||||
RequestStack $requestStack,
|
||||
RoutingExtension $originalExtension
|
||||
) {
|
||||
$this->requestStack = $requestStack;
|
||||
$this->originalExtension = $originalExtension;
|
||||
}
|
||||
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('chill_return_path_or', [$this, 'getReturnPathOr'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
||||
new TwigFunction('chill_path_add_return_path', [$this, 'getPathAddReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
||||
new TwigFunction('chill_path_forward_return_path', [$this, 'getPathForwardReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_return_path_label', [$this, 'getLabelReturnPath']),
|
||||
];
|
||||
}
|
||||
|
||||
public function isUrlGenerationSafe(Node $argsNode)
|
||||
{
|
||||
return $this->originalExtension->isUrlGenerationSafe($argsNode);
|
||||
}
|
||||
|
||||
public function getLabelReturnPath($default)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
return $request->query->get('returnPathLabel', null) ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the return path if it exists, or generate the path if not.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $relative
|
||||
* @return string
|
||||
*/
|
||||
public function getReturnPathOr($name, $parameters = [], $relative = false)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if ($request->query->has('returnPath')) {
|
||||
return $request->query->get('returnPath');
|
||||
}
|
||||
|
||||
return $this->originalExtension->getPath($name, $parameters, $relative);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an url with a returnPath parameter to current page
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $relative
|
||||
* @return string
|
||||
*/
|
||||
public function getPathAddReturnPath($name, $parameters = [], $relative = false, $label = null)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
$parameters['returnPath'] = $request->getRequestUri();
|
||||
|
||||
if ($label) {
|
||||
$parameters['returnPathLabel'] = $label;
|
||||
}
|
||||
|
||||
return $this->originalExtension->getPath($name, $parameters, $relative);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an url with a returnPath parameter to current page
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $relative
|
||||
* @return string
|
||||
*/
|
||||
public function getPathForwardReturnPath($name, $parameters = [], $relative = false)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if ($request->query->has('returnPath')) {
|
||||
$parameters['returnPath'] = $request->query->get('returnPath');
|
||||
}
|
||||
|
||||
return $this->originalExtension
|
||||
->getPath(
|
||||
$name,
|
||||
$parameters,
|
||||
$relative
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\Entity;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
abstract class AbstractChillEntityRender implements ChillEntityRenderInterface
|
||||
{
|
||||
protected function getDefaultOpeningBox($classSuffix): string
|
||||
{
|
||||
return '<span class="chill-entity chill-entity__'.$classSuffix.'">';
|
||||
}
|
||||
|
||||
protected function getDefaultClosingBox(): string
|
||||
{
|
||||
return '</span>';
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\Entity;
|
||||
|
||||
/**
|
||||
* Render an entity using `__toString()`
|
||||
*/
|
||||
class ChillEntityRender extends AbstractChillEntityRender
|
||||
{
|
||||
public function renderBox($entity, array $options): string
|
||||
{
|
||||
return $this->getDefaultOpeningBox('default').$entity
|
||||
.$this->getDefaultClosingBox();
|
||||
}
|
||||
|
||||
public function renderString($entity, array $options): string
|
||||
{
|
||||
return (string) $entity;
|
||||
}
|
||||
|
||||
public function supports($entity, array $options): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\Entity;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
* Class ChillEntityRenderExtension
|
||||
*
|
||||
* @package Chill\MainBundle\Templating\Entity
|
||||
*/
|
||||
class ChillEntityRenderExtension extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
* @var ChillEntityRenderInterface
|
||||
*/
|
||||
protected $renders = [];
|
||||
|
||||
/**
|
||||
* @var ChillEntityRender
|
||||
*/
|
||||
protected $defaultRender;
|
||||
|
||||
/**
|
||||
* ChillEntityRenderExtension constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->defaultRender = new ChillEntityRender();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|TwigFilter[]
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_entity_render_string', [$this, 'renderString'], [
|
||||
'is_safe' => [ 'html' ]
|
||||
]),
|
||||
new TwigFilter('chill_entity_render_box', [$this, 'renderBox'], [
|
||||
'is_safe' => [ 'html' ]
|
||||
])
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function renderString($entity, array $options = []): string
|
||||
{
|
||||
if (NULL === $entity) {
|
||||
return '';
|
||||
}
|
||||
return $this->getRender($entity, $options)
|
||||
->renderString($entity, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function renderBox($entity, array $options = []): string
|
||||
{
|
||||
if (NULL === $entity) {
|
||||
return '';
|
||||
}
|
||||
return $this->getRender($entity, $options)
|
||||
->renderBox($entity, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ChillEntityRenderInterface $render
|
||||
*/
|
||||
public function addRender(ChillEntityRenderInterface $render)
|
||||
{
|
||||
$this->renders[] = $render;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
* @param $options
|
||||
* @return ChillEntityRenderInterface|null
|
||||
*/
|
||||
protected function getRender($entity, $options): ?ChillEntityRenderInterface
|
||||
{
|
||||
foreach ($this->renders as $render) {
|
||||
if ($render->supports($entity, $options)) {
|
||||
return $render;
|
||||
}
|
||||
}
|
||||
return $this->defaultRender;
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\Entity;
|
||||
|
||||
/**
|
||||
* Interface to implement which will render an entity in template on a custom
|
||||
* manner.
|
||||
*/
|
||||
interface ChillEntityRenderInterface
|
||||
{
|
||||
/**
|
||||
* Return true if the class support this object for the given options
|
||||
*
|
||||
* @param type $entity
|
||||
* @param array $options
|
||||
* @return bool
|
||||
*/
|
||||
public function supports($entity, array $options): bool;
|
||||
|
||||
/**
|
||||
* Return the entity as a string.
|
||||
*
|
||||
* Example: returning the name of a person.
|
||||
*
|
||||
* @param object $entity
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function renderString($entity, array $options): string;
|
||||
|
||||
/**
|
||||
* Return the entity in a box
|
||||
*
|
||||
* Example: return a person inside a box:
|
||||
*
|
||||
* ```html
|
||||
* <span class="chill-entity">
|
||||
* <span class="chill-entity__first-name">Roger</span>
|
||||
* <span class="chill-entity__last-name">Dupont</span>
|
||||
* </span>
|
||||
* ```
|
||||
*
|
||||
* @param type $entity
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public function renderBox($entity, array $options): string;
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2020, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\Entity;
|
||||
|
||||
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Repository\UserRepository;
|
||||
use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender;
|
||||
|
||||
class CommentRender extends AbstractChillEntityRender
|
||||
{
|
||||
/**
|
||||
* @var \Chill\MainBundle\Repository\UserRepository
|
||||
*/
|
||||
private $userRepository;
|
||||
|
||||
public function __construct(UserRepository $userRepository)
|
||||
{
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CommentEmbeddable $entity
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function renderBox($entity, array $options): string
|
||||
{
|
||||
$username = '';
|
||||
|
||||
if ($entity->getUserId()) {
|
||||
$user = $this->userRepository->find($entity->getUserId());
|
||||
if ($user instanceof User) {
|
||||
$username = $user->getUsername();
|
||||
}
|
||||
}
|
||||
|
||||
$str = $this->getDefaultOpeningBox('comment-embeddable').
|
||||
'<span class="comment_comment">'.
|
||||
nl2br($entity->getComment()).
|
||||
'</span>';
|
||||
|
||||
if ($entity->getDate() instanceof \DateTime) {
|
||||
$str .= '<span class="comment_date">'.
|
||||
$entity->getDate()->format('d/m/Y H:i');
|
||||
'</span>';
|
||||
}
|
||||
|
||||
if (strlen($username) > 0) {
|
||||
$str .= '<span class="comment_user">'.
|
||||
$username.
|
||||
'</span>';
|
||||
}
|
||||
|
||||
$str .= $this->getDefaultClosingBox();
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CommentEmbeddable $entity
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function renderString($entity, array $options): string
|
||||
{
|
||||
return $entity->getComment();
|
||||
}
|
||||
|
||||
public function supports($entity, array $options): bool
|
||||
{
|
||||
return $entity instanceof CommentEmbeddable;
|
||||
}
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\Entity;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* Add service tagged with `chill.render_entity` to appropriate service
|
||||
*
|
||||
*/
|
||||
class CompilerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
$extension = $container->getDefinition(ChillEntityRenderExtension::class);
|
||||
|
||||
foreach ($container->findTaggedServiceIds('chill.render_entity') as $id => $tags) {
|
||||
$extension->addMethodCall('addRender', [new Reference($id)]);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Templating\Events;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* This event is transmitted on event chill_block.*
|
||||
*
|
||||
* You may access to the context as an array :
|
||||
*
|
||||
* ```
|
||||
* $var = $event['context_key']
|
||||
* ```
|
||||
*
|
||||
* The documentation for the bundle where the event is launched should give
|
||||
* you the context keys.
|
||||
*
|
||||
* The keys are read-only: if you try to update the context using array access
|
||||
* (example, using `$event['context_key'] = $bar;`, an error will be thrown.
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class DelegatedBlockRenderingEvent extends Event implements \ArrayAccess
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var mixed[]
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* The returned content of the event
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $content = '';
|
||||
|
||||
public function __construct(array $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* add content to the event. This content will be printed in the
|
||||
* layout which launched the event
|
||||
*
|
||||
* @param string $text
|
||||
*/
|
||||
public function addContent($text)
|
||||
{
|
||||
$this->content .= $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* the content of the event
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->context[$offset]);
|
||||
}
|
||||
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->context[$offset];
|
||||
}
|
||||
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
throw new \RuntimeException("The event context is read-only, you are not "
|
||||
. "allowed to update it.");
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
throw new \RuntimeException("The event context is read-only, you are not "
|
||||
. "allowed to update it.");
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a suite of a modules, Chill is a software for social workers
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* This helper helps to find the string in current locale from translatable_strings
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
*/
|
||||
class TranslatableStringHelper
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var RequestStack
|
||||
*/
|
||||
private $requestStack;
|
||||
|
||||
private $fallbackLocales;
|
||||
|
||||
public function __construct(RequestStack $requestStack, Translator $translator)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->fallbackLocales = $translator->getFallbackLocales();
|
||||
}
|
||||
|
||||
/**
|
||||
* return the string in current locale if it exists.
|
||||
*
|
||||
* If it does not exists; return the name in the first language available.
|
||||
*
|
||||
* Return a blank string if any strings are available.
|
||||
* Return NULL if $translatableString is NULL
|
||||
*
|
||||
* @param array $translatableStrings
|
||||
* @return string
|
||||
*/
|
||||
public function localize(array $translatableStrings)
|
||||
{
|
||||
if (NULL === $translatableStrings) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
$language = $this->requestStack->getCurrentRequest()->getLocale();
|
||||
|
||||
|
||||
if (isset($translatableStrings[$language])) {
|
||||
|
||||
return $translatableStrings[$language];
|
||||
} else {
|
||||
foreach ($this->fallbackLocales as $locale) {
|
||||
if (array_key_exists($locale, $translatableStrings)) {
|
||||
|
||||
return $translatableStrings[$locale];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// no fallback translation... trying the first available
|
||||
$langs = array_keys($translatableStrings);
|
||||
|
||||
if (count($langs) === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $translatableStrings[$langs[0]];
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class TranslatableStringTwig extends AbstractExtension
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var TranslatableStringHelper $helper
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
/**
|
||||
* TranslatableStringTwig constructor.
|
||||
*
|
||||
* @param TranslatableStringHelper $translatableStringHelper
|
||||
*/
|
||||
public function __construct(TranslatableStringHelper $translatableStringHelper)
|
||||
{
|
||||
$this->helper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a list of filters to add to the existing list.
|
||||
*
|
||||
* (non-PHPdoc)
|
||||
* @see Twig_Extension::getFilters()
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new TwigFilter(
|
||||
'localize_translatable_string', array($this, 'localize')));
|
||||
}
|
||||
|
||||
public function localize(array $translatableStrings)
|
||||
{
|
||||
return $this->helper
|
||||
->localize($translatableStrings);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return The name of the extension.
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_main_localize';
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\UI;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
|
||||
/**
|
||||
* Show a number of notification to user in the upper right corner
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CountNotificationUser
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var NotificationCounterInterface[]
|
||||
*/
|
||||
protected $counters = [];
|
||||
|
||||
public function addNotificationCounter(NotificationCounterInterface $counter)
|
||||
{
|
||||
$this->counters[] = $counter;
|
||||
}
|
||||
|
||||
public function getSumNotification(UserInterface $u): int
|
||||
{
|
||||
$sum = 0;
|
||||
|
||||
foreach ($this->counters as $counter) {
|
||||
$sum += $counter->addNotification($u);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating\UI;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
interface NotificationCounterInterface
|
||||
{
|
||||
/**
|
||||
* Add a number of notification
|
||||
*/
|
||||
public function addNotification(UserInterface $u): int;
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Templating\Widget;
|
||||
|
||||
use Twig\Environment;
|
||||
|
||||
interface WidgetInterface
|
||||
{
|
||||
public function render(Environment $env, $place, array $context, array $config);
|
||||
}
|
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Templating\Widget;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Chill\MainBundle\Templating\Widget\WidgetInterface;
|
||||
use Chill\MainBundle\Templating\Events\DelegatedBlockRenderingEvent;
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* Add the function `chill_delegated_block`.
|
||||
*
|
||||
* In a template, you can now allow rendering of a block from other bundle.
|
||||
*
|
||||
* The layout template must explicitly call the rendering of other block,
|
||||
* with the twig function
|
||||
*
|
||||
* ```
|
||||
* chill_delegated_block('block_name', { 'array' : 'with context' } )
|
||||
* ```
|
||||
*
|
||||
* This will launch an event
|
||||
* `Chill\MainBundle\Templating\Events\DelegatedBlockRenderingEvent` with
|
||||
* the event's name 'chill_block.block_name'.
|
||||
*
|
||||
* You may add content to the page using the function
|
||||
* `DelegatedBlockRenderingEvent::addContent`.
|
||||
*
|
||||
* See also the documentation of
|
||||
* `Chill\MainBundle\Templating\Events\DelegatedBlockRenderingEvent`
|
||||
* for usage of this event class
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class WidgetRenderingTwig extends AbstractExtension
|
||||
{
|
||||
|
||||
/**
|
||||
* Contains the widget. This is a double dimension array :
|
||||
*
|
||||
* - first key is the place,
|
||||
* - second key is the ordering ;
|
||||
* - the value is an array where the widget is the first argument and the
|
||||
* second is the config
|
||||
*
|
||||
* i.e :
|
||||
*
|
||||
* $widget['place']['ordering'] = array($widget, $config);
|
||||
*
|
||||
*
|
||||
*
|
||||
* @var array an array of widget by place and ordering
|
||||
*/
|
||||
protected $widget = array();
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $eventDispatcher;
|
||||
|
||||
public function __construct(EventDispatcherInterface $eventDispatcher)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
}
|
||||
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_main_widget';
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
new TwigFunction('chill_delegated_block',
|
||||
array($this, 'renderingWidget'),
|
||||
array(
|
||||
'is_safe' => array('html'),
|
||||
'needs_environment' => true,
|
||||
'deprecated' => true, 'alternative' => 'chill_widget'
|
||||
)),
|
||||
new TwigFunction('chill_widget',
|
||||
array($this, 'renderingWidget'),
|
||||
array('is_safe' => array('html'), 'needs_environment' => true))
|
||||
);
|
||||
}
|
||||
|
||||
public function renderingWidget(Environment $env, $block, array $context = array())
|
||||
{
|
||||
// get the content of widgets
|
||||
$content = '';
|
||||
foreach ($this->getWidgetsArraysOrdered($block) as $a) {
|
||||
/* @var $widget Widget\WidgetInterface */
|
||||
$widget = $a[0];
|
||||
$config = $a[1];
|
||||
|
||||
$content .= $widget->render($env, $block, $context, $config);
|
||||
}
|
||||
|
||||
// for old rendering events (deprecated)
|
||||
$event = new DelegatedBlockRenderingEvent($context);
|
||||
|
||||
$this->eventDispatcher->dispatch('chill_block.'.$block, $event);
|
||||
|
||||
return $content." ".$event->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* add a widget to this class, which become available for a future call.
|
||||
*
|
||||
* This function is used by DependencyInjection\CompilerPass\WidgetCompilerPass,
|
||||
* which add the widget to this class when it is created by from DI, according
|
||||
* to the given config under `chill_main`.
|
||||
*
|
||||
* @param string $place
|
||||
* @param WidgetInterface $widget
|
||||
* @param array $config
|
||||
*/
|
||||
public function addWidget($place, $ordering, $widget, array $config = array())
|
||||
{
|
||||
$this->widget[$place][$ordering] = array($widget, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $place
|
||||
* @return array
|
||||
*/
|
||||
protected function getWidgetsArraysOrdered($place)
|
||||
{
|
||||
if (!array_key_exists($place, $this->widget)) {
|
||||
$this->widget[$place] = array();
|
||||
}
|
||||
|
||||
\ksort($this->widget[$place]);
|
||||
|
||||
return $this->widget[$place];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user