From 0690467f307a4017f7e0c93d3a4f240f11ce489a Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Wed, 17 May 2017 13:49:10 -0400 Subject: [PATCH 01/19] Fix compatibility warnings for PHP7 --- fields/field.selectbox_link.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index e22d90b..085f59a 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -966,7 +966,7 @@ private function getRelatedFieldsId() { return explode(',', $related_field_id); } - public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){ + public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC', &$select = NULL){ if(in_array(strtolower($order), array('random', 'rand'))) { $sort = 'ORDER BY RAND()'; } From 417de76ed6707834173c9c4a8b72d6d8453e2ebd Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Tue, 23 May 2017 11:02:51 -0400 Subject: [PATCH 02/19] PHP7 Compatibility release --- extension.meta.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extension.meta.xml b/extension.meta.xml index c6c67f8..910e712 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -15,6 +15,10 @@ + + - Update for Symphony 4.x + - PHP7 Compatibility + - [#87](https://github.com/symphonycms/selectbox_link_field/issues/87) Add missing canPrePopulate() function From 87941b27c70b1b6de94418e782ebb6fbd0612c12 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Tue, 30 May 2017 14:28:22 -0400 Subject: [PATCH 03/19] Remove deprecated param from buildSortingSQL --- fields/field.selectbox_link.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 085f59a..0ba4e6a 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -966,7 +966,7 @@ private function getRelatedFieldsId() { return explode(',', $related_field_id); } - public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC', &$select = NULL){ + public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){ if(in_array(strtolower($order), array('random', 'rand'))) { $sort = 'ORDER BY RAND()'; } @@ -1026,6 +1026,11 @@ public function buildSortingSelectSQL($sort, $order = 'ASC') return null; } + public function buildSortingSelectSQL($sort, $order = 'ASC') + { + return null; + } + /*------------------------------------------------------------------------- Grouping: -------------------------------------------------------------------------*/ From 723a3fe587e6ee0b4ab0d5e3443a1730c4dca18f Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Thu, 24 Aug 2017 16:07:50 -0400 Subject: [PATCH 04/19] SQL and PHP cases PHP true,false,null in lowercase SQL keywords uppercase --- extension.driver.php | 12 ++++++------ fields/field.selectbox_link.php | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/extension.driver.php b/extension.driver.php index 9c952d7..e512a9c 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -6,12 +6,12 @@ public function install(){ try{ Symphony::Database()->query(" CREATE TABLE IF NOT EXISTS `tbl_fields_selectbox_link` ( - `id` int(11) unsigned NOT NULL auto_increment, - `field_id` int(11) unsigned NOT NULL, - `allow_multiple_selection` enum('yes','no') NOT NULL default 'no', - `hide_when_prepopulated` enum('yes','no') NOT NULL default 'no', + `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + `field_id` INT(11) UNSIGNED NOT NULL, + `allow_multiple_selection` ENUM('yes','no') NOT NULL default 'no', + `hide_when_prepopulated` ENUM('yes','no') NOT NULL default 'no', `related_field_id` VARCHAR(255) NOT NULL, - `limit` int(4) unsigned NOT NULL default '20', + `limit` INT(4) UNSIGNED NOT NULL default '20', PRIMARY KEY (`id`), KEY `field_id` (`field_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; @@ -90,7 +90,7 @@ public function update($previousVersion = false){ if(version_compare($previousVersion, '1.19', '<')){ try{ - Symphony::Database()->query("ALTER TABLE `tbl_fields_selectbox_link` ADD COLUMN `show_association` enum('yes','no') NOT NULL default 'yes'"); + Symphony::Database()->query("ALTER TABLE `tbl_fields_selectbox_link` ADD COLUMN `show_association` ENUM('yes','no') NOT NULL default 'yes'"); } catch(Exception $e){ // Discard diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 0ba4e6a..b052f85 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -74,9 +74,9 @@ public function fetchSuggestionTypes() public function createTable(){ return Symphony::Database()->query( "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` ( - `id` int(11) unsigned NOT NULL auto_increment, - `entry_id` int(11) unsigned NOT NULL, - `relation_id` int(11) unsigned DEFAULT NULL, + `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, + `entry_id` INT(11) UNSIGNED NOT NULL, + `relation_id` INT(11) UNSIGNED DEFAULT NULL, PRIMARY KEY (`id`), KEY `entry_id` (`entry_id`), KEY `relation_id` (`relation_id`) @@ -95,7 +95,7 @@ public function set($field, $value){ $this->_settings[$field] = $value; } - public function findOptions(array $existing_selection=NULL,$entry_id=NULL){ + public function findOptions(array $existing_selection = null, $entry_id = null){ $values = array(); $limit = $this->get('limit'); @@ -120,7 +120,7 @@ public function findOptions(array $existing_selection=NULL,$entry_id=NULL){ ); EntryManager::setFetchSorting($section->getSortingField(), $section->getSortingOrder()); - $entries = EntryManager::fetch(NULL, $section->get('id'), $limit, 0, null, null, false, false); + $entries = EntryManager::fetch(null, $section->get('id'), $limit, 0, null, null, false, false); $results = array(); foreach($entries as $entry) { @@ -161,7 +161,7 @@ public function getToggleStates(){ return $output; } - public function toggleFieldData(array $data, $newState, $entry_id=null){ + public function toggleFieldData(array $data, $newState, $entry_id = null){ $data['relation_id'] = $newState; return $data; } @@ -186,7 +186,7 @@ public function fetchAssociatedEntryIDs($value){ )); } - 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; @@ -446,7 +446,7 @@ public function displaySettingsPanel(XMLElement &$wrapper, $errors = null){ $wrapper->appendChild($label); // Options - $div = new XMLElement('div', NULL, array('class' => 'two columns')); + $div = new XMLElement('div', null, array('class' => 'two columns')); $wrapper->appendChild($div); // Allow selection of multiple items @@ -481,7 +481,7 @@ public function displaySettingsPanel(XMLElement &$wrapper, $errors = null){ } public function checkPostFieldData($data, &$message, $entry_id = null){ - $message = NULL; + $message = null; if (is_array($data)) { $data = isset($data['relation_id']) @@ -528,7 +528,7 @@ public function commit(){ SectionManager::removeSectionAssociation($id); foreach($this->get('related_field_id') as $field_id){ - SectionManager::createSectionAssociation(NULL, $id, $field_id, $this->get('show_association') == 'yes' ? true : false); + SectionManager::createSectionAssociation(null, $id, $field_id, $this->get('show_association') == 'yes' ? true : false); } return true; @@ -541,7 +541,7 @@ public function commit(){ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null) { $entry_ids = array(); $options = array( - array(NULL, false, NULL) + array(null, false, null) ); if(!is_null($data['relation_id'])){ @@ -582,7 +582,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWit if($this->get('required') != 'yes') $label->appendChild(new XMLElement('i', __('Optional'))); $label->appendChild( Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') == 'yes' ? array( - 'multiple' => 'multiple') : NULL + 'multiple' => 'multiple') : null )) ); @@ -592,7 +592,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWit else $wrapper->appendChild($label); } - public function processRawFieldData($data, &$status, &$message=null, $simulate=false, $entry_id=null) { + public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) { $status = self::__OK__; $result = array(); @@ -640,7 +640,7 @@ public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = fa $wrapper->appendChild($list); } - public function getParameterPoolValue(array $data, $entry_id=NULL){ + public function getParameterPoolValue(array $data, $entry_id = null){ return $this->prepareExportValue($data, ExportableField::LIST_OF + ExportableField::ENTRY, $entry_id); } @@ -854,7 +854,7 @@ public function fetchFilterableOperators() ); } - public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation=false){ + public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = false){ $field_id = $this->get('id'); if(preg_match('/^sql:\s*/', $data[0], $matches)) { @@ -966,7 +966,7 @@ private function getRelatedFieldsId() { return explode(',', $related_field_id); } - public function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){ + public function buildSortingSQL(&$joins, &$where, &$sort, $order = 'ASC'){ if(in_array(strtolower($order), array('random', 'rand'))) { $sort = 'ORDER BY RAND()'; } From 8622372966dd8a7a240fa125659022d208f69eb4 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Fri, 25 Aug 2017 11:27:50 -0400 Subject: [PATCH 05/19] Relase infos .. Again --- extension.meta.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.meta.xml b/extension.meta.xml index 910e712..6486ff1 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -15,7 +15,7 @@ - + - Update for Symphony 4.x - PHP7 Compatibility From bab5017fdb135ce8f82f5ced812a6ae62646d4b9 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Fri, 25 Aug 2017 13:17:32 -0400 Subject: [PATCH 06/19] Correct rebase error Remove temporary buildSortingSelectSQL function. --- fields/field.selectbox_link.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index b052f85..0ac5d3b 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -1026,11 +1026,6 @@ public function buildSortingSelectSQL($sort, $order = 'ASC') return null; } - public function buildSortingSelectSQL($sort, $order = 'ASC') - { - return null; - } - /*------------------------------------------------------------------------- Grouping: -------------------------------------------------------------------------*/ From 8c558d4c525249536306e71c89a5de2573c3cecf Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Wed, 30 May 2018 14:43:53 -0400 Subject: [PATCH 07/19] Code refactoring for Database --- extension.driver.php | 113 +++++++++++------ extension.meta.xml | 3 +- fields/field.selectbox_link.php | 207 +++++++++++++++++--------------- 3 files changed, 183 insertions(+), 140 deletions(-) diff --git a/extension.driver.php b/extension.driver.php index e512a9c..e19c8f0 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -3,42 +3,63 @@ Class extension_selectbox_link_field extends Extension{ public function install(){ - try{ - Symphony::Database()->query(" - CREATE TABLE IF NOT EXISTS `tbl_fields_selectbox_link` ( - `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - `field_id` INT(11) UNSIGNED NOT NULL, - `allow_multiple_selection` ENUM('yes','no') NOT NULL default 'no', - `hide_when_prepopulated` ENUM('yes','no') NOT NULL default 'no', - `related_field_id` VARCHAR(255) NOT NULL, - `limit` INT(4) UNSIGNED NOT NULL default '20', - PRIMARY KEY (`id`), - KEY `field_id` (`field_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; - "); - } - catch(Exception $e){ - return false; - } - - return true; + return Symphony::Database() + ->create('tbl_fields_selectbox_link') + ->ifNotExists() + ->charset('utf8') + ->collate('utf8_unicode_ci') + ->fields([ + 'id' => [ + 'type' => 'int(11)', + 'auto' => true, + ], + 'field_id' => 'int(11)', + 'allow_multiple_selection' => [ + 'type' => 'enum', + 'values' => ['yes','no'], + 'default' => 'no', + ], + 'hide_when_prepopulated' => [ + 'type' => 'enum', + 'values' => ['yes','no'], + 'default' => 'no', + ], + 'related_field_id' => 'varchar(255)', + 'limit' => [ + 'type' => 'int(4)', + 'default' => 20 + ], + ]) + ->keys([ + 'id' => 'primary', + 'field_id' => 'key', + ]) + ->execute() + ->success(); } public function uninstall(){ - if(parent::uninstall() == true){ - Symphony::Database()->query("DROP TABLE `tbl_fields_selectbox_link`"); - return true; - } - - return false; + Symphony::Database() + ->drop('tbl_fields_selectbox_link') + ->ifExists() + ->execute() + ->success(); } public function update($previousVersion = false){ try{ if(version_compare($previousVersion, '1.27', '<')){ - Symphony::Database()->query( - "ALTER TABLE `tbl_fields_selectbox_link` ADD `hide_when_prepopulated` ENUM('yes','no') DEFAULT 'no'" - ); + Symphony::Database() + ->alter('tbl_fields_selectbox_link') + ->add([ + 'hide_when_prepopulated' => [ + 'type' => 'enum', + 'values' => ['yes','no'], + 'default' => 'no', + ], + ]) + ->execute() + ->success(); } } catch(Exception $e){ @@ -47,9 +68,16 @@ public function update($previousVersion = false){ try{ if(version_compare($previousVersion, '1.6', '<')){ - Symphony::Database()->query( - "ALTER TABLE `tbl_fields_selectbox_link` ADD `limit` INT(4) UNSIGNED NOT NULL DEFAULT '20'" - ); + Symphony::Database() + ->alter('tbl_fields_selectbox_link') + ->add([ + 'limit' => [ + 'type' => 'int(4)', + 'default' => 20 + ], + ]) + ->execute() + ->success(); } } catch(Exception $e){ @@ -58,9 +86,11 @@ public function update($previousVersion = false){ if(version_compare($previousVersion, '1.15', '<')){ try{ - $fields = Symphony::Database()->fetchCol('field_id', - "SELECT `field_id` FROM `tbl_fields_selectbox_link`" - ); + $fields = Symphony::Database() + ->select(['field_id']) + ->from('tbl_fields_selectbox_link') + ->execute() + ->column('field_id'); } catch(Exception $e){ // Discard @@ -69,13 +99,18 @@ public function update($previousVersion = false){ if(is_array($fields) && !empty($fields)){ foreach($fields as $field_id){ try{ - Symphony::Database()->query( - "ALTER TABLE `tbl_entries_data_{$field_id}` - CHANGE `relation_id` `relation_id` INT(11) UNSIGNED NULL DEFAULT NULL" - ); + Symphony::Database() + ->alter('tbl_entries_data_' . $field_id) + ->modify([ + 'relation_id' => [ + 'type' => 'int(11)', + 'null' => true, + ], + ]) + ->execute() + ->success(); } catch(Exception $e){ - // Discard } } } diff --git a/extension.meta.xml b/extension.meta.xml index 6486ff1..b294fde 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -1,6 +1,6 @@ - Select Box Link Field + Field: Select Box Link Linking two sections together https://github.com/symphonycms/selectbox_link_field https://getsymphony.com/discuss/thread/473/ @@ -18,6 +18,7 @@ - Update for Symphony 4.x - PHP7 Compatibility + - Code refactoring for Database and EQFA - [#87](https://github.com/symphonycms/selectbox_link_field/issues/87) Add missing canPrePopulate() function diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 0ac5d3b..1777850 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -72,16 +72,29 @@ public function fetchSuggestionTypes() -------------------------------------------------------------------------*/ public function createTable(){ - return Symphony::Database()->query( - "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` ( - `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - `entry_id` INT(11) UNSIGNED NOT NULL, - `relation_id` INT(11) UNSIGNED DEFAULT NULL, - PRIMARY KEY (`id`), - KEY `entry_id` (`entry_id`), - KEY `relation_id` (`relation_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" - ); + return Symphony::Database() + ->create('tbl_entries_data_' . $this->get('id')) + ->ifNotExists() + ->charset('utf8') + ->collate('utf8_unicode_ci') + ->fields([ + 'id' => [ + 'type' => 'int(11)', + 'auto' => true, + ], + 'entry_id' => 'int(11)', + 'relation_id' => [ + 'type' => 'int(11)', + 'null' => true, + ], + ]) + ->keys([ + 'id' => 'primary', + 'entry_id' => 'key', + 'relation_id' => 'key', + ]) + ->execute() + ->success(); } /*------------------------------------------------------------------------- @@ -101,50 +114,44 @@ public function findOptions(array $existing_selection = null, $entry_id = null){ if(!is_array($this->get('related_field_id'))) return $values; - // find the sections of the related fields - $sections = Symphony::Database()->fetch(" - SELECT DISTINCT `s`.`id`, `s`.`sortorder`, `f`.`id` as `field_id` - FROM `tbl_sections` AS `s` - LEFT JOIN `tbl_fields` AS `f` ON `s`.`id` = `f`.`parent_section` - WHERE `f`.`id` IN ('" . implode("','", $this->get('related_field_id')) . "') - ORDER BY `s`.`sortorder` ASC - "); - - if(is_array($sections) && !empty($sections)){ - foreach($sections as $_section) { - $section = SectionManager::fetch($_section['id']); - $group = array( - 'name' => $section->get('name'), - 'section' => $section->get('id'), - 'values' => array() - ); - - EntryManager::setFetchSorting($section->getSortingField(), $section->getSortingOrder()); - $entries = EntryManager::fetch(null, $section->get('id'), $limit, 0, null, null, false, false); + $sections = Symphony::Database() + ->select(['s.id', 's.sortorder', 'f.id' => 'field_id']) + ->distinct() + ->from('tbl_sections', 's') + ->leftJoin('tbl_fields', 'f') + ->on(['s.id' => '$f.parent_section']) + ->where(['f.id' => ['in' => $this->get('related_field_id')]]) + ->orderBy('s.sortorder') + ->execute() + ->rows(); + + foreach($sections as $_section) { + $section = SectionManager::fetch($_section['id']); + $group = array( + 'name' => $section->get('name'), + 'section' => $section->get('id'), + 'values' => array() + ); - $results = array(); - foreach($entries as $entry) { - $results[] = (int)$entry['id']; - } + $results = (new EntryManager)->select()->projection(['e.id'])->section($section->get('id'))->limit($limit)->execute()->column('id'); - // if a value is already selected, ensure it is added to the list (if it isn't in the available options) - if(!is_null($existing_selection) && !empty($existing_selection)){ - $entries_for_field = $this->findEntriesForField($existing_selection, $_section['field_id']); - $results = array_merge($results, $entries_for_field); - } + // if a value is already selected, ensure it is added to the list (if it isn't in the available options) + if(!is_null($existing_selection) && !empty($existing_selection)){ + $entries_for_field = $this->findEntriesForField($existing_selection, $_section['field_id']); + $results = array_merge($results, $entries_for_field); + } - if(is_array($results) && !empty($results)){ - $related_values = $this->findRelatedValues($results); - foreach($related_values as $value){ - $group['values'][$value['id']] = $value['value']; - } + if(is_array($results) && !empty($results)){ + $related_values = $this->findRelatedValues($results); + foreach($related_values as $value){ + $group['values'][$value['id']] = $value['value']; } + } - if(!is_null($entry_id) && isset($group['values'][$entry_id])){ - unset($group['values'][$entry_id]); - } - $values[] = $group; + if(!is_null($entry_id) && isset($group['values'][$entry_id])){ + unset($group['values'][$entry_id]); } + $values[] = $group; } return $values; @@ -167,23 +174,21 @@ public function toggleFieldData(array $data, $newState, $entry_id = null){ } public function fetchAssociatedEntryCount($value){ - return Symphony::Database()->fetchVar('count', 0, sprintf(" - SELECT COUNT(*) as `count` - FROM `tbl_entries_data_%d` - WHERE `relation_id` = %d - ", - $this->get('id'), $value - )); + return Symphony::Database() + ->select(['COUNT(*)' => 'count']) + ->from('tbl_entries_data_' . $this->get('id')) + ->where(['relation_id' => $value]) + ->execute() + ->variable('count'); } public function fetchAssociatedEntryIDs($value){ - return Symphony::Database()->fetchCol('entry_id', sprintf(" - SELECT `entry_id` - FROM `tbl_entries_data_%d` - WHERE `relation_id` = %d - ", - $this->get('id'), $value - )); + return Symphony::Database() + ->select(['entry_id']) + ->from('tbl_entries_data_' . $this->get('id')) + ->where(['relation_id' => $value]) + ->execute() + ->variable('entry_id'); } public function fetchAssociatedEntrySearchValue($data, $field_id = null, $parent_entry_id = null){ @@ -192,12 +197,13 @@ public function fetchAssociatedEntrySearchValue($data, $field_id = null, $parent if(!is_array($data)) return $data; - $searchvalue = Symphony::Database()->fetchRow(0, sprintf(" - SELECT `entry_id` FROM `tbl_entries_data_%d` - WHERE `handle` = '%s' - LIMIT 1", - $field_id, addslashes($data['handle']) - )); + $searchvalue = Symphony::Database() + ->select(['entry_id']) + ->from('tbl_entries_data_' . $field_id) + ->where(['handle' => addslashes($data['handle'])]) + ->limit(1) + ->execute() + ->rows(); return $searchvalue['entry_id']; } @@ -206,17 +212,17 @@ public function findEntriesForField(array $relation_id = array(), $field_id = nu if(empty($relation_id) || !is_array($this->get('related_field_id'))) return array(); try { - // Figure out which `related_field_id` is from that section - $relations = Symphony::Database()->fetchCol('id', sprintf(" - SELECT e.id - FROM `tbl_fields` AS `f` - LEFT JOIN `tbl_sections` AS `s` ON (f.parent_section = s.id) - LEFT JOIN `tbl_entries` AS `e` ON (e.section_id = s.id) - WHERE f.id = %d - AND e.id IN (%s) - ", - $field_id, implode(',',$relation_id), implode(',', $this->get('related_field_id')) - )); + $relations = Symphony::Database() + ->select(['e.id']) + ->from('tbl_fields', 'f') + ->leftJoin('tbl_sections', 's') + ->on(['f.parent_section' => '$s.id']) + ->leftJoin('tbl_entries', 'e') + ->on(['e.section_id' => '$s.id']) + ->where(['f.id' => (int)$field_id]) + ->where(['e.id' => ['in' => $relation_id]]) + ->execute() + ->column('id'); } catch(Exception $e){ return array(); @@ -256,14 +262,15 @@ protected function findRelatedValues(array $relation_id = array()) { $hash = md5(serialize($relation_id).$this->get('element_name')); if(!isset(self::$cache[$hash]['relation_data'])) { - $relation_ids = Symphony::Database()->fetch(sprintf(" - SELECT e.id, e.section_id, s.name, s.handle - FROM `tbl_entries` AS `e` - LEFT JOIN `tbl_sections` AS `s` ON (s.id = e.section_id) - WHERE e.id IN (%s) - ", - implode(',', $relation_id) - )); + // $relation_ids = Symphony::Database()->fetch(sprintf(" + $relation_ids = Symphony::Database() + ->select(['e.id', 'e.section_id', 's.name', 's.handle']) + ->from('tbl_entries', 'e') + ->leftJoin('tbl_sections', 's') + ->on(['s.id' => '$e.section_id']) + ->where(['e.id' => ['in' => $relation_id]]) + ->execute() + ->rows(); // 3. Group the `relation_id`'s by section_id $section_ids = array(); @@ -374,15 +381,13 @@ public function fetchIDfromValue($value) { 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' - LIMIT 1", $related_field_id, Lang::createHandle($value) - )); + return Symphony::Database() + ->select(['entry_id' => 'id']) + ->from('tbl_entries_data_' . $related_field_id) + ->where(['handle' => Lang::createHandle($value)]) + ->limit(1) + ->execute() + ->column('id'); // Skipping returns wrong results when doing an // AND operation, return 0 instead. @@ -947,10 +952,12 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal protected function getFieldSchema($fieldId) { try { - return Symphony::Database()->fetch(" - SHOW COLUMNS FROM `tbl_entries_data_$fieldId` - WHERE `Field` in ('value'); - "); + return Symphony::Database() + ->showColumns() + ->from('tbl_entries_data_' . $fieldId) + ->where(['Field' => ['in' => ['value']]]) + ->execute() + ->rows(); } catch (Exception $ex) { // bail out From fdce65fb8a65e392c8621b093594468ab38f2c5f Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Wed, 30 May 2018 14:53:44 -0400 Subject: [PATCH 08/19] Fix forgotten database queries in extension.driver --- extension.driver.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/extension.driver.php b/extension.driver.php index e19c8f0..b2d7700 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -117,7 +117,13 @@ public function update($previousVersion = false){ } try{ - Symphony::Database()->query("ALTER TABLE `tbl_fields_selectbox_link` CHANGE `related_field_id` `related_field_id` VARCHAR(255) NOT NULL"); + Symphony::Database() + ->alter('tbl_fields_selectbox_link') + ->modify([ + 'related_field_id' => 'varchar(255)' + ]) + ->execute() + ->success(); } catch(Exception $e){ // Discard @@ -125,7 +131,17 @@ public function update($previousVersion = false){ if(version_compare($previousVersion, '1.19', '<')){ try{ - Symphony::Database()->query("ALTER TABLE `tbl_fields_selectbox_link` ADD COLUMN `show_association` ENUM('yes','no') NOT NULL default 'yes'"); + Symphony::Database() + ->alter() + ->add([ + 'show_association' => [ + 'type' => 'enum', + 'values' => ['yes','no'], + 'default' => 'yes', + ], + ]) + ->execute() + ->success(); } catch(Exception $e){ // Discard @@ -134,7 +150,11 @@ public function update($previousVersion = false){ if(version_compare($previousVersion, '1.31', '<')){ try{ - Symphony::Database()->query("ALTER TABLE `tbl_fields_selectbox_link` DROP COLUMN `show_association`"); + Symphony::Database() + ->alter('tbl_fields_selectbox_link') + ->drop('show_association') + ->execute() + ->success(); } catch(Exception $e){ // Discard From ec7848251fc3c90b95336f9da298794999f62501 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Thu, 31 May 2018 09:58:40 -0400 Subject: [PATCH 09/19] Add forgotten "return" for uninstall method --- extension.driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.driver.php b/extension.driver.php index b2d7700..cebc933 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -39,7 +39,7 @@ public function install(){ } public function uninstall(){ - Symphony::Database() + return Symphony::Database() ->drop('tbl_fields_selectbox_link') ->ifExists() ->execute() From cdd9ae9088ba13e94e1754c934440684eb4325cb Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Mon, 11 Jun 2018 13:49:38 -0400 Subject: [PATCH 10/19] Add EQFA --- fields/field.selectbox_link.php | 3 +++ lib/class.entryquerylinkadapter.php | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/class.entryquerylinkadapter.php diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 1777850..5cbec54 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -4,6 +4,7 @@ require_once FACE . '/interface.exportablefield.php'; require_once FACE . '/interface.importablefield.php'; + require_once(EXTENSIONS . '/selectbox_link_field/lib/class.entryquerylinkadapter.php'); class FieldSelectBox_Link extends Field implements ExportableField, ImportableField { private static $cache = array(); @@ -14,6 +15,8 @@ class FieldSelectBox_Link extends Field implements ExportableField, ImportableFi public function __construct(){ parent::__construct(); + $this->entryQueryFieldAdapter = new EntryQueryLinkAdapter($this); + $this->_name = __('Select Box Link'); $this->_required = true; $this->_showassociation = true; diff --git a/lib/class.entryquerylinkadapter.php b/lib/class.entryquerylinkadapter.php new file mode 100644 index 0000000..a5f9f22 --- /dev/null +++ b/lib/class.entryquerylinkadapter.php @@ -0,0 +1,17 @@ + Date: Tue, 19 Jun 2018 11:26:12 -0400 Subject: [PATCH 11/19] Replace deprecated method fetch() by select() --- extension.meta.xml | 1 + fields/field.selectbox_link.php | 60 ++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/extension.meta.xml b/extension.meta.xml index b294fde..a8aa212 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -19,6 +19,7 @@ - Update for Symphony 4.x - PHP7 Compatibility - Code refactoring for Database and EQFA + - Replace deprecated method fetch() by select() - [#87](https://github.com/symphonycms/selectbox_link_field/issues/87) Add missing canPrePopulate() function diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 5cbec54..99e7be6 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -129,14 +129,24 @@ public function findOptions(array $existing_selection = null, $entry_id = null){ ->rows(); foreach($sections as $_section) { - $section = SectionManager::fetch($_section['id']); + $section = (new SectionManager) + ->select() + ->section($_section['id']) + ->execute() + ->next(); $group = array( 'name' => $section->get('name'), 'section' => $section->get('id'), 'values' => array() ); - $results = (new EntryManager)->select()->projection(['e.id'])->section($section->get('id'))->limit($limit)->execute()->column('id'); + $results = (new EntryManager) + ->select() + ->projection(['e.id']) + ->section($section->get('id')) + ->limit($limit) + ->execute() + ->column('id'); // if a value is already selected, ensure it is added to the list (if it isn't in the available options) if(!is_null($existing_selection) && !empty($existing_selection)){ @@ -243,7 +253,13 @@ protected function findRelatedValues(array $relation_id = array()) { $where = ' AND id IN (' . implode(',', $this->get('related_field_id')) . ') '; $hash = md5($where); if(!isset(self::$cache[$hash]['fields'])) { - $fields = FieldManager::fetch(null, null, 'ASC', 'sortorder', null, null, $where); + $fields = (new FieldManager) + ->select() + ->sort('sortorder', 'asc') + ->where(['id' => ['in' => $this->get('related_field_id')]]) + ->execute() + ->rows(); + if(!is_array($fields)) { $fields = array($fields); } @@ -302,11 +318,22 @@ protected function findRelatedValues(array $relation_id = array()) { } } - $section = SectionManager::fetch($section_id); + $section = (new SectionManager) + ->select() + ->section($section_id) + ->execute() + ->next(); + if(($section instanceof Section) === false) continue; - EntryManager::setFetchSorting($section->getSortingField(), $section->getSortingOrder()); - $entries = EntryManager::fetch(array_values($entry_data), $section_id, null, null, null, null, false, true, $schema); + $entries = (new EntryManager) + ->select() + ->sort($section->getSortingField(), $section->getSortingOrder()) + ->entries(array_values($entry_data)) + ->section($section_id) + ->schema($schema) + ->execute() + ->rows(); foreach ($entries as $entry) { $field_data = $entry->getData($field->get('id')); @@ -798,7 +825,11 @@ public function prepareExportValue($data, $mode, $entry_id = null) { else if ($mode === $modes->listEntryObject) { $items = array(); - $entries = EntryManager::fetch($data['relation_id']); + $entries = (new EntryManager) + ->select() + ->entry($data['relation_id']) + ->execute() + ->rows(); foreach ($entries as $entry) { if (is_array($entry) === false || empty($entry)) continue; @@ -1046,7 +1077,11 @@ public function groupRecords($records){ $groups = array($this->get('element_name') => array()); $related_field_id = current($this->get('related_field_id')); - $field = FieldManager::fetch($related_field_id); + $field = (new FieldManager) + ->select() + ->field($related_field_id) + ->execute() + ->next(); if(!$field instanceof Field) return; @@ -1067,7 +1102,14 @@ public function groupRecords($records){ } } else { - $related_data = EntryManager::fetch($value, $field->get('parent_section'), 1, null, null, null, false, true, array($field->get('element_name'))); + $related_data = (new EntryManager) + ->select() + ->entry($value) + ->section($field->get('parent_section')) + ->schema($field->get('element_name')) + ->limit(1) + ->execute() + ->next(); $related_data = current($related_data); if(!$related_data instanceof Entry) continue; From b1561f55ef04e082d63a0efc91e0d2189bc926c3 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Thu, 5 Jul 2018 12:03:26 -0400 Subject: [PATCH 12/19] Fix datasource --- fields/field.selectbox_link.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 99e7be6..ec19bed 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -281,7 +281,6 @@ protected function findRelatedValues(array $relation_id = array()) { $hash = md5(serialize($relation_id).$this->get('element_name')); if(!isset(self::$cache[$hash]['relation_data'])) { - // $relation_ids = Symphony::Database()->fetch(sprintf(" $relation_ids = Symphony::Database() ->select(['e.id', 'e.section_id', 's.name', 's.handle']) ->from('tbl_entries', 'e') @@ -331,6 +330,7 @@ protected function findRelatedValues(array $relation_id = array()) { ->sort($section->getSortingField(), $section->getSortingOrder()) ->entries(array_values($entry_data)) ->section($section_id) + ->includeAllFields() ->schema($schema) ->execute() ->rows(); From 84ec0d115053f067d8915e9ecf781fb922ea75a6 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Thu, 27 Sep 2018 15:25:57 -0400 Subject: [PATCH 13/19] Fix EntryManager query. --- fields/field.selectbox_link.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index ec19bed..ec852b7 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -327,9 +327,9 @@ protected function findRelatedValues(array $relation_id = array()) { $entries = (new EntryManager) ->select() + ->section($section_id) ->sort($section->getSortingField(), $section->getSortingOrder()) ->entries(array_values($entry_data)) - ->section($section_id) ->includeAllFields() ->schema($schema) ->execute() From c95abee22456e457975a34c03a55c90888388417 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Tue, 2 Oct 2018 09:03:34 -0400 Subject: [PATCH 14/19] Change release min supported version to 3.0.0 --- extension.meta.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.meta.xml b/extension.meta.xml index a8aa212..4c7a340 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -15,7 +15,7 @@ - + - Update for Symphony 4.x - PHP7 Compatibility - Code refactoring for Database and EQFA From 3a62bfca1b69cffe0ce428161fd238f6e46b1ef4 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Wed, 3 Oct 2018 09:37:44 -0400 Subject: [PATCH 15/19] Fix release 3.0.0 informations --- extension.meta.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension.meta.xml b/extension.meta.xml index 4c7a340..78ed31b 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -16,7 +16,7 @@ - - Update for Symphony 4.x + - Update for Symphony 3.x - PHP7 Compatibility - Code refactoring for Database and EQFA - Replace deprecated method fetch() by select() From 9eb01c85d6f67003a1b0588b269945ed634d2aeb Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Tue, 18 Dec 2018 09:48:09 -0500 Subject: [PATCH 16/19] Remote SQL collate and charset definitions. --- extension.driver.php | 2 -- fields/field.selectbox_link.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/extension.driver.php b/extension.driver.php index cebc933..d5fa928 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -6,8 +6,6 @@ public function install(){ return Symphony::Database() ->create('tbl_fields_selectbox_link') ->ifNotExists() - ->charset('utf8') - ->collate('utf8_unicode_ci') ->fields([ 'id' => [ 'type' => 'int(11)', diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index ec852b7..0952fd4 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -78,8 +78,6 @@ public function createTable(){ return Symphony::Database() ->create('tbl_entries_data_' . $this->get('id')) ->ifNotExists() - ->charset('utf8') - ->collate('utf8_unicode_ci') ->fields([ 'id' => [ 'type' => 'int(11)', From 2830f9b0393d6c372df3cee675056f3bc197e810 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Mon, 4 Feb 2019 16:00:43 -0500 Subject: [PATCH 17/19] Patch filter with or without space avec symbols. Fix filtering by value. --- fields/field.selectbox_link.php | 6 ++---- lib/class.entryquerylinkadapter.php | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 0952fd4..170a440 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -208,15 +208,13 @@ public function fetchAssociatedEntrySearchValue($data, $field_id = null, $parent if(!is_array($data)) return $data; - $searchvalue = Symphony::Database() + return Symphony::Database() ->select(['entry_id']) ->from('tbl_entries_data_' . $field_id) ->where(['handle' => addslashes($data['handle'])]) ->limit(1) ->execute() - ->rows(); - - return $searchvalue['entry_id']; + ->variable('entry_id'); } public function findEntriesForField(array $relation_id = array(), $field_id = null) { diff --git a/lib/class.entryquerylinkadapter.php b/lib/class.entryquerylinkadapter.php index a5f9f22..a73e6b4 100644 --- a/lib/class.entryquerylinkadapter.php +++ b/lib/class.entryquerylinkadapter.php @@ -10,6 +10,30 @@ public function getFilterColumns() return ['relation_id']; } + protected function filterSingle(EntryQuery $query, $filter) + { + // Extract the portion of the filter which would be a search value + $eFilter = explode(':', $filter, 2); + $eFilterIndex = count($eFilter) === 2 ? 1 : 0; + $eFilterValue = &$eFilter[$eFilterIndex]; + $eFilterValue = trim($eFilterValue); + // If it is not an ID, use fetchAssociatedEntrySearchValue to convert to a proper search value + if (General::intval($eFilterValue) === -1) { + foreach ($this->field->get('related_field_id') as $relatedFieldId) { + $eFilterValue = $this->field->fetchAssociatedEntrySearchValue( + ['handle' => General::createHandle($eFilterValue)], + $relatedFieldId + ); + + if ($eFilterValue) { + break; + } + } + } + // Use the parent's behavior, but with the search value replaced + return parent::filterSingle($query, implode(':', $eFilter)); + } + public function getSortColumns() { return ['relation_id']; From 89620b9a2db4eb3c2db4e9cd8ce2e3efc64b7001 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Wed, 1 May 2019 15:11:42 -0400 Subject: [PATCH 18/19] make sure schema gets an array --- fields/field.selectbox_link.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index 170a440..b7f3ec0 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -1102,7 +1102,7 @@ public function groupRecords($records){ ->select() ->entry($value) ->section($field->get('parent_section')) - ->schema($field->get('element_name')) + ->schema([$field->get('element_name')]) ->limit(1) ->execute() ->next(); From 67a8c5b4ae305c2f1d1f6491a61b09b1939c1aa7 Mon Sep 17 00:00:00 2001 From: Alexandra Nantel Date: Wed, 31 Jul 2019 10:33:28 -0400 Subject: [PATCH 19/19] Fix related_data value --- fields/field.selectbox_link.php | 1 - 1 file changed, 1 deletion(-) diff --git a/fields/field.selectbox_link.php b/fields/field.selectbox_link.php index b7f3ec0..1c04de0 100755 --- a/fields/field.selectbox_link.php +++ b/fields/field.selectbox_link.php @@ -1106,7 +1106,6 @@ public function groupRecords($records){ ->limit(1) ->execute() ->next(); - $related_data = current($related_data); if(!$related_data instanceof Entry) continue;