enrichissement/application/controllers/EnvoiController.php

145 lines
3.9 KiB
PHP

<?php
class EnvoiController extends Zend_Controller_Action
{
public function init()
{
$this->view->headLink()->appendStylesheet('/themes/default/css/enrichissement.css');
$this->view->assign('request', $this->getRequest());
}
/**
* Enter description here ...
*/
public function indexAction(){
$this->redirect('/');
}
public function fileformAction()
{
$this->view->inlineScript()->appendFile('/libs/jquery.form.min.js', 'text/javascript');
$this->view->inlineScript()->appendFile('/themes/default/js/jqueryprogressbar.js', 'text/javascript');
$this->view->assign('filesize', ini_get('upload_max_filesize'));
//Récupérer les clients
$c = Zend_Registry::get('config');
$sqlmetier = Zend_Db::factory($c->profil->db->metier);
$sql = $sqlmetier->select()
->from('sdv1.clients', array('id', 'nom'))
->where("actif = 'Oui'");
$clients = $sqlmetier->fetchAll($sql, null, Zend_Db::FETCH_OBJ);
$this->view->assign('clients', $clients);
}
public function fileuploadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$name = $request->getParam('ref', '');
$idClient = $request->getParam('client');
if (empty($name)) {
echo "Référence non définie.";
}
else
{
$c = Zend_Registry::get('config');
$path = realpath($c->profil->path->data).'/validation';
if(!file_exists($path)) mkdir($path);
if ( isset($_FILES) && count($_FILES)==1 ){
$n = $_FILES['fichier']['name'];
$s = $_FILES['fichier']['size'];
$tmp_name = $_FILES['fichier']['tmp_name'];
$extValide = array('csv');
$extension = strrchr($n,'.');
$extension = substr($extension,1);
//Vérifier l'extension du fichier
if(!in_array($extension, $extValide)){
echo "Extension de fichier incorrect !";
} elseif (move_uploaded_file($tmp_name, $path.'/'.$idClient.'-'.$name.'.'.$extension)){
$url='/envoi/checkfile/file/'.$idClient.'-'.$name.'.'.$extension;
$this->redirect($url);
echo "Fichier envoyé, <a href=\"".
$this->view->url(array(
'controller' => 'envoi',
'action' => 'checkfile',
'file' => $idClient.'-'.$name.'.'.$extension,
))
."\">Vérifier le format</a>";
} else {
echo "Erreur : ".$_FILES['fichier']['error'];
}
}
}
}
/**
* Etat de progression de l'upload du fichier
*/
public function fileprogressAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$request = $this->getRequest();
$key = $request->getParam('key', '');
if (!empty($key)) {
//$rep sera égal à false si la clef n'existe pas dans le cache apc
$rep = apc_fetch('upload_'.$key);
echo json_encode($rep);
}
}
public function checkfileAction()
{
$request = $this->getRequest();
$file = $request->getParam('file');
$c = Zend_Registry::get('config');
$pathIn =$c->profil->path->data.'/validation';
$pathOut = $c->profil->path->data.'/clients';
if(!file_exists($pathOut)) mkdir($pathOut);
//Vérifier le format du fichier
$data = new Scores_Extract_Dict();
$result = $data->checkFileEntete($pathIn.'/'.$file);
if ($result===FALSE)
{
$this->view->assign('errors',array("Impossible de lire le fichier !"));
//Supprimer le fichier
unlink($pathIn.'/'.$file);
}
elseif (is_array($result))
{
$this->view->assign('errors',$result);
//Supprimer le fichier
unlink($pathIn.'/'.$file);
}
elseif (is_int($result))
{
//Enregistrer dans la bdd
$commandesM = new Application_Model_Commandes();
$data = array(
'fichier' => $file,
'idProfil' => 0,
'nbLigne' => $result,
'nbLigneT' => 0,
'error' => '',
'dateAdded' => date('Y-m-d H:i:s'),
);
if ($commandesM->insert($data)){
//Déplacer le fichier
rename($pathIn.'/'.$file, $pathOut.'/'.$file);
}
$this->view->assign('nb', $result);
}
}
}