Skip to content

Commit

Permalink
Merge branch 'release/1.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Dec 7, 2018
2 parents 81f848e + bb84a7d commit 45d015b
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 164 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ env:
global:
- DB=mysql
matrix:
- GLPIVER=9.3/bugfixes
- GLPIVER=9.4/bugfixes

before_script:
- composer self-update
- 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 scripts/cliinstall.php --db=glpitest --user=root --tests
- bin/console glpi:database:install --config-dir=./tests --no-interaction --db-name=glpitest --db-host=127.0.0.1 --db-user=root
- mv ../fields plugins/fields
- cd plugins/fields
- composer install -o
Expand Down
248 changes: 165 additions & 83 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function plugin_fields_getDropdown() {
$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);
Expand Down Expand Up @@ -301,7 +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']);

Expand Down
74 changes: 41 additions & 33 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,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
Expand All @@ -81,7 +81,7 @@ static function install(Migration $migration, $version) {
$compcontainer->getFromDB($comptab);

$fields = new PluginFieldsField();
$fields = $fields->find("plugin_fields_containers_id='$ostab'");
$fields = $fields->find(['plugin_fields_containers_id' => $ostab]);

$classname = self::getClassname(Computer::getType(), $oscontainer->fields['name']);
$osdata = new $classname;
Expand All @@ -92,7 +92,7 @@ static function install(Migration $migration, $version) {
//add fields to compcontainer
foreach ($fields as $field) {
$newname = $field['name'];
$compfields = $fields->find("plugin_fields_containers_id='$comptab' AND name='$newname'");
$compfields = $fields->find(['plugin_fields_containers_id' => $comptab, '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'");
Expand Down Expand Up @@ -327,7 +327,7 @@ function prepareInputForAdd($input) {

if ($input['type'] === "dom") {
//check for already exist dom container with this itemtype
$found = $this->find("`type`='dom'");
$found = $this->find(['type' => 'dom']);
if (count($found) > 0) {
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
foreach (json_decode($founditemtypes) as $founditemtype) {
Expand All @@ -342,7 +342,7 @@ function prepareInputForAdd($input) {

if ($input['type'] === "domtab") {
//check for already exist domtab container with this itemtype on this tab
$found = $this->find("`type`='domtab' AND `subtype`='{$input['subtype']}'");
$found = $this->find(['type' => 'domtab', 'subtype' => $input['subtype']]);
if (count($found) > 0) {
foreach (array_column( $found, 'itemtypes' ) as $founditemtypes) {
foreach (json_decode( $founditemtypes ) as $founditemtype) {
Expand All @@ -364,7 +364,7 @@ function prepareInputForAdd($input) {
}

//check for already existing container with same name
$found = $this->find("`name`='".$input['name']."'");
$found = $this->find(['name' => $input['name']]);
if (count($found) > 0) {
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
foreach (json_decode($founditemtypes) as $founditemtype) {
Expand Down Expand Up @@ -648,7 +648,7 @@ static function showFormSubtype($params, $display = false) {

if (count($tabs)) {
// delete Log of array (don't work with this tab)
$tabs_to_remove = ['Log$1', 'TicketFollowup$1', 'TicketTask$1', 'Document_Item$1'];
$tabs_to_remove = ['Log$1', 'Document_Item$1'];
foreach ($tabs_to_remove as $tab_to_remove) {
if (isset($tabs[$tab_to_remove])) {
unset($tabs[$tab_to_remove]);
Expand Down Expand Up @@ -794,9 +794,11 @@ static function getTypes() {
static function getEntries($type = 'tab', $full = false) {
global $DB;

$sql_type = "1=1";
$condition = [
'is_active' => 1,
];
if ($type !== "all") {
$sql_type = "`type` = '$type'";
$condition[] = ['type' => $type];
}

if (!$DB->tableExists(self::getTable())) {
Expand All @@ -806,7 +808,7 @@ static function getEntries($type = 'tab', $full = false) {
$itemtypes = [];
$container = new self;
$profile = new PluginFieldsProfile;
$found = $container->find("$sql_type AND is_active = 1", "`label`");
$found = $container->find($condition, 'label');
foreach ($found as $item) {
//entities restriction
if (!in_array($item['entities_id'], $_SESSION['glpiactiveentities'])) {
Expand All @@ -824,9 +826,9 @@ static function getEntries($type = 'tab', $full = false) {
continue;
}
//profiles restriction
$found = $profile->find("`profiles_id` = '".$_SESSION['glpiactiveprofile']['id']."'
AND `plugin_fields_containers_id` = '".$item['id']."'
AND `right` >= ".READ);
$found = $profile->find(['profiles_id' => $_SESSION['glpiactiveprofile']['id'],
'plugin_fields_containers_id' => $item['id'],
'right' => ['>=', READ]]);
$first_found = array_shift($found);
if ($first_found['right'] == null || $first_found['right'] == 0) {
continue;
Expand Down Expand Up @@ -877,7 +879,7 @@ function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
$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) {
foreach ($container->find(['is_active' => 1, 'name' => $tab_name]) as $data) {
$dataitemtypes = json_decode($data['itemtypes']);
if (in_array(get_class($item), $dataitemtypes) != false) {
$entities = [$data['entities_id']];
Expand All @@ -899,7 +901,7 @@ 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");
$found_c = $container->find(['type' => 'tab', 'name' => $tabnum, 'is_active' => 1]);
foreach ($found_c as $data) {
$dataitemtypes = json_decode($data['itemtypes']);
if (in_array(get_class($item), $dataitemtypes) != false) {
Expand Down Expand Up @@ -932,7 +934,7 @@ function updateFieldsValues($data, $itemtype, $massiveaction = false) {

//check if data already inserted
$obj = new $classname;
$found = $obj->find("items_id = $items_id");
$found = $obj->find(['items_id' => $items_id]);
if (empty($found)) {
// add fields data
$obj->add($data);
Expand Down Expand Up @@ -1082,8 +1084,9 @@ static function validateValues($data, $itemtype, $massiveaction) {
$container->getFromDB($data['plugin_fields_containers_id']);

$field_obj = new PluginFieldsField();
$fields = $field_obj->find("plugin_fields_containers_id = ".
$data['plugin_fields_containers_id']);
$fields = $field_obj->find([
'plugin_fields_containers_id' => $data['plugin_fields_containers_id']
]);

foreach ($fields as $fields_id => $field) {
if ($field['type'] == "yesno" || $field['type'] == "header") {
Expand Down Expand Up @@ -1165,23 +1168,28 @@ static function validateValues($data, $itemtype, $massiveaction) {


static function findContainer($itemtype, $type = 'tab', $subtype = '') {
$sql_type = "`type` = '$type'";

$condition = [
'is_active' => 1,
['type' => $type],
];

$entity = isset($_SESSION['glpiactiveentities'])
? $_SESSION['glpiactiveentities']
: 0;
$sql_entity = getEntitiesRestrictRequest("AND", "", "", $entity, true, true);
$condition += getEntitiesRestrictCriteria("", "", $entity, true, true);

$sql_subtype = '';
if ($subtype != '') {
if ($subtype == $itemtype.'$main') {
$sql_subtype = " AND type = 'dom' ";
$condition[] = ['type' => 'dom'];
} else {
$sql_subtype = " AND type != 'dom' AND subtype = '$subtype' ";
$condition[] = ['type' => ['!=', 'dom']];
$condition['subtype'] = $subtype;
}
}

$container = new PluginFieldsContainer();
$itemtypes = $container->find($sql_type." AND is_active = 1 ".$sql_entity.$sql_subtype);
$itemtypes = $container->find($condition);
$id = 0;
if (count($itemtypes) < 1) {
return false;
Expand All @@ -1199,13 +1207,8 @@ static function findContainer($itemtype, $type = 'tab', $subtype = '') {
if (isset($_SESSION['glpiactiveprofile']['id'])) {
$profile = new PluginFieldsProfile();
if (isset($id)) {
if (is_array($id)) {
$condition = "`plugin_fields_containers_id` IN (" . implode(", ", $id) . ")";
} 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'],
'plugin_fields_containers_id' => $id]);
$first_found = array_shift($found);
if ($first_found['right'] == null || $first_found['right'] == 0) {
return false;
Expand Down Expand Up @@ -1331,8 +1334,13 @@ 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,
'type' => ['!=', 'header']
],
"ranking"
);

//prepare data to update
$data = ['plugin_fields_containers_id' => $c_id];
Expand Down
4 changes: 2 additions & 2 deletions inc/dropdown.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static function install(Migration $migration, $version) {
// OLD path: GLPI_ROOT."/plugins/fields/front/$class_filename"
// NEW path: PLUGINFIELDS_FRONT_PATH . "/$class_filename"
$obj = new PluginFieldsField;
$fields = $obj->find('type = "dropdown"');
$fields = $obj->find(['type' => 'dropdown']);
foreach ($fields as $field) {
//First, drop old fields from plugin directories
$class_filename = $field['name']."dropdown.class.php";
Expand Down Expand Up @@ -52,7 +52,7 @@ static function uninstall() {
if ($DB->tableExists("glpi_plugin_fields_fields")) {
require_once "field.class.php";
$field = new PluginFieldsField;
$dropdowns = $field->find("`type` = 'dropdown'");
$dropdowns = $field->find(['type' => 'dropdown']);
foreach ($dropdowns as $dropdown) {
self::destroy($dropdown['name']);
}
Expand Down
63 changes: 37 additions & 26 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ 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']."'");
$found = $this->find(
[
'name' => $input['name'],
'plugin_fields_containers_id' => $input['plugin_fields_containers_id'],
]
);

//reject adding for same dropdown on same bloc
if (!empty($found)) {
Expand Down Expand Up @@ -186,7 +189,7 @@ function prepareName($input) {

//for dropdown, if already exist, link to it
if (isset($input['type']) && $input['type'] === "dropdown") {
$found = $this->find("name = '".$input['name']."'");
$found = $this->find(['name' => $input['name']]);
if (!empty($found)) {
return $input['name'];
}
Expand All @@ -199,7 +202,7 @@ function prepareName($input) {
$field = new self;
$field_name = $input['name'];
$i = 2;
while (count($field->find("name = '$field_name'")) > 0) {
while (count($field->find(['name' => $field_name])) > 0) {
$field_name = $input['name'].$i;
$i++;
}
Expand Down Expand Up @@ -436,14 +439,14 @@ 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'");
$found = $profile->find(['profiles_id' => $_SESSION['glpiactiveprofile']['id'],
'plugin_fields_containers_id' => $c_id]);
$first_found = array_shift($found);
$canedit = ($first_found['right'] == CREATE);

//get fields for this container
$field_obj = new self();
$fields = $field_obj->find("plugin_fields_containers_id = $c_id AND is_active = 1", "ranking");
$fields = $field_obj->find(['plugin_fields_containers_id' => $c_id, 'is_active' => 1], "ranking");
echo "<form method='POST' action='".$CFG_GLPI["root_doc"].
"/plugins/fields/front/container.form.php'>";
echo Html::hidden('plugin_fields_containers_id', ['value' => $c_id]);
Expand Down Expand Up @@ -477,20 +480,21 @@ static function showForTabContainer($c_id, $items_id, $itemtype) {
* @return void
*/
static private function showDomContainer($c_id, $itemtype, $items_id, $type = "dom", $subtype = "") {
if (is_array($c_id)) {
$condition = "plugin_fields_containers_id IN (".implode(", ", $c_id).")";
} else {
$condition = "plugin_fields_containers_id = $c_id";
}

if ($c_id === false) {
$condition = "1=0";
if ($c_id !== false) {
//get fields for this container
$field_obj = new self();
$fields = $field_obj->find(
[
'plugin_fields_containers_id' => $c_id,
'is_active' => 1,
],
"ranking"
);
} else {
$fields = [];
}

//get fields for this container
$field_obj = new self();
$fields = $field_obj->find($condition." AND is_active = 1", "ranking");

echo Html::hidden('_plugin_fields_type', ['value' => $type]);
echo Html::hidden('_plugin_fields_subtype', ['value' => $subtype]);
echo self::prepareHtmlFields($fields, $items_id, $itemtype);
Expand Down Expand Up @@ -594,15 +598,22 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true
$obj = new $classname;

//find row for this object with the items_id
$found_values = $obj->find("plugin_fields_containers_id = ".
$first_field['plugin_fields_containers_id']." AND items_id = ".
$items_id);
$found_values = $obj->find(
[
'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'],
'items_id' => $items_id,
]
);
$found_v = array_shift($found_values);

// find profiles (to check if current profile can edit fields)
$fprofile = new PluginFieldsProfile;
$found_p = $fprofile->find("`profiles_id` = '".$_SESSION['glpiactiveprofile']['id']."'
AND `plugin_fields_containers_id` = '".$first_field['plugin_fields_containers_id']."'");
$found_p = $fprofile->find(
[
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'],
]
);
$first_found_p = array_shift($found_p);

// test status for "CommonITILObject" objects
Expand Down Expand Up @@ -787,7 +798,7 @@ static function prepareHtmlFields($fields, $items_id, $itemtype, $canedit = true
'entity' => -1,
'right' => 'all',
'display' => false,
'condition' => 'is_active=1 && is_deleted=0']);
'condition' => ['is_active' => 1, 'is_deleted' => 0]]);
} else {
$showuserlink = 0;
if (Session::haveRight('user', 'r')) {
Expand Down Expand Up @@ -879,7 +890,7 @@ function post_addItem() {
//dropdowns : create files
if ($input['type'] === "dropdown") {
//search if dropdown already exist in other container
$found = $this->find("id != " . $input['id'] . " AND name = '" . $input['name'] . "'");
$found = $this->find(['id' => ['!=', $input['id']], 'name' => $input['name']]);
//for dropdown, if already exist, don't create files
if (empty($found)) {
PluginFieldsDropdown::create($input);
Expand Down
Loading

0 comments on commit 45d015b

Please sign in to comment.