简体   繁体   中英

Sonata Admin custom template

I am trying to create a custom page in Sonata where I basically read the current month created records of a particular table (Quotes). There is also money involved in every quote so at the end of the table I add an extra row for the Total of the month.

Now, since this is a custom template, I wanted to be able to put the Show action button that Sonata has on every quote, but I cannot find a way to do it... Any ideas? Thanks!

Im not using the Admin class since I am overriding the listAction method in a custom CRUDController as you can see here...

public function listAction()
    {
        if (false === $this->admin->isGranted('LIST')) {
            throw new AccessDeniedException();
        }

        $datagrid = $this->admin->getDatagrid();
        $formView = $datagrid->getForm()->createView();

        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

        $data = array();

        $em = $this->getDoctrine()->getManager();

        $request = $this->getRequest();

        $form = $this->createFormBuilder(null, array('label' => 'Month: '))
            ->add('month', 'choice', array(
                'choices' => $this->getMonths(),
                'label' => false
            ))
            ->add('search', 'submit', array(
                'attr' => array('class' => 'btn-small btn-info'),
            ))
            ->getForm();

        $form->handleRequest($request);

        if($request->isMethod('POST')){

            $startDate = new \DateTime($form->get('month')->getData());
            $endDate = new \DateTime($form->get('month')->getData());
            $endDate->modify('last day of this month');

        } else {

            $endDate = new \DateTime('now');
            $startDate = new \DateTime('now');
            $startDate->modify('first day of this month');

        }

        $dql = 'SELECT q FROM AcmeQuoteBundle:Quote q WHERE q.date BETWEEN \' '
            . $startDate->format('Y-m-d') . '\' AND \'' . $endDate->format('Y-m-d')
            . '\' ORDER BY q.date DESC';

        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query,
            $this->get('request')->query->get('page', 1)/*page number*/,
            50/*limit per page*/
        );

        $data['logrecords'] = $pagination;

        return $this->render('AcmeQuoteBundle:Admin:quoteReport.html.twig',
            array('data' => $data,
                'action' => 'list',
                'form'     => $form->createView(),
                'datagrid' => $datagrid
            ));
    }

and here is the twig that I am using:

{% block list_table %}

{% set logrecords = data.logrecords %}

{# total items count #}
<div class="count">
    <h4>Total number of records founded: {{ logrecords.getTotalItemCount }} </h4>
</div>
<table class="table table-striped table-hover table-bordered ">
    <thead class="info">
    <tr >
        <th>#</th>
        <th>{{ knp_pagination_sortable(logrecords, 'Created at', 'q.date') }}</th>
        <th>{{ knp_pagination_sortable(logrecords, 'Quote token', 'q.viewToken') }}</th>
        <th>Business name</th>
        <th>$AUD</th>
        <th>Action</th>
    </tr>
    </thead>
    <tbody>
    {% set grandTotal = 0.0 %}
    {% for quote in logrecords %}

        {% set total = 0.0 %}
        {% for item in quote.items %}
            {% set total = (item.unit * item.rate) %}
        {% endfor %}

        {% set grandTotal = grandTotal + total  %}

        <tr>
            <td>{{ loop.index }}</td>
            <td>{{ quote.date | date('Y-m-d') }}</td>
            <td>{{ quote.viewToken }}</td>
            <td>{{ quote.request.business.name }}</td>
            <td>{{ total }}</td>
            <td>
                <!-- THIS IS WHERE I WANT TO SHOW THE SHOW BUTTON -->
                <a href="" role="button"><i class="icon-search"></i> SHOW</a>
            </td>
        </tr>
    {% endfor %}
        <tr>
            <td colspan="3"></td>
            <td>TOTAL (AUD): </td>
            <td>{{ grandTotal }}</td>
            <td></td>
        </tr>
    </tbody>
</table>
{# display navigation #}
<div class="navigation">
    {{ knp_pagination_render(logrecords) }}
</div>

{% endblock list_table %}

If I understand correctly, you wanna add a new route with custom action and templates to your list view.

The first you have to do is to modify your Admin as following:

// Add a new route to your admin
protected function configureRoutes(RouteCollection $collection)
{
  $collection->add('custom_show_action', $this->getRouterIdParameter().'/customshow');
}

// Final Step is to add your custom route and a template to the listMapper
protected function configureListFields(ListMapper $listMapper)
{
    // Optional you can change the list layout with the following function
    // and to there some calculation on the visible entries.
    $this->setTemplate('list', '::custom_list_layout.html.twig');
    // ...
    $listMapper
        // ...
        ->add('_action', 'actions', array(
            'actions' => array(
                'view' => array(),
                'edit' => array(),
                'custom_show_action' => array('template' => '::your_template.html.twig'),
            )
        ))
    ;
   // ...
}

I recommend to calculate values in the controller. In my case I've done this for an invoice bundle in the following way:

// Acme/DemoBundle/Controller/AdminController
public function listAction()
{
    if (false === $this->admin->isGranted('LIST')) {
        throw new AccessDeniedException();
    }

    $datagrid = $this->admin->getDatagrid();
    $formView = $datagrid->getForm()->createView();

    $money_open = 0;
    $money_payed = 0;
    $money_referal = 0;

    foreach($datagrid->getResults() as $key => $object) {
        if ($object->getPayedAt()) {
            $money_payed += $object->getPrice();
            if ($object->getReferal() && $object->getReferalPrice() > 0) {
                $money_referal += $object->getReferalPrice();   
            }
        } else {
            $money_open += $object->getPrice();
        }
    }

    // set the theme for the current Admin Form
    $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

    return $this->render($this->admin->getTemplate('list'), array(
        'action'   => 'list',
        'form'     => $formView,
        'datagrid' => $datagrid,
        'money_open' => $money_open,
        'money_payed' => $money_payed,
        'money_referal' => $money_referal,
    ));
}

As you can see my money calculation is done in controller and I only have to set it to the template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM