Index: branches/5.2.x/core/units/helpers/cat_dbitem_export_helper.php =================================================================== diff -u -N -r14585 -r14596 --- branches/5.2.x/core/units/helpers/cat_dbitem_export_helper.php (.../cat_dbitem_export_helper.php) (revision 14585) +++ branches/5.2.x/core/units/helpers/cat_dbitem_export_helper.php (.../cat_dbitem_export_helper.php) (revision 14596) @@ -1,6 +1,6 @@ false) { + if ( $object == $this->false ) { $object =& $event->getObject(); /* @var $object kCatDBItem */ } @@ -161,13 +161,21 @@ $fields = $object->getFields(); foreach ($fields as $field_name => $field_options) { - if ($object->isVirtualField($field_name) || !getArrayValue($field_options, 'required') ) continue; - if ( $object->GetDBField($field_name) ) continue; + if ( $object->isVirtualField($field_name) || !$object->isRequired($field_name) ) { + continue; + } + if ( $object->GetDBField($field_name) ) { + continue; + } + $formatter_class = getArrayValue($field_options, 'formatter'); - if ($formatter_class) // not tested - { + + if ( $formatter_class ) { + // not tested $formatter =& $this->Application->recallObject($formatter_class); + /* @var $formatter kFormatter */ + $sample_value = $formatter->GetSample($field_name, $field_options, $object); } @@ -176,7 +184,7 @@ } $object->UpdateFormattersSubFields(); - if ($set_status && $has_empty) { + if ( $set_status && $has_empty ) { $object->SetDBField('Status', 0); } } @@ -185,8 +193,10 @@ * Verifies that all user entered export params are correct * * @param kEvent $event + * @return bool + * @access protected */ - function verifyOptions(&$event) + protected function verifyOptions(&$event) { if ($this->Application->RecallVar($event->getPrefixSpecial().'_ForceNotValid')) { @@ -197,12 +207,12 @@ $this->fillRequiredFields($event, $this->false); $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $cross_unique_fields = Array('FieldsSeparatedBy', 'FieldsEnclosedBy'); if (($object->GetDBField('CategoryFormat') == 1) || ($event->Special == 'import')) // in one field { - $object->setRequired('CategorySeparator', true); + $object->setRequired('CategorySeparator'); $cross_unique_fields[] = 'CategorySeparator'; } @@ -265,7 +275,7 @@ foreach ($fields as $field_name => $field_options) { if ($object->skipField($field_name)) continue; - if (getArrayValue($field_options, 'required') && !in_array($field_name, $this->exportOptions['ExportColumns']) ) { + if ( $object->isRequired($field_name) && !in_array($field_name, $this->exportOptions['ExportColumns']) ) { $missing_columns[] = $field_name; $object->setError('ExportColumns', 'required_fields_missing', 'la_error_RequiredColumnsMissing'); $ret = false; @@ -406,10 +416,12 @@ function getCustomSQL() { $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); + /* @var $ml_formatter kMultiLanguage */ $custom_sql = ''; + foreach ($this->customFields as $custom_id => $custom_name) { - $custom_sql .= 'custom_data.'.$ml_formatter->LangFieldName('cust_'.$custom_id).' AS cust_'.$custom_name.', '; + $custom_sql .= 'custom_data.' . $ml_formatter->LangFieldName('cust_' . $custom_id) . ' AS cust_' . $custom_name . ', '; } return substr($custom_sql, 0, -2); @@ -424,10 +436,12 @@ } else { $items_list =& $this->Application->recallObject($this->curItem->Prefix.'.export-items-list', $this->curItem->Prefix.'_List'); + /* @var $items_list kDBList */ + $items_list->SetPerPage(-1); if ($options['export_ids'] != '') { - $items_list->AddFilter('export_ids', $items_list->TableName.'.'.$items_list->IDField.' IN ('.implode(',',$options['export_ids']).')'); + $items_list->addFilter('export_ids', $items_list->TableName.'.'.$items_list->IDField.' IN ('.implode(',',$options['export_ids']).')'); } if ($count_only) { @@ -596,7 +610,7 @@ // 1. convert custom field $field_name = preg_replace('/^Custom_(.*)/', '__CUSTOM__\\1', $field_name); - // 2. convert category field (mixed version & serparated version) + // 2. convert category field (mixed version & separated version) $field_name = preg_replace('/^Category(Path|[0-9]+)/', '__CATEGORY__Category\\1', $field_name); $valid_fields = $object->getPossibleExportColumns(); @@ -686,31 +700,38 @@ $this->curItem->setID( $this->curItem->GetDBField($this->curItem->IDField) ); } - function setFieldValue($field_index, $value) + /** + * Sets value of import/export object + * @param int $field_index + * @param mixed $value + * @return void + * @access protected + */ + protected function setFieldValue($field_index, $value) { - if (empty($value)) { + if ( empty($value) ) { $value = null; } $field_name = getArrayValue($this->exportFields, $field_index); - if ($field_name == 'ResourceId') { - return false; + if ( $field_name == 'ResourceId' ) { + return ; } - if (substr($field_name, 0, 7) == 'Custom_') { - $field_name = 'cust_'.substr($field_name, 7); + if ( substr($field_name, 0, 7) == 'Custom_' ) { + $field_name = 'cust_' . substr($field_name, 7); $this->curItem->SetField($field_name, $value); } - elseif ($field_name == 'CategoryPath' || $field_name == '__CATEGORY__CategoryPath') { - $this->curItem->CategoryPath = $value ? explode($this->exportOptions['CategorySeparator'], $value) : Array(); + elseif ( $field_name == 'CategoryPath' || $field_name == '__CATEGORY__CategoryPath' ) { + $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) - 1 ] = $value; + elseif ( substr($field_name, 0, 8) == 'Category' ) { + $this->curItem->CategoryPath[(int)substr($field_name, 8) - 1] = $value; } - elseif (substr($field_name, 0, 20) == '__CATEGORY__Category') { - $this->curItem->CategoryPath[ (int)substr($field_name, 20) ] = $value; + elseif ( substr($field_name, 0, 20) == '__CATEGORY__Category' ) { + $this->curItem->CategoryPath[(int)substr($field_name, 20)] = $value; } - elseif (substr($field_name, 0, 11) == '__VIRTUAL__') { + elseif ( substr($field_name, 0, 11) == '__VIRTUAL__' ) { $field_name = substr($field_name, 11); $this->curItem->SetField($field_name, $value); } @@ -724,6 +745,14 @@ } } + /** + * Resets import object + * + * @param kEvent $event + * @param int $object_type + * @param Array $record_data + * @return void + */ function resetImportObject(&$event, $object_type, $record_data = null) { switch ($object_type) { @@ -1051,39 +1080,43 @@ { $category_id = $this->curItem->GetDBField('CategoryId'); $category_path = $this->getFromCache('category_path', $category_id); - if (!$category_path) - { + + if ( !$category_path ) { $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); - $sql = 'SELECT '.$ml_formatter->LangFieldName('CachedNavbar').' - FROM '.TABLE_PREFIX.'Category - WHERE CategoryId = '.$category_id; + /* @var $ml_formatter kMultiLanguage */ + + $sql = 'SELECT ' . $ml_formatter->LangFieldName('CachedNavbar') . ' + FROM ' . TABLE_PREFIX . 'Category + WHERE CategoryId = ' . $category_id; $category_path = $this->Conn->GetOne($sql); - $category_path = $category_path ? explode('&|&', $category_path) : Array(); - if ($category_path && strtolower($category_path[0]) == 'content') { + $category_path = $category_path ? explode('&|&', $category_path) : Array (); + + if ( $category_path && strtolower($category_path[0]) == 'content' ) { array_shift($category_path); } - if ($this->exportOptions['IsBaseCategory']) { + if ( $this->exportOptions['IsBaseCategory'] ) { $i = $this->exportOptions['BaseLevel']; - while ($i > 0) { + while ( $i > 0 ) { array_shift($category_path); $i--; } } $category_count = $this->getMaxCategoryLevel(); - if ($this->exportOptions['CategoryFormat'] == 1) { + + if ( $this->exportOptions['CategoryFormat'] == 1 ) { // category path in single field - $category_path = $category_count ? Array( implode($this->exportOptions['CategorySeparator'], $category_path) ) : Array(); + $category_path = $category_count ? Array (implode($this->exportOptions['CategorySeparator'], $category_path)) : Array (); } else { // category path in separated fields $levels_used = count($category_path); - if ($levels_used < $category_count) - { + + if ( $levels_used < $category_count ) { $i = 0; - while ($i < $category_count - $levels_used) { + while ( $i < $category_count - $levels_used ) { $category_path[] = ''; $i++; } @@ -1157,17 +1190,31 @@ return fgetcsv($this->filePointer, 10000, $this->exportOptions['FieldsSeparatedBy'], $this->exportOptions['FieldsEnclosedBy']); } + /** + * Saves import/export options + * + * @param kEvent $event + * @param Array $options + * @return void + */ function saveOptions(&$event, $options = null) { - if (!isset($options)) { + if ( !isset($options) ) { $options = $this->exportOptions; } - $this->Application->StoreVar($event->getPrefixSpecial().'_options', serialize($options) ); + + $this->Application->StoreVar($event->getPrefixSpecial() . '_options', serialize($options)); } + /** + * Loads import/export options + * + * @param kEvent $event + * @return void + */ function loadOptions(&$event) { - return unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); + return unserialize( $this->Application->RecallVar($event->getPrefixSpecial() . '_options') ); } /** @@ -1178,6 +1225,7 @@ function prepareExportColumns(&$event) { $object =& $event->getObject( Array('skip_autoload' => true) ); + /* @var $object kCatDBItem */ if ( !$object->isField('ExportColumns') ) { // import/export prefix was used (see kDBEventHandler::prepareObject) but object don't plan to be imported/exported @@ -1206,11 +1254,13 @@ foreach ($fields as $field_name => $field_options) { if ( !$object->skipField($field_name) ) { - $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); + $available_columns[$field_name] = $field_name.( $object->isRequired($field_name) ? '*' : ''); } } $handler =& $this->Application->recallObject($event->Prefix.'_EventHandler'); + /* @var $handler kDBEventHandler */ + $available_columns = array_merge($available_columns, $handler->getCustomExportColumns($event)); // custom fields @@ -1253,19 +1303,32 @@ $this->PrepareExportPresets($event); } + /** + * Prepares export presets + * + * @param kEvent $event + * @return void + */ function PrepareExportPresets(&$event) { - $object =& $event->getObject( Array('skip_autoload' => true) ); - $options = $object->GetFieldOptions('ExportPresets'); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ + $options = $object->GetFieldOptions('ExportPresets'); $export_settings = $this->Application->RecallPersistentVar('export_settings'); - if (!$export_settings) return ; - $export_settings = unserialize($export_settings); - if (!isset($export_settings[$event->Prefix])) return ; + if ( !$export_settings ) { + return; + } + $export_settings = unserialize($export_settings); - $export_presets = array(''=>''); + if ( !isset($export_settings[$event->Prefix]) ) { + return; + } + + $export_presets = array ('' => ''); + foreach ($export_settings[$event->Prefix] as $key => $val) { $export_presets[implode('|', $val['ExportColumns'])] = $key; } @@ -1292,28 +1355,32 @@ * Updates uploaded files list * * @param kEvent $event + * @return void + * @access protected */ - function updateImportFiles(&$event) + protected function updateImportFiles(&$event) { - if ($event->Special != 'import') { - return false; + if ( $event->Special != 'import' ) { + return ; } $object =& $event->getObject(); - $import_filenames = Array(); + $import_filenames = Array (); $file_helper =& $this->Application->recallObject('FileHelper'); /* @var $file_helper FileHelper */ $file_helper->CheckFolder(EXPORT_PATH); - if ($folder_handle = opendir(EXPORT_PATH)) { - while (false !== ($file = readdir($folder_handle))) { - if (is_dir(EXPORT_PATH.'/'.$file) || substr($file, 0, 1) == '.' || strtolower($file) == 'cvs' || strtolower($file) == 'dummy' || filesize(EXPORT_PATH.'/'.$file) == 0) continue; + if ( $folder_handle = opendir(EXPORT_PATH) ) { + while ( false !== ($file = readdir($folder_handle)) ) { + if ( is_dir(EXPORT_PATH . '/' . $file) || substr($file, 0, 1) == '.' || strtolower($file) == 'cvs' || strtolower($file) == 'dummy' || filesize(EXPORT_PATH . '/' . $file) == 0 ) { + continue; + } - $file_size = kUtil::formatSize( filesize(EXPORT_PATH.'/'.$file) ); - $import_filenames[$file] = $file.' ('.$file_size.')'; + $file_size = kUtil::formatSize(filesize(EXPORT_PATH . '/' . $file)); + $import_filenames[$file] = $file . ' (' . $file_size . ')'; } closedir($folder_handle); } @@ -1344,74 +1411,76 @@ */ function OnExportBegin(&$event) { - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if (!$items_info) - { - $items_info = unserialize( $this->Application->RecallVar($event->getPrefixSpecial().'_ItemsInfo') ); + $items_info = $this->Application->GetVar($event->getPrefixSpecial(true)); + + if ( !$items_info ) { + $items_info = unserialize($this->Application->RecallVar($event->getPrefixSpecial() . '_ItemsInfo')); $this->Application->SetVar($event->getPrefixSpecial(true), $items_info); } list($item_id, $field_values) = each($items_info); - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ + $object->SetFieldsFromHash($field_values); $field_values['ImportFilename'] = $object->GetDBField('ImportFilename'); //if upload formatter has renamed the file during moving !!! $object->setID($item_id); $this->setRequiredFields($event); // save export/import options - if ($event->Special == 'export') - { - $export_ids = $this->Application->RecallVar($event->Prefix.'_export_ids'); - $export_cats_ids = $this->Application->RecallVar($event->Prefix.'_export_cats_ids'); + if ( $event->Special == 'export' ) { + $export_ids = $this->Application->RecallVar($event->Prefix . '_export_ids'); + $export_cats_ids = $this->Application->RecallVar($event->Prefix . '_export_cats_ids'); // used for multistep export $field_values['export_ids'] = $export_ids ? explode(',', $export_ids) : false; - $field_values['export_cats_ids'] = $export_cats_ids ? explode(',', $export_cats_ids) : Array( $this->Application->GetVar('m_cat_id') ); + $field_values['export_cats_ids'] = $export_cats_ids ? explode(',', $export_cats_ids) : Array ($this->Application->GetVar('m_cat_id')); } $field_values['ExportColumns'] = $field_values['ExportColumns'] ? explode('|', substr($field_values['ExportColumns'], 1, -1) ) : Array(); $field_values['start_from'] = 0; - $this->Application->HandleEvent($nevent, $event->Prefix.':OnBeforeExportBegin', array('options'=>$field_values)); + $nevent = new kEvent($event->Prefix . ':OnBeforeExportBegin'); + $nevent->setEventParam('options', $field_values); + $this->Application->HandleEvent($nevent); $field_values = $nevent->getEventParam('options'); $this->saveOptions($event, $field_values); - if( $this->verifyOptions($event) ) - { - if ($this->_getExportSavePreset($object)) { + if ( $this->verifyOptions($event) ) { + if ( $this->_getExportSavePreset($object) ) { $name = $object->GetDBField('ExportPresetName'); $export_settings = $this->Application->RecallPersistentVar('export_settings'); - $export_settings = $export_settings ? unserialize($export_settings) : array(); + $export_settings = $export_settings ? unserialize($export_settings) : array (); $export_settings[$event->Prefix][$name] = $field_values; $this->Application->StorePersistentVar('export_settings', serialize($export_settings)); } $progress_t = $this->Application->RecallVar('export_progress_t'); - if ($progress_t) { + if ( $progress_t ) { $this->Application->RemoveVar('export_progress_t'); } else { - $progress_t = $this->getModuleName($event).'/'.$event->Special.'_progress'; + $progress_t = $this->getModuleName($event) . '/' . $event->Special . '_progress'; } $event->redirect = $progress_t; - if ($event->Special == 'import') { + if ( $event->Special == 'import' ) { $import_category = (int)$this->Application->RecallVar('ImportCategory'); // in future could use module root category if import category will be unavailable :) $event->SetRedirectParam('m_cat_id', $import_category); // for template permission checking $this->Application->StoreVar('m_cat_id', $import_category); // for event permission checking } } - else - { + else { // make uploaded file local & change source selection $filename = getArrayValue($field_values, 'ImportFilename'); - if ($filename) { + + if ( $filename ) { $this->updateImportFiles($event); $object->SetDBField('ImportSource', 2); $field_values['ImportSource'] = 2; @@ -1452,6 +1521,8 @@ $required_fields['export'] = Array('ExportFormat', 'ExportFilename','ExportColumns'); $object =& $event->getObject(); + /* @var $object kDBItem */ + if ($this->_getExportSavePreset($object)) { $required_fields['export'][] = 'ExportPresetName'; } @@ -1470,9 +1541,7 @@ } $required_fields = array_merge($required_fields['common'], $required_fields[$event->Special]); - foreach ($required_fields as $required_field) { - $object->setRequired($required_field, true); - } + $object->setRequired($required_fields); } } \ No newline at end of file