Blog Component - added sorting by autor

David Exodus Posted in Component Development 6 years ago

Hello Folks

I hope someone can help me resolve that little error here.

I've modified the code of the file components/Blog/plugins/default/blog/pages/all.php to the following:

<div class="ossn-page-contents">
<div class="blog">
        <?php

        $blogs = $params['blogs'];
        $count = $params['count'];

            function cmp($a, $b) {

                return strcmp($a->owner_guid, $b->owner_guid);
            }
            usort($blogs, "cmp");

            if($blogs){
                foreach($blogs as $item){
                    echo ossn_plugin_view('blog/list/item', array('item' => $item)); 
                }
                echo ossn_view_pagination($count);
            }


        ?>
</div>

</div>

Now this Code sorts the blog posts by autot. But I've got a little problem. It does not take all blog posts to sort. It seems like after the count trigger has set for a second page, he sorts on the second page a new block of blog items instead of taking all the items and make a pagination stop in between the right order.

I'm sorry if this wasn't understandable, i'll post also images of what I mean:
the first page is correct as it should be

the first page is correct as it should be.
Now on the second page, this blog post should be in the first group on the first page:
this row should be in the first group

I appreciate any help on resolving this.

Replies
German Michael Zülsdorff Replied 6 years ago

I wil release a Blog update later today. It will come with your ideas included.

"All blogs" will show a list of blogs sorted by 'last edited' on top by default, but there's an option to list all blogs sorted by membername, too.

ch David Exodus Replied 6 years ago

Sorry, had the wrong code pasted for the edit.php file, its corrected now.
Yes i already thought about a separate entry. Guess then You need a date value passed to the item.php where it stands next to each blogpost and triggered by each save after edit. I will try that when I've got time for it. But I'm not quite sure how to pass this value over multiple files yet.

German Michael Zülsdorff Replied 6 years ago

Honestly i don't think there's a way to make a html input only partly editable.
So the timestamp should be saved and retrieved separately.

ch David Exodus Replied 6 years ago

Thanks for the hint. I guessed that it would be better if i change it there somehow, but wasn't sure about the class name and the attributes which can be used.

For the last edited part i did the following simple thing:

in components/Blog/plugins/default/forms/blog/add.php

add the variable $editeddate = date("d.m.Y");

the first < div > is changed:

<div>
<label><?php echo ossn_print('com:blog:blog:title');?></label>
<input type="text" name="title" value="<?php echo " (zuletzt bearbeitet: ". $editeddate.")"; ?>" />

</div>

and in file components/Blog/plugins/default/forms/blog/edit.php

also add the variable $editeddate = date("d.m.Y");

and change the first < div > to following:

<div>
<label><?php echo ossn_print('com:blog:blog:title');?></label>
<input type="text" name="title" value="<?php echo substr($params['blog']->title, 0, -33)." (zuletzt bearbeitet: ". $editeddate.")"; ?>"/>

</div>

now the problem is, if a user makes a new blog post, the last edited date ist already in the title bar, i had an user which deleted that part, so if he went to edit it. It would cut off his title. So I'm still figuring out how to make this value not editable.

German Michael Zülsdorff Replied 6 years ago

To put it this way: Your sorting is a little late. :)

To understand what's happening: OssnPagination doesn't fetch ALL records at once to display bundles of 10 entries later, it actually fetches only 10 records step by step in order to prevent memory from exhausting.
Thus, displaying page 1 will result in a query like .... LIMIT 0, 10, displaying page 2 will result in .... LIMIT 10, 10, and so on...

In other words: Your current sort is taking only those owner_guids into account which have been actually fetched by the current page request.

So, to really achieve what you wanted the sorting needs to be part of the SQL query itself and you simply have to change the class method like

public function getBlogs(array $params = array()) {
            return $this->searchObject(array_merge(array(
                    'type' => 'user',
                    'subtype' => 'blog',
                    'order_by' => 'owner_guid'
            ), $params));
    }

I'd appreciate if you would share your code for displaying the 'last edited' part.