1<?php
2/**
3 * DokuWiki Plugin selfmeasurement (Syntax Component)
4 *
5 * @license BSD-3 Clause http://www.gnu.org/licenses/bsd.html
6 * @author  Eric Maeker, MD (fr) <eric.maeker@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16
17require_once DOKU_PLUGIN.'syntax.php';
18
19class syntax_plugin_selfmeasurement extends DokuWiki_Syntax_Plugin {
20
21    public static $lastFormId = 1;
22    private $formId = 0;
23
24    /** Version 1.00 - 26/02/2013 */
25    function getInfo(){
26        return array(
27            'author' => 'Eric Maeker',
28            'email'  => 'eric@maeker.fr',
29            'date'   => '2013-02-26',
30            'name'   => 'selfmeasurement',
31            'desc'   => 'Medical plugin to help users to correctly monitor their blood pressure.',
32            'url'    => 'http://www.dokuwiki.org/wiki:plugins',
33        );
34    }
35
36    public function getType() {
37        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
38        return 'substition';
39    }
40
41    public function getPType() {
42        // return 'FIXME: normal|block|stack';
43        return 'normal';
44    }
45
46    public function getSort() {
47        return 32;
48    }
49
50    /**
51     * Connect DokuWiki syntax:
52     *     {{selfmeasurement}}
53     */
54    public function connectTo($mode) {
55        $this->Lexer->addSpecialPattern('\{\{selfmeasurement\}\}',$mode,'plugin_selfmeasurement');
56    }
57
58    public function handle($match, $state, $pos, &$handler) {
59        $data = array();
60        $match = substr($match,18,-2); //strip markup from start and end
61        // handle params
62        $params = explode('|',$match);
63        foreach($params as $param){
64            $splitparam = explode('=', $param);
65            $data[$splitparam[0]] = $splitparam[1];
66        }
67        return $data;
68    }
69
70    /**
71     * Rendering function. If the rendering contains form values, send the report, otherwise add the form.
72     */
73    public function render($mode, &$renderer, $data) {
74        //if ($mode != 'xhtml') return false;
75
76        if (isset($_POST['submit-form-'.$this->formId])) {
77            $this->_send_measurements($renderer);
78            return true;
79        }
80
81        // include the form
82        $form = file_get_contents(dirname(__FILE__)."/form.html");
83        $form = str_replace("__FORM__", "action=\"".$_SERVER['REQUEST_URI']."#form-".$this->formId."\"f method=\"POST\"", $form);
84        $form = str_replace("__SUBMIT_NAME__", "submit-form-".$this->formId, $form);
85        $renderer->doc .= $form;
86        return true;
87    }
88
89    /**
90     * When the rendering is required with a validated form:
91     * Create the report and send it by mail (according to the conf)
92     */
93    protected function _send_measurements(&$renderer) {
94        $report_html = file_get_contents(dirname(__FILE__)."/report.html");
95        $report_txt  = file_get_contents(dirname(__FILE__)."/report.txt");
96
97        // replace all measurements from the post to the report
98        for($i = 1; $i <= 18; $i++) {
99            $find = "_S" . sprintf("%02s", $i) . "_";
100            $post = "s" . $i;
101            $report_html = str_replace($find, sprintf("%5.5s", $_POST[$post]), $report_html);
102            $report_txt = str_replace($find, sprintf("%5.5s", $_POST[$post]), $report_txt);
103
104            $find = "_D" . sprintf("%02s", $i) . "_";
105            $post = "d" . $i;
106            $report_html = str_replace($find, sprintf("%5.5s", $_POST[$post]), $report_html);
107            $report_txt = str_replace($find, sprintf("%5.5s", $_POST[$post]), $report_txt);
108        }
109
110        // Compute day averages & full measurement averages
111        $fullSystAverage = 0;
112        $fullDiastAverage = 0;
113        $fullSystNb = 0;
114        $fullDiastNb = 0;
115
116        for($i = 0; $i < 3; $i++) {
117            $systAverage = 0;
118            $diastAverage = 0;
119            $systNb = 0;
120            $diastNb = 0;
121
122            for($j = 1; $j <= 6; $j++) {
123                $nb = ($i*6 + $j);
124                $post = "s" . $nb;
125                $systAverage += $_POST[$post];
126                if ($_POST[$post] != '')
127                    $systNb++;
128
129                $post = "d" . $nb;
130                $diastAverage += $_POST[$post];
131                if ($_POST[$post] != '')
132                    $diastNb++;
133            }
134            if ($systNb == 0) {
135                $systAverage = 0;
136            } else {
137                $fullSystAverage += $systAverage;
138                $fullSystNb += $systNb;
139                $systAverage = $systAverage / $systNb;
140            }
141
142            if ($diastNb == 0) {
143                $diasAverage = 0;
144            } else {
145                $fullDiastAverage += $diastAverage;
146                $fullDiastNb += $diastNb;
147                $diastAverage = $diastAverage / $diastNb;
148            }
149
150            $day = ($i+1);
151            $find = "_SYS_AV_DAY" . $day . "_";
152            $report_html = str_replace($find, sprintf("%5.5s", $systAverage), $report_html);
153            $report_txt = str_replace($find, sprintf("%5.5s", $systAverage), $report_txt);
154            $find = "_DIA_AV_DAY" . $day . "_";
155            $report_html = str_replace($find, sprintf("%5.5s", $diastAverage), $report_html);
156            $report_txt = str_replace($find, sprintf("%5.5s", $diastAverage), $report_txt);
157        }
158
159        // Manage full measurements averages
160        if ($fullSystNb == 0) {
161            $fullSystAverage = 0;
162        } else {
163            $fullSystAverage = $fullSystAverage / $fullSystNb;
164        }
165
166        if ($fullDiastNb == 0) {
167            $fullDiastAverage = 0;
168        } else {
169            $fullDiastAverage = $fullDiastAverage / $fullDiastNb;
170        }
171        $find = "_SYS_AV_";
172        $report_html = str_replace($find, sprintf("%8.8s", $fullSystAverage), $report_html);
173        $report_txt = str_replace($find, sprintf("%8.8s", $fullSystAverage), $report_txt);
174        $find = "_DIA_AV_";
175        $report_html = str_replace($find, sprintf("%8.8s", $fullDiastAverage), $report_html);
176        $report_txt = str_replace($find, sprintf("%8.8s", $fullDiastAverage), $report_txt);
177        $find = "_SYS_NB_";
178        $report_html = str_replace($find, sprintf("%8.8s", $fullSystNb), $report_html);
179        $report_txt = str_replace($find, sprintf("%8.8s", $fullSystNb), $report_txt);
180        $find = "_DIA_NB_";
181        $report_html = str_replace($find, sprintf("%8.8s", $fullDiastNb), $report_html);
182        $report_txt = str_replace($find, sprintf("%8.8s", $fullDiastNb), $report_txt);
183
184        // Replace all tokens
185        // __NAMES__ __DATE__
186        $find = "__NAMES__";
187        $report_html = str_replace($find, $_POST['names'], $report_html);
188        $report_txt = str_replace($find, $_POST['names'], $report_txt);
189        $find = "__DATE__";
190        $date = $_POST['date'];
191        if ($date == "")
192            $date = date("Y-m-d", $_SERVER['REQUEST_TIME']);
193        $report_html = str_replace($find, $date, $report_html);
194        $report_txt = str_replace($find, $date, $report_txt);
195
196        // Prepare mail (To, Names, Subject)
197        $lf = "\r\n";  // LineFeed
198        $to  = $this->getConf('automeasurement_sendto');
199        $name = str_replace(" ", "_", $_POST['names']);
200        if ($name == "")
201            $name = "Inconnu";
202        $subject = '[Auto-mesures] - ' . $name;
203        $filename = 'selfmeasurement_' . $name . "_" . $date;
204
205        // Prepare mail (Headers, Attachement)
206        $random_hash = "DokuWiki-SelfMeasurement--" . md5(date('r', time()));
207
208        $headers  = "From: " . $to . $lf;
209        $headers .= "Content-Type: multipart/mixed; boundary=\"".$random_hash."\"" . $lf;
210        $headers .= "Reply-To: " . $to . $lf;
211        $headers .= "Date: " . date('r', $_SERVER['REQUEST_TIME']) . $lf;
212        $headers .= "Mime-Version: 1.0" . $lf;
213        $headers .= $lf;
214
215        // TODO: add style.css in the report_html var (manage the __text__ tokens)
216        $attachment_html = chunk_split(base64_encode($report_html));
217        //$attachment_txt = chunk_split(base64_encode($report_txt));
218
219        // Prepare mail (Body)
220        $output  = "--$random_hash" . $lf;
221        $output .= "Content-Transfer-Encoding: 7bit" . $lf;
222        $output .= "Content-Type: text/plain; charset='utf-8'" . $lf;
223        $output .= $lf;
224        $output .= $report_txt . $lf;
225        $output .= $lf;
226        $output .= "--$random_hash" . $lf;
227        $output .= "Content-Disposition: attachment" . $lf;
228        $output .= "Content-Type: text/html; name=$filename.html" . $lf;
229        $output .= "Content-Transfer-Encoding: base64" . $lf;
230        $output .= $lf;
231        $output .= $attachment_html;
232        $output .= $lf;
233        $output .= "--$random_hash" . $lf;
234        $output .= "Content-Disposition: attachment" . $lf;
235        $output .= "Content-Type: text/plain; charset='utf-8'; name=$filename.txt" . $lf;
236        $output .= "Content-Transfer-Encoding: quoted-printable" . $lf;
237        $output .= $lf;
238        $output .= $report_txt; //$attachment_txt;
239        $output .= "--$random_hash--" . $lf;
240
241        if (mail($to, $subject, $output, $headers)) {
242            $renderer->doc .= "<h1>Les r&eacute;sultats ont &eacute;t&eacute; transmis. Inutile d'imprimer la page</h1>"; // TODO: use a conf/lang instead of magic number
243        } else {
244            $renderer->doc .= "<h1>Erreur: Les r&eacute;sultats n'ont pas &eacute;t&eacute; transmis. Veuillez imprimer la page.</h1>"; // TODO: use a conf/lang instead of magic number
245        }
246
247        $renderer->doc .= $report_html;
248
249        return true;
250    }
251}
252
253// vim:ts=4:sw=4:et:
254