]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
$reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
$this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
foreach ($segment->manif_vars as $val)
$this->manif_vars[] = $val; //copy all segment image names into current array
return $this;
}
/**
* Display all the current template variables
*
* @return string
*/
public function printVars()
{
return print_r('' . print_r($this->vars, true) . '
', true);
}
/**
* Display the XML content of the file from odt document
* as it is at the moment
*
* @return string
*/
public function __toString()
{
return $this->contentXml;
}
/**
* Display loop segments declared with setSegment()
*
* @return string
*/
public function printDeclaredSegments()
{
return '' . print_r(implode(' ', array_keys($this->segments)), true) . '
';
}
/**
* Declare a segment in order to use it in a loop
*
* @param string $segment
* @throws OdfException
* @return Segment
*/
public function setSegment($segment)
{
if (array_key_exists($segment, $this->segments)) {
return $this->segments[$segment];
}
// $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)\[!--\sEND\s$segment\s--\]#sm";
$reg = "#\[!--\sBEGIN\s$segment\s--\](.*)\[!--\sEND\s$segment\s--\]#sm";
if (preg_match($reg, html_entity_decode($this->contentXml), $m) == 0) {
throw new OdfException("'$segment' segment not found in the document");
}
$this->segments[$segment] = new Segment($segment, $m[1], $this);
return $this->segments[$segment];
}
/**
* Save the odt file on the disk
*
* @param string $file name of the desired file
* @throws OdfException
* @return void
*/
public function saveToDisk($file = null)
{
if ($file !== null && is_string($file)) {
if (file_exists($file) && !(is_file($file) && is_writable($file))) {
throw new OdfException('Permission denied : can\'t create ' . $file);
}
$this->_save();
copy($this->tmpfile, $file);
} else {
$this->_save();
}
}
/**
* Internal save
*
* @throws OdfException
* @return void
*/
private function _save()
{
$this->file->open($this->tmpfile);
$this->_parse();
if (! $this->file->addFromString('content.xml', $this->contentXml) || ! $this->file->addFromString('styles.xml' , $this->stylesXml ) ) {
throw new OdfException('Error during file export addFromString');
}
$lastpos=strrpos($this->manifestXml, "\n", -15); //find second last newline in the manifest.xml file
$manifdata = "";
//Enter all images description in $manifdata variable
foreach ($this->manif_vars as $val)
{
$ext = substr(strrchr($val, '.'), 1);
$manifdata = $manifdata.''."\n";
}
//Place content of $manifdata variable in manifest.xml file at appropriate place
$this->manifestXml = substr_replace($this->manifestXml, "\n".$manifdata, $lastpos+1, 0);
//$this->manifestXml = $this->manifestXml ."\n".$manifdata;
if (! $this->file->addFromString('META-INF/manifest.xml', $this->manifestXml)) {
throw new OdfException('Error during manifest file export');
}
foreach ($this->images as $imageKey => $imageValue) {
$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
}
$this->file->close(); // seems to bug on windows CLI sometimes
}
/**
* Export the file as attached file by HTTP
*
* @param string $name (optionnal)
* @throws OdfException
* @return void
*/
public function exportAsAttachedFile($name="")
{
$this->_save();
if (headers_sent($filename, $linenum)) {
throw new OdfException("headers already sent ($filename at $linenum)");
}
if( $name == "" )
{
$name = md5(uniqid()) . ".odt";
}
header('Content-type: application/vnd.oasis.opendocument.text');
header('Content-Disposition: attachment; filename="'.$name.'"');
readfile($this->tmpfile);
}
/**
* Returns a variable of configuration
*
* @return string The requested variable of configuration
*/
public function getConfig($configKey)
{
if (array_key_exists($configKey, $this->config)) {
return $this->config[$configKey];
}
return false;
}
/**
* Returns the temporary working file
*
* @return string le chemin vers le fichier temporaire de travail
*/
public function getTmpfile()
{
return $this->tmpfile;
}
/**
* Delete the temporary file when the object is destroyed
*/
public function __destruct() {
if (file_exists($this->tmpfile)) {
unlink($this->tmpfile);
}
}
}
?>