Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/ContactBundle/Admin/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/ContactBundle/Admin/MailChimpBatchAdmin.php

<?php

namespace App\ContactBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * MailChimp Batch Admin
 *
 * @author Otto Saayman <otto@igotafrica.com>
 * @package ContactBundle
 * @subpackage Admin
 * @version 0.0.1
 */
class MailChimpBatchAdmin extends AbstractAdmin
{
    protected function generateBaseRouteName(bool $isChildAdmin = false): string
    {
        return 'mailchimp_batch';
    }

    protected function generateBaseRoutePattern(bool $isChildAdmin = false): string
    {
        return 'mailchimp_batch';
    }

    /**
     * Container
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @return null
     */
    public function setContainer($container)
    {
        $this->container = $container;
    }

    /**
     * @return ContainerInterface
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Configure list fields
     * @param ListMapper $listMapper
     * @return void
     */
    protected function configureListFields(ListMapper $listMapper): void
    {
        $listMapper
            ->add('id', null, ['label' => 'Batch ID'])
            ->add('status', null, [
                'label' => 'Status',
            ])
            ->add('totalOperations', null, ['label' => 'Total Operations'])
            ->add('finishedOperations', null, ['label' => 'Finished Operations'])
            ->add('erroredOperations', null, ['label' => 'Errored Operations'])
            ->add('submittedAt', null, ['label' => 'Submitted At'])
            ->add('completedAt', null, ['label' => 'Completed At'])
            ->add('_action', 'actions', [
                'label' => 'Actions',
                'actions' => [
                    'show' => [],
                    'refresh' => [
                        'template' => '@ContactBundle/Admin/MailChimpBatch/list__action_refresh.html.twig'
                    ]
                ]
            ])
        ;
    }

    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $showMapper
            ->add('id', null, ['label' => 'Batch ID'])
            ->add('status', null, ['label' => 'Status'])
            ->add('totalOperations', null, ['label' => 'Total Operations'])
            ->add('finishedOperations', null, ['label' => 'Finished Operations'])
            ->add('erroredOperations', null, ['label' => 'Errored Operations'])
            ->add('submittedAt', null, ['label' => 'Submitted At'])
            ->add('completedAt', null, ['label' => 'Completed At'])
            ->add('responseBodyUrl', null, [
                'label' => 'Response URL',
                'template' => '@ContactBundle/Admin/MailChimpBatch/show__responseBodyUrl.html.twig'
            ])
            ->add('jsonReports', null, [
                'label' => 'JSON Reports',
                'template' => '@ContactBundle/Admin/MailChimpBatch/show__jsonReports.html.twig'
            ])
        ;
    }

    protected function configureRoutes(RouteCollectionInterface $collection): void
    {
        $collection
            ->remove('create')
            ->remove('edit')
            ->remove('delete')
            ->add('refresh', $this->getRouterIdParameter() . '/refresh')
        ;
    }

    /**
     * Refresh batch from MailChimp API
     */
    public function refreshAction($id)
    {
        $object = $this->admin->getObject($id);

        if (!$object) {
            throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Batch not found');
        }

        $batchManager = $this->getContainer()->get('contact.mailchimp_batch.manager');
        $success = $batchManager->refreshBatch($object->getId());

        if ($success) {
            $this->addFlash('sonata_flash_success', 'Batch refreshed successfully from MailChimp');
        } else {
            $this->addFlash('sonata_flash_error', 'Failed to refresh batch from MailChimp');
        }

        return $this->redirect($this->admin->generateUrl('list'));
    }
}