Debian Codegraph @

lib/modules/pykotaBillingCode.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2013 - 2024 Roland Gruber
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /**
23 * Manages PyKota billing codes.
24 *
25 * @package modules
26 * @author Roland Gruber
27 */
28
29 use LAM\TYPES\ConfiguredType;
30
31 /**
32 * Manages PyKota billing codes.
33 *
34 * @package modules
35 */
36 class pykotaBillingCode extends baseModule {
37
38 /**
39 * These attributes will be ignored by default if a new account is copied from an existing one.
40 */
41 private const ATTRIBUTES_TO_IGNORE_ON_COPY = ['pykotaBalance', 'pykotaPageCounter', 'pykotaBillingCode'];
42
43 /** @var array<string, string>|null $codeCache cache for existing codes (array(dn1 => pykotaBillingCode1, dn2 => pykotaBillingCode2)) */
44 private ?array $codeCache = null;
45
46 /**
47 * {@inheritDoc}
48 */
49 public function can_manage(): bool {
50 return $this->get_scope() === 'pykotaBillingCodeType';
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 public function get_metaData(): array {
57 $return = [];
58 // icon
59 $return['icon'] = 'printer.svg';
60 // alias name
61 $return["alias"] = _("PyKota");
62 // this is a base module
63 $return["is_base"] = true;
64 // RDN attribute
65 $return["RDN"] = ["cn" => "high"];
66 // LDAP filter
67 $return["ldap_filter"] = ['or' => "(objectClass=pykotaBilling)"];
68 // module dependencies
69 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
70 // managed object classes
71 $return['objectClasses'] = ['pykotaObject', 'pykotaBilling'];
72 // managed attributes
73 $return['attributes'] = ['cn', 'pykotaBillingCode', 'description', 'pykotaBalance', 'pykotaPageCounter'];
74 // help Entries
75 $return['help'] = [
76 'pykotaBillingCode' => [
77 "Headline" => _("Billing code"), 'attr' => 'pykotaBillingCode',
78 "Text" => _("Billing code name which should be created. Valid characters are: a-z, A-Z, 0-9 and .-_ .")
79 ],
80 'description' => [
81 "Headline" => _("Description"), 'attr' => 'description',
82 "Text" => _("Billing code description.")
83 ],
84 'pykotaBalance' => [
85 "Headline" => _('Balance'), 'attr' => 'pykotaBalance',
86 "Text" => _('Used balance for the billing code.')
87 ],
88 'pykotaPageCounter' => [
89 "Headline" => _('Page count'), 'attr' => 'pykotaPageCounter',
90 "Text" => _('Number of pages printed with this billing code.')
91 ],
92 'reset' => [
93 "Headline" => _('Reset'), 'attr' => 'pykotaBalance, pykotaPageCounter',
94 "Text" => _('Resets the billing code\'s balance and page counter to 0.')
95 ],
96 ];
97 // upload fields
98 $return['upload_columns'] = [
99 [
100 'name' => 'pykotaBillingCode_pykotaBillingCode',
101 'description' => _('Printer name'),
102 'help' => 'cn',
103 'example' => _('billingCode01'),
104 'required' => true,
105 'unique' => true,
106 ],
107 [
108 'name' => 'pykotaBillingCode_description',
109 'description' => _('Description'),
110 'help' => 'description',
111 ],
112 ];
113 // available PDF fields
114 $return['PDF_fields'] = [
115 'pykotaBillingCode' => _('Billing code'),
116 'description' => _('Description'),
117 'pykotaBalance' => _('Balance'),
118 'pykotaPageCounter' => _('Page count'),
119 ];
120 return $return;
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 protected function load_Messages(): void {
127 $this->messages['pykotaBillingCode'][0] = ['ERROR', _('Billing code'), _('Billing code contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
128 $this->messages['pykotaBillingCode'][1] = ['ERROR', _('Account %s:') . ' pykotaBillingCode_cn', _('Billing code contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
129 $this->messages['pykotaBillingCode'][2] = ['ERROR', _('Billing code'), _('Billing code already exists!')];
130 $this->messages['pykotaBillingCode'][3] = ['ERROR', _('Account %s:') . ' pykotaBillingCode_cn', _('Billing code already exists!')];
131 }
132
133 /**
134 * {@inheritDoc}
135 */
136 public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
137 parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, self::ATTRIBUTES_TO_IGNORE_ON_COPY));
138 }
139
140 /**
141 * {@inheritDoc}
142 */
143 public function display_html_attributes(): htmlElement|array {
144 $container = new htmlResponsiveRow();
145 // pykotaBillingCode
146 $this->addSimpleInputTextField($container, 'pykotaBillingCode', _('Billing code'), true);
147 // balance
148 $container->addLabel(new htmlOutputText(_('Balance')));
149 $pykotaBalance = '';
150 if (isset($this->attributes['pykotaBalance'][0])) {
151 $pykotaBalance = $this->attributes['pykotaBalance'][0];
152 }
153 $balanceGroup = new htmlGroup();
154 $balanceGroup->addElement(new htmlOutputText($pykotaBalance));
155 $balanceGroup->addElement(new htmlHelpLink('pykotaBalance'));
156 $container->addField($balanceGroup);
157 // page count
158 $container->addLabel(new htmlOutputText(_('Page count')));
159 $pykotaPageCounter = '';
160 if (isset($this->attributes['pykotaPageCounter'][0])) {
161 $pykotaPageCounter = $this->attributes['pykotaPageCounter'][0];
162 }
163 $pageCounterGroup = new htmlGroup();
164 $pageCounterGroup->addElement(new htmlOutputText($pykotaPageCounter));
165 $pageCounterGroup->addElement(new htmlHelpLink('pykotaPageCounter'));
166 $container->addField($pageCounterGroup);
167 // description
168 $this->addSimpleInputTextField($container, 'description', _('Description'), false, null, true);
169 // reset
170 $container->addVerticalSpacer('2rem');
171 $resetGroup = new htmlGroup();
172 $resetGroup->addElement(new htmlButton('resetCounters', _('Reset')));
173 $resetGroup->addElement(new htmlHelpLink('reset'));
174 $container->add($resetGroup, 12, 12, 12, 'text-center');
175 return $container;
176 }
177
178 /**
179 * {@inheritDoc}
180 */
181 public function process_attributes(): array {
182 $errors = [];
183 // pykotaBillingCode
184 if (isset($_POST['pykotaBillingCode']) && ($_POST['pykotaBillingCode'] != '')) {
185 if (!get_preg($_POST['pykotaBillingCode'], 'username')) {
186 $errors[] = $this->messages['pykotaBillingCode'][0];
187 }
188 else {
189 $this->attributes['pykotaBillingCode'][0] = $_POST['pykotaBillingCode'];
190 $this->attributes['cn'][0] = $_POST['pykotaBillingCode'];
191 if ((!isset($this->orig['pykotaBillingCode'][0]) || ($this->attributes['pykotaBillingCode'][0] != $this->orig['pykotaBillingCode'][0]))
192 && $this->codeExists($_POST['pykotaBillingCode'])) {
193 $errors[] = $this->messages['pykotaBillingCode'][2];
194 }
195 }
196 }
197 else {
198 if (isset($this->attributes['cn'][0])) {
199 unset($this->attributes['cn'][0]);
200 }
201 if (isset($this->attributes['pykotaBillingCode'][0])) {
202 unset($this->attributes['pykotaBillingCode'][0]);
203 }
204 }
205 // description
206 $this->attributes['description'][0] = $_POST['description'];
207 // reset
208 if (isset($_POST['resetCounters'])) {
209 $this->attributes['pykotaBalance'][0] = '0.0';
210 $this->attributes['pykotaPageCounter'][0] = '0';
211 }
212 return $errors;
213 }
214
215 /**
216 * {@inheritDoc}
217 */
218 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
219 $messages = [];
220 $this->loadCodeCache();
221 for ($i = 0; $i < count($rawAccounts); $i++) {
222 // add object classes
223 if (!in_array('pykotaBilling', $partialAccounts[$i]['objectClass'])) {
224 $partialAccounts[$i]['objectClass'][] = 'pykotaBilling';
225 }
226 if (!in_array('pykotaObject', $partialAccounts[$i]['objectClass'])) {
227 $partialAccounts[$i]['objectClass'][] = 'pykotaObject';
228 }
229 // pykotaBillingCode
230 if (!get_preg($rawAccounts[$i][$ids['pykotaBillingCode_pykotaBillingCode']], 'username')) {
231 $errMsg = $this->messages['pykotaBillingCode'][1];
232 $errMsg[] = [$i];
233 $messages[] = $errMsg;
234 }
235 elseif ($this->codeExists($rawAccounts[$i][$ids['pykotaBillingCode_pykotaBillingCode']])) {
236 $errMsg = $this->messages['pykotaBillingCode'][3];
237 $errMsg[] = [$i];
238 $messages[] = $errMsg;
239 }
240 else {
241 $partialAccounts[$i]['cn'] = $rawAccounts[$i][$ids['pykotaBillingCode_pykotaBillingCode']];
242 $partialAccounts[$i]['pykotaBillingCode'] = $rawAccounts[$i][$ids['pykotaBillingCode_pykotaBillingCode']];
243 }
244 // description
245 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'pykotaBillingCode_description', 'description');
246 // balance
247 $partialAccounts[$i]['pykotaBalance'] = '0.0';
248 // page count
249 $partialAccounts[$i]['pykotaPageCounter'] = '0';
250 }
251 return $messages;
252 }
253
254 /**
255 * {@inheritDoc}
256 */
257 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
258 $return = [];
259 $this->loadCodeCache();
260 $this->addSimplePDFField($return, 'pykotaBillingCode', _('Billing code'));
261 $this->addSimplePDFField($return, 'description', _('Description'));
262 $this->addSimplePDFField($return, 'pykotaBalance', _('Balance'));
263 $this->addSimplePDFField($return, 'pykotaPageCounter', _('Page count'));
264 return $return;
265 }
266
267 /**
268 * Returns if the given billing code already exists.
269 *
270 * @param String $code pykotaBillingCode attribute value
271 * @return bool pykotaBillingCode exists
272 */
273 private function codeExists($code): bool {
274 if ($this->codeCache == null) {
275 $this->loadCodeCache();
276 }
277 foreach ($this->codeCache as $bCode) {
278 if (!empty($bCode) && ($bCode == $code)) {
279 return true;
280 }
281 }
282 return false;
283 }
284
285 /**
286 * Loads the list of billing code names into the cache.
287 */
288 private function loadCodeCache(): void {
289 if ($this->codeCache !== null) {
290 return;
291 }
292 $results = searchLDAPByFilter('(objectClass=pykotaBilling)', ['pykotaBillingCode', 'dn'], [$this->get_scope()]);
293 $this->codeCache = [];
294 foreach ($results as $result) {
295 if (isset($result['pykotabillingcode'][0])) {
296 $this->codeCache[$result['dn']] = $result['pykotabillingcode'][0];
297 }
298 }
299 }
300
301 /**
302 * @inheritDoc
303 */
304 public function getListAttributeDescriptions(ConfiguredType $type): array {
305 return [
306 "description" => _('Description'),
307 "cn" => _('Billing code'),
308 'pykotabalance' => _('Balance'),
309 'pykotapagecounter' => _('Page count'),
310 ];
311 }
312
313 }