Skip to content

Commit

Permalink
Merge pull request #25 from MightyMCoder/develop
Browse files Browse the repository at this point in the history
restructuring, bugfix import and add functionality for create fields from import page
  • Loading branch information
MightyMCoder authored Dec 29, 2024
2 parents fe8e266 + d4fbe21 commit 0cc0c2f
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 172 deletions.
13 changes: 11 additions & 2 deletions fields_edit_new.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
*
* Parameters:
*
* imf_id : ID of the item field that should be edited
* imf_id : ID of the item field that should be edited
* field_name : Name of the field that should be set
* redirect_to_import : If true, the user will be redirected to the import page after saving the field
*
***********************************************************************************************
*/
Expand All @@ -23,6 +25,8 @@

// Initialize and check the parameters
$getimfId = admFuncVariableIsValid($_GET, 'imf_id', 'int');
$getFieldName = admFuncVariableIsValid($_GET, 'field_name', 'string', array('defaultValue' => "", 'directOutput' => true));
$getRedirectToImport = admFuncVariableIsValid($_GET, 'redirect_to_import', 'bool', array('defaultValue' => false));

$pPreferences = new CConfigTablePIM();
$pPreferences->read();
Expand All @@ -47,6 +51,11 @@
}
}

// Set the name of the field if it was passed as a parameter
if ($getFieldName !== "") {
$itemField->setValue('imf_name', $getFieldName);
}

if (isset($_SESSION['fields_request'])) {
// User returned to this form due to incorrect input, now write the previously entered content into the object
$itemField->setArray($_SESSION['fields_request']);
Expand All @@ -72,7 +81,7 @@ function setValueList() {
', true);

// show form
$form = new HtmlForm('item_fields_edit_form', SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/fields_function.php', ['imf_id' => $getimfId, 'mode' => 1]), $page);
$form = new HtmlForm('item_fields_edit_form', SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/fields_function.php', ['imf_id' => $getimfId, 'mode' => 1, 'redirect_to_import' => $getRedirectToImport]), $page);

$form->addInput('imf_name', $gL10n->get('SYS_NAME'), $itemField->getValue('imf_name', 'database'), array(
'maxLength' => 100,
Expand Down
38 changes: 22 additions & 16 deletions fields_function.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
*
* Parameters:
*
* imf_id : ID of the item field to be managed
* mode : 1 - create or edit item field
* 2 - delete item field
* 4 - change sequence of item field
* sequence : direction to move the item field, values are TableUserField::MOVE_UP, TableUserField::MOVE_DOWN
* imf_id : ID of the item field to be managed
* mode : 1 - create or edit item field
* 2 - delete item field
* 4 - change sequence of item field
* sequence : direction to move the item field, values are TableUserField::MOVE_UP, TableUserField::MOVE_DOWN
* redirect_to_import : If true, the user will be redirected to the import page after saving the field
*
* Methods:
*
* handleCreateOrUpdate($itemField) : Handles the creation or update of an item field
* handleDelete($itemField) : Handles the deletion of an item field
* handleChangeSequence($itemField, $getSequence): Handles changing the sequence of an item field
* validateRequiredFields($itemField) : Validates the required fields for an item field
* checkFieldExists($itemField) : Checks if an item field already exists
* handleCreateOrUpdate($itemField, $getRedirectToImport) : Handles the creation or update of an item field
* handleDelete($itemField) : Handles the deletion of an item field
* handleChangeSequence($itemField, $getSequence) : Handles changing the sequence of an item field
* validateRequiredFields($itemField) : Validates the required fields for an item field
* checkFieldExists($itemField) : Checks if an item field already exists
***********************************************************************************************
*/

Expand All @@ -36,6 +37,7 @@
$getimfId = admFuncVariableIsValid($_GET, 'imf_id', 'int');
$getMode = admFuncVariableIsValid($_GET, 'mode', 'int', array('requireValue' => true));
$getSequence = admFuncVariableIsValid($_GET, 'sequence', 'string', array('validValues' => array(TableUserField::MOVE_UP, TableUserField::MOVE_DOWN)));
$getRedirectToImport = admFuncVariableIsValid($_GET, 'redirect_to_import', 'bool', array('defaultValue' => false));

$pPreferences = new CConfigTablePIM();
$pPreferences->read();
Expand All @@ -60,7 +62,7 @@

switch ($getMode) {
case 1:
handleCreateOrUpdate($itemField);
handleCreateOrUpdate($itemField, $getRedirectToImport);
break;
case 2:
handleDelete($itemField);
Expand All @@ -74,7 +76,7 @@
* Handles the creation or update of an item field.
* @param TableAccess $itemField The item field object to be created or updated.
*/
function handleCreateOrUpdate($itemField) {
function handleCreateOrUpdate($itemField, $redirectToImport = false) {
global $gMessage, $gL10n, $gDb, $gCurrentOrgId;

$_SESSION['fields_request'] = $_POST;
Expand All @@ -84,7 +86,7 @@ function handleCreateOrUpdate($itemField) {

// Check if the field already exists
if (isset($_POST['imf_name']) && $itemField->getValue('imf_name') !== $_POST['imf_name']) {
checkFieldExists($itemField);
checkFieldExists($_POST['imf_name']);
}

// Make HTML in description secure
Expand Down Expand Up @@ -122,7 +124,11 @@ function handleCreateOrUpdate($itemField) {

unset($_SESSION['fields_request']);

$gMessage->setForwardUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/fields.php', 1000);
if ($redirectToImport) {
$gMessage->setForwardUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import/import_column_config.php', 1000);
} else {
$gMessage->setForwardUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/fields.php', 1000);
}
$gMessage->show($gL10n->get('SYS_SAVE_DATA'));
// => EXIT
}
Expand Down Expand Up @@ -200,7 +206,7 @@ function validateRequiredFields($itemField) {

/**
* Checks if an item field already exists.
* @param TableAccess $itemField The item field object to be checked.
* @param string $itemField The item field string to be checked.
*/
function checkFieldExists($itemField) {
global $gMessage, $gL10n, $gDb, $gCurrentOrgId, $getimfId;
Expand All @@ -210,7 +216,7 @@ function checkFieldExists($itemField) {
AND (imf_org_id = ? -- $gCurrentOrgId
OR imf_org_id IS NULL)
AND imf_id <> ? -- $getimfId;';
$statement = $gDb->queryPrepared($sql, array($_POST['imf_name'], $gCurrentOrgId, $getimfId));
$statement = $gDb->queryPrepared($sql, array($itemField, $gCurrentOrgId, $getimfId));

if ((int) $statement->fetchColumn() > 0) {
$gMessage->show($gL10n->get('ORG_FIELD_EXIST'));
Expand Down
8 changes: 4 additions & 4 deletions import.php → import/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
***********************************************************************************************
*/

require_once(__DIR__ . '/../../adm_program/system/common.php');
require_once(__DIR__ . '/common_function.php');
require_once(__DIR__ . '/../../../adm_program/system/common.php');
require_once(__DIR__ . '/../common_function.php');

// Access only with valid login
require_once(__DIR__ . '/../../adm_program/system/login_valid.php');
require_once(__DIR__ . '/../../../adm_program/system/login_valid.php');

// only authorized user are allowed to start this module
if (!isUserAuthorizedForPreferences()) {
Expand Down Expand Up @@ -59,7 +59,7 @@
$page = new HtmlPage('admidio-items-import', $headline);

// show form
$form = new HtmlForm('import_items_form', ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import_read_file.php', $page, array('enableFileUpload' => true));
$form = new HtmlForm('import_items_form', ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import/import_read_file.php', $page, array('enableFileUpload' => true));
$formats = array(
'AUTO' => $gL10n->get('SYS_AUTO_DETECT'),
'XLSX' => $gL10n->get('SYS_EXCEL_2007_365'),
Expand Down
44 changes: 28 additions & 16 deletions import_column_config.php → import/import_column_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
***********************************************************************************************
*/

require_once(__DIR__ . '/../../adm_program/system/common.php');
require_once(__DIR__ . '/common_function.php');
require_once(__DIR__ . '/classes/items.php');
require_once(__DIR__ . '/../../../adm_program/system/common.php');
require_once(__DIR__ . '/../common_function.php');
require_once(__DIR__ . '/../classes/items.php');

// Access only with valid login
require_once(__DIR__ . '/../../adm_program/system/login_valid.php');
require_once(__DIR__ . '/../../../adm_program/system/login_valid.php');

// only authorized user are allowed to start this module
if (!isUserAuthorizedForPreferences()) {
Expand Down Expand Up @@ -52,9 +52,6 @@
*/
function getColumnAssignmentHtml(array $arrayColumnList, array $arrayCsvColumns): string
{
global $gL10n;

$categoryName = null;
$html = '';

foreach ($arrayColumnList as $field) {
Expand Down Expand Up @@ -98,13 +95,13 @@ function getColumnAssignmentHtml(array $arrayColumnList, array $arrayCsvColumns)

// create html page object
$page = new HtmlPage('admidio-items-import-csv', $headline);

//page->addHtml('<p class="lead">'.$gL10n->get('SYS_ASSIGN_FIELDS_DESC').'</p>');
$page->addHtml('<p class="lead">'.$gL10n->get('PLG_INVENTORY_MANAGER_IMPORT_ASSIGN_FIELDS').'</p>');

// show form
$form = new HtmlForm('import_assign_fields_form', ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import_items.php', $page, array('type' => 'vertical'));
$form = new HtmlForm('import_assign_fields_form', ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import/import_items.php', $page, array('type' => 'vertical'));
$form->addCheckbox('first_row', $gL10n->get('SYS_FIRST_LINE_COLUMN_NAME'), $formValues['first_row']);
$form->addHtml('<div class="alert alert-warning alert-small" id="admidio-import-unused"><i class="fas fa-exclamation-triangle"></i>'.$gL10n->get('SYS_IMPORT_UNUSED_HEAD').'<div id="admidio-import-unused-fields">-</div></div>');
$form->addHtml('<div class="alert alert-warning alert-small" id="admidio-import-unused"><i class="fas fa-exclamation-triangle"></i>'.$gL10n->get('PLG_INVENTORY_MANAGER_IMPORT_UNUSED_HEAD').'<div id="admidio-import-unused-fields">-</div></div>');

$page->addJavascript(
'
$(".admidio-import-field").change(function() {
Expand All @@ -120,21 +117,31 @@ function getColumnAssignmentHtml(array $arrayColumnList, array $arrayCsvColumns)
used.push($(this).text());
}
});
var outstr = $(available).not(used).get().join(", ");
var outstr = "";
$(available).not(used).each(function(index, value) {
if (value === "Nr.") {
outstr += "<tr><td>" + value + "</td><td></td></tr>";
} else {
outstr += "<tr><td>" + value + "</td><td><a href=\"' . ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/fields_edit_new.php?field_name=" + encodeURIComponent(value) + "&redirect_to_import=true\" class=\"btn btn-primary btn-sm\">' . $gL10n->get('PLG_INVENTORY_MANAGER_ITEMFIELD_CREATE') . '</a></td></tr>";
}
});
if (outstr == "") {
outstr = "-";
} else {
outstr = "<table class=\"table table-condensed\"><tbody>" + outstr + "</tbody></table>";
}
$("#admidio-import-unused #admidio-import-unused-fields").html(outstr);
});
$(".admidio-import-field").trigger("change");',
true
);


$htmlFieldTable = '
<table class="table table-condensed import-config import-config-csv">
<thead>
<tr>
<th>'.$gL10n->get('SYS_PROFILE_FIELD').'</th>
<th>'.$gL10n->get('PLG_INVENTORY_MANAGER_ITEMFIELDS').'</th>
<th>'.$gL10n->get('SYS_FILE_COLUMN').'</th>
</tr>
</thead>';
Expand All @@ -152,9 +159,14 @@ function getColumnAssignmentHtml(array $arrayColumnList, array $arrayCsvColumns)
$row = array();
foreach ($items->mItemFields as $columnKey => $columnValue) {
$imfName = $columnValue->GetValue('imf_name');
$row = array(
$columnValue->GetValue('imf_name_intern') => $gL10n->get($imfName)
);

// If the field name starts with 'PIM_', it is a language key
if (strpos($imfName, 'PIM_') !== false) {
$row = array($columnValue->GetValue('imf_name_intern') => $gL10n->get($imfName));
} else {
$row = array($columnValue->GetValue('imf_name_intern') => $imfName);
}

$arrayImportableFields[] = $row;
}

Expand Down
12 changes: 6 additions & 6 deletions import_items.php → import/import_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
***********************************************************************************************
*/

require_once(__DIR__ . '/../../adm_program/system/common.php');
require_once(__DIR__ . '/common_function.php');
require_once(__DIR__ . '/classes/items.php');
require_once(__DIR__ . '/../../../adm_program/system/common.php');
require_once(__DIR__ . '/../common_function.php');
require_once(__DIR__ . '/../classes/items.php');

// Access only with valid login
require_once(__DIR__ . '/../../adm_program/system/login_valid.php');
require_once(__DIR__ . '/../../../adm_program/system/login_valid.php');

// only authorized user are allowed to start this module
if (!isUserAuthorizedForPreferences()) {
Expand Down Expand Up @@ -83,7 +83,6 @@
}
}


$valueList = array();
foreach ($assignedFieldColumn as $row => $values) {
foreach ($items->mItemFields as $fields){
Expand Down Expand Up @@ -162,9 +161,10 @@
}

if (count($assignedFieldColumn) > 0) {

// save item
$_POST['redirect'] = 0;
require_once(__DIR__ . '/items_save.php');
require(__DIR__ . '/../items_save.php');
$importSuccess = true;
unset($_POST);
}
Expand Down
10 changes: 5 additions & 5 deletions import_read_file.php → import/import_read_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
***********************************************************************************************
*/

require_once(__DIR__ . '/../../adm_program/system/common.php');
require_once(__DIR__ . '/common_function.php');
require_once(__DIR__ . '/classes/items.php');
require_once(__DIR__ . '/../../../adm_program/system/common.php');
require_once(__DIR__ . '/../common_function.php');
require_once(__DIR__ . '/../classes/items.php');

// Access only with valid login
require_once(__DIR__ . '/../../adm_program/system/login_valid.php');
require_once(__DIR__ . '/../../../adm_program/system/login_valid.php');

// Initialize and check the parameters
$postImportFormat = admFuncVariableIsValid(
Expand Down Expand Up @@ -145,5 +145,5 @@
}
}

admRedirect(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import_column_config.php');
admRedirect(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER . '/import/import_column_config.php');
// => EXIT
2 changes: 1 addition & 1 deletion inventory_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
}

if (isUserAuthorizedForPreferences()) {
$page->addPageFunctionsMenuItem('menu_preferences', $gL10n->get('SYS_SETTINGS'), SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER .'/preferences.php'), 'fa-cog');
$page->addPageFunctionsMenuItem('menu_preferences', $gL10n->get('SYS_SETTINGS'), SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_PLUGINS . PLUGIN_FOLDER .'/preferences/preferences.php'), 'fa-cog');
$page->addPageFunctionsMenuItem('itemcreate_form_btn', $gL10n->get('PLG_INVENTORY_MANAGER_ITEM_CREATE'), SecurityUtils::encodeUrl(ADMIDIO_URL.FOLDER_PLUGINS . PLUGIN_FOLDER .'/items_edit_new.php', array('item_id' => 0)), 'fas fa-plus-circle');
}

Expand Down
Loading

0 comments on commit 0cc0c2f

Please sign in to comment.