enrichissement/application/controllers/EnvoiController.php

150 lines
4.2 KiB
PHP
Raw Normal View History

2012-01-13 13:35:11 +00:00
<?php
2012-05-09 21:34:16 +00:00
class EnvoiController extends Zend_Controller_Action
2012-01-13 13:35:11 +00:00
{
public function init()
{
$this->view->headScript()->appendFile('/scripts/enrichissement.js', 'text/javascript');
$this->view->headLink()->appendStylesheet('/styles/enrichissement.css');
2012-01-13 13:35:11 +00:00
}
2012-05-09 21:34:16 +00:00
2012-01-13 13:35:11 +00:00
/**
* Enter description here ...
*/
public function indexAction(){}
2012-05-09 21:34:16 +00:00
2012-01-13 13:35:11 +00:00
public function fileformAction()
{
$this->view->headScript()->appendFile('/scripts/jquery.form.js', 'text/javascript');
$this->view->headScript()->appendFile('/scripts/jqueryprogressbar.js', 'text/javascript');
2012-01-15 17:43:37 +00:00
$this->view->assign('filesize', ini_get('upload_max_filesize'));
//Récupérer les clients
$dbConfig = array(
2012-01-15 21:27:05 +00:00
'host' => MYSQL_HOST,
2012-01-27 16:20:40 +00:00
'port' => MYSQL_PORT,
2012-01-15 21:27:05 +00:00
'username' => MYSQL_USER,
'password' => MYSQL_PASS,
'dbname' => MYSQL_DEFAULT_DB,
2012-01-27 16:13:19 +00:00
'driver_options' => array(MYSQLI_INIT_COMMAND => 'SET NAMES UTF8;'),
//'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;')
2012-01-15 17:43:37 +00:00
);
2012-01-27 16:13:19 +00:00
$sqlmetier = Zend_Db::factory('Mysqli', $dbConfig);
2012-01-15 17:43:37 +00:00
$sql = $sqlmetier->select()
->from('sdv1.clients', array('id', 'nom'))
->where("actif = 'Oui'");
$clients = $sqlmetier->fetchAll($sql, null, Zend_Db::FETCH_OBJ);
2012-05-09 21:34:16 +00:00
$this->view->assign('clients', $clients);
2012-01-13 13:35:11 +00:00
}
2012-05-09 21:34:16 +00:00
2012-01-13 13:35:11 +00:00
public function fileuploadAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-09 21:34:16 +00:00
2012-01-19 15:25:47 +00:00
$request = $this->getRequest();
2012-05-09 21:34:16 +00:00
2012-01-19 15:25:47 +00:00
$name = $request->getParam('ref', '');
2012-02-01 16:58:38 +00:00
$idClient = $request->getParam('client');
if (empty($name)) {
2012-05-09 21:34:16 +00:00
echo "Référence non définie.";
}
else
{
2012-05-09 21:34:16 +00:00
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/configuration.ini', 'path');
2012-01-15 17:43:37 +00:00
$path = realpath($config->data).'/validation';
if(!file_exists($path)) mkdir($path);
2012-05-09 21:34:16 +00:00
if ( isset($_FILES) && count($_FILES)==1 ){
$n = $_FILES['fichier']['name'];
$s = $_FILES['fichier']['size'];
$tmp_name = $_FILES['fichier']['tmp_name'];
2012-05-09 21:34:16 +00:00
$extValide = array('csv');
$extension = strrchr($n,'.');
$extension = substr($extension,1);
//Vérifier l'extension du fichier
2012-05-09 21:34:16 +00:00
if(!in_array($extension, $extValide)){
echo "Extension de fichier incorrect !";
2012-01-15 17:43:37 +00:00
} elseif (move_uploaded_file($tmp_name, $path.'/'.$idClient.'-'.$name.'.'.$extension)){
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'];
}
2012-01-13 13:35:11 +00:00
}
}
2012-01-13 13:35:11 +00:00
}
2012-05-09 21:34:16 +00:00
2012-01-13 13:35:11 +00:00
/**
* Etat de progression de l'upload du fichier
*/
public function fileprogressAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
2012-05-09 21:34:16 +00:00
$request = $this->getRequest();
2012-01-13 13:35:11 +00:00
$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);
}
}
2012-05-09 21:34:16 +00:00
2012-01-15 17:43:37 +00:00
public function checkfileAction()
{
$request = $this->getRequest();
$file = $request->getParam('file');
2012-05-09 21:34:16 +00:00
2012-01-19 15:25:47 +00:00
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/configuration.ini', 'path');
2012-05-09 21:34:16 +00:00
$pathIn = $config->data.'/validation';
2012-01-19 15:25:47 +00:00
$pathOut = $config->data.'/clients';
if(!file_exists($pathOut)) mkdir($pathOut);
2012-05-09 21:34:16 +00:00
2012-01-15 17:43:37 +00:00
//Vérifier le format du fichier
require_once 'Scores/Enrichissement.php';
$data = new Enrichissement();
2012-05-09 21:34:16 +00:00
2012-01-15 17:43:37 +00:00
$result = $data->checkFileEntete($pathIn.'/'.$file);
2012-05-09 21:34:16 +00:00
if ($result===FALSE)
2012-01-15 17:43:37 +00:00
{
$this->view->assign('errors',array("Impossible de lire le fichier !"));
//Supprimer le fichier
unlink($pathIn.'/'.$file);
2012-05-09 21:34:16 +00:00
}
2012-01-15 17:43:37 +00:00
elseif (is_array($result))
{
2012-05-09 21:34:16 +00:00
$this->view->assign('errors',$result);
2012-01-15 17:43:37 +00:00
//Supprimer le fichier
unlink($pathIn.'/'.$file);
2012-05-09 21:34:16 +00:00
}
2012-01-15 17:43:37 +00:00
elseif (is_int($result))
{
//Enregistrer dans la bdd
$commandesM = new Application_Model_Commandes();
$data = array(
'fichier' => $file,
'idProfil' => 0,
'nbLigne' => $result,
'nbLigneT' => 0,
2012-01-19 15:25:47 +00:00
'error' => '',
2012-01-15 17:43:37 +00:00
'dateAdded' => date('Y-m-d H:i:s'),
);
if ($commandesM->insert($data)){
//Déplacer le fichier
2012-01-19 15:25:47 +00:00
rename($pathIn.'/'.$file, $pathOut.'/'.$file);
2012-01-15 17:43:37 +00:00
}
2012-01-19 15:25:47 +00:00
$this->view->assign('nb', $result);
2012-01-15 17:43:37 +00:00
}
}
2012-05-09 21:34:16 +00:00
2012-01-13 13:35:11 +00:00
}