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

Update for Symphony 4.x #38

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
109 changes: 66 additions & 43 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

define_safe(IMAGE_UPLOAD_NAME, 'Image Upload');
define_safe(IMAGE_UPLOAD_GROUP, 'image_upload');
define_safe('IMAGE_UPLOAD_NAME', 'Image Upload');
define_safe('IMAGE_UPLOAD_GROUP', 'image_upload');

class extension_image_upload extends Extension
{
Expand All @@ -13,22 +13,34 @@ class extension_image_upload extends Extension

public function install()
{
return Symphony::Database()->query(
"CREATE TABLE `tbl_fields_image_upload` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`destination` varchar(255) NOT NULL,
`validator` varchar(50),
`unique` varchar(50),
`min_width` int(11) unsigned,
`min_height` int(11) unsigned,
`max_width` int(11) unsigned,
`max_height` int(11) unsigned,
`resize` enum('yes','no') NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
return Symphony::Database()
->create('tbl_fields_image_upload')
->ifNotExists()
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'field_id' => 'int(11)',
'destination' => 'varchar(255)',
'validator' => 'varchar(50)',
'unique' => 'varchar(50)',
'min_width' => 'int(11)',
'min_height' => 'int(11)',
'max_width' => 'int(11)',
'max_height' => 'int(11)',
'resize' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'yes'
],
])
->keys([
'id' => 'primary',
'field_id' => 'key',
])
->execute()
->success();
}

public function update($previousVersion = false) {
Expand All @@ -37,41 +49,49 @@ public function update($previousVersion = false) {

// Before 1.1
if ($ret && version_compare($previousVersion, '1.1', '<')) {
$query = "ALTER TABLE `tbl_fields_image_upload`
ADD `max_width` int(11) unsigned,
ADD `max_height` int(11) unsigned,
DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";

try {
Symphony::Database()->query($query);
}
catch (Exception $e) {
}
Symphony::Database()
->alter('tbl_fields_image_upload')
->add([
'max_width' => 'int(11)',
'max_height' => 'int(11)',
])
->execute()
->success();
}

// Before 1.3
if ($ret && version_compare($previousVersion, '1.3', '<')) {
$query = "ALTER TABLE `tbl_fields_image_upload`
ADD COLUMN `resize` enum('yes','no') NOT NULL DEFAULT 'yes'";
try {
$ret = Symphony::Database()->query($query);
}
catch (Exception $e) {
// ignore ?
}
Symphony::Database()
->alter('tbl_fields_image_upload')
->add([
'resize' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'yes',
],
])
->execute()
->success();
}

// Before 1.4
if ($ret && version_compare($previousVersion, '1.4', '<')) {
// Remove directory from the upload fields, #1719
$upload_tables = Symphony::Database()->fetchCol("field_id", "SELECT `field_id` FROM `tbl_fields_image_upload`");
$upload_tables = Symphony::Database()
->select(['field_id'])
->from('tbl_fields_image_upload')
->execute()
->column('field_id');

if (is_array($upload_tables) && !empty($upload_tables)) {
foreach($upload_tables as $field) {
Symphony::Database()->query(sprintf(
"UPDATE tbl_entries_data_%d SET file = substring_index(file, '/', -1)",
$field
));
Symphony::Database()
->update("tbl_entries_data_$field")
->set([
'file' => "substring_index(file, '/', -1)",
])
->execute()
->success();
}
}
}
Expand All @@ -80,7 +100,10 @@ public function update($previousVersion = false) {
}

public function uninstall() {
return Symphony::Database()->query("DROP TABLE `tbl_fields_image_upload`");
return Symphony::Database()
->drop('tbl_fields_image_upload')
->ifExists()
->execute()
->success();
}

}
7 changes: 4 additions & 3 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
<website>https://deuxhuithuit.com/</website>
</author>
</authors>
<dependencies>
<dependency version="1.15">https://github.com/symphonycms/jit_image_manipulation</dependency>
</dependencies>
<releases>
<release version="2.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x"><![CDATA[
* Update for Symphony 4.x
* Code refactoring for Database and EQFA
]]></release>
<release version="1.6.6" date="2016-08-16" min="2.4.x" max="2.x.x"><![CDATA[
* Fixed parameters to match the parent method
* Removed the deprecated /e preg_match call
Expand Down
39 changes: 20 additions & 19 deletions fields/field.image_upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class fieldImage_upload extends fieldUpload
protected static $svgMimeTypes = array(
'image/svg+xml',
'image/svg',
'text/plain',
'application/octet-stream',
);

/*------------------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -37,10 +39,10 @@ public function __construct()
*/
public static function resize($file, $width, $height, $mimetype)
{
$jit_status = ExtensionManager::fetchStatus(array('handle' => 'jit_image_manipulation'));
$jit_status = Symphony::ExtensionManager()->fetchStatus(array('handle' => 'jit_image_manipulation'));

// process image using JIT mode 1
if ($jit_status[0] === EXTENSION_ENABLED) {
if ($jit_status[0] === Extension::EXTENSION_ENABLED) {
require_once(EXTENSIONS.'/jit_image_manipulation/lib/class.image.php');

try {
Expand All @@ -61,17 +63,17 @@ public static function resize($file, $width, $height, $mimetype)

return true;
}

protected static function removePx($value)
{
return str_replace('px', '', $value);
}

protected static function isSvg($type)
{
return General::in_iarray($type, self::$svgMimeTypes);
}

/**
* Adds support for svg
*/
Expand Down Expand Up @@ -287,10 +289,10 @@ public function commit()
$settings['destination'] = $this->get('destination');
$settings['validator'] = ($settings['validator'] == 'custom' ? null : $this->get('validator'));
$settings['unique'] = $this->get('unique');
$settings['min_width'] = $this->get('min_width');
$settings['min_height'] = $this->get('min_height');
$settings['max_width'] = $this->get('max_width');
$settings['max_height'] = $this->get('max_height');
$settings['min_width'] = (int)$this->get('min_width');
$settings['min_height'] = (int)$this->get('min_height');
$settings['max_width'] = (int)$this->get('max_width');
$settings['max_height'] = (int)$this->get('max_height');
$settings['resize'] = $this->get('resize') == 'yes' ? 'yes' : 'no';

return FieldManager::saveSettings($id, $settings);
Expand Down Expand Up @@ -466,13 +468,9 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWit
if ($label != null) {
// try to find the i element
$i = $this->getChildrenWithClass($wrapper, null, 'i');
if ($i == null) {
// create one and prepend it if nothing found
$i = new XMLElement('i');
$label->prependChild($i);
if ($i != null) {
$i->setValue(' '.$this->generateHelpMessage());
}

$i->setValue(' '.$this->generateHelpMessage());
}
}

Expand All @@ -497,7 +495,7 @@ protected function generateHelpMessage()
$sizes[__('Max width')] = $this->get('max_width');
$sizes[__('Max height')] = $this->get('max_height');
}

foreach($sizes as $key => $size) {
if (!empty($size) && $size != 0) {
$sizeMessage .= $key.': '.$size.'px, ';
Expand Down Expand Up @@ -532,20 +530,23 @@ public function prepareTableValue($data, XMLElement $link = null, $entry_id = nu

$destination = str_replace('/workspace', '', $this->get('destination')) . '/';

$extman = Symphony::ExtensionManager();
$status = $extman->fetchStatus(array('handle' => 'jit_image_manipulation'));

$src = '';
if (isset($data['mimetype']) && self::isSvg($data['mimetype'])) {
if ((isset($data['mimetype']) && self::isSvg($data['mimetype'])) || !in_array(Extension::EXTENSION_ENABLED, $status)) {
$src = URL . '/workspace' . $destination . $file;
}
else {
$src = URL . '/image/1/' . $width . '/' . $height . $destination . $file;
}
$image = '<img style="vertical-align: middle; max-height:40px;" src="' . $src . '" alt="'.$this->get('label').' of Entry '.$entry_id.'"/>';
$image = '<img src="' . $src . '" alt="'.$this->get('label').' of Entry '.$entry_id.'"/>';

if ($link) {
$link->setValue($image);
}
else{
$link = Widget::Anchor($image, URL.$this->get('destination').'/'.$file);
$link = Widget::Anchor($image, URL.$this->get('destination').'/'.$file, null, null, null, array('target' => '_blank'));
}
$link->setAttribute('data-path', $this->get('destination'));
return $link->generate();
Expand Down