Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2: CSV Importer in cluster #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/components/com_cluster/administrator/access.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<access component="com_cluster">
<section name="component">
<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
<action name="core.edit.state" title="JACTION_EDITSTATE" description="JACTION_EDITSTATE_COMPONENT_DESC" />
<action name="core.edit.own" title="JACTION_EDITOWN" description="JACTION_EDITOWN_COMPONENT_DESC" />
</section>
<section name="component">
<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
<action name="core.edit.state" title="JACTION_EDITSTATE" description="JACTION_EDITSTATE_COMPONENT_DESC" />
<action name="core.edit.own" title="JACTION_EDITOWN" description="JACTION_EDITOWN_COMPONENT_DESC" />
<action name="core.import" title="JACTION_IMPORT" description="JACTION_IMPORT_COMPONENT_DESC" />
</section>
</access>
187 changes: 187 additions & 0 deletions src/components/com_cluster/administrator/assets/js/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
var clusterImport = {
validateImport: function (thisfile, loaderId) {
let obj = jQuery(thisfile);
obj.closest('.controls').children('.statusbar').remove();

let format_lesson_form = obj.closest('form');

/* Hide all alerts msgs */
let status = new clusterImport.createStatusbar(obj); //Using this we can set progress.

/* Get uploaded file object */
let uploadedfile = obj[0].files[0];

if (uploadedfile == undefined)
{
alert(Joomla.Text._('COM_CLUSTER_PLEASE_SELECT_FILE'));
return false;
}

/* Get uploaded file name */
let filename = uploadedfile.name;

/* pop out extension of file*/
let ext = filename.split('.').pop().toLowerCase();
let fileExt = filename.split('.').pop();

if (fileExt != 'csv')
{
let finalMsg = new Object();
finalMsg['errormsg'] = Joomla.Text._('COM_CLUSTER_CSV_FILE_UPLOAD_ERROR');
status.setMsg(finalMsg);
jQuery('.fileupload-preview').empty();
jQuery('#csv-upload').val('');

window.parent.SqueezeBox.close();
return false;
}

/* IF evrything is correct so far, popolate file name in fileupload-preview*/

let file_name_container = jQuery(".fileupload-preview", obj.closest('.fileupload-new'));

jQuery(file_name_container).show();
jQuery(file_name_container).text(filename);

clusterImport.startImporting(uploadedfile, status, thisfile, loaderId);
},
createStatusbar: function (obj) {
this.statusbar = jQuery("<div class='statusbar'></div>");
this.filename = jQuery("<div class='filename'></div>").appendTo(this.statusbar);
this.size = jQuery("<div class='filesize'></div>").appendTo(this.statusbar);
this.success = jQuery('<div class=""></div>').appendTo(this.statusbar);
this.error = jQuery('<div class=""></div>').appendTo(this.statusbar);

obj.closest('.controls').append(this.statusbar);

this.setFileNameSize = function(name, size)
{
var sizeStr = "";
var sizeKB = size/1024;
if(parseInt(sizeKB) > 1024)
{
var sizeMB = sizeKB/1024;
sizeStr = sizeMB.toFixed(2)+" MB";
}
else
{
sizeStr = sizeKB.toFixed(2)+" KB";
}

this.filename.html(name);
this.size.html(sizeStr);
}
this.setMsg = function(msg)
{
this.statusbar.show();

if(msg['errormsg'])
{
Joomla.renderMessages({"error":[msg.errormsg]});
window.parent.SqueezeBox.close();
}

if(msg['successmsg'])
{
Joomla.renderMessages({"success":[msg.successmsg]});
window.parent.SqueezeBox.close();
}

if(msg['messages'])
{
var message = jQuery('<div>').addClass('import-messages');
this.success.removeClass('msg alert');

jQuery.each(msg['messages'], function(i, value){
var key = Object.keys(value)[0];
var curMessage = jQuery('<div>').addClass('alert alert-' + key).html(value[key]).get(0);
message.append(curMessage);
});

this.success.html(message);
this.success.show();
}
}
},
startImporting: function (file, status, thisfile, loaderId) {
var finalMsg = new Object();
if(file == undefined)
{
finalMsg['errormsg'] = file_not_selected_error;
status.setMsg(finalMsg);
return false;
}

var filename = file.name;

if(window.FormData !== undefined) // for HTML5 browsers
{
var newfilename = clusterImport.sendFileToServer(file, status, thisfile, loaderId);
}
else
{
alert(Joomla.Text._('COM_CLUSTER_PLEASE_UPGRADE_YOUR_BROWSER'));
}
},

sendFileToServer: function (file, status, fileinputtag, loaderId) {
var formData = new FormData();
formData.append( 'FileInput', file );

var returnvar = true;
var jqXHR = jQuery.ajax({
xhr: function() {
var xhrobj = jQuery.ajaxSettings.xhr();
if (xhrobj.upload) {
xhrobj.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
}, false);
}
return xhrobj;
},
url:Joomla.getOptions('system.paths').base +'/index.php?option=com_cluster&task=import.csvImport&format=json',
type:'POST',
data:formData,
mimeType:"multipart/form-data",
contentType: false,
dataType:'json',
cache: false,
processData:false,
success: function(response)
{
var output = response['OUTPUT'];
var result = output['flag'];
var finalMsg = new Object();

/* File uploading on local is done*/
if (result == 0)
{
finalMsg['errormsg'] = output['msg'];
status.setMsg(finalMsg);
jQuery('.fileupload-preview').empty();
}
else
{
finalMsg['successmsg'] = output['msg'];
status.setMsg(finalMsg);
}

jQuery('#csv-upload').val('');
},
error: function(jqXHR, textStatus, errorThrown)
{
finalMsg['errormsg'] = jqXHR.responseText;
status.setMsg(finalMsg);
returnvar = false;
}
});

return returnvar;
status.setAbort(jqXHR);
}
}
23 changes: 23 additions & 0 deletions src/components/com_cluster/administrator/controllers/defines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @package Cluster
*
* @author Techjoomla <extensions@techjoomla.com>
* @copyright Copyright (C) 2009 - 2019 Techjoomla. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

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

define('COM_CLUSTER_GROUP_TITLE', "cluster_name");
define('COM_CLUSTER_GROUP_EMAIL', "email");
define('COM_CLUSTER_CLIENT', "client");
define('COM_CLUSTER_CLIENT_ID', "client_id");
define('COM_CLUSTER_GROUP_ID', "cluster_id");
define('COM_CLUSTER_CLUSTER_STATE', "state_cluster");
define('COM_CLUSTER_CLUSTERUSERS_STATE', "state_clusterusers");
define('COM_CLUSTER_CLUSTER_CREATED_BY', "created_by_cluster");
define('COM_CLUSTER_CLUSTERUSERS_CREATED_BY', "created_by_clusterusers");
define('COM_CLUSTER_CLUSTER_MODIFIED_BY', "modified_by_cluster");
define('COM_CLUSTER_CLUSTERUSERS_MODIFIED_BY', "modified_by_clusterusers");
Loading