<?

/* START - Import 'export module' BLOCK to sales_order page */
if (strstr($_SERVER['REQUEST_URI'],'sales_order')) {
 global $orderMag;
 global $json_data;
 

 include('includes.php');
 /* HERE the samples are converted to json*/
    
 //echo ($json_data);
 $data = json_decode($json_data,true);
 
 if (!$data) $msg = '
 <div style="background:rgb(255, 249, 233);color: rgb(235, 94, 0);text-align:center;padding:3px;">
 <strong>Syntax error in the samples.php</strong><br /><br />
 How to fix:<br />
 STEP 1: copy your json code (bellow given)<br />
 STEP 2: go to the http://www.jsonlint.com<br />
 STEP 3: paste and you will find where you have made the json syntax error<br />
 STEP 4: Correct it<br />
 <br /><strong>Your json code is:</strong><br />
 '. str_replace(",",", ",$json_data). '</div>';

 $samples = $data[0];
 $sampletexts = '';
 foreach($samples as $key => $value) {
   $sampletexts.='<option value="'.$key.'">'.$key.'</option>';
 }
 
/* START - Export action */
if ($_GET['export']=="true" && !strstr($_SERVER['REQUEST_URI'],'grid')) {
 $currentsample = $samples[$_GET['sample']];
 $ordersTXT = key($currentsample);
 $orderTXT = key($currentsample[$ordersTXT]);
 $xmlString = ""; 
 
 $filename = createFileName().".xml";
 
 /* START - Export orders from last export */
 if ($_GET['type']=="fromlast") {
 $killer=false;
 
 $lastexported = getLastId();
 $currentorder=$lastexported+1;
 $o=0;
 while ($killer==false) {
   $orderMag = Mage::getModel('sales/order')->load($currentorder);
   if($orderMag->getId()=="") {$killer=true;} else {
   $o++;
    $xmlString.= "<$orderTXT>".array2xml($currentsample[$ordersTXT][$orderTXT])."</$orderTXT>"; 
    $currentorder++;
   }
 }
 $currentorder--;
 $lastexported++;
 
 if ($o==0) {
   $msg = "<div style=\"background:rgb(255, 249, 233);color: rgb(235, 94, 0);text-align:center;padding:3px;\">No new order(s) since last export</div>";
 }else {
  $msg = "<div style=\"background:rgb(255, 249, 233);color: rgb(235, 94, 0);text-align:center;padding:3px;\">Exported orders: from ".$lastexported." to ". $currentorder. " with ".$_GET['sample']." sample</div>";
  saveExportsFile($_GET['sample'],"Exported orders: from ".$lastexported." to ". $currentorder,$filename,$currentorder);
 }
 }
 /* END - Export orders from last export */



 /* START - Export selected orders */
 if ($_GET['type']=="custom") {
 if ($_GET['orderlist']!="") {
 $orderlist = split("_",$_GET['orderlist']);
 $o=0;
 foreach ($orderlist as $currentorder) {
   $o++;
   $orderMag = Mage::getModel('sales/order')->load($currentorder);
   $xmlString.= "<$orderTXT>".array2xml($currentsample[$ordersTXT][$orderTXT])."</$orderTXT>"; 
 }
 
       foreach ($orderMag->getAllItems() as $item) {
   
      }
      //print_r($item);
 $msg = "<div style=\"background:rgb(255, 249, 233);color: rgb(235, 94, 0);text-align:center;padding:3px;\">Exported orders: ". str_replace("_", ", ",$_GET['orderlist']). " with ".$_GET['sample']." sample</div>";
 
 saveExportsFile($_GET['sample'],"Exported orders: ". str_replace("_", ", ",$_GET['orderlist']),$filename,getLastId()); 
 } else {$msg = '<div style="background:rgb(255, 249, 233);color: rgb(235, 94, 0);text-align:center;padding:3px;">No selected order(s)</div>';}
 }
 /* END - Export selected orders */


 
 /* START - Write XML to file */
 if ($o>0) {
  $xmlString = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><$ordersTXT>".$xmlString. "</$ordersTXT>";
  $myFile = "export/".$filename;
  $fh = fopen($myFile, 'w');
  fwrite($fh, formatXmlString(removeEmptyTags(removeEmptyTags($xmlString))));
  fclose($fh);
 }
  /* END - Write XML to file */ 
}
/* END - Export action */


/* START - Visible BLOCK */
?>

<script>function getValueSelected(obj){return obj.options[obj.selectedIndex].value;}</script>

<div class="entry-edit">
    <div class="entry-edit-head"><h4 class="icon-head head-edit-form fieldset-legend">Export Orders</h4></div>
    <div class="fieldset " id="base_fieldset">
    <div class="hor-scroll">
    <?=$msg;?>
<table width="100%">
<tr>
<td width="300"> 
 <strong>Export orders from last export</strong><br />
 Last exported orderID: <?=getLastId()?><br />
 Export sample: <select style="width:130px;margin-left:20px;" id="sz_type1" ><?=$sampletexts?></select><br />
 <a href="/index.php/admin/sales_order/?export=true&type=fromlast" onclick="this.href += '&sample='+getValueSelected(getElementById('sz_type1'));" id="sz_export1">Export</a>
 <div style="border-bottom: 4px solid rgb(223, 223, 223); height:1px; margin:15px 0;"></div>
 <strong>Export selected orders</strong><br />
 Export sample: <select style="width:130px;margin-left:20px;" id="sz_type2"><?=$sampletexts?></select><br />
 <a href="/index.php/admin/sales_order/?export=true&type=custom" onclick="this.href += '&sample='+getValueSelected(document.getElementById('sz_type2'))+'&orderlist='+sales_order_grid_massactionJsObject.getCheckedValues().replace(/,/g, '_');">Export</a>
</td>
<td>
 <div class="entry-edit-head"  style="text-align:center;background: rgb(238, 226, 190);margin-left:10px;"><h4 class="icon-head head-edit-form fieldset-legend" style="text-align:center; color:black">Exported XML file(s)</h4></div>
   <div style="margin-left:10px;height:160px;overflow:auto" class="fieldset " id="base_fieldset">
   <table width="100%">
   <?=getExportFiles()?>
   </table>
   </div>
</td>
</tr> 
</table>
    </div>
    </div>
</div>  
<?php 
/* END - Visible BLOCK */
}
/* END - Import 'export module' BLOCK to sales_order page */
?>
