diff --git a/.travis.yml b/.travis.yml index bd597fe6..426c3c5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: php php: - - 5.5 - 5.6 - 7.0 - 7.1 @@ -11,7 +10,7 @@ env: - DB=mysql matrix: #- GLPIVER=9.1.2 ==> not released yet! - - GLPIVER=9.1/bugfixes + - GLPIVER=9.2/bugfixes - GLPIVER=master before_script: @@ -19,7 +18,7 @@ before_script: - git clone --depth=1 https://github.com/glpi-project/glpi -b $GLPIVER ../glpi && cd ../glpi - composer install --no-dev - mysql -u root -e 'create database glpitest;' - - php tools/cliinstall.php --db=glpi-test --user=travis --tests + - php tools/cliinstall.php --db=glpitest --user=root --tests - mv ../fields plugins/fields - cd plugins/fields - composer install -o @@ -27,12 +26,9 @@ before_script: script: - vendor/bin/robo --no-interaction code:cs - mysql -u root -e 'select version();' - - ./vendor/bin/atoum -bf tests/bootstrap.php -d tests/units/ + - ./vendor/bin/atoum --debug -bf tests/bootstrap.php -d tests/units/ matrix: - exclude: - - php: 5.4 - env: GLPIVER=master allow_failures: - php: nightly diff --git a/ajax/reorder.php b/ajax/reorder.php index 646a320a..fd09ce21 100644 --- a/ajax/reorder.php +++ b/ajax/reorder.php @@ -1,7 +1,7 @@ showForm($_POST['itemtype'], $_POST['items_id'], $_POST['id']); } else { - _e('Access denied'); + echo __('Access denied'); } Html::ajaxFooter(); diff --git a/composer.json b/composer.json index a1eee50b..fa74e9fe 100644 --- a/composer.json +++ b/composer.json @@ -3,12 +3,16 @@ "prefer-stable": true, "require-dev": { "glpi-project/tools": "^0.1.0", - "atoum/atoum": "^2.8" - }, - "scripts": { - "post-install-cmd": "if [ $COMPOSER_DEV_MODE -eq 1 ]; then patch -d vendor/atoum/atoum -p1 < tools/atoum-php71.patch; fi" + "atoum/atoum": "^3.1" }, "require": { + "php": ">=5.6.0", "zendframework/zend-loader": "^2.5" + }, + "config": { + "optimize-autoloader": true, + "platform": { + "php": "5.6.0" + } } } diff --git a/composer.lock b/composer.lock index 06136092..3cb7e30e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "8d1f5d1ddab4f85044538fbdc947088f", + "content-hash": "32a64ad2ed16ec8dc77828bbaeebc023", "packages": [ { "name": "zendframework/zend-loader", @@ -54,16 +54,16 @@ "packages-dev": [ { "name": "atoum/atoum", - "version": "2.8.2", + "version": "3.1.1", "source": { "type": "git", "url": "https://github.com/atoum/atoum.git", - "reference": "4d0136b21185eea5fc2ee638f77b291e6c537100" + "reference": "6af6b609a108fd2e63f2f42efbb4f0b799e6c710" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/atoum/atoum/zipball/4d0136b21185eea5fc2ee638f77b291e6c537100", - "reference": "4d0136b21185eea5fc2ee638f77b291e6c537100", + "url": "https://api.github.com/repos/atoum/atoum/zipball/6af6b609a108fd2e63f2f42efbb4f0b799e6c710", + "reference": "6af6b609a108fd2e63f2f42efbb4f0b799e6c710", "shasum": "" }, "require": { @@ -72,14 +72,18 @@ "ext-session": "*", "ext-tokenizer": "*", "ext-xml": "*", - "php": ">=5.3.3" + "php": "^5.6.0 || ^7.0.0" }, "replace": { "mageekguy/atoum": "*" }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~1.12" + }, "suggest": { "atoum/stubs": "Provides IDE support (like autocompletion) for atoum", - "ext-mbstring": "Provides support for UTF-8 strings" + "ext-mbstring": "Provides support for UTF-8 strings", + "ext-xdebug": "Provides code coverage report (>= 2.3)" }, "bin": [ "bin/atoum" @@ -87,7 +91,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.x-dev" + "dev-master": "3.x-dev" } }, "autoload": { @@ -130,33 +134,33 @@ "test", "unit testing" ], - "time": "2016-08-12T13:45:10+00:00" + "time": "2017-07-19T19:59:03+00:00" }, { "name": "consolidation/annotated-command", - "version": "2.4.0", + "version": "2.4.13", "source": { "type": "git", "url": "https://github.com/consolidation/annotated-command.git", - "reference": "80afffd362bd1cf83bef60db690a8c50d8390803" + "reference": "abb685e42c83d0b698b4e22059e5d505588f7d3c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/annotated-command/zipball/80afffd362bd1cf83bef60db690a8c50d8390803", - "reference": "80afffd362bd1cf83bef60db690a8c50d8390803", + "url": "https://api.github.com/repos/consolidation/annotated-command/zipball/abb685e42c83d0b698b4e22059e5d505588f7d3c", + "reference": "abb685e42c83d0b698b4e22059e5d505588f7d3c", "shasum": "" }, "require": { - "consolidation/output-formatters": "^3.1.5", + "consolidation/output-formatters": "^3.1.10", "php": ">=5.4.0", - "phpdocumentor/reflection-docblock": "^2.0|^3.0.2", - "psr/log": "~1", + "phpdocumentor/reflection-docblock": "^2.0|^3.0.2|^4.0", + "psr/log": "^1", "symfony/console": "^2.8|~3", - "symfony/event-dispatcher": "^2.5|~3", - "symfony/finder": "^2.5|~3" + "symfony/event-dispatcher": "^2.5|^3", + "symfony/finder": "^2.5|^3" }, "require-dev": { - "phpunit/phpunit": "4.*", + "phpunit/phpunit": "^4.8", "satooshi/php-coveralls": "^1.0", "squizlabs/php_codesniffer": "^2.7" }, @@ -182,7 +186,56 @@ } ], "description": "Initialize Symfony Console commands from annotated command class methods.", - "time": "2017-02-04T06:13:54+00:00" + "time": "2017-08-28T20:16:37+00:00" + }, + { + "name": "consolidation/config", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/consolidation/config.git", + "reference": "e3c7311f8926488fe2fbce0ec6af56be417da504" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/consolidation/config/zipball/e3c7311f8926488fe2fbce0ec6af56be417da504", + "reference": "e3c7311f8926488fe2fbce0ec6af56be417da504", + "shasum": "" + }, + "require": { + "dflydev/dot-access-data": "^1.1.0", + "grasmash/yaml-expander": "^1.1", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4", + "satooshi/php-coveralls": "^1.0", + "squizlabs/php_codesniffer": "2.*", + "symfony/console": "^2.5|^3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Consolidation\\Config\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Greg Anderson", + "email": "greg.1.anderson@greenknowe.org" + } + ], + "description": "Provide configuration services for a commandline tool.", + "time": "2017-07-28T18:05:53+00:00" }, { "name": "consolidation/log", @@ -233,33 +286,33 @@ }, { "name": "consolidation/output-formatters", - "version": "3.1.7", + "version": "3.1.11", "source": { "type": "git", "url": "https://github.com/consolidation/output-formatters.git", - "reference": "da39a0f14d5aaaee06732bb7cef2aea1de056b40" + "reference": "3a1160440819269e6d8d9c11db67129384b8fb35" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/output-formatters/zipball/da39a0f14d5aaaee06732bb7cef2aea1de056b40", - "reference": "da39a0f14d5aaaee06732bb7cef2aea1de056b40", + "url": "https://api.github.com/repos/consolidation/output-formatters/zipball/3a1160440819269e6d8d9c11db67129384b8fb35", + "reference": "3a1160440819269e6d8d9c11db67129384b8fb35", "shasum": "" }, "require": { "php": ">=5.4.0", - "symfony/console": "~2.5|~3.0", + "symfony/console": "^2.8|~3", "symfony/finder": "~2.5|~3.0" }, "require-dev": { - "phpunit/phpunit": "4.*", + "phpunit/phpunit": "^4.8", "satooshi/php-coveralls": "^1.0", - "squizlabs/php_codesniffer": "2.*", + "squizlabs/php_codesniffer": "^2.7", "victorjonsson/markdowndocs": "^1.3" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.x-dev" + "dev-master": "3.x-dev" } }, "autoload": { @@ -278,7 +331,7 @@ } ], "description": "Format text by applying transformations provided by plug-in formatters.", - "time": "2017-01-21T06:26:40+00:00" + "time": "2017-08-17T22:11:07+00:00" }, { "name": "consolidation/robo", @@ -286,16 +339,17 @@ "source": { "type": "git", "url": "https://github.com/consolidation/Robo.git", - "reference": "efd4b5d9a25bce330fe27c1e23e5a808ece485ba" + "reference": "19c7a1888f40bdc8e80a80fdc95d0577802611b0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation/Robo/zipball/efd4b5d9a25bce330fe27c1e23e5a808ece485ba", - "reference": "efd4b5d9a25bce330fe27c1e23e5a808ece485ba", + "url": "https://api.github.com/repos/consolidation/Robo/zipball/19c7a1888f40bdc8e80a80fdc95d0577802611b0", + "reference": "19c7a1888f40bdc8e80a80fdc95d0577802611b0", "shasum": "" }, "require": { "consolidation/annotated-command": "^2.2", + "consolidation/config": "^1.0.1", "consolidation/log": "~1", "consolidation/output-formatters": "^3.1.5", "league/container": "^2.2", @@ -319,11 +373,11 @@ "pear/archive_tar": "^1.4.2", "phpunit/php-code-coverage": "~2|~4", "satooshi/php-coveralls": "~1", - "squizlabs/php_codesniffer": "~2" + "squizlabs/php_codesniffer": "^2.8" }, "suggest": { "henrikbjorn/lurker": "For monitoring filesystem changes in taskWatch", - "natxet/CssMin": "For minifying JS files in taskMinify", + "natxet/CssMin": "For minifying CSS files in taskMinify", "patchwork/jsqueeze": "For minifying JS files in taskMinify", "pear/archive_tar": "Allows tar archives to be created and extracted in taskPack and taskExtract, respectively." }, @@ -333,7 +387,8 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.x-dev" + "dev-master": "1.x-dev", + "dev-state": "1.x-dev" } }, "autoload": { @@ -355,22 +410,25 @@ } ], "description": "Modern task runner", - "time": "2017-02-06 20:04:22" + "time": "2017-08-11T21:46:23+00:00" }, { "name": "container-interop/container-interop", - "version": "1.1.0", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/container-interop/container-interop.git", - "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e" + "reference": "79cbf1341c22ec75643d841642dd5d6acd83bdb8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e", - "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e", + "url": "https://api.github.com/repos/container-interop/container-interop/zipball/79cbf1341c22ec75643d841642dd5d6acd83bdb8", + "reference": "79cbf1341c22ec75643d841642dd5d6acd83bdb8", "shasum": "" }, + "require": { + "psr/container": "^1.0" + }, "type": "library", "autoload": { "psr-4": { @@ -382,7 +440,67 @@ "MIT" ], "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", - "time": "2014-12-30T15:22:37+00:00" + "homepage": "https://github.com/container-interop/container-interop", + "time": "2017-02-14T19:40:03+00:00" + }, + { + "name": "dflydev/dot-access-data", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/dflydev/dflydev-dot-access-data.git", + "reference": "3fbd874921ab2c041e899d044585a2ab9795df8a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dflydev/dflydev-dot-access-data/zipball/3fbd874921ab2c041e899d044585a2ab9795df8a", + "reference": "3fbd874921ab2c041e899d044585a2ab9795df8a", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-0": { + "Dflydev\\DotAccessData": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Dragonfly Development Inc.", + "email": "info@dflydev.com", + "homepage": "http://dflydev.com" + }, + { + "name": "Beau Simensen", + "email": "beau@dflydev.com", + "homepage": "http://beausimensen.com" + }, + { + "name": "Carlos Frutos", + "email": "carlos@kiwing.it", + "homepage": "https://github.com/cfrutos" + } + ], + "description": "Given a deep data structure, access data by dot notation.", + "homepage": "https://github.com/dflydev/dflydev-dot-access-data", + "keywords": [ + "access", + "data", + "dot", + "notation" + ], + "time": "2017-01-20T21:14:22+00:00" }, { "name": "glpi-project/coding-standard", @@ -423,16 +541,16 @@ }, { "name": "glpi-project/tools", - "version": "0.1.1", + "version": "0.1.2", "source": { "type": "git", "url": "https://github.com/glpi-project/tools.git", - "reference": "89083f6e71fac05190c7cc76a9c5afd8b1f421ef" + "reference": "55b3ba30c9c7f32acf5d9e8d6747e1e369809b13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/glpi-project/tools/zipball/89083f6e71fac05190c7cc76a9c5afd8b1f421ef", - "reference": "89083f6e71fac05190c7cc76a9c5afd8b1f421ef", + "url": "https://api.github.com/repos/glpi-project/tools/zipball/55b3ba30c9c7f32acf5d9e8d6747e1e369809b13", + "reference": "55b3ba30c9c7f32acf5d9e8d6747e1e369809b13", "shasum": "" }, "require": { @@ -469,28 +587,76 @@ "plugins", "tools" ], - "time": "2017-02-08T08:20:09+00:00" + "time": "2017-03-03T14:40:45+00:00" + }, + { + "name": "grasmash/yaml-expander", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/grasmash/yaml-expander.git", + "reference": "720c54b2c99b80d5d696714b6826183d34edce93" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/grasmash/yaml-expander/zipball/720c54b2c99b80d5d696714b6826183d34edce93", + "reference": "720c54b2c99b80d5d696714b6826183d34edce93", + "shasum": "" + }, + "require": { + "dflydev/dot-access-data": "^1.1.0", + "php": ">=5.4", + "symfony/yaml": "^2.8.11|^3" + }, + "require-dev": { + "phpunit/phpunit": "^4.8|^5.5.4", + "satooshi/php-coveralls": "^1.0", + "squizlabs/php_codesniffer": "^2.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Grasmash\\YamlExpander\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Matthew Grasmick" + } + ], + "description": "Expands internal property references in a yaml file.", + "time": "2017-08-01T16:15:05+00:00" }, { "name": "league/container", - "version": "2.2.0", + "version": "2.4.1", "source": { "type": "git", "url": "https://github.com/thephpleague/container.git", - "reference": "c0e7d947b690891f700dc4967ead7bdb3d6708c1" + "reference": "43f35abd03a12977a60ffd7095efd6a7808488c0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/container/zipball/c0e7d947b690891f700dc4967ead7bdb3d6708c1", - "reference": "c0e7d947b690891f700dc4967ead7bdb3d6708c1", + "url": "https://api.github.com/repos/thephpleague/container/zipball/43f35abd03a12977a60ffd7095efd6a7808488c0", + "reference": "43f35abd03a12977a60ffd7095efd6a7808488c0", "shasum": "" }, "require": { - "container-interop/container-interop": "^1.1", - "php": ">=5.4.0" + "container-interop/container-interop": "^1.2", + "php": "^5.4.0 || ^7.0" }, "provide": { - "container-interop/container-interop-implementation": "^1.1" + "container-interop/container-interop-implementation": "^1.2", + "psr/container-implementation": "^1.0" }, "replace": { "orno/di": "~2.0" @@ -501,7 +667,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.x-dev", + "dev-2.x": "2.x-dev", "dev-1.x": "1.x-dev" } }, @@ -533,7 +699,7 @@ "provider", "service" ], - "time": "2016-03-17T11:07:59+00:00" + "time": "2017-05-10T09:20:27+00:00" }, { "name": "natxet/CssMin", @@ -680,22 +846,22 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "3.1.1", + "version": "3.2.2", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e" + "reference": "4aada1f93c72c35e22fb1383b47fee43b8f1d157" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/8331b5efe816ae05461b7ca1e721c01b46bafb3e", - "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/4aada1f93c72c35e22fb1383b47fee43b8f1d157", + "reference": "4aada1f93c72c35e22fb1383b47fee43b8f1d157", "shasum": "" }, "require": { "php": ">=5.5", "phpdocumentor/reflection-common": "^1.0@dev", - "phpdocumentor/type-resolver": "^0.2.0", + "phpdocumentor/type-resolver": "^0.3.0", "webmozart/assert": "^1.0" }, "require-dev": { @@ -721,24 +887,24 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2016-09-30T07:12:33+00:00" + "time": "2017-08-08T06:39:58+00:00" }, { "name": "phpdocumentor/type-resolver", - "version": "0.2.1", + "version": "0.3.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb" + "reference": "fb3933512008d8162b3cdf9e18dba9309b7c3773" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb", - "reference": "e224fb2ea2fba6d3ad6fdaef91cd09a172155ccb", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/fb3933512008d8162b3cdf9e18dba9309b7c3773", + "reference": "fb3933512008d8162b3cdf9e18dba9309b7c3773", "shasum": "" }, "require": { - "php": ">=5.5", + "php": "^5.5 || ^7.0", "phpdocumentor/reflection-common": "^1.0" }, "require-dev": { @@ -768,7 +934,56 @@ "email": "me@mikevanriel.com" } ], - "time": "2016-11-25T06:54:22+00:00" + "time": "2017-06-03T08:32:36+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" }, { "name": "psr/log", @@ -819,16 +1034,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "2.8.0", + "version": "2.9.1", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "86dd55a522238211f9f3631e3361703578941d9a" + "reference": "dcbed1074f8244661eecddfc2a675430d8d33f62" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/86dd55a522238211f9f3631e3361703578941d9a", - "reference": "86dd55a522238211f9f3631e3361703578941d9a", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/dcbed1074f8244661eecddfc2a675430d8d33f62", + "reference": "dcbed1074f8244661eecddfc2a675430d8d33f62", "shasum": "" }, "require": { @@ -893,29 +1108,34 @@ "phpcs", "standards" ], - "time": "2017-02-02T03:30:00+00:00" + "time": "2017-05-22T02:43:20+00:00" }, { "name": "symfony/console", - "version": "v3.2.3", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "7a8405a9fc175f87fed8a3c40856b0d866d61936" + "reference": "d6596cb5022b6a0bd940eae54a1de78646a5fda6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/7a8405a9fc175f87fed8a3c40856b0d866d61936", - "reference": "7a8405a9fc175f87fed8a3c40856b0d866d61936", + "url": "https://api.github.com/repos/symfony/console/zipball/d6596cb5022b6a0bd940eae54a1de78646a5fda6", + "reference": "d6596cb5022b6a0bd940eae54a1de78646a5fda6", "shasum": "" }, "require": { - "php": ">=5.5.9", + "php": "^5.5.9|>=7.0.8", "symfony/debug": "~2.8|~3.0", "symfony/polyfill-mbstring": "~1.0" }, + "conflict": { + "symfony/dependency-injection": "<3.3" + }, "require-dev": { "psr/log": "~1.0", + "symfony/config": "~3.3", + "symfony/dependency-injection": "~3.3", "symfony/event-dispatcher": "~2.8|~3.0", "symfony/filesystem": "~2.8|~3.0", "symfony/process": "~2.8|~3.0" @@ -929,7 +1149,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2-dev" + "dev-master": "3.3-dev" } }, "autoload": { @@ -956,37 +1176,36 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2017-02-06T12:04:21+00:00" + "time": "2017-08-27T14:52:21+00:00" }, { "name": "symfony/debug", - "version": "v3.2.3", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/symfony/debug.git", - "reference": "b4d9818f127c60ce21ed62c395da7df868dc8477" + "reference": "084d804fe35808eb2ef596ec83d85d9768aa6c9d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/debug/zipball/b4d9818f127c60ce21ed62c395da7df868dc8477", - "reference": "b4d9818f127c60ce21ed62c395da7df868dc8477", + "url": "https://api.github.com/repos/symfony/debug/zipball/084d804fe35808eb2ef596ec83d85d9768aa6c9d", + "reference": "084d804fe35808eb2ef596ec83d85d9768aa6c9d", "shasum": "" }, "require": { - "php": ">=5.5.9", + "php": "^5.5.9|>=7.0.8", "psr/log": "~1.0" }, "conflict": { "symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2" }, "require-dev": { - "symfony/class-loader": "~2.8|~3.0", "symfony/http-kernel": "~2.8|~3.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2-dev" + "dev-master": "3.3-dev" } }, "autoload": { @@ -1013,29 +1232,32 @@ ], "description": "Symfony Debug Component", "homepage": "https://symfony.com", - "time": "2017-01-28T02:37:08+00:00" + "time": "2017-08-27T14:52:21+00:00" }, { "name": "symfony/event-dispatcher", - "version": "v3.2.3", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "9137eb3a3328e413212826d63eeeb0217836e2b6" + "reference": "54ca9520a00386f83bca145819ad3b619aaa2485" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/9137eb3a3328e413212826d63eeeb0217836e2b6", - "reference": "9137eb3a3328e413212826d63eeeb0217836e2b6", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/54ca9520a00386f83bca145819ad3b619aaa2485", + "reference": "54ca9520a00386f83bca145819ad3b619aaa2485", "shasum": "" }, "require": { - "php": ">=5.5.9" + "php": "^5.5.9|>=7.0.8" + }, + "conflict": { + "symfony/dependency-injection": "<3.3" }, "require-dev": { "psr/log": "~1.0", "symfony/config": "~2.8|~3.0", - "symfony/dependency-injection": "~2.8|~3.0", + "symfony/dependency-injection": "~3.3", "symfony/expression-language": "~2.8|~3.0", "symfony/stopwatch": "~2.8|~3.0" }, @@ -1046,7 +1268,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2-dev" + "dev-master": "3.3-dev" } }, "autoload": { @@ -1073,29 +1295,29 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "time": "2017-01-02T20:32:22+00:00" + "time": "2017-07-29T21:54:42+00:00" }, { "name": "symfony/filesystem", - "version": "v3.2.3", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "a0c6ef2dc78d33b58d91d3a49f49797a184d06f4" + "reference": "b32a0e5f928d0fa3d1dd03c78d020777e50c10cb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/a0c6ef2dc78d33b58d91d3a49f49797a184d06f4", - "reference": "a0c6ef2dc78d33b58d91d3a49f49797a184d06f4", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/b32a0e5f928d0fa3d1dd03c78d020777e50c10cb", + "reference": "b32a0e5f928d0fa3d1dd03c78d020777e50c10cb", "shasum": "" }, "require": { - "php": ">=5.5.9" + "php": "^5.5.9|>=7.0.8" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2-dev" + "dev-master": "3.3-dev" } }, "autoload": { @@ -1122,29 +1344,29 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2017-01-08T20:47:33+00:00" + "time": "2017-07-29T21:54:42+00:00" }, { "name": "symfony/finder", - "version": "v3.2.3", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "8c71141cae8e2957946b403cc71a67213c0380d6" + "reference": "b2260dbc80f3c4198f903215f91a1ac7fe9fe09e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/8c71141cae8e2957946b403cc71a67213c0380d6", - "reference": "8c71141cae8e2957946b403cc71a67213c0380d6", + "url": "https://api.github.com/repos/symfony/finder/zipball/b2260dbc80f3c4198f903215f91a1ac7fe9fe09e", + "reference": "b2260dbc80f3c4198f903215f91a1ac7fe9fe09e", "shasum": "" }, "require": { - "php": ">=5.5.9" + "php": "^5.5.9|>=7.0.8" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2-dev" + "dev-master": "3.3-dev" } }, "autoload": { @@ -1171,20 +1393,20 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2017-01-02T20:32:22+00:00" + "time": "2017-07-29T21:54:42+00:00" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.3.0", + "version": "v1.5.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "e79d363049d1c2128f133a2667e4f4190904f7f4" + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/e79d363049d1c2128f133a2667e4f4190904f7f4", - "reference": "e79d363049d1c2128f133a2667e4f4190904f7f4", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7c8fae0ac1d216eb54349e6a8baa57d515fe8803", + "reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803", "shasum": "" }, "require": { @@ -1196,7 +1418,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3-dev" + "dev-master": "1.5-dev" } }, "autoload": { @@ -1230,29 +1452,29 @@ "portable", "shim" ], - "time": "2016-11-14T01:06:16+00:00" + "time": "2017-06-14T15:44:48+00:00" }, { "name": "symfony/process", - "version": "v3.2.3", + "version": "v3.3.8", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "32646a7cf53f3956c76dcb5c82555224ae321858" + "reference": "b7666e9b438027a1ea0e1ee813ec5042d5d7f6f0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/32646a7cf53f3956c76dcb5c82555224ae321858", - "reference": "32646a7cf53f3956c76dcb5c82555224ae321858", + "url": "https://api.github.com/repos/symfony/process/zipball/b7666e9b438027a1ea0e1ee813ec5042d5d7f6f0", + "reference": "b7666e9b438027a1ea0e1ee813ec5042d5d7f6f0", "shasum": "" }, "require": { - "php": ">=5.5.9" + "php": "^5.5.9|>=7.0.8" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2-dev" + "dev-master": "3.3-dev" } }, "autoload": { @@ -1279,7 +1501,62 @@ ], "description": "Symfony Process Component", "homepage": "https://symfony.com", - "time": "2017-02-03T12:11:38+00:00" + "time": "2017-07-29T21:54:42+00:00" + }, + { + "name": "symfony/yaml", + "version": "v3.3.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "1d8c2a99c80862bdc3af94c1781bf70f86bccac0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/1d8c2a99c80862bdc3af94c1781bf70f86bccac0", + "reference": "1d8c2a99c80862bdc3af94c1781bf70f86bccac0", + "shasum": "" + }, + "require": { + "php": "^5.5.9|>=7.0.8" + }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.3-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": "2017-07-29T21:54:42+00:00" }, { "name": "webmozart/assert", @@ -1337,6 +1614,11 @@ "stability-flags": [], "prefer-stable": true, "prefer-lowest": false, - "platform": [], - "platform-dev": [] + "platform": { + "php": ">=5.6.0" + }, + "platform-dev": [], + "platform-overrides": { + "php": "5.6.0" + } } diff --git a/front/container.form.php b/front/container.form.php index 5fe52c30..e75346ca 100644 --- a/front/container.form.php +++ b/front/container.form.php @@ -34,6 +34,6 @@ } else { Html::header(__("Additionnal fields", "fields"), $_SERVER['PHP_SELF'], "config", "pluginfieldsmenu", "fieldscontainer"); - $container->display(array('id' => $_GET["id"])); + $container->display(['id' => $_GET["id"]]); Html::footer(); } diff --git a/front/regenerate_files.php b/front/regenerate_files.php index 6a911fbc..140465a3 100644 --- a/front/regenerate_files.php +++ b/front/regenerate_files.php @@ -4,6 +4,6 @@ Session::checkRight('entity', READ); -regenerateFiles(); +plugin_fields_checkFiles(true); Html::back(); \ No newline at end of file diff --git a/hook.php b/hook.php index 57f814dc..80849f9e 100644 --- a/hook.php +++ b/hook.php @@ -41,7 +41,7 @@ function plugin_fields_install() { $plugin_fields->getFromDBbyDir('fields'); $version = $plugin_fields->fields['version']; - $classesToInstall = array( + $classesToInstall = [ 'PluginFieldsField', 'PluginFieldsDropdown', 'PluginFieldsLabelTranslation', @@ -50,7 +50,7 @@ function plugin_fields_install() { 'PluginFieldsValue', 'PluginFieldsProfile', 'PluginFieldsMigration' - ); + ]; $migration = new Migration($version); echo "
"; @@ -62,9 +62,9 @@ function plugin_fields_install() { //load all classes foreach ($classesToInstall as $class) { - if ($plug=isPluginItemType($class)) { - $dir= GLPI_ROOT . "/plugins/fields/inc/"; - $item=strtolower($plug['class']); + if ($plug = isPluginItemType($class)) { + $dir = GLPI_ROOT . "/plugins/fields/inc/"; + $item = strtolower($plug['class']); if (file_exists("$dir$item.class.php")) { include_once ("$dir$item.class.php"); } @@ -73,9 +73,9 @@ function plugin_fields_install() { //install foreach ($classesToInstall as $class) { - if ($plug=isPluginItemType($class)) { - $dir= GLPI_ROOT . "/plugins/fields/inc/"; - $item=strtolower($plug['class']); + if ($plug = isPluginItemType($class)) { + $dir = GLPI_ROOT . "/plugins/fields/inc/"; + $item =strtolower($plug['class']); if (file_exists("$dir$item.class.php")) { if (!call_user_func(array($class,'install'), $migration, $version)) { return false; @@ -88,6 +88,9 @@ function plugin_fields_install() { echo ""; echo "
"; + // Check class and front files for existing containers and dropdown fields + plugin_fields_checkFiles(); + return true; } @@ -99,7 +102,7 @@ function plugin_fields_install() { function plugin_fields_uninstall() { global $DB; - if (! class_exists('PluginFieldsProfile')) { + if (!class_exists('PluginFieldsProfile')) { Session::addMessageAfterRedirect(__("The plugin can't be uninstalled when the plugin is disabled", 'fields'), true, WARNING, true); return false; @@ -114,19 +117,21 @@ function plugin_fields_uninstall() { echo ""; echo ""; - $classesToUninstall = array('PluginFieldsDropdown', - 'PluginFieldsContainer', - 'PluginFieldsContainer_Field', - 'PluginFieldsLabelTranslation', - 'PluginFieldsField', - 'PluginFieldsValue', - 'PluginFieldsProfile', - 'PluginFieldsMigration'); + $classesToUninstall = [ + 'PluginFieldsDropdown', + 'PluginFieldsContainer', + 'PluginFieldsContainer_Field', + 'PluginFieldsLabelTranslation', + 'PluginFieldsField', + 'PluginFieldsValue', + 'PluginFieldsProfile', + 'PluginFieldsMigration' + ]; foreach ($classesToUninstall as $class) { - if ($plug=isPluginItemType($class)) { + if ($plug = isPluginItemType($class)) { - $dir = GLPI_ROOT . "/plugins/fields/inc/"; + $dir = GLPI_ROOT . "/plugins/fields/inc/"; $item = strtolower($plug['class']); if (file_exists("$dir$item.class.php")) { @@ -145,29 +150,20 @@ function plugin_fields_uninstall() { unset($_SESSION['uninstall_fields']); // clean display preferences - $DB->query("DELETE FROM glpi_displaypreferences WHERE itemtype LIKE 'PluginFields%'"); + $pref = new DisplayPreference; + $pref->deleteByCriteria([ + 'itemtype' => ['LIKE' , 'PluginFields%'] + ]); return true; } -function regenerateFiles() { - $container = new PluginFieldsContainer; - $found_container = $container->find(); - foreach ($found_container as $current_container) { - $containers_id = $current_container['id']; - $container->getFromDB($containers_id); - $container->post_addItem(); - } -} - - function plugin_fields_getAddSearchOptions($itemtype) { if (isset($_SESSION['glpiactiveentities']) - && is_array($_SESSION['glpiactiveentities']) - && count($_SESSION['glpiactiveentities']) > 0) { + && is_array($_SESSION['glpiactiveentities']) + && count($_SESSION['glpiactiveentities']) > 0) { $itemtypes = PluginFieldsContainer::getEntries('all'); - if ($itemtypes !== false && in_array($itemtype, $itemtypes)) { return PluginFieldsContainer::getAddSearchOptions($itemtype); } @@ -178,10 +174,10 @@ function plugin_fields_getAddSearchOptions($itemtype) { // Define Dropdown tables to be manage in GLPI : function plugin_fields_getDropdown() { - $dropdowns = array(); + $dropdowns = []; $field_obj = new PluginFieldsField; - $fields = $field_obj->find("`type` = 'dropdown'"); + $fields = $field_obj->find("`type` = 'dropdown'"); foreach ($fields as $field) { $field['itemtype'] = PluginFieldsField::getType(); $label = PluginFieldsLabelTranslation::getLabelFor($field); @@ -196,7 +192,7 @@ function plugin_fields_getDropdown() { /**** MASSIVE ACTIONS ****/ // Display specific massive actions for plugin fields -function plugin_fields_MassiveActionsFieldsDisplay($options=array()) { +function plugin_fields_MassiveActionsFieldsDisplay($options = []) { $itemtypes = PluginFieldsContainer::getEntries('all'); if (in_array($options['itemtype'], $itemtypes)) { @@ -218,8 +214,8 @@ function plugin_fields_MassiveActionsFieldsDisplay($options=array()) { * @param $params input data * @return an array of actions */ -function plugin_fields_getRuleActions($params) { - $actions = array(); +function plugin_fields_getRuleActions($params = []) { + $actions = []; switch ($params['rule_itemtype']) { case "PluginFusioninventoryTaskpostactionRule": @@ -239,7 +235,7 @@ function plugin_fields_getRuleActions($params) { } -function plugin_fields_rule_matched($params) { +function plugin_fields_rule_matched($params = []) { global $DB; $container = new PluginFieldsContainer; @@ -281,7 +277,7 @@ function plugin_fields_rule_matched($params) { } } -function plugin_fields_giveItem($itemtype,$ID,$data,$num) { +function plugin_fields_giveItem($itemtype, $ID, $data, $num) { $searchopt = &Search::getOptions($itemtype); $table = $searchopt[$ID]["table"]; $field = $searchopt[$ID]["field"]; @@ -305,8 +301,7 @@ function plugin_datainjection_populate_fields() { global $INJECTABLE_TYPES; $container = new PluginFieldsContainer(); - $found = $container->find("`is_active` = 1"); - + $found = $container->find("`is_active` = 1"); foreach ($found as $id => $values) { $types = json_decode($values['itemtypes']); @@ -314,7 +309,7 @@ function plugin_datainjection_populate_fields() { $classname = "PluginFields" . ucfirst($type. preg_replace('/s$/', '', $values['name'])) . 'Injection'; - $INJECTABLE_TYPES[$classname] = 'fields'; + $INJECTABLE_TYPES[$classname] = 'fields'; } } } diff --git a/inc/autoload.php b/inc/autoload.php index dbc3f89f..ec2b04ca 100644 --- a/inc/autoload.php +++ b/inc/autoload.php @@ -4,7 +4,7 @@ class PluginFieldsAutoloader implements SplAutoloader { - protected $paths = array(); + protected $paths = []; public function __construct($options = null) { if (null !== $options) { @@ -47,11 +47,11 @@ public function autoload($classname) { return false; } - $filename = implode(".", array( + $filename = implode(".", [ $class_name, "class", "php" - )); + ]); foreach ($this->paths as $path) { $test = $path . DIRECTORY_SEPARATOR . $filename; @@ -64,7 +64,7 @@ public function autoload($classname) { } public function register() { - spl_autoload_register(array($this, 'autoload')); + spl_autoload_register([$this, 'autoload']); } } diff --git a/inc/container.class.php b/inc/container.class.php index f41da3e9..188b42ec 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -8,9 +8,8 @@ static function canCreate() { } static function titleList() { - echo "
"; + echo "
 ". + __("Regenerate container files", "fields")."
"; } /** @@ -24,10 +23,9 @@ static function titleList() { static function install(Migration $migration, $version) { global $DB; - $obj = new self(); - $table = $obj->getTable(); + $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB->tableExists($table)) { $migration->displayMessage(sprintf(__("Installing %s"), $table)); $query = "CREATE TABLE IF NOT EXISTS `$table` ( @@ -47,7 +45,7 @@ static function install(Migration $migration, $version) { } // multiple itemtype for one container - if (!FieldExists($table, "itemtypes")) { + if (!$DB->fieldExists($table, "itemtypes")) { $migration->changeField($table, 'itemtype', 'itemtypes', 'longtext'); $migration->migrationOneTable($table); @@ -57,7 +55,7 @@ static function install(Migration $migration, $version) { //add display preferences for this class $d_pref = new DisplayPreference; - $found = $d_pref->find("itemtype = '".__CLASS__."'"); + $found = $d_pref->find("itemtype = '".__CLASS__."'"); if (count($found) == 0) { for ($i = 2; $i <= 5; $i++) { $DB->query("REPLACE INTO glpi_displaypreferences VALUES @@ -65,29 +63,86 @@ static function install(Migration $migration, $version) { } } - if (!FieldExists($table, "subtype")) { - $migration->addField($table, 'subtype', 'VARCHAR(255) DEFAULT NULL', array('after' => 'type')); + if (!$DB->fieldExists($table, "subtype")) { + $migration->addField($table, 'subtype', 'VARCHAR(255) DEFAULT NULL', ['after' => 'type']); $migration->migrationOneTable($table); } + //Computer OS tab is no longer part of computer object. Moving to main + $ostab = self::findContainer(Computer::getType(), 'domtab', Computer::getType() . '$1'); + if ($ostab) { + //check if we already have a container on Computer main tab + $comptab = self::findContainer(Computer::getType(), 'dom'); + if ($comptab) { + $oscontainer = new PluginFieldsContainer(); + $oscontainer->getFromDB($ostab); + + $compcontainer = new PluginFieldsContainer(); + $compcontainer->getFromDB($comptab); + + $fields = new PluginFieldsField(); + $fields = $fields->find("plugin_fields_containers_id='$ostab'"); + + $classname = self::getClassname(Computer::getType(), $oscontainer->fields['name']); + $osdata = new $classname; + $classname = self::getClassname(Computer::getType(), $compcontainer->fields['name']); + $compdata = new $classname; + + $fieldnames = []; + //add fields to compcontainer + foreach ($fields as $field) { + $newname = $field['name']; + $compfields = $fields->find("plugin_fields_containers_id='$comptab' AND name='$newname'"); + if ($compfields) { + $newname = $newname . '_os'; + $DB->query("UPDATE glpi_plugin_fields_fields SET name='$newname' WHERE name='{$field['name']}' AND plugin_fields_containers_id='$ostab'"); + } + $compdata::addField($newname, $field['type']); + $fieldnames[$field['name']] = $newname; + } + + $sql = "UPDATE glpi_plugin_fields_fields SET plugin_fields_containers_id='$comptab' WHERE plugin_fields_containers_id='$ostab'"; + $DB->query($sql); + $DB->query("DELETE FROM glpi_plugin_fields_containers WHERE id='$ostab'"); + + //migrate existing data + $existings = $osdata->find(); + foreach ($existings as $existing) { + $data = []; + foreach ($fieldnames as $oldname => $newname) { + $data[$newname] = $existing[$olddname]; + } + $compdata->add($data); + } + + //drop old table + $DB->query("DROP TABLE " . $osdata::getTable()); + } else { + $sql = "UPDATE glpi_plugin_fields_containers SET type='dom', subtype=NULL WHERE id='$ostab'"; + $comptab = $ostab; + $DB->query($sql); + } + } + $migration->displayMessage(__("Updating generated containers files", "fields")); // -> 0.90-1.3: generated class moved // OLD path: GLPI_ROOT."/plugins/fields/inc/$class_filename" // NEW path: PLUGINFIELDS_CLASS_PATH . "/$class_filename" - $obj = new self; + $obj = new self; $containers = $obj->find(); foreach ($containers as $container) { //First, drop old fields from plugin directories - $itemtypes = (count($container['itemtypes']) > 0) ? json_decode($container['itemtypes'], true) : array(); + $itemtypes = (count($container['itemtypes']) > 0) + ? json_decode($container['itemtypes'], true) + : []; foreach ($itemtypes as $itemtype) { - $class_filename = strtolower($itemtype . - preg_replace('/s$/', '', $container['name']) . ".class.php"); + $sysname = self::getSystemName($itemtype, $container['name']); + $class_filename = $sysname.".class.php"; if (file_exists(GLPI_ROOT."/plugins/fields/inc/$class_filename")) { unlink(GLPI_ROOT."/plugins/fields/inc/$class_filename"); } - $injclass_filename = strtolower($itemtype . - preg_replace('/s$/', '', $container['name']) . "injection.class.php"); + $injclass_filename = $sysname."injection.class.php"; if (file_exists(GLPI_ROOT."/plugins/fields/inc/$injclass_filename")) { unlink(GLPI_ROOT."/plugins/fields/inc/$injclass_filename"); } @@ -107,83 +162,113 @@ static function uninstall() { $obj = new self; $containers = $obj->find(); foreach ($containers as $containers_id => $container) { - $obj->delete(array('id'=>$containers_id)); + $obj->delete(['id' => $containers_id]); } //drop global container table - $obj = new self(); - $DB->query("DROP TABLE IF EXISTS `".$obj->getTable()."`"); + $DB->query("DROP TABLE IF EXISTS `".self::getTable()."`"); //delete display preferences for this item - $DB->query("DELETE FROM glpi_displaypreferences WHERE `itemtype` = '".__CLASS__."'"); + $pref = new DisplayPreference; + $pref->deleteByCriteria([ + 'itemtype' => __CLASS__ + ]); return true; } - function getSearchOptions() { - $tab = array(); - - $tab[1]['table'] = $this->getTable(); - $tab[1]['field'] = 'name'; - $tab[1]['name'] = __("Name"); - $tab[1]['datatype'] = 'itemlink'; - $tab[1]['itemlink_type'] = $this->getType(); - $tab[1]['massiveaction'] = false; - - $tab[2]['table'] = $this->getTable(); - $tab[2]['field'] = 'label'; - $tab[2]['name'] = __("Label"); - $tab[2]['massiveaction'] = false; - - $tab[3]['table'] = $this->getTable(); - $tab[3]['field'] = 'itemtypes'; - $tab[3]['name'] = __("Associated item type"); - $tab[3]['datatype'] = 'specific'; - $tab[3]['massiveaction'] = false; - $tab[3]['nosearch'] = true; - - $tab[4]['table'] = $this->getTable(); - $tab[4]['field'] = 'type'; - $tab[4]['name'] = __("Type"); - $tab[4]['searchtype'] = array('equals', 'notequals'); - $tab[4]['massiveaction'] = false; - - $tab[5]['table'] = $this->getTable(); - $tab[5]['field'] = 'is_active'; - $tab[5]['name'] = __("Active"); - $tab[5]['datatype'] = 'bool'; - $tab[5]['searchtype'] = array('equals', 'notequals'); - - $tab[6]['table'] = 'glpi_entities'; - $tab[6]['field'] = 'completename'; - $tab[6]['name'] = __("Entity"); - $tab[6]['massiveaction'] = false; - $tab[6]['datatype'] = 'dropdown'; - - $tab[7]['table'] = $this->getTable(); - $tab[7]['field'] = 'is_recursive'; - $tab[7]['name'] = __("Child entities"); - $tab[7]['massiveaction'] = false; - $tab[7]['datatype'] = 'bool'; - - $tab[8]['table'] = $this->getTable(); - $tab[8]['field'] = 'id'; - $tab[8]['name'] = __("ID"); - $tab[8]['datatype'] = 'number'; - $tab[8]['massiveaction'] = false; + function post_getEmpty() { + $this->fields['is_active'] = 1; + $this->fields['is_recursive'] = 1; + } + + function getSearchOptionsNew() { + $tab = []; + + $tab[] = [ + 'id' => 1, + 'table' => self::getTable(), + 'field' => 'name', + 'name' => __("Name"), + 'datatype' => 'itemlink', + 'itemlink_type' => self::getType(), + 'massiveaction' => false, + ]; + + $tab[] = [ + 'id' => 2, + 'table' => self::getTable(), + 'field' => 'label', + 'name' => __("Label"), + 'massiveaction' => false, + ]; + + $tab[] = [ + 'id' => 3, + 'table' => self::getTable(), + 'field' => 'itemtypes', + 'name' => __("Associated item type"), + 'datatype' => 'specific', + 'massiveaction' => false, + 'nosearch' => true, + ]; + + $tab[] = [ + 'id' => 4, + 'table' => self::getTable(), + 'field' => 'type', + 'name' => __("Type"), + 'searchtype' => ['equals', 'notequals'], + 'massiveaction' => false, + ]; + + $tab[] = [ + 'id' => 5, + 'table' => self::getTable(), + 'field' => 'is_active', + 'name' => __("Active"), + 'datatype' => 'bool', + 'searchtype' => ['equals', 'notequals'], + ]; + + $tab[] = [ + 'id' => 6, + 'table' => 'glpi_entities', + 'field' => 'completename', + 'name' => __("Entity"), + 'massiveaction' => false, + 'datatype' => 'dropdown', + ]; + + $tab[] = [ + 'id' => 7, + 'table' => self::getTable(), + 'field' => 'is_recursive', + 'name' => __("Child entities"), + 'massiveaction' => false, + 'datatype' => 'bool', + ]; + + $tab[] = [ + 'id' => 8, + 'table' => self::getTable(), + 'field' => 'id', + 'name' => __("ID"), + 'datatype' => 'number', + 'massiveaction' => false, + ]; return $tab; } - static function getSpecificValueToDisplay($field, $values, array $options=array()) { + static function getSpecificValueToDisplay($field, $values, array $options = []) { if (!is_array($values)) { - $values = array($field => $values); + $values = [$field => $values]; } switch ($field) { case 'type': $types = self::getTypes(); return $types[$values[$field]]; - break; case 'itemtypes' : $types = json_decode($values[$field]); $obj = ''; @@ -198,7 +283,6 @@ static function getSpecificValueToDisplay($field, $values, array $options=array( $i++; } return $obj; - break; } } @@ -218,8 +302,8 @@ function getValueToSelect($field_id_or_search_options, $name = '', $values = '', return parent::getValueToSelect($field_id_or_search_options, $name, $values, $options); } - function defineTabs($options=array()) { - $ong = array(); + function defineTabs($options = array()) { + $ong = []; $this->addDefaultFormTab($ong); $this->addStandardTab('PluginFieldsField', $ong, $options); $this->addStandardTab('PluginFieldsProfile', $ong, $options); @@ -230,12 +314,14 @@ function defineTabs($options=array()) { function prepareInputForAdd($input) { if (!isset($input['itemtypes'])) { - Session::AddMessageAfterRedirect(__("You cannot add block without associated element type", "fields"), false, ERROR); + Session::AddMessageAfterRedirect(__("You cannot add block without associated element type", + "fields"), + false, ERROR); return false; } if (!is_array($input['itemtypes'])) { - $input['itemtypes'] = array($input['itemtypes']); + $input['itemtypes'] = [$input['itemtypes']]; } if ($input['type'] === "dom") { @@ -268,10 +354,11 @@ function prepareInputForAdd($input) { } } - //construct field name by processing label (remove non alphanumeric char and any trailing s) + // construct field name by processing label + // (remove non alphanumeric char and any trailing spaces) $input['name'] = strtolower(preg_replace("/[^\da-z]/i", "", preg_replace('/s*$/', '', $input['label']))); // if empty, uses a random number - if (strlen( $input['name'] ) == 0) { + if (strlen($input['name']) == 0) { $input['name'] = rand(); } @@ -288,7 +375,9 @@ function prepareInputForAdd($input) { } } - $input['itemtypes'] = (isset($input['itemtypes'])) ? json_encode($input['itemtypes'], TRUE): NULL; + $input['itemtypes'] = isset($input['itemtypes']) + ? json_encode($input['itemtypes'], TRUE) + : NULL; return $input; } @@ -305,17 +394,18 @@ function post_addItem() { } foreach (json_decode($this->fields['itemtypes']) as $itemtype) { //install table for receive field - $classname = "PluginFields" . ucfirst($itemtype . - preg_replace('/s$/', '', $this->fields['name'])); + $classname = self::getClassname($itemtype, $this->fields['name']); $classname::install(); } } public static function generateTemplate($fields) { - $itemtypes = (strlen($fields['itemtypes']) > 0) ? json_decode($fields['itemtypes'], TRUE) : array(); + $itemtypes = strlen($fields['itemtypes']) > 0 + ? json_decode($fields['itemtypes'], TRUE) + : []; foreach ($itemtypes as $itemtype) { - $classname = "PluginFields" . ucfirst($itemtype . - preg_replace('/s$/', '', $fields['name'])); + $sysname = self::getSystemName($itemtype, $fields['name']); + $classname = self::getClassname($itemtype, $fields['name']); $template_class = file_get_contents(GLPI_ROOT . "/plugins/fields/templates/container.class.tpl"); @@ -323,8 +413,7 @@ public static function generateTemplate($fields) { $template_class = str_replace("%%ITEMTYPE%%", $itemtype, $template_class); $template_class = str_replace("%%CONTAINER%%", $fields['id'], $template_class); $template_class = str_replace("%%ITEMTYPE_RIGHT%%", $itemtype::$rightname, $template_class); - $class_filename = strtolower($itemtype . - preg_replace('/s$/', '', $fields['name']) . ".class.php"); + $class_filename = $sysname.".class.php"; if (file_put_contents(PLUGINFIELDS_CLASS_PATH . "/$class_filename", $template_class) === false) { Toolbox::logDebug("Error : class file creation - $class_filename"); return false; @@ -337,8 +426,7 @@ public static function generateTemplate($fields) { $template_class = str_replace("%%ITEMTYPE%%", $itemtype, $template_class); $template_class = str_replace("%%CONTAINER_ID%%", $fields['id'], $template_class); $template_class = str_replace("%%CONTAINER_NAME%%", $fields['label'], $template_class); - $class_filename = strtolower($itemtype . - preg_replace('/s$/', '', $fields['name']) . "injection.class.php"); + $class_filename = $sysname."injection.class.php"; if (file_put_contents(PLUGINFIELDS_CLASS_PATH . "/$class_filename", $template_class) === false) { Toolbox::logDebug("Error : datainjection class file creation - $class_filename"); return false; @@ -351,36 +439,29 @@ function pre_deleteItem() { $_SESSION['delete_container'] = true; foreach (json_decode($this->fields['itemtypes']) as $itemtype) { - - $classname = "PluginFields".ucfirst(strtolower($itemtype. - preg_replace('/s$/', '', $this->fields['name']))); - $class_filename = strtolower($itemtype. - preg_replace('/s$/', '', $this->fields['name'])).".class.php"; - - $injection_filename = strtolower($itemtype. - preg_replace('/s$/', '', $this->fields['name']))."injection.class.php"; + $classname = self::getClassname($itemtype, $this->fields['name']); + $sysname = self::getSystemName($itemtype, $this->fields['name']); + $class_filename = $sysname.".class.php"; + $injection_filename = $sysname."injection.class.php"; //delete fields $field_obj = new PluginFieldsField; - $fields = $field_obj->find("plugin_fields_containers_id = ".$this->fields['id']); - foreach ($fields as $fields_id => $field) { - $field_obj->delete(array('id' => $fields_id)); - } + $field_obj->deleteByCriteria([ + 'plugin_fields_containers_id' => $this->fields['id'] + ]); //delete profiles $profile_obj = new PluginFieldsProfile; - $profiles = $profile_obj->find("plugin_fields_containers_id = ".$this->fields['id']); - foreach ($profiles as $profiles_id => $profile) { - $profile_obj->delete(array('id' => $profiles_id)); - } + $profile_obj->deleteByCriteria([ + 'plugin_fields_containers_id' => $this->fields['id'] + ]); //delete label translations $translation_obj = new PluginFieldsLabelTranslation(); - $translations = $translation_obj->find("plugin_fields_itemtype = '" . self::getType() . - "' AND plugin_fields_items_id = ". $this->fields['id']); - foreach ($translations as $translation_id => $translation) { - $translation_obj->delete(['id' => $translation_id]); - } + $translation_obj->deleteByCriteria([ + 'plugin_fields_itemtype' => self::getType(), + 'plugin_fields_items_id' => $this->fields['id'] + ]); //delete table if (class_exists($classname)) { @@ -418,7 +499,7 @@ static function preItemPurge($item) { if (in_array($itemtype, $itemtypes)) { $classname = 'PluginFields' . $itemtype . getSingular($container['name']); $fields = new $classname(); - $fields->deleteByCriteria(array('items_id' => $item->fields['id'])); + $fields->deleteByCriteria(['items_id' => $item->fields['id']], true); } } return true; @@ -428,7 +509,7 @@ static function getTypeName($nb = 0) { return __("Block", "fields"); } - public function showForm($ID, $options=array()) { + public function showForm($ID, $options = array()) { global $CFG_GLPI; $this->initForm($ID, $options); @@ -438,7 +519,7 @@ public function showForm($ID, $options=array()) { echo ""; echo "".__("Label")." : "; echo ""; - Html::autocompletionTextField($this, 'label', array('value' => $this->fields["label"])); + Html::autocompletionTextField($this, 'label', ['value' => $this->fields["label"]]); echo ""; echo " "; echo " "; @@ -453,16 +534,15 @@ public function showForm($ID, $options=array()) { } else { Dropdown::showFromArray('type', self::getTypes(), - array('value' => $this->fields["type"], - 'rand' => $rand)); - $params = array('type' => '__VALUE__', - 'itemtype' => $this->fields["itemtypes"], - 'subtype' => $this->fields['subtype'], - 'rand' => $rand); + ['value' => $this->fields["type"], + 'rand' => $rand]); Ajax::updateItemOnSelectEvent("dropdown_type$rand", "itemtypes_$rand", "../ajax/container_itemtypes_dropdown.php", - $params); + ['type' => '__VALUE__', + 'itemtype' => $this->fields["itemtypes"], + 'subtype' => $this->fields['subtype'], + 'rand' => $rand]); } echo ""; echo "".__("Associated item type")." : "; @@ -484,8 +564,8 @@ public function showForm($ID, $options=array()) { } else { echo " "; - self::showFormItemtype(array('rand' => $rand, - 'subtype' => $this->fields['subtype'])); + self::showFormItemtype(['rand' => $rand, + 'subtype' => $this->fields['subtype']]); echo ""; } echo ""; @@ -530,20 +610,19 @@ static function showFormItemtype($params = array()) { $rand = $params['rand']; Dropdown::showFromArray("itemtypes", self::getItemtypes($is_domtab), - array('rand' => $rand, - 'multiple' => !$is_domtab, - 'width' => 200, - 'display_emptychoice' => $is_domtab)); + ['rand' => $rand, + 'multiple' => !$is_domtab, + 'width' => 200, + 'display_emptychoice' => $is_domtab]); if ($is_domtab) { - $params = array('type' => '__VALUE0__', - 'itemtype' => '__VALUE1__', - 'subtype' => $params["subtype"], - 'rand' => $rand); Ajax::updateItemOnSelectEvent(array("dropdown_type$rand", "dropdown_itemtypes$rand"), "subtype_$rand", "../ajax/container_subtype_dropdown.php", - $params); + ['type' => '__VALUE0__', + 'itemtype' => '__VALUE1__', + 'subtype' => $params["subtype"], + 'rand' => $rand]); } } @@ -566,7 +645,7 @@ static function showFormSubtype($params, $display = false) { if (count($tabs)) { // delete Log of array (don't work with this tab) - $tabs_to_remove = array('Log$1', 'TicketFollowup$1', 'TicketTask$1', 'Document_Item$1'); + $tabs_to_remove = ['Log$1', 'TicketFollowup$1', 'TicketTask$1', 'Document_Item$1']; foreach ($tabs_to_remove as $tab_to_remove) { if (isset($tabs[$tab_to_remove])) { unset($tabs[$tab_to_remove]); @@ -575,7 +654,7 @@ static function showFormSubtype($params, $display = false) { // For delete number : foreach ($tabs as $key => &$value) { - $results = array(); + $results = []; if (preg_match_all('#(.+)#', $value, $results)) { $value = str_replace($results[0][0], "", $value); } @@ -585,7 +664,10 @@ static function showFormSubtype($params, $display = false) { $params['subtype'] = null; } - $out .= Dropdown::showFromArray('subtype', $tabs, array('value' => $params['subtype'], 'width' => '100%', 'display' => false)); + $out .= Dropdown::showFromArray('subtype', $tabs, + ['value' => $params['subtype'], + 'width' => '100%', + 'display' => false]); $out .= ""; } } @@ -609,61 +691,63 @@ static function getItemtypes($is_domtab) { $tabs = []; - $assets = ['Computer' => _n("Computer", "Computers", 2)]; + $assets = ['Computer' => Computer::getTypeName(2)]; if (!$is_domtab) { $assets += [ - 'Monitor' => _n("Monitor", "Monitors", 2), - 'Software' => _n("Software", "Software", 2), - 'NetworkEquipment' => _n("Network", "Networks", 2), - 'Peripheral' => _n("Device", "Devices", 2), - 'Printer' => _n("Printer", "Printers", 2), - 'CartridgeItem' => _n("Cartridge", "Cartridges", 2), - 'ConsumableItem' => _n("Consumable", "Consumables", 2), - 'Phone' => _n("Phone", "Phones", 2) + 'Monitor' => Monitor::getTypeName(2), + 'Software' => Software::getTypeName(2), + 'NetworkEquipment' => NetworkEquipment::getTypeName(2), + 'Peripheral' => Peripheral::getTypeName(2), + 'Printer' => Printer::getTypeName(2), + 'CartridgeItem' => CartridgeItem::getTypeName(2), + 'ConsumableItem' => ConsumableItem::getTypeName(2), + 'Phone' => Phone::getTypeName(2) ]; } $tabs[__('Assets')] = $assets; $assistance = [ - 'Ticket' => _n("Ticket", "Tickets", 2), - 'Problem' => _n("Problem", "Problems", 2), - 'Change' => _n("Change", "Changes", 2), + 'Ticket' => Ticket::getTypeName(2), + 'Problem' => Problem::getTypeName(2), + 'Change' => Change::getTypeName(2), ]; if (!$is_domtab) { $assistance += [ - 'TicketRecurrent' => __("Recurrent tickets") + 'TicketRecurrent' => TicketRecurrent::getTypeName(2) ]; } $tabs[__('Assistance')] = $assistance; if (!$is_domtab) { $tabs += [ - __("Management") => array( - 'SoftwareLicense' => _n("License", "Licenses", 2), - 'Budget' => _n("Budget", "Budgets", 2), - 'Supplier' => _n("Supplier", "Suppliers", 2), - 'Contact' => _n("Contact", "Contacts", 2), - 'Contract' => _n("Contract", "Contracts", 2), - 'Document' => _n("Document", "Documents", 2)), - __("Tools") => array( - 'Project' => __("Project"), - 'ProjectTask' => _n("Project task", "Project tasks", 2), - 'Reminder' => _n("Note", "Notes", 2), - 'RSSFeed' => __("RSS feed")) + __("Management") => [ + 'SoftwareLicense' => SoftwareLicense::getTypeName(2), + 'Budget' => Budget::getTypeName(2), + 'Supplier' => Supplier::getTypeName(2), + 'Contact' => Contact::getTypeName(2), + 'Contract' => Contract::getTypeName(2), + 'Document' => Document::getTypeName(2) + ], + __("Tools") => [ + 'Project' => Project::getTypeName(2), + 'ProjectTask' => ProjectTask::getTypeName(2), + 'Reminder' => Reminder::getTypeName(2), + 'RSSFeed' => RSSFeed::getTypeName(2), + ] ]; } $administration = []; if (!$is_domtab) { $administration += [ - 'User' => _n("User", "Users", 2), - 'Group' => _n("Group", "Groups", 2) + 'User' => User::getTypeName(2), + 'Group' => Group::getTypeName(2) ]; } - $administration['Entity'] = _n("Entity", "Entities", 2); + $administration['Entity'] = Entity::getTypeName(2); if (!$is_domtab) { $administration += [ - 'Profile' => _n("Profile", "Profiles", 2) + 'Profile' => Profile::getTypeName(2) ]; } $tabs[__('Administration')] = $administration; @@ -673,35 +757,53 @@ static function getItemtypes($is_domtab) { if ($isPlugin) { $plugin_name = Plugin::getInfo($isPlugin['plugin'], 'name'); - $tabs[__("Plugins")][$itemtype] = $plugin_name . ' - ' . $itemtype::getTypeName(Session::getPluralNumber()); + $tabs[__("Plugins")][$itemtype] = $plugin_name.' - '.$itemtype::getTypeName(2); } } + if (!$is_domtab) { + $dropdowns = []; + // flatten dropdows + $raw_dropdowns = Dropdown::getStandardDropdownItemTypes(); + array_walk_recursive($raw_dropdowns, function($val, $key) use (&$dropdowns) { + $dropdowns[$key] = $val; + }); + $tabs[__('Dropdowns')] = $dropdowns; + + $tabs[__('Other')] = [ + 'NetworkPort' => NetworkPort::getTypeName(2), + 'Notification' => Notification::getTypeName(2), + 'NotificationTemplate' => NotificationTemplate::getTypeName(2), + ]; + } + return $tabs; } static function getTypes() { - return array( + return [ 'tab' => __("Add tab", "fields"), 'dom' => __("Insertion in the form (before save button)", "fields"), 'domtab' => __("Insertion in the form of a specific tab (before save button)", "fields") - ); + ]; } static function getEntries($type = 'tab', $full = false) { + global $DB; + $sql_type = "1=1"; if ($type !== "all") { $sql_type = "`type` = '$type'"; } - if (!TableExists("glpi_plugin_fields_containers")) { + if (!$DB->tableExists(self::getTable())) { return false; } - $itemtypes = array(); + $itemtypes = []; $container = new self; - $profile = new PluginFieldsProfile; - $found = $container->find("$sql_type AND is_active = 1", "`label`"); + $profile = new PluginFieldsProfile; + $found = $container->find("$sql_type AND is_active = 1", "`label`"); foreach ($found as $item) { //entities restriction if (!in_array($item['entities_id'], $_SESSION['glpiactiveentities'])) { @@ -745,38 +847,41 @@ static function getEntries($type = 'tab', $full = false) { static function getUsedItemtypes($type = 'all', $must_be_active = false) { global $DB; - $itemtypes = array(); - $where = ($type == 'all') ? '1=1' : 'type = "' . $type . '"'; + $itemtypes = []; + $where = $type == 'all' + ? '1=1' + : 'type = "'.$type.'"'; if ($must_be_active) { $where .= ' AND is_active = 1'; } $query = 'SELECT DISTINCT `itemtypes` FROM `glpi_plugin_fields_containers` - WHERE ' . $where; + WHERE '.$where; $result = $DB->query($query); while (list($data) = $DB->fetch_array($result)) { $jsonitemtype = json_decode($data); - $itemtypes = array_merge($itemtypes, $jsonitemtype); + $itemtypes = array_merge($itemtypes, $jsonitemtype); } return $itemtypes; } - function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { + function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { $itemtypes = self::getEntries('tab', true); if (isset($itemtypes[$item->getType()])) { - $tabs_entries = array(); - $container = new self; + $tabs_entries = []; + $container = new self; foreach ($itemtypes[$item->getType()] as $tab_name => $tab_label) { // needs to check if entity of item is in hierachy of $tab_name foreach ($container->find("`is_active` = 1 AND `name` = '$tab_name'") as $data) { $dataitemtypes = json_decode($data['itemtypes']); if (in_array(get_class($item), $dataitemtypes) != FALSE) { - $entities = array( $data['entities_id'] ); + $entities = [$data['entities_id']]; if ($data['is_recursive']) { - $entities = getSonsOf( getTableForItemType( 'Entity' ), $data['entities_id']); + $entities = getSonsOf(getTableForItemType('Entity'), $data['entities_id']); } + if (in_array($item->fields['entities_id'], $entities)) { $tabs_entries[$tab_name] = $tab_label; } @@ -789,7 +894,6 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { - //retrieve container for current tab $container = new self; $found_c = $container->find("`type` = 'tab' AND `name` = '$tabnum' AND is_active = 1"); @@ -820,12 +924,11 @@ function updateFieldsValues($data, $itemtype, $massiveaction = false) { $container_obj = new PluginFieldsContainer; $container_obj->getFromDB($data['plugin_fields_containers_id']); - $items_id = $data['items_id']; + $items_id = $data['items_id']; + $classname = self::getClassname($itemtype, $container_obj->fields['name']); - $classname = "PluginFields".ucfirst($itemtype. - preg_replace('/s$/', '', $container_obj->fields['name'])); - $obj = new $classname; //check if data already inserted + $obj = new $classname; $found = $obj->find("items_id = $items_id"); if (empty($found)) { // add fields data @@ -842,7 +945,7 @@ function updateFieldsValues($data, $itemtype, $massiveaction = false) { //construct history on itemtype object (Historical tab) self::constructHistory($data['plugin_fields_containers_id'], $items_id, - $itemtype, $data, $first_found); + $itemtype, $data, $first_found); } return true; @@ -858,7 +961,7 @@ function updateFieldsValues($data, $itemtype, $massiveaction = false) { * @return nothing */ static function constructHistory($containers_id, $items_id, $itemtype, $data, - $old_values = array()) { + $old_values = array()) { // Don't log few itemtypes $obj = new $itemtype(); if ($obj->dohistory == false) { @@ -869,8 +972,13 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data, $searchoptions = self::getAddSearchOptions($itemtype, $containers_id); //define non-data keys - $blacklist_k = array('plugin_fields_containers_id' => 0, 'items_id' => 0, 'itemtype' => $itemtype, - 'update_fields_values' => 0, '_glpi_csrf_token' => 0); + $blacklist_k = [ + 'plugin_fields_containers_id' => 0, + 'items_id' => 0, + 'itemtype' => $itemtype, + 'update_fields_values' => 0, + '_glpi_csrf_token' => 0 + ]; //remove non-data keys $data = array_diff_key($data, $blacklist_k); @@ -883,7 +991,7 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data, //log only not empty values if (!empty($value)) { //prepare log - $changes = array(0, "N/A", $value); + $changes = [0, "N/A", $value]; //find searchoption foreach ($searchoptions as $id_search_option => $searchoption) { @@ -892,13 +1000,14 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data, //manage dropdown values if ($searchoption['datatype'] === 'dropdown') { - $changes = array($id_search_option, "", - Dropdown::getDropdownName($searchoption['table'], $value)); + $changes = [$id_search_option, + "", + Dropdown::getDropdownName($searchoption['table'], $value)]; } //manage bool dropdown values if ($searchoption['datatype'] === 'bool') { - $changes = array($id_search_option, "", Dropdown::getYesNo($value)); + $changes = [$id_search_option, "", Dropdown::getYesNo($value)]; } } } @@ -911,7 +1020,7 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data, // -- update existing item -- //find changes - $updates = array(); + $updates = []; foreach ($old_values as $key => $old_value) { if (!isset($data[$key]) || empty($old_value) && empty($data[$key]) @@ -921,7 +1030,7 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data, } if ($data[$key] !== $old_value) { - $updates[$key] = array(0, $old_value, $data[$key]); + $updates[$key] = [0, $old_value, $data[$key]]; } } @@ -962,9 +1071,9 @@ static function constructHistory($containers_id, $items_id, $itemtype, $data, static function validateValues($data, $itemtype, $massiveaction) { global $DB; - $valid = true; - $empty_errors = array(); - $number_errors = array(); + $valid = true; + $empty_errors = []; + $number_errors = []; $container = new self(); $container->getFromDB($data['plugin_fields_containers_id']); @@ -974,15 +1083,15 @@ static function validateValues($data, $itemtype, $massiveaction) { $data['plugin_fields_containers_id']); foreach ($fields as $fields_id => $field) { - if ($field['type'] == "yesno" ||$field['type'] == "header") { + if ($field['type'] == "yesno" || $field['type'] == "header") { continue; } $name = $field['name']; if (isset($data[$name])) { $value = $data[$name]; - } else if (isset($data['plugin_fields_' . $name . 'dropdowns_id'])) { - $value = $data['plugin_fields_' . $name . 'dropdowns_id']; + } else if (isset($data['plugin_fields_'.$name.'dropdowns_id'])) { + $value = $data['plugin_fields_'.$name.'dropdowns_id']; } else if ($field['mandatory'] == 1) { $tablename = "glpi_plugin_fields_" . strtolower( $itemtype . getPlural(preg_replace('/s$/', '', $container->fields['name'])) @@ -1013,9 +1122,12 @@ static function validateValues($data, $itemtype, $massiveaction) { $field['label'] = PluginFieldsLabelTranslation::getLabelFor($field); // Check mandatory fields - if (($field['mandatory'] == 1) - && (empty($value) - || (in_array($field['type'], array('date', 'datetime')) && $value == 'NULL'))) { + if ($field['mandatory'] == 1 + && ($value == "" + || in_array($field['type'], ['dropdown', 'dropdownuser']) + && $value == 0 + || in_array($field['type'], ['date', 'datetime']) + && $value == 'NULL')) { $empty_errors[] = $field['label']; $valid = false; } else if ($field['type'] == 'number' && !empty($value) && !is_numeric($value)) { @@ -1031,18 +1143,18 @@ static function validateValues($data, $itemtype, $massiveaction) { } if (!empty($empty_errors)) { - Session::AddMessageAfterRedirect(__("Some mandatory fields are empty", "fields") - . " : " . implode(', ', $empty_errors), false, ERROR); + Session::AddMessageAfterRedirect(__("Some mandatory fields are empty", "fields"). + " : ".implode(', ', $empty_errors), false, ERROR); } if (!empty($number_errors)) { - Session::AddMessageAfterRedirect(__("Some numeric fields contains non numeric values", "fields") - . " : " . implode(', ', $number_errors), false, ERROR); + Session::AddMessageAfterRedirect(__("Some numeric fields contains non numeric values", "fields"). + " : ".implode(', ', $number_errors), false, ERROR); } if (!empty($url_errors)) { - Session::AddMessageAfterRedirect(__("Some URL fields contains invalid links", "fields") - . " : " . implode(', ', $url_errors), false, ERROR); + Session::AddMessageAfterRedirect(__("Some URL fields contains invalid links", "fields"). + " : ".implode(', ', $url_errors), false, ERROR); } return $valid; @@ -1051,7 +1163,9 @@ static function validateValues($data, $itemtype, $massiveaction) { static function findContainer($itemtype, $type = 'tab', $subtype = '') { $sql_type = "`type` = '$type'"; - $entity = isset($_SESSION['glpiactiveentities']) ? $_SESSION['glpiactiveentities'] : 0; + $entity = isset($_SESSION['glpiactiveentities']) + ? $_SESSION['glpiactiveentities'] + : 0; $sql_entity = getEntitiesRestrictRequest("AND", "", "", $entity, true, true); $sql_subtype = ''; @@ -1087,8 +1201,8 @@ static function findContainer($itemtype, $type = 'tab', $subtype = '') { } else { $condition = "`plugin_fields_containers_id` = '$id'"; } - $found = $profile->find("`profiles_id` = '" . $_SESSION['glpiactiveprofile']['id'] . "' - AND $condition"); + $found = $profile->find("`profiles_id` = '".$_SESSION['glpiactiveprofile']['id']."' + AND $condition"); $first_found = array_shift($found); if ($first_found['right'] == NULL || $first_found['right'] == 0) { return false; @@ -1116,7 +1230,7 @@ static function postItemAdd(CommonDBTM $item) { if ($container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction']))) { return true; } - return $item->input = array(); + return $item->input = []; } } @@ -1134,10 +1248,11 @@ static function preItemUpdate(CommonDBTM $item) { $data = $item->plugin_fields_data; //update data $container = new self(); - if (count($data) == 0 || $container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction']))) { + if (count($data) == 0 + || $container->updateFieldsValues($data, $item->getType(), isset($_REQUEST['massiveaction']))) { return true; } - return $item->input = array(); + return $item->input = []; } } @@ -1163,10 +1278,9 @@ static function preItem(CommonDBTM $item) { if ($type == 'domtab') { $subtype = $_REQUEST['_plugin_fields_subtype']; } - $c_id = self::findContainer(get_Class($item), $type, $subtype); - if ($c_id === false) { - $c_id = self::findContainer(get_Class($item)); //tries for 'tab' - if ($c_id === false) { + if (false === ($c_id = self::findContainer(get_Class($item), $type, $subtype))) { + // tries for 'tab' + if (false === ($c_id = self::findContainer(get_Class($item)))) { return false; } } @@ -1186,13 +1300,14 @@ static function preItem(CommonDBTM $item) { $item->fields = $item->input; } - $current_entity = ($item::getType() == Entity::getType() ? $item->getID() : $item->fields['entities_id']); + $current_entity = $item::getType() == Entity::getType() + ? $item->getID() + : $item->fields['entities_id']; if (!in_array($current_entity, $entities)) { return false; } - $data = self::populateData($c_id, $item); - if ($data !== false) { + if (false !== ($data = self::populateData($c_id, $item))) { if (self::validateValues($data, $item::getType(), isset($_REQUEST['massiveaction'])) === false) { return $item->input = []; } @@ -1213,7 +1328,8 @@ static function preItem(CommonDBTM $item) { static private function populateData($c_id, CommonDBTM $item) { //find fields associated to found container $field_obj = new PluginFieldsField(); - $fields = $field_obj->find("plugin_fields_containers_id = $c_id AND type != 'header'", "ranking"); + $fields = $field_obj->find("plugin_fields_containers_id = $c_id + AND type != 'header'", "ranking"); //prepare data to update $data = ['plugin_fields_containers_id' => $c_id]; @@ -1251,7 +1367,7 @@ static private function populateData($c_id, CommonDBTM $item) { static function getAddSearchOptions($itemtype, $containers_id = false) { global $DB; - $opt = array(); + $opt = []; $i = 76665; @@ -1369,11 +1485,6 @@ static function getAddSearchOptions($itemtype, $containers_id = false) { private static function getSubtypes($item) { $tabs = []; switch ($item::getType()) { - case Computer::getType(): - $tabs = [ - 'Computer$1' => __('Operating system') - ]; - break; case Ticket::getType(): case Problem::getType(): $tabs = [ @@ -1408,4 +1519,25 @@ private static function getSubtypes($item) { return $tabs; } + + /** + * Retrieve the classname for a label (raw_name) & an itemtype + * @param string $itemtype the name of associated CommonDBTM class + * @param string $raw_name the label of container + * @return string the classname + */ + static function getClassname($itemtype = "", $raw_name = "") { + return "PluginFields".ucfirst(self::getSystemName($itemtype, $raw_name)); + } + + /** + * Retrieve the systemname for a label (raw_name) & an itemtype + * Used to generate class files + * @param string $itemtype the name of associated CommonDBTM class + * @param string $raw_name the label of container + * @return string the classname + */ + static function getSystemName($itemtype = "", $raw_name = "") { + return strtolower($itemtype.preg_replace('/s$/', '', $raw_name)); + } } diff --git a/inc/dropdown.class.php b/inc/dropdown.class.php index 80d76cdc..8f4e1134 100644 --- a/inc/dropdown.class.php +++ b/inc/dropdown.class.php @@ -49,7 +49,7 @@ static function uninstall() { global $DB; //remove dropdown tables and files - if (TableExists("glpi_plugin_fields_fields")) { + if ($DB->tableExists("glpi_plugin_fields_fields")) { require_once "field.class.php"; $field = new PluginFieldsField; $dropdowns = $field->find("`type` = 'dropdown'"); diff --git a/inc/field.class.php b/inc/field.class.php index acd3cf09..9844a15a 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -18,10 +18,9 @@ static function canCreate() { static function install(Migration $migration, $version) { global $DB; - $obj = new self(); - $table = $obj->getTable(); + $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB->tableExists($table)) { $migration->displayMessage(sprintf(__("Installing %s"), $table)); $query = "CREATE TABLE IF NOT EXISTS `$table` ( @@ -45,16 +44,16 @@ static function install(Migration $migration, $version) { $migration->displayMessage("Updating $table"); - if (!FieldExists($table, 'is_active')) { - $migration->addField($table, 'is_active', 'bool', array('value' => 1)); + if (!$DB->fieldExists($table, 'is_active')) { + $migration->addField($table, 'is_active', 'bool', ['value' => 1]); $migration->addKey($table, 'is_active', 'is_active'); } - if (!FieldExists($table, 'is_readonly')) { - $migration->addField( $table, 'is_readonly', 'bool', array('default' => false)); + if (!$DB->fieldExists($table, 'is_readonly')) { + $migration->addField( $table, 'is_readonly', 'bool', ['default' => false]); $migration->addKey($table, 'is_readonly', 'is_readonly'); } - if (!FieldExists($table, 'mandatory')) { - $migration->addField($table, 'mandatory', 'bool', array('value' => 0)); + if (!$DB->fieldExists($table, 'mandatory')) { + $migration->addField($table, 'mandatory', 'bool', ['value' => 0]); } $migration->executeMigration(); @@ -64,8 +63,7 @@ static function install(Migration $migration, $version) { static function uninstall() { global $DB; - $obj = new self(); - $DB->query("DROP TABLE IF EXISTS `".$obj->getTable()."`"); + $DB->query("DROP TABLE IF EXISTS `".self::getTable()."`"); return true; } @@ -84,8 +82,8 @@ function prepareInputForAdd($input) { if ($input['type'] === "dropdown") { //search if dropdown already exist in this container $found = $this->find("name = '".$input['name']."' - AND plugin_fields_containers_id = '". - $input['plugin_fields_containers_id']."'"); + AND plugin_fields_containers_id = '". + $input['plugin_fields_containers_id']."'"); //reject adding for same dropdown on same bloc if (!empty($found)) { @@ -115,8 +113,7 @@ function prepareInputForAdd($input) { $container_obj = new PluginFieldsContainer; $container_obj->getFromDB($input['plugin_fields_containers_id']); foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) { - $classname = "PluginFields" . ucfirst(strtolower($itemtype . - preg_replace('/s$/', '', $container_obj->fields['name']))); + $classname = PluginFieldsContainer::getClassname($itemtype, $container_obj->fields['name']); $classname::addField($input['name'], $input['type']); } } @@ -132,8 +129,9 @@ function pre_deleteItem() { global $DB; //remove field in container table - if ($this->fields['type'] !== "header" && !isset($_SESSION['uninstall_fields']) - && !isset($_SESSION['delete_container'])) { + if ($this->fields['type'] !== "header" + && !isset($_SESSION['uninstall_fields']) + && !isset($_SESSION['delete_container'])) { if ($this->fields['type'] === "dropdown") { $oldname = $this->fields['name']; @@ -144,20 +142,17 @@ function pre_deleteItem() { $container_obj = new PluginFieldsContainer; $container_obj->getFromDB($this->fields['plugin_fields_containers_id']); foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) { - $classname = "PluginFields" . ucfirst(strtolower($itemtype . - preg_replace('/s$/', '', $container_obj->fields['name']))); + $classname = PluginFieldsContainer::getClassname($itemtype, $container_obj->fields['name']); $classname::removeField($this->fields['name']); } - $classname::removeField($this->fields['name']); } //delete label translations $translation_obj = new PluginFieldsLabelTranslation(); - $translations = $translation_obj->find("plugin_fields_itemtype = '" . self::getType() . - "' AND plugin_fields_items_id = ". $this->fields['id']); - foreach ($translations as $translation_id => $translation) { - $translation_obj->delete(['id' => $translation_id]); - } + $translation_obj->deleteByCriteria([ + 'plugin_fields_itemtype' => self::getType(), + 'plugin_fields_items_id' => $this->fields['id'] + ]); if (isset($oldname)) { $this->fields['name'] = $oldname; @@ -209,8 +204,7 @@ function prepareName($input) { $container = new PluginFieldsContainer; $container->getFromDB($input['plugin_fields_containers_id']); - $field = new self; - + $field = new self; $field_name = $input['name']; $i = 2; while (count($field->find("name = '$field_name'")) > 0) { @@ -230,7 +224,7 @@ function getNextRanking() { global $DB; $sql = "SELECT max(`ranking`) AS rank - FROM `".$this->getTable()."` + FROM `".self::getTable()."` WHERE `plugin_fields_containers_id` = '". $this->fields['plugin_fields_containers_id']."'"; $result = $DB->query($sql); @@ -253,19 +247,18 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { } return self::createTabEntry(__("Fields", "fields"), - countElementsInTable($this->getTable(), + countElementsInTable(self::getTable(), "`plugin_fields_containers_id` = '".$item->getID()."'")); } static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { - $fup = new self(); $fup->showSummary($item); return true; } - function defineTabs($options=array()) { - $ong = array(); + function defineTabs($options = array()) { + $ong = []; $this->addDefaultFormTab($ong); $this->addStandardTab('PluginFieldsLabelTranslation', $ong, $options); @@ -278,7 +271,7 @@ function showSummary($container) { $cID = $container->fields['id']; // Display existing Fields - $tmp = array('plugin_fields_containers_id' => $cID); + $tmp = ['plugin_fields_containers_id' => $cID]; $canadd = $this->can(-1, CREATE, $tmp); $query = "SELECT `id`, `label` @@ -289,38 +282,38 @@ function showSummary($container) { $rand = mt_rand(); - echo "
\n"; - echo "\n"; + echo Html::scriptEnd(); echo "
". - ""; - echo __("Add a new field", "fields")."

\n"; + ""; + echo __("Add a new field", "fields")."
"; if ($DB->numrows($result) == 0) { echo ""; echo "
".__("No field for this block", "fields")."
"; } else { echo '
'; - echo ''; + echo Html::hidden("_plugin_fields_containers_id", ['value' => $cID, + 'id' => 'plugin_fields_containers_id']); echo ""; echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; echo ""; - echo "\n"; + echo ""; $fields_type = self::getTypes(); @@ -331,15 +324,15 @@ function showSummary($container) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "'; - echo "\n"; + echo ""; } } } echo '
" . __("Label") . "" . __("Type") . "" . __("Default values") . "" . __("Mandatory field") . "" . __("Active") . "" . __("Read only", "fields") . "".__("Label") ."".__("Type") ."".__("Default values") ."".__("Mandatory field") ."".__("Active") ."".__("Read only", "fields")." 
"; - echo "{$this->fields['label']}"; + echo "{$this->fields['label']}"; echo "".$fields_type[$this->fields['type']]."".$this->fields['default_value']."".Dropdown::getYesNo($this->fields["mandatory"]).""; - echo ($this->fields['is_active'] == 1) + echo ($this->isActive()) ? __('Yes') - : '' . __('No') . ''; + : ''.__('No').''; echo ""; @@ -348,20 +341,24 @@ function showSummary($container) { echo ''; echo '
'; - echo '#'; + echo '#'; echo '
'; echo '
'; echo '
'; - echo Html::scriptBlock('redipsInit()'); + echo Html::scriptBlock('$(document).ready(function() { + redipsInit() + });'); } - function showForm($ID, $options=array()) { + function showForm($ID, $options = array()) { + global $CFG_GLPI; + if (isset($options['parent_id']) && !empty($options['parent_id'])) { $container = new PluginFieldsContainer; $container->getFromDB($options['parent_id']); @@ -375,7 +372,7 @@ function showForm($ID, $options=array()) { } else { // Create item $edit = false; - $_SESSION['saveInput'] = array('plugin_fields_containers_id' => $container->getField('id')); + $_SESSION['saveInput'] = ['plugin_fields_containers_id' => $container->getField('id')]; } $this->initForm($ID, $options); @@ -384,9 +381,8 @@ function showForm($ID, $options=array()) { echo ""; echo "".__("Label")." : "; echo ""; - echo ""; - Html::autocompletionTextField($this, 'label', array('value' => $this->fields["label"])); + echo Html::hidden('plugin_fields_containers_id', ['value' => $container->getField('id')]); + Html::autocompletionTextField($this, 'label', ['value' => $this->fields["label"]]); echo ""; if (!$edit) { @@ -394,25 +390,30 @@ function showForm($ID, $options=array()) { echo ""; echo "".__("Type")." : "; echo ""; - Dropdown::showFromArray('type', self::getTypes(), - array('value' => $this->fields["type"])); + Dropdown::showFromArray('type', self::getTypes(), ['value' => $this->fields["type"]]); echo ""; } echo "".__("Default values")." : "; echo ""; Html::autocompletionTextField($this, 'default_value', - array('value' => $this->fields["default_value"])); + ['value' => $this->fields["default_value"]]); if ($this->fields["type"] == "dropdown") { - echo ' - '.__('Configure', 'fields').''; + echo ' + '.__('Configure', 'fields').' + '; + } + if (in_array($this->fields['type'], ['date', 'datetime'])) { + echo ""; } echo ""; echo ""; echo ""; - echo "" . __('Active') . " :"; + echo "".__('Active')." :"; echo ""; Dropdown::showYesNo('is_active', $this->fields["is_active"]); echo ""; @@ -439,7 +440,7 @@ static function showForTabContainer($c_id, $items_id, $itemtype) { //profile restriction (for reading profile) $profile = new PluginFieldsProfile; $found = $profile->find("`profiles_id` = '".$_SESSION['glpiactiveprofile']['id']."' - AND `plugin_fields_containers_id` = '$c_id'"); + AND `plugin_fields_containers_id` = '$c_id'"); $first_found = array_shift($found); $canedit = ($first_found['right'] == CREATE); @@ -447,10 +448,10 @@ static function showForTabContainer($c_id, $items_id, $itemtype) { $field_obj = new self(); $fields = $field_obj->find("plugin_fields_containers_id = $c_id AND is_active = 1", "ranking"); echo "
"; - echo ""; - echo ""; - echo ""; + "/plugins/fields/front/container.form.php'>"; + echo Html::hidden('plugin_fields_containers_id', ['value' => $c_id]); + echo Html::hidden('items_id', ['value' => $items_id]); + echo Html::hidden('itemtype', ['value' => $itemtype]); echo ""; echo self::prepareHtmlFields($fields, $items_id, $itemtype, $canedit); @@ -493,13 +494,13 @@ static private function showDomContainer($c_id, $itemtype, $items_id, $type = "d $field_obj = new self(); $fields = $field_obj->find($condition." AND is_active = 1", "ranking"); - echo ""; - echo ""; + echo Html::hidden('_plugin_fields_type', ['value' => $type]); + echo Html::hidden('_plugin_fields_subtype', ['value' => $subtype]); echo self::prepareHtmlFields($fields, $items_id, $itemtype); } /** - * Display field sin any existing tab + * Display fields in any existing tab * * @param array $params [item, options] * @@ -508,38 +509,45 @@ static private function showDomContainer($c_id, $itemtype, $items_id, $type = "d static function showForTab($params) { global $CFG_GLPI; - $item = $params['item']; + $item = $params['item']; $options = $params['options']; + $functions = array_column(debug_backtrace(), 'function'); + if (!isset($_SESSION['glpi_tabs'][strtolower($item::getType())])) { return; }; $subtype = $_SESSION['glpi_tabs'][strtolower($item::getType())]; - $type = (substr($subtype, -strlen('$main')) === '$main' ? 'dom' : 'domtab'); + $type = substr($subtype, -strlen('$main')) === '$main' + || in_array('showPrimaryForm', $functions) + || in_array('showFormHelpdesk', $functions) + ? 'dom' + : 'domtab'; + + // if we are in 'dom' or 'tab' type, no need for subtype ('domtab' specific) + if ($type != 'domtab') { + $subtype = ""; + } //find container (if not exist, do nothing) if (isset($_REQUEST['c_id'])) { $c_id = $_REQUEST['c_id']; - } else { - $c_id = PluginFieldsContainer::findContainer(get_Class($item), $type, $subtype); - if ($c_id === false) { - $c_id = PluginFieldsContainer::findContainer(get_Class($item)); //tries for 'tab' - if ($c_id === false) { - return false; - } - } + } else if (!$c_id = PluginFieldsContainer::findContainer(get_Class($item), $type, $subtype)) { + return false; } //need to check if container is usable on this object entity $loc_c = new PluginFieldsContainer; $loc_c->getFromDB($c_id); - $entities = array($loc_c->fields['entities_id']); + $entities = [$loc_c->fields['entities_id']]; if ($loc_c->fields['is_recursive']) { $entities = getSonsOf(getTableForItemType('Entity'), $loc_c->fields['entities_id']); } - $current_entity = ($item::getType() == Entity::getType() ? $item->getID() : $item->fields['entities_id']); + $current_entity = $item::getType() == Entity::getType() + ? $item->getID() + : $item->fields['entities_id']; if (!in_array($current_entity, $entities)) { return false; } @@ -550,8 +558,8 @@ static function showForTab($params) { } $current_url = $_SERVER['REQUEST_URI']; if (strpos($current_url, ".form.php") === false - && strpos($current_url, ".injector.php") === false - && strpos($current_url, ".public.php") === false) { + && strpos($current_url, ".injector.php") === false + && strpos($current_url, ".public.php") === false) { return false; } @@ -610,8 +618,8 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true } if (in_array($items_obj->fields['status'], $items_obj->getClosedStatusArray()) - || in_array($items_obj->fields['status'], $items_obj->getSolvedStatusArray()) - || $first_found_p['right'] != CREATE) { + || in_array($items_obj->fields['status'], $items_obj->getSolvedStatusArray()) + || $first_found_p['right'] != CREATE) { $canedit = false; } } else { @@ -643,7 +651,7 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true } } - if (! $field['is_readonly']) { + if (!$field['is_readonly']) { if ($field['type'] == "dropdown") { if (isset($_SESSION['plugin']['fields']['values_sent']["plugin_fields_". $field['name']. @@ -658,8 +666,14 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true } //get default value - if (empty($value) && !empty($field['default_value'])) { + if ($value === "" && $field['default_value'] !== "") { $value = $field['default_value']; + + // shortcut for date/datetime + if (in_array($field['type'], ['date', 'datetime']) + && $value == 'now') { + $value = $_SESSION["glpi_currenttime"]; + } } //show field @@ -668,7 +682,9 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true $html.= ""; } - $required = ($field['mandatory'] == 1) ? "*" : ''; + $required = $field['mandatory'] == 1 + ? "*" + : ''; $field['itemtype'] = self::getType(); $txt_label = PluginFieldsLabelTranslation::getLabelFor($field); @@ -691,7 +707,7 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true case 'text': $value = Html::cleanInputText($value); if ($canedit && !$readonly) { - $html.= ""; + $html.= Html::input($field['name'], ['value' => $value]); } else { $html.= $value; } @@ -699,7 +715,7 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true case 'url': $value = Html::cleanInputText($value); if ($canedit && !$readonly) { - $html.= ""; + $html.= Html::input($field['name'], ['value' => $value]); if ($value != '') { $html .= "" . __('show', 'fields') . ""; } @@ -709,8 +725,13 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true break; case 'textarea': if ($canedit && !$readonly) { - $html.= ""; + $html.= Html::textarea([ + 'name' => $field['name'], + 'value' => $value, + 'cols' => 45, + 'rows' => 4, + 'display' => false, + ]); } else { $html.= nl2br($value); } @@ -721,17 +742,16 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true $obj = new $itemtype; $obj->getFromDB($items_id); - ob_start(); if (strpos($field['name'], "dropdowns_id") !== false) { $dropdown_itemtype = getItemTypeForTable( getTableNameForForeignKeyField($field['name'])); } else { $dropdown_itemtype = PluginFieldsDropdown::getClassname($field['name']); } - Dropdown::show($dropdown_itemtype, array('value' => $value, - 'entity' => $obj->getEntityID())); - $html.= ob_get_contents(); - ob_end_clean(); + $html.= Dropdown::show($dropdown_itemtype, + ['value' => $value, + 'entity' => $obj->getEntityID(), + 'display' => false]); } else { $dropdown_table = "glpi_plugin_fields_".$field['name']."dropdowns"; $html.= Dropdown::getDropdownName($dropdown_table, $value); @@ -739,30 +759,23 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true break; case 'yesno': if ($canedit && !$readonly) { - ob_start(); - Dropdown::showYesNo($field['name'], $value); - $html.= ob_get_contents(); - ob_end_clean(); + $html.= Dropdown::showYesNo($field['name'], $value, -1, ['display' => false]); } else { $html.= Dropdown::getYesNo($value); } break; case 'date': if ($canedit && !$readonly) { - ob_start(); - Html::showDateField($field['name'], ['value' => $value]); - $html.= ob_get_contents(); - ob_end_clean(); + $html.= Html::showDateField($field['name'], ['value' => $value, + 'display' => false]); } else { $html.= Html::convDate($value); } break; case 'datetime': if ($canedit && !$readonly) { - ob_start(); - Html::showDateTimeField($field['name'], ['value' => $value]); - $html.= ob_get_contents(); - ob_end_clean(); + $html.= Html::showDateTimeField($field['name'], ['value' => $value, + 'display' => false]); } else { $html.= Html::convDateTime($value); } @@ -772,14 +785,12 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true continue; } if ($canedit && !$readonly) { - ob_start(); - User::dropdown(array('name' => $field['name'], - 'value' => $value, - 'entity' => -1, - 'right' => 'all', - 'condition' => 'is_active=1 && is_deleted=0')); - $html.= ob_get_contents(); - ob_end_clean(); + $html.= User::dropdown(['name' => $field['name'], + 'value' => $value, + 'entity' => -1, + 'right' => 'all', + 'display' => false, + 'condition' => 'is_active=1 && is_deleted=0']); } else { $showuserlink = 0; if (Session::haveRight('user', 'r')) { @@ -809,9 +820,6 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true static function showSingle($itemtype, $searchOption, $massiveaction = false) { global $DB; - //find container for field in massive action - $field_obj = new self; - //clean dropdown [pre/su]fix if exists $cleaned_linkfield = preg_replace("/plugin_fields_(.*)dropdowns_id/", "$1", $searchOption['linkfield']); @@ -831,16 +839,16 @@ static function showSingle($itemtype, $searchOption, $massiveaction = false) { $data = $DB->fetch_assoc($res); //display an hidden post field to store container id - echo ""; + echo Html::hidden('c_id', ['value' => $data['plugin_fields_containers_id']]); //prepare array for function prepareHtmlFields - $fields = array(array( - 'id' => 0, - 'type' => $searchOption['pfields_type'], - 'plugin_fields_containers_id' => $data['plugin_fields_containers_id'], - 'name' => $cleaned_linkfield, - 'is_readonly' => $data['is_readonly'] - )); + $fields = [[ + 'id' => 0, + 'type' => $searchOption['pfields_type'], + 'plugin_fields_containers_id' => $data['plugin_fields_containers_id'], + 'name' => $cleaned_linkfield, + 'is_readonly' => $data['is_readonly'] + ]]; //show field echo self::prepareHtmlFields($fields, 0, $itemtype, true, false, $massiveaction); @@ -848,8 +856,13 @@ static function showSingle($itemtype, $searchOption, $massiveaction = false) { return true; } + function post_getEmpty() { + $this->fields['is_active'] = 1; + $this->fields['type'] = 'text'; + } + static function getTypes() { - return array( + return [ 'header' => __("Header", "fields"), 'text' => __("Text (single line)", "fields"), 'textarea' => __("Text (multiples lines)", "fields"), @@ -860,7 +873,7 @@ static function getTypes() { 'date' => __("Date", "fields"), 'datetime' => __("Date & time", "fields"), 'dropdownuser' => _n("User", "Users", 2) - ); + ]; } function post_addItem() { diff --git a/inc/labeltranslation.class.php b/inc/labeltranslation.class.php index ea7fc46d..cec1466b 100644 --- a/inc/labeltranslation.class.php +++ b/inc/labeltranslation.class.php @@ -14,10 +14,9 @@ class PluginFieldsLabelTranslation extends CommonDBTM { static function install(Migration $migration, $version) { global $DB; - $obj = new self(); - $table = $obj->getTable(); + $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB->tableExists($table)) { $migration->displayMessage(sprintf(__("Installing %s"), $table)); $query = "CREATE TABLE IF NOT EXISTS `$table` ( @@ -43,8 +42,7 @@ static function install(Migration $migration, $version) { static function uninstall() { global $DB; - $obj = new self(); - $DB->query("DROP TABLE IF EXISTS `".$obj->getTable()."`"); + $DB->query("DROP TABLE IF EXISTS `".self::getTable()."`"); return true; } @@ -56,30 +54,25 @@ static function getTypeName($nb = 0) { static function createForItem(CommonDBTM $item) { $translation = new PluginFieldsLabelTranslation(); - $translation->add( - [ - 'plugin_fields_itemtype' => $item::getType(), - 'plugin_fields_items_id' => $item->getID(), - 'language' => $_SESSION['glpilanguage'], - 'label' => $item->fields['label'] - ] - ); + $translation->add([ + 'plugin_fields_itemtype' => $item::getType(), + 'plugin_fields_items_id' => $item->getID(), + 'language' => $_SESSION['glpilanguage'], + 'label' => $item->fields['label'] + ]); return true; } function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { - $nb = countElementsInTable($this->getTable(), - "`plugin_fields_itemtype` = '{$item::getType()}' AND - `plugin_fields_items_id` = '{$item->getID()}'"); + $nb = countElementsInTable(self::getTable(), + "`plugin_fields_itemtype` = '{$item::getType()}' AND + `plugin_fields_items_id` = '{$item->getID()}'"); return self::createTabEntry(self::getTypeName($nb), $nb); } static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { self::showTranslations($item); - /*$fup = new self(); - $fup->showSummary($item); - return true;*/ } /** @@ -95,18 +88,17 @@ static function showTranslations(CommonDBTM $item) { $canedit = $item->can($item->getID(), UPDATE); $rand = mt_rand(); if ($canedit) { - echo "
\n"; - echo "\n"; + echo Html::scriptEnd(); echo ""; foreach ($found as $data) { echo ""; + onClick=\"viewEditTranslation".$data['id']."$rand();\"" : '').">"; if ($canedit) { echo "
".__("Label", "fields")."
"; Html::showMassiveActionCheckBox(__CLASS__, $data["id"]); @@ -147,17 +138,16 @@ static function showTranslations(CommonDBTM $item) { } echo ""; if ($canedit) { - echo "\n\n"; + echo Html::scriptEnd(); } echo Dropdown::getLanguageName($data['language']); echo ""; @@ -172,7 +162,7 @@ static function showTranslations(CommonDBTM $item) { } } else { echo ""; - echo "
" . __("No translation found")."
"; + echo "
".__("No translation found")."
"; } return true; @@ -207,17 +197,20 @@ function showForm($itemtype, $items_id, $id=-1) { echo Dropdown::getLanguageName($this->fields['language']); } else { Dropdown::showLanguages("language", - array('display_none' => false, - 'value' => $_SESSION['glpilanguage'], - 'used' => self::getAlreadyTranslatedForItem($itemtype, $items_id))); + ['display_none' => false, + 'value' => $_SESSION['glpilanguage'], + 'used' => self::getAlreadyTranslatedForItem($itemtype, $items_id)]); } echo " "; echo ""; echo ""; echo ""; - echo ""; - echo "\n"; + echo Html::input('label', [ + 'value' => $this->fields["label"], + 'id' => 'label' + ]); + echo ""; $this->showFormButtons(); return true; @@ -234,8 +227,8 @@ function showForm($itemtype, $items_id, $id=-1) { static function getAlreadyTranslatedForItem($itemtype, $items_id) { global $DB; - $tab = array(); - foreach ($DB->request(getTableForItemType(__CLASS__), + $tab = []; + foreach ($DB->request(self::getTable(), "`plugin_fields_itemtype` = '$itemtype' AND `plugin_fields_items_id` = '$items_id'") as $data) { $tab[$data['language']] = $data['language']; @@ -252,10 +245,9 @@ static function getAlreadyTranslatedForItem($itemtype, $items_id) { */ static public function getLabelFor(array $item) { $obj = new self; - $found = $obj->find( - "`plugin_fields_itemtype` = '{$item['itemtype']}' AND - `plugin_fields_items_id`='{$item['id']}' AND - `language` = '{$_SESSION['glpilanguage']}'" + $found = $obj->find("`plugin_fields_itemtype` = '{$item['itemtype']}' + AND `plugin_fields_items_id`='{$item['id']}' + AND `language` = '{$_SESSION['glpilanguage']}'" ); if (count($found) > 0) { diff --git a/inc/menu.class.php b/inc/menu.class.php index 37d396b3..a7837c13 100644 --- a/inc/menu.class.php +++ b/inc/menu.class.php @@ -13,11 +13,11 @@ static function getMenuContent() { } $front_fields = "/plugins/fields/front"; - $menu = array(); + $menu = []; $menu['title'] = self::getMenuName(); $menu['page'] = "$front_fields/container.php"; - $itemtypes = array('PluginFieldsContainer' => 'fieldscontainer'); + $itemtypes = ['PluginFieldsContainer' => 'fieldscontainer']; foreach ($itemtypes as $itemtype => $option) { $menu['options'][$option]['title'] = $itemtype::getTypeName(2); diff --git a/inc/migration.class.php b/inc/migration.class.php index c30e0b08..0cb8e3fd 100644 --- a/inc/migration.class.php +++ b/inc/migration.class.php @@ -3,9 +3,11 @@ class PluginFieldsMigration { static function install(Migration $migration, $version) { + global $DB; + $fields_migration = new self; - if (TableExists("glpi_plugin_customfields_fields")) { + if ($DB->tableExists("glpi_plugin_customfields_fields")) { if (!$fields_migration->updateFromCustomfields()) { return false; } @@ -26,7 +28,7 @@ function updateFromCustomfields($glpi_version = "0.80") { } function migrateCustomfieldTypes($old_type) { - $types = array( + $types = [ 'sectionhead' => 'header', 'general' => 'text', 'money' => 'text', @@ -36,23 +38,23 @@ function migrateCustomfieldTypes($old_type) { 'dropdown' => 'dropdown', 'yesno' => 'yesno', 'date' => 'date' - ); + ]; return $types[$old_type]; } static function getSQLType($field_type) { - $types = array( - 'text' => 'VARCHAR(255) DEFAULT NULL', - 'url' => 'TEXT DEFAULT NULL', - 'textarea' => 'TEXT DEFAULT NULL', - 'number' => 'VARCHAR(255) DEFAULT NULL', - 'dropdown' => 'INT(11) NOT NULL DEFAULT 0', - 'yesno' => 'INT(11) NOT NULL DEFAULT 0', - 'date' => 'VARCHAR(255) DEFAULT NULL', - 'datetime' => 'VARCHAR(255) DEFAULT NULL', + $types = [ + 'text' => 'VARCHAR(255) DEFAULT NULL', + 'url' => 'TEXT DEFAULT NULL', + 'textarea' => 'TEXT DEFAULT NULL', + 'number' => 'VARCHAR(255) DEFAULT NULL', + 'dropdown' => 'INT(11) NOT NULL DEFAULT 0', + 'yesno' => 'INT(11) NOT NULL DEFAULT 0', + 'date' => 'VARCHAR(255) DEFAULT NULL', + 'datetime' => 'VARCHAR(255) DEFAULT NULL', 'dropdownuser' => 'INT(11) NOT NULL DEFAULT 0' - ); + ]; return $types[$field_type]; } diff --git a/inc/profile.class.php b/inc/profile.class.php index a86fe9f2..d7cb308f 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -5,10 +5,9 @@ class PluginFieldsProfile extends CommonDBTM { static function install(Migration $migration) { global $DB; - $obj = new self(); - $table = $obj->getTable(); + $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB->tableExists($table)) { $migration->displayMessage(sprintf(__("Installing %s"), $table)); $query = "CREATE TABLE IF NOT EXISTS `$table` ( @@ -30,19 +29,18 @@ static function install(Migration $migration) { static function uninstall() { global $DB; - $obj = new self(); - $DB->query("DROP TABLE IF EXISTS `".$obj->getTable()."`"); + $DB->query("DROP TABLE IF EXISTS `".self::getTable()."`"); return true; } - function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { + function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { return self::createTabEntry(_n("Profile", "Profiles", 2)); } - static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { + static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { $profile = new Profile; $found_profiles = $profile->find(); @@ -62,7 +60,8 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtempl echo ""; echo "".$profile_item['name'].""; echo ""; - Profile::dropdownNoneReadWrite("rights[".$profile_item['id']."]", $first_found['right']); + Profile::dropdownRight("rights[".$profile_item['id']."]", + ['value' => $first_found['right']]); echo ""; echo ""; } @@ -83,23 +82,23 @@ static function updateProfile($input) { $fields_profile = new self; foreach ($input['rights'] as $profiles_id => $right) { $found = $fields_profile->find("`profiles_id` = '$profiles_id' - AND `plugin_fields_containers_id` = '". - $input['plugin_fields_containers_id']."'"); + AND `plugin_fields_containers_id` = '". + $input['plugin_fields_containers_id']."'"); if (count( $found ) > 0) { $first_found = array_shift($found); - $fields_profile->update(array( + $fields_profile->update([ 'id' => $first_found['id'], 'profiles_id' => $profiles_id, 'plugin_fields_containers_id' => $input['plugin_fields_containers_id'], 'right' => $right - )); + ]); } else { - $fields_profile->add(array( + $fields_profile->add([ 'profiles_id' => $profiles_id, 'plugin_fields_containers_id' => $input['plugin_fields_containers_id'], 'right' => $right - )); + ]); } } @@ -113,11 +112,11 @@ static function createForContainer(PluginFieldsContainer $container) { $fields_profile = new self; foreach ($found_profiles as $profile_item) { - $fields_profile->add(array( + $fields_profile->add([ 'profiles_id' => $profile_item['id'], 'plugin_fields_containers_id' => $container->fields['id'], 'right' => CREATE - )); + ]); } return true; } @@ -128,17 +127,17 @@ static function addNewProfile(Profile $profile) { $fields_profile = new self; foreach ($found_containers as $container) { - $fields_profile->add(array( + $fields_profile->add([ 'profiles_id' => $profile->fields['id'], 'plugin_fields_containers_id' => $container['id'] - )); + ]); } return true; } static function deleteProfile(Profile $profile) { $fields_profile = new self; - $fields_profile->deleteByCriteria(array('profiles_id' => $profile->fields['id'])); + $fields_profile->deleteByCriteria(['profiles_id' => $profile->fields['id']]); return true; } } diff --git a/plugin.xml b/plugin.xml index f0553fe7..26e1fdd3 100644 --- a/plugin.xml +++ b/plugin.xml @@ -72,6 +72,10 @@ Il existe un [script de migration](https://github.com/pluginsGLPI/customfields/b Johan Cwiklinski + + 1.7.0 + 9.2 + 1.6.2 9.1.2 diff --git a/setup.php b/setup.php index 54e4f8b8..98f97428 100644 --- a/setup.php +++ b/setup.php @@ -26,7 +26,7 @@ -------------------------------------------------------------------------- */ -define ('PLUGIN_FIELDS_VERSION', '1.6.2'); +define ('PLUGIN_FIELDS_VERSION', '1.7.0'); if (!defined("PLUGINFIELDS_DIR")) { define("PLUGINFIELDS_DIR", GLPI_ROOT . "/plugins/fields"); @@ -34,23 +34,23 @@ if (!defined("PLUGINFIELDS_DOC_DIR")) { define("PLUGINFIELDS_DOC_DIR", GLPI_PLUGIN_DOC_DIR . "/fields"); - if (!file_exists(PLUGINFIELDS_DOC_DIR)) { - mkdir(PLUGINFIELDS_DOC_DIR); - } +} +if (!file_exists(PLUGINFIELDS_DOC_DIR)) { + mkdir(PLUGINFIELDS_DOC_DIR); } if (!defined("PLUGINFIELDS_CLASS_PATH")) { define("PLUGINFIELDS_CLASS_PATH", PLUGINFIELDS_DOC_DIR . "/inc"); - if (!file_exists(PLUGINFIELDS_CLASS_PATH)) { - mkdir(PLUGINFIELDS_CLASS_PATH); - } +} +if (!file_exists(PLUGINFIELDS_CLASS_PATH)) { + mkdir(PLUGINFIELDS_CLASS_PATH); } if (!defined("PLUGINFIELDS_FRONT_PATH")) { define("PLUGINFIELDS_FRONT_PATH", PLUGINFIELDS_DOC_DIR."/front"); - if (!file_exists(PLUGINFIELDS_FRONT_PATH)) { - mkdir(PLUGINFIELDS_FRONT_PATH); - } +} +if (!file_exists(PLUGINFIELDS_FRONT_PATH)) { + mkdir(PLUGINFIELDS_FRONT_PATH); } /** @@ -64,13 +64,10 @@ function plugin_init_fields() { $PLUGIN_HOOKS['csrf_compliant']['fields'] = true; + // manage autoload of plugin custom classes include_once(PLUGINFIELDS_DIR . "/vendor/autoload.php"); include_once(PLUGINFIELDS_DIR . "/inc/autoload.php"); - - $options = array( - PLUGINFIELDS_CLASS_PATH - ); - $pluginfields_autoloader = new PluginFieldsAutoloader($options); + $pluginfields_autoloader = new PluginFieldsAutoloader([PLUGINFIELDS_CLASS_PATH]); $pluginfields_autoloader->register(); $plugin = new Plugin(); @@ -79,48 +76,51 @@ function plugin_init_fields() { && Session::getLoginUserID() ) { // Init hook about itemtype(s) for plugin fields - $PLUGIN_HOOKS['plugin_fields'] = array(); + if (!isset($PLUGIN_HOOKS['plugin_fields'])) { + $PLUGIN_HOOKS['plugin_fields'] = array(); + } // When a Category is changed during ticket creation - if (isset($_POST) && !empty($_POST) && isset($_POST['_plugin_fields_type'])) { - if ($_SERVER['REQUEST_URI'] == Ticket::getFormURL()) { - //$_SESSION['plugin_fields']['Ticket'] = $_POST; - foreach ($_POST as $key => $value) { - if (! is_array($value)) { - $_SESSION['plugin']['fields']['values_sent'][$key] = stripcslashes($value); - } + if (isset($_POST) && !empty($_POST) + && isset($_POST['_plugin_fields_type']) + && $_SERVER['REQUEST_URI'] == Ticket::getFormURL()) { + foreach ($_POST as $key => $value) { + if (!is_array($value)) { + $_SESSION['plugin']['fields']['values_sent'][$key] = stripcslashes($value); } } } // complete rule engine - $PLUGIN_HOOKS['use_rules']['fields'] = array('PluginFusioninventoryTaskpostactionRule'); + $PLUGIN_HOOKS['use_rules']['fields'] = ['PluginFusioninventoryTaskpostactionRule']; $PLUGIN_HOOKS['rule_matched']['fields'] = 'plugin_fields_rule_matched'; if (isset($_SESSION['glpiactiveentities'])) { + // add link in plugin page $PLUGIN_HOOKS['config_page']['fields'] = 'front/container.php'; // add entry to configuration menu - $PLUGIN_HOOKS["menu_toadd"]['fields'] = array('config' => 'PluginFieldsMenu'); + $PLUGIN_HOOKS["menu_toadd"]['fields'] = ['config' => 'PluginFieldsMenu']; // add tabs to itemtypes Plugin::registerClass('PluginFieldsContainer', - array('addtabon' => array_unique(PluginFieldsContainer::getEntries()))); + ['addtabon' => array_unique(PluginFieldsContainer::getEntries())]); //include js and css - $debug = (isset($_SESSION['glpi_use_mode']) && $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE ? true : false); + $debug = (isset($_SESSION['glpi_use_mode']) + && $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE ? true : false); if (!$debug && file_exists(__DIR__ . '/css/fields.min.css')) { - $PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.min.css'; + $PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.min.css'; } else { - $PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.css'; + $PLUGIN_HOOKS['add_css']['fields'][] = 'css/fields.css'; } // Add/delete profiles to automaticaly to container - $PLUGIN_HOOKS['item_add']['fields']['Profile'] = array("PluginFieldsProfile", - "addNewProfile"); - $PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] = array("PluginFieldsProfile", - "deleteProfile"); + $PLUGIN_HOOKS['item_add']['fields']['Profile'] + = ["PluginFieldsProfile", "addNewProfile"]; + $PLUGIN_HOOKS['pre_item_purge']['fields']['Profile'] + = ["PluginFieldsProfile", "deleteProfile"]; //load drag and drop javascript library on Package Interface $PLUGIN_HOOKS['add_javascript']['fields'][] = "js/redips-drag-min.js"; @@ -133,7 +133,8 @@ function plugin_init_fields() { // Add Fields to Datainjection if ($plugin->isActivated('datainjection')) { - $PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] = "plugin_datainjection_populate_fields"; + $PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] + = "plugin_datainjection_populate_fields"; } //Retrieve dom container @@ -151,12 +152,9 @@ function plugin_init_fields() { } } + // Display fields in any existing tab $PLUGIN_HOOKS['post_item_form']['fields'] = ['PluginFieldsField', 'showForTab']; - - // Check class and front files for existing containers and dropdown fields - plugin_fields_checkFiles(); - } } @@ -168,12 +166,20 @@ function plugin_init_fields() { * @return array */ function plugin_version_fields() { - return array ('name' => __("Additionnal fields", "fields"), - 'version' => PLUGIN_FIELDS_VERSION, - 'author' => 'Teclib\', Olivier Moron', - 'homepage' => 'teclib.com', - 'license' => 'GPLv2+', - 'minGlpiVersion' => '9.1.2'); + return [ + 'name' => __("Additionnal fields", "fields"), + 'version' => PLUGIN_FIELDS_VERSION, + 'author' => 'Teclib\', Olivier Moron', + 'homepage' => 'teclib.com', + 'license' => 'GPLv2+', + 'requirements' => [ + 'glpi' => [ + 'min' => '9.2', + 'max' => '9.3', + 'dev' => true + ] + ] + ]; } /** @@ -183,48 +189,56 @@ function plugin_version_fields() { * @return boolean */ function plugin_fields_check_prerequisites() { - if (version_compare(GLPI_VERSION, '9.1.2', 'lt')) { - if (method_exists('Plugin', 'messageIncompatible')) { - echo Plugin::messageIncompatible('core', '9.1.2'); - } else { - echo "This plugin requires GLPI 9.1.2"; - } + $version = rtrim(GLPI_VERSION, '-dev'); + if (version_compare($version, '9.2', 'lt')) { + echo "This plugin requires GLPI 9.2"; return false; } return true; } +/** + * Check all stored containers files (classes & front) are present, or create they if needed + * + * @return void + */ +function plugin_fields_checkFiles($force = false) { + global $DB; -function plugin_fields_checkFiles() { $plugin = new Plugin(); + if ($force) { + //clean all existing files + array_map('unlink', glob(PLUGINFIELDS_DOC_DIR.'/*/*')); + } + if (isset($_SESSION['glpiactiveentities']) && $plugin->isInstalled('fields') && $plugin->isActivated('fields') && Session::getLoginUserID()) { - Plugin::registerClass('PluginFieldsContainer'); - Plugin::registerClass('PluginFieldsDropdown'); - Plugin::registerClass('PluginFieldsField'); - - if (TableExists("glpi_plugin_fields_containers")) { + if ($DB->tableExists(PluginFieldsContainer::getTable())) { $container_obj = new PluginFieldsContainer(); $containers = $container_obj->find(); foreach ($containers as $container) { - $itemtypes = (count($container['itemtypes']) > 0) ? json_decode($container['itemtypes'], TRUE) : array(); + $itemtypes = (strlen($container['itemtypes']) > 0) + ? json_decode($container['itemtypes'], TRUE) + : []; foreach ($itemtypes as $itemtype) { - $classname = "PluginFields".ucfirst($itemtype. - preg_replace('/s$/', '', $container['name'])); + $classname = PluginFieldsContainer::getClassname($itemtype, $container['name']); if (!class_exists($classname)) { PluginFieldsContainer::generateTemplate($container); } + + // regenerate table (and fields) also + $classname::install($container['id']); } } } - if (TableExists("glpi_plugin_fields_fields")) { + if ($DB->tableExists(PluginFieldsField::getTable())) { $fields_obj = new PluginFieldsField(); $fields = $fields_obj->find("`type` = 'dropdown'"); foreach ($fields as $field) { @@ -242,11 +256,5 @@ function plugin_fields_checkFiles() { * @return boolean */ function plugin_fields_check_config($verbose = false) { - if (true) { // Your configuration check - return true; - } - if ($verbose) { - echo __("Installed / not configured"); - } - return false; + return true; } diff --git a/templates/container.class.tpl b/templates/container.class.tpl index 9131b141..731d7150 100644 --- a/templates/container.class.tpl +++ b/templates/container.class.tpl @@ -4,13 +4,14 @@ class %%CLASSNAME%% extends CommonDBTM { static $rightname = '%%ITEMTYPE_RIGHT%%'; - static function install() { + static function install($containers_id = 0) { global $DB; $obj = new self(); $table = $obj->getTable(); - if (!TableExists($table)) { + // create Table + if (!$DB->tableExists($table)) { $query = "CREATE TABLE IF NOT EXISTS `$table` ( `id` INT(11) NOT NULL auto_increment, `items_id` INT(11) NOT NULL, @@ -22,6 +23,15 @@ class %%CLASSNAME%% extends CommonDBTM ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; $DB->query($query) or die ($DB->error()); } + + // and its fields + if ($containers_id) { + foreach ($DB->request(PluginFieldsField::getTable(), [ + 'plugin_fields_containers_id' => $containers_id + ]) as $field) { + self::addField($field['name'], $field['type']); + } + } } static function uninstall() { @@ -32,22 +42,18 @@ class %%CLASSNAME%% extends CommonDBTM } static function addField($fieldname, $type) { - global $DB; - - $sql_type = PluginFieldsMigration::getSQLType($type); + if ($type != 'header') { + $sql_type = PluginFieldsMigration::getSQLType($type); - $obj = new self(); - return $DB->query("ALTER TABLE `".$obj->getTable()."` - ADD COLUMN `$fieldname` $sql_type - "); + $migration = new Migration(0); + $migration->addField(self::getTable(), $fieldname, $sql_type); + $migration->migrationOneTable(self::getTable()); + } } static function removeField($fieldname) { - global $DB; + $migration = new Migration; + $migration->dropField(self::getTable(), $fieldname); - $obj = new self(); - return $DB->query("ALTER TABLE `".$obj->getTable()."` - DROP COLUMN `$fieldname` - "); } } diff --git a/templates/dropdown.class.tpl b/templates/dropdown.class.tpl index 6bbbd953..32a37ff7 100644 --- a/templates/dropdown.class.tpl +++ b/templates/dropdown.class.tpl @@ -20,7 +20,7 @@ class %%CLASSNAME%% extends CommonTreeDropdown { $obj = new self(); $table = $obj->getTable(); - if (!TableExists($table)) { + if (!$DB->tableExists($table)) { $query = "CREATE TABLE IF NOT EXISTS `$table` ( `id` INT(11) NOT NULL auto_increment, `name` VARCHAR(255) DEFAULT NULL, diff --git a/tests/DbTestCase.php b/tests/FieldsDbTestCase.php similarity index 53% rename from tests/DbTestCase.php rename to tests/FieldsDbTestCase.php index 7bbb54af..fbe0bcd0 100644 --- a/tests/DbTestCase.php +++ b/tests/FieldsDbTestCase.php @@ -32,7 +32,7 @@ // Generic test classe, to be extended for CommonDBTM Object -class DbTestCase extends atoum { +class FieldsDbTestCase extends \DbTestCase { public function setUp() { if (!file_exists(PLUGINFIELDS_DOC_DIR)) { @@ -54,70 +54,6 @@ public function setUp() { } } - // By default, no session, not connected - $_SESSION = []; - } - - public function beforeTestMethod($testMethod) { - global $DB; - - // Need Innodb -- $DB->begin_transaction() -- workaround: - $DB->objcreated = array(); - } - - public function afterTestMethod($testMethod) { - global $DB; - - // Need Innodb -- $DB->rollback() -- workaround: - foreach ($DB->objcreated as $table => $ids) { - foreach ($ids as $id) { - $DB->query($q = "DELETE FROM `$table` WHERE `id`=$id"); - } - } - unset($DB->objcreated); - } - - - /** - * Connect using the test user - */ - protected function login() { - - $auth = new Auth(); - if (!$auth->login(TU_USER, TU_PASS, true)) { - $this->markTestSkipped('No login'); - } - } - - /** - * Get a unique random string - */ - protected function getUniqueString() { - static $str = NULL; - - if (is_null($this->str)) { - return $this->str = uniqid('str'); - } - return $this->str .= 'x'; - } - - /** - * Get a unique random integer - */ - protected function getUniqueInteger() { - static $int = NULL; - - if (is_null($this->int)) { - return $this->int = mt_rand(1000, 10000); - } - return $this->int++; - } - - /** - * change current entity - */ - protected function setEntity($entityname, $subtree) { - - $this->assertTrue(Session::changeActiveEntities(getItemByTypeName('Entity', $entityname, true), $subtree)); + parent::setUp(); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f4da32c5..259ae456 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -8,11 +8,16 @@ define('GLPI_ROOT', dirname(dirname(dirname(__DIR__)))); define("GLPI_CONFIG_DIR", GLPI_ROOT . "/tests"); include GLPI_ROOT . "/inc/includes.php"; -//include_once GLPI_ROOT . '/tests/DbTestCase.php'; +include_once GLPI_ROOT . '/tests/DbTestCase.php'; //install plugin $plugin = new \Plugin(); $plugin->getFromDBbyDir('fields'); +//check from prerequisites as Plugin::install() does not! +if (!plugin_fields_check_prerequisites()) { + echo "\nPrerequisites are not met!"; + die(1); +} if (!$plugin->isInstalled('fields')) { call_user_func([$plugin, 'install'], $plugin->getID()); } @@ -20,4 +25,4 @@ call_user_func([$plugin, 'activate'], $plugin->getID()); } -include_once __DIR__ . '/DbTestCase.php'; +include_once __DIR__ . '/FieldsDbTestCase.php'; diff --git a/tests/units/PluginFieldsContainer.php b/tests/units/PluginFieldsContainer.php index d5665aa0..87047fae 100644 --- a/tests/units/PluginFieldsContainer.php +++ b/tests/units/PluginFieldsContainer.php @@ -2,7 +2,7 @@ namespace tests\units; -class PluginFieldsContainer extends \DbTestCase { +class PluginFieldsContainer extends \FieldsDbTestCase { public function testGetTypeName() { $this->string(\PluginFieldsContainer::getTypeName())->isIdenticalTo('Block'); @@ -45,9 +45,11 @@ public function testGetTypes() { public function testShowFormSubtype() { $subtypes = [ - \Computer::getType() => "/]*>" . "