<?php
while(ob_get_level() > 1)
ob_end_clean();
$config =& hikashop_config();
$format = $config->get('export_format','csv');
$separator = $config->get('csv_separator',';');
$force_quote = $config->get('csv_force_quote',1);
$export = hikashop_get('helper.spreadsheet');
$export->init($format, 'hikashop_export', $separator, $force_quote);
if(!empty($this->orders)){
$maxProd = 0;
$productFields = null;
foreach($this->orders as $order){
$nbProd = count($order->products);
if($maxProd < $nbProd){
$maxProd = $nbProd;
if(empty($productFields)){
$productFields = array_keys(get_object_vars(reset($order->products)));
}
}
}
if($maxProd && !empty($productFields)) {
$first = array();
$o = reset($this->orders);
foreach($o as $key => $val) {
if(is_array($val))
continue;
$first[] = $key;
}
$o = null;
for($i=1;$i<=$maxProd;$i++){
foreach($productFields as $field){
$first[] = 'item'.$i.'_'.$field;
}
}
} else {
$first = array_keys(get_object_vars(reset($this->orders)));
}
$export->writeLine($first);
/* Added */
$db = JFactory::getDBO();
$db->setQuery('SELECT b.zone_namekey, b.zone_code_3 FROM '.hikashop_table('zone_link').' as a LEFT JOIN '.hikashop_table('zone').' AS b ON a.zone_child_namekey = b.zone_namekey WHERE a.zone_parent_namekey = '.$db->quote('country_United_States_of_America_223'));
$usStates = $db->loadObjectList();
$stateCodes = array();
foreach($usStates as $usState){
$stateCodes[$usState->zone_namekey] = $usState->zone_code_3;
}
/* EO Added */
foreach($this->orders as $row){
// Let's apply the right date format... an human one!
if(!empty($row->user_created)) $row->user_created = hikashop_getDate($row->user_created,'%Y-%m-%d %H:%M:%S');
if(!empty($row->order_created)) $row->order_created = hikashop_getDate($row->order_created,'%Y-%m-%d %H:%M:%S');
if(!empty($row->order_modified)) $row->order_modified = hikashop_getDate($row->order_modified,'%Y-%m-%d %H:%M:%S');
if($maxProd && !empty($productFields)){
for($i=1;$i<=$maxProd;$i++){
$prod =& $row->products[$i-1];
foreach($productFields as $field){
$n = 'item_'.$i.'_'.$field;
$row->$n = @$prod->$field;
}
}
}
/* Added */
foreach($row as $key => $value){
if(in_array($value,array_keys($stateCodes))){
$row->$key = $stateCodes[$value];
}
}
/* EO Added */
$export->writeLine($row);
}
}
$export->send();
exit;