| 1 |  |  | <?php namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Button\Guesser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Anomaly\Streams\Platform\Addon\Module\ModuleCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Contracts\Routing\UrlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class HrefGuesser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link          http://anomaly.is/streams-Platform | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author        AnomalyLabs, Inc. <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author        Ryan Thompson <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package       Anomaly\Streams\Platform\Ui\ControlPanel\Component\Button\Guesser | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | class HrefGuesser | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * The URL generator. | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @var UrlGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * The request object. | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var Request | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     protected $request; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * The module collection. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var ModuleCollection | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     protected $modules; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Create a new HrefGuesser instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param UrlGenerator     $url | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param Request          $request | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param ModuleCollection $modules | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function __construct(UrlGenerator $url, Request $request, ModuleCollection $modules) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->url     = $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->modules = $modules; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * Guess the HREF for a button. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param ControlPanelBuilder $builder | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function guess(ControlPanelBuilder $builder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $buttons  = $builder->getButtons(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $sections = $builder->getControlPanelSections(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $active = $sections->active(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $module = $this->modules->active(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         foreach ($buttons as &$button) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             // If we already have an HREF then skip it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (isset($button['attributes']['href'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             // Determine the HREF based on the button type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             switch (array_get($button, 'button')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 case 'add': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 case 'new': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 case 'create': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     $button['attributes']['href'] = $active->getHref('create'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 case 'export': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     if ($module) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                         $button['attributes']['href'] = $this->url->to( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                             'entry/handle/export/' . $module->getNamespace() . '/' . array_get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                                 $button, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                                 'namespace' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                             ) . '/' . array_get($button, 'stream') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if (!isset($button['attributes']['href']) && isset($button['button'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $button['attributes']['href'] = $active->getHref($button['button']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $builder->setButtons($buttons); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |