2 /* +-----------------------------------------------------------------------------+
3 * OpenEMR - Open Source Electronic Medical Record
4 * Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @author Basil PT <basil@zhservices.com>
19 * +------------------------------------------------------------------------------+
22 namespace Documents\Model
;
24 use Zend\Db\TableGateway\AbstractTableGateway
;
25 use Zend\Db\Adapter\Adapter
;
26 use \Application\Model\ApplicationTable
;
28 class DocumentsTable
extends AbstractTableGateway
31 * Save the category - document mapping
32 * @param $category_id integer Category ID
33 * @param $document_id integer Document ID
35 public function insertDocumentCategory($category_id, $document_id)
37 $obj = new ApplicationTable();
38 $sql = "INSERT INTO categories_to_documents (category_id, document_id) VALUES (?, ?)";
39 $result = $obj->zQuery($sql, array($category_id, $document_id));
43 * Move the document to a different category
44 * @param $category_id integer Category ID
45 * @param $document_id integer Document ID
47 public function updateDocumentCategory($category_id, $document_id)
49 $obj = new ApplicationTable();
50 $sql = "UPDATE categories_to_documents SET category_id = ? WHERE document_id = ?";
51 $result = $obj->zQuery($sql, array($category_id, $document_id));
55 * getCategories - Get Document Categories
56 * @param Integer $categoryParentId
59 public function getCategories($categoryParentId)
61 $obj = new ApplicationTable();
62 $sql = "SELECT * FROM `categories` WHERE `parent` = ? ORDER BY `order`";
63 $result = $obj->zQuery($sql, array($categoryParentId));
65 foreach ($result as $row) {
66 $category[$row['cat_id']]= array(
67 'category_id' => $row['id'],
68 'category_name' => $row['name'],
76 * getDocument - get Document Data by Id
77 * @param Integer $documentId Document Id
80 public function getDocument($documentId)
82 $obj = new ApplicationTable();
83 $sql = "SELECT * FROM documents AS doc
84 JOIN categories_to_documents AS cat_doc ON cat_doc.document_id = doc.id
86 $result = $obj->zQuery($sql, array($documentId));
87 return $result->current();
91 * getCategoryIDs - get Category Ids By Name
92 * @param Array $categories - Category Lists
95 public function getCategoryIDs($categories = array())
97 $obj = new ApplicationTable();
98 $categories_count = count($categories);
100 for ($i=0; $i<$categories_count; $i++
) {
104 $sql = "SELECT `id`,`name` FROM `categories` ".
105 "WHERE `name` IN (". implode(",", $cat_name) .")";
106 $result = $obj->zQuery($sql, $categories);
108 foreach ($result as $row) {
109 $category[$row['name']] = $row['id'];
116 * saveDocumentdetails - save document details
117 * @param Array $current_document - document details
119 public function saveDocumentdetails($current_document)
121 $obj = new ApplicationTable();
122 foreach ($current_document as $values) {
132 $result = $obj->zQuery($sql, array($values['doc_docdate'], $values['patientname'], $values['notes'], $values['issue'],$values['docname'],$values['doc_id']));
133 $this->updateDocumentCategory($values['category'], $values['doc_id']);
138 * getCategory - get document categories
141 public function getCategory()
143 $obj = new ApplicationTable();
144 $sql = "SELECT * FROM `categories`";
145 $result = $obj->zQuery($sql);
146 foreach ($result as $values) {
147 $category[] = $values;
154 * deleteDocument - remove document from list
157 public function deleteDocument($docid)
159 $obj = new ApplicationTable();
165 $obj->zQuery($sql, array(0,$docid));
169 *Update document category using category name
170 *@param $category_name - Name of the category to which the document has to be moved
171 *@param $document_id - Documents whose category has to be updated with $category_name
173 public function updateDocumentCategoryUsingCatname($category_name, $document_id)
175 $obj = new ApplicationTable();
176 $sql = "UPDATE categories_to_documents
177 JOIN categories ON `name` = ?
179 WHERE document_id = ?";
180 $result = $obj->zQuery($sql, array($category_name, $document_id));