diff --git a/.travis.yml b/.travis.yml index c5435a5b..7cc0a2bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ php: services: - mysql - env: global: - DB=mysql diff --git a/composer.json b/composer.json index 7f281522..e6dc0ce2 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "require": { - "php": "^7.2" + "php": "^7.2", + "symfony/yaml": "^4.4" }, "require-dev": { "atoum/atoum": "^3.1", diff --git a/composer.lock b/composer.lock index 3531ded2..fb67b42b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,130 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "60eb7d1cf0f21f4b53c8aaeea526703a", - "packages": [], + "content-hash": "8d2c1b3a5503cf3cfd1737c73281e2db", + "packages": [ + { + "name": "symfony/polyfill-ctype", + "version": "v1.17.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d", + "reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.17-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "time": "2020-06-06T08:46:27+00:00" + }, + { + "name": "symfony/yaml", + "version": "v4.4.15", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "c7885964b1eceb70b0981556d0a9b01d2d97c8d1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/c7885964b1eceb70b0981556d0a9b01d2d97c8d1", + "reference": "c7885964b1eceb70b0981556d0a9b01d2d97c8d1", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/console": "<3.4" + }, + "require-dev": { + "symfony/console": "^3.4|^4.0|^5.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.4-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2020-09-27T03:36:23+00:00" + } + ], "packages-dev": [ { "name": "atoum/atoum", @@ -547,6 +669,7 @@ ], "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", "homepage": "https://github.com/container-interop/container-interop", + "abandoned": "psr/container", "time": "2017-02-14T19:40:03+00:00" }, { @@ -856,7 +979,7 @@ "time": "2017-05-10T09:20:27+00:00" }, { - "name": "natxet/CssMin", + "name": "natxet/cssmin", "version": "v3.0.6", "source": { "type": "git", @@ -942,6 +1065,7 @@ "javascript", "minification" ], + "abandoned": true, "time": "2015-03-25T10:11:08+00:00" }, { @@ -1166,20 +1290,6 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-05-30T20:06:45+00:00" }, { @@ -1250,20 +1360,6 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-05-20T08:37:50+00:00" }, { @@ -1372,20 +1468,6 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-05-30T18:50:54+00:00" }, { @@ -1435,98 +1517,8 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-03-27T16:54:36+00:00" }, - { - "name": "symfony/polyfill-ctype", - "version": "v1.17.1", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d", - "reference": "2edd75b8b35d62fd3eeabba73b26b8f1f60ce13d", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "suggest": { - "ext-ctype": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.17-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Ctype\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for ctype functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "ctype", - "polyfill", - "portable" - ], - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2020-06-06T08:46:27+00:00" - }, { "name": "symfony/polyfill-mbstring", "version": "v1.17.1", @@ -1588,20 +1580,6 @@ "portable", "shim" ], - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-06-06T08:46:27+00:00" }, { @@ -1664,20 +1642,6 @@ "portable", "shim" ], - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-06-06T08:46:27+00:00" }, { @@ -1744,20 +1708,6 @@ "portable", "shim" ], - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-06-06T08:46:27+00:00" }, { @@ -1807,20 +1757,6 @@ ], "description": "Symfony Process Component", "homepage": "https://symfony.com", - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], "time": "2020-05-30T20:06:45+00:00" }, { @@ -1880,79 +1816,6 @@ "standards" ], "time": "2019-10-14T12:27:06+00:00" - }, - { - "name": "symfony/yaml", - "version": "v4.4.10", - "source": { - "type": "git", - "url": "https://github.com/symfony/yaml.git", - "reference": "c2d2cc66e892322cfcc03f8f12f8340dbd7a3f8a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/c2d2cc66e892322cfcc03f8f12f8340dbd7a3f8a", - "reference": "c2d2cc66e892322cfcc03f8f12f8340dbd7a3f8a", - "shasum": "" - }, - "require": { - "php": ">=7.1.3", - "symfony/polyfill-ctype": "~1.8" - }, - "conflict": { - "symfony/console": "<3.4" - }, - "require-dev": { - "symfony/console": "^3.4|^4.0|^5.0" - }, - "suggest": { - "symfony/console": "For validating YAML files using the lint command" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "4.4-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\Yaml\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Yaml Component", - "homepage": "https://symfony.com", - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2020-05-20T08:37:50+00:00" } ], "aliases": [], @@ -1966,6 +1829,5 @@ "platform-dev": [], "platform-overrides": { "php": "7.2.0" - }, - "plugin-api-version": "1.1.0" + } } diff --git a/front/export_to_yaml.php b/front/export_to_yaml.php new file mode 100644 index 00000000..e748a68a --- /dev/null +++ b/front/export_to_yaml.php @@ -0,0 +1,20 @@ + ". - __("Regenerate container files", "fields").""; + __("Regenerate container files", "fields")."   ". + __("Export to YAML", "fields")."
"; + } /** @@ -563,6 +565,9 @@ static function getTypeName($nb = 0) { public function showForm($ID, $options = []) { global $CFG_GLPI; + echo "
 ". + __("Export to YAML", "fields")."

"; + $this->initForm($ID, $options); $this->showFormHeader($options); $rand = mt_rand(); diff --git a/setup.php b/setup.php index 715ccc05..80901953 100644 --- a/setup.php +++ b/setup.php @@ -61,6 +61,8 @@ mkdir(PLUGINFIELDS_FRONT_PATH); } +use Symfony\Component\Yaml\Yaml; + /** * Init hooks of the plugin. * REQUIRED @@ -74,6 +76,9 @@ function plugin_init_fields() { // manage autoload of plugin custom classes include_once(PLUGINFIELDS_DIR . "/inc/autoload.php"); + + // manage autoload of vendor classes + include_once(PLUGINFIELDS_DIR . "/vendor/autoload.php"); $pluginfields_autoloader = new PluginFieldsAutoloader([PLUGINFIELDS_CLASS_PATH]); $pluginfields_autoloader->register(); @@ -244,3 +249,110 @@ function plugin_fields_checkFiles($force = false) { } } } + +function plugin_fields_exportBlockAsYaml($container_id = null) { + global $DB; + + $plugin = new Plugin(); + $yaml_conf = [ + 'container' => [], + ]; + + if (isset($_SESSION['glpiactiveentities']) + && $plugin->isInstalled('fields') + && $plugin->isActivated('fields') + && Session::getLoginUserID()) { + + if ($DB->tableExists(PluginFieldsContainer::getTable())) { + $where = []; + $where["is_active"] = true; + if ($container_id != null) { + $where["id"] = $container_id; + } + $container_obj = new PluginFieldsContainer(); + $containers = $container_obj->find($where); + + foreach ($containers as $container) { + $itemtypes = (strlen($container['itemtypes']) > 0) + ? json_decode($container['itemtypes'], true) + : []; + + foreach ($itemtypes as $itemtype) { + $fields_obj = new PluginFieldsField(); + // to get translation + $container["itemtype"] = PluginFieldsContainer::getType(); + $yaml_conf['container'][$container['id']."-".$itemtype] = [ + "id" => (int) $container['id'], + "name" => PluginFieldsLabelTranslation::getLabelFor($container), + "itemtype" => $itemtype, + "type" => $container['type'], + "subtype" => $container['subtype'], + "fields" => [], + ]; + $fields = $fields_obj->find(["plugin_fields_containers_id" => $container['id'], + "is_active" => true, + "is_readonly" => false]); + if (count($fields)) { + foreach ($fields as $field) { + $tmp_field = []; + $tmp_field['id'] = (int) $field['id']; + + //to get translation + $field["itemtype"] = PluginFieldsField::getType(); + $tmp_field['label'] = PluginFieldsLabelTranslation::getLabelFor($field); + $tmp_field['xml_node'] = strtoupper($field['name']); + $tmp_field['type'] = $field['type']; + $tmp_field['ranking'] = $field['ranking']; + $tmp_field['default_value'] = $field['default_value']; + $tmp_field['mandatory'] = $field['mandatory']; + $tmp_field['possible_value'] = ""; + + switch ($field['type']) { + case 'dropdown': + $obj = new $itemtype; + $obj->getEmpty(); + + $dropdown_itemtype = PluginFieldsDropdown::getClassname($field['name']); + $tmp_field['xml_node'] = strtoupper(getForeignKeyFieldForItemType($dropdown_itemtype)); + + $dropdown_obj = new $dropdown_itemtype(); + $dropdown_datas = $dropdown_obj->find(); + $datas = []; + foreach ($dropdown_datas as $key => $value) { + $items = []; + $items['id'] = (int)$value['id']; + $items['value'] = $value['name']; + $datas[] = $items; + } + $tmp_field['possible_value'] = $datas; + break; + case 'yesno': + $datas = []; + $datas["0"]['id'] = 0; + $datas["0"]['value'] = __('No'); + $datas["1"]['id'] = 1; + $datas["1"]['value'] = __('Yes'); + $tmp_field['possible_value'] = $datas; + break; + case 'dropdownuser': + $datas = Dropdown::getDropdownUsers(['is_active' => 1,'is_deleted' => 0], false); + $tmp_field['possible_value'] = $datas['results']; + break; + } + $yaml_conf['container'][$container['id']."-".$itemtype]["fields"][] = $tmp_field; + } + } + } + } + } + } + + if (count($yaml_conf)) { + $dump = Yaml::dump($yaml_conf, 10); + $filename = GLPI_TMP_DIR."/fields_conf.yaml"; + file_put_contents($filename, $dump); + return true; + } + + return false; +}