<?php
namespace App\Admin;
use App\Entity\User;
use App\Utils\DateUtils;
use DateTime;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin as BaseAbstractAdmin;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Filter\Model\FilterData;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
use Sonata\DoctrineORMAdminBundle\Filter\DateRangeFilter;
use Sonata\Form\Type\DateRangePickerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class AbstractAdmin extends BaseAbstractAdmin
{
private Security $security;
public function __construct(?string $code = null, ?string $class = null, ?string $baseControllerName = null, Security $security = null)
{
parent::__construct($code, $class, $baseControllerName);
$this->security = $security;
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
if (is_object($this->getUser()) && $this->getUser()->hasRole(User::ROLE_ADMIN)) {
// disable softdeleted filter for admin
$filters = $this->getModelManager()->getEntityManager($this->getClass())->getFilters();
if (array_key_exists('softdeleteable', $filters->getEnabledFilters())) {
$filters->disable('softdeleteable');
}
}
}
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $form): void
{
if (is_null($this->getSubject()) || is_null($this->getSubject()->getId())) {
$this->configureFormFieldsCreation($form);
} else {
$this->configureFormFieldsEdition($form);
}
}
protected function getUser(): ?UserInterface
{
$token = $this->getSecurity()->getToken();
if ($token) {
return $token->getUser();
}
return null;
}
protected function getRepository()
{
return $this->getModelManager()->getEntityManager($this->getClass())->getRepository($this->getClass());
}
/**
* Add a filter on a time range
* @param DatagridMapper $filter
* @param string $fieldName // name of the entity field
*/
protected function addTimeRangeFilterOnField(DatagridMapper $filter, string $fieldName)
{
$filter->add($fieldName, DateRangeFilter::class, [
'advanced_filter' => false,
'show_filter' => true,
'field_type' => DateRangePickerType::class,
]);
}
/**
* Add a filter on a period for a given field
* @param DatagridMapper $filter
* @param string $fieldName // name of the entity field
* @param null $fullFieldName
*/
protected function addPeriodFilterOnField(DatagridMapper $filter, string $fieldName, $fullFieldName = null, string $label=null)
{
//filter on a period
$filter
->add('period', CallbackFilter::class, array(
'show_filter' => true,
'advanced_filter' => false,
'label' => !is_null($label)?$label:'Période',
'callback' => function (ProxyQueryInterface $query, string $alias, string $field, FilterData $data) use ($fieldName, $fullFieldName) {
if (!$data->hasValue()) {
return false;
}
$startAndEndDates = DateUtils::getFirstAndLastDayOfPeriod($data->getValue());
$startDate = $startAndEndDates[0];
$endDate = $startAndEndDates[1];
// $queryBuilder->join($alias . '.prospectsOnStore', 'pos');
if (is_null($fullFieldName)) {
$fullFieldName = $alias . '.' . $fieldName;
}
$query->andWhere($fullFieldName . ' >= :startDate and ' . $fullFieldName . ' <= :endDate');
$query->setParameter('startDate', $startDate);
$query->setParameter('endDate', $endDate);
return true;
},
'field_type' => ChoiceType::class,
'field_options' => array(
'choices' => [
'Aujourd\'hui' => DateUtils::PERIOD_TODAY,
'Semaine en cours' => DateUtils::PERIOD_THIS_WEEK,
'Mois en cours' => DateUtils::PERIOD_THIS_MONTH,
'Année en cours' => DateUtils::PERIOD_THIS_YEAR,
'Semaine dernière' => DateUtils::PERIOD_LAST_WEEK,
'Mois dernier' => DateUtils::PERIOD_LAST_MONTH,
'Année dernière' => DateUtils::PERIOD_LAST_YEAR,
],
),
));
}
/**
* Add a filter on a period for a given field
* @param DatagridMapper $filter
* @param string $fieldName // name of the entity field
* @param null $label
* @param null $fullFieldName
*/
protected function addPreviousMonthsFilterOnField(
DatagridMapper $filter,
string $fieldName,
$label=null,
$fullFieldName=null
) {
$now = new DateTime();
$choices = [];
for ($m = 0; $m < 24; $m++) {
$choices[$now->format('m/y')] = $now->format('Y-m-01');
$now->modify('-1 months');
}
$filter
->add('month', CallbackFilter::class, array(
'show_filter' => true,
'advanced_filter' => false,
'label' => is_null($label) ? 'Mois' : $label,
'callback' => function (ProxyQueryInterface $query, string $alias, string $field, FilterData $data) use ($fieldName, $fullFieldName) {
if (!$data->hasValue()) {
return false;
}
$now = DateUtils::getFirstDayOfMonthMidnightFromString($data->getValue());
$startDate = clone $now;
//$startDate->modify('-'.$data->getValue().' months');
$endDate = clone $startDate;
$endDate = DateUtils::getLastDayOfMonthMidnight($endDate);
// $queryBuilder->join($alias . '.prospectsOnStore', 'pos');
if (is_null($fullFieldName)) {
$fullFieldName = $alias . '.' . $fieldName;
}
$query->andWhere($fullFieldName . ' >= :startDate and ' . $fullFieldName . ' <= :endDate');
$query->setParameter('startDate', $startDate);
$query->setParameter('endDate', $endDate);
return true;
},
'field_type' => ChoiceType::class,
'field_options' => array(
'choices' => $choices,
),
));
}
/**
* Add a filter on acquisitionMode
* @param DatagridMapper $filter
* @param string $fieldName // name of the entity field
*/
protected function addAcquisitionModeFilterOnField(DatagridMapper $filter, string $fieldName)
{
$choices = [];
$modes = ['SMS', 'Emailing', 'Facebook', 'google', 'GoogleAds', 'bing', 'TELEMARKETING', 'Webpush', 'TLMKT', 'Blog', '4366-1', '1429-1',
'3219-19', 'taboola', 'Annuaire-ORL', '659-1', '5052-1', 'emailing'];
foreach ($modes as $m) {
$m = strtolower($m);
$choices[$m] = $m;
}
asort($choices);
$filter
->add($fieldName, ChoiceFilter::class, array(
'show_filter' => true,
'advanced_filter' => false,
'label' => 'Mode d\'acquisition',
'field_type' => ChoiceType::class,
'field_options' => array(
'choices' => $choices,
),
));
}
/**
* Add a filter on acquisitionBase
* @param DatagridMapper $filter
* @param $fieldName // name of the entity field
*/
protected function addAcquisitionBaseFilterOnField(DatagridMapper $filter, $fieldName)
{
$filter
->add($fieldName, null, array(
'show_filter' => true,
'advanced_filter' => false,
'label' => 'Base d\'acquisition',
));
}
/**
* Add a filter on admin type (partner or customer)
* @param DatagridMapper $filter
* @param $fieldName // name of the entity field
*/
protected function addAdminTypeFilterOnField(DatagridMapper $filter, $fieldName)
{
$choices = [
'Indépendants' => 'freelancer',
'Partenaire' => 'partner',
];
$filter
->add($fieldName, ChoiceFilter::class, array(
'show_filter' => true,
'advanced_filter' => false,
'label' => 'Type de client',
'field_type' => ChoiceType::class,
'field_options' => array(
'choices' => $choices,
),
));
}
public function setSecurity($security)
{
$this->security = $security;
}
public function getSecurity(): Security
{
return $this->security;
}
protected function configureDefaultSortValues(array &$sortValues): void
{
// reverse order (default = 'ASC')
$sortValues[DatagridInterface::SORT_ORDER] = 'DESC';
// name of the ordered field (default = the model's id field, if any)
$sortValues[DatagridInterface::SORT_BY] = 'id';
}
}