COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- Bug when you make more than 2 same titles in same category
Bug when you make more than 2 same titles in same category
- Filip
- Topic Author
- Offline
- New Member
Less
More
6 years 1 week ago #170345
by Filip
never walk alone
Bug when you make more than 2 same titles in same category was created by Filip
Hi,
I know how multiple same titles are resolved by adding suffix 2 at the end of alias (url). What does not work as it should is when you create more than 2 same titles within same category. This does not happen very often, of course, but when it does suffix is always stuck at number - 2! Which is wrong since it'll create links to just item that has number 2 at the end for each item that came after number 2.
\administrator\components\com_k2\tables\k2item.php is problem and code there:
What this does is it checks if we already have that alias in db and when it discovers we do, it adds number at the end. At beginning it adds 2. But next time we make same title (alias) it goes through database and discovers AGAIN just one alias that is same. It adds +1 to $result which gives us again: 2. And so on... Please test to see. Make 3 or more items with same title w/o entering alias (which I am sure majority of joomla users do) and after 2nd one you'll get aliases created automaticly and all of them will have number 2 at the end. In code I see idea was to increase number by 1 each time, but either sql query or php code should check further and not include "-2" (or any other) in search. I know this can not be generic, but maybe there could be an option allowing to check aliases this way.
BTW, you made Joomla so much better with K2, I use it in so many ways and in so man places! Thank you!!!
I know how multiple same titles are resolved by adding suffix 2 at the end of alias (url). What does not work as it should is when you create more than 2 same titles within same category. This does not happen very often, of course, but when it does suffix is always stuck at number - 2! Which is wrong since it'll create links to just item that has number 2 at the end for each item that came after number 2.
\administrator\components\com_k2\tables\k2item.php is problem and code there:
// Check if alias already exists. If so warn the user
$params = JComponentHelper::getParams('com_k2');
if ($params->get('k2Sef') && !$params->get('k2SefInsertItemId'))
{
$db = JFactory::getDbo();
$db->setQuery("SELECT id FROM #__k2_items WHERE alias = ".$db->quote($this->alias)." AND id != ".(int)$this->id);
$result = count($db->loadObjectList());
if ($result > 0)
{
$this->alias .= '-'.((int)$result + 1);
$application = JFactory::getApplication();
$application->enqueueMessage(JText::_('K2_WARNING_DUPLICATE_TITLE_ALIAS_DETECTED'), 'notice');
}
}
What this does is it checks if we already have that alias in db and when it discovers we do, it adds number at the end. At beginning it adds 2. But next time we make same title (alias) it goes through database and discovers AGAIN just one alias that is same. It adds +1 to $result which gives us again: 2. And so on... Please test to see. Make 3 or more items with same title w/o entering alias (which I am sure majority of joomla users do) and after 2nd one you'll get aliases created automaticly and all of them will have number 2 at the end. In code I see idea was to increase number by 1 each time, but either sql query or php code should check further and not include "-2" (or any other) in search. I know this can not be generic, but maybe there could be an option allowing to check aliases this way.
BTW, you made Joomla so much better with K2, I use it in so many ways and in so man places! Thank you!!!
never walk alone
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 1 week ago #170354
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Bug when you make more than 2 same titles in same category
Thanks for noticing this bug. I actually made a simpler fix: just insert the item ID after the item alias (which is always unique). :)
Already pushed on K2's repo (for 2.9.1) so you can test it out here: github.com/getk2/k2/archive/master.zip
And thank you for your kind words :)
Already pushed on K2's repo (for 2.9.1) so you can test it out here: github.com/getk2/k2/archive/master.zip
And thank you for your kind words :)
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Filip
- Topic Author
- Offline
- New Member
6 years 1 week ago #170357
by Filip
never walk alone
Replied by Filip on topic Bug when you make more than 2 same titles in same category
Yes, nice and simple solution! Good thinking. I'll certanly use it. Thank you for quick reply, cheers!
never walk alone
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 1 week ago #170359
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Bug when you make more than 2 same titles in same category
You're welcome.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Filip
- Topic Author
- Offline
- New Member
6 years 6 days ago - 6 years 6 days ago #170368
by Filip
never walk alone
Replied by Filip on topic Bug when you make more than 2 same titles in same category
Hi again, Fotis,
I tested your solution and have to say that it (unfortunately) does not work :)
Actually, it's more matter of rareness of my case, but this has to be fixed. And I think I did it (see below), you just go through it and see if you agree...
Your code,will put suffix "-0" every time you add item that has same title (alias) pass 2nd item of that kind. Reason? "$this->id" does not exist at the moment of creation of an item. Editing an item is ok, when you enter editing mode and delete alias then click "Save" - it's created with ID as suffix, since script was able to read it now...
SOLUTION:
before if statement mentioned above we need to fetch ID "candidate", so we need to add something like this (one more if/else is one possible solution):Then your if statement with changed comparison parameter:
That should be it. Please test it, I might be wrong, always questioning myself ;)
I tested your solution and have to say that it (unfortunately) does not work :)
Actually, it's more matter of rareness of my case, but this has to be fixed. And I think I did it (see below), you just go through it and see if you agree...
Your code,
// Check if the item alias already exists. If so warn the user and append the item ID to it.
$params = JComponentHelper::getParams('com_k2');
if ($params->get('k2Sef') && !$params->get('k2SefInsertItemId')) {
$db = JFactory::getDbo();
$db->setQuery("SELECT id FROM #__k2_items WHERE alias = ".$db->quote($this->alias)." AND id != ".(int)$this->id);
$result = count($db->loadObjectList());
if ($result > 0) {
$this->alias .= '-'.(int)$this->id;
$application = JFactory::getApplication();
$application->enqueueMessage(JText::_('K2_WARNING_DUPLICATE_TITLE_ALIAS_DETECTED'), 'notice');
}
}
SOLUTION:
before if statement mentioned above we need to fetch ID "candidate", so we need to add something like this (one more if/else is one possible solution):
if ($this->id == null) {
$db = JFactory::getDbo();
$db->setQuery("SELECT MAX(id)+1 from #__k2_items");
$target_id = $db->loadResult();
}
else
$target_id = $this->id;
// Check if the item alias already exists. If so warn the user and append the item ID to it.
$params = JComponentHelper::getParams('com_k2');
if ($params->get('k2Sef') && !$params->get('k2SefInsertItemId')) {
$db = JFactory::getDbo();
$db->setQuery("SELECT id FROM #__k2_items WHERE alias = ".$db->quote($this->alias)." AND id != ".(int)$target_id);
$result = count($db->loadObjectList());
if ($result > 0) {
$this->alias .= '-'.(int)$target_id;
$application = JFactory::getApplication();
$application->enqueueMessage(JText::_('K2_WARNING_DUPLICATE_TITLE_ALIAS_DETECTED'), 'notice');
}
}
That should be it. Please test it, I might be wrong, always questioning myself ;)
never walk alone
Last edit: 6 years 6 days ago by Filip.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 6 days ago #170372
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Bug when you make more than 2 same titles in same category
An extra SQL query is probably overkill. I'll just update the code to add a hash from the title and the current datetime.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Filip
- Topic Author
- Offline
- New Member
6 years 6 days ago #170373
by Filip
never walk alone
Replied by Filip on topic Bug when you make more than 2 same titles in same category
This particular SQL is simple and lightning fast one, just seeks last id. And url looks more reasonable than hash + date/time... Think of it, please. And thank you once again for being fast on reply :)
Cheers!
Cheers!
never walk alone
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 6 days ago #170374
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Bug when you make more than 2 same titles in same category
Fixed and pushed on K2's repo (for v2.9.1): github.com/getk2/k2/archive/master.zip
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 6 days ago #170375
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Bug when you make more than 2 same titles in same category
One less query in the database is always important. And performance is crucial for us with K2.
Keep in mind that the notice shown after the title alias is changed (on "save") serves as a way to remind the author of the item to update the title alias.
Keep in mind that the notice shown after the title alias is changed (on "save") serves as a way to remind the author of the item to update the title alias.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- Bug when you make more than 2 same titles in same category