Debian Codegraph @

lib/modules/pykotaPrinter.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 - 2026 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 printers.
24 *
25 * @package modules
26 * @author Roland Gruber
27 */
28
29 use LAM\TYPES\ConfiguredType;
30
31 /**
32 * Manages PyKota printers.
33 *
34 * @package modules
35 */
36 class pykotaPrinter 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 = ['pykotaPrinterName'];
42
43 /** @var array<string, array<string, string>>|null $printerCache cache for existing printers (array(dn => array(cn => ..., description => ...))) */
44 private ?array $printerCache = null;
45 /** @var string[]|null $groupCache printer group cache */
46 private ?array $groupCache = null;
47 /** @var array<string, string> $passThroughOptions list of pass through options: label => value */
48 private array $passThroughOptions;
49
50 /**
51 * Creates a new pykotaPrinter object.
52 *
53 * @param string $scope account type (user, group, host)
54 */
55 public function __construct($scope) {
56 $this->passThroughOptions = [
57 _('Yes') => 't',
58 _('No') => 'f',
59 ];
60 // call parent constructor
61 parent::__construct($scope);
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 public function can_manage(): bool {
68 return $this->get_scope() === 'pykotaPrinterType';
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 public function get_metaData(): array {
75 $return = [];
76 // icon
77 $return['icon'] = 'printer.svg';
78 // alias name
79 $return["alias"] = _("PyKota");
80 // this is a base module
81 $return["is_base"] = true;
82 // RDN attribute
83 $return["RDN"] = ["cn" => "high"];
84 // LDAP filter
85 $return["ldap_filter"] = ['or' => "(objectClass=pykotaPrinter)"];
86 // module dependencies
87 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
88 // managed object classes
89 $return['objectClasses'] = ['pykotaObject', 'pykotaPrinter'];
90 // managed attributes
91 $return['attributes'] = ['cn', 'description', 'pykotaMaxJobSize', 'pykotaPassThrough', 'pykotaPricePerJob', 'pykotaPricePerPage', 'pykotaPrinterName', 'uniqueMember'];
92 // help Entries
93 $return['help'] = [
94 'cn' => [
95 "Headline" => _("Printer name"), 'attr' => 'cn',
96 "Text" => _("Printer name of the printer which should be created. Valid characters are: a-z, A-Z, 0-9 and .-_ .")
97 ],
98 'description' => [
99 "Headline" => _("Description"), 'attr' => 'description',
100 "Text" => _("Printer description.")
101 ],
102 'pykotaMaxJobSize' => [
103 "Headline" => _('Maximum job size'), 'attr' => 'pykotaMaxJobSize',
104 "Text" => _('The maximum number of pages per job allowed on the printer. 0 means unlimited.')
105 ],
106 'pykotaPassThrough' => [
107 "Headline" => _('Passthrough'), 'attr' => 'pykotaPassThrough',
108 "Text" => _('In passthrough mode, users are allowed to print without any impact on their quota or account balance.')
109 ],
110 'pykotaPricePerJob' => [
111 "Headline" => _('Price per job'), 'attr' => 'pykotaPricePerJob',
112 "Text" => _('The price for each print job.')
113 ],
114 'pykotaPricePerPage' => [
115 "Headline" => _('Price per page'), 'attr' => 'pykotaPricePerPage',
116 "Text" => _('The price for each page of a print job.')
117 ],
118 'uniqueMember' => [
119 "Headline" => _('Group members'), 'attr' => 'uniqueMember',
120 "Text" => _('If this entry should be a printer group then you can set the member names here.')
121 ],
122 'uniqueMemberUpload' => [
123 "Headline" => _('Group members'), 'attr' => 'uniqueMember',
124 "Text" => _('If this entry should be a printer group then you can set the member names here.')
125 . ' ' . _('Multiple values are separated by comma.')
126 ],
127 'filter' => [
128 "Headline" => _("Filter"),
129 "Text" => _("Here you can enter a filter value. Only entries which contain the filter text will be shown.")
130 . ' ' . _('Possible wildcards are: "*" = any character, "^" = line start, "$" = line end')
131 ],
132 ];
133 // profile options
134 $profileContainer = new htmlResponsiveRow();
135 $profileContainer->add(new htmlResponsiveInputField(_('Maximum job size'), 'pykotaPrinter_pykotaMaxJobSize', '', 'pykotaMaxJobSize'));
136 $return['profile_options'] = $profileContainer;
137 $return['profile_mappings']['pykotaPrinter_pykotaMaxJobSize'] = 'pykotaMaxJobSize';
138 $return['profile_checks']['pykotaPrinter_pykotaMaxJobSize'] = [
139 'type' => 'ext_preg',
140 'regex' => 'digit',
141 'error_message' => $this->messages['pykotaMaxJobSize'][0]];
142 // upload fields
143 $return['upload_columns'] = [
144 [
145 'name' => 'pykotaPrinter_cn',
146 'description' => _('Printer name'),
147 'help' => 'cn',
148 'example' => _('printer01'),
149 'required' => true,
150 ],
151 [
152 'name' => 'pykotaPrinter_description',
153 'description' => _('Description'),
154 'help' => 'description',
155 'example' => _('Color laser printer'),
156 ],
157 [
158 'name' => 'pykotaPrinter_pykotaMaxJobSize',
159 'description' => _('Maximum job size'),
160 'help' => 'pykotaMaxJobSize',
161 'example' => '100',
162 'default' => '0',
163 ],
164 [
165 'name' => 'pykotaPrinter_pykotaPassThrough',
166 'description' => _('Passthrough'),
167 'help' => 'pykotaPassThrough',
168 'example' => _('No'),
169 'default' => _('No'),
170 'values' => _('Yes') . ', ' . _('No'),
171 ],
172 [
173 'name' => 'pykotaPrinter_pykotaPricePerJob',
174 'description' => _('Price per job'),
175 'help' => 'pykotaPricePerJob',
176 'example' => '0.01',
177 ],
178 [
179 'name' => 'pykotaPrinter_pykotaPricePerPage',
180 'description' => _('Price per page'),
181 'help' => 'pykotaPricePerPage',
182 'example' => '0.01',
183 ],
184 [
185 'name' => 'pykotaPrinter_uniqueMember',
186 'description' => _('Group members'),
187 'help' => 'uniqueMemberUpload',
188 'example' => _('printergroup1'),
189 ],
190 ];
191 // available PDF fields
192 $return['PDF_fields'] = [
193 'cn' => _('Printer name'),
194 'description' => _('Description'),
195 'pykotaMaxJobSize' => _('Maximum job size'),
196 'pykotaPassThrough' => _('Passthrough'),
197 'pykotaPricePerJob' => _('Price per job'),
198 'pykotaPricePerPage' => _('Price per page'),
199 'uniqueMember' => _('Group members'),
200 'parentUniqueMember' => _('Printer groups'),
201 ];
202 return $return;
203 }
204
205 /**
206 * {@inheritDoc}
207 */
208 protected function load_Messages(): void {
209 $this->messages['cn'][0] = ['ERROR', _('Printer name'), _('Printer name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
210 $this->messages['cn'][1] = ['ERROR', _('Account %s:') . ' pykotaPrinter_cn', _('Printer name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
211 $this->messages['cn'][2] = ['ERROR', _('Printer name'), _('Printer name already exists!')];
212 $this->messages['cn'][3] = ['ERROR', _('Account %s:') . ' pykotaPrinter_cn', _('Printer name already exists!')];
213 $this->messages['pykotaMaxJobSize'][0] = ['ERROR', _('Maximum job size'), _('Please enter a valid number.')];
214 $this->messages['pykotaMaxJobSize'][1] = ['ERROR', _('Account %s:') . ' pykotaPrinter_pykotaMaxJobSize', _('Please enter a valid number.')];
215 $this->messages['pykotaPricePerJob'][0] = ['ERROR', _('Price per job'), _('Please enter a valid number (e.g. "1.5").')];
216 $this->messages['pykotaPricePerJob'][1] = ['ERROR', _('Account %s:') . ' pykotaPrinter_pykotaPricePerJob', _('Please enter a valid number (e.g. "1.5").')];
217 $this->messages['pykotaPricePerPage'][0] = ['ERROR', _('Price per page'), _('Please enter a valid number (e.g. "1.5").')];
218 $this->messages['pykotaPricePerPage'][1] = ['ERROR', _('Account %s:') . ' pykotaPrinter_pykotaPricePerPage', _('Please enter a valid number (e.g. "1.5").')];
219 $this->messages['pykotaPassThrough'][0] = ['ERROR', _('Account %s:') . ' pykotaPrinter_pykotaPassThrough', _('Please enter "Yes" or "No".')];
220 $this->messages['uniqueMember'][0] = ['ERROR', _('Account %s:') . ' pykotaPrinter_uniqueMember', _('Unable to find a printer with name "%s".')];
221 }
222
223 /**
224 * {@inheritDoc}
225 */
226 public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void {
227 parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, self::ATTRIBUTES_TO_IGNORE_ON_COPY));
228 }
229
230 /**
231 * {@inheritDoc}
232 */
233 public function display_html_attributes(): htmlElement|array {
234 $container = new htmlResponsiveRow();
235 // cn
236 $this->addSimpleInputTextField($container, 'cn', _('Printer name'), true);
237 // job size
238 $this->addSimpleInputTextField($container, 'pykotaMaxJobSize', _('Maximum job size'));
239 // price per job
240 $this->addSimpleInputTextField($container, 'pykotaPricePerJob', _('Price per job'));
241 // price per page
242 $this->addSimpleInputTextField($container, 'pykotaPricePerPage', _('Price per page'));
243 // passthrough
244 $pykotaPassThroughOption = 'f';
245 if (!empty($this->attributes['pykotaPassThrough'][0])) {
246 $pykotaPassThroughOption = $this->attributes['pykotaPassThrough'][0];
247 }
248 $pykotaPassThroughSelect = new htmlResponsiveSelect('pykotaPassThrough', $this->passThroughOptions, [$pykotaPassThroughOption], _('Passthrough'), 'pykotaPassThrough');
249 $pykotaPassThroughSelect->setHasDescriptiveElements(true);
250 $container->add($pykotaPassThroughSelect);
251 // description
252 $this->addMultiValueInputTextField($container, 'description', _('Description'), false, null, true);
253 // printer groups
254 if (!$this->getAccountContainer()->isNewAccount) {
255 $groups = $this->getPrinterGroups();
256 $this->loadPrinterNameCache();
257 $parentPrinters = [];
258 foreach ($groups as $groupDN) {
259 $parentPrinters[] = $this->printerCache[$groupDN]['cn'];
260 }
261 if ($parentPrinters !== []) {
262 $container->addLabel(new htmlOutputText(_('Printer groups')));
263 $parentPrinterText = new htmlOutputText(implode(', ', $parentPrinters));
264 $container->addField($parentPrinterText);
265 }
266 }
267 // printer members
268 $memberLabel = new htmlOutputText(_('Group members'));
269 $container->addLabel($memberLabel);
270 $addMemberButton = new htmlAccountPageButton(static::class, 'members', 'open', 'add.svg', true);
271 $addMemberButton->setTitle(_('Add'));
272 $memberHelp = new htmlHelpLink('uniqueMember');
273 $memberTable = new htmlTable();
274 if (!empty($this->attributes['uniqueMember'][0])) {
275 $memberTable->alignment = htmlElement::ALIGN_TOP;
276 for ($i = 0; $i < count($this->attributes['uniqueMember']); $i++) {
277 $member = $this->attributes['uniqueMember'][$i];
278 if (isset($this->printerCache[$member]['cn'])) {
279 $member = $this->printerCache[$member]['cn'];
280 }
281 $memberTable->addElement(new htmlOutputText($member));
282 $delButton = new htmlButton('uniqueMemberDel_' . $i, 'del.svg', true);
283 $delButton->setTitle(_('Delete'));
284 $memberTable->addElement($delButton);
285 if ($i === count($this->attributes['uniqueMember']) - 1) {
286 $memberTable->addElement($addMemberButton);
287 $memberTable->addElement($memberHelp);
288 }
289 $memberTable->addNewLine();
290 }
291 }
292 else {
293 $memberTable->addElement($addMemberButton);
294 $memberTable->addElement($memberHelp);
295 }
296 $container->addField($memberTable);
297 return $container;
298 }
299
300 /**
301 * {@inheritDoc}
302 */
303 public function process_attributes(): array {
304 $errors = [];
305 // cn
306 if (isset($_POST['cn']) && ($_POST['cn'] != '')) {
307 if (!get_preg($_POST['cn'], 'username')) {
308 $errors[] = $this->messages['cn'][0];
309 }
310 else {
311 $this->attributes['cn'][0] = $_POST['cn'];
312 $this->attributes['pykotaPrinterName'][0] = $_POST['cn'];
313 if ((!isset($this->orig['cn'][0]) || ($this->attributes['cn'][0] != $this->orig['cn'][0]))
314 && $this->cnExists($_POST['cn'])) {
315 $errors[] = $this->messages['cn'][2];
316 }
317 }
318 }
319 else {
320 if (isset($this->attributes['cn'][0])) {
321 unset($this->attributes['cn'][0]);
322 }
323 if (isset($this->attributes['pykotaPrinterName'][0])) {
324 unset($this->attributes['pykotaPrinterName'][0]);
325 }
326 }
327 // description
328 $this->processMultiValueInputTextField('description', $errors);
329 // job size
330 $pykotaMaxJobSize = '0';
331 if (isset($_POST['pykotaMaxJobSize']) && ($_POST['pykotaMaxJobSize'] != '')) {
332 $pykotaMaxJobSize = $_POST['pykotaMaxJobSize'];
333 if (!get_preg($pykotaMaxJobSize, 'digit')) {
334 $errors[] = $this->messages['pykotaMaxJobSize'][0];
335 }
336 }
337 $this->attributes['pykotaMaxJobSize'][0] = $pykotaMaxJobSize;
338 // price per job
339 $pykotaPricePerJob = '0.0';
340 if (isset($_POST['pykotaPricePerJob']) && ($_POST['pykotaPricePerJob'] != '')) {
341 $pykotaPricePerJob = $_POST['pykotaPricePerJob'];
342 $pykotaPricePerJob = str_replace(',', '.', $pykotaPricePerJob);
343 if (!str_contains($pykotaPricePerJob, '.')) {
344 $pykotaPricePerJob .= '.0';
345 }
346 if (!get_preg($pykotaPricePerJob, 'float')) {
347 $errors[] = $this->messages['pykotaPricePerJob'][0];
348 }
349 }
350 $this->attributes['pykotaPricePerJob'][0] = $pykotaPricePerJob;
351 // price per page
352 $pykotaPricePerPage = '0.0';
353 if (isset($_POST['pykotaPricePerPage']) && ($_POST['pykotaPricePerPage'] != '')) {
354 $pykotaPricePerPage = $_POST['pykotaPricePerPage'];
355 $pykotaPricePerPage = str_replace(',', '.', $pykotaPricePerPage);
356 if (!str_contains($pykotaPricePerPage, '.')) {
357 $pykotaPricePerPage .= '.0';
358 }
359 if (!get_preg($pykotaPricePerPage, 'float')) {
360 $errors[] = $this->messages['pykotaPricePerPage'][0];
361 }
362 }
363 $this->attributes['pykotaPricePerPage'][0] = $pykotaPricePerPage;
364 // passthrough
365 $this->attributes['pykotaPassThrough'][0] = $_POST['pykotaPassThrough'];
366 // delete members
367 foreach (array_keys($_POST) as $key) {
368 if (str_starts_with($key, 'uniqueMemberDel_')) {
369 $index = substr($key, strlen('uniqueMemberDel_'));
370 unset($this->attributes['uniqueMember'][$index]);
371 $this->attributes['uniqueMember'] = array_values($this->attributes['uniqueMember']);
372 break;
373 }
374 }
375 return $errors;
376 }
377
378 /**
379 * This function will create the meta HTML code to show a page to add members.
380 *
381 * @return htmlElement HTML meta data
382 */
383 public function display_html_members(): htmlElement {
384 $return = new htmlResponsiveRow();
385 $options = [];
386 $this->loadPrinterNameCache();
387 foreach ($this->printerCache as $dn => $attrs) {
388 $label = empty($attrs['description']) ? $attrs['cn'] : $attrs['cn'] . ' (' . $attrs['description'] . ')';
389 // skip own entry
390 if (!$this->getAccountContainer()->isNewAccount && ($this->getAccountContainer()->dn_orig == $dn)) {
391 continue;
392 }
393 // skip already set members
394 if (!empty($this->attributes['uniqueMember'][0]) && in_array($dn, $this->attributes['uniqueMember'])) {
395 continue;
396 }
397 $options[$label] = $dn;
398 }
399 $size = 20;
400 if (count($options) < 20) {
401 $size = count($options);
402 }
403 $membersSelect = new htmlSelect('members', $options, [], $size);
404 $membersSelect->setHasDescriptiveElements(true);
405 $membersSelect->setMultiSelect(true);
406 $membersSelect->setTransformSingleSelect(false);
407 $return->add($membersSelect);
408 $filterGroup = new htmlGroup();
409 $filterGroup->addElement(new htmlOutputText(_('Filter')));
410 $filterInput = new htmlInputField('newFilter', null);
411 $filterInput->setCSSClasses(['maxwidth20']);
412 $filterInput->filterSelectBox('members');
413 $filterGroup->addElement($filterInput);
414 $filterGroup->addElement(new htmlHelpLink('filter'));
415 $return->add($filterGroup);
416 $return->addVerticalSpacer('2rem');
417 $buttonTable = new htmlGroup();
418 $buttonTable->addElement(new htmlAccountPageButton(static::class, 'attributes', 'addMembers', _('Add')));
419 $buttonTable->addElement(new htmlSpacer('0.5rem', null));
420 $buttonTable->addElement(new htmlAccountPageButton(static::class, 'attributes', 'cancel', _('Cancel')));
421 $return->add($buttonTable);
422 return $return;
423 }
424
425 /**
426 * Processes user input of the members page.
427 * It checks if all input values are correct and updates the associated LDAP attributes.
428 *
429 * @return string[][] list of info/error messages
430 */
431 public function process_members(): array {
432 $return = [];
433 if (isset($_POST['form_subpage_' . static::class . '_attributes_addMembers']) && isset($_POST['members'])) {
434 for ($i = 0; $i < count($_POST['members']); $i++) {
435 $this->attributes['uniqueMember'][] = $_POST['members'][$i];
436 }
437 }
438 return $return;
439 }
440
441 /**
442 * {@inheritDoc}
443 */
444 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
445 $messages = [];
446 $this->loadPrinterNameCache();
447 for ($i = 0; $i < count($rawAccounts); $i++) {
448 // add object classes
449 if (!in_array('pykotaPrinter', $partialAccounts[$i]['objectClass'])) {
450 $partialAccounts[$i]['objectClass'][] = 'pykotaPrinter';
451 }
452 if (!in_array('pykotaObject', $partialAccounts[$i]['objectClass'])) {
453 $partialAccounts[$i]['objectClass'][] = 'pykotaObject';
454 }
455 // cn
456 if (!empty($rawAccounts[$i][$ids['pykotaPrinter_cn']])) {
457 if (!get_preg($rawAccounts[$i][$ids['pykotaPrinter_cn']], 'username')) {
458 $errMsg = $this->messages['cn'][1];
459 $errMsg[] = [$i];
460 $messages[] = $errMsg;
461 }
462 elseif ($this->cnExists($rawAccounts[$i][$ids['pykotaPrinter_cn']])) {
463 $errMsg = $this->messages['cn'][3];
464 $errMsg[] = [$i];
465 $messages[] = $errMsg;
466 }
467 else {
468 $partialAccounts[$i]['cn'] = $rawAccounts[$i][$ids['pykotaPrinter_cn']];
469 $partialAccounts[$i]['pykotaPrinterName'] = $rawAccounts[$i][$ids['pykotaPrinter_cn']];
470 }
471 }
472 // description
473 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'pykotaPrinter_description', 'description');
474 // job size
475 $partialAccounts[$i]['pykotaMaxJobSize'] = '0.0';
476 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'pykotaPrinter_pykotaMaxJobSize', 'pykotaMaxJobSize',
477 'digit', $this->messages['pykotaMaxJobSize'][1], $messages);
478 // price per job
479 if (!empty($rawAccounts[$i][$ids['pykotaPrinter_pykotaPricePerJob']])) {
480 $pykotaPricePerJob = $rawAccounts[$i][$ids['pykotaPrinter_pykotaPricePerJob']];
481 $pykotaPricePerJob = str_replace(',', '.', $pykotaPricePerJob);
482 if (!str_contains($pykotaPricePerJob, '.')) {
483 $pykotaPricePerJob .= '.0';
484 }
485 if (!get_preg($pykotaPricePerJob, 'float')) {
486 $errMsg = $this->messages['pykotaPricePerJob'][1];
487 $errMsg[] = [$i];
488 $messages[] = $errMsg;
489 }
490 else {
491 $partialAccounts[$i]['pykotaPricePerJob'] = $pykotaPricePerJob;
492 }
493 }
494 else {
495 $partialAccounts[$i]['pykotaPricePerJob'] = '0.0';
496 }
497 // price per page
498 if (!empty($rawAccounts[$i][$ids['pykotaPrinter_pykotaPricePerPage']])) {
499 $pykotaPricePerPage = $rawAccounts[$i][$ids['pykotaPrinter_pykotaPricePerPage']];
500 $pykotaPricePerPage = str_replace(',', '.', $pykotaPricePerPage);
501 if (!str_contains($pykotaPricePerPage, '.')) {
502 $pykotaPricePerPage .= '.0';
503 }
504 if (!get_preg($pykotaPricePerPage, 'float')) {
505 $errMsg = $this->messages['pykotaPricePerPage'][1];
506 $errMsg[] = [$i];
507 $messages[] = $errMsg;
508 }
509 else {
510 $partialAccounts[$i]['pykotaPricePerPage'] = $pykotaPricePerPage;
511 }
512 }
513 else {
514 $partialAccounts[$i]['pykotaPricePerPage'] = '0.0';
515 }
516 // passthrough
517 if (!empty($rawAccounts[$i][$ids['pykotaPrinter_pykotaPassThrough']])) {
518 if (isset($this->passThroughOptions[$rawAccounts[$i][$ids['pykotaPrinter_pykotaPassThrough']]])) {
519 $partialAccounts[$i]['pykotaPassThrough'] = $this->passThroughOptions[$rawAccounts[$i][$ids['pykotaPrinter_pykotaPassThrough']]];
520 }
521 else {
522 $errMsg = $this->messages['pykotaPassThrough'][0];
523 $errMsg[] = [$i];
524 $messages[] = $errMsg;
525 }
526 }
527 else {
528 $partialAccounts[$i]['pykotaPassThrough'] = 'f';
529 }
530 // group members
531 if (!empty($rawAccounts[$i][$ids['pykotaPrinter_uniqueMember']])) {
532 $members = preg_split('/,[ ]*/', $rawAccounts[$i][$ids['pykotaPrinter_uniqueMember']]);
533 $memberDNs = [];
534 foreach ($members as $cn) {
535 if (empty($cn)) {
536 continue;
537 }
538 // search printer cache for cn to get DN
539 $found = false;
540 foreach ($this->printerCache as $dn => $attrs) {
541 if ($this->printerCache[$dn]['cn'] == $cn) {
542 $found = true;
543 $memberDNs[] = $dn;
544 break;
545 }
546 }
547 if (!$found) {
548 $errMsg = $this->messages['uniqueMember'][0];
549 $errMsg[] = [$i, htmlspecialchars($cn)];
550 $messages[] = $errMsg;
551 }
552 }
553 if ($memberDNs !== []) {
554 $partialAccounts[$i]['uniqueMember'] = $memberDNs;
555 }
556 }
557 }
558 return $messages;
559 }
560
561 /**
562 * {@inheritDoc}
563 */
564 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
565 $return = [];
566 $this->loadPrinterNameCache();
567 $this->addSimplePDFField($return, 'cn', _('Printer name'));
568 $this->addSimplePDFField($return, 'description', _('Description'));
569 $this->addSimplePDFField($return, 'pykotaMaxJobSize', _('Maximum job size'));
570 $this->addSimplePDFField($return, 'pykotaPricePerJob', _('Price per job'));
571 $this->addSimplePDFField($return, 'pykotaPricePerPage', _('Price per page'));
572 // passthrough
573 $passthroughOptions = array_flip($this->passThroughOptions);
574 $passthroughValue = '';
575 if (!empty($this->attributes['pykotaPassThrough'][0]) && isset($passthroughOptions[$this->attributes['pykotaPassThrough'][0]])) {
576 $passthroughValue = $passthroughOptions[$this->attributes['pykotaPassThrough'][0]];
577 }
578 $this->addPDFKeyValue($return, 'pykotaPassThrough', _('Passthrough'), $passthroughValue);
579 // members
580 if (!empty($this->attributes['uniqueMember'][0])) {
581 $members = [];
582 foreach ($this->attributes['uniqueMember'] as $member) {
583 $members[] = empty($this->printerCache[$member]['cn']) ? getAbstractDN($member) : $this->printerCache[$member]['cn'];
584 }
585 $this->addPDFKeyValue($return, 'uniqueMember', _('Group members'), implode(', ', $members));
586 }
587 // printer groups
588 $parentGroups = [];
589 $groups = $this->getPrinterGroups();
590 foreach ($groups as $group) {
591 $parentGroups[] = empty($this->printerCache[$group]['cn']) ? getAbstractDN($group) : $this->printerCache[$group]['cn'];
592 }
593 if ($parentGroups !== []) {
594 $this->addPDFKeyValue($return, 'parentUniqueMember', _('Printer groups'), implode(', ', $parentGroups));
595 }
596 return $return;
597 }
598
599 /**
600 * Returns if the given cn already exists.
601 *
602 * @param String $cn cn attribute value
603 * @return bool cn exists
604 */
605 private function cnExists($cn): bool {
606 if ($this->printerCache == null) {
607 $this->loadPrinterNameCache();
608 }
609 foreach ($this->printerCache as $attrs) {
610 if (!empty($attrs['cn']) && ($attrs['cn'] == $cn)) {
611 return true;
612 }
613 }
614 return false;
615 }
616
617 /**
618 * Loads the list of printer names into the cache.
619 */
620 private function loadPrinterNameCache(): void {
621 if ($this->printerCache != null) {
622 return;
623 }
624 $results = searchLDAPByFilter('(objectClass=pykotaPrinter)', ['cn', 'dn', 'description'], [$this->get_scope()]);
625 $this->printerCache = [];
626 foreach ($results as $result) {
627 if (isset($result['cn'][0])) {
628 $this->printerCache[$result['dn']]['cn'] = $result['cn'][0];
629 }
630 if (isset($result['description'][0])) {
631 $this->printerCache[$result['dn']]['description'] = $result['description'][0];
632 }
633 }
634 }
635
636 /**
637 * Returns the printer group memberships.
638 *
639 * @return string[] DNs of parent groups
640 */
641 private function getPrinterGroups() {
642 if ($this->groupCache !== null) {
643 return $this->groupCache;
644 }
645 $results = searchLDAPByFilter('(&(objectClass=pykotaPrinter)(uniqueMember=' . $this->getAccountContainer()->dn_orig . '))', ['dn'], [$this->get_scope()]);
646 $this->groupCache = [];
647 foreach ($results as $result) {
648 $this->groupCache[] = $result['dn'];
649 }
650 return $this->groupCache;
651 }
652
653 /**
654 * @inheritDoc
655 */
656 public function getListAttributeDescriptions(ConfiguredType $type): array {
657 return [
658 'cn' => _('Printer name'),
659 'description' => _('Description'),
660 'pykotamaxjobsize' => _('Maximum job size'),
661 'pykotapassthrough' => _('Passthrough'),
662 'pykotapriceperjob' => _('Price per job'),
663 'pykotapriceperpage' => _('Price per page'),
664 'uniquemember' => _('Group members'),
665 'parentuniquemember' => _('Printer groups'),
666 ];
667 }
668
669 }