Index: trunk/core/units/general/cat_dbitem_export.php =================================================================== diff -u -N -r3648 -r3698 --- trunk/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3648) +++ trunk/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3698) @@ -170,7 +170,25 @@ $object->setError('ExportColumns', 'required'); $ret = false; } + else { + // 1.1. check that all required fields are present in imported file + $missing_columns = Array(); + foreach ($object->Fields as $field_name => $field_options) { + if ($object->SkipField($field_name)) continue; + if (getArrayValue($field_options, 'required') && !in_array($field_name, $this->exportOptions['ExportColumns']) ) { + $missing_columns[] = $field_name; + $object->setError('ExportColumns', 'required_fields_missing', 'la_error_RequiredColumnsMissing'); + $ret = false; + } + } + + if (!$ret) { + $this->Application->Debugger->appendHTML('Missing required for import/export:'); + $this->Application->Debugger->dumpVars($missing_columns); + } + } + // 2. check, that we have only mixed category field or only separated category fields $category_found['mixed'] = false; $category_found['separated'] = false; @@ -372,6 +390,8 @@ $records_exported = 0; foreach ($records as $record_info) { $this->curItem->SetDBFieldsFromHash($record_info); + $this->setCurrentID(); + $this->curItem->raiseEvent('OnAfterItemLoad', $this->curItem->GetID() ); if ($this->hasCustomFields) { $this->loadItemCustomFields(); @@ -415,7 +435,7 @@ $field_name = preg_replace('/^Category(Path|[0-9]+)/', '__CATEGORY__Category\\1', $field_name); $valid_fields = $object->getPossibleExportColumns(); - return isset($valid_fields[$field_name]); + return isset($valid_fields[$field_name]) || isset($valid_fields['__VIRTUAL__'.$field_name]); } /** @@ -471,10 +491,11 @@ $this->customValues = Array(); $data = $this->readRecord(); if ($data) { + $this->curItem->Clear(); foreach ($data as $field_index => $field_value) { $this->setFieldValue($field_index, $field_value); } - $this->curItem->setID( $this->curItem->GetDBField($this->curItem->IDField) ); + $this->setCurrentID(); $this->processCurrentItem($event); } $bytes_imported = ftell($this->filePointer) - $this->exportOptions['start_from']; @@ -490,6 +511,11 @@ return $this->exportOptions; } + function setCurrentID() + { + $this->curItem->setID( $this->curItem->GetDBField($this->curItem->IDField) ); + } + function setFieldValue($field_index, $value) { $field_name = $this->exportFields[$field_index]; @@ -499,11 +525,15 @@ $this->customValues[$field_name] = $value; } elseif ($field_name == 'CategoryPath') { - $this->curItem->CategoryPath = explode($this->exportOptions['CategorySeparator'], $value); + $this->curItem->CategoryPath = $value ? explode($this->exportOptions['CategorySeparator'], $value) : Array(); } elseif (substr($field_name, 0, 8) == 'Category') { $this->curItem->CategoryPath[ (int)substr($field_name, 8) ] = $value; } + elseif (substr($field_name, 0, 11) == '__VIRTUAL__') { + $field_name = substr($field_name, 11); + $this->curItem->SetField($field_name, $value); + } else { $this->curItem->SetField($field_name, $value); } @@ -517,7 +547,7 @@ */ function &getTempItem(&$event) { - return $this->Application->recallObject($event->Prefix.'.-tmpitem', $event->Prefix, Array('skip_autoload' => true)); + return $this->Application->recallObject($event->Prefix.'.-tmpitem'.$event->Special, $event->Prefix, Array('skip_autoload' => true)); } /** @@ -701,6 +731,9 @@ { return $this->getCategoryTitle(); } + elseif (substr($field, 0, 11) == '__VIRTUAL__') { + $ret = substr($field, 11); + } else { $ret = $field; @@ -717,15 +750,17 @@ */ function getFieldValue($field) { - if (substr($field, 0, 10) == '__CUSTOM__') - { + if (substr($field, 0, 10) == '__CUSTOM__') { $field = substr($field, 10, strlen($field) ); $ret = isset($this->customValues[$field]) ? $this->customValues[$field] : ''; } - elseif (substr($field, 0, 12) == '__CATEGORY__') - { + elseif (substr($field, 0, 12) == '__CATEGORY__') { return $this->getCategoryPath(); } + elseif (substr($field, 0, 11) == '__VIRTUAL__') { + $field = substr($field, 11); + return $this->curItem->GetField($field); + } else { $ret = $this->curItem->GetField($field); @@ -742,16 +777,15 @@ */ function getCategoryTitle() { + // category path in separated fields + $category_count = $this->getMaxCategoryLevel(); if ($this->exportOptions['CategoryFormat'] == 1) { // category path in one field - return Array('CategoryPath'); + return $category_count ? Array('CategoryPath') : Array(); } else { - // category path in separated fields - $category_count = $this->getMaxCategoryLevel(); - $i = 0; $ret = Array(); while ($i < $category_count) { @@ -787,13 +821,13 @@ } } + $category_count = $this->getMaxCategoryLevel(); if ($this->exportOptions['CategoryFormat'] == 1) { // category path in single field - $category_path = Array( implode($this->exportOptions['CategorySeparator'], $category_path) ); + $category_path = $category_count ? Array( implode($this->exportOptions['CategorySeparator'], $category_path) ) : Array(); } else { // category path in separated fields - $category_count = $this->getMaxCategoryLevel(); $levels_used = count($category_path); if ($levels_used < $category_count) { Index: trunk/kernel/units/general/cat_event_handler.php =================================================================== diff -u -N -r3635 -r3698 --- trunk/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 3635) +++ trunk/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 3698) @@ -536,9 +536,25 @@ function OnAfterItemUpdate(&$event) { $this->CalculateHotLimit($event); + + if ( substr($event->Special, -6) == 'import') { + $this->setCustomExportColumns($event); + } } /** + * sets values for import process + * + * @param kEvent $event + */ + function OnAfterItemCreate(&$event) + { + if ( substr($event->Special, -6) == 'import') { + $this->setCustomExportColumns($event); + } + } + + /** * Makes simple search for products * based on keywords string * @@ -1423,15 +1439,16 @@ } // db fields - $fields = array_keys($object->Fields); - foreach ($fields as $field_name) + foreach ($object->Fields as $field_name => $field_options) { if (!$object->SkipField($field_name)) { - $available_columns[$field_name] = $field_name; + $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); } } + $available_columns = array_merge_recursive2($available_columns, $this->getCustomExportColumns($event)); + // custom fields $fields = array_keys($object->CustomFields); foreach ($fields as $field_name) @@ -1486,7 +1503,27 @@ } } + /** + * Returns specific to each item type columns only + * + * @param kEvent $event + * @return Array + */ + function getCustomExportColumns(&$event) + { + return Array(); + } + /** + * Sets non standart virtual fields (e.g. to other tables) + * + * @param kEvent $event + */ + function setCustomExportColumns(&$event) + { +// $object =& $event->getObject(); + } + // ImportLocalFilename function getExportField($field_key) Index: trunk/core/units/general/cat_event_handler.php =================================================================== diff -u -N -r3635 -r3698 --- trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 3635) +++ trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 3698) @@ -536,9 +536,25 @@ function OnAfterItemUpdate(&$event) { $this->CalculateHotLimit($event); + + if ( substr($event->Special, -6) == 'import') { + $this->setCustomExportColumns($event); + } } /** + * sets values for import process + * + * @param kEvent $event + */ + function OnAfterItemCreate(&$event) + { + if ( substr($event->Special, -6) == 'import') { + $this->setCustomExportColumns($event); + } + } + + /** * Makes simple search for products * based on keywords string * @@ -1423,15 +1439,16 @@ } // db fields - $fields = array_keys($object->Fields); - foreach ($fields as $field_name) + foreach ($object->Fields as $field_name => $field_options) { if (!$object->SkipField($field_name)) { - $available_columns[$field_name] = $field_name; + $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); } } + $available_columns = array_merge_recursive2($available_columns, $this->getCustomExportColumns($event)); + // custom fields $fields = array_keys($object->CustomFields); foreach ($fields as $field_name) @@ -1486,7 +1503,27 @@ } } + /** + * Returns specific to each item type columns only + * + * @param kEvent $event + * @return Array + */ + function getCustomExportColumns(&$event) + { + return Array(); + } + /** + * Sets non standart virtual fields (e.g. to other tables) + * + * @param kEvent $event + */ + function setCustomExportColumns(&$event) + { +// $object =& $event->getObject(); + } + // ImportLocalFilename function getExportField($field_key) Index: trunk/core/kernel/utility/formatters.php =================================================================== diff -u -N -r3523 -r3698 --- trunk/core/kernel/utility/formatters.php (.../formatters.php) (revision 3523) +++ trunk/core/kernel/utility/formatters.php (.../formatters.php) (revision 3698) @@ -142,31 +142,47 @@ class kOptionsFormatter extends kFormatter { -//function Format($value, $options, &$errors) function Format($value, $field_name, &$object, $format=null) { if ( is_null($value) ) return ''; $options = $object->GetFieldOptions($field_name); - if ( isset($format) ) $options['format'] = $format; $label = getArrayValue($options['options'], $value); - if( $label !== false ) - { - if( getArrayValue($options,'use_phrases') ) - { + if ($label !== false) { + if (getArrayValue($options, 'use_phrases')) { return $this->Application->Phrase($label); } - else - { + else { return $label; } } - else - { + else { return $value; } } + + function Parse($value, $field_name, &$object) + { + if ($value == '') return NULL; + + $options = $object->GetFieldOptions($field_name); + $use_phrases = getArrayValue($options, 'use_phrases'); + + $found = false; + foreach ($options['options'] as $option_key => $option_value) { + if ($use_phrases) { + $option_value = $this->Application->Phrase($option_value); + } + + if ($option_value == $value) { + $found = true; + break; + } + } + + return $found ? $option_key : $value; + } } /** Index: trunk/kernel/units/general/cat_dbitem_export.php =================================================================== diff -u -N -r3648 -r3698 --- trunk/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3648) +++ trunk/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 3698) @@ -170,7 +170,25 @@ $object->setError('ExportColumns', 'required'); $ret = false; } + else { + // 1.1. check that all required fields are present in imported file + $missing_columns = Array(); + foreach ($object->Fields as $field_name => $field_options) { + if ($object->SkipField($field_name)) continue; + if (getArrayValue($field_options, 'required') && !in_array($field_name, $this->exportOptions['ExportColumns']) ) { + $missing_columns[] = $field_name; + $object->setError('ExportColumns', 'required_fields_missing', 'la_error_RequiredColumnsMissing'); + $ret = false; + } + } + + if (!$ret) { + $this->Application->Debugger->appendHTML('Missing required for import/export:'); + $this->Application->Debugger->dumpVars($missing_columns); + } + } + // 2. check, that we have only mixed category field or only separated category fields $category_found['mixed'] = false; $category_found['separated'] = false; @@ -372,6 +390,8 @@ $records_exported = 0; foreach ($records as $record_info) { $this->curItem->SetDBFieldsFromHash($record_info); + $this->setCurrentID(); + $this->curItem->raiseEvent('OnAfterItemLoad', $this->curItem->GetID() ); if ($this->hasCustomFields) { $this->loadItemCustomFields(); @@ -415,7 +435,7 @@ $field_name = preg_replace('/^Category(Path|[0-9]+)/', '__CATEGORY__Category\\1', $field_name); $valid_fields = $object->getPossibleExportColumns(); - return isset($valid_fields[$field_name]); + return isset($valid_fields[$field_name]) || isset($valid_fields['__VIRTUAL__'.$field_name]); } /** @@ -471,10 +491,11 @@ $this->customValues = Array(); $data = $this->readRecord(); if ($data) { + $this->curItem->Clear(); foreach ($data as $field_index => $field_value) { $this->setFieldValue($field_index, $field_value); } - $this->curItem->setID( $this->curItem->GetDBField($this->curItem->IDField) ); + $this->setCurrentID(); $this->processCurrentItem($event); } $bytes_imported = ftell($this->filePointer) - $this->exportOptions['start_from']; @@ -490,6 +511,11 @@ return $this->exportOptions; } + function setCurrentID() + { + $this->curItem->setID( $this->curItem->GetDBField($this->curItem->IDField) ); + } + function setFieldValue($field_index, $value) { $field_name = $this->exportFields[$field_index]; @@ -499,11 +525,15 @@ $this->customValues[$field_name] = $value; } elseif ($field_name == 'CategoryPath') { - $this->curItem->CategoryPath = explode($this->exportOptions['CategorySeparator'], $value); + $this->curItem->CategoryPath = $value ? explode($this->exportOptions['CategorySeparator'], $value) : Array(); } elseif (substr($field_name, 0, 8) == 'Category') { $this->curItem->CategoryPath[ (int)substr($field_name, 8) ] = $value; } + elseif (substr($field_name, 0, 11) == '__VIRTUAL__') { + $field_name = substr($field_name, 11); + $this->curItem->SetField($field_name, $value); + } else { $this->curItem->SetField($field_name, $value); } @@ -517,7 +547,7 @@ */ function &getTempItem(&$event) { - return $this->Application->recallObject($event->Prefix.'.-tmpitem', $event->Prefix, Array('skip_autoload' => true)); + return $this->Application->recallObject($event->Prefix.'.-tmpitem'.$event->Special, $event->Prefix, Array('skip_autoload' => true)); } /** @@ -701,6 +731,9 @@ { return $this->getCategoryTitle(); } + elseif (substr($field, 0, 11) == '__VIRTUAL__') { + $ret = substr($field, 11); + } else { $ret = $field; @@ -717,15 +750,17 @@ */ function getFieldValue($field) { - if (substr($field, 0, 10) == '__CUSTOM__') - { + if (substr($field, 0, 10) == '__CUSTOM__') { $field = substr($field, 10, strlen($field) ); $ret = isset($this->customValues[$field]) ? $this->customValues[$field] : ''; } - elseif (substr($field, 0, 12) == '__CATEGORY__') - { + elseif (substr($field, 0, 12) == '__CATEGORY__') { return $this->getCategoryPath(); } + elseif (substr($field, 0, 11) == '__VIRTUAL__') { + $field = substr($field, 11); + return $this->curItem->GetField($field); + } else { $ret = $this->curItem->GetField($field); @@ -742,16 +777,15 @@ */ function getCategoryTitle() { + // category path in separated fields + $category_count = $this->getMaxCategoryLevel(); if ($this->exportOptions['CategoryFormat'] == 1) { // category path in one field - return Array('CategoryPath'); + return $category_count ? Array('CategoryPath') : Array(); } else { - // category path in separated fields - $category_count = $this->getMaxCategoryLevel(); - $i = 0; $ret = Array(); while ($i < $category_count) { @@ -787,13 +821,13 @@ } } + $category_count = $this->getMaxCategoryLevel(); if ($this->exportOptions['CategoryFormat'] == 1) { // category path in single field - $category_path = Array( implode($this->exportOptions['CategorySeparator'], $category_path) ); + $category_path = $category_count ? Array( implode($this->exportOptions['CategorySeparator'], $category_path) ) : Array(); } else { // category path in separated fields - $category_count = $this->getMaxCategoryLevel(); $levels_used = count($category_path); if ($levels_used < $category_count) {