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