Skip to content

Commit dbe7494

Browse files
committed
remove hardcoded table names
This finds the table name using the getSupportedExtension function, except for categories. There are also changes for the field description and some comments. Also removed hardcoded error messages.
1 parent 9b3d1c6 commit dbe7494

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

administrator/language/en-GB/en-GB.plg_system_languagefilter.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ PLG_SYSTEM_LANGUAGEFILTER_FIELD_GLOBAL_MASTER_LANGUAGE_LABEL="Global Master Lang
1313
PLG_SYSTEM_LANGUAGEFILTER_FIELD_GLOBAL_MASTER_LANGUAGE_DESC="Note that changing this value will overwrite all existing Master Child relationships with their states."
1414
PLG_SYSTEM_LANGUAGEFILTER_FIELD_ITEM_ASSOCIATIONS_LABEL="Item Associations"
1515
PLG_SYSTEM_LANGUAGEFILTER_FIELD_USE_MASTER_LANGUAGE_LABEL="Set a Master Language?"
16-
PLG_SYSTEM_LANGUAGEFILTER_FIELD_USE_MASTER_LANGUAGE_DESC="When enabled, a master-child relationship is added between the languages, depending on the selected master language. This adds an additional 'outdated' state to a child element depending on its master. If disabled, functions dependent on this data are not available."
16+
PLG_SYSTEM_LANGUAGEFILTER_FIELD_USE_MASTER_LANGUAGE_DESC="When enabled, a master-child relationship is added between the languages, depending on the selected master language. This adds an additional 'outdated' state to a child element depending on its master. Please enable versions to get a comparison view for outdated items. If this field is disabled, functions dependent on this data are not available."
1717
PLG_SYSTEM_LANGUAGEFILTER_FIELD_XDEFAULT_LABEL="Add x-default Meta Tag"
1818
PLG_SYSTEM_LANGUAGEFILTER_FIELD_XDEFAULT_LANGUAGE_LABEL="x-default Language"
1919
PLG_SYSTEM_LANGUAGEFILTER_OPTION_DEFAULT_LANGUAGE="Default frontend language"

plugins/system/languagefilter/languagefilter.php

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Joomla\CMS\Router\Router;
2828
use Joomla\CMS\Table\Table;
2929
use Joomla\CMS\Uri\Uri;
30+
use Joomla\Component\Associations\Administrator\Helper\AssociationsHelper;
3031
use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
3132
use Joomla\Registry\Registry;
3233
use Joomla\String\StringHelper;
@@ -969,7 +970,7 @@ private function getLanguageCookie()
969970
/**
970971
* Before Saving extensions
971972
* Method is called when an extension is going to be saved
972-
* change parameters for master language because there depends on other parameters
973+
* change parameters for master language because they depends on other parameters
973974
*
974975
* @param string $context The extension
975976
* @param JTable $table DataBase Table object
@@ -1045,12 +1046,15 @@ public function onExtensionAfterSave($context, $table, $isNew)
10451046
}
10461047

10471048
/**
1048-
* Method to set the master item of an association as parent and the children get the parent id
1049-
* Also reset the modified date of the master item. Master and children will be up-to-date, as they get the same modified date
1049+
* Method to set the master of an association.
1050+
* This resets all the current master ids and the modified date and set these new.
1051+
* Master and children will be up-to-date, as they get the same modified date
10501052
*
10511053
* @param string $language The global master language
10521054
*
1053-
* @return void
1055+
* @return boolean Returns true on success, false on failure.
1056+
*
1057+
* @throws \Exception
10541058
*
10551059
* @since 4.0.0
10561060
*/
@@ -1074,9 +1078,7 @@ private function _setMasterItem($language)
10741078
}
10751079
catch (ExecutionFailureException $e)
10761080
{
1077-
$this->_message .= Text::_(' , failed to update the master with his childs');
1078-
1079-
return;
1081+
return false;
10801082
}
10811083
}
10821084
else
@@ -1098,43 +1100,26 @@ private function _setMasterItem($language)
10981100
->where($db->quoteName('key') . ' = ' . $db->quote($value));
10991101
$assocContext = $db->setQuery($contextQuery)->loadResult();
11001102

1101-
$extension = '';
1103+
$checkCategoryComponent = '';
1104+
$component = explode('.', $assocContext)[0];
11021105

1103-
// get the correct table to look in depending on the context
1104-
switch ($assocContext)
1106+
if($component === 'com_categories'){
1107+
$fromTable = $db->quoteName('#__categories', 'e');
1108+
$modified = $db->quoteName('e.modified_time');
1109+
$checkCategoryComponent = $db->quoteName('e.extension');
1110+
}
1111+
else
11051112
{
1106-
case 'com_content.item':
1107-
$fromTable = $db->quoteName('#__content', 'e');
1108-
$modified = $db->quoteName('e.modified');
1109-
$typeAlias = 'com_content.article';
1110-
break;
1111-
1112-
case 'com_menus.item' :
1113-
$fromTable = $db->quoteName('#__menu', 'e');
1114-
$modified = '';
1115-
$typeAlias = '';
1116-
break;
1117-
1118-
case 'com_categories.item':
1119-
$fromTable = $db->quoteName('#__categories', 'e');
1120-
$modified = $db->quoteName('e.modified_time');
1121-
$extension = $db->quoteName('e.extension');
1122-
break;
1123-
1124-
case 'com_contact.item':
1125-
$fromTable = $db->quoteName('#__contact_details', 'e');
1126-
$modified = $db->quoteName('e.modified');
1127-
$typeAlias = 'com_contact.contact';
1128-
break;
1129-
1130-
case 'com_newsfeeds.item':
1131-
$fromTable = $db->quoteName('#__newsfeeds', 'e');
1132-
$modified = $db->quoteName('e.modified');
1133-
$typeAlias = 'com_newsfeeds.newsfeed';
1134-
break;
1113+
$extension = AssociationsHelper::getSupportedExtension($component);
1114+
$extensionType = $extension['helper']->getItemTypes()[0];
1115+
$extensionTable = $extension['types'][$extensionType]->get('details')['tables'];
1116+
$tableName = $extensionTable[array_keys($extensionTable)[0]];
1117+
$fromTable = $db->quoteName($tableName, 'e');
1118+
$typeAlias = $component . '.' . $extensionType;
1119+
$modified = ($component === 'com_menus') ? '' : $db->quoteName('e.modified');
11351120
}
11361121

1137-
// get items with the global master language
1122+
// get ids of items with the global master language
11381123
$subQuery = $db->getQuery(true)
11391124
->select($db->quoteName('e.id'))
11401125
->from($fromTable)
@@ -1152,20 +1137,20 @@ private function _setMasterItem($language)
11521137
if ($modified)
11531138
{
11541139
// get the context of this category
1155-
if($extension){
1156-
$categoryQuery = $db->getQuery(true)
1157-
->select($extension)
1140+
if ($checkCategoryComponent)
1141+
{
1142+
$categoryQuery = $db->getQuery(true)
1143+
->select($checkCategoryComponent)
11581144
->from($fromTable)
11591145
->where($db->quoteName('id') . ' = ' . $db->quote($masterId));
11601146
$categoryMasterExtension = $db->setQuery($categoryQuery)->loadResult();
11611147
$typeAlias = $categoryMasterExtension . '.category';
11621148
}
11631149

1164-
// if enabled use the history save_date otherwise use the modified date
1165-
$component = $categoryMasterExtension ?? explode('.', $assocContext)[0];
1150+
$component = $categoryMasterExtension ?? $component;
11661151
$saveHistory = ComponentHelper::getParams($component)->get('save_history', 0);
11671152

1168-
// if versions are enabled get the save_data of the master item from history table
1153+
// if versions are enabled get the save_data of the master item from history table otherwise use the modified date
11691154
if ($saveHistory)
11701155
{
11711156
$typeId = Table::getInstance('ContentType')->getTypeId($typeAlias);
@@ -1179,10 +1164,8 @@ private function _setMasterItem($language)
11791164
$masterModQuery = $db->getQuery(true)
11801165
->select($modified)
11811166
->from($fromTable)
1182-
->where($db->quoteName('id') . ' = '
1183-
. $db->quote($masterId));
1184-
$masterModified = $db->setQuery($masterModQuery)
1185-
->loadResult();
1167+
->where($db->quoteName('id') . ' = ' . $db->quote($masterId));
1168+
$masterModified = $db->setQuery($masterModQuery)->loadResult();
11861169
}
11871170
}
11881171

@@ -1205,9 +1188,7 @@ private function _setMasterItem($language)
12051188
}
12061189
catch (ExecutionFailureException $e)
12071190
{
1208-
$this->_message .= Text::_(' , failed to update the master with his childs');
1209-
1210-
return;
1191+
return false;
12111192
}
12121193

12131194
// Set the master id and modified date to the children
@@ -1226,12 +1207,10 @@ private function _setMasterItem($language)
12261207
}
12271208
catch (ExecutionFailureException $e)
12281209
{
1229-
$this->_message .= Text::_(' , failed to update the master with his childs');
1230-
1231-
return;
1210+
return false;
12321211
}
1233-
12341212
}
12351213
}
1214+
return true;
12361215
}
12371216
}

0 commit comments

Comments
 (0)