Skip to content

Commit

Permalink
add type and model dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Aug 22, 2024
1 parent d73f61f commit 30a3daa
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 4 deletions.
51 changes: 51 additions & 0 deletions front/asset/ruledictionarymodel.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = 'RuleDictionary' . $classname . 'ModelCollection';
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.form.php");
51 changes: 51 additions & 0 deletions front/asset/ruledictionarymodel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = 'RuleDictionary' . $classname . 'ModelCollection';
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.php");
51 changes: 51 additions & 0 deletions front/asset/ruledictionarytype.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = 'RuleDictionary' . $classname . 'TypeCollection';
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.form.php");
51 changes: 51 additions & 0 deletions front/asset/ruledictionarytype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = 'RuleDictionary' . $classname . 'TypeCollection';
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.php");
4 changes: 2 additions & 2 deletions src/Glpi/Asset/AssetDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ public function getAssetClassName(bool $with_namespace = true): string
* Get the definition's concrete asset model class name.
*
* @param bool $with_namespace
* @return string
* @return class-string<CommonDBTM>
*/
public function getAssetModelClassName(bool $with_namespace = true): string
{
Expand All @@ -867,7 +867,7 @@ public function getAssetModelClassName(bool $with_namespace = true): string
* Get the definition's concrete asset type class name.
*
* @param bool $with_namespace
* @return string
* @return class-string<CommonDBTM>
*/
public function getAssetTypeClassName(bool $with_namespace = true): string
{
Expand Down
123 changes: 123 additions & 0 deletions src/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ private function boostrapConcreteClass(AssetDefinition $definition): void
public function autoloadAssetClass(string $classname): void
{
$patterns = [
'/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)ModelCollection$/' => 'loadConcreteModelDictionaryCollectionClass',
'/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)TypeCollection$/' => 'loadConcreteTypeDictionaryCollectionClass',
'/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)Model$/' => 'loadConcreteModelDictionaryClass',
'/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)Type$/' => 'loadConcreteTypeDictionaryClass',
'/^Glpi\\\CustomAsset\\\([A-Za-z]+)Model$/' => 'loadConcreteModelClass',
'/^Glpi\\\CustomAsset\\\([A-Za-z]+)Type$/' => 'loadConcreteTypeClass',
'/^Glpi\\\CustomAsset\\\([A-Za-z]+)$/' => 'loadConcreteClass',
Expand Down Expand Up @@ -436,4 +440,123 @@ final class {$definition->getAssetTypeClassName(false)} extends AssetType {
$reflected_class = new ReflectionClass($definition->getAssetTypeClassName());
$reflected_class->setStaticPropertyValue('definition', $definition);
}

private function loadConcreteModelDictionaryClass(AssetDefinition $definition): void
{
eval(<<<PHP
namespace Glpi\\CustomAsset;
use RuleDictionnaryDropdown;
final class RuleDictionary{$definition->getAssetModelClassName(false)} extends RuleDictionnaryDropdown
{
public function getCriterias()
{
static \$criterias = [];
if (count(\$criterias)) {
return \$criterias;
}
\$criterias['name']['field'] = 'name';
\$criterias['name']['name'] = _n('Model', 'Models', 1);
\$criterias['name']['table'] = {$definition->getAssetModelClassName()::getTable()};
\$criterias['manufacturer']['field'] = 'name';
\$criterias['manufacturer']['name'] = Manufacturer::getTypeName(1);
\$criterias['manufacturer']['table'] = 'glpi_manufacturers';
return \$criterias;
}
public function getActions()
{
\$actions = [];
\$actions['name']['name'] = _n('Model', 'Models', 1);
\$actions['name']['force_actions'] = ['append_regex_result', 'assign', 'regex_result'];
return \$actions;
}
PHP
);
}

private function loadConcreteTypeDictionaryClass(AssetDefinition $definition): void
{
eval(<<<PHP
namespace Glpi\\CustomAsset;
use RuleDictionnaryDropdown;
final class RuleDictionary{$definition->getAssetTypeClassName(false)} extends RuleDictionnaryDropdown
{
public function getCriterias()
{
static \$criterias = [];
if (count(\$criterias)) {
return \$criterias;
}
\$criterias['name']['field'] = 'name';
\$criterias['name']['name'] = _n('Type', 'Types', 1);
\$criterias['name']['table'] = {$definition->getAssetTypeClassName()::getTable()};
return \$criterias;
}
public function getActions()
{
\$actions = [];
\$actions['name']['name'] = _n('Type', 'Types', 1);
\$actions['name']['force_actions'] = ['append_regex_result', 'assign','regex_result'];
return \$actions;
}
}
PHP
);
}

private function loadConcreteModelDictionaryCollectionClass(AssetDefinition $definition): void
{
eval(<<<PHP
namespace Glpi\\CustomAsset;
use RuleDictionnaryDropdownCollection;
final class RuleDictionary{$definition->getAssetModelClassName(false)}Collection extends RuleDictionnaryDropdownCollection
{
public \$item_table = {$definition->getAssetModelClassName()::getTable()};
public \$menu_option = "model.{$definition->fields['system_name']}";
public function getTitle()
{
return sprintf(__('Dictionary of % models'), {$definition->getAssetModelClassName()}::getTypeName());
}
}
PHP
);
}

private function loadConcreteTypeDictionaryCollectionClass(AssetDefinition $definition): void
{
eval(<<<PHP
namespace Glpi\\CustomAsset;
use RuleDictionnaryDropdownCollection;
final class RuleDictionary{$definition->getAssetTypeClassName(false)}Collection extends RuleDictionnaryDropdownCollection
{
public \$item_table = {$definition->getAssetTypeClassName()::getTable()};
public \$menu_option = "model.{$definition->fields['system_name']}";
public function getTitle()
{
return sprintf(__('Dictionary of % types'), {$definition->getAssetTypeClassName()}::getTypeName());
}
}
PHP
);
}
}
Loading

0 comments on commit 30a3daa

Please sign in to comment.