diff --git a/extension.driver.php b/extension.driver.php index 3eae1e3..84240f0 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -3,14 +3,14 @@ Copyight: Deux Huit Huit 2015 LICENCE: MIT http://deuxhuithuit.mit-license.org; */ - + if(!defined("__IN_SYMPHONY__")) die("

Error

You cannot directly access this file

"); - + if (!class_exists('extension_selectbox_link_field')) { define('__SBLF_NOT_INSTALLED', 1); class extension_selectbox_link_field extends Extension {} } - + /** * * @author Deux Huit Huit @@ -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 ******* */ /** @@ -34,7 +34,7 @@ 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; } @@ -42,15 +42,15 @@ public function install() { // 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; } @@ -69,4 +69,4 @@ public function uninstall() { return true; } - } \ No newline at end of file + } diff --git a/extension.meta.xml b/extension.meta.xml index 49342f6..2f88364 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -19,11 +19,14 @@ - languages selectbox_link_field frontend_localisation + + - Update for Symphony 4.x + - PHP 7 Compatibility + - Added basic sorting capabilities (SBL 1.34+ required) @@ -34,4 +37,4 @@ - First release - \ No newline at end of file + diff --git a/fields/field.multilingual_selectbox_link.php b/fields/field.multilingual_selectbox_link.php index 5787765..c7c1ca0 100644 --- a/fields/field.multilingual_selectbox_link.php +++ b/fields/field.multilingual_selectbox_link.php @@ -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 @@ -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'); @@ -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. @@ -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) { @@ -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); @@ -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])) { @@ -170,7 +184,7 @@ public function prepareTextValue($data, $entry_id = null) { } $label .= ', '; } - + return trim($label, ', '); } - } \ No newline at end of file + }