using OemrUI class in Fees > Fee Sheet, Billing, Payment, Checkout, Batch Payments...
[openemr.git] / services / DocumentService.php
blob6048e40c9b2ddb18e660eedf9240c1b27358e65c
1 <?php
2 /**
3 * DocumentService
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Matthew Vita <matthewvita48@gmail.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Services;
17 use Particle\Validator\Validator;
19 class DocumentService
21 public function __construct()
25 public function isValidPath($path)
27 $docPathParts = explode("/", $path);
29 unset($docPathParts[0]);
31 $categoriesSql = " SELECT parent, id";
32 $categoriesSql .= " FROM categories";
33 $categoriesSql .= " WHERE replace(LOWER(name), ' ', '') = ?";
35 $lastParent = null;
36 $isValidPath = true;
37 foreach ($docPathParts as $index => $part) {
38 $categoryResults = sqlQuery($categoriesSql, str_replace("_", "", $part));
40 if ($index === 1) {
41 $lastParent = $categoryResults["id"];
42 continue;
45 if ($categoryResults["parent"] === $lastParent) {
46 $lastParent = $categoryResults["id"];
47 } else {
48 $isValidPath = false;
49 break;
53 return $isValidPath;
56 public function getLastIdOfPath($path)
58 $docPathParts = explode("/", $path);
59 $lastInPath = end($docPathParts);
61 $sql = " SELECT id";
62 $sql .= " FROM categories";
63 $sql .= " WHERE replace(LOWER(name), ' ', '') = ?";
65 $results = sqlQuery($sql, str_replace("_", "", $lastInPath));
66 return $results['id'];
69 public function getAllAtPath($pid, $path)
71 if (!$this->isValidPath($path)) {
72 return false;
75 $categoryId = $this->getLastIdOfPath($path);
77 $documentsSql = " SELECT doc.url, doc.id, doc.mimetype, doc.docdate";
78 $documentsSql .= " FROM documents doc";
79 $documentsSql .= " JOIN categories_to_documents ctd on ctd.document_id = doc.id";
80 $documentsSql .= " WHERE ctd.category_id = ? and doc.foreign_id = ?";
82 $documentResults = sqlStatement($documentsSql, array($categoryId, $pid));
84 $fileResults = array();
85 while ($row = sqlFetchArray($documentResults)) {
86 array_push($fileResults, array(
87 "filename" => basename($row["url"]),
88 "id" => $row["id"],
89 "mimetype" => $row["mimetype"],
90 "docdate" => $row["docdate"]
91 ));
93 return $fileResults;
96 public function insertAtPath($pid, $path, $fileData)
98 if (!$this->isValidPath($path)) {
99 return false;
102 $categoryId = $this->getLastIdOfPath($path);
104 $nextIdResult = sqlQuery("SELECT MAX(id)+1 as id FROM documents");
106 $insertDocSql = " INSERT INTO documents SET";
107 $insertDocSql .= " id=?,";
108 $insertDocSql .= " type='file_url',";
109 $insertDocSql .= " size=?,";
110 $insertDocSql .= " date=NOW(),";
111 $insertDocSql .= " url=?,";
112 $insertDocSql .= " mimetype=?,";
113 $insertDocSql .= " foreign_id=?";
115 sqlInsert(
116 $insertDocSql,
117 array(
118 $nextIdResult["id"],
119 $fileData["size"],
120 $GLOBALS['oer_config']['documents']['repository'] . $categoryId . "/" . $fileData["name"],
121 $fileData["type"],
122 $pid
126 $cateToDocsSql = " INSERT INTO categories_to_documents SET";
127 $cateToDocsSql .= " category_id=?,";
128 $cateToDocsSql .= " document_id=?";
130 sqlInsert(
131 $cateToDocsSql,
132 array(
133 $categoryId,
134 $nextIdResult["id"]
138 $newPath = $GLOBALS['oer_config']['documents']['repository'] . "/" . $categoryId;
139 if (!file_exists($newPath)) {
140 mkdir($newPath, 0700, true);
143 $moved = move_uploaded_file($fileData["tmp_name"], $newPath . "/" . $fileData["name"]);
145 return $moved;
148 public function getFile($pid, $did)
150 return sqlQuery("SELECT url FROM documents WHERE id = ? AND foreign_id = ?", array($did, $pid));