| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * A button you can add to a GridField to export that GridField as a CSV. Should work with any sized GridField, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * as the export is done using a queuedjob in the background. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class GridFieldQueuedExportButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var array Map of a property name on the exported objects, with values being the column title in the CSV file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Note that titles are only used when {@link $csvHasHeader} is set to TRUE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $exportColumns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $csvSeparator = ","; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $csvHasHeader = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Fragment to write the button to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $targetFragment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $targetFragment The HTML fragment to write the button into | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array $exportColumns The columns to include in the export | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct($targetFragment = "after", $exportColumns = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->targetFragment = $targetFragment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->exportColumns = $exportColumns; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Place the export button in a <p> tag below the field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function getHTMLFragments($gridField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $button = new GridField_FormAction( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $gridField, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'export', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             _t('TableListField.CSVEXPORT', 'Export to CSV'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'export', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $button->setAttribute('data-icon', 'download-csv'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $button->addExtraClass('action_batch_export'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $button->setForm($gridField->getForm()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * This class is an action button | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function getActions($gridField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return array('export', 'findgridfield'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function handleAction(GridField $gridField, $actionName, $arguments, $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ($actionName == 'export') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return $this->startExport($gridField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } else if ($actionName == 'findgridfield') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return new GridFieldQueuedExportButton_Response($gridField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     function startExport($gridField) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $job = new GenerateCSVJob(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         // Set the parameters that allow re-discovering this gridfield during background execution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $job->setGridField($gridField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $job->setSession(Controller::curr()->getSession()->get_all()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // Set the parameters that control CSV exporting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $job->setSeparator($this->csvSeparator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $job->setIncludeHeader($this->csvHasHeader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($this->exportColumns) $job->setColumns($this->exportColumns); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Queue the job | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         singleton('QueuedJobService')->queueJob($job); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // Redirect to the status update page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return Controller::curr()->redirect($gridField->Link() . '/export/' . $job->getSignature()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * This class is also a URL handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getURLHandlers($gridField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'export/$ID'          => 'checkExport', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'export_download/$ID' => 'downloadExport' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Handle the export, for both the action button and the URL | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     public function checkExport($gridField, $request = null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $id = $request->param('ID'); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         $job = QueuedJobDescriptor::get()->filter('Signature', $id)->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         $controller = $gridField->getForm()->getController(); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $breadcrumbs = $controller->Breadcrumbs(false); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         $breadcrumbs->push(new ArrayData(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             'Title' => 'Export CSV', | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             'Link'  => false | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         ))); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         $parents = $controller->Breadcrumbs(false)->items; | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $backlink = array_pop($parents)->Link; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |         $data = new ArrayData(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             'Backlink'    => $backlink, | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             'Breadcrumbs' => $breadcrumbs, | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             'GridName'    => $gridField->getname() | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         if ($job->JobStatus == QueuedJob::STATUS_COMPLETE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             $data->Link = $gridField->Link() . '/export_download/' . $job->Signature; | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         } else if ($job->JobStatus == QueuedJob::STATUS_BROKEN) { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             $data->ErrorMessage = "Sorry, but there was an error exporting the CSV"; | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         } else if ($job->JobStatus == QueuedJob::STATUS_CANCELLED) { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             $data->ErrorMessage = "This export job was cancelled"; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             $data->Count = $job->StepsProcessed; | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |             $data->Total = $job->TotalSteps; | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $return = $data->renderWith('GridFieldQueuedExportButton'); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         if ($request->isAjax()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |             return $return; | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |             return $controller->customise(array('Content' => $return)); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function downloadExport($gridField, $request = null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $id = $request->param('ID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $now = Date("d-m-Y-H-i"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $fileName = "export-$now.csv"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return SS_HTTPRequest::send_file(file_get_contents('/tmp/' . $id . '.csv'), $fileName, 'text/csv'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function getExportColumns() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         return $this->exportColumns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function setExportColumns($cols) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $this->exportColumns = $cols; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function getCsvSeparator() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         return $this->csvSeparator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     public function setCsvSeparator($separator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $this->csvSeparator = $separator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     public function getCsvHasHeader() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         return $this->csvHasHeader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @param boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     public function setCsvHasHeader($bool) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         $this->csvHasHeader = $bool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  * A special type of SS_HTTPResponse that GridFieldQueuedExportButton returns in response to the "findgridfield" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  * action, which includes a reference to the gridfield | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | class GridFieldQueuedExportButton_Response extends SS_HTTPResponse { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     private $gridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     public function __construct(GridField $gridField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $this->gridField = $gridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         parent::__construct('', 500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |     public function getGridField() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         return $this->gridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 216 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 217 |  |  |  | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.