Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active June 3, 2021 08:34
Show Gist options
  • Select an option

  • Save webdevilopers/11520ffd82d762c4ca72 to your computer and use it in GitHub Desktop.

Select an option

Save webdevilopers/11520ffd82d762c4ca72 to your computer and use it in GitHub Desktop.

Revisions

  1. webdevilopers revised this gist Jun 29, 2015. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions services.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?xml version="1.0" ?>

    <container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
    <parameter key="plusquam.contract_bundle.block.recent_contracts.class">Plusquam\Bundle\ContractBundle\Block\RecentContractsBlockService</parameter>
    </parameters>

    <services>
    <service id="sonata.block.service.recent_contracts" class="%plusquam.contract_bundle.block.recent_contracts.class%">
    <tag name="sonata.block"/>

    <argument>sonata.order.block.recent_orders</argument>
    <argument type="service" id="templating"></argument>
    <argument type="service" id="doctrine.orm.entity_manager"></argument>
    <argument type="service" id="security.context"></argument>
    </service>
    </services>

    </container>
  2. webdevilopers revised this gist Jun 29, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions edit.html.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    {% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}

    {% block sonata_tab_content %}

    </div>

    {{ sonata_block_render({
    'type': 'sonata.block.service.recent_contracts'
    }) }}
  3. webdevilopers created this gist Jun 29, 2015.
    91 changes: 91 additions & 0 deletions RecentContractsBlockService.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    <?php

    namespace Plusquam\Bundle\ContractBundle\Block;

    use Sonata\BlockBundle\Block\BlockContextInterface;
    use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
    use Symfony\Component\HttpFoundation\Response;

    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Validator\ErrorElement;

    use Sonata\BlockBundle\Model\BlockInterface;
    use Sonata\BlockBundle\Block\BaseBlockService;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Symfony\Component\Security\Core\SecurityContextInterface;

    use Doctrine\ORM\EntityManager;

    /**
    * Class RecentContractBlockService
    *
    * @package Plusquam\Bundle\ContractBundle\Block;
    *
    * @author Michael Borchers
    */
    class RecentContractsBlockService extends BaseBlockService
    {
    /**
    * @var EntityManager
    */
    protected $entityManager;

    /**
    * @var SecurityContextInterface
    */
    protected $securityContext;

    /**
    * @param string $name
    * @param EngineInterface $templating
    * @param EntityManager $entityManager
    * @param SecurityContextInterface $securityContext
    */
    public function __construct($name, EngineInterface $templating, EntityManager $entityManager, SecurityContextInterface $securityContext)
    {
    $this->entityManager = $entityManager;
    $this->securityContext = $securityContext;

    parent::__construct($name, $templating);
    }

    /**
    * {@inheritdoc}
    */
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
    $criteria = array();

    $user = $this->securityContext->getToken()->getUser();
    $criteria['projectManager'] = $user->getId();

    return $this->renderPrivateResponse($blockContext->getTemplate(), array(
    'context' => $blockContext,
    'settings' => $blockContext->getSettings(),
    'block' => $blockContext->getBlock(),
    'contracts' => $this->entityManager->getRepository('PlusquamContractBundle:Contract')
    ->findBy($criteria, array('updatedAt' => 'DESC'), $blockContext->getSetting('number'))
    ), $response);
    }

    /**
    * {@inheritdoc}
    */
    public function getName()
    {
    return 'Recent Contracts';
    }

    /**
    * {@inheritdoc}
    */
    public function setDefaultSettings(OptionsResolverInterface $resolver)
    {
    $resolver->setDefaults(array(
    'number' => 5,
    'mode' => 'admin',
    'title' => 'Recent Contracts',
    'template' => 'PlusquamContractBundle:Block:recent_contracts.html.twig'
    ));
    }
    }
    4 changes: 4 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    sonata_block:
    default_contexts: [cms]
    blocks:
    sonata.block.service.recent_contracts: ~
    45 changes: 45 additions & 0 deletions recent_contracts.html.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    {% extends sonata_block.templates.block_base %}

    {% block block %}
    {% sonata_template_box 'This is the recent contracts list block.' %}

    <div class="sonata-contract-block-recent-contract panel panel-default">
    {% if settings.title %}
    <div class="panel-heading">
    <div class="panel-title">
    <h4 class="sonata-contract-block-recent-contract">{{ settings.title }}</h4>
    </div>
    </div>
    {% endif %}

    {% if contracts|length > 0 %}
    <table class="sonata-contract-block-contract-container table table-condensed">
    {% for contract in contracts %}
    <tr>
    {% if context.getSetting('mode') == 'admin' %}
    <td><a href="{{ url('admin_plusquam_contract_contract_edit', { 'id': contract.id }) }}">{{ contract.number }}</a></td>
    {% else %}
    <td><a href="{{ url('admin_plusquam_contract_contract_show', { 'reference': contract.updatedAt }) }}">{{ contract.updatedAt }}</a></td>
    {% endif %}
    <td>{{ contract.createdAt|format_datetime(null) }}</td>
    </tr>
    {% endfor %}
    </table>
    <div class="panel-body">
    {% if context.getSetting('mode') == 'admin' %}
    <a href="{{ url('admin_plusquam_contract_contract_list') }}" class="btn btn-primary btn-small pull-right"><i class="icon-list icon-white"></i>&nbsp;{{ 'view_all_contracts'|trans({}, 'SonataOrderBundle') }}</a>
    {% else %}
    <a href="{{ url('admin_plusquam_contract_contract_list') }}" class="btn btn-primary btn-small pull-right"><i class="glyphicon glyphicon-list icon-white"></i>&nbsp;{{ 'view_all_contracts'|trans({}, 'SonataOrderBundle') }}</a>
    {% endif %}
    </div>
    {% else %}
    <div class="panel-body">
    {% if context.getSetting('mode') == 'admin' %}
    <p>{{ 'no_contract_found'|trans({}, 'SonataOrderBundle') }}</p>
    {% else %}
    <p>{{ 'sonata.contract.list.no_contracts'|trans({}, 'SonataOrderBundle') }}</p>
    {% endif %}
    </div>
    {% endif %}
    </div>
    {% endblock %}