3
* © Copyright 2010 IntraHealth International, Inc.
5
* This File is part of I2CE
7
* I2CE is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
* @author Carl Leitner <litlfred@ibiblio.org>
27
* Class I2CE_PrintedForm_Render
33
class I2CE_PrintedForm_Render_ODT extends I2CE_PrintedForm_Render{
35
protected function processElement_text($left_x,$top_y,$formData,$textProps, $elementConfig) {
39
protected function processElement_image($left_x,$top_y,$formData,$textProps, $elementConfig) {
43
protected function addPage($textProps) {
49
*Abstract method to render the form. Makes sure all ducks are in a row
50
* @returns boolean true on sucess.
52
public function render() {
53
if (!is_string($this->std_form) || strlen($this->std_form) == 0) {
54
I2CE::raiseError("No standard printed form set");
57
$this->stdConfig = I2CE::getConfig()->traverse( '/modules/PrintedForms/forms/' . $this->std_form, false);
58
if (!$this->stdConfig instanceof I2CE_MagicDataNode) {
59
I2CE::raiseError("No standard printed form /modules/PrintedForms/forms/" . $this->std_form);;
62
if (! $this->stdConfig->setIfIsSet($relationship, 'relationship' )) {
63
I2CE::raiseError("No relationship set");
67
$this->rel = new I2CE_FormRelationship($relationship, $this->base_rel_config);
68
} catch (Exception $e) {
69
I2CE::raiseError("Could not instatiate relationship $relationship");
73
if (! $this->stdConfig->setIfIsSet($template, 'template' )) {
74
I2CE::raiseError("No template set");
77
$this->template_file = I2CE::getFileSearch()->search('ODT_TEMPLATES',$template);
78
if (!$this->template_file) {
79
I2CE::raiseError("No template file found from $template");
82
$this->template_contents = new ZipArchive();
83
if ($this->template_contents->open($this->template_file)!==TRUE) {
84
I2CE::raiseError("Could not extract odt file");
87
if ( ($this->mimetype = $this->template_contents->getFromName('mimetype')) === false) {
88
I2CE::raiseError("Could not determine mime type");
90
I2CE::raiseError("mime type " . $this->mimetype);
92
$this->content = $this->stdConfig->getAsArray('content');
94
foreach ($this->ids as $id) {
95
if (!is_string($id)) {
98
$fs = $this->rel->getFormsSatisfyingRelationship($id);
99
if (!is_array($fs) || count($fs) == 0) {
104
if (count($forms) == 0) {
105
I2CE::raiseError("No valid forms");
109
$this->forms = $forms;
110
$textProps = array();
111
I2CE::longExecution( );
112
$success = $this->_render($textProps);
113
$this->template_contents->close();
118
* @var protected string $template_file. The absolute file location of the template we are reading from
120
protected $template_file = false;
123
* @var protected string $template_contnets. The contents of the template file
125
protected $template_contents = false;
128
* @var protected array $output. Indedx by id's of the output files
130
protected $output = array();
132
protected $formData = array();
135
*business method to render the forms
136
* @param array $textProps
137
* @returns boolean true on sucess.
139
protected function _render($textProps) {
140
$this->output = array();
142
foreach ($this->forms as $id=>$formData) {
143
$this->setCurrentId($id);
144
$this->formData = $formData;
145
if (! ($file = $this->processFile())) {
146
I2CE::raiseError("Could not add form: $id");
149
$this->output[$id] = $file;
157
protected function processFile() {
158
$temp = tempnam('/tmp', 'zip');
159
$zip = new ZipArchive();
160
if (($zip->open($temp,ZipArchive::CREATE | ZipArchive::OVERWRITE)) === false) {
161
I2CE::raiseError("Could not create $temp");
165
for ($i=0; $i<$this->template_contents->numFiles;$i++) {
166
$stats = $this->template_contents->statIndex($i);
167
$contents = $this->template_contents->getFromIndex($i);
168
if ($stats['name'] == 'content.xml') {
169
$contents =$this->processContents($contents);
171
$zip->addFromString($stats['name'],$contents);
174
return file_get_contents($temp);
179
protected function replacement($matches) {
180
$replacement = $matches[0]; //default to the whole string i.e. we are not reaplcing.
181
$original = $matches[1];
182
if (substr($original,0,2) == '++') {
183
//it is a special variable
184
$spec = strtolower(substr($original,2,4));
186
if (preg_match('/^\(\s*(.*?)\s*\)$/', substr($original,6), $matches)) {
191
if (strlen($arg) == 0) {
194
$replacement = @strftime($arg);
197
if (!$this->user instanceof I2CE_User) {
198
$this->user = new I2CE_User();
200
$replacement = $user->firstname . ' ' . $user->lastname;
203
@eval('$replacement = ' . $arg . ';');
210
} else if (substr($original,0,1) == '+') {
211
//it is a relationship function
212
$replacement = $this->rel->evaluateFunction(substr($original,1),$this->formData);
215
list($namedform,$field) = array_pad(explode("+",$original,2),2,'');
216
$namedform = trim($namedform);
217
$field = trim($field);
218
if ($namedform == $this->stdConfig->relationship) {
219
$namedform = 'primary_form';
221
if ($namedform && $field && array_key_exists($namedform,$this->formData) && ($this->formData[$namedform] instanceof I2CE_Form)) {
222
$fieldObj = $this->formData[$namedform]->getField($field);
223
if ($fieldObj instanceof I2CE_FormField) {
224
$replacement = $fieldObj->getDisplayValue();
231
protected function processContents($contents) {
232
return preg_replace_callback('/{{{([0-9a-zA-Z_\-\+]+)}}}/',array($this,'replacement'),$contents);
237
*Abstract method to retreive/display the contents of the rendered forms
238
* @param boolean $as_string. Defaults to false
239
* @returns mixed. If {$as_string} is false the it is a boolean true on sucess. If $as_string is true, then it is a string on success, false on failure
241
public function display($as_string = false) {
242
while (ob_get_level() > 0) {
243
if($cont = trim(ob_get_clean())) {
244
I2CE::raiseError("(".$cont .')');
248
if (count($this->output) == 0) {
254
} else if (count($this->output) == 1) {
256
//we don't want mime type. this goes to the archive
257
reset($this->output);
258
return current($this->output);
261
//we do want mime type. this goes to the browser
262
reset($this->output);
263
header($this->mimetype);
264
$expires = 60*5; //5 minute expiration
266
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
267
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
268
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
269
header('Content-Length: '.strlen(current($this->output)));
270
header('Content-Type: ' . $this->mimetype);
271
header('Content-disposition: attachment; filename="'. addslashes($this->getFileName()) .'"');
272
echo current($this->output);
273
exit; // we want to make sure there is no further output or that the $this->page->display() method is not called
276
//do it as a zip file
277
I2CE::raiseError("Zip archive not supported yet");
282
public function getMimeType() {
283
return $this->mimetype;
286
public function getFileName() {
287
return basename($this->template_file);
295
# c-default-style: "bsd"
296
# indent-tabs-mode: nil