<?
/**
 * VoiceOne Settings/Extensions/import
 *
 * @package voiceone
 * @subpackage  contacts
 */
  $noSession=true;
  include ("../../../admin/config/cfg.php");
  //================== POST ====================
  function _contact2string($contact){
    $line="";
    $line.=$contact['name'];
    $line.=";".$contact['surname'];
    $line.=";".$contact['company'];
    $line.=";".$contact['order_by'];
    if($contact['read']==1) $line.=";1"; else $line.=";0";
    if($contact['write']==1) $line.=";1"; else $line.=";0"; 
    $line.=";".$contact['phonenumber'];
    if($contact['phonenumber_o']==1) $line.=";1"; else $line.=";0";
    $line.=";".$contact['mobile'];
    if($contact['mobile_on_cid']==1) $line.=";1"; else $line.=";0";
    $line.=";".$contact['fax'];
    if($contact['fax_on_cid']==1) $line.=";1"; else $line.=";0";
    return $line;
  }
  if((isset($_POST['type']))&&(isset($_POST['user']))&&($_POST['user']!=0)){
    $lineArr=array();
    switch($_POST['type']){
      case 0:
        //Export all contacts user can see
        $contactArr=$asterisk->getContact(0,$_POST['user'],0);
        for($i=0;$i<count($contactArr);$i++){
          $lineArr[]=_contact2string($contactArr[$i]);
        }
        break;
      case 1:
        //Export user's contacts
        $contactArr=$asterisk->getContact(0,$_POST['user'],1);
        for($i=0;$i<count($contactArr);$i++){
          $lineArr[]=_contact2string($contactArr[$i]);
        }
        break;
      case 2:
        //Export only user's private contacts
        $contactArr=$asterisk->getContact(0,$_POST['user'],1);
        if(is_array($contactArr)){
          for($i=0;$i<count($contactArr);$i++){
            if($contactArr[$i]['read']==1) continue;
            $lineArr[]=_contact2string($contactArr[$i]);
          }
        }
        break;
      case 3:
        //Export only user's public contacts
        $contactArr=$asterisk->getContact(0,$_POST['user'],1);
        if(is_array($contactArr)){
          for($i=0;$i<count($contactArr);$i++){
            if($contactArr[$i]['read']!=1) continue;
            $lineArr[]=_contact2string($contactArr[$i]);
          }
        }
        break;
    }
    $file_content=implode("\n",$lineArr);
    //return file
    $desc="";
    header('Content-Type: '.'text/csv');
    header('Content-Disposition: attachment; filename="export.csv"'); 
    echo $file_content;
    echo $cwUserId;
  }else{
    echo "<script> location.href='./'; </script>";
  }
?>