Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added convert to multilingual from image_upload
This commit makes it possible, like the multilingual_field aleady has, to convert a image_upload to a multilingual one
- Loading branch information
Showing
2 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |