Index: branches/5.2.x/core/units/helpers/cat_dbitem_export_helper.php =================================================================== diff -u -N -r14596 -r14685 --- branches/5.2.x/core/units/helpers/cat_dbitem_export_helper.php (.../cat_dbitem_export_helper.php) (revision 14596) +++ branches/5.2.x/core/units/helpers/cat_dbitem_export_helper.php (.../cat_dbitem_export_helper.php) (revision 14685) @@ -1,6 +1,6 @@ getFields(); + if ( $object->isField('CreatedById') ) { + // CSV file was created without required CreatedById column + if ( $object->isRequired('CreatedById') ) { + $object->setRequired('CreatedById', false); + } + + if ( !is_numeric( $object->GetDBField('CreatedById') ) ) { + $object->SetDBField('CreatedById', $this->Application->RecallVar('user_id')); + } + } + foreach ($fields as $field_name => $field_options) { if ( $object->isVirtualField($field_name) || !$object->isRequired($field_name) ) { continue; @@ -170,12 +181,12 @@ } $formatter_class = getArrayValue($field_options, 'formatter'); - + if ( $formatter_class ) { // not tested $formatter =& $this->Application->recallObject($formatter_class); /* @var $formatter kFormatter */ - + $sample_value = $formatter->GetSample($field_name, $field_options, $object); } @@ -419,101 +430,101 @@ /* @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 . ', '; } return substr($custom_sql, 0, -2); } - function getPlainExportSQL($count_only = false) { - if ($count_only && isset($this->exportOptions['ForceCountSQL'])) { + function getPlainExportSQL($count_only = false) + { + if ( $count_only && isset($this->exportOptions['ForceCountSQL']) ) { $sql = $this->exportOptions['ForceCountSQL']; } - elseif (!$count_only && isset($this->exportOptions['ForceSelectSQL'])) { + elseif ( !$count_only && isset($this->exportOptions['ForceSelectSQL']) ) { $sql = $this->exportOptions['ForceSelectSQL']; } else { - $items_list =& $this->Application->recallObject($this->curItem->Prefix.'.export-items-list', $this->curItem->Prefix.'_List'); + $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']).')'); + if ( $this->exportOptions['export_ids'] != '' ) { + $items_list->addFilter('export_ids', $items_list->TableName . '.' . $items_list->IDField . ' IN (' . implode(',', $this->exportOptions['export_ids']) . ')'); } - if ($count_only) { - $sql = $items_list->getCountSQL( $items_list->GetSelectSQL(true,false) ); + if ( $count_only ) { + $sql = $items_list->getCountSQL($items_list->GetSelectSQL(true, false)); } else { $sql = $items_list->GetSelectSQL(); } } - if (!$count_only) - { - $sql .= ' LIMIT '.$this->exportOptions['start_from'].','.EXPORT_STEP; + if ( !$count_only ) { + $sql .= ' LIMIT ' . $this->exportOptions['start_from'] . ',' . EXPORT_STEP; } -// else { -// $sql = preg_replace("/^.*SELECT(.*?)FROM(?!_)/is", "SELECT COUNT(*) AS count FROM ", $sql); -// } + /*else { + $sql = preg_replace("/^.*SELECT(.*?)FROM(?!_)/is", "SELECT COUNT(*) AS count FROM ", $sql); + }*/ return $sql; } function getExportSQL($count_only = false) { - if (!$this->Application->getUnitOption($this->curItem->Prefix, 'CatalogItem')) { + if ( !$this->Application->getUnitOption($this->curItem->Prefix, 'CatalogItem') ) { return $this->GetPlainExportSQL($count_only); // in case this is not a CategoryItem } - if ($this->exportOptions['export_ids'] === false) - { + if ( $this->exportOptions['export_ids'] === false ) { // get links from current category & all it's subcategories - $join_clauses = Array(); + $join_clauses = Array (); $custom_sql = $this->getCustomSQL(); - if ($custom_sql) { - $custom_table = $this->Application->getUnitOption($this->curItem->Prefix.'-cdata', 'TableName'); - $join_clauses[$custom_table.' custom_data'] = 'custom_data.ResourceId = item_table.ResourceId'; + + if ( $custom_sql ) { + $custom_table = $this->Application->getUnitOption($this->curItem->Prefix . '-cdata', 'TableName'); + $join_clauses[$custom_table . ' custom_data'] = 'custom_data.ResourceId = item_table.ResourceId'; } - $join_clauses[TABLE_PREFIX.'CategoryItems ci'] = 'ci.ItemResourceId = item_table.ResourceId'; - $join_clauses[TABLE_PREFIX.'Category c'] = 'c.CategoryId = ci.CategoryId'; + $join_clauses[TABLE_PREFIX . 'CategoryItems ci'] = 'ci.ItemResourceId = item_table.ResourceId'; + $join_clauses[TABLE_PREFIX . 'Category c'] = 'c.CategoryId = ci.CategoryId'; - $sql = 'SELECT item_table.*, ci.CategoryId'.($custom_sql ? ', '.$custom_sql : '').' - FROM '.$this->curItem->TableName.' item_table'; + $sql = 'SELECT item_table.*, ci.CategoryId' . ($custom_sql ? ', ' . $custom_sql : '') . ' + FROM ' . $this->curItem->TableName . ' item_table'; foreach ($join_clauses as $table_name => $join_expression) { - $sql .= ' LEFT JOIN '.$table_name.' ON '.$join_expression; + $sql .= ' LEFT JOIN ' . $table_name . ' ON ' . $join_expression; } + $sql .= ' WHERE '; - if ($this->exportOptions['export_cats_ids'][0] == 0) - { + if ( $this->exportOptions['export_cats_ids'][0] == 0 ) { $sql .= '1'; } else { foreach ($this->exportOptions['export_cats_ids'] as $category_id) { - $sql .= '(c.ParentPath LIKE "%|'.$category_id.'|%") OR '; + $sql .= '(c.ParentPath LIKE "%|' . $category_id . '|%") OR '; } + $sql = substr($sql, 0, -4); } $sql .= ' ORDER BY ci.PrimaryCat DESC'; // NEW } else { // get only selected links - $sql = 'SELECT item_table.*, '.$this->exportOptions['export_cats_ids'][0].' AS CategoryId - FROM '.$this->curItem->TableName.' item_table - WHERE '.$this->curItem->IDField.' IN ('.implode(',', $this->exportOptions['export_ids']).')'; + $sql = 'SELECT item_table.*, ' . $this->exportOptions['export_cats_ids'][0] . ' AS CategoryId + FROM ' . $this->curItem->TableName . ' item_table + WHERE ' . $this->curItem->IDField . ' IN (' . implode(',', $this->exportOptions['export_ids']) . ')'; } - if (!$count_only) - { - $sql .= ' LIMIT '.$this->exportOptions['start_from'].','.EXPORT_STEP; + if ( !$count_only ) { + $sql .= ' LIMIT ' . $this->exportOptions['start_from'] . ',' . EXPORT_STEP; } else { $sql = preg_replace("/^.*SELECT(.*?)FROM(?!_)/is", "SELECT COUNT(*) AS count FROM ", $sql); @@ -729,7 +740,7 @@ $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; + $this->curItem->CategoryPath[(int)substr($field_name, 20) - 1] = $value; } elseif ( substr($field_name, 0, 11) == '__VIRTUAL__' ) { $field_name = substr($field_name, 11); @@ -747,7 +758,7 @@ /** * Resets import object - * + * * @param kEvent $event * @param int $object_type * @param Array $record_data @@ -765,6 +776,7 @@ break; } $this->curItem->Clear(); + $this->curItem->SetDBField('CategoryId', NULL); // since default value is import root category $this->customFields = $this->Application->getUnitOption($event->Prefix, 'CustomFields'); if (isset($record_data)) { @@ -1260,7 +1272,7 @@ $handler =& $this->Application->recallObject($event->Prefix.'_EventHandler'); /* @var $handler kDBEventHandler */ - + $available_columns = array_merge($available_columns, $handler->getCustomExportColumns($event)); // custom fields @@ -1328,7 +1340,7 @@ } $export_presets = array ('' => ''); - + foreach ($export_settings[$event->Prefix] as $key => $val) { $export_presets[implode('|', $val['ExportColumns'])] = $key; } @@ -1479,7 +1491,7 @@ else { // make uploaded file local & change source selection $filename = getArrayValue($field_values, 'ImportFilename'); - + if ( $filename ) { $this->updateImportFiles($event); $object->SetDBField('ImportSource', 2); @@ -1522,7 +1534,7 @@ $object =& $event->getObject(); /* @var $object kDBItem */ - + if ($this->_getExportSavePreset($object)) { $required_fields['export'][] = 'ExportPresetName'; }