| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MewesK\TwigSpreadsheetBundle\Wrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpOffice\PhpSpreadsheet\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PhpOffice\PhpSpreadsheet\IOFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PhpOffice\PhpSpreadsheet\Settings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpOffice\PhpSpreadsheet\Spreadsheet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhpOffice\PhpSpreadsheet\Writer\BaseWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Bridge\Twig\AppVariable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class DocumentWrapper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class DocumentWrapper extends BaseWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var Spreadsheet|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * DocumentWrapper constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param array             $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param \Twig_Environment $environment | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     public function __construct(array $context, \Twig_Environment $environment) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         parent::__construct($context, $environment); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->object = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array $properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws \PhpOffice\PhpSpreadsheet\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function start(array $properties = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // load template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (isset($properties['template'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $templatePath = $this->expandPath($properties['template']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $reader = IOFactory::createReaderForFile($templatePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->object = $reader->load($templatePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // create new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->object = new Spreadsheet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $this->object->removeSheetByIndex(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->attributes['properties'] = $properties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->setProperties($properties, $this->mappings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param bool        $preCalculateFormulas | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param null|string $diskCachingDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws \PhpOffice\PhpSpreadsheet\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function end(bool $preCalculateFormulas = true, string $diskCachingDirectory = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $format = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // try document property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (isset($this->attributes['format'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $format = $this->attributes['format']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |          // try Symfony request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         elseif (isset($this->context['app'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |              * @var AppVariable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $appVariable = $this->context['app']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $format = $appVariable->getRequest()->getRequestFormat(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         // set default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($format === null || !is_string($format)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $format = 'xlsx'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         switch (strtolower($format)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             case 'csv': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $writerType = 'Csv'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             case 'ods': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $writerType = 'Ods'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             case 'pdf': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $writerType = 'Pdf'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 if (!class_exists('mPDF')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     throw new Exception('Error loading mPDF. Is mPDF correctly installed?'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             case 'xls': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $writerType = 'Xls'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             case 'xlsx': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $writerType = 'Xlsx'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         if ($this->object !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |              * @var BaseWriter $writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $writer = IOFactory::createWriter($this->object, $writerType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $writer->setPreCalculateFormulas($preCalculateFormulas); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $writer->setUseDiskCaching($diskCachingDirectory !== null, $diskCachingDirectory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $writer->save('php://output'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->object = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @return Spreadsheet|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     public function getObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return $this->object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param Spreadsheet|null $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function setObject(Spreadsheet $object = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->object = $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     protected function configureMappings(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             'category' => function ($value) { $this->object->getProperties()->setCategory($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             'company' => function ($value) { $this->object->getProperties()->setCompany($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             'created' => function ($value) { $this->object->getProperties()->setCreated($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             'creator' => function ($value) { $this->object->getProperties()->setCreator($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             'defaultStyle' => function ($value) { $this->object->getDefaultStyle()->applyFromArray($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             'description' => function ($value) { $this->object->getProperties()->setDescription($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             'format' => function ($value) { $this->attributes['format'] = $value; }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             'keywords' => function ($value) { $this->object->getProperties()->setKeywords($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             'lastModifiedBy' => function ($value) { $this->object->getProperties()->setLastModifiedBy($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             'manager' => function ($value) { $this->object->getProperties()->setManager($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             'modified' => function ($value) { $this->object->getProperties()->setModified($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             'security' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 'lockRevision' => function ($value) { $this->object->getSecurity()->setLockRevision($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 'lockStructure' => function ($value) { $this->object->getSecurity()->setLockStructure($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 'lockWindows' => function ($value) { $this->object->getSecurity()->setLockWindows($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 'revisionsPassword' => function ($value) { $this->object->getSecurity()->setRevisionsPassword($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 'workbookPassword' => function ($value) { $this->object->getSecurity()->setWorkbookPassword($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             'subject' => function ($value) { $this->object->getProperties()->setSubject($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             'template' => function ($value) { $this->attributes['template'] = $value; }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             'title' => function ($value) { $this->object->getProperties()->setTitle($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * Resolves paths using Twig namespaces. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * The path must start with the namespace. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * Namespaces are case sensitive. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     private function expandPath(string $path): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $loader = $this->environment->getLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         if ($loader instanceof \Twig_Loader_Filesystem && mb_strpos($path, '@') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |              * @var \Twig_Loader_Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             foreach ($loader->getNamespaces() as $namespace) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |                 if (mb_strpos($path, $namespace) === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                     foreach ($loader->getPaths($namespace) as $namespacePath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |                         $expandedPathAttribute = str_replace('@'.$namespace, $namespacePath, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |                         if (file_exists($expandedPathAttribute)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |                             return $expandedPathAttribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 210 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 211 |  |  |  |