Advertisement
Guest User

PHPExcel\Template

a guest
Nov 27th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Mozart
  5.  * Date: 28.11.2014
  6.  * Time: 0:18
  7.  */
  8.  
  9.  
  10. class PHPExcel_Template {
  11.     /**
  12.      * ZipArchive object
  13.      *
  14.      * @var mixed
  15.      */
  16.     private $zipClass;
  17.     /**
  18.      * Temporary file name
  19.      *
  20.      * @var string
  21.      */
  22.     private $tempFileName;
  23.     /**
  24.      * Document XML
  25.      *
  26.      * @var string
  27.      */
  28.     private $documentXML;
  29.  
  30.     function __construct($strFilename) {
  31.  
  32.         $this->tempFileName = tempnam(sys_get_temp_dir(), '');
  33.         if ($this->tempFileName === false) {
  34.             throw new Exception('Could not create temporary file with unique name in the default temporary directory.');
  35.         }
  36.         // Copy the source File to the temp File
  37.         if (!copy($strFilename, $this->tempFileName)) {
  38.             throw new Exception("Could not copy the template from {$strFilename} to {$this->tempFileName}.");
  39.         }
  40.         $this->zipClass = new PHPExcel_Shared_ZipArchive();
  41.         $this->zipClass->open($this->tempFileName);
  42.  
  43.         $this->documentXML = $this->zipClass->getFromName('xl/sharedStrings.xml');
  44.  
  45.     }
  46.  
  47.     public function save()
  48.     {
  49.         // Close zip file
  50.         if ($this->zipClass->close() === false) {
  51.             throw new PHPExcel_Exception('Could not close zip file.');
  52.         }
  53.         return $this->tempFileName;
  54.     }
  55.  
  56.     public function saveAs($strFilename)
  57.     {
  58.         $this->zipClass->addFromString('xl/sharedStrings.xml', $this->documentXML);
  59.         $tempFilename = $this->save();
  60.         if (file_exists($strFilename)) {
  61.             unlink($strFilename);
  62.         }
  63.         rename($tempFilename, $strFilename);
  64.     }
  65.  
  66.     public function getString() {
  67.         return $this->documentXML;
  68.     }
  69.  
  70.     protected function getVariablesForPart($documentPartXML)
  71.     {
  72.         preg_match_all('/\$\{(.*?)}/i', $documentPartXML, $matches);
  73.         return $matches[1];
  74.     }
  75.  
  76.     public function getVariables()
  77.     {
  78.         $variables = $this->getVariablesForPart($this->documentXML);
  79.         return array_unique($variables);
  80.     }
  81.  
  82.  
  83.     protected function setValueForPart($documentPartXML, $search, $replace, $limit)
  84.     {
  85.         $pattern = '|\$\{([^\}]+)\}|U';
  86.         preg_match_all($pattern, $documentPartXML, $matches);
  87.         foreach ($matches[0] as $value) {
  88.             $valueCleaned = preg_replace('/<[^>]+>/', '', $value);
  89.             $valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
  90.             $documentPartXML = str_replace($value, $valueCleaned, $documentPartXML);
  91.         }
  92.         if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
  93.             $search = '${' . $search . '}';
  94.         }
  95.         $replace = htmlspecialchars($replace);
  96.         $regExpDelim = '/';
  97.         $escapedSearch = preg_quote($search, $regExpDelim);
  98.         return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit);
  99.     }
  100.  
  101.     public function setValue($search, $replace, $limit = -1)
  102.     {
  103.         $this->documentXML = $this->setValueForPart($this->documentXML, $search, $replace, $limit);
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement