Skip to content

Commit

Permalink
#218: Correct DbIoProductsAttribsBasicHandler's import
Browse files Browse the repository at this point in the history
  • Loading branch information
lat9 committed Jan 21, 2024
1 parent f66a023 commit cd41ee7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
Expand Up @@ -3,7 +3,7 @@
// Part of the DataBase I/O Manager (aka DbIo) plugin, created by Cindy Merkin (cindy@vinosdefrutastropicales.com)
// Copyright (c) 2015-2024, Vinos de Frutas Tropicales.
//
// Last updated: DbIo v2.0.1.
// Last updated: DbIo v2.0.1
//
if (!defined('IS_ADMIN_FLAG')) {
exit('Illegal access');
Expand All @@ -18,14 +18,15 @@
// - The v_products_model field identifies the (presumed) single product associated with the (possibly) multiple imports.
// - The combination of v_products_options_type and v_products_options_name are used to find existing option/option-value pairs in the
// store's DEFAULT LANGUAGE. All combinations must pre-exist in the database or the associated record's information is not imported.
// - Only **new** option/option-value pairs are successfully imported!
//
class DbIoProductsAttribsBasicHandler extends DbIoHandler
{
public static function getHandlerInformation()
{
DbIoHandler::loadHandlerMessageFile('ProductsAttribsBasic');
return [
'version' => '2.0.0',
'version' => '2.0.1',
'handler_version' => '1.0.0',
'include_header' => true,
'export_only' => false,
Expand Down Expand Up @@ -175,7 +176,7 @@ protected function importFinishProcessing()
global $db;

$message = '';
if (!isset($this->saved_data['option']) || !isset($this->saved_data['option']['products_options_type']) || !isset($this->saved_data['option']['products_options_name'])) {
if (!isset($this->saved_data['option'], $this->saved_data['option']['products_options_type'], $this->saved_data['option']['products_options_name'])) {
$message = " product's option fields do not exist.";
} elseif (!isset($this->saved_data['option_values'])) {
$message = " product's option value field(s) do not exist.";
Expand All @@ -196,27 +197,49 @@ protected function importFinishProcessing()
} else {
$products_options_id = $option_check->fields['products_options_id'];

$options_values_names = explode('^', $this->saved_data['option_values']);
$options_values = [];
foreach ($options_values_names as $current_value_name) {
$options_values[] = $db->prepare_input($current_value_name);
}
$options_values_list = "'" . implode("', '", $options_values) . "'";
// -----
// Check to see that the submitted option values' names are present in
// the database and associated with the option named. If so, add the
// option-value's options_values_id to the list of combinations to be
// added for the product and remove that entry from the options' values'
// names-array.
//
// If the array of option-value-names submitted still has entries when
// this loop completes, that implies that at least one option-value named
// is not valid for the supplied option name.
//
$options_values_check = $db->ExecuteNoCache(
"SELECT pov.products_options_values_id, pov.products_options_values_name
FROM " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " pov2po
WHERE pov.products_options_values_name IN ($options_values_list)
WHERE pov2po.products_options_id = $products_options_id
AND pov.language_id = $language_id
AND pov.products_options_values_id = pov2po.products_options_values_id
AND pov2po.products_options_id = $products_options_id"
AND pov.products_options_values_id = pov2po.products_options_values_id"
);
if (count($options_values_names) !== $options_values_check->RecordCount()) {
$values_found = [];
foreach ($options_values_check as $next_value) {
$values_found[] = $next_value['products_options_values_name'];
$options_values_names = explode('^', $this->saved_data['option_values']);
$options_values_ids = [];
foreach ($options_values_check as $next_value) {
$value_index = array_search($next_value['products_options_values_name'], $options_values_names);
if ($value_index !== false) {
$options_values_ids[] = $next_value['products_options_values_id'];
unset($options_values_names[$value_index]);
}
$values_not_found = implode(', ', array_diff($options_values_names, $values_found));
if (count($options_values_names) === 0) {
break;
}
}

// -----
// If there were options' values' names that are not currently present in the
// database and associated with the selected option, the record cannot be imported.
//
if (count($options_values_names) !== 0) {
$values_not_found = implode(', ', $options_values_names);
$message = " one or more option-values ($values_not_found) were either not present in the default language or are not associated with the products_options_id of $products_options_id.";
// -----
// Otherwise, loop through each of the options_values_id's associated
// with the current import record. If any are not currently recorded
// for the current product, add them.
//
} else {
$products_id = $this->key_fields['products_id'];
$attributes_insert_sql = [
Expand All @@ -233,17 +256,17 @@ protected function importFinishProcessing()
'type' => 'integer'
],
];
foreach ($options_values_check as $next_value) {
foreach ($options_values_ids as $next_values_id) {
$check = $db->ExecuteNoCache(
"SELECT products_attributes_id
FROM " . TABLE_PRODUCTS_ATTRIBUTES . "
WHERE products_id = $products_id
AND options_id = $products_options_id
AND options_values_id = " . $options_values_check->fields['products_options_values_id'] . "
AND options_values_id = $next_values_id
LIMIT 1"
);
if ($check->EOF) {
$attributes_insert_sql['options_values_id']['value'] = $options_values_check->fields['products_options_values_id'];
$attributes_insert_sql['options_values_id']['value'] = $next_values_id;
$attrib_insert_query = $this->importBuildSqlQuery(TABLE_PRODUCTS_ATTRIBUTES, '', $attributes_insert_sql, '', true, true);
if ($this->operation !== 'check') {
$db->Execute($attrib_insert_query);
Expand Down
4 changes: 2 additions & 2 deletions YOUR_ADMIN/includes/init_includes/init_dbio_admin.php
Expand Up @@ -14,8 +14,8 @@
return;
}

define('DBIO_CURRENT_VERSION', '2.0.1-beta2');
define('DBIO_CURRENT_UPDATE_DATE', '2024-01-20');
define('DBIO_CURRENT_VERSION', '2.0.1-beta3');
define('DBIO_CURRENT_UPDATE_DATE', '2024-01-21');

$version_release_date = DBIO_CURRENT_VERSION . ' (' . DBIO_CURRENT_UPDATE_DATE . ')';

Expand Down
@@ -1,10 +1,12 @@
<?php
// -----
// Part of the DataBase I/O Manager (aka DbIo) plugin, created by Cindy Merkin (cindy@vinosdefrutastropicales.com)
// Copyright (c) 2015-2020, Vinos de Frutas Tropicales.
// Copyright (c) 2015-2024, Vinos de Frutas Tropicales.
//
// Last updated: DbIo v2.0.1
//

// -----
// Defines the handler's descriptive text.
//
define('DBIO_PRODUCTSATTRIBSBASIC_DESCRIPTION', 'This report-format supports import/export of the <em>basic</em> products\' attributes\' values. The report, indexed by the associated product\'s <b>model-number</b>, includes one record per product/product-option pair with the option-specific values separated by ^ characters &mdash; using your store\'s <code>DEFAULT_LANGUAGE</code>.<br><br><b>Notes:</b><ol><li>Your store\'s products <em>must</em> each have a unique model number for any &quot;import&quot; action to successfully complete.</li><li>All options\' names and option values\' names <b>must already exist within your database</b> for an associated attributes\' record to be successfully imported.</li></ol>');
define('DBIO_PRODUCTSATTRIBSBASIC_DESCRIPTION', 'This report-format supports import/export of the <em>basic</em> products\' attributes\' values. The report, indexed by the associated product\'s <b>model-number</b>, includes one record per product/product-option pair with the option-specific values separated by ^ characters &mdash; using your store\'s <code>DEFAULT_LANGUAGE</code>.<br><br><b>Notes:</b><ol><li>Your store\'s products <em>must</em> each have a unique model number for any &quot;import&quot; action to successfully complete.</li><li>All options\' names and option values\' names <b>must already exist within your database</b> for an associated attributes\' record to be successfully imported.</li><li><b>Only <i>new</i></b> option-combinations will be successfully added!</li></ol>');
4 changes: 3 additions & 1 deletion docs/dbio/readme.html
Expand Up @@ -594,14 +594,16 @@ <h1 id="title">Database I/O Manager (DbIo) for Zen Cart&reg;</h1>
<noscript><h3>Version History</h3></noscript>
<p>You can view the details of these changes on the plugin's <a href="https://github.com/lat9/dbio/issues" target="_blank">GitHub repository.</a></p>
<ul>
<li>v2.0.1-beta2, 2024-01-20 (lat9/torvista):<ul>
<li>v2.0.1-beta3, 2024-01-21 (lat9/torvista):<ul>
<li>BUGFIX: Correct sprintf parameter declaration used when files are split.</li>
<li>BUGFIX: Correct PHP Fatal error when exporting via <var>DbIoProductsAttribsBasicHandler</var>.</li>
<li>BUGFIX: Correct duplicate form names in <strong>Tools :: Database I/O Manager</strong>.</li>
<li>BUGFIX: Correct <var>DbIoProductAttribsBasicHander</var> import processing.</li>
<li>The following files were changed:<ol>
<li>/YOUR_ADMIN/dbio_manager.php</li>
<li>/YOUR_ADMIN/includes/classes/dbio/DbIoProductsAttribsBasicHandler.php</li>
<li>/YOUR_ADMIN/includes/languages/english/dbio_manager.php</li>
<li>/YOUR_ADMIN/includes/languages/english/dbio/DbIoProductsAttribsBasicHandler.php</li>
<li>/YOUR_ADMIN/includes/init_includes/init_dbio_admin.php</li>
</ol></li>
</ul></li>
Expand Down

0 comments on commit cd41ee7

Please sign in to comment.