182 lines
5.0 KiB
PHP
182 lines
5.0 KiB
PHP
<?php
|
|
|
|
class AddFileOrder extends Module
|
|
{
|
|
protected $token = '5d4f496cffb54a296b2a539cc8fda9c19876cc43';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->name = 'addfileorder';
|
|
$this->tab = 'back_office_order_feature';
|
|
$this->version = '1.0';
|
|
$this->author = 'Antadis';
|
|
$this->need_instance = 0;
|
|
$this->ps_versions_compliance = array('min' => '1.5', 'max' => _PS_VERSION_);
|
|
|
|
parent::__construct();
|
|
|
|
$this->displayName = $this->l('Add file in order');
|
|
$this->description = $this->l('Ajout de fichier pour une commande');
|
|
|
|
$this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');
|
|
}
|
|
|
|
public function install()
|
|
{
|
|
return parent::install() && $this->registerHook('DisplayAdminOrder') && $this->registerHook('displayOrderDetail') && $this->registerHook('DisplayHeader');
|
|
}
|
|
|
|
public function hookDisplayHeader($params)
|
|
{
|
|
Tools::addCSS($this->_path.'views/css/addfileorder.css', 'all');
|
|
Tools::addJS($this->_path.'views/js/addfileorder.js', 'all');
|
|
}
|
|
|
|
public function hookDisplayAdminOrder($params)
|
|
{
|
|
Tools::addCSS($this->_path.'views/css/addfileorder.css', 'all');
|
|
Tools::addJS($this->_path.'views/js/addfileorder.js', 'all');
|
|
|
|
$id_order = Tools::getValue('id_order');
|
|
$error_add = null;
|
|
$error_del = null;
|
|
|
|
if(isset($_FILES['file'])) {
|
|
$error_add = $this->addFileGet($id_order);
|
|
}
|
|
|
|
if(Tools::getValue('fileName')) {
|
|
$error_del = $this->deleteFile(Tools::getValue('fileName'), $id_order);
|
|
}
|
|
|
|
$this->context->smarty->assign(
|
|
array(
|
|
'dirExist' => $this->checkOrderDir($id_order),
|
|
'files' => $this->getFileOrderAdmin($id_order),
|
|
'file_add_success' => $error_add,
|
|
'file_delete_success' => $error_del,
|
|
)
|
|
);
|
|
|
|
return $this->display(__FILE__, 'views/templates/admin/addfileorder.tpl');
|
|
}
|
|
|
|
public function hookDisplayOrderDetail($params)
|
|
{
|
|
$id_order = Tools::getValue('id_order');
|
|
|
|
$this->context->smarty->assign(
|
|
array(
|
|
'dirExist' => $this->checkOrderDir($id_order),
|
|
'files' => $this->getFileOrderCustomer($id_order),
|
|
)
|
|
);
|
|
|
|
return $this->display(__FILE__, 'views/templates/hook/addfileorder.tpl');
|
|
}
|
|
|
|
private function deleteFile($name, $orderNumber)
|
|
{
|
|
if ($this->checkOrderDir($orderNumber) && unlink(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber.'/'.$name)) {
|
|
if ($this->countNumberFile($orderNumber) === 0){
|
|
if (rmdir(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function countNumberFile($orderNumber)
|
|
{
|
|
$numberFile = 0;
|
|
if ($this->checkOrderDir($orderNumber)) {
|
|
foreach (new DirectoryIterator(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber) as $file) {
|
|
if(!$file->isDot()) {
|
|
$numberFile++;
|
|
}
|
|
}
|
|
return $numberFile;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected function getFileOrderCustomer($orderNumber)
|
|
{
|
|
$files = array();
|
|
if ($this->checkOrderDir($orderNumber)) {
|
|
foreach (new DirectoryIterator(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber) as $file) {
|
|
if(!$file->isDot()) {
|
|
array_push($files, array(
|
|
'name' => $file->getFilename(),
|
|
'download' => $this->context->link->getModuleLink('addfileorder', 'getFile').'?nameFile='.$file->getFilename().'&id_order='.$orderNumber,
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
return $files;
|
|
}
|
|
|
|
private function getFileOrderAdmin($orderNumber)
|
|
{
|
|
$files = array();
|
|
if ($this->checkOrderDir($orderNumber)) {
|
|
foreach (new DirectoryIterator(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber) as $file) {
|
|
if(!$file->isDot()) {
|
|
array_push($files, array(
|
|
'name' => $file->getFilename(),
|
|
'download' => $this->context->link->getModuleLink('addfileorder', 'getFile').'?token='.$this->token.'&nameFile='.$file->getFilename().'&id_order='.$orderNumber,
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
return $files;
|
|
}
|
|
|
|
private function addFileGet($orderNumber)
|
|
{
|
|
if (file_exists(_PS_ROOT_DIR_.'/../files') === false) {
|
|
mkdir(_PS_ROOT_DIR_.'/../files');
|
|
}
|
|
|
|
$numberFile = $this->countNumberFile($orderNumber);
|
|
$upload = new Uploader();
|
|
$upload->setAcceptTypes(array('pdf', 'jpeg', 'jpg'));
|
|
|
|
if ($this->checkOrderDir($orderNumber)) {
|
|
$upload->setSavePath(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber.'/');
|
|
$upload->upload($_FILES['file']);
|
|
} else {
|
|
mkdir(_PS_ROOT_DIR_.'/../files/order_'.$orderNumber);
|
|
$upload->setSavePath(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber.'/');
|
|
$upload->upload($_FILES['file']);
|
|
}
|
|
|
|
if ($this->countNumberFile($orderNumber) > $numberFile) {
|
|
return true;
|
|
}
|
|
|
|
if ($this->countNumberFile($orderNumber) === 0) {
|
|
rmdir(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected function checkOrderDir($orderNumber)
|
|
{
|
|
if (file_exists(_PS_ROOT_DIR_.'/../files/order_'.(int) $orderNumber) === false) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
} |