<?php
/*******************************************************************************
  Az eredti descriptor file felépítése:
  =====================================
    $data["file"]    = $adatok[0];
    $data["eredeti"] = $adatok[1];
    $data["vendegkep"] = $adatok[2];
    $data["kepalap"] = $adatok[3];
    $data["cim"]     = $adatok[4];
    $data["cimhun"]  = $adatok[5] ? $adatok[5] : $adatok[4];
    $data["prver"]   = ($adatok[6]=="-1") ? "2012" : $adatok[6];

    Pl. (valójában sortörés nélkül van):
    ------------------------------------
    85151612-3251-2A4C-838D-BDAE5C2ED382.oli#
    85151612-3251-2A4C-838D-BDAE5C2ED382.oli#
    3000585D-7B7A-BF4D-A501-3ACDE09CCA31.bmp#
    3000585D-7B7A-BF4D-A501-3ACDE09CCA31.bmp#
    Logus_90_Arbore_NA_single (smartshapes(3).oli)#
    #
    2011
    
  Az új descriptor file felépítése:
  =====================================
    $data["file"]=$adatok[0],
    $data["vendegkep"]=$adatok[1],
    $data["nev_eng"]=$adatok[2],                                                             
    $data["nev_hun"]=$adatok[3],
    $data["tipus"]=$adatok[4],
    $data["kategoria"]=$adatok[5],
    $data["alkategoria"]=$adatok[6],
    $data["gyarto"]=$adatok[7],
    $data["prver"]=($adatok[8]=="-1" ? "2012" : $adatok[8]),

    Pl. (valójában sortörés nélkül van):
    ------------------------------------
    85151612-3251-2A4C-838D-BDAE5C2ED382.oli#
    3000585D-7B7A-BF4D-A501-3ACDE09CCA31.bmp#
    brown chair#
    barna szék#
    material#
    Wood#
    Oak#
    Marrakesh#
    -1
*******************************************************************************/      

defined('_JEXEC') or die; // No direct access to this file

jimport('joomla.application.component.view'); // import Joomla view library
jimport('joomla.application.module.helper');
jimport('joomla.session.session');

class ShowroomViewUpload extends JViewLegacy
{
  protected $arrAllowedGroups=array(7,8);
  protected $acceptLang=array('eng'=>'en-GB','hun'=>'hu-HU');
  protected $defLang='eng';
  protected $desc=array();
  protected $data=array();
  protected $selected_type;
  protected $selected_manufacture;
  protected $selected_family;
  protected $selected_category;
  protected $selected_subcategory;

  public $pf='ProductsController';	  
    
  public function display($tpl = null) 
  {
    $app=&JFactory::getApplication();
    $session=&JFactory::getSession();
    $document=&JFactory::getDocument();
    $model=$this->getModel();
    
    if (isset($_GET['lang']))
    {
      if (array_key_exists(trim($_GET['lang']), $this->acceptLang)) { $session->set('sr_lang',trim($_GET['lang'])); }
      else { $session->set('sr_lang',$this->defLang); }
    }
    
    if (isset($_GET['file'])) $session->set('sr_file',(trim(addslashes($_GET['file']))=='none' ? '' : trim(addslashes($_GET['file']))));
    if (isset($_GET['type'])) $session->set('sr_type',(trim(addslashes($_GET['type']))=='none' ? '' : trim(addslashes($_GET['type']))));
    if (isset($_GET['template']) && trim($_GET['template'])=='blankframe') $session->set('sr_template','blankframe');
    if (!isset($_SESSION['__default']['sr_lang'])) $session->set('sr_lang',$this->defLang);

    JFactory::getLanguage()->load('com_showroom', JPATH_SITE, $this->acceptLang[$session->get('sr_lang')], true);

    $this->sess=$_SESSION['__default'];
    $this->types=$this->get('Types');
    $this->manufactures=$model->getManufactures($this->sess['sr_type']);
    $this->families=$model->getFamilies($this->sess['sr_type'],$this->sess['sr_manufacture']);
    $this->family=$model->getFamily(((int)$this->sess["sr_family"]>0 ? $this->sess['sr_family'] : 0));
    $this->querystring='&lang='.$session->get('sr_lang')
      .($session->get('sr_template')>'' ? '&template='.$session->get('sr_template') : '')      
      .($session->get('sr_file')>'' ? '&file='.$session->get('sr_file') : '')
      .($session->get('sr_type')>'' ? '&type='.$session->get('sr_type') : '');

    if ($session->get('sr_file')>"") {
      $descriptor='/home/vendeg/'.$session->get('sr_file');
      if (is_file($descriptor))
      {
        $this->desc=explode("#",file_get_contents($descriptor));                                                                            

        if (is_array($this->desc) && count($this->desc)) { foreach ($this->desc as $k=>$v) $this->desc[$k]=trim($v); }

        if ($this->desc[0]==$this->desc[1]) // regi struktura
        {
          $this->data=array(
            "file"=>$this->desc[0],
            "vendegkep"=>$this->desc[2],
            "nev_eng"=>$this->desc[4],
            "nev_hun"=>($this->desc[5] ? $this->desc[5] : $this->desc[4]),
            "tipus"=>$this->sess['sr_type'],
            "kategoria"=>"",
            "alkategoria"=>"",
            "gyarto"=>"",
            "prver"=>($this->desc[6]=="-1" ? "2012" : $this->desc[6]),
            "ifc"=>false
          );
        }
        else // uj struktura
        {
          $this->data=array(
            "file"=>$this->desc[0],
            "vendegkep"=>$this->desc[1],
            "nev_eng"=>$this->desc[2],                                                             
            "nev_hun"=>$this->desc[3],
            "tipus"=>$this->desc[4],
            "kategoria"=>$this->desc[5],
            "alkategoria"=>$this->desc[6],
            "gyarto"=>$this->desc[7],
            "prver"=>($this->desc[8]=="-1" ? "2012" : $this->desc[8]),
            "ifc"=>(substr($this->desc[0],-4)==".ifc"?TRUE:FALSE)
          );
        }         
        
        if ($this->data['gyarto']>"") { $manufacure=$model->getManufactureByName($this->data['gyarto']); }
        if ($this->data['kategoria']>"") { $kategoria=$model->getParentByName($this->data['tipus'],$this->data['kategoria']); }
        if ($this->data['alkategoria']>"" && $kategoria->id>0) { $alkategoria=$model->getSubcatByName($this->data['tipus'],$kategoria->id,$this->data['alkategoria']); }
        
        $this->selected_type=$this->data['tipus'];
        $this->selected_manufacture=((int)$manufacure->id>0 ? (int)$manufacure->id : 0);
        $this->selected_family=$this->sess['sr_family'];
        $this->selected_category=((int)$kategoria->id>0 ? (int)$kategoria->id : 0);
        $this->selected_subcategory=((int)$alkategoria->id>0 ? (int)$alkategoria->id : 0);

//      kis kép elhelyezése a végleges helyére
        $honnan='/home/vendeg/'.$this->data['vendegkep'];
        $hova=$_SERVER['DOCUMENT_ROOT'].'/public/img/collection/'.$this->data['tipus'];
        if (!is_dir($hova)) mkdir($hova, 0777); 
        $hova.='/'.$this->data['vendegkep'];
        if (is_file($honnan) && !is_file($hova)) { copy($honnan,$hova); }

        if (isset($_POST['save']))
        {
          $tomb=array(
            'm_id'=>(int)$_POST['m_id'],
            'c_id'=>(int)$_POST['c_id'],
            'struct_id'=>(int)$_POST['subcat'],
            'title_hun'=>trim(addslashes($_POST['title_hun'])),
            'title_eng'=>trim(addslashes($_POST['title_eng'])),
            'desc_hun'=>trim(addslashes($_POST['desc_hun'])),
            'desc_eng'=>trim(addslashes($_POST['desc_eng'])),
            'tipus'=>$_POST['tipus'],
            'fajl'=>$this->data['file'],
            'version'=>$this->data['prver'],
            'deleted'=>(isset($_POST['deleted']) ? 0 : 1),
          );

//          $newID=($this->data['ifc']==FALSE ? $model->saveRecord($tomb) : (int)$_POST['joinID']);
          $newID=$model->saveRecord($tomb);
          
          if ((int)$newID>0)
          {
            $man=$model->getManufacture($tomb['m_id']);

//          objektum file(ok) elhelyezése a végleges helyére
            $honnan='/home/vendeg/'.$this->data['file'];
            $hova=$_SERVER['DOCUMENT_ROOT'].'/public/collection/'.$man->dir;
            if (!is_dir($hova)) mkdir($hova, 0777);
            $hova.='/'.$newID;
            if (!is_dir($hova)) mkdir($hova, 0777); 
            $hova.='/'.$this->data['file'];
            if (is_file($honnan)) copy($honnan,$hova);
            
            if ($this->data['ifc']==TRUE)
            {
              $honnan='/home/vendeg/'.str_replace(".ifc",".oli",$this->data['file']);
              $hova=$_SERVER['DOCUMENT_ROOT'].'/public/collection/'.$man->dir.'/'.$newID.'/'.str_replace(".ifc",".oli",$this->data['file']);
              if (is_file($honnan)) copy($honnan,$hova);
            }

//          feltöltött kép elhelyezése a végleges helyére
            if (is_array($_FILES['fileupload']) && !empty($_FILES['fileupload']) && /*$this->data['ifc']==FALSE &&*/ in_array($_FILES['fileupload']['type'],array('image/jpeg','image/jpg')))
            {
              $origSize=getimagesize($_FILES['fileupload']['tmp_name']);
              $hova=$_SERVER['DOCUMENT_ROOT']."/public/img/collection/".$man->dir;
              if (!is_dir($hova)) mkdir($hova,0777); 
              $hova.="/".$newID;
              if (!is_dir($hova)) mkdir($hova,0777);  
              if (!is_dir($hova."/orig")) mkdir($hova."/orig",0777); 
      
              $this->make_thumb($_FILES['fileupload']['tmp_name'], $hova.'/zz_big.jpg', (int)round(($origSize[0]/$origSize[1])*400),400);
              $this->make_thumb($_FILES['fileupload']['tmp_name'], $hova.'/zz_thumb.jpg', (int)round(($origSize[0]/$origSize[1])*120),120);
              $this->make_thumb($_FILES['fileupload']['tmp_name'], $hova.'/zz_small.jpg', (int)round(($origSize[0]/$origSize[1])*66),66);
              
              move_uploaded_file($_FILES['fileupload']['tmp_name'], $hova.'/orig/'.$_FILES['fileupload']['name']);
            }
          }
        }
      }
      else { print ("descriptor file nem elérhető"); }
		}      

    $this->parents=$model->getParents();
    $this->subcat=$model->getSubcat($this->selected_category);     
    $app->getPathway()->addItem(JText::_(COM_SHOWROOM_SHOWROOM), '/index.php?option=com_showroom&view=showroom');
    $app->getPathway()->addItem(JText::_(COM_SHOWROOM_UPLOAD));

    parent::display($tpl);
  }
  
  protected function make_thumb($src, $dest, $desired_width,$desired_h) 
  {
    $source_image=imagecreatefromjpeg($src);
    $width=imagesx($source_image);
    $height=imagesy($source_image);
    $desired_height=$desired_h;
    $virtual_image=imagecreatetruecolor($desired_width, $desired_height);
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
    imagejpeg($virtual_image, $dest);
  }   
} // end of class
?>