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