<?php
/*

  This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
  Copyright (C) 2004 - 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
*/

/**
 * Provides MAC addresses for hosts.
 *
 * @package modules
 * @author Roland Gruber
 */

use LAM\TYPES\ConfiguredType;

/**
 * Provides MAC addresses for hosts.
 *
 * @package modules
 */
class ieee802device 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 = ['macAddress'];

	/**
	 * {@inheritDoc}
	 */
	public function can_manage(): bool {
		return $this->get_scope() === 'host';
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_metaData(): array {
		$return = [];
		// icon
		$return['icon'] = 'network.svg';
		// alias name
		$return["alias"] = _("MAC address");
		// module dependencies
		$return['dependencies'] = ['depends' => [], 'conflicts' => []];
		// managed object classes
		$return['objectClasses'] = ['ieee802Device'];
		// managed attributes
		$return['attributes'] = ['macAddress'];
		// help Entries
		$return['help'] = [
			'macAddress' => [
				"Headline" => _("MAC address"), 'attr' => 'macAddress',
				"Text" => _("This is the MAC address of the network card of the device (e.g. 00:01:02:DE:EF:18).")
			],
			'macList' => [
				"Headline" => _("MAC address list"), 'attr' => 'macAddress',
				"Text" => _("This is a comma separated list of MAC addresses.")
			]];
		// upload fields
		$return['upload_columns'] = [
			[
				'name' => 'ieee802Device_mac',
				'description' => _('MAC address'),
				'help' => 'macList',
				'example' => '00:01:02:DE:EF:18'
			]
		];
		// available PDF fields
		$return['PDF_fields'] = [
			'macAddress' => _('MAC addresses')
		];
		return $return;
	}

	/**
	 * {@inheritDoc}
	 */
	protected function load_Messages(): void {
		$this->messages['macAddress'][0] = ['ERROR', 'MAC address is invalid!'];  // third array value is set dynamically
		$this->messages['macAddress'][1] = ['ERROR', _('Account %s:') . ' ieee802Device_mac', 'MAC address is invalid!'];
	}

	/**
	 * {@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() {
		$return = new htmlResponsiveRow();
		$this->addMultiValueInputTextField($return, 'macAddress', _('MAC address'), false, 17);
		return $return;
	}

	/**
	 * {@inheritDoc}
	 */
	public function process_attributes(): array {
		$errors = [];
		$this->processMultiValueInputTextField('macAddress', $errors, 'macAddress');
		// check if MAC is already assigned
		if (!empty($this->attributes['macAddress'])) {
			foreach ($this->attributes['macAddress'] as $macAddress) {
				if (!empty($this->orig['macAddress']) && in_array($macAddress, $this->orig['macAddress'])) {
					// check only new MAC addresses
					continue;
				}
				$result = searchLDAPByAttribute('macAddress', $macAddress, 'ieee802Device', ['dn'], ['host']);
				if (!empty($result)) {
					$errors[] = ['WARN', _('This MAC address is already in use.'), getAbstractDN($result[0]['dn'])];
				}
			}
		}
		return $errors;
	}

	/**
	 * {@inheritDoc}
	 */
	public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
		$messages = [];
		for ($i = 0; $i < count($rawAccounts); $i++) {
			// add object class
			if (!in_array("ieee802Device", $partialAccounts[$i]['objectClass'])) {
				$partialAccounts[$i]['objectClass'][] = "ieee802Device";
			}
			// add MACs
			$this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'ieee802Device_mac', 'macAddress', 'macAddress', $this->messages['macAddress'][1], $messages, '/,[ ]*/');
		}
		return $messages;
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_pdfEntries(array $pdfKeys, string $typeId): array {
		$return = [];
		$this->addSimplePDFField($return, 'macAddress', _('MAC addresses'));
		return $return;
	}

	/**
	 * @inheritDoc
	 */
	public function getListAttributeDescriptions(ConfiguredType $type): array {
		return [
			'macaddress' => _('MAC addresses')
		];
	}

}
