Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.55% covered (warning)
67.55%
204 / 302
50.00% covered (danger)
50.00%
12 / 24
CRAP
0.00% covered (danger)
0.00%
0 / 1
SeedDMS_Core_Group
67.55% covered (warning)
67.55%
204 / 302
50.00% covered (danger)
50.00%
12 / 24
575.80
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getInstance
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
6.01
 getAllInstances
85.71% covered (warning)
85.71%
12 / 14
0.00% covered (danger)
0.00%
0 / 1
6.10
 isType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDMS
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDMS
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getID
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setName
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getComment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setComment
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getUsers
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
 getManagers
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
4.00
 addUser
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 removeUser
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 isMember
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
11.36
 toggleManager
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 remove
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
272
 getReviewStatus
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
8.04
 getApprovalStatus
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
8.05
 getReceiptStatus
85.71% covered (warning)
85.71%
18 / 21
0.00% covered (danger)
0.00%
0 / 1
9.24
 getRevisionStatus
88.89% covered (warning)
88.89%
24 / 27
0.00% covered (danger)
0.00%
0 / 1
12.20
 getWorkflowStatus
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 getNotifications
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
5.01
1<?php
2declare(strict_types=1);
3
4/**
5 * Implementation of the group object in the document management system
6 *
7 * @category   DMS
8 * @package    SeedDMS_Core
9 * @license    GPL 2
10 * @version    @version@
11 * @author     Uwe Steinmann <uwe@steinmann.cx>
12 * @copyright  Copyright (C) 2002-2005 Markus Westphal, 2006-2008 Malcolm Cowe,
13 *             2010-2024 Uwe Steinmann
14 * @version    Release: @package_version@
15 */
16
17/**
18 * Class to represent a user group in the document management system
19 *
20 * @category   DMS
21 * @package    SeedDMS_Core
22 * @author     Markus Westphal, Malcolm Cowe, Uwe Steinmann <uwe@steinmann.cx>
23 * @copyright  Copyright (C) 2002-2005 Markus Westphal, 2006-2008 Malcolm Cowe,
24 *             2010-2024 Uwe Steinmann
25 * @version    Release: @package_version@
26 */
27class SeedDMS_Core_Group { /* {{{ */
28    /**
29     * The id of the user group
30     *
31     * @var integer
32     */
33    protected $_id;
34
35    /**
36     * The name of the user group
37     *
38     * @var string
39     */
40    protected $_name;
41
42    /**
43     * @var SeedDMS_Core_User[]
44     */
45    protected $_users;
46
47    /**
48     * The comment of the user group
49     *
50     * @var string
51     */
52    protected $_comment;
53
54    /**
55     * Back reference to DMS this user group belongs to
56     *
57     * @var SeedDMS_Core_DMS
58     */
59    protected $_dms;
60
61    public function __construct($id, $name, $comment) { /* {{{ */
62        $this->_id = $id;
63        $this->_name = $name;
64        $this->_comment = $comment;
65        $this->_dms = null;
66    } /* }}} */
67
68    /**
69     * Return an instance of a group object
70     *
71     * @param string|integer $id Id, name of group, depending
72     * on the 3rd parameter.
73     * @param SeedDMS_Core_DMS $dms instance of dms
74     * @param string $by search by group name if set to 'name'. 
75     * Search by Id of group if left empty.
76     * @return SeedDMS_Core_Group|bool instance of class SeedDMS_Core_Group if group was
77     * found, null if group was not found, false in case of error
78     */
79    public static function getInstance($id, $dms, $by='') { /* {{{ */
80        $db = $dms->getDB();
81
82        switch($by) {
83        case 'name':
84            $queryStr = "SELECT * FROM `tblGroups` WHERE `name` = ".$db->qstr($id);
85            break;
86        default:
87            $queryStr = "SELECT * FROM `tblGroups` WHERE `id` = " . (int) $id;
88        }
89
90        $resArr = $db->getResultArray($queryStr);
91        if (is_bool($resArr) && $resArr == false)
92            return false;
93        else if (count($resArr) != 1) //wenn, dann wohl eher 0 als > 1 ;-)
94            return null;
95
96        $resArr = $resArr[0];
97
98        $group = new self($resArr["id"], $resArr["name"], $resArr["comment"]);
99        $group->setDMS($dms);
100        return $group;
101    } /* }}} */
102
103    /**
104     * Get all groups
105     *
106     * @param $orderby set to `name` for odering by name
107     * @param SeedDMS_Core_DMS $dms
108     * @return SeedDMS_Core_Group[]|bool list of groups or false in case of an error
109     */
110    public static function getAllInstances($orderby, $dms) { /* {{{ */
111        $db = $dms->getDB();
112
113        switch($orderby) {
114        case 'name':
115            $queryStr = "SELECT * FROM `tblGroups` ORDER BY `name`";
116            break;
117        default:
118            $queryStr = "SELECT * FROM `tblGroups` order by `id`";
119        }
120        $resArr = $db->getResultArray($queryStr);
121
122        if (is_bool($resArr) && $resArr == false)
123            return false;
124
125        $groups = array();
126        for ($i = 0; $i < count($resArr); $i++) {
127            $group = new self($resArr[$i]["id"], $resArr[$i]["name"], $resArr[$i]["comment"]);
128            $group->setDMS($dms);
129            $groups[$i] = $group;
130        }
131
132        return $groups;
133    } /* }}} */
134
135    /**
136     * Check if this object is of type 'group'.
137     *
138     * @param string $type type of object
139     */
140    public function isType($type) { /* {{{ */
141        return $type == 'group';
142    } /* }}} */
143
144    /**
145     * @param SeedDMS_Core_DMS $dms
146     */
147    public function setDMS($dms) { /* {{{ */
148        $this->_dms = $dms;
149    } /* }}} */
150
151    /**
152     * @return SeedDMS_Core_DMS $dms
153     */
154    public function getDMS() {
155        return $this->_dms;
156    }
157
158    /**
159     * Return internal id of group
160     *
161     * @return int
162     */
163    public function getID() { return $this->_id; }
164
165    /**
166     * Return name of group
167     *
168     * @return string
169     */
170    public function getName() { return $this->_name; }
171
172    /**
173     * Set new name of group
174     *
175     * @param $newName new name
176     * @return bool true on success, otherwise false
177     */
178    public function setName($newName) { /* {{{ */
179        $newName = trim($newName);
180        if(!$newName)
181            return false;
182
183        $db = $this->_dms->getDB();
184
185        $queryStr = "UPDATE `tblGroups` SET `name` = ".$db->qstr($newName)." WHERE `id` = " . $this->_id;
186        if (!$db->getResult($queryStr))
187            return false;
188
189        $this->_name = $newName;
190        return true;
191    } /* }}} */
192
193    /**
194     * Return comment of group
195     *
196     * @return string
197     */
198    public function getComment() { return $this->_comment; }
199
200    /**
201     * Set new comment of group
202     *
203     * @param $newComment
204     * @return bool true on success, otherwise false
205     */
206    public function setComment($newComment) { /* {{{ */
207        $db = $this->_dms->getDB();
208
209        $queryStr = "UPDATE `tblGroups` SET `comment` = ".$db->qstr($newComment)." WHERE `id` = " . $this->_id;
210        if (!$db->getResult($queryStr))
211            return false;
212
213        $this->_comment = $newComment;
214        return true;
215    } /* }}} */
216
217    /**
218     * Get all users which are members of the group
219     *
220     * @return SeedDMS_Core_User[]|bool
221     */
222    public function getUsers() { /* {{{ */
223        $db = $this->_dms->getDB();
224
225        if (!isset($this->_users)) {
226            $queryStr = "SELECT `tblUsers`.* FROM `tblUsers` ".
227                "LEFT JOIN `tblGroupMembers` ON `tblGroupMembers`.`userID`=`tblUsers`.`id` ".
228                "WHERE `tblGroupMembers`.`groupID` = '". $this->_id ."'";
229            $resArr = $db->getResultArray($queryStr);
230            if (is_bool($resArr) && $resArr == false)
231                return false;
232
233            $this->_users = array();
234
235            $classnamerole = $this->_dms->getClassname('role');
236            $classname = $this->_dms->getClassname('user');
237            foreach ($resArr as $row) {
238                /** @var SeedDMS_Core_User $user */
239                $role = $classnamerole::getInstance($row['role'], $this->_dms);
240                $user = new $classname($row["id"], $row["login"], $row["pwd"], $row["fullName"], $row["email"], $row["language"], $row["theme"], $row["comment"], $role, $row['hidden']);
241                $user->setDMS($this->_dms);
242                array_push($this->_users, $user);
243            }
244        }
245        return $this->_users;
246    } /* }}} */
247
248    /**
249     * Get all users which are managers of the group
250     *
251     * @return SeedDMS_Core_User[]|bool
252     */
253    public function getManagers() { /* {{{ */
254        $db = $this->_dms->getDB();
255
256        $queryStr = "SELECT `tblUsers`.* FROM `tblUsers` ".
257            "LEFT JOIN `tblGroupMembers` ON `tblGroupMembers`.`userID`=`tblUsers`.`id` ".
258            "WHERE `tblGroupMembers`.`groupID` = '". $this->_id ."' AND `tblGroupMembers`.`manager` = 1";
259        $resArr = $db->getResultArray($queryStr);
260        if (is_bool($resArr) && $resArr == false)
261            return false;
262
263        $managers = array();
264
265        $classnamerole = $this->_dms->getClassname('role');
266        $classname = $this->_dms->getClassname('user');
267        foreach ($resArr as $row) {
268            /** @var SeedDMS_Core_User $user */
269            $role = $classnamerole::getInstance($row['role'], $this->_dms);
270            $user = new $classname($row["id"], $row["login"], $row["pwd"], $row["fullName"], $row["email"], $row["language"], $row["theme"], $row["comment"], $role, $row['hidden']);
271            $user->setDMS($this->_dms);
272            array_push($managers, $user);
273        }
274        return $managers;
275    } /* }}} */
276
277    /**
278     * Add user as member
279     *
280     * @param SeedDMS_Core_User $user
281     * @param bool $asManager if set, user will be a manager of the group
282     * @return bool
283     */
284    public function addUser($user,$asManager=false) { /* {{{ */
285        $db = $this->_dms->getDB();
286
287        $queryStr = "INSERT INTO `tblGroupMembers` (`groupID`, `userID`, `manager`) VALUES (".$this->_id.", ".$user->getID(). ", " . ($asManager?"1":"0") ." )";
288        $res = $db->getResult($queryStr);
289
290        if (!$res) return false;
291
292        unset($this->_users);
293        return true;
294    } /* }}} */
295
296    /**
297     * Remove users as a member of the group
298     *
299     * @param SeedDMS_Core_User $user
300     * @return bool
301     */
302    public function removeUser($user) { /* {{{ */
303        $db = $this->_dms->getDB();
304
305        $queryStr = "DELETE FROM `tblGroupMembers` WHERE `groupID` = ".$this->_id." AND `userID` = ".$user->getID();
306        $res = $db->getResult($queryStr);
307
308        if (!$res) return false;
309        unset($this->_users);
310        return true;
311    } /* }}} */
312
313    /**
314     * Check if user is member of group
315     *
316     * @param SeedDMS_Core_User $user user to be checked
317     * @param boolean $asManager also check whether user is manager of group if
318     * set to true, otherwise does not care about manager status
319     * @return boolean true if user is member, otherwise false
320     */
321    public function isMember($user, $asManager=false) { /* {{{ */
322        if (isset($this->_users)&&!$asManager) {
323            foreach ($this->_users as $usr)
324                if ($usr->getID() == $user->getID())
325                    return true;
326            return false;
327        }
328
329        $db = $this->_dms->getDB();
330        $queryStr = "SELECT * FROM `tblGroupMembers` WHERE `groupID` = " . $this->_id . " AND `userID` = " . $user->getID();
331        if ($asManager)
332             $queryStr .= " AND `manager` = 1";
333
334        $resArr = $db->getResultArray($queryStr);
335
336        if (is_bool($resArr) && $resArr == false) return false;
337        if (count($resArr) != 1) return false;
338
339        return true;
340    } /* }}} */
341
342    /**
343     * Toggle manager status of user
344     *
345     * @param SeedDMS_Core_User $user
346     * @return boolean true if operation was successful, otherwise false
347     */
348    public function toggleManager($user) { /* {{{ */
349        $db = $this->_dms->getDB();
350
351        if (!$this->isMember($user)) return false;
352
353        if ($this->isMember($user,true)) $queryStr = "UPDATE `tblGroupMembers` SET `manager` = 0 WHERE `groupID` = ".$this->_id." AND `userID` = ".$user->getID();
354        else $queryStr = "UPDATE `tblGroupMembers` SET `manager` = 1 WHERE `groupID` = ".$this->_id." AND `userID` = ".$user->getID();
355
356        if (!$db->getResult($queryStr)) return false;
357        return true;
358    } /* }}} */
359
360    /**
361     * Delete user group
362     *
363     * This method deletes the user group and all its references, like access
364     * control lists, notifications, etc.
365     *
366     * @param SeedDMS_Core_User $user the user doing the removal (needed for entry in
367     *        review log.
368     * @return boolean true on success or false in case of an error
369     */
370    public function remove($user) { /* {{{ */
371        $db = $this->_dms->getDB();
372
373        $db->startTransaction();
374
375        $queryStr = "DELETE FROM `tblGroupMembers` WHERE `groupID` = " . $this->_id;
376        if (!$db->getResult($queryStr)) {
377            $db->rollbackTransaction();
378            return false;
379        }
380        $queryStr = "DELETE FROM `tblACLs` WHERE `groupID` = " . $this->_id;
381        if (!$db->getResult($queryStr)) {
382            $db->rollbackTransaction();
383            return false;
384        }
385        $queryStr = "DELETE FROM `tblNotify` WHERE `groupID` = " . $this->_id;
386        if (!$db->getResult($queryStr)) {
387            $db->rollbackTransaction();
388            return false;
389        }
390        $queryStr = "DELETE FROM `tblMandatoryReviewers` WHERE `reviewerGroupID` = " . $this->_id;
391        if (!$db->getResult($queryStr)) {
392            $db->rollbackTransaction();
393            return false;
394        }
395        $queryStr = "DELETE FROM `tblMandatoryApprovers` WHERE `approverGroupID` = " . $this->_id;
396        if (!$db->getResult($queryStr)) {
397            $db->rollbackTransaction();
398            return false;
399        }
400        $queryStr = "DELETE FROM `tblWorkflowTransitionGroups` WHERE `groupid` = " . $this->_id;
401        if (!$db->getResult($queryStr)) {
402            $db->rollbackTransaction();
403            return false;
404        }
405        $queryStr = "DELETE FROM `tblGroups` WHERE `id` = " . $this->_id;
406        if (!$db->getResult($queryStr)) {
407            $db->rollbackTransaction();
408            return false;
409        }
410
411        // TODO : update document status if reviewer/approver has been deleted
412
413
414        $reviewStatus = $this->getReviewStatus();
415        foreach ($reviewStatus as $r) {
416            $queryStr = "INSERT INTO `tblDocumentReviewLog` (`reviewID`, `status`, `comment`, `date`, `userID`) ".
417                "VALUES ('". $r["reviewID"] ."', '-2', 'Review group removed from process', ".$db->getCurrentDatetime().", '". $user->getID() ."')";
418            $res=$db->getResult($queryStr);
419            if(!$res) {
420                $db->rollbackTransaction();
421                return false;
422            }
423        }
424
425        $approvalStatus = $this->getApprovalStatus();
426        foreach ($approvalStatus as $a) {
427            $queryStr = "INSERT INTO `tblDocumentApproveLog` (`approveID`, `status`, `comment`, `date`, `userID`) ".
428                "VALUES ('". $a["approveID"] ."', '-2', 'Approval group removed from process', ".$db->getCurrentDatetime().", '". $user->getID() ."')";
429            $res=$db->getResult($queryStr);
430            if(!$res) {
431                $db->rollbackTransaction();
432                return false;
433            }
434        }
435
436        $receiptStatus = $this->getReceiptStatus();
437        foreach ($receiptStatus as $r) {
438            $queryStr = "INSERT INTO `tblDocumentReceiptLog` (`receiptID`, `status`, `comment`, `date`, `userID`) ".
439                "VALUES ('". $r["receiptID"] ."', '-2', 'Recipients group removed from process', ".$db->getCurrentDatetime().", '". $user->getID() ."')";
440            $res=$db->getResult($queryStr);
441            if(!$res) {
442                $db->rollbackTransaction();
443                return false;
444            }
445        }
446
447        $revisionStatus = $this->getRevisionStatus();
448        foreach ($revisionStatus as $r) {
449            $queryStr = "INSERT INTO `tblDocumentRevisionLog` (`revisionID`, `status`, `comment`, `date`, `userID`) ".
450                "VALUES ('". $r["revisionID"] ."', '-2', 'Revisors group removed from process', ".$db->getCurrentDatetime().", '". $user->getID() ."')";
451            $res=$db->getResult($queryStr);
452            if(!$res) {
453                $db->rollbackTransaction();
454                return false;
455            }
456        }
457
458        $db->commitTransaction();
459
460        return true;
461    } /* }}} */
462
463    public function getReviewStatus($documentID=null, $version=null) { /* {{{ */
464        $db = $this->_dms->getDB();
465
466        if (!$db->createTemporaryTable("ttreviewid")) {
467            return false;
468        }
469
470        $status = array();
471
472        // See if the group is assigned as a reviewer.
473        $queryStr = "SELECT `tblDocumentReviewers`.*, `tblDocumentReviewLog`.`status`, ".
474            "`tblDocumentReviewLog`.`comment`, `tblDocumentReviewLog`.`date`, ".
475            "`tblDocumentReviewLog`.`userID` ".
476            "FROM `tblDocumentReviewers` ".
477            "LEFT JOIN `tblDocumentReviewLog` USING (`reviewID`) ".
478            "LEFT JOIN `ttreviewid` on `ttreviewid`.`maxLogID` = `tblDocumentReviewLog`.`reviewLogID` ".
479            "WHERE `ttreviewid`.`maxLogID`=`tblDocumentReviewLog`.`reviewLogID` ".
480            ($documentID==null ? "" : "AND `tblDocumentReviewers`.`documentID` = '". (int) $documentID ."' ").
481            ($version==null ? "" : "AND `tblDocumentReviewers`.`version` = '". (int) $version ."' ").
482            "AND `tblDocumentReviewers`.`type`='1' ".
483            "AND `tblDocumentReviewers`.`required`='". $this->_id ."' ".
484            "ORDER BY `tblDocumentReviewLog`.`reviewLogID` DESC";
485        $resArr = $db->getResultArray($queryStr);
486        if (is_bool($resArr) && $resArr == false)
487            return false;
488        if (count($resArr)>0) {
489            foreach ($resArr as $res)
490                $status[] = $res;
491        }
492        return $status;
493    } /* }}} */
494
495    public function getApprovalStatus($documentID=null, $version=null) { /* {{{ */
496        $db = $this->_dms->getDB();
497
498        if (!$db->createTemporaryTable("ttapproveid")) {
499            return false;
500        }
501
502        $status = array();
503
504        // See if the group is assigned as an approver.
505        $queryStr = "SELECT `tblDocumentApprovers`.*, `tblDocumentApproveLog`.`status`, ".
506            "`tblDocumentApproveLog`.`comment`, `tblDocumentApproveLog`.`date`, ".
507            "`tblDocumentApproveLog`.`userID` ".
508            "FROM `tblDocumentApprovers` ".
509            "LEFT JOIN `tblDocumentApproveLog` USING (`approveID`) ".
510            "LEFT JOIN `ttapproveid` on `ttapproveid`.`maxLogID` = `tblDocumentApproveLog`.`approveLogID` ".
511            "WHERE `ttapproveid`.`maxLogID`=`tblDocumentApproveLog`.`approveLogID` ".
512            ($documentID==null ? "" : "AND `tblDocumentApprovers`.`documentID` = '". (int) $documentID ."' ").
513            ($version==null ? "" : "AND `tblDocumentApprovers`.`version` = '". (int) $version ."' ").
514            "AND `tblDocumentApprovers`.`type`='1' ".
515            "AND `tblDocumentApprovers`.`required`='". $this->_id ."' ";
516        $resArr = $db->getResultArray($queryStr);
517        if (is_bool($resArr) && $resArr == false)
518            return false;
519        if (count($resArr)>0) {
520            foreach ($resArr as $res)
521                $status[] = $res;
522        }
523
524        return $status;
525    } /* }}} */
526
527    function getReceiptStatus($documentID=null, $version=null) { /* {{{ */
528        $db = $this->_dms->getDB();
529
530        $status = array();
531
532        // See if the group is assigned as a recipient.
533        $queryStr = "SELECT `tblDocumentRecipients`.*, `tblDocumentReceiptLog`.`status`, ".
534            "`tblDocumentReceiptLog`.`comment`, `tblDocumentReceiptLog`.`date`, ".
535            "`tblDocumentReceiptLog`.`userID` ".
536            "FROM `tblDocumentRecipients` ".
537            "LEFT JOIN `tblDocumentReceiptLog` USING (`receiptID`) ".
538            "WHERE `tblDocumentRecipients`.`type`='1' ".
539            ($documentID==null ? "" : "AND `tblDocumentRecipients`.`documentID` = '". (int) $documentID ."' ").
540            ($version==null ? "" : "AND `tblDocumentRecipients`.`version` = '". (int) $version ."' ").
541            "AND `tblDocumentRecipients`.`required`='". $this->_id ."' ";
542        $resArr = $db->getResultArray($queryStr);
543        if (is_bool($resArr) && $resArr == false)
544            return false;
545        if (count($resArr)>0) {
546            foreach ($resArr as $res) {
547                if(isset($status[$res['documentID']])) {
548                    if($status[$res['documentID']]['date'] < $res['date']) {
549                        $status[$res['documentID']] = $res;
550                    }
551                } else {
552                    $status[$res['documentID']] = $res;
553                }
554            }
555        }
556        return $status;
557    } /* }}} */
558
559    function getRevisionStatus($documentID=null, $version=null) { /* {{{ */
560        $db = $this->_dms->getDB();
561
562        $status = array();
563
564        if (!$db->createTemporaryTable("ttcontentid")) {
565            return false;
566        }
567        // See if the group is assigned as a revisor.
568        $queryStr = "SELECT `tblDocumentRevisors`.*, `tblDocumentRevisionLog`.`status`, ".
569            "`tblDocumentRevisionLog`.`comment`, `tblDocumentRevisionLog`.`date`, ".
570            "`tblDocumentRevisionLog`.`userID` ".
571            "FROM `tblDocumentRevisors` ".
572            "LEFT JOIN `tblDocumentRevisionLog` USING (`revisionID`) ".
573            "LEFT JOIN `ttcontentid` ON `ttcontentid`.`maxVersion` = `tblDocumentRevisors`.`version` AND `ttcontentid`.`document` = `tblDocumentRevisors`.`documentID` ".
574            "WHERE `tblDocumentRevisors`.`type`='1' ".
575            ($documentID==null ? "" : "AND `tblDocumentRevisors`.`documentID` = '". (int) $documentID ."' ").
576            ($version==null ? "" : "AND `tblDocumentRevisors`.`version` = '". (int) $version ."' ").
577            ($documentID==null && $version==null ? "AND `ttcontentid`.`maxVersion` = `tblDocumentRevisors`.`version` " : "").
578            "AND `tblDocumentRevisors`.`required`='". $this->_id ."' ".
579            "ORDER BY `tblDocumentRevisionLog`.`revisionLogID` DESC";
580        $resArr = $db->getResultArray($queryStr);
581        if ($resArr === false)
582            return false;
583        if (count($resArr)>0) {
584            foreach ($resArr as $res) {
585                if($res['date']) {
586                    if(isset($status[$res['documentID']])) {
587                        if($status[$res['documentID']]['date'] < $res['date']) {
588                            $status[$res['documentID']] = $res;
589                        }
590                    } else {
591                        $status[$res['documentID']] = $res;
592                    }
593                }
594            }
595        }
596        return $status;
597    } /* }}} */
598
599    /**
600     * Get a list of documents with a workflow
601     *
602     * @param int $documentID optional document id for which to retrieve the
603     *        reviews
604     * @param int $version optional version of the document
605     * @return bool|array list of all workflows
606     */
607    public function getWorkflowStatus($documentID=null, $version=null) { /* {{{ */
608        $db = $this->_dms->getDB();
609
610        $queryStr = 'select distinct d.*, c.`groupid` from `tblWorkflowTransitions` a left join `tblWorkflows` b on a.`workflow`=b.`id` left join `tblWorkflowTransitionGroups` c on a.`id`=c.`transition` left join `tblWorkflowDocumentContent` d on b.`id`=d.`workflow` where d.`document` is not null and a.`state`=d.`state` and c.`groupid`='.$this->_id;
611        if($documentID) {
612            $queryStr .= ' AND d.`document`='.(int) $documentID;
613            if($version)
614                $queryStr .= ' AND d.`version`='.(int) $version;
615        }
616        $resArr = $db->getResultArray($queryStr);
617        if (is_bool($resArr) && $resArr == false)
618            return false;
619        $result = array();
620        if (count($resArr)>0) {
621            foreach ($resArr as $res) {
622                $result[] = $res;
623            }
624        }
625        return $result;
626    } /* }}} */
627
628    /**
629     * Get all notifications of group
630     *
631     * @param integer $type type of item (T_DOCUMENT or T_FOLDER)
632     * @return SeedDMS_Core_Notification[]|bool array of notifications
633     */
634    public function getNotifications($type=0) { /* {{{ */
635        $db = $this->_dms->getDB();
636        $queryStr = "SELECT `tblNotify`.* FROM `tblNotify` ".
637         "WHERE `tblNotify`.`groupID` = ". $this->_id;
638        if($type) {
639            $queryStr .= " AND `tblNotify`.`targetType` = ". (int) $type;
640        }
641
642        $resArr = $db->getResultArray($queryStr);
643        if (is_bool($resArr) && !$resArr)
644            return false;
645
646        $notifications = array();
647        foreach ($resArr as $row) {
648            $not = new SeedDMS_Core_Notification($row["target"], $row["targetType"], $row["userID"], $row["groupID"]);
649            $not->setDMS($this->_dms);
650            array_push($notifications, $not);
651        }
652
653        return $notifications;
654    } /* }}} */
655
656} /* }}} */