/
Note.class.php
201 lines (175 loc) · 5.18 KB
/
Note.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* class Note
* This class offers functionality to store sequential comments/notes about an external object or anything with a unique id.
* It is not intended that once a note is save it can be editied or changed.
*/
use OpenEMR\Common\ORDataObject\ORDataObject;
class Note extends ORDataObject
{
/*
* Database unique identifier
* @var id
*/
var $id;
/*
* DB unique identifier reference to some other table, this is not unique in the notes table
* @var int
*/
var $foreign_id;
/*
* Narrative comments about whatever object is represented by the foreign id this note is associated with
* @var string upto 255 character string
*/
var $note;
/*
* Foreign key identifier of who initially persisited the note,
* potentially ownership could be changed but that would be up to an external non-document object process
* @var int
*/
var $owner;
/*
* Date the note was first persisted
* @var date
*/
var $date;
/*
* Timestamp of the last time the note was changed and persisted, auto maintained by DB, manually change at your own peril
* @var int
*/
var $revision;
/**
* Constructor sets all Note attributes to their default value
* @param int $id optional existing id of a specific note, if omitted a "blank" note is created
*/
function __construct($id = "")
{
//call the parent constructor so we have a _db to work with
parent::__construct();
//shore up the most basic ORDataObject bits
$this->id = $id;
$this->_table = "notes";
$this->note = "";
$this->date = date("Y-m-d H:i:s");
if ($id != "") {
$this->populate();
}
}
/**
* Convenience function to get an array of many document objects
* For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
* @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
*/
public static function notes_factory($foreign_id = "")
{
$notes = array();
$sqlArray = array();
if (empty($foreign_id)) {
$foreign_id_sql = " like '%'";
} else {
$foreign_id_sql = " = ?";
$sqlArray[] = strval($foreign_id);
}
$d = new note();
$sql = "SELECT id FROM " . escape_table_name($d->_table) . " WHERE foreign_id " . $foreign_id_sql . " ORDER BY DATE DESC";
//echo $sql;
$result = $d->_db->Execute($sql, $sqlArray);
while ($result && !$result->EOF) {
$notes[] = new Note($result->fields['id']);
$result->MoveNext();
}
return $notes;
}
public function getOwnerName()
{
if (!empty($this->owner)) {
$user_info = sqlQuery("SELECT `fname`, `lname` FROM `users` where `id`=?", [$this->owner]);
if (!empty($user_info)) {
return ($user_info['fname'] . " " . $user_info['lname']);
}
}
}
/**
* Convenience function to generate string debug data about the object
*/
function toString($html = false)
{
$string .= "\n"
. "ID: " . $this->id . "\n"
. "FID: " . $this->foreign_id . "\n"
. "note: " . $this->note . "\n"
. "date: " . $this->date . "\n"
. "owner: " . $this->owner . "\n"
. "revision: " . $this->revision . "\n";
if ($html) {
return nl2br($string);
} else {
return $string;
}
}
/**#@+
* Getter/Setter methods used by reflection to affect object in persist/poulate operations
* @param mixed new value for given attribute
*/
function set_id($id)
{
$this->id = $id;
}
function get_id()
{
return $this->id;
}
function set_foreign_id($fid)
{
$this->foreign_id = $fid;
}
function get_foreign_id()
{
return $this->foreign_id;
}
function set_note($note)
{
$this->note = $note;
}
function get_note()
{
return $this->note;
}
function set_date($date)
{
$this->date = $date;
}
function get_date()
{
return $this->date;
}
function set_owner($owner)
{
$this->owner = $owner;
}
function get_owner()
{
return $this->owner;
}
/*
* No getter for revision because it is updated automatically by the DB.
*/
function set_revision($revision)
{
$this->revision = $revision;
}
/*
* Overridden function to store current object state in the db.
* This overide is to allow for a "just in time" foreign id, often this is needed
* when the object is never directly exposed and is handled as part of a larger
* object hierarchy.
* @param int $fid foreign id that should be used so that this note can be related (joined) on it later
*/
function persist($fid = "")
{
if (!empty($fid)) {
$this->foreign_id = $fid;
}
parent::persist();
}
} // end of Note