| 1 |  |  | <?php namespace Anomaly\Streams\Platform\Http\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Anomaly\Streams\Platform\Ui\Form\Command\GetFormCriteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Anomaly\Streams\Platform\Ui\Form\FormBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Anomaly\Streams\Platform\Ui\Form\FormCriteria; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Cache\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Routing\Redirector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class FormController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link   http://pyrocms.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author PyroCMS, Inc. <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Ryan Thompson <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class FormController extends PublicController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Handle the form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param  Repository                                 $cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param  Redirector                                 $redirect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param                                             $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |     public function handle(Repository $cache, Redirector $redirect, $key) | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         $parameters = $cache->get('form::' . $key); | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         /* @var FormCriteria $criteria */ | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         $criteria = $this->dispatch(new GetFormCriteria($parameters)); | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         /* @var FormBuilder $builder */ | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         $builder = $criteria->build(); | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |         $response = $builder | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |             ->build() | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |             ->post() | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |             ->getFormResponse(); | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |         $builder->flash(); | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         if (!$builder->hasFormErrors()) { | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |             $cache->forget('form::' . $key); | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         if ($response && $response->getStatusCode() !== 200) { | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             return $response; | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         if ($builder->isAjax()) { | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             return $builder->getFormResponse(); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         if ($builder->hasFormErrors()) { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             return $redirect->back(); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         return $response ?: $redirect->back(); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |