Cotonti / Open Source PHP Content Management FrameworkContent Management Framework

Forums / Cotonti / Extensions / Pages order in tag output

staleo
#1 2014-04-25 22:31

Hi all,

I got a question on how to sort the pages with the same tag. In the plugin settings, there are four options to choose from: to sort pages by id, by date, by title, and by category. Dear community, how do I sort the pages by begin date?

Thanks in advance,

Cheers.

Macik
#2 2014-04-27 09:57

This functionallity is hard coded in Tags plugin. So you can not alter it simply with settings.

But it's a flexible enough to be extended with other plugin for this. 

So you can write simple plugin with 2 hooks:

  • on «tags.search.pages.query» hook
	// check is it possible to sort by this field
	if ($db->fieldExists($db_pages,'page_'.$o)) {

		$order = 'ORDER BY `page_'.$o.'`';
	}
  • on «tags.first» hook
    // column => title
    $addition_fields = array(
    	'begin' => 'Begin date',
    	'author' => 'Author',
    	// on so on — any page field
    );
    foreach ($addition_fields as $order=>$title)
    {
    	$ord = mb_strtolower($order);
    	$selected = $ord == $o ? 'selected="selected"' : '';
    	$tag_order .= cot_rc('input_option', array('value' => $ord, 'selected' => $selected, 'title' => $title));
    }
    

     

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
staleo
#3 2014-04-28 10:35

Macik,

Thank you for the info, but unfortunately I lack the experience of writing the plugins, as well as have a rather weak understanding of what you imply by hooks, and how the latter deal with the former. If this doesn't bother you, can you please explain how do I implement your solution step by step? I'd appreciate it a lot!

Thank you again.

Macik
#4 2014-04-28 18:41

Making plugins is just a piece of cake:

Look at here, check readme, download it and install — this is magic box for plugin creation.

Then let's create our simple plugin — look at this http://screenshot.galaxyhost.org/img/20140428-75f-72kb.jpg :

  1. Set (marked with green) plugin name, check setup file, addition files for our Hooks — `tagsort.first` and `tagsort.pages.query`. Marked wih blue is optional. Other are useless for out plug and should be unchecked.
  2. Add addition info in «Section 2» (http://screenshot.galaxyhost.org/img/20140428-5mp-48kb.jpg) — title, description, author name.

Press «Create extension files». Magic is done. Now we should work a little:

Locate files in `plugins/extension_template/created/tagsort`, copy it to plugins folder as usual plug. 

Open our generated files (`tagsort.first.php` and `tagsort.pages.query.php`), fix hook names and add corresponding code:
http://screenshot.galaxyhost.org/img/20140428-dob-66kb.jpg 
http://screenshot.galaxyhost.org/img/20140428-gs7-42kb.jpg

It's never been so easy. :)

Some code explanation (see post above):

  • For `tags.first` we extend drop down selection control with addition «sorting by» values as:  Begin date, author.
  • For `tags.search.pages.query` we alter part of tags search SQL query that sets pages order by our selected «sort by» value.

 

 

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
staleo
#5 2014-04-28 22:51

Macik,

Thank you for a such a broad descriprion of how your plugin works, but anyway, for some reason, your solution didn't work for me properly. Here's the tagsort plugin http://joxi.ru/nNheUxjKTJByDnIHAZc — that seems, the tags.first hook simply doesn't correspond with the Tags plugin settings. There are 4 options of how to sort the pages, just like it used to be, no "Author", no "Begin date", nothing.

I could of course make some mistake, and your help with it would be very much appreciated.

Above that, if you don't mind, I found an issue, which could be rather important for your plugin users, particularly for those like me, lacking the experience of writing plugins for their needs. Right after filling up the second section, "Misc", I found out that clicking "Submit" simply doesn't change anything. No output, no answer, the button just leaves everything how it was. I tried refilling the data 3 or 4 times, before I cought what was wrong. The issue here is that user doesn't know where exactly plugin tries to create a subfolder /created. The right answer is: in /datas, but who knows that? :) Therefore, this could be a good idea to give user a short hint, like, "Make /datas writable, we will do the rest", or something.

Macik
#6 2014-05-01 16:29

Thank you for a such a broad descriprion of how your plugin works, but anyway, for some reason, your solution didn't work for me properly. Here's the tagsort plugin http://joxi.ru/nNheUxjKTJByDnIHAZc — that seems, the tags.first hook simply doesn't correspond with the Tags plugin settings. There are 4 options of how to sort the pages, just like it used to be, no "Author", no "Begin date", nothing.

I could of course make some mistake, and your help with it would be very much appreciated.

It's quite strange. 

I had checked you archive and it works fine for me — http://screenshot.galaxyhost.org/img/20140501-5q3-35kb.jpg

What about versions that you use (Cotonti and Tags plugin)?

I use Siena version and

Version=0.9.14
Date=2012-02-19

of `Tags` plugin 

Above that, if you don't mind, I found an issue, which could be rather important for your plugin users, particularly for those like me, lacking the experience of writing plugins for their needs. Right after filling up the second section, "Misc", I found out that clicking "Submit" simply doesn't change anything. No output, no answer, the button just leaves everything how it was. I tried refilling the data 3 or 4 times, before I cought what was wrong. The issue here is that user doesn't know where exactly plugin tries to create a subfolder /created. The right answer is: in /datas, but who knows that? :) Therefore, this could be a good idea to give user a short hint, like, "Make /datas writable, we will do the rest", or something.

Thank you for your opinion.

First of all. it should be displayed Log message (like here: http://screenshot.galaxyhost.org/img/20140501-bpb-62kb.jpg )
As for target dir, you are right — I forgot that by default its location is `datas/created/`. It can be altered via plugin settings.

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
staleo
#7 2014-05-01 19:08

Macik,

Of course I messed everything up. :)

  • For `tags.first` we extend drop down selection control with addition «sorting by» values as:  Begin date, author.

For some reason, I thought you imply the sorting values in the Tags plugin settings (http://domain.com/admin/config?n=edit&o=plug&p=tags), while in fact you were meaning the sorting options on the actual tags output page (http://domain.com/tags/bla-bla-tah-here). Now it's all fine, it works. But I was actually planning to hide the sorting options, just leaving the "By begin date" as a default method of sorting pages. Are there any ways of doing so?

Thank you in advance!

Добавлено 4 минуты спустя:

Macik,

Also, if you have any ideas on another Tags issue, welcome! Here's the topic: http://www.cotonti.com/forums?m=posts&q=7730.

Macik
#8 2014-05-02 15:53

 Now it's all fine, it works. But I was actually planning to hide the sorting options, just leaving the "By begin date" as a default method of sorting pages. Are there any ways of doing so?

This way it's faster to do it via edit template `tags.tpl` and replace:
<select name="order">
	<option value="">{PHP.L.tags_Orderby}</option>
	<option value="">--</option>
	{TAGS_ORDER}
</select>

with:

<input type="hidden" name="order" value="begin" />

Then you can stop `tags.first` part of plugin, leaving only `tagsort.pages.query`.

Also, if you have any ideas on another Tags issue, welcome! Here's the topic: http://www.cotonti.com/forums?m=posts&q=7730.

I'll check it later.

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
staleo
#9 2014-05-20 18:10

All right, here's the solution we found.

1. In a table cot_config we search a required setting with a query:

SELECT *
FROM `cot_config`
WHERE `config_owner` = 'plug'
AND `config_cat` = 'tags'
AND `config_name`='sort'

2. Here we add a required field. If you need to sort pages by page_begin, just add begin (without page_).

3. Here we go! In the Tags plugin settings, find a dropdown menu with the options how to sort pages with the same tag. Choose Begin.

Voila! For this short manual, we send deepest thanks to Nastya Kizza. :)

p.s. Thx to Macik anyway!