Skip to content

Commit

Permalink
#122: Refactor handler prior to change
Browse files Browse the repository at this point in the history
  • Loading branch information
lat9 committed Apr 27, 2019
1 parent c67c2ce commit 02b233d
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions YOUR_ADMIN/includes/classes/dbio/DbIoProductsAttribsRawHandler.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
// -----
// Part of the DataBase Import/Export (aka dbIO) plugin, created by Cindy Merkin (cindy@vinosdefrutastropicales.com)
// Copyright (c) 2016, Vinos de Frutas Tropicales.
// Part of the DataBase Import/Export (aka DbIo) plugin, created by Cindy Merkin (cindy@vinosdefrutastropicales.com)
// Copyright (c) 2016-2019, Vinos de Frutas Tropicales.
//
if (!defined ('IS_ADMIN_FLAG')) {
exit ('Illegal access');
}

// -----
// This dbIO class handles the import and export of information in the Zen Cart 'products_attributes' and 'products_attributes_download' tables.
// This DbIo class handles the import and export of information in the Zen Cart 'products_attributes' and 'products_attributes_download' tables.
//
// Each table-record is exported as a single CSV record; all currently-defined fields in each of those tables are exported.
//
Expand All @@ -17,10 +17,10 @@
//
class DbIoProductsAttribsRawHandler extends DbIoHandler
{
public static function getHandlerInformation ()
public static function getHandlerInformation()
{
DbIoHandler::loadHandlerMessageFile ('ProductsAttribsRaw');
return array (
DbIoHandler::loadHandlerMessageFile('ProductsAttribsRaw');
return array(
'version' => '1.0.0',
'handler_version' => '1.0.0',
'include_header' => true,
Expand All @@ -33,7 +33,7 @@ public static function getHandlerInformation ()
// There are some "subtleties" about this report that require some force-feeding for the SQL necessary to properly
// create the associated report, so override the export SQL generation.
//
public function exportFinalizeInitialization ()
public function exportFinalizeInitialization()
{
// -----
// Grab the fields from the various product/attribute tables.
Expand All @@ -43,13 +43,13 @@ public function exportFinalizeInitialization ()
// -----
// Insert the products_model, products_options_name and products_options_values_name fields, to make the output a little more readable.
//
$this->select_clause = str_replace (
array (
$this->select_clause = str_replace(
array(
'pa.products_id,',
'pa.options_id,',
'pa.options_values_id,',
),
array (
array(
'pa.products_id, p.products_model,',
'pa.options_id, po.products_options_name,',
'pa.options_values_id, pov.products_options_values_name,',
Expand All @@ -59,18 +59,18 @@ public function exportFinalizeInitialization ()
// -----
// Need also to update the headers so that the column-offsets match properly, in light of the above additions.
//
$inserted_columns = array (
$inserted_columns = array(
'v_products_id' => 'v_products_model',
'v_options_id' => 'v_products_options_name',
'v_options_values_id' => 'v_products_options_values_name',
);
foreach ($inserted_columns as $insert_after => $inserted_column) {
$index = array_search ($insert_after, $this->headers);
$index = array_search($insert_after, $this->headers);
if ($index !== false) {
array_splice ($this->headers, $index+1, 0, $inserted_column);
array_splice($this->headers, $index+1, 0, $inserted_column);
}
}
$this->headers = array_values ($this->headers);
$this->headers = array_values($this->headers);

// -----
// Tie all the tables together via the WHERE/AND clauses.
Expand All @@ -84,7 +84,7 @@ public function exportFinalizeInitialization ()

$this->order_by_clause = "pa.products_id ASC, pa.options_id ASC, pa.options_values_id ASC";

return parent::exportFinalizeInitialization ();
return parent::exportFinalizeInitialization();
}

// ----------------------------------------------------------------------------------
Expand All @@ -95,35 +95,35 @@ public function exportFinalizeInitialization ()
// This function, called during the overall class construction, is used to set this handler's database
// configuration for the DbIO operations.
//
protected function setHandlerConfiguration ()
protected function setHandlerConfiguration()
{
$this->stats['report_name'] = 'ProductsAttribsRaw';
$this->config = self::getHandlerInformation ();
$this->config['keys'] = array (
TABLE_PRODUCTS_ATTRIBUTES => array (
$this->config = self::getHandlerInformation();
$this->config['keys'] = array(
TABLE_PRODUCTS_ATTRIBUTES => array(
'alias' => 'pa',
'capture_key_value' => true,
'products_attributes_id' => array (
'products_attributes_id' => array(
'type' => self::DBIO_KEY_IS_MASTER,
),
'products_id' => array (
'products_id' => array(
'type' => self::DBIO_KEY_IS_VARIABLE | self::DBIO_KEY_SELECTED,
),
'options_id' => array (
'options_id' => array(
'type' => self::DBIO_KEY_IS_VARIABLE | self::DBIO_KEY_SELECTED,
),
'options_values_id' => array (
'options_values_id' => array(
'type' => self::DBIO_KEY_IS_VARIABLE | self::DBIO_KEY_SELECTED,
),
),
);
$this->config['tables'] = array (
TABLE_PRODUCTS_ATTRIBUTES => array (
$this->config['tables'] = array(
TABLE_PRODUCTS_ATTRIBUTES => array(
'alias' => 'pa',
),
TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD => array (
TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD => array(
'alias' => 'pad',
'io_field_overrides' => array (
'io_field_overrides' => array(
'products_attributes_id' => false,
),
),
Expand All @@ -133,7 +133,7 @@ protected function setHandlerConfiguration ()
// -----
// While the products_attributes_id field might be specified by the imported CSV, it's not a value that's importable.
//
protected function importHeaderFieldCheck ($field_name)
protected function importHeaderFieldCheck($field_name)
{
return ($field_name == 'products_attributes_id') ? self::DBIO_NO_IMPORT : self::DBIO_IMPORT_OK;
}
Expand All @@ -146,31 +146,31 @@ protected function importHeaderFieldCheck ($field_name)
// Note: The checking needs to be performed **only** for an insert, since a products_attributes record is located
// by a match on those three fields.
//
protected function importCheckKeyValue ($data)
protected function importCheckKeyValue($data)
{
global $db;
if ($this->import_is_insert) {
$products_id = $this->importGetFieldValue ('products_id', $data);
$check = $db->Execute ("SELECT products_id FROM " . TABLE_PRODUCTS . " WHERE products_id = '$products_id' LIMIT 1");
$products_id = $this->importGetFieldValue('products_id', $data);
$check = $db->Execute("SELECT products_id FROM " . TABLE_PRODUCTS . " WHERE products_id = '$products_id' LIMIT 1");
if ($check->EOF) {
$this->record_status = false;
$this->debugMessage ("ProductsAttribsRawHandler::importCheckKeyValue: Unknown products_id ($products_id) at line #" . $this->stats['record_count'] . ', record not inserted.', self::DBIO_WARNING);
$this->debugMessage("ProductsAttribsRawHandler::importCheckKeyValue: Unknown products_id ($products_id) at line #" . $this->stats['record_count'] . ', record not inserted.', self::DBIO_WARNING);
}
unset ($check);

$options_id = $this->importGetFieldValue ('options_id', $data);
$check = $db->Execute ("SELECT products_options_id FROM " . TABLE_PRODUCTS_OPTIONS . " WHERE products_options_id = '$options_id' LIMIT 1");
$options_id = $this->importGetFieldValue('options_id', $data);
$check = $db->Execute("SELECT products_options_id FROM " . TABLE_PRODUCTS_OPTIONS . " WHERE products_options_id = '$options_id' LIMIT 1");
if ($check->EOF) {
$this->record_status = false;
$this->debugMessage ("ProductsAttribsRawHandler::importCheckKeyValue: Unknown options_id ($options_id) at line #" . $this->stats['record_count'] . ', record not inserted.', self::DBIO_WARNING);
$this->debugMessage("ProductsAttribsRawHandler::importCheckKeyValue: Unknown options_id ($options_id) at line #" . $this->stats['record_count'] . ', record not inserted.', self::DBIO_WARNING);
}
unset ($check);
unset($check);

$options_values_id = $this->importGetFieldValue ('options_values_id', $data);
$check = $db->Execute ("SELECT products_options_values_id FROM " . TABLE_PRODUCTS_OPTIONS_VALUES . " WHERE products_options_values_id = '$options_values_id' LIMIT 1");
$options_values_id = $this->importGetFieldValue('options_values_id', $data);
$check = $db->Execute("SELECT products_options_values_id FROM " . TABLE_PRODUCTS_OPTIONS_VALUES . " WHERE products_options_values_id = '$options_values_id' LIMIT 1");
if ($check->EOF) {
$this->record_status = false;
$this->debugMessage ("ProductsAttribsRawHandler::importCheckKeyValue: Unknown options_values_id ($options_values_id) at line #" . $this->stats['record_count'] . ', record not inserted.', self::DBIO_WARNING);
$this->debugMessage("ProductsAttribsRawHandler::importCheckKeyValue: Unknown options_values_id ($options_values_id) at line #" . $this->stats['record_count'] . ', record not inserted.', self::DBIO_WARNING);
}
}
return $this->record_status;
Expand All @@ -180,10 +180,10 @@ protected function importCheckKeyValue ($data)
// Fix-up any blank values in the attribute's download maxdays/maxcount fields (they're output as blank if no associated downloads-table
// record is found) to prevent unwanted warnings from being issued.
//
protected function importProcessField ($table_name, $field_name, $language_id, $field_value)
protected function importProcessField($table_name, $field_name, $language_id, $field_value)
{
if ($table_name == TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD) {
if (($field_name == 'products_attributes_maxdays' || $field_name == 'products_attributes_maxcount') && empty ($field_value)) {
if (($field_name == 'products_attributes_maxdays' || $field_name == 'products_attributes_maxcount') && empty($field_value)) {
$field_value = '0';
}
} else {
Expand All @@ -197,19 +197,19 @@ protected function importProcessField ($table_name, $field_name, $language_id, $
break;
}
}
parent::importProcessField ($table_name, $field_name, $language_id, $field_value);
parent::importProcessField($table_name, $field_name, $language_id, $field_value);
}

// -----
// This function, called to create an import-record's associated SQL, checks to see if the current attribute is to have a download-file associated with it.
// That's tested by the presence of a value (unchecked) in the 'products_attributes_filename' field. If the value is present, the associated record in the
// 'products_attributes_download' table is created/modified; if that field's value is empty (a null-string), then any existing record will be removed.
//
protected function importBuildSqlQuery ($table_name, $table_alias, $table_fields, $extra_where_clause = '', $is_override = false, $is_insert = true)
protected function importBuildSqlQuery($table_name, $table_alias, $table_fields, $extra_where_clause = '', $is_override = false, $is_insert = true)
{
$proceed_with_query = true;
if ($table_name == TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD) {
if (empty ($this->import_sql_data[TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD]['products_attributes_filename']['value'])) {
if (empty($this->import_sql_data[TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD]['products_attributes_filename']['value'])) {
$proceed_with_query = false;
} elseif ($this->operation != 'check') {
if ($this->import_is_insert) {
Expand All @@ -219,28 +219,28 @@ protected function importBuildSqlQuery ($table_name, $table_alias, $table_fields
}
}
}
return ($proceed_with_query) ? parent::importBuildSqlQuery ($table_name, $table_alias, $table_fields, $extra_where_clause, $is_override, $is_insert) : false;
return ($proceed_with_query) ? parent::importBuildSqlQuery($table_name, $table_alias, $table_fields, $extra_where_clause, $is_override, $is_insert) : false;
}

// -----
// At the end of a record's import, make sure that there's an entry in the po2pov table that ties the just-processed option/value pair together.
//
protected function importRecordPostProcess ($record_key_value)
protected function importRecordPostProcess($record_key_value)
{
global $db;
if ($this->operation != 'check' && $this->import_is_insert) {
$record_inserted = 'no';
$check = $db->Execute ("SELECT products_options_id FROM " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . "
WHERE products_options_id = " . $this->saved_data['options_id'] . "
AND products_options_values_id = " . $this->saved_data['options_values_id'] . "
LIMIT 1");
$check = $db->Execute("SELECT products_options_id FROM " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . "
WHERE products_options_id = " . $this->saved_data['options_id'] . "
AND products_options_values_id = " . $this->saved_data['options_values_id'] . "
LIMIT 1");
if ($check->EOF) {
$record_inserted = 'yes';
$sql_query = "INSERT INTO " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " (products_options_id, products_options_values_id) VALUES ( " . $this->saved_data['options_id'] . ', ' . $this->saved_data['options_values_id'] . ')';
$this->debugMessage ("ProductsAttribsRaw::importRecordPostProcess\n$sql_query", self::DBIO_STATUS); //- Forces the generated SQL to be logged!!
$db->Execute ($sql_query);
$this->debugMessage("ProductsAttribsRaw::importRecordPostProcess\n$sql_query", self::DBIO_STATUS); //- Forces the generated SQL to be logged!!
$db->Execute($sql_query);
}
$this->debugMessage ("ProductsAttribsRaw::importRecordPostProcess, record updated ($record_inserted), options: " . print_r ($this->saved_data, true));
$this->debugMessage("ProductsAttribsRaw::importRecordPostProcess, record updated ($record_inserted), options: " . print_r($this->saved_data, true));
}
}

Expand Down

0 comments on commit 02b233d

Please sign in to comment.