xml form generator (version from 1/28/2011)
[openemr.git] / contrib / forms / xmlformgen / xslt / save.php.xslt
blobb6c890e6ca3e2fbc292ea90d863680224d8f8dc5
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!-- Generated by Hand -->
3 <!--
4 Copyright (C) 2011 Julia Longtin <julia.longtin@gmail.com>
6 This program is free software; you can redistribute it and/or
7 Modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 -->
11 <xsl:stylesheet version="1.0"
12 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
13 <xsl:output method="xml" omit-xml-declaration="yes"/>
14 <xsl:include href="common_objects.xslt"/>
15 <xsl:strip-space elements="*"/>
16 <xsl:template match="/">
17 <xsl:apply-templates select="form"/>
18 </xsl:template>
19 <!-- The variable telling field_objects.xslt what form is calling it -->
20 <xsl:variable name="page">save</xsl:variable>
21 <!-- if fetchrow has contents, a variable with that name will be created by field_objects.xslt, and all fields created by it will retreive values from it. -->
22 <xsl:variable name="fetchrow">xyzzy</xsl:variable>
23 <xsl:template match="form">
24 <xsl:text disable-output-escaping="yes"><![CDATA[<?php
25 /* this page is intended to be the 'action=' target of a form object.
26 * it is called to save the contents of the form into the database
29 /* for $GLOBALS[], ?? */
30 require_once('../../globals.php');
31 /* for acl_check(), ?? */
32 require_once($GLOBALS['srcdir'].'/api.inc');
33 /* for ??? */
34 require_once($GLOBALS['srcdir'].'/forms.inc');
35 /* for formDataCore() */
36 require_once($GLOBALS['srcdir'].'/formdata.inc.php');
38 ]]></xsl:text>
39 <xsl:apply-templates select="table|RealName|safename|acl|layout"/>
40 <xsl:text disable-output-escaping="yes"><![CDATA[
41 /* an array of all of the fields' names and their types. */
42 $field_names = array(]]></xsl:text>
43 <xsl:for-each select="//field">
44 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
45 <xsl:value-of select="@name" />
46 <xsl:text disable-output-escaping="yes"><![CDATA[' => ']]></xsl:text>
47 <xsl:value-of select="@type" />
48 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
49 <xsl:if test="position()!=last()">,</xsl:if>
50 </xsl:for-each>
51 <xsl:text disable-output-escaping="yes"><![CDATA[);
52 /* an array of the lists the fields may draw on. */
53 $lists = array(]]></xsl:text>
54 <xsl:for-each select="//field[@type='checkbox_list' or @type='dropdown_list']">
55 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
56 <xsl:value-of select="@name" />
57 <xsl:if test="@type='dropdown_list' or @type='checkbox_list'">
58 <xsl:text disable-output-escaping="yes"><![CDATA[' => ']]></xsl:text>
59 <xsl:variable name="i" select="@list"/>
60 <xsl:value-of select="//list[@name=$i]/@id"/>
61 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
62 <xsl:if test="position()!=last()">, </xsl:if>
63 </xsl:if>
64 </xsl:for-each>
65 <xsl:text disable-output-escaping="yes"><![CDATA[);
67 /* get each field from $_POST[], storing them into $field_names associated with their names. */
68 foreach($field_names as $key=>$val)
70 $pos = '';
71 $neg = '';
72 if ($val == 'textbox' || $val == 'textarea' || $val == 'provider' || $val == 'textfield')
74 $field_names[$key]=$_POST['form_'.$key];
76 if ($val == 'date')
78 $field_names[$key]=$_POST[$key];
80 if (($val == 'checkbox_list'))
82 $field_names[$key]='';
83 if (isset($_POST['form_'.$key]) && $_POST['form_'.$key] != 'none' ) /* if the form submitted some entries selected in that field */
85 $lres=sqlStatement("select * from list_options where list_id = '".$lists[$key]."' ORDER BY seq, title");
86 while ($lrow = sqlFetchArray($lres))
88 if (is_array($_POST['form_'.$key]))
90 if ($_POST['form_'.$key][$lrow[option_id]])
92 if ($field_names[$key] != '')
93 $field_names[$key]=$field_names[$key].'|';
94 $field_names[$key] = $field_names[$key].$lrow[option_id];
100 if (($val == 'dropdown_list'))
102 $field_names[$key]='';
103 if (isset($_POST['form_'.$key]) && $_POST['form_'.$key] != 'none' ) /* if the form submitted some entries selected in that field */
105 $lres=sqlStatement("select * from list_options where list_id = '".$lists[$key]."' ORDER BY seq, title");
106 while ($lrow = sqlFetchArray($lres))
108 if ($_POST['form_'.$key] == $lrow[option_id])
110 $field_names[$key]=$lrow[option_id];
111 break;
118 /* at this point, field_names[] contains an array of name->value pairs of the fields we expected from the form. */
120 /* escape form data for entry to the database. */
121 foreach ($field_names as $k => $var) {
122 $field_names[$k] = formDataCore($var);
125 if ($encounter == '') $encounter = date('Ymd');
127 if ($_GET['mode'] == 'new') {
128 /* NOTE - for customization you can replace $_POST with your own array
129 * of key=>value pairs where 'key' is the table field name and
130 * 'value' is whatever it should be set to
131 * ex) $newrecord['parent_sig'] = $_POST['sig'];
132 * $newid = formSubmit($table_name, $newrecord, $_GET['id'], $userauthorized);
135 /* make sure we're at the beginning of the array */
136 reset($field_names);
138 ]]></xsl:text>
139 <xsl:if test="//table[@type='form']">
140 <xsl:text disable-output-escaping="yes"><![CDATA[ /* save the data into the form's encounter-based table */
141 $newid = formSubmit($table_name, $field_names, $_GET['id'], $userauthorized);
142 ]]></xsl:text>
143 </xsl:if>
144 <xsl:if test="//table[@type='extended']">
145 <xsl:text disable-output-escaping="yes"><![CDATA[ /* save the data into the form's table */
146 /* construct our sql statement */
147 $sql= 'insert into '.$table_name." set date = NOW(), pid = '".$_SESSION['pid']."',";
148 foreach ($field_names as $k => $var) {
149 $sql .= " $k = '$var',";
152 /* remove the last comma */
153 $sql = substr($sql, 0, -1);
155 /* insert into the table */
156 $newid=sqlInsert($sql);
158 if ($id!='') /* if we're passed an ID, update the old form_id to point to a new one. */
160 $sql= "update forms set date = NOW(), encounter='".$encounter."', form_name='".$form_name."', form_id='".$newid."', pid='".$pid."', user='".$_SESSION['authUser']."', groupname='".$_SESSION['authProvider']."', authorized='".$userauthorized."', formdir='".$form_folder."' where form_name='".$form_name."' and encounter='".$encounter."' and pid='".$pid."' and form_id='".$id."'";
161 echo $sql;
162 sqlStatement($sql);
164 else
165 ]]></xsl:text>
166 </xsl:if>
167 <xsl:text disable-output-escaping="yes"><![CDATA[ /* link this form into the encounter. */
168 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
170 ]]></xsl:text>
171 <xsl:if test="//table[@type='extended']">
172 <xsl:text disable-output-escaping="yes"><![CDATA[
173 elseif ($_GET['mode'] == 'update') {
174 /* make sure we're at the beginning of the array */
175 reset($field_names);
177 /* save the data into the form's table */
178 /* construct our sql statement */
179 $sql= 'insert into '.$table_name." set date = NOW(), pid = '".$_SESSION['pid']."',";
180 foreach ($field_names as $k => $var) {
181 $sql .= " $k = '$var',";
184 /* remove the last comma */
185 $sql = substr($sql, 0, -1);
187 /* insert into the table */
188 $newid=sqlInsert($sql);
190 if ($_GET['return'] == 'encounter') {
191 /* link this form into the encounter. */
192 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
195 ]]></xsl:text>
196 </xsl:if>
197 <xsl:if test="//table[@type='form']">
198 <xsl:text disable-output-escaping="yes"><![CDATA[
199 elseif ($_GET['mode'] == 'update') {
200 /* make sure we're at the beginning of the array */
201 reset($field_names);
203 /* update the data in the form's table */
204 $success = formUpdate($table_name, $field_names, $_GET['id'], $userauthorized);
205 /* sqlInsert('update '.$table_name." set pid = {".$_SESSION['pid']."},groupname='".$_SESSION['authProvider']."',user='".$_SESSION['authUser']."',authorized=$userauthorized,activity=1,date = NOW(), where id=$id"); */
207 ]]></xsl:text>
208 </xsl:if>
209 <xsl:text disable-output-escaping="yes"><![CDATA[
211 $_SESSION['encounter'] = $encounter;
213 formHeader('Redirecting....');
214 ]]></xsl:text>
215 <xsl:if test="//table[@type='extended']">
216 <xsl:text disable-output-escaping="yes"><![CDATA[
217 if ($_GET['return'] == 'show') {
218 formJump("{$GLOBALS['rootdir']}/forms/".$form_folder.'/show.php');
219 ]]></xsl:text>
220 <xsl:text disable-output-escaping="yes"><![CDATA[}
221 else
223 ]]></xsl:text>
224 </xsl:if>
225 <xsl:if test="//table[@type='form' or @type='extended']">
226 <xsl:text disable-output-escaping="yes"><![CDATA[/* defaults to the encounters page. */
227 formJump();
228 ]]></xsl:text>
229 </xsl:if>
230 <xsl:if test="//table[@type='extended']">
231 <xsl:text disable-output-escaping="yes"><![CDATA[}
232 ]]></xsl:text>
233 </xsl:if>
234 <xsl:text disable-output-escaping="yes"><![CDATA[
235 formFooter();
237 ]]></xsl:text>
238 </xsl:template>
239 </xsl:stylesheet>