| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\AdminBundle\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\AdminBundle\Admin\AdminInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sonata\AdminBundle\Admin\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sonata\AdminBundle\Search\SearchHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sonata\BlockBundle\Block\BlockContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sonata\BlockBundle\Block\Service\AbstractBlockService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Twig\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @final since sonata-project/admin-bundle 3.52 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @author Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class AdminSearchBlockService extends AbstractBlockService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var Pool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var SearchHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected $searchHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * NEXT_MAJOR: Remove `$deprecatedTemplatingOrNull` argument. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param Environment|string $twigOrDeprecatedName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $twigOrDeprecatedName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ?EngineInterface $deprecatedTemplatingOrNull, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         Pool $pool, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         SearchHandler $searchHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // NEXT_MAJOR: Remove conditions, only pass the Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if ($deprecatedTemplatingOrNull instanceof EngineInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             @trigger_error(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 . ' and will throw a \TypeError in version 4.0. You must pass null instead.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 EngineInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 __METHOD__ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ), E_USER_DEPRECATED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             if (!\is_string($twigOrDeprecatedName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 throw new \TypeError(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     . ' You must pass an string instead.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     EngineInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             parent::__construct($twigOrDeprecatedName, $deprecatedTemplatingOrNull); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } elseif ($twigOrDeprecatedName instanceof Environment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if (!\is_null($deprecatedTemplatingOrNull)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 throw new \TypeError(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     Environment::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     EngineInterface::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             parent::__construct($twigOrDeprecatedName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             throw new \TypeError(sprintf( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 __METHOD__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 Environment::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->pool = $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->searchHandler = $searchHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code')); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         } catch (ServiceNotFoundException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         if (!$admin instanceof AdminInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             throw new \RuntimeException('The requested service is not an Admin instance'); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         $admin->checkAccess('list'); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $pager = $this->searchHandler->search( | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             $admin, | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             $blockContext->getSetting('query'), | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             $blockContext->getSetting('page'), | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             $blockContext->getSetting('per_page') | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         if (false === $pager) { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |             $response = $response ?: new Response(); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             return $response->setContent('')->setStatusCode(204); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |             'block' => $blockContext->getBlock(), | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             'settings' => $blockContext->getSettings(), | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             'admin_pool' => $this->pool, | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             'pager' => $pager, | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             'admin' => $admin, | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         ], $response); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return 'Admin Search Result'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function configureSettings(OptionsResolver $resolver): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $resolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ->setDefaults([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 'admin_code' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 'query' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 'page' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 'per_page' => 10, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 'icon' => '<i class="fa fa-list"></i>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             ->setRequired('admin_code') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             ->setAllowedTypes('admin_code', ['string']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                        
If you suppress an error, we recommend checking for the error condition explicitly: