Skip to content

Commit dd7186e

Browse files
authored
Merge pull request #1999 from Admidio/fix-inventory-per-org-intallation
Fix inventory per org intallation
2 parents 82cae4f + 23ed343 commit dd7186e

File tree

4 files changed

+91
-36
lines changed

4 files changed

+91
-36
lines changed

demo_data/admidio-mysql.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,9 @@ CREATE TABLE `%PREFIX%_inventory_field_select_options` (
449449

450450
INSERT INTO `%PREFIX%_inventory_field_select_options` (`ifo_id`, `ifo_inf_id`, `ifo_value`, `ifo_system`, `ifo_sequence`, `ifo_obsolete`) VALUES
451451
(1, 3, 'SYS_INVENTORY_FILTER_IN_USE_ITEMS', 1, 1, 0),
452-
(2, 3, 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', 1, 2, 0);
452+
(2, 3, 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', 1, 2, 0),
453+
(3, 10, 'SYS_INVENTORY_FILTER_IN_USE_ITEMS', 1, 1, 0),
454+
(4, 10, 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', 1, 2, 0);
453455

454456
-- --------------------------------------------------------
455457

install/db_scripts/update_5_0.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,5 +537,6 @@ WHERE usf_fn.usf_name_intern = 'FIRST_NAME' AND usf_ln.usf_name_intern = 'LAST_N
537537
<step id="1900" database="mysql">ALTER TABLE %PREFIX%_category_report MODIFY COLUMN crt_col_fields varchar(255)</step>
538538
<step id="1910" database="pgsql">ALTER TABLE %PREFIX%_category_report ALTER COLUMN crt_col_fields TYPE varchar(255)</step>
539539
<step id="1920">ALTER TABLE %PREFIX%_category_report ADD COLUMN crt_col_conditions varchar(255)</step>
540+
<step id="1930">UpdateStepsCode::updateStep50FixInventorySelectOptions</step>
540541
<step>stop</step>
541542
</update>

src/InstallationUpdate/Service/UpdateStepsCode.php

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,52 @@ public static function updateStep51AddSocialNetworkProfileFields(): void
218218
}
219219
}
220220

221+
/**
222+
* This method updates wrongly assigned select options for the inventory status field.
223+
* In previous new Admidio 5.0 installations and additional added organizations, the options were wrongly assigned
224+
* to the organization id instead of the field id. Therefore, we check whether options exist whose assigned id
225+
* matches the organization id of a STATUS field and update them to the corresponding field id.
226+
* @throws Exception
227+
*/
228+
public static function updateStep50FixInventorySelectOptions(): void
229+
{
230+
$statusFields = array();
231+
$statusFieldOptions = array();
232+
233+
// Select all item fields with internal name STATUS
234+
$sql = 'SELECT inf_id, inf_org_id FROM ' . TBL_INVENTORY_FIELDS . ' WHERE inf_name_intern = ?';
235+
$inventoryStatement = self::$db->queryPrepared($sql, array('STATUS'));
236+
237+
while ($row = $inventoryStatement->fetch()) {
238+
$statusFields[] = array( 'id' => (int) $row['inf_id'], 'org_id' => (int) $row['inf_org_id']);
239+
}
240+
241+
// Select all status field options present for this installation
242+
$sql = 'SELECT ifo_id, ifo_inf_id FROM ' . TBL_INVENTORY_FIELD_OPTIONS . ' WHERE ifo_value IN (?, ?)';
243+
$inventoryOptionStatement = self::$db->queryPrepared( $sql, array('SYS_INVENTORY_FILTER_IN_USE_ITEMS', 'SYS_INVENTORY_FILTER_RETIRED_ITEMS') );
244+
245+
while ($row = $inventoryOptionStatement->fetch()) {
246+
$statusFieldOptions[] = array( 'id' => (int) $row['ifo_id'], 'inf_id' => (int) $row['ifo_inf_id']);
247+
}
248+
249+
// check if there are options wrongly assigned to the organization id of a status field instead of the corresponding field id and update them
250+
foreach ($statusFieldOptions as $statusFieldOption) {
251+
foreach ($statusFields as $statusField) {
252+
if ($statusField['id'] === $statusFieldOption['inf_id']) {
253+
// The option is already assigned to the field
254+
continue 2;
255+
}
256+
257+
if ($statusField['org_id'] === $statusFieldOption['inf_id']) {
258+
// The option is wrongly assigned to the organization id
259+
$sql = 'UPDATE ' . TBL_INVENTORY_FIELD_OPTIONS . ' SET ifo_inf_id = ? WHERE ifo_id = ?';
260+
self::$db->queryPrepared($sql, array($statusField['id'], $statusFieldOption['id']));
261+
continue 2;
262+
}
263+
}
264+
}
265+
}
266+
221267
public static function updateStep50MoveFieldListValues(): void
222268
{
223269
global $gDbType;
@@ -296,24 +342,25 @@ public static function updateStep50AddInventoryFields(): void
296342
$itemField->setValue('inf_sequence', (int)$itemFieldData['inf_sequence']);
297343
$itemField->save();
298344
}
299-
}
300345

301-
// add default options for the status field
302-
$sql = 'SELECT inf_id FROM ' . TBL_INVENTORY_FIELDS . '
303-
WHERE inf_name_intern = \'STATUS\'';
304-
$statusFieldId = self::$db->queryPrepared($sql)->fetchColumn();
346+
// add default options for the status field
347+
$sql = 'SELECT inf_id FROM ' . TBL_INVENTORY_FIELDS . '
348+
WHERE inf_name_intern = \'STATUS\'
349+
and inf_org_id = ? -- $row[org_id] ';
350+
$statusFieldId = self::$db->queryPrepared($sql, array((int)$row['org_id']))->fetchColumn();
305351

306-
if ($statusFieldId !== false) {
307-
$arrStatusOptions = array(
308-
array('inf_name' => 'SYS_INVENTORY_FILTER_IN_USE_ITEMS', 'ifo_sequence' => 1),
309-
array('inf_name' => 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', 'ifo_sequence' => 2),
310-
);
352+
if ($statusFieldId !== false) {
353+
$arrStatusOptions = array(
354+
array('ifo_value' => 'SYS_INVENTORY_FILTER_IN_USE_ITEMS', 'ifo_sequence' => 1),
355+
array('ifo_value' => 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', 'ifo_sequence' => 2),
356+
);
311357

312-
foreach ($arrStatusOptions as $statusOption) {
313-
$sql = 'INSERT INTO ' . TBL_INVENTORY_FIELD_OPTIONS . '
358+
foreach ($arrStatusOptions as $statusOption) {
359+
$sql = 'INSERT INTO ' . TBL_INVENTORY_FIELD_OPTIONS . '
314360
(ifo_inf_id, ifo_value, ifo_system, ifo_sequence)
315361
VALUES (?, ?, ?, ?)';
316-
self::$db->queryPrepared($sql, array($statusFieldId, $statusOption['inf_name'], true, $statusOption['ifo_sequence']));
362+
self::$db->queryPrepared($sql, array($statusFieldId, $statusOption['ifo_value'], true, $statusOption['ifo_sequence']));
363+
}
317364
}
318365
}
319366
}

src/Organizations/Entity/Organization.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ public function createBasicData(int $userId)
226226
// insert inventory fields
227227
$sql = 'INSERT INTO ' . TBL_INVENTORY_FIELDS . '
228228
(inf_uuid, inf_org_id, inf_type, inf_name_intern, inf_name, inf_description, inf_system, inf_required_input, inf_sequence, inf_usr_id_create, inf_timestamp_create, inf_usr_id_change, inf_timestamp_change)
229-
VALUES (?, ?, \'TEXT\', \'ITEMNAME\', \'SYS_INVENTORY_ITEMNAME\', \'SYS_INVENTORY_ITEMNAME_DESC\', 1, 1, 0, ?, ?, NULL, NULL),
230-
(?, ?, \'CATEGORY\', \'CATEGORY\', \'SYS_CATEGORY\', \'SYS_INVENTORY_CATEGORY_DESC\', 1, 1, 1, ?, ?, NULL, NULL),
231-
(?, ?, \'DROPDOWN\', \'STATUS\', \'SYS_INVENTORY_STATUS\', \'SYS_INVENTORY_STATUS_DESC\', 1, 1, 2, ?, ?, NULL, NULL),
232-
(?, ?, \'TEXT\', \'KEEPER\', \'SYS_INVENTORY_KEEPER\', \'SYS_INVENTORY_KEEPER_DESC\', 1, 0, 3, ?, ?, NULL, NULL),
233-
(?, ?, \'TEXT\', \'LAST_RECEIVER\', \'SYS_INVENTORY_LAST_RECEIVER\', \'SYS_INVENTORY_LAST_RECEIVER_DESC\', 1, 0, 4, ?, ?, NULL, NULL),
234-
(?, ?, \'DATE\', \'BORROW_DATE\', \'SYS_INVENTORY_BORROW_DATE\', \'SYS_INVENTORY_BORROW_DATE_DESC\', 1, 0, 5, ?, ?, NULL, NULL),
235-
(?, ?, \'DATE\', \'RETURN_DATE\', \'SYS_INVENTORY_RETURN_DATE\', \'SYS_INVENTORY_RETURN_DATE_DESC\', 1, 0, 6, ?, ?, NULL, NULL);
229+
VALUES (?, ?, \'TEXT\', \'ITEMNAME\', \'SYS_INVENTORY_ITEMNAME\', \'SYS_INVENTORY_ITEMNAME_DESC\', true, 1, 0, ?, ?, NULL, NULL),
230+
(?, ?, \'CATEGORY\', \'CATEGORY\', \'SYS_CATEGORY\', \'SYS_INVENTORY_CATEGORY_DESC\', true, 1, 1, ?, ?, NULL, NULL),
231+
(?, ?, \'DROPDOWN\', \'STATUS\', \'SYS_INVENTORY_STATUS\', \'SYS_INVENTORY_STATUS_DESC\', true, 1, 2, ?, ?, NULL, NULL),
232+
(?, ?, \'TEXT\', \'KEEPER\', \'SYS_INVENTORY_KEEPER\', \'SYS_INVENTORY_KEEPER_DESC\', true, 0, 3, ?, ?, NULL, NULL),
233+
(?, ?, \'TEXT\', \'LAST_RECEIVER\', \'SYS_INVENTORY_LAST_RECEIVER\', \'SYS_INVENTORY_LAST_RECEIVER_DESC\', true, 0, 4, ?, ?, NULL, NULL),
234+
(?, ?, \'DATE\', \'BORROW_DATE\', \'SYS_INVENTORY_BORROW_DATE\', \'SYS_INVENTORY_BORROW_DATE_DESC\', true, 0, 5, ?, ?, NULL, NULL),
235+
(?, ?, \'DATE\', \'RETURN_DATE\', \'SYS_INVENTORY_RETURN_DATE\', \'SYS_INVENTORY_RETURN_DATE_DESC\', true, 0, 6, ?, ?, NULL, NULL);
236236
';
237237
$queryParams = array(
238238
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
@@ -245,21 +245,26 @@ public function createBasicData(int $userId)
245245
);
246246
$this->db->queryPrepared($sql, $queryParams);
247247

248-
// insert default values for inventory field 'status'
249-
$sql = 'INSERT INTO ' . TBL_INVENTORY_FIELD_OPTIONS . '
250-
(ifo_inf_id, ifo_value, ifo_system, ifo_sequence)
251-
VALUES ((SELECT inf_id
252-
FROM ' . TBL_INVENTORY_FIELDS . '
253-
WHERE inf_org_id = ? -- $orgId
254-
AND inf_name_intern = \'STATUS\'),
255-
?, ?, ?)';
256-
257-
// status in use
258-
$queryParams = array($orgId, 'SYS_INVENTORY_FILTER_IN_USE_ITEMS', true, 1);
259-
$this->db->queryPrepared($sql, $queryParams);
260-
// status retired
261-
$queryParams = array($orgId, 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', true, 2);
262-
$this->db->queryPrepared($sql, $queryParams);
248+
// insert default options for the status field
249+
$sql = 'SELECT inf_id FROM ' . TBL_INVENTORY_FIELDS . '
250+
WHERE inf_name_intern = \'STATUS\'
251+
AND inf_org_id = ? -- $orgId';
252+
$statusFieldId = $this->db->queryPrepared($sql, array($orgId))->fetchColumn();
253+
254+
if ($statusFieldId !== false) {
255+
$arrStatusOptions = array(
256+
array('inf_name' => 'SYS_INVENTORY_FILTER_IN_USE_ITEMS', 'ifo_sequence' => 1),
257+
array('inf_name' => 'SYS_INVENTORY_FILTER_RETIRED_ITEMS', 'ifo_sequence' => 2),
258+
);
259+
260+
foreach ($arrStatusOptions as $statusOption) {
261+
$sql = 'INSERT INTO ' . TBL_INVENTORY_FIELD_OPTIONS . '
262+
(ifo_inf_id, ifo_value, ifo_system, ifo_sequence)
263+
VALUES (?, ?, true, ?)';
264+
$this->db->queryPrepared($sql, array($statusFieldId, $statusOption['inf_name'], $statusOption['ifo_sequence']));
265+
}
266+
}
267+
263268

264269
// now create default roles
265270

0 commit comments

Comments
 (0)