Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to iterator #671

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,24 @@ function plugin_fields_rule_matched($params = [])

if (isset($params['input']['plugin_fusioninventory_agents_id'])) {
foreach ($params['output'] as $field => $value) {
// check if current field is in a tab container
$query = "SELECT c.id
FROM glpi_plugin_fields_fields f
LEFT JOIN glpi_plugin_fields_containers c
ON c.id = f.plugin_fields_containers_id
WHERE f.name = '$field'";
$res = $DB->query($query);
if ($DB->numrows($res) > 0) {
$data = $DB->fetchAssoc($res);
// check if current field is in a tab container
$iterator = $DB->request([
'SELECT' => 'glpi_plugin_fields_containers.id',
'FROM' => 'glpi_plugin_fields_containers',
'LEFT JOIN' => [
'glpi_plugin_fields_fields' => [
'FKEY' => [
'glpi_plugin_fields_containers' => 'id',
'glpi_plugin_fields_fields' => 'plugin_fields_containers_id'
]
]
],
'WHERE' => [
'glpi_plugin_fields_fields.name' => $field,
]
]);
if (count($iterator) > 0) {
$data = $iterator->next();

//retrieve computer
$agents_id = $params['input']['plugin_fusioninventory_agents_id'];
Expand Down
107 changes: 79 additions & 28 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,20 @@ public static function installBaseData(Migration $migration, $version)
$migration->changeField($table, 'itemtype', 'itemtypes', 'longtext');
$migration->migrationOneTable($table);

$query = "UPDATE `$table` SET `itemtypes` = CONCAT('[\"', `itemtypes`, '\"]')";
$DB->query($query) or die($DB->error());
$DB->updateOrDie(
$table,
[
'itemtypes' => new QueryExpression(
sprintf(
'CONCAT(%s, %s, %s)',
$DB->quoteValue('[\"'),
$DB->quoteName('itemtype'),
$DB->quoteValue('\"]')
)
),
],
[1]
);
}

//add display preferences for this class
Expand Down Expand Up @@ -252,15 +264,36 @@ public static function installUserData(Migration $migration, $version)
$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'");
$DB->update(
'glpi_plugin_fields_fields',
[
'name' => $newname
],
[
'name' => $field['name'],
'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'");
$DB->update(
'glpi_plugin_fields_fields',
[
'plugin_fields_containers_id' => $comptab
],
[
'plugin_fields_containers_id' => $ostab
]
);
$DB->delete(
'glpi_plugin_fields_containers',
[
'id' => $ostab
]
);

//migrate existing data
$existings = $osdata->find();
Expand All @@ -275,9 +308,16 @@ public static function installUserData(Migration $migration, $version)
//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);
$DB->update(
'glpi_plugin_fields_containers',
[
'type' => 'dom',
'subtype' => null
],
[
'id' => $ostab
]
);
}
}

Expand Down Expand Up @@ -1017,15 +1057,25 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false)
{
global $DB;
$itemtypes = [];
$where = $type == 'all' ? '1=1' : ('type = "' . $type . '"');
$where = [];

if ($type !== 'all') {
$where['type'] = $type;
}

if ($must_be_active) {
$where .= ' AND is_active = 1';
$where['is_active'] = 1;
}

$query = 'SELECT DISTINCT `itemtypes` FROM `glpi_plugin_fields_containers` WHERE ' . $where;
$result = $DB->query($query);
while (list($data) = $DB->fetchArray($result)) {
$jsonitemtype = json_decode($data);
$iterator = $DB->request([
'SELECT' => 'itemtypes',
'DISTINCT' => true,
'FROM' => self::getTable(),
'WHERE' => $where,
]);

foreach ($iterator as $data) {
$jsonitemtype = json_decode($data['itemtypes']);
$itemtypes = array_merge($itemtypes, $jsonitemtype);
}

Expand Down Expand Up @@ -1354,19 +1404,20 @@ public static function validateValues($data, $itemtype, $massiveaction)
} else if ($field['mandatory'] == 1 && isset($data['items_id'])) {
$tablename = getTableForItemType(self::getClassname($itemtype, $container->fields['name']));

$query = "SELECT * FROM `$tablename` WHERE
`itemtype`='$itemtype'
AND `items_id`='{$data['items_id']}'
AND `plugin_fields_containers_id`='{$data['plugin_fields_containers_id']}'";

$db_result = [];
if ($result = $DB->query($query)) {
$db_result = $DB->fetchAssoc($result);
if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) {
$value = $db_result['plugin_fields_' . $name . 'dropdowns_id'];
} else if (isset($db_result[$name])) {
$value = $db_result[$name];
}
$iterator = $DB->request([
'FROM' => $tablename,
'WHERE' => [
'itemtype' => $itemtype,
'items_id' => $data['items_id'],
'plugin_fields_containers_id' => $data['plugin_fields_containers_id'],
],
]);

$db_result = $iterator->next();
if (isset($db_result['plugin_fields_' . $name . 'dropdowns_id'])) {
$value = $db_result['plugin_fields_' . $name . 'dropdowns_id'];
} else if (isset($db_result[$name])) {
$value = $db_result[$name];
}
} else {
if ($massiveaction) {
Expand Down
88 changes: 58 additions & 30 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,16 @@ public function post_purgeItem()
$old_container = $this->fields['plugin_fields_containers_id'];
$old_ranking = $this->fields['ranking'];

$query = "UPDATE $table SET
ranking = ranking-1
WHERE plugin_fields_containers_id = $old_container
AND ranking > $old_ranking";
$DB->query($query);
$D->update(
$table,
[
'ranking' => new QueryExpression($DB->quoteName('ranking') . ' - 1')
],
[
'plugin_fields_containers_id' => $old_container,
'ranking' => ['>', $old_ranking]
]
);

return true;
}
Expand Down Expand Up @@ -441,14 +446,18 @@ public function getNextRanking()
{
global $DB;

$sql = "SELECT max(`ranking`) AS `rank`
FROM `" . self::getTable() . "`
WHERE `plugin_fields_containers_id` = '" .
$this->fields['plugin_fields_containers_id'] . "'";
$result = $DB->query($sql);
$iterator = $DB->request([
'SELECT' => new \QueryExpression(
'max(' . $DB->quoteName('ranking') . ') AS ' . $DB->quoteName('rank')
),
'FROM' => self::getTable(),
'WHERE' => [
'plugin_fields_containers_id' => $this->fields['plugin_fields_containers_id']
]
]);

if ($DB->numrows($result) > 0) {
$data = $DB->fetchAssoc($result);
if (count($iterator) > 0) {
$data = $iterator->next();
return $data["rank"] + 1;
}
return 0;
Expand Down Expand Up @@ -496,14 +505,14 @@ public function showSummary($container)
$cID = $container->fields['id'];

// Display existing Fields
$query = "SELECT `id`, `label`
FROM `" . $this->getTable() . "`
WHERE `plugin_fields_containers_id` = '$cID'
ORDER BY `ranking` ASC";
$result = $DB->query($query);
$iterator = $DB->request([
'SELECT' => ['id', 'label'],
'FROM' => self::getTable(),
'WHERE' => ['plugin_fields_containers_id' => $cID],
'ORDER' => 'ranking ASC'
]);

$rand = mt_rand();

echo "<div id='viewField$cID$rand'></div>";

$ajax_params = [
Expand All @@ -525,7 +534,7 @@ public function showSummary($container)
"<a href='javascript:viewAddField$cID$rand();'>";
echo __("Add a new field", "fields") . "</a></div><br>";

if ($DB->numrows($result) == 0) {
if (count($iterator) == 0) {
echo "<table class='tab_cadre_fixe'><tr class='tab_bg_2'>";
echo "<th class='b'>" . __("No field for this block", "fields") . "</th></tr></table>";
} else {
Expand All @@ -548,7 +557,7 @@ public function showSummary($container)

Session::initNavigateListItems('PluginFieldsField', __('Fields list'));

while ($data = $DB->fetchArray($result)) {
foreach ($iterator as $data) {
if ($this->getFromDB($data['id'])) {
echo "<tr class='tab_bg_2' style='cursor:pointer'>";

Expand Down Expand Up @@ -1154,20 +1163,39 @@ public static function showSingle($itemtype, $searchOption, $massiveaction = fal
);

//find field
$query = "SELECT fields.plugin_fields_containers_id, fields.is_readonly, fields.multiple, fields.default_value
FROM glpi_plugin_fields_fields fields
LEFT JOIN glpi_plugin_fields_containers containers
ON containers.id = fields.plugin_fields_containers_id
AND containers.itemtypes LIKE '%$itemtype%'
WHERE fields.name = '$cleaned_linkfield'";
$res = $DB->query($query);
if ($DB->numrows($res) == 0) {
$iterator = $DB->request([
'SELECT' => [
'fields.plugin_fields_containers_id',
'fields.is_readonly',
'fields.multiple',
'fields.default_value'
],
'FROM' => self::getTable() . ' AS fields',
'LEFT JOIN' => [
'glpi_plugin_fields_containers AS containers' => [
'FKEY' => [
'containers' => 'id',
'fields' => 'plugin_fields_containers_id',
[
'AND' => [
'containers.itemtypes' => ['LIKE' => "%$itemtype%"]
]
]
]
]
],
'WHERE' => [
'fields.name' => $cleaned_linkfield
],
]);

if (count($iterator) == 0) {
return false;
}

$data = $DB->fetchAssoc($res);
$data = $iterator->next();

//display an hidden post field to store container id
//display a hidden post field to store container id
echo Html::hidden('c_id', ['value' => $data['plugin_fields_containers_id']]);

//prepare array for function prepareHtmlFields
Expand Down
Loading