| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sunnysideup\EcommerceGoogleShoppingFeed\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Controller for displaying the xml feed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * <code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * http://site.com/shoppingfeed.txt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * </code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class TSVGoogleShoppingFeedController extends GoogleShoppingFeedController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private static $allowed_actions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'index', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function TSVOutput() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $apiClass = Config::inst()->get(GoogleShoppingFeedController::class, 'api_class'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $apiClass = new $apiClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $data = $apiClass->getTSVData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return $this->convertToCSV($data, "\t"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function getExtension(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return 'txt'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function getContentType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return 'text/tab-separated-values; charset="utf-8"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     protected function convertToCSV($rows, $delimiter = ';', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $delimiter_esc = preg_quote($delimiter, '/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $enclosure_esc = preg_quote($enclosure, '/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $string = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         foreach ($rows as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             if ($string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 $string .= "\r\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $output = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             foreach ($row as $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 if (! $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                     $output[] = $enclosure . $field . $enclosure; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 } elseif ($encloseAll || preg_match("/(?:{$delimiter_esc}|{$enclosure_esc}|\\s)/", $field)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     $output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                     $output[] = $field; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $string .= implode($delimiter, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             unset($output); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.