Skip to content

Commit

Permalink
// add dropzone import and install module with basic error restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoustai authored and tchauviere committed Jan 20, 2016
1 parent 3434dac commit 6502f97
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/PrestaShopBundle/Controller/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function importAction(Request $request)

public function moduleAction(Request $request)
{
$action = $request->attributes->get('action'). 'Module';
$module = $request->attributes->get('module_name');

$ret = array();
Expand Down Expand Up @@ -106,20 +105,21 @@ public function moduleAction(Request $request)
*/
public function importModuleAction(Request $request)
{
$action = $request->attributes->get('action');
$module_name = $request->attributes->get('module_name').'.zip';

$file = $request->files->get($module_name);
$file = $request->files->get('module_file');
$orinal_file_name = $file->getClientOriginalName();
$module_name = basename($orinal_file_name, '.'.$file->guessExtension());

if ($file) {
$file_name = md5(uniqid()) . '.zip';
$file_name = md5(uniqid()).'.zip';
$file->move(_PS_CACHE_DIR_.'tmp'.DIRECTORY_SEPARATOR.'upload', $file_name);

if (!$this->unzipModule($file_name, $module_name)) {
// @TODO: clean file
return new JsonResponse(array('status' => false, 'msg' => 'unzip failed'), 200, array( 'Content-Type' => 'application/json' ));
}

if (!$this->install($module_name)) {
if (!$this->installModule($module_name)) {
// @TODO: clean file
return new JsonResponse(array('status' => false, 'msg' => 'Module Failed to install'), 200, array( 'Content-Type' => 'application/json' ));
}

Expand All @@ -132,10 +132,10 @@ public function importModuleAction(Request $request)

final private function unzipModule($file_name, $module_name)
{
$file = _PS_CACHE_DIR_.'tmp'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.$filename;
$file = _PS_CACHE_DIR_.'tmp'.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.$file_name;

if (file_exist($file)) {
$zip_archive = new ZipArchive();
if (file_exists($file)) {
$zip_archive = new \ZipArchive();
$ret = $zip_archive->open($file);

if ($ret === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ admin_module_manage_update_all:
_controller: PrestaShopBundle\Controller\Admin\ModuleController::moduleAction

admin_module_import:
path: /import/{module_name}
path: /import
methods: [POST]
defaults:
module_name:
Expand Down
20 changes: 20 additions & 0 deletions src/PrestaShopBundle/Resources/public/admin/js/module/drop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$(document).ready(function() {

$('a[title="Add a module"]').attr('data-toggle', 'modal');
$('a[title="Add a module"]').attr('data-target', '#module-modal-read-more');

Dropzone.options.importDropzone = {
url: 'import',
acceptedFiles: '.zip, .tar',
paramName: "module_file", // The name that will be used to transfer the file
maxFilesize: 5, // MB
uploadMultiple: false,
addRemoveLinks: true,
accept: function(file, done) {
console.log(file);
},
success: function (file, response) {
console.log(response);
}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div id="module-modal-read-more" class="modal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<h4>Add a new Module</h4>
<form action="/file-upload" class="dropzone" id="importDropzone">
</form>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="modules-list">
g<div id="modules-list">
<div class="row">
{% for module in modules %}
<div class="module-item-list col-md-12" data-id="{{ module.id }}" data-name="{{ module.displayName }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<script src="{{ asset_url|admin_asset_path }}"></script>
{% endjavascripts %}


{% endblock %}

{% block content %}
Expand Down

0 comments on commit 6502f97

Please sign in to comment.