ESign non-standard tables (#359)
[openemr.git] / library / ESign / Form / Signable.php
blob26564636dbaedba22fe63ae667822311e2c86c2c
1 <?php
3 namespace ESign;
5 /**
6 * Form implementation of SignableIF interface, which represents an
7 * object that can be signed, locked and/or amended.
8 *
9 * Copyright (C) 2013 OEMR 501c3 www.oemr.org
11 * LICENSE: This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
22 * @package OpenEMR
23 * @author Ken Chapple <ken@mi-squared.com>
24 * @author Medical Information Integration, LLC
25 * @link http://www.open-emr.org
26 **/
28 require_once $GLOBALS['srcdir'].'/ESign/DbRow/Signable.php';
29 require_once $GLOBALS['srcdir'].'/ESign/SignableIF.php';
30 require_once $GLOBALS['srcdir'].'/formdata.inc.php';
32 class Form_Signable extends DbRow_Signable implements SignableIF
34 protected $_encounterId = null;
35 protected $_formId = null;
36 protected $_formDir = null;
38 public function __construct( $formId, $formDir, $encounterId )
40 $this->_formId = $formId;
41 $this->_formDir = $formDir;
42 $this->_encounterId = $encounterId;
43 parent::__construct( $formId, 'forms' );
46 protected function getLastLockHash()
48 $hash = null;
49 if ( $this->isLocked() ) {
50 // Check to see if there was an explicit lock hash
51 $hash = parent::getLastLockHash();
53 // If there was no explicit lock hash, then we must have been locked because
54 // our encounter was locked, so get our last hash
55 if ( $hash === null ) {
56 $statement = "SELECT E.tid, E.table, E.hash FROM esign_signatures E ";
57 $statement .= "WHERE E.tid = ? AND E.table = ? ";
58 $statement .= "ORDER BY E.datetime DESC LIMIT 1";
59 $row = sqlQuery( $statement, array( $this->_tableId, $this->_tableName ) );
60 $hash = null;
61 if ( $row && isset($row['hash']) ) {
62 $hash = $row['hash'];
67 return $hash;
70 /**
71 * Check to see if this table is locked (read-only)
73 * A form is locked if it has a lock entry in the esign_signatures
74 * table, or if it's encounter is locked.
76 * @see \ESign\DbRow_Signable::isLocked()
78 public function isLocked()
80 // Initialize to false and check individual form
81 $locked = false;
82 if ( $GLOBALS['lock_esign_individual'] ) {
83 $locked = parent::isLocked();
86 // Check the "parent" encounter if signing is allowed at encounter level
87 if ( !$locked && $GLOBALS['lock_esign_all'] ) {
88 $statement = "SELECT E.is_lock FROM esign_signatures E ";
89 $statement .= "WHERE E.tid = ? AND E.table = ? AND E.is_lock = ? ";
90 $statement .= "ORDER BY E.datetime DESC LIMIT 1";
91 $row = sqlQuery( $statement, array( $this->_encounterId, 'form_encounter', SignatureIF::ESIGN_LOCK ) );
92 if ( $row && $row['is_lock'] == SignatureIF::ESIGN_LOCK ) {
93 $locked = true;
97 return $locked;
101 * Get the data in an array for this form.
103 * First, we check the forms table to get the row id in the
104 * specific table. Then we get the row of data from the specific
105 * form_* table.
107 * @see \ESign\SignableIF::getData()
109 public function getData()
111 // Use default standards based on formdir value
112 // Exceptions are specified in formdir_keys list
113 $row = sqlQuery("SELECT title FROM list_options WHERE list_id = ? AND option_id = ? AND activity = 1",
114 array('formdir_keys', $this->_formDir));
115 if (isset($row['title'])) {
116 $excp = json_decode("{".$row['title']."}");
118 $tbl = (isset($excp->tbl) ? $excp->tbl : "form_".$this->_formDir);
119 $id = (isset($excp->id) ? $excp->id : 'id');
120 $limit = (isset($excp->limit) ? $excp->limit : 1);
122 // Get form data based on key from forms table
123 $sql = sprintf("SELECT fd.* FROM %s fd
124 INNER JOIN forms f ON fd.%s = f.form_id
125 WHERE f.id = ?",
126 escape_table_name($tbl), escape_sql_column_name($id, array($tbl)));
127 if ($limit <> '*') {
128 $sql .= ' LIMIT '.escape_limit($limit);
130 $rs = sqlStatement($sql, array( $this->_formId ) );
131 if (sqlNumRows($rs) == 1) { // maintain legacy hash
132 $frs = sqlFetchArray($rs);
133 } else {
134 $frs = array();
135 while ($fr = sqlFetchArray($rs)) {
136 array_push($frs, $fr);
140 return $frs;