.inc files migration to .inc.php (#5897)
[openemr.git] / contrib / forms / xmlformgen / xslt / save.php.xslt
blob5b48658ee752103ac5e3ed48df67deff26cc577b
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 require_once($GLOBALS['srcdir'].'/api.inc.php');
32 /* for ??? */
33 require_once($GLOBALS['srcdir'].'/forms.inc.php');
35 use OpenEMR\Common\Acl\AclMain;
37 ]]></xsl:text>
38 <xsl:apply-templates select="table|RealName|safename|acl|layout"/>
39 <xsl:text disable-output-escaping="yes"><![CDATA[
40 /* an array of all of the fields' names and their types. */
41 $field_names = array(]]></xsl:text>
42 <xsl:for-each select="//field">
43 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
44 <xsl:value-of select="@name" />
45 <xsl:text disable-output-escaping="yes"><![CDATA[' => ']]></xsl:text>
46 <xsl:value-of select="@type" />
47 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
48 <xsl:if test="position()!=last()">,</xsl:if>
49 </xsl:for-each>
50 <xsl:text disable-output-escaping="yes"><![CDATA[);
51 /* an array of the lists the fields may draw on. */
52 $lists = array(]]></xsl:text>
53 <xsl:for-each select="//field[@type='checkbox_list' or @type='checkbox_combo_list' or @type='dropdown_list']">
54 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
55 <xsl:value-of select="@name" />
56 <xsl:if test="@type='dropdown_list' or @type='checkbox_list' or @type='checkbox_combo_list'">
57 <xsl:text disable-output-escaping="yes"><![CDATA[' => ']]></xsl:text>
58 <xsl:variable name="i" select="@list"/>
59 <xsl:value-of select="//list[@name=$i]/@id"/>
60 <xsl:text disable-output-escaping="yes"><![CDATA[']]></xsl:text>
61 <xsl:if test="position()!=last()">, </xsl:if>
62 </xsl:if>
63 </xsl:for-each>
64 <xsl:text disable-output-escaping="yes"><![CDATA[);
66 /* get each field from $_POST[], storing them into $field_names associated with their names. */
67 foreach($field_names as $key=>$val)
69 $pos = '';
70 $neg = '';
71 if ($val == 'textbox' || $val == 'textarea' || $val == 'provider' || $val == 'textfield')
73 $field_names[$key]=$_POST['form_'.$key];
75 if ($val == 'date')
77 $field_names[$key]=$_POST[$key];
79 if (($val == 'checkbox_list' ))
81 $field_names[$key]='';
82 if (isset($_POST['form_'.$key]) && $_POST['form_'.$key] != 'none' ) /* if the form submitted some entries selected in that field */
84 $lres=sqlStatement("select * from list_options where list_id = '".$lists[$key]."' ORDER BY seq, title");
85 while ($lrow = sqlFetchArray($lres))
87 if (is_array($_POST['form_'.$key]))
89 if ($_POST['form_'.$key][$lrow['option_id']])
91 if ($field_names[$key] != '')
92 $field_names[$key]=$field_names[$key].'|';
93 $field_names[$key] = $field_names[$key].$lrow['option_id'];
99 if (($val == 'checkbox_combo_list'))
101 $field_names[$key]='';
102 if (isset($_POST['check_'.$key]) && $_POST['check_'.$key] != 'none' ) /* if the form submitted some entries selected in that field */
104 $lres=sqlStatement("select * from list_options where list_id = '".$lists[$key]."' ORDER BY seq, title");
105 while ($lrow = sqlFetchArray($lres))
107 if (is_array($_POST['check_'.$key]))
109 if ($_POST['check_'.$key][$lrow['option_id']])
111 if ($field_names[$key] != '')
112 $field_names[$key]=$field_names[$key].'|';
113 $field_names[$key] = $field_names[$key].$lrow['option_id'].":xx".$_POST['form_'.$key][$lrow['option_id']];
119 if (($val == 'dropdown_list'))
121 $field_names[$key]='';
122 if (isset($_POST['form_'.$key]) && $_POST['form_'.$key] != 'none' ) /* if the form submitted some entries selected in that field */
124 $lres=sqlStatement("select * from list_options where list_id = '".$lists[$key]."' ORDER BY seq, title");
125 while ($lrow = sqlFetchArray($lres))
127 if ($_POST['form_'.$key] == $lrow['option_id'])
129 $field_names[$key]=$lrow['option_id'];
130 break;
137 /* at this point, field_names[] contains an array of name->value pairs of the fields we expected from the form. */
139 /* escape form data for entry to the database. */
140 foreach ($field_names as $k => $var) {
141 $field_names[$k] = add_escape_custom($var);
144 if ($encounter == '') $encounter = date('Ymd');
146 if ($_GET['mode'] == 'new') {
147 /* NOTE - for customization you can replace $_POST with your own array
148 * of key=>value pairs where 'key' is the table field name and
149 * 'value' is whatever it should be set to
150 * ex) $newrecord['parent_sig'] = $_POST['sig'];
151 * $newid = formSubmit($table_name, $newrecord, $_GET['id'], $userauthorized);
154 /* make sure we're at the beginning of the array */
155 reset($field_names);
157 ]]></xsl:text>
158 <xsl:if test="//table[@type='form']">
159 <xsl:text disable-output-escaping="yes"><![CDATA[ /* save the data into the form's encounter-based table */
160 $newid = formSubmit($table_name, $field_names, $_GET['id'], $userauthorized);
161 ]]></xsl:text>
162 </xsl:if>
163 <xsl:if test="//table[@type='extended']">
164 <xsl:text disable-output-escaping="yes"><![CDATA[ /* save the data into the form's table */
165 /* construct our sql statement */
166 $sql= 'insert into '.$table_name." set date = NOW(), pid = '".$_SESSION['pid']."',";
167 foreach ($field_names as $k => $var) {
168 $sql .= " $k = '$var',";
171 /* remove the last comma */
172 $sql = substr($sql, 0, -1);
174 /* insert into the table */
175 $newid=sqlInsert($sql);
177 if ($id!='') /* if we're passed an ID, update the old form_id to point to a new one. */
179 $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."'";
180 echo $sql;
181 sqlStatement($sql);
183 else
184 ]]></xsl:text>
185 </xsl:if>
186 <xsl:text disable-output-escaping="yes"><![CDATA[ /* link this form into the encounter. */
187 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
189 ]]></xsl:text>
190 <xsl:if test="//table[@type='extended']">
191 <xsl:text disable-output-escaping="yes"><![CDATA[
192 elseif ($_GET['mode'] == 'update') {
193 /* make sure we're at the beginning of the array */
194 reset($field_names);
196 /* save the data into the form's table */
197 /* construct our sql statement */
198 $sql= 'insert into '.$table_name." set date = NOW(), pid = '".$_SESSION['pid']."',";
199 foreach ($field_names as $k => $var) {
200 $sql .= " $k = '$var',";
203 /* remove the last comma */
204 $sql = substr($sql, 0, -1);
206 /* insert into the table */
207 $newid=sqlInsert($sql);
209 if ($_GET['return'] == 'encounter') {
210 /* link this form into the encounter. */
211 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
214 ]]></xsl:text>
215 </xsl:if>
216 <xsl:if test="//table[@type='form']">
217 <xsl:text disable-output-escaping="yes"><![CDATA[
218 elseif ($_GET['mode'] == 'update') {
219 /* make sure we're at the beginning of the array */
220 reset($field_names);
222 /* update the data in the form's table */
223 $success = formUpdate($table_name, $field_names, $_GET['id'], $userauthorized);
224 /* sqlStatement('update '.$table_name." set pid = {".$_SESSION['pid']."},groupname='".$_SESSION['authProvider']."',user='".$_SESSION['authUser']."',authorized=$userauthorized,activity=1,date = NOW(), where id=$id"); */
226 ]]></xsl:text>
227 </xsl:if>
228 <xsl:text disable-output-escaping="yes"><![CDATA[
230 formHeader('Redirecting....');
231 ]]></xsl:text>
232 <xsl:if test="//table[@type='extended']">
233 <xsl:text disable-output-escaping="yes"><![CDATA[
234 if ($_GET['return'] == 'show') {
235 formJump("{$GLOBALS['rootdir']}/forms/".$form_folder.'/show.php');
236 ]]></xsl:text>
237 <xsl:text disable-output-escaping="yes"><![CDATA[}
238 else
240 ]]></xsl:text>
241 </xsl:if>
242 <xsl:if test="//table[@type='form' or @type='extended']">
243 <xsl:text disable-output-escaping="yes"><![CDATA[/* defaults to the encounters page. */
244 formJump();
245 ]]></xsl:text>
246 </xsl:if>
247 <xsl:if test="//table[@type='extended']">
248 <xsl:text disable-output-escaping="yes"><![CDATA[}
249 ]]></xsl:text>
250 </xsl:if>
251 <xsl:text disable-output-escaping="yes"><![CDATA[
252 formFooter();
254 ]]></xsl:text>
255 </xsl:template>
256 </xsl:stylesheet>