Keyword

Bug when you make more than 2 same titles in same category

More
5 years 4 months ago #170345 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:
// 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.

More
5 years 4 months ago #170354 by JoomlaWorks
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 :)

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
5 years 4 months ago #170357 by Filip
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.

More
5 years 4 months ago #170359 by JoomlaWorks
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.

More
5 years 4 months ago - 5 years 4 months ago #170368 by Filip
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,
// 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');
            }
        }
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):
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;
Then your if statement with changed comparison parameter:
// 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: 5 years 4 months ago by Filip.

Please Log in or Create an account to join the conversation.

More
5 years 4 months ago #170372 by JoomlaWorks
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.

More
5 years 4 months ago #170373 by Filip
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!

never walk alone

Please Log in or Create an account to join the conversation.

More
5 years 4 months ago #170374 by JoomlaWorks
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.

More
5 years 4 months ago #170375 by JoomlaWorks
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.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum