WBCE CMS Forum

WBCE CMS – Way Better Content Editing.

Du bist nicht angemeldet.

#1 13.09.2021 18:31:48

losttrip
Mitglied

NWI - Tags - Drag & Drop reordering

WBCE Version: 1.5.0
News with Images 5.0.7

How difficult would it be to implement drag & drop reordering of Tags, as it is with Groups.

This would be helpful in many situations.  For instance, a bunch of year Tags are added - 2020, 2019,2018,2017
Then, later you add 2021, and it is now shown AFTER the rest (confusing) - 2020, 2019,2018,2017, 2021

Offline

#2 17.09.2021 03:50:13

losttrip
Mitglied

Re: NWI - Tags - Drag & Drop reordering

Is this something that could be considered for future versions, or is there not perceived value in the ability to re-order Tags?

Offline

#3 17.09.2021 07:41:52

florian
Administrator

Re: NWI - Tags - Drag & Drop reordering

Currently, there is no sorting option at all, and due to the database structure it would be also quite difficult to add a sorting function in general. So although I see the need, I'm afraid this feature will not be added to NWI.
As a quick&dirty solution, you can modify the functions.inc.php.
Find

function mod_nwi_get_tags_for_post($post_id)

and in that function, before the line

return $tags;

add

sort($tags);

Code allein macht nicht glücklich. Jetzt spenden!

Offline

#4 17.09.2021 14:23:16

losttrip
Mitglied

Re: NWI - Tags - Drag & Drop reordering

I understand.

To test, I implemented your suggested quick&dirty solution, but I don't see any difference reflected in the backend.
What is this solution supposed to do?

function mod_nwi_get_tags_for_post($post_id)
{
    global $database;
    $tags = array();

    $query_tags = $database->query(sprintf(
        "SELECT  t1.*, t4.`page_id` " .
        "FROM `%smod_news_img_tags` AS t1 " .
        "JOIN `%smod_news_img_tags_posts` AS t2 " .
        "ON t1.`tag_id`=t2.`tag_id` ".
        "JOIN `%smod_news_img_posts` AS t3 ".
        "ON t2.`post_id`=t3.`post_id` ".
        "JOIN `%ssections` AS t4 ".
        "ON t3.`section_id`=t4.`section_id` ".
        "WHERE t2.`post_id`=%d",
        TABLE_PREFIX, TABLE_PREFIX, TABLE_PREFIX, TABLE_PREFIX, $post_id
    ));

    if (!empty($query_tags) && $query_tags->numRows() > 0) {
        while($t = $query_tags->fetchRow()) {
            $tags[$t['tag_id']] = $t;
        }
    }

    sort($tags);
    return $tags;
}   // end function mod_nwi_get_tags_for_post()

Keep in mind, the tags were added in this order - 2020, 2019, 2018, 2022
What I was hoping to see on the NWI post editor window, would be the Tags shown in this order - 2022, 2020, 2019, 2018

The tags are still listed in the order added in the Tags list:
e672ee5cdb3ee6dc6c34e1befa3d03f7.png


And they are still listed in the order added in the NWI post editor:
951344fdbc5c9b65d9ef063885b8faf2.png

Beitrag geändert von losttrip (17.09.2021 15:16:12)

Offline

#5 17.09.2021 18:15:29

webbird
Administrator

Re: NWI - Tags - Drag & Drop reordering

The tags are added to the $tags array using the tag_id as key and the tag data as value. Arrays with numeric keys are always sorted by that key, so in this case, by the tag_id.

I think the easiest way would be to edit the tag_id's in the database, but this will of course not help if add new tags later.

To sort an array by value, you can try to use this function:

[== PHP ==]
        /**
         * sort an array
         *
         * @access public
         * @param  array   $array          - array to sort
         * @param  mixed   $index          - key to sort by
         * @param  string  $order          - 'asc' (default) || 'desc'
         * @param  boolean $natsort        - default: false
         * @param  boolean $case_sensitive - sort case sensitive; default: false
         *
         **/
        function sort($array, $index, $order='asc', $natsort=false, $case_sensitive=false)
        {
            if (is_array($array) && count($array)) {
                foreach (array_keys($array) as $key) {
                    $temp[$key] = $array[$key][$index];
                }
                if (!$natsort) {
                    ($order=='asc') ? asort($temp) : arsort($temp);
                } else {
                    ($case_sensitive) ? natsort($temp) : natcasesort($temp);
                    if ($order != 'asc') {
                        $temp = array_reverse($temp, true);
                    }
                }
                foreach (array_keys($temp) as $key) {
                    (is_numeric($key)) ? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
                }
                return $sorted;
            }
            return $array;
        }   // end function sort()

...

        $sorted_tags = sort($tags, 'tag', 'asc', true);

Ich habe eine Amazon-Wishlist. wink Oder spende an das Projekt.
Ich kann, wenn ich will, aber wer will, dass ich muss, kann mich mal

Offline

#6 18.09.2021 07:13:04

florian
Administrator

Re: NWI - Tags - Drag & Drop reordering

@webbird Hm, I don't get it, sorry.

I have the array $tags with apfel, erdbeere, banane, which should be reordered to apfel, banane, erdbeere:

Array
(
     [2] => Array
        (
             [0] => 2
             [tag_id] => 2
             [1] => apfel
             [tag] => apfel
             [2] => 
             [tag_color] => 
             [3] => 11
             [section_id] => 11
             [4] => 2
        )

     [3] => Array
        (
             [0] => 3
             [tag_id] => 3
             [1] => erdbeere
             [tag] => erdbeere
             [2] => 
             [tag_color] => 
             [3] => 11
             [section_id] => 11
             [4] => 3
        )

     [4] => Array
        (
             [0] => 4
             [tag_id] => 4
             [1] => banane
             [tag] => banane
             [2] => 
             [tag_color] => 
             [3] => 11
             [section_id] => 11
             [4] => 4
        )

)

and I added the function (renamed to "tagsort") to the news_img/functions.inc.php.

But whatever I try:
tagsort($tags,1);
tagsort($tags,0);
tagsort($tags,'tag');
All this has no effect but only a notice in the error log:

2021-09-18T04:57:55+00:00 [Notice] /modules/news_img/functions.inc.php:[39] from /modules/news_img/modify_post.php:[130] mod_nwi_get_tags "Undefined index: "

Calling with full parameters, like
tagsort($tags,'tag','asc',false,false);
tagsort($tags,1,'asc',false,false);
makes the notice disappear, but does not sort the output either.

I also wonder why the native php "sort" function does not work (it does in function mod_nwi_get_tags_for_post, that's why the tags appear sorted in the frontend) does not work either.

Can you please provide some more assistance?

Beitrag geändert von florian (18.09.2021 07:14:18)


Code allein macht nicht glücklich. Jetzt spenden!

Offline

#7 20.09.2021 13:52:52

webbird
Administrator

Re: NWI - Tags - Drag & Drop reordering

To get the tags sorted on "Modify post" page, edit modify_post.php.

FIND
$tags = mod_nwi_get_tags($section_id);

AFTER, ADD
$tags = tagsort($tags, 'tag', 'asc', true);

The modify_post.php does not use mod_nwi_get_tags_for_post() but mod_nwi_get_tags() to get all tags. Then, it creates an array with assigned tags. So, if you wish to sort over ALL tags, you can add the tagsort() call to mod_nwi_get_tags() method.

The default sort() method does not work because the tags array is multilevel. The sort() method sorts by the key - which is, in this case, the auto incremented tag_id = order by addition.

Beitrag geändert von webbird (20.09.2021 13:55:56)


Ich habe eine Amazon-Wishlist. wink Oder spende an das Projekt.
Ich kann, wenn ich will, aber wer will, dass ich muss, kann mich mal

Offline

#8 21.09.2021 06:55:25

florian
Administrator

Re: NWI - Tags - Drag & Drop reordering

Great, thank you.
I've committed the changes to the NWI repo.
The functions.inc.php, modify.php and modify_post.php has to be edited. See commit for details:
https://github.com/WBCE/News-with-Image … 8723a68acf


Code allein macht nicht glücklich. Jetzt spenden!

Offline

Fußzeile des Forums

up