Skip to content

Commit

Permalink
Added convert to multilingual from image_upload
Browse files Browse the repository at this point in the history
This commit makes it possible, like the multilingual_field aleady has,
to convert a image_upload to a multilingual one
  • Loading branch information
nitriques committed Aug 16, 2016
1 parent 933c6a8 commit 7f9f03f
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
89 changes: 89 additions & 0 deletions assets/multilingual_image_upload.settings.js
@@ -0,0 +1,89 @@
(function ($, undefined) {

'use strict';

if (!!Symphony.Extensions.MultilingualImageUpload) {
return;
}
Symphony.Extensions.MultilingualImageUpload = true;

// from backend.views.js
var change = function (e) {
var selectbox = $(this);
var parent = selectbox.parents('.instance');
var headline = parent.find('.frame-header h4');
var values = selectbox.find(':selected');
var span = headline.find('.required');

if(!!values.length) {
var langs = [];
values.each(function (index, elem) {
var text = $(this).text();
langs.push(text.split(' ||')[0]);
if (index < values.length - 2) {
langs.push(', ');
} else if (index < values.length - 1) {
langs.push(' and ');
}
});

if (!span.length) {
span = $('<span />', {
class: 'required'
}).appendTo(headline);
}

span.text(
'— ' + langs.join('') + ' ' +
Symphony.Language.get(langs.length > 1 ? 'are' : 'is') + ' ' +
Symphony.Language.get('required')
);
}

// Is not required
else {
headline.find('.required').remove();
}
};

$(function () {
$('.field-multilingual_image_upload.instance select[name*="[required_languages]"]')
.on('change', change)
.trigger('change');

if (!!Symphony.Context.get().version) {
Symphony.Elements.contents.find('.instance.field-image_upload').each(function () {
var $field = $(this);
var $header = $field.find('.frame-header');
$header.append(
$('<a />').attr('class', 'field-multilingual_image_upload-converter debug')
.attr('style', 'right: 11rem;font-size: 0.9em;')
.text(Symphony.Language.get('Convert to multilingual'))
);
}).on('click', '.field-multilingual_image_upload-converter', function (e) {
var $field = $(this).closest('.field-image_upload');
var id = $field.find('input[name$=\\[id\\]]').val();

e.stopPropagation();
if (!confirm(Symphony.Language.get('Are you sure?'))) {
return false;
}

$.post(Symphony.Context.get('path') + '/extension/multilingual_image_upload/convert/' + id + '/')
.done(function (data) {
if (data && data.ok) {
window.location.reload();
}
else if (data.error) {
alert(data.error);
}
else {
alert('Unknown error.');
}
});
return;
});
}
});

})(jQuery);
114 changes: 114 additions & 0 deletions content/content.convert.php
@@ -0,0 +1,114 @@
<?php
/*
Copyright: Deux Huit Huit 2016
LICENCE: MIT http://deuxhuithuit.mit-license.org;
*/

if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");

require_once(TOOLKIT . '/class.jsonpage.php');
require_once(EXTENSIONS . '/multilingual_image_upload/fields/field.multilingual_image_upload.php');

class contentExtensionMultilingual_Image_UploadConvert extends JSONPage {

/**
*
* Builds the content view
*/
public function view() {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->_Result['status'] = Page::HTTP_STATUS_BAD_REQUEST;
$this->_Result['error'] = 'This page accepts posts only';
$this->setHttpStatus($this->_Result['status']);
return;
}

if (!is_array($this->_context) || empty($this->_context)) {
$this->_Result['error'] = 'Parameters not found';
return;
}

$id = MySQL::cleanValue($this->_context[0]);
$this->_Result['ok'] = true;

$field = FieldManager::fetch($id);

if ($field == null || !($field instanceof fieldImage_upload)) {
$this->_Result['error'] = "Field $id not found.";
$this->_Result['ok'] = false;
return;
}

try {
// Check for languages
$langs = FLang::getLangs();
if (empty($langs)) {
throw new Exception('No language found. Please check that you have at least one.');
}

$destination = MySQL::cleanValue($field->get('destination'));
$validator = MySQL::cleanValue($field->get('validator'));
$unique = MySQL::cleanValue($field->get('unique'));
$min_width = MySQL::cleanValue($field->get('min_width'));
$min_height = MySQL::cleanValue($field->get('min_height'));
$max_width = MySQL::cleanValue($field->get('max_width'));
$max_height = MySQL::cleanValue($field->get('max_height'));
$resize = MySQL::cleanValue($field->get('resize'));
$requiredLang = $field->get('required') == 'yes' ? "'main'" : 'NULL';

// ALTER data table SQL: add new cols
$entries_table = "tbl_entries_data_$id";
$query = "ALTER TABLE `$entries_table` ";
$cols = fieldMultilingual_image_upload::generateTableColumns();
foreach ($cols as $col) {
$query .= ' ADD COLUMN ' . $col;
}
$keys = fieldMultilingual_image_upload::generateTableKeys();
foreach ($keys as $key) {
$query .= ' ADD ' . $key;
}
$query = trim($query, ',') . ';';
Symphony::Database()->query($query);

// Copy values to default lang
$defLang = FLang::getMainLang();
$query = "UPDATE `$entries_table` SET ";
$query .= " `file-$defLang` = `file`,
`size-$defLang` = `size`,
`mimetype-$defLang` = `mimetype`,
`meta-$defLang` = `meta`;";

Symphony::Database()->query($query);

// Insert into multilingual
Symphony::Database()->query("
INSERT INTO `tbl_fields_multilingual_image_upload`
(`field_id`, `destination`, `validator`, `unique`, `default_main_lang`,
`min_width`, `min_height`,
`max_width`, `max_height`,
`resize`, `required_languages`)
VALUES
($id, '$destination', '$validator', '$unique', 'yes',
'$min_width', '$min_height',
'$max_width', '$max_height',
'$resize', $requiredLang)
");

// remove from textbox
Symphony::Database()->query("
DELETE FROM `tbl_fields_textbox`
WHERE `field_id` = $id
");

// update type
Symphony::Database()->query("
UPDATE `tbl_fields` SET `type` = 'multilingual_image_upload'
WHERE `id` = $id
");

} catch (Exception $ex) {
$this->_Result['ok'] = false;
$this->_Result['error'] = $ex->getMessage();
}
}
}

0 comments on commit 7f9f03f

Please sign in to comment.