Sonata Admin自定义模板

时间:2013-09-03 07:19:54

标签: php symfony sonata-admin

我正在尝试在Sonata中创建一个自定义页面,其中我基本上读取了当前月份创建的特定表格的记录(行情)。每个报价都涉及到金钱,因此在表格末尾我为该月的总计添加了一行。

现在,由于这是一个自定义模板,我希望能够在每个引号上放置Sonata所具有的Show动作按钮,但我找不到办法来做到这一点......任何想法?谢谢!

我没有使用Admin类,因为我正在覆盖自定义CRUDController中的listAction方法,你可以在这里看到...

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
            ));
    }

这是我正在使用的树枝:

{% 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 %}

1 个答案:

答案 0 :(得分:4)

如果我理解正确,您需要在列表视图中添加带有自定义操作和模板的新路线。

您首先需要修改您的管理员,如下所示:

// 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'),
            )
        ))
    ;
   // ...
}

我建议在控制器中计算值。在我的情况下,我已通过以下方式为发票包完成此操作:

// 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,
    ));
}

正如您所见,我的资金计算是在控制器中完成的,我只需将其设置为模板。