Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.97% covered (warning)
77.97%
46 / 59
61.54% covered (warning)
61.54%
8 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SeedDMS_Core_KeywordCategory
77.97% covered (warning)
77.97%
46 / 59
61.54% covered (warning)
61.54%
8 / 13
28.66
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
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
 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
 getOwner
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 setName
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 setOwner
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
 getKeywordLists
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 countKeywordLists
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 editKeywordList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addKeywordList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 removeKeywordList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 remove
66.67% covered (warning)
66.67%
8 / 12
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2declare(strict_types=1);
3
4/**
5 * Implementation of keyword categories 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 keyword category 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_KeywordCategory { /* {{{ */
28    /**
29     * @var integer $_id id of keyword category
30     * @access protected
31     */
32    protected $_id;
33
34    /**
35     * @var integer $_ownerID id of user who is the owner
36     * @access protected
37     */
38    protected $_ownerID;
39
40    /**
41     * @var object $_owner user who is the owner
42     * @access protected
43     */
44    protected $_owner;
45
46    /**
47     * @var string $_name name of category
48     * @access protected
49     */
50    protected $_name;
51
52    /**
53     * @var SeedDMS_Core_DMS $_dms reference to dms this category belongs to
54     * @access protected
55     */
56    protected $_dms;
57
58    /**
59     * SeedDMS_Core_KeywordCategory constructor.
60     *
61     * @param $id
62     * @param $ownerID
63     * @param $name
64     */
65    public function __construct($id, $ownerID, $name) { /* {{{ */
66        $this->_id = $id;
67        $this->_name = $name;
68        $this->_ownerID = $ownerID;
69        $this->_owner = null;
70        $this->_dms = null;
71    } /* }}} */
72
73    /**
74     * @param SeedDMS_Core_DMS $dms
75     */
76    public function setDMS($dms) { /* {{{ */
77        $this->_dms = $dms;
78    } /* }}} */
79
80    /**
81     * Return internal id of keyword category
82     *
83     * @return int
84     */
85    public function getID() { return $this->_id; }
86
87    /**
88     * Return name of keyword category
89     *
90     * @return string
91     */
92    public function getName() { return $this->_name; }
93
94    /**
95     * Return owner of keyword category
96     *
97     * @return bool|SeedDMS_Core_User
98     */
99    public function getOwner() { /* {{{ */
100        if (!isset($this->_owner))
101            $this->_owner = $this->_dms->getUser($this->_ownerID);
102        return $this->_owner;
103    } /* }}} */
104
105    /**
106     * Set name of keyword category
107     *
108     * @param $newName
109     * @return bool
110     */
111    public function setName($newName) { /* {{{ */
112        $newName = trim($newName);
113        if (!$newName)
114            return false;
115
116        $db = $this->_dms->getDB();
117
118        $queryStr = "UPDATE `tblKeywordCategories` SET `name` = ".$db->qstr($newName)." WHERE `id` = ". $this->_id;
119        if (!$db->getResult($queryStr))
120            return false;
121
122        $this->_name = $newName;
123        return true;
124    } /* }}} */
125
126    /**
127     * Set owner of keyword category
128     *
129     * @param SeedDMS_Core_User $user
130     * @return bool
131     */
132    public function setOwner($user) { /* {{{ */
133        if (!$user || !$user->isType('user'))
134            return false;
135
136        $db = $this->_dms->getDB();
137
138        $queryStr = "UPDATE `tblKeywordCategories` SET `owner` = " . $user->getID() . " WHERE `id` = " . $this->_id;
139        if (!$db->getResult($queryStr))
140            return false;
141
142        $this->_ownerID = $user->getID();
143        $this->_owner = $user;
144        return true;
145    } /* }}} */
146
147    /**
148     * Get list of keywords in category
149     *
150     * @return array keywords of category
151     */
152    public function getKeywordLists() { /* {{{ */
153        $db = $this->_dms->getDB();
154
155        $queryStr = "SELECT * FROM `tblKeywords` WHERE `category` = " . $this->_id . " order by `keywords`";
156        return $db->getResultArray($queryStr);
157    }
158
159    /**
160     * Return number of keywords in category
161     *
162     * @return integer number of keywords in this list
163     */
164    public function countKeywordLists() { /* {{{ */
165        $db = $this->_dms->getDB();
166
167        $queryStr = "SELECT COUNT(*) as `c` FROM `tblKeywords` where `category`=".$this->_id;
168        $resArr = $db->getResultArray($queryStr);
169        if (is_bool($resArr) && !$resArr)
170            return false;
171
172        return $resArr[0]['c'];
173    } /* }}} */
174
175    /**
176     * Change a keyword
177     *
178     * This method identifies the keyword by its id and also ensures that
179     * the keyword belongs to the category, though the keyword id would be
180     * sufficient to uniquely identify the keyword.
181     *
182     * @param $kid
183     * @param $keywords
184     * @return bool
185     */
186    public function editKeywordList($kid, $keywords) { /* {{{ */
187        $db = $this->_dms->getDB();
188
189        $queryStr = "UPDATE `tblKeywords` SET `keywords` = ".$db->qstr($keywords)." WHERE `id` = ".(int) $kid." AND `category`=".$this->_id;
190        return $db->getResult($queryStr);
191    } /* }}} */
192
193    /**
194     * Add a new keyword to category
195     *
196     * @param $keywords new keyword
197     * @return bool
198     */
199    public function addKeywordList($keywords) { /* {{{ */
200        $db = $this->_dms->getDB();
201
202        $queryStr = "INSERT INTO `tblKeywords` (`category`, `keywords`) VALUES (" . $this->_id . ", ".$db->qstr($keywords).")";
203        return $db->getResult($queryStr);
204    } /* }}} */
205
206    /**
207     * Remove keyword
208     *
209     * This method identifies the keyword by its id and also ensures that
210     * the keyword belongs to the category, though the keyword id would be
211     * sufficient to uniquely identify the keyword.
212     *
213     * @param $kid
214     * @return bool
215     */
216    public function removeKeywordList($kid) { /* {{{ */
217        $db = $this->_dms->getDB();
218
219        $queryStr = "DELETE FROM `tblKeywords` WHERE `id` = ".(int) $kid." AND `category`=".$this->_id;
220        return $db->getResult($queryStr);
221    } /* }}} */
222
223    /**
224     * Delete all keywords of category and category itself
225     *
226     * @return bool
227     */
228    public function remove() { /* {{{ */
229        $db = $this->_dms->getDB();
230
231        $db->startTransaction();
232        $queryStr = "DELETE FROM `tblKeywords` WHERE `category` = " . $this->_id;
233        if (!$db->getResult($queryStr)) {
234            $db->rollbackTransaction();
235            return false;
236        }
237
238        $queryStr = "DELETE FROM `tblKeywordCategories` WHERE `id` = " . $this->_id;
239        if (!$db->getResult($queryStr)) {
240            $db->rollbackTransaction();
241            return false;
242        }
243
244        $db->commitTransaction();
245        return true;
246    } /* }}} */
247} /* }}} */