Index: branches/unlabeled/unlabeled-1.66.2/core/units/general/cat_event_handler.php =================================================================== diff -u -r6553 -r6575 --- branches/unlabeled/unlabeled-1.66.2/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 6553) +++ branches/unlabeled/unlabeled-1.66.2/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 6575) @@ -461,7 +461,8 @@ if ($event->Special == 'export' || $event->Special == 'import') { - $this->prepareExportColumns($event); + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $export_helper->prepareExportColumns($event); } } @@ -1330,17 +1331,6 @@ /* === RELATED TO IMPORT/EXPORT: BEGIN === */ /** - * Returns module folder - * - * @param kEvent $event - * @return string - */ - function getModuleFolder(&$event) - { - return $this->Application->getUnitOption($event->Prefix, 'ModuleFolder'); - } - - /** * Shows export dialog * * @param kEvent $event @@ -1367,7 +1357,8 @@ $this->Application->StoreVar($event->Prefix.'_export_ids', $selected_ids ? implode(',', $selected_ids) : '' ); $this->Application->StoreVar($event->Prefix.'_export_cats_ids', $selected_cats_ids); - $event->redirect = $this->getModuleFolder($event).'/export'; + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $event->redirect = $export_helper->getModuleFolder($event).'/export'; $redirect_params = Array( 'm_opener' => 'd', $this->Prefix.'.export_event' => 'OnNew', @@ -1377,181 +1368,6 @@ } /** - * Export form validation & processing - * - * @param kEvent $event - */ - function OnExportBegin(&$event) - { - $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->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); - - $export_object =& $this->Application->recallObject('CatItemExportHelper'); - - // 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'); - - // 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['ExportColumns'] = $field_values['ExportColumns'] ? explode('|', substr($field_values['ExportColumns'], 1, -1) ) : Array(); - $field_values['start_from'] = 0; - $export_object->saveOptions($event, $field_values); - - if( $export_object->verifyOptions($event) ) - { - $event->redirect = $this->getModuleFolder($event).'/'.$event->Special.'_progress'; - - } - else - { - // make uploaded file local & change source selection - $filename = getArrayValue($field_values, 'ImportFilename'); - if ($filename) { - $this->updateImportFiles($event); - $object->SetDBField('ImportSource', 2); - $field_values['ImportSource'] = 2; - $object->SetDBField('ImportLocalFilename', $filename); - $field_values['ImportLocalFilename'] = $filename; - $export_object->saveOptions($event, $field_values); - } - - $event->status = erFAIL; - $event->redirect = false; - } - } - - /** - * Enter description here... - * - * @param kEvent $event - */ - function OnExportCancel(&$event) - { - $this->OnGoBack($event); - } - - /** - * Sets correct available & export fields - * - * @param kEvent $event - */ - function prepareExportColumns(&$event) - { - $object =& $event->getObject( Array('skip_autoload' => true) ); - - $available_columns = Array(); - - // category field (mixed) - $available_columns['__CATEGORY__CategoryPath'] = 'CategoryPath'; - - if ($event->Special == 'import') { - // category field (separated fields) - $max_level = $this->Application->ConfigValue('MaxImportCategoryLevels'); - $i = 0; - while ($i < $max_level) { - $available_columns['__CATEGORY__Category'.($i + 1)] = 'Category'.($i + 1); - $i++; - } - } - - // db fields - foreach ($object->Fields as $field_name => $field_options) - { - if (!$object->SkipField($field_name)) - { - $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); - } - } - - $available_columns = array_merge_recursive2($available_columns, $this->getCustomExportColumns($event)); - - // custom fields - foreach ($object->customFields as $custom_id => $custom_name) - { - $available_columns['__CUSTOM__'.$custom_name] = $custom_name; - } - - // columns already in use - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if ($items_info) - { - list($item_id, $field_values) = each($items_info); - $export_keys = $field_values['ExportColumns']; - $export_keys = $export_keys ? explode('|', substr($export_keys, 1, -1) ) : Array(); - } - else { - $export_keys = Array(); - } - - $export_columns = Array(); - foreach ($export_keys as $field_key) - { - $field_name = $this->getExportField($field_key); - $export_columns[$field_key] = $field_name; - unset($available_columns[$field_key]); - } - - $options = $object->GetFieldOptions('ExportColumns'); - $options['options'] = $export_columns; - $object->SetFieldOptions('ExportColumns', $options); - - $options = $object->GetFieldOptions('AvailableColumns'); - $options['options'] = $available_columns; - $object->SetFieldOptions('AvailableColumns', $options); - - $this->updateImportFiles($event); - } - - /** - * Updates uploaded files list - * - * @param kEvent $event - */ - function updateImportFiles(&$event) - { - if ($event->Special != 'import') { - return false; - } - - $object =& $event->getObject(); - - $import_filenames = Array(); - - 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 = formatSize( filesize(EXPORT_PATH.'/'.$file) ); - $import_filenames[$file] = $file.' ('.$file_size.')'; - } - closedir($folder_handle); - } - - $options = $object->GetFieldOptions('ImportLocalFilename'); - $options['options'] = $import_filenames; - $object->SetFieldOptions('ImportLocalFilename', $options); - } - - /** * Returns specific to each item type columns only * * @param kEvent $event @@ -1632,22 +1448,6 @@ return preg_match('#(http|https)://(.*)#', $path); } -// ImportLocalFilename - - function getExportField($field_key) - { - $prepends = Array('__CUSTOM__', '__CATEGORY__'); - foreach ($prepends as $prepend) - { - if (substr($field_key, 0, strlen($prepend) ) == $prepend) - { - $field_key = substr($field_key, strlen($prepend), strlen($field_key) ); - break; - } - } - return $field_key; - } - /** * Prepares item for import/export operations * @@ -1658,41 +1458,12 @@ parent::OnNew($event); if ($event->Special != 'import' && $event->Special != 'export') return ; - $this->setRequiredFields($event); + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $export_helper->setRequiredFields($event); $this->Application->StoreVar('ImportCategory', 0); } /** - * set required fields based on import or export params - * - * @param kEvent $event - */ - function setRequiredFields(&$event) - { - $required_fields['common'] = Array('FieldsSeparatedBy', 'LineEndings', 'CategoryFormat'); - - $required_fields['export'] = Array('ExportFormat', 'ExportFilename','ExportColumns'); - $required_fields['import'] = Array('FieldTitles', 'ImportSource', 'CheckDuplicatesMethod'); // ImportFilename, ImportLocalFilename - - $object =& $event->getObject(); - if ($event->Special == 'import') - { - $import_source = Array(1 => 'ImportFilename', 2 => 'ImportLocalFilename'); - $used_field = $import_source[ $object->GetDBField('ImportSource') ]; - - $required_fields[$event->Special][] = $used_field; - $object->Fields[$used_field]['error_field'] = 'ImportSource'; - - if ($object->GetDBField('FieldTitles') == 2) $required_fields[$event->Special][] = 'ExportColumns'; // manual field titles - } - - $required_fields = array_merge($required_fields['common'], $required_fields[$event->Special]); - foreach ($required_fields as $required_field) { - $object->setRequired($required_field, true); - } - } - - /** * Process items selected in item_selector * * @param kEvent $event Index: branches/unlabeled/unlabeled-1.25.2/kernel/units/admin/admin_tag_processor.php =================================================================== diff -u -r6486 -r6575 --- branches/unlabeled/unlabeled-1.25.2/kernel/units/admin/admin_tag_processor.php (.../admin_tag_processor.php) (revision 6486) +++ branches/unlabeled/unlabeled-1.25.2/kernel/units/admin/admin_tag_processor.php (.../admin_tag_processor.php) (revision 6575) @@ -319,7 +319,7 @@ ksort($section_data['children'], SORT_NUMERIC); foreach ($section_data['children'] as $priority => $section_name) { if (!$this->Application->CheckPermission($section_name.'.view', 1)) continue; - + $tab_data =& $sections_helper->getSectionData($section_name); $block_params['t'] = $tab_data['url']['t']; $block_params['title'] = $tab_data['label']; @@ -377,7 +377,7 @@ return $oFCKeditor->CreateHtml(); } - + /** * Allows to construct link for opening any type of catalog item selector * @@ -391,39 +391,102 @@ $mode = $params['mode']; unset($params['mode']); } - + $params['t'] = 'item_selector/item_selector_'.$mode; - + $default_params = Array('no_amp' => 1, 'pass' => 'all,'.$params['prefix']); unset($params['prefix']); - + $pass_through = Array(); if (isset($params['tabs_dependant'])) { // {yes, no} $pass_through['td'] = $params['tabs_dependant']; unset($params['tabs_dependant']); } - + if (isset($params['selection_mode'])) { // {single, multi} $pass_through['tm'] = $params['selection_mode']; unset($params['selection_mode']); } - + if (isset($params['tab_prefixes'])) { // {all, none, Application->ProcessParsedTag('m', 't', $params); } + + function TimeFrame($params) + { + $w = adodb_date('w'); + $m = adodb_date('m'); + $y = adodb_date('Y'); + //FirstDayOfWeek is 0 for Sunday and 1 for Monday + $fdow = $this->Application->ConfigValue('FirstDayOfWeek'); + if ($fdow && $w == 0) $w = 7; + $today_start = adodb_mktime(0,0,0,adodb_date('m'),adodb_date('d'),$y); + $first_day_of_this_week = $today_start - ($w - $fdow)*86400; + $first_day_of_this_month = adodb_mktime(0,0,0,$m,1,$y); + $this_quater = ceil($m/3); + $this_quater_start = adodb_mktime(0,0,0,$this_quater*3-2,1,$y); + + switch ($params['type']) { + case 'last_week_start': + $timestamp = $first_day_of_this_week - 86400*7; + break; + case 'last_week_end': + $timestamp = $first_day_of_this_week - 1; + break; + + case 'last_month_start': + $timestamp = $m == 1 ? adodb_mktime(0,0,0,12,1,$y-1) : adodb_mktime(0,0,0,$m-1,1,$y); + break; + case 'last_month_end': + $timestamp = $first_day_of_this_month = adodb_mktime(0,0,0,$m,1,$y) - 1; + break; + + case 'last_quater_start': + $timestamp = $this_quater == 1 ? adodb_mktime(0,0,0,10,1,$y-1) : adodb_mktime(0,0,0,($this_quater-1)*3-2,1,$y); + break; + case 'last_quater_end': + $timestamp = $this_quater_start - 1; + break; + + case 'last_6_months_start': + $timestamp = $m <= 6 ? adodb_mktime(0,0,0,$m+6,1,$y-1) : adodb_mktime(0,0,0,$m-6,1,$y); + break; + + case 'last_year_start': + $timestamp = adodb_mktime(0,0,0,1,1,$y-1); + break; + case 'last_year_end': + $timestamp = adodb_mktime(23,59,59,12,31,$y-1); + break; + } + + + if (isset($params['format'])) { + $format = $params['format']; + if(preg_match("/_regional_(.*)/", $format, $regs)) + { + $lang =& $this->Application->recallObject('lang.current'); + $format = $lang->GetDBField($regs[1]); + } + return adodb_date($format, $timestamp); + } + + return $timestamp; + + } } ?> \ No newline at end of file Index: branches/unlabeled/unlabeled-1.65.2/core/kernel/db/db_tag_processor.php =================================================================== diff -u -r6450 -r6575 --- branches/unlabeled/unlabeled-1.65.2/core/kernel/db/db_tag_processor.php (.../db_tag_processor.php) (revision 6450) +++ branches/unlabeled/unlabeled-1.65.2/core/kernel/db/db_tag_processor.php (.../db_tag_processor.php) (revision 6575) @@ -1587,6 +1587,53 @@ return $ret; } + function ExportStatus($params) + { + $export_object =& $this->Application->recallObject('CatItemExportHelper'); + + $event = new kEvent($this->getPrefixSpecial().':OnDummy'); + + $action_method = 'perform'.ucfirst($this->Special); + $field_values = $export_object->$action_method($event); + + // finish code is done from JS now + if ($field_values['start_from'] == $field_values['total_records']) + { + if ($this->Special == 'import') { + $this->Application->StoreVar('PermCache_UpdateRequired', 1); + $this->Application->Redirect('categories/cache_updater', Array('m_opener' => 'r', 'pass' => 'm', 'continue' => 1, 'no_amp' => 1)); + } + elseif ($this->Special == 'export') { + $finish_t = $this->Application->RecallVar('export_finish_t'); + $this->Application->Redirect($finish_t, Array('pass' => 'all')); + $this->Application->RemoveVar('export_finish_t'); + } + } + + $export_options = $export_object->loadOptions($event); + return $export_options['start_from'] * 100 / $export_options['total_records']; + } + + /** + * Returns path where exported category items should be saved + * + * @param Array $params + */ + function ExportPath($params) + { + $ret = EXPORT_PATH.'/'; + + if( getArrayValue($params, 'as_url') ) + { + $ret = str_replace( FULL_PATH.'/', $this->Application->BaseURL(), $ret); + } + + $export_options = unserialize($this->Application->RecallVar($this->getPrefixSpecial().'_options')); + $ret .= $export_options['ExportFilename'].'.'.($export_options['ExportFormat'] == 1 ? 'csv' : 'xml'); + + return $ret; + } + } ?> \ No newline at end of file Index: branches/unlabeled/unlabeled-1.13.2/kernel/units/general/cat_tag_processor.php =================================================================== diff -u -r6431 -r6575 --- branches/unlabeled/unlabeled-1.13.2/kernel/units/general/cat_tag_processor.php (.../cat_tag_processor.php) (revision 6431) +++ branches/unlabeled/unlabeled-1.13.2/kernel/units/general/cat_tag_processor.php (.../cat_tag_processor.php) (revision 6575) @@ -63,26 +63,6 @@ return $this->Application->ProcessParsedTag('m', 't', $params); } - /** - * Returns path where exported category items should be saved - * - * @param Array $params - */ - function ExportPath($params) - { - $ret = EXPORT_PATH.'/'; - - if( getArrayValue($params, 'as_url') ) - { - $ret = str_replace( FULL_PATH.'/', $this->Application->BaseURL(), $ret); - } - - $export_options = unserialize($this->Application->RecallVar($this->getPrefixSpecial().'_options')); - $ret .= $export_options['ExportFilename'].'.'.($export_options['ExportFormat'] == 1 ? 'csv' : 'xml'); - - return $ret; - } - function CategoryPath($params) { if (!isset($params['cat_id'])) { Index: branches/unlabeled/unlabeled-1.25.2/core/units/admin/admin_tag_processor.php =================================================================== diff -u -r6486 -r6575 --- branches/unlabeled/unlabeled-1.25.2/core/units/admin/admin_tag_processor.php (.../admin_tag_processor.php) (revision 6486) +++ branches/unlabeled/unlabeled-1.25.2/core/units/admin/admin_tag_processor.php (.../admin_tag_processor.php) (revision 6575) @@ -319,7 +319,7 @@ ksort($section_data['children'], SORT_NUMERIC); foreach ($section_data['children'] as $priority => $section_name) { if (!$this->Application->CheckPermission($section_name.'.view', 1)) continue; - + $tab_data =& $sections_helper->getSectionData($section_name); $block_params['t'] = $tab_data['url']['t']; $block_params['title'] = $tab_data['label']; @@ -377,7 +377,7 @@ return $oFCKeditor->CreateHtml(); } - + /** * Allows to construct link for opening any type of catalog item selector * @@ -391,39 +391,102 @@ $mode = $params['mode']; unset($params['mode']); } - + $params['t'] = 'item_selector/item_selector_'.$mode; - + $default_params = Array('no_amp' => 1, 'pass' => 'all,'.$params['prefix']); unset($params['prefix']); - + $pass_through = Array(); if (isset($params['tabs_dependant'])) { // {yes, no} $pass_through['td'] = $params['tabs_dependant']; unset($params['tabs_dependant']); } - + if (isset($params['selection_mode'])) { // {single, multi} $pass_through['tm'] = $params['selection_mode']; unset($params['selection_mode']); } - + if (isset($params['tab_prefixes'])) { // {all, none, Application->ProcessParsedTag('m', 't', $params); } + + function TimeFrame($params) + { + $w = adodb_date('w'); + $m = adodb_date('m'); + $y = adodb_date('Y'); + //FirstDayOfWeek is 0 for Sunday and 1 for Monday + $fdow = $this->Application->ConfigValue('FirstDayOfWeek'); + if ($fdow && $w == 0) $w = 7; + $today_start = adodb_mktime(0,0,0,adodb_date('m'),adodb_date('d'),$y); + $first_day_of_this_week = $today_start - ($w - $fdow)*86400; + $first_day_of_this_month = adodb_mktime(0,0,0,$m,1,$y); + $this_quater = ceil($m/3); + $this_quater_start = adodb_mktime(0,0,0,$this_quater*3-2,1,$y); + + switch ($params['type']) { + case 'last_week_start': + $timestamp = $first_day_of_this_week - 86400*7; + break; + case 'last_week_end': + $timestamp = $first_day_of_this_week - 1; + break; + + case 'last_month_start': + $timestamp = $m == 1 ? adodb_mktime(0,0,0,12,1,$y-1) : adodb_mktime(0,0,0,$m-1,1,$y); + break; + case 'last_month_end': + $timestamp = $first_day_of_this_month = adodb_mktime(0,0,0,$m,1,$y) - 1; + break; + + case 'last_quater_start': + $timestamp = $this_quater == 1 ? adodb_mktime(0,0,0,10,1,$y-1) : adodb_mktime(0,0,0,($this_quater-1)*3-2,1,$y); + break; + case 'last_quater_end': + $timestamp = $this_quater_start - 1; + break; + + case 'last_6_months_start': + $timestamp = $m <= 6 ? adodb_mktime(0,0,0,$m+6,1,$y-1) : adodb_mktime(0,0,0,$m-6,1,$y); + break; + + case 'last_year_start': + $timestamp = adodb_mktime(0,0,0,1,1,$y-1); + break; + case 'last_year_end': + $timestamp = adodb_mktime(23,59,59,12,31,$y-1); + break; + } + + + if (isset($params['format'])) { + $format = $params['format']; + if(preg_match("/_regional_(.*)/", $format, $regs)) + { + $lang =& $this->Application->recallObject('lang.current'); + $format = $lang->GetDBField($regs[1]); + } + return adodb_date($format, $timestamp); + } + + return $timestamp; + + } } ?> \ No newline at end of file Index: branches/unlabeled/unlabeled-1.13.2/core/units/general/cat_tag_processor.php =================================================================== diff -u -r6431 -r6575 --- branches/unlabeled/unlabeled-1.13.2/core/units/general/cat_tag_processor.php (.../cat_tag_processor.php) (revision 6431) +++ branches/unlabeled/unlabeled-1.13.2/core/units/general/cat_tag_processor.php (.../cat_tag_processor.php) (revision 6575) @@ -63,26 +63,6 @@ return $this->Application->ProcessParsedTag('m', 't', $params); } - /** - * Returns path where exported category items should be saved - * - * @param Array $params - */ - function ExportPath($params) - { - $ret = EXPORT_PATH.'/'; - - if( getArrayValue($params, 'as_url') ) - { - $ret = str_replace( FULL_PATH.'/', $this->Application->BaseURL(), $ret); - } - - $export_options = unserialize($this->Application->RecallVar($this->getPrefixSpecial().'_options')); - $ret .= $export_options['ExportFilename'].'.'.($export_options['ExportFormat'] == 1 ? 'csv' : 'xml'); - - return $ret; - } - function CategoryPath($params) { if (!isset($params['cat_id'])) { Index: branches/unlabeled/unlabeled-1.75.2/core/kernel/db/db_event_handler.php =================================================================== diff -u -r6547 -r6575 --- branches/unlabeled/unlabeled-1.75.2/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 6547) +++ branches/unlabeled/unlabeled-1.75.2/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 6575) @@ -116,9 +116,10 @@ 'OnPreCreate' => Array('self' => 'add|add.pending'), 'OnEdit' => Array('self' => 'edit|edit.pending'), + 'OnExport' => Array('self' => 'view|advanced:export'), + 'OnExportBegin' => Array('self' => 'view|advanced:export'), - // theese event do not harm, but just in case check them too :) 'OnCancelEdit' => Array('self' => true, 'subitem' => true), 'OnCancel' => Array('self' => true, 'subitem' => true), @@ -1831,13 +1832,13 @@ $virtual_fields['cust_'.$custom_name] = array_merge_recursive2($field_options, $virtual_fields['cust_'.$custom_name]); $custom_fields[$custom_id] = $custom_name; } - + $config_calculated_fields = $this->Application->getUnitOption($main_prefix, 'CalculatedFields', Array()); foreach ($config_calculated_fields as $special => $special_fields) { $config_calculated_fields[$special] = array_merge_recursive2($config_calculated_fields[$special], $calculated_fields); } $this->Application->setUnitOption($main_prefix, 'CalculatedFields', $config_calculated_fields); - + $this->Application->setUnitOption($main_prefix, 'CustomFields', $custom_fields); $this->Application->setUnitOption($main_prefix, 'VirtualFields', $virtual_fields); } @@ -1881,6 +1882,100 @@ $this->finalizePopup($event); } + +/** EXPORT RELATED **/ + + /** + * Shows export dialog + * + * @param kEvent $event + */ + function OnExport(&$event) + { + $this->StoreSelectedIDs($event); + $selected_ids = $this->getSelectedIDs($event); + + if (implode(',', $selected_ids) == '') { + // K4 fix when no ids found bad selected ids array is formed + $selected_ids = false; + } + + $this->Application->StoreVar($event->Prefix.'_export_ids', $selected_ids ? implode(',', $selected_ids) : '' ); + + $export_t = $this->Application->GetVar('export_template'); + $this->Application->LinkVar('export_finish_t'); + $this->Application->LinkVar('export_progress_t'); + $this->Application->StoreVar('export_oroginal_special', $event->Special); + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $event->redirect = $export_t ? $export_t : $export_helper->getModuleFolder($event).'/export'; + + $redirect_params = Array( 'm_opener' => 'd', + $this->Prefix.'.export_event' => 'OnNew', + 'pass' => 'all,'.$this->Prefix.'.export'); + + $event->setRedirectParams($redirect_params); + } + + /** + * Apply some special processing to + * object beeing recalled before using + * it in other events that call prepareObject + * + * @param Object $object + * @param kEvent $event + * @access protected + */ + function prepareObject(&$object, &$event) + { + if ($event->Special == 'export' || $event->Special == 'import') + { + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $export_helper->prepareExportColumns($event); + } + } + + /** + * Returns specific to each item type columns only + * + * @param kEvent $event + * @return Array + */ + function getCustomExportColumns(&$event) + { + return Array(); + } + + /** + * Export form validation & processing + * + * @param kEvent $event + */ + function OnExportBegin(&$event) + { + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $export_helper->OnExportBegin($event); + } + + /** + * Enter description here... + * + * @param kEvent $event + */ + function OnExportCancel(&$event) + { + $this->OnGoBack($event); + } + + /** + * Allows configuring export options + * + * @param kEvent $event + */ + function OnBeforeExportBegin(&$event) + { + + } + } Index: branches/unlabeled/unlabeled-1.19.2/core/units/general/helpers/permissions_helper.php =================================================================== diff -u -r6434 -r6575 --- branches/unlabeled/unlabeled-1.19.2/core/units/general/helpers/permissions_helper.php (.../permissions_helper.php) (revision 6434) +++ branches/unlabeled/unlabeled-1.19.2/core/units/general/helpers/permissions_helper.php (.../permissions_helper.php) (revision 6575) @@ -216,6 +216,7 @@ function showDebug($text, $params) { + return ; $is_ajax = $this->Application->GetVar('ajax') == 'yes' || isset($params['ajax']) || isset($params['tab_init']); if (!$this->Application->isDebugMode() || $is_ajax) return true; echo $text.'
'; Index: branches/unlabeled/unlabeled-1.21.2/kernel/units/general/cat_dbitem_export.php =================================================================== diff -u -r6114 -r6575 --- branches/unlabeled/unlabeled-1.21.2/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 6114) +++ branches/unlabeled/unlabeled-1.21.2/kernel/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 6575) @@ -364,34 +364,88 @@ function getCustomSQL() { $ml_formatter =& $this->Application->recallObject('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 preg_replace('/(.*), /', '\\1', $custom_sql); } - + + function getPlainExportSQL($count_only = false) { + if ($count_only && isset($this->exportOptions['ForceCountSQL'])) return $this->exportOptions['ForceCountSQL']; + if (!$count_only && isset($this->exportOptions['ForceSelectSQL'])) return $this->exportOptions['ForceSelectSQL']; + + $items_list =& $this->Application->recallObject($this->curItem->Prefix.'.export-items-list', $this->curItem->Prefix.'_List'); + $items_list->SetPerPage(-1); + return $items_list->GetSelectSQL($count_only); + + + if ($this->exportOptions['export_ids'] === false) + { + + + // get links from current category & all it's subcategories + $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'; + } + + $sql = 'SELECT item_table.*'.($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 .= ' WHERE 1'; + + $sql .= ' ORDER BY item_table.'.$this->curItem->IDField.' ASC'; // NEW + } + else { + // get only selected links + $sql = 'SELECT item_table.* + 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; + } + 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')) { + return $this->GetPlainExportSQL($count_only); // in case this is not a CategoryItem + } + if ($this->exportOptions['export_ids'] === false) { // get links from current category & all it's subcategories $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'; } - + $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'; - + foreach ($join_clauses as $table_name => $join_expression) { $sql .= ' LEFT JOIN '.$table_name.' ON '.$join_expression; } @@ -475,7 +529,7 @@ $this->curItem->SetDBFieldsFromHash($record_info); $this->setCurrentID(); $this->curItem->raiseEvent('OnAfterItemLoad', $this->curItem->GetID() ); - + $data_array = Array(); foreach ($this->exportFields as $export_field) { @@ -650,7 +704,7 @@ } $this->curItem->Clear(); $this->customFields = $this->Application->getUnitOption($event->Prefix, 'CustomFields'); - + if (isset($record_data)) { $this->setImportData($record_data); } @@ -679,7 +733,7 @@ if (is_null($lang_prefix)) { $lang_prefix = 'l'.$this->Application->GetVar('m_lang').'_'; } - + foreach ($this->curItem->CategoryPath as $category_index => $category_name) { if (!$category_name) continue; $category_key = crc32( implode(':', array_slice($this->curItem->CategoryPath, 0, $category_index + 1) ) ); @@ -766,7 +820,7 @@ else { $where_clause .= '(item_table.`'.$field_name.'` = '.$this->Conn->qstr($field_value).') AND '; } - + } $where_clause = preg_replace('/(.*) AND $/', '\\1', $where_clause); @@ -777,7 +831,7 @@ $parent_path = $this->getParentPath($category_id); $where_clause = '(c.ParentPath LIKE "'.$parent_path.'%") AND '.$where_clause; } - + $cdata_table = $this->Application->getUnitOption($event->Prefix.'-cdata', 'TableName'); $sql = 'SELECT '.$this->curItem->IDField.' FROM '.$this->curItem->TableName.' item_table @@ -828,11 +882,11 @@ /*function saveLog($msg) { static $first_time = true; - + $fp = fopen(FULL_PATH.'/sqls.log', $first_time ? 'w' : 'a'); fwrite($fp, $msg."\n"); fclose($fp); - + $first_time = false; }*/ @@ -1069,6 +1123,240 @@ { return unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); } + + /** + * Sets correct available & export fields + * + * @param kEvent $event + */ + function prepareExportColumns(&$event) + { + $object =& $event->getObject( Array('skip_autoload' => true) ); + + $available_columns = Array(); + + if ($this->Application->getUnitOption($event->Prefix, 'CatalogItem')) { + // category field (mixed) + $available_columns['__CATEGORY__CategoryPath'] = 'CategoryPath'; + + if ($event->Special == 'import') { + // category field (separated fields) + $max_level = $this->Application->ConfigValue('MaxImportCategoryLevels'); + $i = 0; + while ($i < $max_level) { + $available_columns['__CATEGORY__Category'.($i + 1)] = 'Category'.($i + 1); + $i++; + } + } + } + + // db fields + foreach ($object->Fields as $field_name => $field_options) + { + if (!$object->SkipField($field_name)) + { + $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); + } + } + + $handler =& $this->Application->recallObject($event->Prefix.'_EventHandler'); + $available_columns = array_merge_recursive2($available_columns, $handler->getCustomExportColumns($event)); + + // custom fields + foreach ($object->customFields as $custom_id => $custom_name) + { + $available_columns['__CUSTOM__'.$custom_name] = $custom_name; + } + + // columns already in use + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + if ($items_info) + { + list($item_id, $field_values) = each($items_info); + $export_keys = $field_values['ExportColumns']; + $export_keys = $export_keys ? explode('|', substr($export_keys, 1, -1) ) : Array(); + } + else { + $export_keys = Array(); + } + + $export_columns = Array(); + foreach ($export_keys as $field_key) + { + $field_name = $this->getExportField($field_key); + $export_columns[$field_key] = $field_name; + unset($available_columns[$field_key]); + } + + $options = $object->GetFieldOptions('ExportColumns'); + $options['options'] = $export_columns; + $object->SetFieldOptions('ExportColumns', $options); + + $options = $object->GetFieldOptions('AvailableColumns'); + $options['options'] = $available_columns; + $object->SetFieldOptions('AvailableColumns', $options); + + $this->updateImportFiles($event); + } + + function getExportField($field_key) + { + $prepends = Array('__CUSTOM__', '__CATEGORY__'); + foreach ($prepends as $prepend) + { + if (substr($field_key, 0, strlen($prepend) ) == $prepend) + { + $field_key = substr($field_key, strlen($prepend), strlen($field_key) ); + break; + } + } + return $field_key; + } + + /** + * Updates uploaded files list + * + * @param kEvent $event + */ + function updateImportFiles(&$event) + { + if ($event->Special != 'import') { + return false; + } + + $object =& $event->getObject(); + + $import_filenames = Array(); + + 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 = formatSize( filesize(EXPORT_PATH.'/'.$file) ); + $import_filenames[$file] = $file.' ('.$file_size.')'; + } + closedir($folder_handle); + } + + $options = $object->GetFieldOptions('ImportLocalFilename'); + $options['options'] = $import_filenames; + $object->SetFieldOptions('ImportLocalFilename', $options); + } + + /** + * Returns module folder + * + * @param kEvent $event + * @return string + */ + function getModuleFolder(&$event) + { + return $this->Application->getUnitOption($event->Prefix, 'ModuleFolder'); + } + + /** + * Export form validation & processing + * + * @param kEvent $event + */ + function OnExportBegin(&$event) + { + $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->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); + + $export_object =& $this->Application->recallObject('CatItemExportHelper'); + + // 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'); + + // 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['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)); + $field_values = $nevent->getEventParam('options'); + + $export_object->saveOptions($event, $field_values); + + if( $export_object->verifyOptions($event) ) + { + $progress_t = $this->Application->RecallVar('export_progress_t'); + if ($progress_t) { + $this->Application->RemoveVar('export_progress_t'); + } + else { + $progress_t = $export_object->getModuleFolder($event).'/'.$event->Special.'_progress'; + } + $event->redirect = $progress_t; + } + else + { + // make uploaded file local & change source selection + $filename = getArrayValue($field_values, 'ImportFilename'); + if ($filename) { + $export_object->updateImportFiles($event); + $object->SetDBField('ImportSource', 2); + $field_values['ImportSource'] = 2; + $object->SetDBField('ImportLocalFilename', $filename); + $field_values['ImportLocalFilename'] = $filename; + $export_object->saveOptions($event, $field_values); + } + + $event->status = erFAIL; + $event->redirect = false; + } + } + + /** + * set required fields based on import or export params + * + * @param kEvent $event + */ + function setRequiredFields(&$event) + { + $required_fields['common'] = Array('FieldsSeparatedBy', 'LineEndings', 'CategoryFormat'); + + $required_fields['export'] = Array('ExportFormat', 'ExportFilename','ExportColumns'); + $required_fields['import'] = Array('FieldTitles', 'ImportSource', 'CheckDuplicatesMethod'); // ImportFilename, ImportLocalFilename + + $object =& $event->getObject(); + if ($event->Special == 'import') + { + $import_source = Array(1 => 'ImportFilename', 2 => 'ImportLocalFilename'); + $used_field = $import_source[ $object->GetDBField('ImportSource') ]; + + $required_fields[$event->Special][] = $used_field; + $object->Fields[$used_field]['error_field'] = 'ImportSource'; + + if ($object->GetDBField('FieldTitles') == 2) $required_fields[$event->Special][] = 'ExportColumns'; // manual field titles + } + + $required_fields = array_merge($required_fields['common'], $required_fields[$event->Special]); + foreach ($required_fields as $required_field) { + $object->setRequired($required_field, true); + } + } + } ?> Index: branches/unlabeled/unlabeled-1.19.2/kernel/units/general/helpers/permissions_helper.php =================================================================== diff -u -r6434 -r6575 --- branches/unlabeled/unlabeled-1.19.2/kernel/units/general/helpers/permissions_helper.php (.../permissions_helper.php) (revision 6434) +++ branches/unlabeled/unlabeled-1.19.2/kernel/units/general/helpers/permissions_helper.php (.../permissions_helper.php) (revision 6575) @@ -216,6 +216,7 @@ function showDebug($text, $params) { + return ; $is_ajax = $this->Application->GetVar('ajax') == 'yes' || isset($params['ajax']) || isset($params['tab_init']); if (!$this->Application->isDebugMode() || $is_ajax) return true; echo $text.'
'; Index: branches/unlabeled/unlabeled-1.66.2/kernel/units/general/cat_event_handler.php =================================================================== diff -u -r6553 -r6575 --- branches/unlabeled/unlabeled-1.66.2/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 6553) +++ branches/unlabeled/unlabeled-1.66.2/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 6575) @@ -461,7 +461,8 @@ if ($event->Special == 'export' || $event->Special == 'import') { - $this->prepareExportColumns($event); + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $export_helper->prepareExportColumns($event); } } @@ -1330,17 +1331,6 @@ /* === RELATED TO IMPORT/EXPORT: BEGIN === */ /** - * Returns module folder - * - * @param kEvent $event - * @return string - */ - function getModuleFolder(&$event) - { - return $this->Application->getUnitOption($event->Prefix, 'ModuleFolder'); - } - - /** * Shows export dialog * * @param kEvent $event @@ -1367,7 +1357,8 @@ $this->Application->StoreVar($event->Prefix.'_export_ids', $selected_ids ? implode(',', $selected_ids) : '' ); $this->Application->StoreVar($event->Prefix.'_export_cats_ids', $selected_cats_ids); - $event->redirect = $this->getModuleFolder($event).'/export'; + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $event->redirect = $export_helper->getModuleFolder($event).'/export'; $redirect_params = Array( 'm_opener' => 'd', $this->Prefix.'.export_event' => 'OnNew', @@ -1377,181 +1368,6 @@ } /** - * Export form validation & processing - * - * @param kEvent $event - */ - function OnExportBegin(&$event) - { - $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->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); - - $export_object =& $this->Application->recallObject('CatItemExportHelper'); - - // 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'); - - // 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['ExportColumns'] = $field_values['ExportColumns'] ? explode('|', substr($field_values['ExportColumns'], 1, -1) ) : Array(); - $field_values['start_from'] = 0; - $export_object->saveOptions($event, $field_values); - - if( $export_object->verifyOptions($event) ) - { - $event->redirect = $this->getModuleFolder($event).'/'.$event->Special.'_progress'; - - } - else - { - // make uploaded file local & change source selection - $filename = getArrayValue($field_values, 'ImportFilename'); - if ($filename) { - $this->updateImportFiles($event); - $object->SetDBField('ImportSource', 2); - $field_values['ImportSource'] = 2; - $object->SetDBField('ImportLocalFilename', $filename); - $field_values['ImportLocalFilename'] = $filename; - $export_object->saveOptions($event, $field_values); - } - - $event->status = erFAIL; - $event->redirect = false; - } - } - - /** - * Enter description here... - * - * @param kEvent $event - */ - function OnExportCancel(&$event) - { - $this->OnGoBack($event); - } - - /** - * Sets correct available & export fields - * - * @param kEvent $event - */ - function prepareExportColumns(&$event) - { - $object =& $event->getObject( Array('skip_autoload' => true) ); - - $available_columns = Array(); - - // category field (mixed) - $available_columns['__CATEGORY__CategoryPath'] = 'CategoryPath'; - - if ($event->Special == 'import') { - // category field (separated fields) - $max_level = $this->Application->ConfigValue('MaxImportCategoryLevels'); - $i = 0; - while ($i < $max_level) { - $available_columns['__CATEGORY__Category'.($i + 1)] = 'Category'.($i + 1); - $i++; - } - } - - // db fields - foreach ($object->Fields as $field_name => $field_options) - { - if (!$object->SkipField($field_name)) - { - $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); - } - } - - $available_columns = array_merge_recursive2($available_columns, $this->getCustomExportColumns($event)); - - // custom fields - foreach ($object->customFields as $custom_id => $custom_name) - { - $available_columns['__CUSTOM__'.$custom_name] = $custom_name; - } - - // columns already in use - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if ($items_info) - { - list($item_id, $field_values) = each($items_info); - $export_keys = $field_values['ExportColumns']; - $export_keys = $export_keys ? explode('|', substr($export_keys, 1, -1) ) : Array(); - } - else { - $export_keys = Array(); - } - - $export_columns = Array(); - foreach ($export_keys as $field_key) - { - $field_name = $this->getExportField($field_key); - $export_columns[$field_key] = $field_name; - unset($available_columns[$field_key]); - } - - $options = $object->GetFieldOptions('ExportColumns'); - $options['options'] = $export_columns; - $object->SetFieldOptions('ExportColumns', $options); - - $options = $object->GetFieldOptions('AvailableColumns'); - $options['options'] = $available_columns; - $object->SetFieldOptions('AvailableColumns', $options); - - $this->updateImportFiles($event); - } - - /** - * Updates uploaded files list - * - * @param kEvent $event - */ - function updateImportFiles(&$event) - { - if ($event->Special != 'import') { - return false; - } - - $object =& $event->getObject(); - - $import_filenames = Array(); - - 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 = formatSize( filesize(EXPORT_PATH.'/'.$file) ); - $import_filenames[$file] = $file.' ('.$file_size.')'; - } - closedir($folder_handle); - } - - $options = $object->GetFieldOptions('ImportLocalFilename'); - $options['options'] = $import_filenames; - $object->SetFieldOptions('ImportLocalFilename', $options); - } - - /** * Returns specific to each item type columns only * * @param kEvent $event @@ -1632,22 +1448,6 @@ return preg_match('#(http|https)://(.*)#', $path); } -// ImportLocalFilename - - function getExportField($field_key) - { - $prepends = Array('__CUSTOM__', '__CATEGORY__'); - foreach ($prepends as $prepend) - { - if (substr($field_key, 0, strlen($prepend) ) == $prepend) - { - $field_key = substr($field_key, strlen($prepend), strlen($field_key) ); - break; - } - } - return $field_key; - } - /** * Prepares item for import/export operations * @@ -1658,41 +1458,12 @@ parent::OnNew($event); if ($event->Special != 'import' && $event->Special != 'export') return ; - $this->setRequiredFields($event); + $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + $export_helper->setRequiredFields($event); $this->Application->StoreVar('ImportCategory', 0); } /** - * set required fields based on import or export params - * - * @param kEvent $event - */ - function setRequiredFields(&$event) - { - $required_fields['common'] = Array('FieldsSeparatedBy', 'LineEndings', 'CategoryFormat'); - - $required_fields['export'] = Array('ExportFormat', 'ExportFilename','ExportColumns'); - $required_fields['import'] = Array('FieldTitles', 'ImportSource', 'CheckDuplicatesMethod'); // ImportFilename, ImportLocalFilename - - $object =& $event->getObject(); - if ($event->Special == 'import') - { - $import_source = Array(1 => 'ImportFilename', 2 => 'ImportLocalFilename'); - $used_field = $import_source[ $object->GetDBField('ImportSource') ]; - - $required_fields[$event->Special][] = $used_field; - $object->Fields[$used_field]['error_field'] = 'ImportSource'; - - if ($object->GetDBField('FieldTitles') == 2) $required_fields[$event->Special][] = 'ExportColumns'; // manual field titles - } - - $required_fields = array_merge($required_fields['common'], $required_fields[$event->Special]); - foreach ($required_fields as $required_field) { - $object->setRequired($required_field, true); - } - } - - /** * Process items selected in item_selector * * @param kEvent $event Index: branches/unlabeled/unlabeled-1.21.2/core/units/general/cat_dbitem_export.php =================================================================== diff -u -r6114 -r6575 --- branches/unlabeled/unlabeled-1.21.2/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 6114) +++ branches/unlabeled/unlabeled-1.21.2/core/units/general/cat_dbitem_export.php (.../cat_dbitem_export.php) (revision 6575) @@ -364,34 +364,88 @@ function getCustomSQL() { $ml_formatter =& $this->Application->recallObject('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 preg_replace('/(.*), /', '\\1', $custom_sql); } - + + function getPlainExportSQL($count_only = false) { + if ($count_only && isset($this->exportOptions['ForceCountSQL'])) return $this->exportOptions['ForceCountSQL']; + if (!$count_only && isset($this->exportOptions['ForceSelectSQL'])) return $this->exportOptions['ForceSelectSQL']; + + $items_list =& $this->Application->recallObject($this->curItem->Prefix.'.export-items-list', $this->curItem->Prefix.'_List'); + $items_list->SetPerPage(-1); + return $items_list->GetSelectSQL($count_only); + + + if ($this->exportOptions['export_ids'] === false) + { + + + // get links from current category & all it's subcategories + $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'; + } + + $sql = 'SELECT item_table.*'.($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 .= ' WHERE 1'; + + $sql .= ' ORDER BY item_table.'.$this->curItem->IDField.' ASC'; // NEW + } + else { + // get only selected links + $sql = 'SELECT item_table.* + 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; + } + 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')) { + return $this->GetPlainExportSQL($count_only); // in case this is not a CategoryItem + } + if ($this->exportOptions['export_ids'] === false) { // get links from current category & all it's subcategories $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'; } - + $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'; - + foreach ($join_clauses as $table_name => $join_expression) { $sql .= ' LEFT JOIN '.$table_name.' ON '.$join_expression; } @@ -475,7 +529,7 @@ $this->curItem->SetDBFieldsFromHash($record_info); $this->setCurrentID(); $this->curItem->raiseEvent('OnAfterItemLoad', $this->curItem->GetID() ); - + $data_array = Array(); foreach ($this->exportFields as $export_field) { @@ -650,7 +704,7 @@ } $this->curItem->Clear(); $this->customFields = $this->Application->getUnitOption($event->Prefix, 'CustomFields'); - + if (isset($record_data)) { $this->setImportData($record_data); } @@ -679,7 +733,7 @@ if (is_null($lang_prefix)) { $lang_prefix = 'l'.$this->Application->GetVar('m_lang').'_'; } - + foreach ($this->curItem->CategoryPath as $category_index => $category_name) { if (!$category_name) continue; $category_key = crc32( implode(':', array_slice($this->curItem->CategoryPath, 0, $category_index + 1) ) ); @@ -766,7 +820,7 @@ else { $where_clause .= '(item_table.`'.$field_name.'` = '.$this->Conn->qstr($field_value).') AND '; } - + } $where_clause = preg_replace('/(.*) AND $/', '\\1', $where_clause); @@ -777,7 +831,7 @@ $parent_path = $this->getParentPath($category_id); $where_clause = '(c.ParentPath LIKE "'.$parent_path.'%") AND '.$where_clause; } - + $cdata_table = $this->Application->getUnitOption($event->Prefix.'-cdata', 'TableName'); $sql = 'SELECT '.$this->curItem->IDField.' FROM '.$this->curItem->TableName.' item_table @@ -828,11 +882,11 @@ /*function saveLog($msg) { static $first_time = true; - + $fp = fopen(FULL_PATH.'/sqls.log', $first_time ? 'w' : 'a'); fwrite($fp, $msg."\n"); fclose($fp); - + $first_time = false; }*/ @@ -1069,6 +1123,240 @@ { return unserialize($this->Application->RecallVar($event->getPrefixSpecial().'_options')); } + + /** + * Sets correct available & export fields + * + * @param kEvent $event + */ + function prepareExportColumns(&$event) + { + $object =& $event->getObject( Array('skip_autoload' => true) ); + + $available_columns = Array(); + + if ($this->Application->getUnitOption($event->Prefix, 'CatalogItem')) { + // category field (mixed) + $available_columns['__CATEGORY__CategoryPath'] = 'CategoryPath'; + + if ($event->Special == 'import') { + // category field (separated fields) + $max_level = $this->Application->ConfigValue('MaxImportCategoryLevels'); + $i = 0; + while ($i < $max_level) { + $available_columns['__CATEGORY__Category'.($i + 1)] = 'Category'.($i + 1); + $i++; + } + } + } + + // db fields + foreach ($object->Fields as $field_name => $field_options) + { + if (!$object->SkipField($field_name)) + { + $available_columns[$field_name] = $field_name.(getArrayValue($field_options, 'required') ? '*' : ''); + } + } + + $handler =& $this->Application->recallObject($event->Prefix.'_EventHandler'); + $available_columns = array_merge_recursive2($available_columns, $handler->getCustomExportColumns($event)); + + // custom fields + foreach ($object->customFields as $custom_id => $custom_name) + { + $available_columns['__CUSTOM__'.$custom_name] = $custom_name; + } + + // columns already in use + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + if ($items_info) + { + list($item_id, $field_values) = each($items_info); + $export_keys = $field_values['ExportColumns']; + $export_keys = $export_keys ? explode('|', substr($export_keys, 1, -1) ) : Array(); + } + else { + $export_keys = Array(); + } + + $export_columns = Array(); + foreach ($export_keys as $field_key) + { + $field_name = $this->getExportField($field_key); + $export_columns[$field_key] = $field_name; + unset($available_columns[$field_key]); + } + + $options = $object->GetFieldOptions('ExportColumns'); + $options['options'] = $export_columns; + $object->SetFieldOptions('ExportColumns', $options); + + $options = $object->GetFieldOptions('AvailableColumns'); + $options['options'] = $available_columns; + $object->SetFieldOptions('AvailableColumns', $options); + + $this->updateImportFiles($event); + } + + function getExportField($field_key) + { + $prepends = Array('__CUSTOM__', '__CATEGORY__'); + foreach ($prepends as $prepend) + { + if (substr($field_key, 0, strlen($prepend) ) == $prepend) + { + $field_key = substr($field_key, strlen($prepend), strlen($field_key) ); + break; + } + } + return $field_key; + } + + /** + * Updates uploaded files list + * + * @param kEvent $event + */ + function updateImportFiles(&$event) + { + if ($event->Special != 'import') { + return false; + } + + $object =& $event->getObject(); + + $import_filenames = Array(); + + 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 = formatSize( filesize(EXPORT_PATH.'/'.$file) ); + $import_filenames[$file] = $file.' ('.$file_size.')'; + } + closedir($folder_handle); + } + + $options = $object->GetFieldOptions('ImportLocalFilename'); + $options['options'] = $import_filenames; + $object->SetFieldOptions('ImportLocalFilename', $options); + } + + /** + * Returns module folder + * + * @param kEvent $event + * @return string + */ + function getModuleFolder(&$event) + { + return $this->Application->getUnitOption($event->Prefix, 'ModuleFolder'); + } + + /** + * Export form validation & processing + * + * @param kEvent $event + */ + function OnExportBegin(&$event) + { + $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->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); + + $export_object =& $this->Application->recallObject('CatItemExportHelper'); + + // 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'); + + // 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['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)); + $field_values = $nevent->getEventParam('options'); + + $export_object->saveOptions($event, $field_values); + + if( $export_object->verifyOptions($event) ) + { + $progress_t = $this->Application->RecallVar('export_progress_t'); + if ($progress_t) { + $this->Application->RemoveVar('export_progress_t'); + } + else { + $progress_t = $export_object->getModuleFolder($event).'/'.$event->Special.'_progress'; + } + $event->redirect = $progress_t; + } + else + { + // make uploaded file local & change source selection + $filename = getArrayValue($field_values, 'ImportFilename'); + if ($filename) { + $export_object->updateImportFiles($event); + $object->SetDBField('ImportSource', 2); + $field_values['ImportSource'] = 2; + $object->SetDBField('ImportLocalFilename', $filename); + $field_values['ImportLocalFilename'] = $filename; + $export_object->saveOptions($event, $field_values); + } + + $event->status = erFAIL; + $event->redirect = false; + } + } + + /** + * set required fields based on import or export params + * + * @param kEvent $event + */ + function setRequiredFields(&$event) + { + $required_fields['common'] = Array('FieldsSeparatedBy', 'LineEndings', 'CategoryFormat'); + + $required_fields['export'] = Array('ExportFormat', 'ExportFilename','ExportColumns'); + $required_fields['import'] = Array('FieldTitles', 'ImportSource', 'CheckDuplicatesMethod'); // ImportFilename, ImportLocalFilename + + $object =& $event->getObject(); + if ($event->Special == 'import') + { + $import_source = Array(1 => 'ImportFilename', 2 => 'ImportLocalFilename'); + $used_field = $import_source[ $object->GetDBField('ImportSource') ]; + + $required_fields[$event->Special][] = $used_field; + $object->Fields[$used_field]['error_field'] = 'ImportSource'; + + if ($object->GetDBField('FieldTitles') == 2) $required_fields[$event->Special][] = 'ExportColumns'; // manual field titles + } + + $required_fields = array_merge($required_fields['common'], $required_fields[$event->Special]); + foreach ($required_fields as $required_field) { + $object->setRequired($required_field, true); + } + } + } ?>