| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 0.00% covered (danger) | 0.00% | 0 / 75 | 0.00% covered (danger) | 0.00% | 0 / 18 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 4
DocumentIterator | 0.00% covered (danger) | 0.00% | 0 / 31 | 0.00% covered (danger) | 0.00% | 0 / 7 | 506 | 0.00% covered (danger) | 0.00% | 0 / 1
__construct | 0.00% covered (danger) | 0.00% | 0 / 6 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
rewind | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
valid | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
next | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
key | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
current | 0.00% covered (danger) | 0.00% | 0 / 6 | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 |
populate | 0.00% covered (danger) | 0.00% | 0 / 15 | 0.00% covered (danger) | 0.00% | 0 / 1 | 210 |
FolderIterator | 0.00% covered (danger) | 0.00% | 0 / 31 | 0.00% covered (danger) | 0.00% | 0 / 7 | 506 | 0.00% covered (danger) | 0.00% | 0 / 1
__construct | 0.00% covered (danger) | 0.00% | 0 / 6 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
rewind | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
valid | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
next | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
key | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
current | 0.00% covered (danger) | 0.00% | 0 / 6 | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 |
populate | 0.00% covered (danger) | 0.00% | 0 / 15 | 0.00% covered (danger) | 0.00% | 0 / 1 | 210 |
FolderFilterIterator | 0.00% covered (danger) | 0.00% | 0 / 6 | 0.00% covered (danger) | 0.00% | 0 / 2 | 12 | 0.00% covered (danger) | 0.00% | 0 / 1
__construct | 0.00% covered (danger) | 0.00% | 0 / 2 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |
accept | 0.00% covered (danger) | 0.00% | 0 / 4 | 0.00% covered (danger) | 0.00% | 0 / 1 | 6 |
RecursiveFolderIterator | 0.00% covered (danger) | 0.00% | 0 / 7 | 0.00% covered (danger) | 0.00% | 0 / 2 | 20 | 0.00% covered (danger) | 0.00% | 0 / 1
hasChildren | 0.00% covered (danger) | 0.00% | 0 / 6 | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 |
getChildren | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |

1 | <?php
2 | declare(strict_types=1);
3 |
4 | namespace SeedDMS\Core;
5 |
6 | /**
7 | * Implementation of the document iterartor
8 | *
9 | * @category DMS
10 | * @package SeedDMS_Core
11 | * @license GPL 2
12 | * @version @version@
13 | * @author Uwe Steinmann <uwe@steinmann.cx>
14 | * @copyright Copyright (C) 2010-2024 Uwe Steinmann
15 | * @version Release: @package_version@
16 | */
17 |
18 | class DocumentIterator implements \Iterator {
19 | /**
20 | * @var object folder
21 | */
22 | protected $_folder;
23 |
24 | /**
25 | * @var object dms
26 | */
27 | protected $_dms;
28 |
29 | /**
30 | * @var array documents
31 | */
32 | protected $_documents;
33 |
34 | /**
35 | * @var int $_pointer
36 | */
37 | protected $_pointer;
38 |
39 | /**
40 | * @var array $_cache
41 | */
42 | protected $_cache;
43 |
44 | public function __construct($folder) {
45 | $this->_folder = $folder;
46 | $this->_dms = $folder->getDMS();
47 | $this->_documents = array();
48 | $this->_pointer = 0;
49 | $this->_cache = array();
50 | $this->populate();
51 | }
52 |
53 | public function rewind(): void {
54 | $this->_pointer = 0;
55 | }
56 |
57 | public function valid(): bool {
58 | return isset($this->_documents[$this->_pointer]);
59 | }
60 |
61 | public function next(): void {
62 | $this->_pointer++;
63 | }
64 |
65 | public function key(): mixed {
66 | return $this->_documents[$this->_pointer];
67 | }
68 |
69 | public function current(): mixed {
70 | if ($this->_documents[$this->_pointer]) {
71 | $documentid = $this->_documents[$this->_pointer]['id'];
72 | if (!isset($this->_cache[$documentid])) {
73 | // echo $documentid." not cached<br />";
74 | $this->_cache[$documentid] = $this->_dms->getdocument($documentid);
75 | }
76 | return $this->_cache[$documentid];
77 | }
78 | return null;
79 | }
80 |
81 | private function populate($orderby = "", $dir = "asc", $limit = 0, $offset = 0) { /* {{{ */
82 | $db = $this->_dms->getDB();
83 |
84 | $queryStr = "SELECT `id` FROM `tblDocuments` WHERE `folder` = " . $this->_folder->getID();
85 |
86 | if ($orderby && $orderby[0]=="n") $queryStr .= " ORDER BY `name`";
87 | elseif ($orderby && $orderby[0]=="s") $queryStr .= " ORDER BY `sequence`";
88 | elseif ($orderby && $orderby[0]=="d") $queryStr .= " ORDER BY `date`";
89 | if ($dir == 'desc')
90 | $queryStr .= " DESC";
91 | if (is_int($limit) && $limit > 0) {
92 | $queryStr .= " LIMIT ".$limit;
93 | if (is_int($offset) && $offset > 0)
94 | $queryStr .= " OFFSET ".$offset;
95 | }
96 |
97 | $resArr = $db->getResultArray($queryStr);
98 | if (is_bool($resArr) && $resArr == false)
99 | return false;
100 |
101 | $this->_documents = $resArr;
102 | } /* }}} */
103 | }
104 |
105 | class FolderIterator implements \Iterator { /* {{{ */
106 | /**
107 | * @var object folder
108 | */
109 | protected $_folder;
110 |
111 | /**
112 | * @var object dms
113 | */
114 | protected $_dms;
115 |
116 | /**
117 | * @var array documents
118 | */
119 | protected $_folders;
120 |
121 | /**
122 | * @var int $_pointer
123 | */
124 | protected $_pointer;
125 |
126 | /**
127 | * @var array $_cache
128 | */
129 | protected $_cache;
130 |
131 | public function __construct($folder) { /* {{{ */
132 | $this->_folder = $folder;
133 | $this->_dms = $folder->getDMS();
134 | $this->_folders = array();
135 | $this->_pointer = 0;
136 | $this->_cache = array();
137 | $this->populate();
138 | } /* }}} */
139 |
140 | #[\ReturnTypeWillChange]
141 | public function rewind() { /* {{{ */
142 | $this->_pointer = 0;
143 | } /* }}} */
144 |
145 | #[\ReturnTypeWillChange]
146 | public function valid() { /* {{{ */
147 | return isset($this->_folders[$this->_pointer]);
148 | } /* }}} */
149 |
150 | #[\ReturnTypeWillChange]
151 | public function next() { /* {{{ */
152 | $this->_pointer++;
153 | } /* }}} */
154 |
155 | #[\ReturnTypeWillChange]
156 | public function key() { /* {{{ */
157 | return $this->_folders[$this->_pointer];
158 | } /* }}} */
159 |
160 | #[\ReturnTypeWillChange]
161 | public function current() { /* {{{ */
162 | if ($this->_folders[$this->_pointer]) {
163 | $folderid = $this->_folders[$this->_pointer]['id'];
164 | if (!isset($this->_cache[$folderid])) {
165 | // echo $folderid." not cached<br />";
166 | $this->_cache[$folderid] = $this->_dms->getFolder($folderid);
167 | }
168 | return $this->_cache[$folderid];
169 | }
170 | return null;
171 | } /* }}} */
172 |
173 | private function populate($orderby = "", $dir = "asc", $limit = 0, $offset = 0) { /* {{{ */
174 | $db = $this->_dms->getDB();
175 |
176 | $queryStr = "SELECT `id` FROM `tblFolders` WHERE `parent` = " . $this->_folder->getID();
177 |
178 | if ($orderby && $orderby[0]=="n") $queryStr .= " ORDER BY `name`";
179 | elseif ($orderby && $orderby[0]=="s") $queryStr .= " ORDER BY `sequence`";
180 | elseif ($orderby && $orderby[0]=="d") $queryStr .= " ORDER BY `date`";
181 | if ($dir == 'desc')
182 | $queryStr .= " DESC";
183 | if (is_int($limit) && $limit > 0) {
184 | $queryStr .= " LIMIT ".$limit;
185 | if (is_int($offset) && $offset > 0)
186 | $queryStr .= " OFFSET ".$offset;
187 | }
188 |
189 | $resArr = $db->getResultArray($queryStr);
190 | if (is_bool($resArr) && $resArr == false)
191 | return false;
192 |
193 | $this->_folders = $resArr;
194 | } /* }}} */
195 | } /* }}} */
196 |
197 | /**
198 | * The FolderFilterIterator checks if the given user has access on
199 | * the current folder.
200 | * FilterIterator uses an inner iterator passed to the constructor
201 | * to iterate over the sub folders of a folder.
202 | *
203 | $iter = new FolderIterator($folder);
204 | $iter2 = new FolderFilterIterator($iter, $user);
205 | foreach($iter2 as $ff) {
206 | echo $ff->getName()."<br />";
207 | }
208 | */
209 | class FolderFilterIterator extends \FilterIterator { /* {{{ */
210 | /**
211 | * @var object filter
212 | */
213 | protected $folderFilter;
214 |
215 | public function __construct(\Iterator $iterator, $filter) {
216 | parent::__construct($iterator);
217 | $this->folderFilter = $filter;
218 | }
219 |
220 | public function accept(): bool { /* {{{ */
221 | $folder = $this->getInnerIterator()->current();
222 | if (strcasecmp($folder->getName(), $this->folderFilter) == 0) {
223 | return false;
224 | }
225 | return true;
226 | } /* }}} */
227 | } /* }}} */
228 |
229 | /**
230 | $iter = new RecursiveFolderIterator($folder);
231 | $iter2 = new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::SELF_FIRST);
232 | foreach($iter2 as $ff) {
233 | echo $ff->getID().': '.$ff->getName()."<br />";
234 | }
235 | */
236 | class RecursiveFolderIterator extends FolderIterator implements \RecursiveIterator { /* {{{ */
237 |
238 | #[\ReturnTypeWillChange]
239 | public function hasChildren() { /* {{{ */
240 | $db = $this->_dms->getDB();
241 | $queryStr = "SELECT id FROM `tblFolders` WHERE `parent` = ".(int) $this->current()->getID();
242 | $resArr = $db->getResultArray($queryStr);
243 | if (is_bool($resArr) && !$resArr)
244 | return false;
245 | return true;
246 | } /* }}} */
247 |
248 | #[\ReturnTypeWillChange]
249 | public function getChildren() { /* {{{ */
250 | return new RecursiveFolderIterator($this->current());
251 | } /* }}} */
252 | } /* }}} */
253 |