Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Olde Hampsink committed May 12, 2014
2 parents bd1f011 + f2a8434 commit 5e51fec
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 99 deletions.
2 changes: 1 addition & 1 deletion ImportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getName()

function getVersion()
{
return '0.6.0';
return '0.6.1';
}

function getDeveloper()
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Map fields (2)

Changelog
=================
###0.6.1###
- Get pending task info in import overview
- Ability to choose wether you want to receive an e-mail or not

###0.6###
- Added an import history tab

Expand Down
24 changes: 15 additions & 9 deletions controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function actionUploadFile() {
// Get behavior
$behavior = craft()->request->getPost('importBehavior');

// Send e-mail?
$email = craft()->request->getPost('importEmail');

// Backup?
$backup = craft()->request->getPost('importBackup');

Expand All @@ -52,6 +55,7 @@ public function actionUploadFile() {
$import->section = $section;
$import->entrytype = $entrytype;
$import->behavior = $behavior;
$import->email = $email;
$import->backup = $backup;

// Validate model
Expand All @@ -63,12 +67,7 @@ public function actionUploadFile() {
// Send variables to template and display
$this->renderTemplate('import/_map', array(
'import' => $import,
'columns' => $columns,
'file' => craft()->path->getTempUploadsPath().$file->getName(),
'section' => $section,
'entrytype' => $entrytype,
'behavior' => $behavior,
'backup' => $backup
'columns' => $columns
));

} else {
Expand Down Expand Up @@ -96,6 +95,9 @@ public function actionImportFile() {
// Get file
$file = craft()->request->getParam('file');

// Email?
$email = craft()->request->getParam('email');

// Backup?
$backup = craft()->request->getParam('backup');

Expand All @@ -115,17 +117,21 @@ public function actionImportFile() {
'section' => $section,
'entrytype' => $entrytype,
'behavior' => $behavior,
'email' => $email,
'backup' => $backup
);

// Create history
$history = craft()->import_history->start((object)$settings);

// Create the import task
craft()->tasks->createTask('Import', Craft::t('Importing') . ' ' . basename($file), array_merge($settings, array('history' => $history)));
$task = craft()->tasks->createTask('Import', Craft::t('Importing') . ' ' . basename($file), array_merge($settings, array('history' => $history)));

// Notify user
craft()->userSession->setNotice(Craft::t('Import process started.'));

// Send variables to template and display
$this->renderTemplate('import/_progress');
// Redirect to index
$this->redirect('import?task=' . $task->id);

}

Expand Down
1 change: 1 addition & 0 deletions models/ImportModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function defineAttributes()
'behavior' => array(AttributeType::Enum, 'required' => true, 'values' => array(self::BehaviorAppend, self::BehaviorReplace, self::BehaviorDelete)),
'file' => array(AttributeType::String, 'required' => true, 'label' => Craft::t('File')),
'type' => array(AttributeType::Enum, 'required' => true, 'values' => array(self::TypeCSV, self::TypeCSVWin, self::TypeCSVIE, self::TypeCSVApp, self::TypeCSVExc, self::TypeCSVOff, self::TypeCSVOff2, self::TypeCSVOth)),
'email' => array(AttributeType::Bool, 'label' => Craft::t('Send e-mail notification')),
'backup' => array(AttributeType::Bool, 'label' => Craft::t('Backup Database'))
);
}
Expand Down
57 changes: 35 additions & 22 deletions resources/js/import.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
$(function() {

// Find entry types by chosen section
$('[name="importSection"]').change(function() {

$('#entrytypes').html('');
Craft.postActionRequest('import/getEntryTypes', { 'section': $(this).val() }, function(entrytypes) {

$.each(entrytypes, function(index, value) {
$('#entrytypes').append('<option value="' + value.id + '">' + value.name + '</option>');
if($('#sections').length) {

// Find entry types by chosen section
$('#sections').change(function() {

$('#entrytypes').html('');
Craft.postActionRequest('import/getEntryTypes', { 'section': $(this).val() }, function(entrytypes) {

$.each(entrytypes, function(index, value) {
$('#entrytypes').append('<option value="' + value.id + '">' + value.name + '</option>');
});

});

});

// Only show backup option when receiving email
$('#email').change(function() {
$('#backup').prop('disabled', !$(this).is(':checked'));
});

});
}

// Make sure each field gets mapped once
$('.mapper select').change(function() {
if($('.mapper select').length) {

// Disable chosen option for all mapper fields
$('.mapper select option:disabled').prop('disabled', false);
$('.mapper select option:selected').each(function() {

// Not import field can always get mapped
if($(this).val() != 'dont') {
$('.mapper select option[value="' + $(this).val() + '"]').not($(this)).prop('disabled', true);
}
// Make sure each field gets mapped once
$('.mapper select').change(function() {

// Disable chosen option for all mapper fields
$('.mapper select option:disabled').prop('disabled', false);
$('.mapper select option:selected').each(function() {

// Not import field can always get mapped
if($(this).val() != 'dont') {
$('.mapper select option[value="' + $(this).val() + '"]').not($(this)).prop('disabled', true);
}

});

});

});
// Trigger on load also
$('.mapper select').trigger('change');

// Trigger on load also
$('.mapper select').trigger('change');
}

});
76 changes: 40 additions & 36 deletions services/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,47 +120,51 @@ public function row($row, $data, $settings) {
public function finish($settings, $backup) {

craft()->import_history->end($settings->history);

// Gather results
$results = array(
'success' => $settings->rows,
'errors' => array()
);

// Gather errors
foreach($this->log as $line => $result) {
$results['errors'][$line] = $result;
}

// Recalculate successful results
$results['success'] -= count($results['errors']);
if($settings->email) {

// Prepare the mail
$email = new EmailModel();
$emailSettings = craft()->email->getSettings();
$email->toEmail = $emailSettings['emailAddress'];

// Zip the backup
if($settings->backup && IOHelper::fileExists($backup)) {
$destZip = craft()->path->getTempPath().IOHelper::getFileName($backup, false).'.zip';
if(IOHelper::fileExists($destZip)) {
IOHelper::deleteFile($destZip, true);
// Gather results
$results = array(
'success' => $settings->rows,
'errors' => array()
);

// Gather errors
foreach($this->log as $line => $result) {
$results['errors'][$line] = $result;
}
IOHelper::createFile($destZip);
if(Zip::add($destZip, $backup, craft()->path->getDbBackupPath())) {
$backup = $destZip;

// Recalculate successful results
$results['success'] -= count($results['errors']);

// Prepare the mail
$email = new EmailModel();
$emailSettings = craft()->email->getSettings();
$email->toEmail = $emailSettings['emailAddress'];

// Zip the backup
if($settings->backup && IOHelper::fileExists($backup)) {
$destZip = craft()->path->getTempPath().IOHelper::getFileName($backup, false).'.zip';
if(IOHelper::fileExists($destZip)) {
IOHelper::deleteFile($destZip, true);
}
IOHelper::createFile($destZip);
if(Zip::add($destZip, $backup, craft()->path->getDbBackupPath())) {
$backup = $destZip;
}
}

// Set email content
$email->subject = Craft::t('The import task is finished');
$email->htmlBody = TemplateHelper::getRaw(craft()->templates->render('import/_email', array(
'results' => $results,
'backup' => $backup
)));

// Send it
craft()->email->sendEmail($email);

}

// Set email content
$email->subject = Craft::t('The import task is finished');
$email->htmlBody = TemplateHelper::getRaw(craft()->templates->render('import/_email', array(
'results' => $results,
'backup' => $backup
)));

// Send it
craft()->email->sendEmail($email);

}

Expand Down
1 change: 1 addition & 0 deletions tasks/ImportTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected function defineSettings() {
'section' => AttributeType::Number,
'entrytype' => AttributeType::Number,
'behavior' => AttributeType::Name,
'email' => AttributeType::Email,
'backup' => AttributeType::Bool,
'history' => AttributeType::Number
);
Expand Down
15 changes: 8 additions & 7 deletions templates/_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
{% set content %}
<form id="import-form" method="post" action="" class="centered" accept-charset="UTF-8">
<input type="hidden" name="action" value="import/importFile">
<input type="hidden" name="section" value="{{ section }}">
<input type="hidden" name="entrytype" value="{{ entrytype }}">
<input type="hidden" name="behavior" value="{{ behavior }}">
<input type="hidden" name="file" value="{{ file }}">
<input type="hidden" name="backup" value="{{ backup }}">
<input type="hidden" name="section" value="{{ import.section }}">
<input type="hidden" name="entrytype" value="{{ import.entrytype }}">
<input type="hidden" name="behavior" value="{{ import.behavior }}">
<input type="hidden" name="file" value="{{ import.file }}">
<input type="hidden" name="email" value="{{ import.email }}">
<input type="hidden" name="backup" value="{{ import.backup }}">
<input type="hidden" name="unique[]" value="">

{% set entrytypes = craft.sections.getSectionById(section).getEntryTypes() %}
{% set entrytypes = craft.sections.getSectionById(import.section).getEntryTypes() %}

<p>{{ "Choose where you want to import your fields. * Please note that \"{title}\" is always required."|t({
title: entrytypes[0].titleLabel
Expand Down Expand Up @@ -72,7 +73,7 @@
</div>
</div>
</td>
{% if craft.request.getParam('importBehavior') != 'append' %}
{% if import.behavior != 'append' %}
<td>
{{ forms.checkboxField({
label: 'Criterium'|t,
Expand Down
19 changes: 0 additions & 19 deletions templates/_progress.html

This file was deleted.

19 changes: 18 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@

{% set selectedTab = 'overview' %}

{% if craft.request.getParam('task') %}

{% set javascript %}
setTimeout(function() {
var taskCheck = setInterval(function() {
if((Craft.cp.runningTaskInfo && Craft.cp.runningTaskInfo.id != {{ craft.request.getParam('task') }}) || Craft.cp.runningTaskInfo === null) {
clearInterval(taskCheck);
window.location.href = 'import';
}
}, 500);
}, 500);
{% endset %}

{% includeJs javascript %}

{% endif %}

{% set content %}

<table class="data fullwidth">
<table class="data fullwidth" id="history">
<thead>
<tr>
<th scope="col" data-attribute="date">{{ "Filename"|t }}</th>
Expand Down
24 changes: 22 additions & 2 deletions templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<div class="field">
<div class="input">
<div class="select">
<select name="importSection">
<select name="importSection" id="sections">
{% for section in sections %}
<option value="{{ section.id }}">{{ section.name }}</option>
{% endfor %}
Expand Down Expand Up @@ -115,6 +115,26 @@
</div>
</td>
</tr>
<tr>
<td>
<div class="field">
<div class="heading">
<label>{{ "Send e-mail notification"|t }}</label>
<div class="instructions">
<p>{{ "Send an e-mail summary when the import process is completed."|t }}</p>
</div>
</div>
</div>
</td>
<td>
<div class="field">
<div class="input">
<input type="hidden" name="importEmail" value="0">
<input type="checkbox" name="importEmail" value="1" id="email" />
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="field">
Expand All @@ -130,7 +150,7 @@
<div class="field">
<div class="input">
<input type="hidden" name="importBackup" value="0">
<input type="checkbox" name="importBackup" value="1" />
<input type="checkbox" name="importBackup" value="1" id="backup" disabled />
</div>
</div>
</td>
Expand Down
Loading

0 comments on commit 5e51fec

Please sign in to comment.