~ihris+botswana/ihris-botswana/trunk

« back to all changes in this revision

Viewing changes to modules/PrintedFormODT/lib/I2CE_PrintedForm_Render_ODT.php

  • Committer: Carl Leitner
  • Date: 2011-08-02 14:03:55 UTC
  • Revision ID: litlfred@ibiblio.org-20110802140355-iqxqwu3c5ngs2t9x
removed printed forms modules that are now in the main codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
* © Copyright 2010 IntraHealth International, Inc.
4
 
5
 
* This File is part of I2CE 
6
 
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.
11
 
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.
16
 
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/>.
19
 
* @package I2CE
20
 
* @subpackage I2CE
21
 
* @author Carl Leitner <litlfred@ibiblio.org>
22
 
* @version v4.0.4
23
 
* @since v4.0.4
24
 
* @filesource 
25
 
*/ 
26
 
/** 
27
 
* Class I2CE_PrintedForm_Render
28
 
29
 
* @access public
30
 
*/
31
 
 
32
 
 
33
 
class I2CE_PrintedForm_Render_ODT extends I2CE_PrintedForm_Render{
34
 
 
35
 
    protected function processElement_text($left_x,$top_y,$formData,$textProps, $elementConfig) {
36
 
        
37
 
    }
38
 
 
39
 
    protected function processElement_image($left_x,$top_y,$formData,$textProps, $elementConfig) {
40
 
 
41
 
    }
42
 
 
43
 
    protected function addPage($textProps)  {
44
 
 
45
 
    }
46
 
 
47
 
 
48
 
    /**
49
 
     *Abstract method to render the form. Makes sure all ducks are in a row
50
 
     * @returns boolean true on sucess.
51
 
     */
52
 
    public function render() {
53
 
        if (!is_string($this->std_form) || strlen($this->std_form) == 0) {
54
 
            I2CE::raiseError("No standard printed form set");
55
 
            return false;
56
 
        }
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);;
60
 
            return false;
61
 
        }
62
 
        if (!  $this->stdConfig->setIfIsSet($relationship, 'relationship' )) {
63
 
            I2CE::raiseError("No relationship set");
64
 
            return false;
65
 
        }
66
 
        try {
67
 
            $this->rel = new I2CE_FormRelationship($relationship, $this->base_rel_config);
68
 
        } catch (Exception $e) {
69
 
            I2CE::raiseError("Could not instatiate relationship $relationship");
70
 
            return false;
71
 
        } 
72
 
        $template = false;
73
 
        if (!  $this->stdConfig->setIfIsSet($template, 'template' )) {
74
 
            I2CE::raiseError("No template  set");
75
 
            return false;
76
 
        }
77
 
        $this->template_file = I2CE::getFileSearch()->search('ODT_TEMPLATES',$template);
78
 
        if (!$this->template_file) {
79
 
            I2CE::raiseError("No template file found from $template");
80
 
            return false;
81
 
        }
82
 
        $this->template_contents = new ZipArchive();
83
 
        if ($this->template_contents->open($this->template_file)!==TRUE) {
84
 
            I2CE::raiseError("Could not extract odt file");
85
 
            return;
86
 
        }
87
 
        if ( ($this->mimetype = $this->template_contents->getFromName('mimetype')) === false) {
88
 
            I2CE::raiseError("Could not determine mime type");
89
 
        } else {
90
 
            I2CE::raiseError("mime type " . $this->mimetype);
91
 
        }
92
 
        $this->content = $this->stdConfig->getAsArray('content');
93
 
        $forms = array();
94
 
        foreach ($this->ids as $id) {
95
 
            if (!is_string($id)) {
96
 
                continue;
97
 
            }
98
 
            $fs = $this->rel->getFormsSatisfyingRelationship($id);
99
 
            if (!is_array($fs) || count($fs) == 0) {
100
 
                continue;
101
 
            }
102
 
            $forms[$id] = $fs;
103
 
        }
104
 
        if (count($forms) == 0) {
105
 
            I2CE::raiseError("No valid forms");
106
 
            return false;
107
 
        }
108
 
 
109
 
        $this->forms = $forms;
110
 
        $textProps = array();
111
 
        I2CE::longExecution(  );
112
 
        $success = $this->_render($textProps);
113
 
        $this->template_contents->close();
114
 
        return $success;
115
 
    }
116
 
 
117
 
    /**
118
 
     * @var protected string $template_file. The absolute file location of the template we are reading from
119
 
     */
120
 
    protected $template_file = false;
121
 
 
122
 
    /**
123
 
     * @var protected string $template_contnets. The contents of the template file
124
 
     */
125
 
    protected $template_contents = false;
126
 
 
127
 
    /**
128
 
     * @var protected array $output. Indedx by id's of the output files
129
 
     */
130
 
    protected $output = array();
131
 
 
132
 
    protected $formData = array();
133
 
 
134
 
    /**
135
 
     *business  method to render the forms
136
 
     * @param array $textProps
137
 
     * @returns boolean true on sucess.
138
 
     */
139
 
    protected function _render($textProps) {
140
 
        $this->output = array();
141
 
        $counter = 0;
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");
147
 
                return false;
148
 
            }
149
 
            $this->output[$id] = $file;
150
 
            $counter++;
151
 
        }
152
 
        return true;        
153
 
    }
154
 
 
155
 
 
156
 
 
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");
162
 
            return false;
163
 
        }
164
 
 
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);
170
 
            }
171
 
            $zip->addFromString($stats['name'],$contents);
172
 
        }
173
 
        $zip->close();
174
 
        return file_get_contents($temp);
175
 
    }
176
 
 
177
 
    protected $user;
178
 
 
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));
185
 
            $arg = '';
186
 
            if (preg_match('/^\(\s*(.*?)\s*\)$/', substr($original,6), $matches)) {
187
 
                $arg = $matches[1];
188
 
            }
189
 
            switch ($spec) {
190
 
            case 'date':
191
 
                if (strlen($arg) == 0) {
192
 
                    $arg = '%x';
193
 
                }
194
 
                $replacement = @strftime($arg);
195
 
                break;
196
 
            case 'user':
197
 
                if (!$this->user instanceof I2CE_User) {
198
 
                    $this->user = new I2CE_User();
199
 
                }
200
 
                $replacement = $user->firstname . ' ' . $user->lastname;
201
 
                break;
202
 
            case 'eval':
203
 
                @eval('$replacement = ' . $arg . ';');
204
 
                break;
205
 
            default:
206
 
                //do nothing
207
 
                break;
208
 
 
209
 
            }
210
 
        } else if (substr($original,0,1) == '+') {
211
 
            //it is a relationship function
212
 
            $replacement  = $this->rel->evaluateFunction(substr($original,1),$this->formData);
213
 
        } else {
214
 
            //it is a form+field
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';
220
 
            }            
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();
225
 
                }
226
 
            }
227
 
        }
228
 
        return $replacement;
229
 
    }
230
 
 
231
 
    protected function processContents($contents) {
232
 
        return preg_replace_callback('/{{{([0-9a-zA-Z_\-\+]+)}}}/',array($this,'replacement'),$contents);
233
 
    }
234
 
    
235
 
 
236
 
    /**
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
240
 
     */
241
 
    public function display($as_string = false) {
242
 
        while (ob_get_level()  > 0) {
243
 
            if($cont = trim(ob_get_clean())) { 
244
 
                I2CE::raiseError("(".$cont .')');
245
 
            }
246
 
        }
247
 
        
248
 
        if (count($this->output) == 0) {
249
 
            if ($as_string) {
250
 
                return '';
251
 
            } else {
252
 
                return true;
253
 
            }
254
 
        } else if (count($this->output) == 1) {
255
 
            if ($as_string) {
256
 
                //we don't want mime type.  this goes to the archive
257
 
                reset($this->output);
258
 
                return current($this->output);
259
 
                exit;
260
 
            } else {
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
265
 
                $expires =0;
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
274
 
            }
275
 
        } else {
276
 
            //do it as a zip file
277
 
            I2CE::raiseError("Zip archive not supported yet");
278
 
            return false;
279
 
        }
280
 
    }
281
 
 
282
 
    public  function getMimeType() {
283
 
        return $this->mimetype;
284
 
    }
285
 
 
286
 
    public  function getFileName() {
287
 
        return basename($this->template_file);
288
 
    }
289
 
 
290
 
 
291
 
    
292
 
  }
293
 
# Local Variables:
294
 
# mode: php
295
 
# c-default-style: "bsd"
296
 
# indent-tabs-mode: nil
297
 
# c-basic-offset: 4
298
 
# End: