Skip to content
Open
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
4 changes: 2 additions & 2 deletions .travis-before-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ cd "$DRUPAL_TI_DRUPAL_DIR"
mkdir -p "$DRUPAL_TI_DRUPAL_DIR/$DRUPAL_TI_MODULES_PATH"

drush dl -y commerce
drush dl -y addressfield rules ctools entity views
drush en -y commerce_cart commerce_customer_ui commerce_product_ui commerce_line_item_ui commerce_order_ui commerce_payment commerce_payment_example commerce_tax_ui simpletest
drush dl -y addressfield rules ctools entity views physical
drush en -y commerce_cart commerce_customer_ui commerce_product_ui commerce_line_item_ui commerce_order_ui commerce_payment commerce_payment_example commerce_tax_ui simpletest physical
drush en -y commerce_xls_import
git clone https://github.com/box/spout.git
mv spout sites/all/libraries/spout
Expand Down
116 changes: 116 additions & 0 deletions classes/CommerceXlsImportDimensionsHandler.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* @file
* Class definition for CommerceXlsImportDimensionsHandler.
*/

/**
* Class CommerceXlsImportDimensionsHandler.
*/
class CommerceXlsImportDimensionsHandler extends CommerceXlsImportValueHandler implements CommerceXlsImportValueHandlerInterface {

/**
* {@inheritdoc}
*/
public static function validate($value, EntityDrupalWrapper $wrapper, $field_name) {
$errors = array();
$allowed_units = array_keys(physical_dimension_units());
$dimensions = self::fromCsv($value);

foreach (array('width', 'length', 'height') as $unit) {
if (!isset($dimensions[$unit])) {
$errors[] = t('The "!unit" value is not found.', array('!unit' => $unit));
}
elseif (!is_numeric($dimensions[$unit])) {
$errors[] = t('The "!unit" value must be numeric.', array('!unit' => $unit));
}
}

if (!isset($dimensions['unit'])) {
$errors[] = t('The "unit" value is not found.');
}
elseif (!in_array($dimensions['unit'], $allowed_units)) {
$allowed_units_formatted = implode(', ', $allowed_units);
$errors[] = t('The "unit" value must be one of the following: !allowed_units_formatted.', array('!allowed_units_formatted' => $allowed_units_formatted));
}

$valid = empty($errors);
return array(
'status' => $valid ? COMMERCE_XLS_IMPORT_DATA_SUCCESS : COMMERCE_XLS_IMPORT_DATA_ERROR,
'message' => $valid ? NULL : implode(' ', $errors),
);
}

/**
* {@inheritdoc}
*/
public static function set($value, EntityDrupalWrapper $wrapper, $field_name) {
$valid = self::validate($value, $wrapper, $field_name);

if ($valid['status'] === COMMERCE_XLS_IMPORT_DATA_SUCCESS && !empty($value)) {
$dimensions = self::fromCsv($value);
module_load_include('module', 'physical', 'physical');

$field_value = physical_dimensions_field_data_auto_creation();
$field_value['length'] = $dimensions['length'];
$field_value['width'] = $dimensions['width'];
$field_value['height'] = $dimensions['height'];
$field_value['unit'] = $dimensions['unit'];

$wrapper->{$field_name} = $field_value;
}

return $valid;
}

/**
* {@inheritdoc}
*/
public static function get(EntityDrupalWrapper $wrapper, $field_name) {
$dimensions = $wrapper->{$field_name}->value();
return self::toCsv($dimensions);
}

/**
* {@inheritdoc}
*/
public static function toCsv($value, $delimiter = ',', $enclosure = '"') {
$formatted = array();
if (is_array($value)) {
foreach ($value as $k => $v) {
// $k would be length/width/height/unit.
// $v would be a numerical value or in/ft etc.
$k = strtolower(trim($k));
$v = strtolower(trim($v));
$formatted[] = ($k . '=' . $v);
}
}
return parent::toCsv($formatted);
}

/**
* {@inheritdoc}
*/
public static function fromCsv($value) {
$dimensions = parent::fromCsv($value);
$unserialized = array();

if (is_array($dimensions)) {
foreach ($dimensions as $dimension) {
$dimension = trim($dimension);
$split = explode('=', $dimension);
if (count($split) == 2) {
// $k would be length/width/height/unit.
// $v would be a numerical value or in/ft etc.
$k = strtolower(trim($split[0]));
$v = strtolower(trim($split[1]));
$unserialized[$k] = $v;
}
}
}

return $unserialized;
}

}
112 changes: 112 additions & 0 deletions classes/CommerceXlsImportWeightHandler.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* @file
* Class definition for CommerceXlsImportWeightHandler.
*/

/**
* Class CommerceXlsImportWeightHandler.
*/
class CommerceXlsImportWeightHandler extends CommerceXlsImportValueHandler implements CommerceXlsImportValueHandlerInterface {

/**
* {@inheritdoc}
*/
public static function validate($value, EntityDrupalWrapper $wrapper, $field_name) {
$errors = array();
$allowed_units = array_keys(physical_weight_units());
$dimensions = self::fromCsv($value);

if (!isset($dimensions['weight'])) {
$errors[] = t('The "weight" value is not found.');
}
elseif (!is_numeric($dimensions['weight'])) {
$errors[] = t('The "weight" value must be numeric.');
}

if (!isset($dimensions['unit'])) {
$errors[] = t('The "unit" value is not found.');
}
elseif (!in_array($dimensions['unit'], $allowed_units)) {
$allowed_units_formatted = implode(', ', $allowed_units);
$errors[] = t('The "unit" value must be one of the following: !allowed_units_formatted.', array('!allowed_units_formatted' => $allowed_units_formatted));
}

$valid = empty($errors);
return array(
'status' => $valid ? COMMERCE_XLS_IMPORT_DATA_SUCCESS : COMMERCE_XLS_IMPORT_DATA_ERROR,
'message' => $valid ? NULL : implode(' ', $errors),
);
}

/**
* {@inheritdoc}
*/
public static function set($value, EntityDrupalWrapper $wrapper, $field_name) {
$valid = self::validate($value, $wrapper, $field_name);

if ($valid['status'] === COMMERCE_XLS_IMPORT_DATA_SUCCESS && !empty($value)) {
$weight = self::fromCsv($value);
module_load_include('module', 'physical', 'physical');

$field_value = physical_weight_field_data_auto_creation();
$field_value['weight'] = $weight['weight'];
$field_value['unit'] = $weight['unit'];

$wrapper->{$field_name} = $field_value;
}

return $valid;
}

/**
* {@inheritdoc}
*/
public static function get(EntityDrupalWrapper $wrapper, $field_name) {
$weight = $wrapper->{$field_name}->value();
return self::toCsv($weight);
}

/**
* {@inheritdoc}
*/
public static function toCsv($value, $delimiter = ',', $enclosure = '"') {
$formatted = array();
if (is_array($value)) {
foreach ($value as $k => $v) {
// $k would be weight/unit.
// $v would be a numerical value or lb/kg etc.
$k = strtolower(trim($k));
$v = strtolower(trim($v));
$formatted[] = ($k . '=' . $v);
}
}
return parent::toCsv($formatted);
}

/**
* {@inheritdoc}
*/
public static function fromCsv($value) {
$dimensions = parent::fromCsv($value);
$unserialized = array();

if (is_array($dimensions)) {
foreach ($dimensions as $dimension) {
$dimension = trim($dimension);
$split = explode('=', $dimension);
if (count($split) == 2) {
// $k would be weight/unit.
// $v would be a numerical value or lb/kg etc.
$k = strtolower(trim($split[0]));
$v = strtolower(trim($split[1]));
$unserialized[$k] = $v;
}
}
}

return $unserialized;
}

}
4 changes: 4 additions & 0 deletions commerce_xls_import.info
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies[] = libraries
stylesheets[all][] = css/commerce_xls_import.css

files[] = tests/commerce_xls_import.test
files[] = tests/commerce_xls_import_physical.test
files[] = tests/commerce_xls_import_physical.unit.test
files[] = classes/CommerceXlsImportSettings.inc
files[] = classes/CommerceXlsImportReadFilter.inc

Expand All @@ -30,3 +32,5 @@ files[] = classes/CommerceXlsImportTaxonomyTermReferenceHandler.inc
files[] = classes/CommerceXlsImportTextHandler.inc
files[] = classes/CommerceXlsImportTextWithSummaryHandler.inc
files[] = classes/CommerceXlsImportVariationTitleHandler.inc
files[] = classes/CommerceXlsImportDimensionsHandler.inc
files[] = classes/CommerceXlsImportWeightHandler.inc
8 changes: 8 additions & 0 deletions commerce_xls_import.module
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ function commerce_xls_import_commerce_xls_import_field_type_handler_info() {
'class_name' => 'CommerceXlsImportVariationTitleHandler',
);

$handlers['physical_dimensions'] = array(
'class_name' => 'CommerceXlsImportDimensionsHandler',
);

$handlers['physical_weight'] = array(
'class_name' => 'CommerceXlsImportWeightHandler',
);

return $handlers;
}

Expand Down
Loading