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

PHP7 Compatibility release #1

Open
wants to merge 9 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
18 changes: 9 additions & 9 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Copyight: Deux Huit Huit 2015
LICENCE: MIT http://deuxhuithuit.mit-license.org;
*/

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

if (!class_exists('extension_selectbox_link_field')) {
define('__SBLF_NOT_INSTALLED', 1);
class extension_selectbox_link_field extends Extension {}
}

/**
*
* @author Deux Huit Huit
Expand All @@ -24,7 +24,7 @@ class extension_multilingual_selectbox_link_field extends extension_selectbox_li
* @var string
*/
const EXT_NAME = 'Field: Multilingual Selectbox Link';

/* ********* INSTALL/UPDATE/UNINSTALL ******* */

/**
Expand All @@ -34,23 +34,23 @@ public function install() {
if (__SBLF_NOT_INSTALLED) {
$sbl_status = ExtensionManager::fetchStatus(array('handle' => 'selectbox_link_field'));
$sbl_status = current($sbl_status);
if ($sbl_status != EXTENSION_ENABLED) {
if ($sbl_status != Extension::EXTENSION_ENABLED) {
Administration::instance()->Page->pageAlert('Could not load `selectbox_link_field` extension.', Alert::ERROR);
return false;
}
}
// depends on "Languages"
$languages_status = ExtensionManager::fetchStatus(array('handle' => 'languages'));
$languages_status = current($languages_status);
if ($languages_status != EXTENSION_ENABLED) {
if ($languages_status != Extension::EXTENSION_ENABLED) {
Administration::instance()->Page->pageAlert('Could not load `languages` extension.', Alert::ERROR);
return false;
}

// create table "alias"
Administration::instance()->Database()->query("CREATE OR REPLACE VIEW `tbl_fields_multilingual_selectbox_link` AS
SELECT * FROM `tbl_fields_selectbox_link`;");

return true;
}

Expand All @@ -69,4 +69,4 @@ public function uninstall() {
return true;
}

}
}
7 changes: 5 additions & 2 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
</author>
</authors>
<dependencies>
<dependency version="1.2">languages</dependency>
<dependency version="1.32">selectbox_link_field</dependency>
<dependency version="2.5">frontend_localisation</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">
- Update for Symphony 4.x
- PHP 7 Compatibility
</release>
<release version="1.1.0" date="2016-02-01" min="2.5.0" max="2.6.x">
- Added basic sorting capabilities (SBL 1.34+ required)
</release>
Expand All @@ -34,4 +37,4 @@
- First release
</release>
</releases>
</extension>
</extension>
104 changes: 59 additions & 45 deletions fields/field.multilingual_selectbox_link.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require_once(TOOLKIT . '/class.field.php');
require_once(EXTENSIONS . '/selectbox_link_field/fields/field.selectbox_link.php');
require_once(EXTENSIONS . '/frontend_localisation/lib/class.FLang.php');

/**
*
* Field class that will represent relationships between entries
Expand All @@ -22,26 +22,28 @@ public function __construct(){
parent::__construct();
$this->_name = __('Multilingual Select Box Link');
}

protected function getFieldSchema($fieldId) {
$lc = FLang::getLangCode();

if (empty($lc)) {
$lc = FLang::getMainLang();
}

try {
return Symphony::Database()->fetch("
SHOW COLUMNS FROM `tbl_entries_data_$fieldId`
WHERE `Field` in ('value-$lc');
");
return Symphony::Database()
->showColumns()
->from('tbl_entries_data_' . $fieldId)
->where(['Field' => ['in' => ['value-' . $lc]]])
->execute()
->rows();
}
catch (Exception $ex) {
// bail out
}
return parent::getFieldSchema($fieldId);
}

public function fetchIDfromValue($value) {
$id = null;
$related_field_ids = $this->get('related_field_id');
Expand All @@ -51,22 +53,23 @@ public function fetchIDfromValue($value) {
if (empty($lc)) {
$lc = FLang::getMainLang();
}

$value = Lang::createHandle($value);

$try_parent = false;

foreach($related_field_ids as $related_field_id) {
try {
$return = Symphony::Database()->fetchCol("id", sprintf("
SELECT
`entry_id` as `id`
FROM
`tbl_entries_data_%d`
WHERE
`handle` = '%s' OR `handle-{$lc}` = '%s'
LIMIT 1", $related_field_id, $value, $value
));
$return = Symphony::Database()
->select(['entry_id' => 'id'])
->from('tbl_entries_data_' . $related_field_id)
->where(['or' => [
['handle' => $value],
['handle-' . $lc => $value],
]])
->limit(1)
->execute()
->column('id');

// Skipping returns wrong results when doing an
// AND operation, return 0 instead.
Expand All @@ -80,57 +83,68 @@ public function fetchIDfromValue($value) {
$try_parent = true;
}
}

if ($try_parent) {
return parent::fetchIDfromValue($value);
}

return (is_null($id)) ? 0 : (int)$id;
}
public function fetchAssociatedEntrySearchValue($data, $field_id=NULL, $parent_entry_id=NULL){

public function fetchAssociatedEntrySearchValue($data, $field_id = null, $parent_entry_id = null){
// We dont care about $data, but instead $parent_entry_id
if(!is_null($parent_entry_id)) return $parent_entry_id;

if(!is_array($data)) return $data;
$handle = addslashes($data['handle']);

$handle = $data['handle'];

$try_parent = false;
$searchvalue = array();

$searchvalue = null;

try {
$searchvalue = Symphony::Database()->fetchRow(0, sprintf("
SELECT `entry_id` FROM `tbl_entries_data_%d`
WHERE `handle` = '%s' OR `handle-{$lc}` = '%s'
LIMIT 1",
$field_id, $handle, $handle
));
$searchvalue = Symphony::Database()
->select(['entry_id'])
->from('tbl_entries_data_' . $field_id)
->where(['or' => [
['handle' => $handle],
['handle-' . $lc => $handle],
]])
->limit(1)
->execute()
->variable('entry_id');
} catch (Exception $ex) {
// Try the parent since this would normally be the case when a handle
// column doesn't exist!
$try_parent = true;
}

if ($try_parent) {
return parent::fetchAssociatedEntrySearchValue($data, $field_id, $parent_entry_id);
}

return $searchvalue['entry_id'];
return $searchvalue;
}

private static function startsWith($haystack, $needle) {
// search backwards starting from haystack length characters from the end
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
}

protected function findRelatedValues(array $relation_id = array()) {
$relation_data = parent::findRelatedValues($relation_id);
if (is_array($relation_data)) {
foreach ($relation_data as $r => $relation) {
$e = EntryManager::fetch($relation['id']);
$ed = $e[0]->getData();
$section_id = SectionManager::fetchIDFromHandle($relation['section_handle']);
$e = (new EntryManager)
->select()
->entry($relation['id'])
->section($section_id)
->includeAllFields()
->execute()
->next();
$ed = $e->getData();
foreach ($this->get('related_field_id') as $fieldId) {
if (is_array($ed[$fieldId])) {
foreach ($ed[$fieldId] as $key => $value) {
Expand All @@ -144,7 +158,7 @@ protected function findRelatedValues(array $relation_id = array()) {
}
return $relation_data;
}

public function prepareTextValue($data, $entry_id = null) {
if(!is_array($data) || (is_array($data) && !isset($data['relation_id']))) {
return parent::prepareTextValue($data, $entry_id);
Expand All @@ -160,7 +174,7 @@ public function prepareTextValue($data, $entry_id = null) {
if (empty($lc)) {
$lc = FLang::getMainLang();
}

$label = '';
foreach($result as $item){
if (isset($item['value-' . $lc])) {
Expand All @@ -170,7 +184,7 @@ public function prepareTextValue($data, $entry_id = null) {
}
$label .= ', ';
}

return trim($label, ', ');
}
}
}