Jump to content
kixe

Page sorting and moving via API

Recommended Posts

This morning I pushed a module to github which extends the page api with moving and sorting capability.

https://github.com/kixe/PageMove

 * CALLABLE (static & instance)
 * PageMove::execute($page, $parent = null, $newIndex = 0, $selector = 'all');
 * $modules->get('PageMove')->execute($page, $parent = null, $newIndex = 0, $selector = 'all');
 *
 * EXTENDED PAGE API
 * @method $page->move($parent, $newIndex = null, $selector = 'all')
 * @method $page->setIndex($newIndex, $selector = 'all') // set absolute index include=all
 * @method $page->getIndex($selector = '') // same as $page->index without argument @see getSelector()
 *
 * @method $page->moveFirst($parent = null)
 * @method $page->moveLast($parent = null)
 * @method $page->moveForward($steps = 1, $selector = 'all')
 * @method $page->moveBackwards($steps = 1, $selector = 'all')
 *
 * @property $page->moveFirst // same parent
 * @property $page->moveLast
 * @property $page->moveForward
 * @property $page->moveBackwards
 *
 * EXTENDED PAGES API
 * @method $pages->move($page, $parent = null, $newIndex = 0, $selector = 'all')
 * @method $pages->resortChildren($page, $selectorValue)
 * // same like core function $pages->sort($page, true); with capibility to change the sort condition

Have a nice weekend.

  • Like 8

Share this post


Link to post
Share on other sites

Is $page->first means move page to first position? If so, wouldn't be a more descriptive name better, eg moveFirst or similar?

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for input. I have been searching for something really short. I am also not sure about the modules name. Any recommendation welcome. :rolleyes:

I changed the method/ property names to moveFirst and moveLast.

  • Like 1

Share this post


Link to post
Share on other sites
53 minutes ago, LostKobrakai said:

Just so you don't miss them; there are sorting functions in the dev version of 3.0:

Of course I missed them. Thanks for the hint.

  • Like 1

Share this post


Link to post
Share on other sites

@LostKobrakai

I took a look in the core sorting functions. Unfortunately it doesn't work as expected and the $pages API reference is faulty and should be updated:

// set $page to have sort=5, moving any 5+ sort pages ahead
$pages->sort($page, 5); // works as expected

// same as above using alternate syntax
$page->sort = 5; // WILL NOT moving any 5+ sort pages ahead it just changes the sort value. Duplicates possible
$pages->sort($page); // WILL NOT set anything. This is a getter not a setter. Similar to $page->sort

In the $page API reference, $page->sort is described as follow which is faulty too.

Quote

Sort order of this page relative to siblings (applicable when manual sorting is used).

Use $page->index instead to get expected/ described result. But you cannot use $page->index as a setter!


Currently the only (nearly)  working solution needs the following 2 steps

$pages->sort($page->parent, true); // re-build sort values for children of $page, removing duplicates and gaps
$pages->sort($page, 5); // sort +=1 for all pages above

In this example you will get an unexpected result If the sort position related to the siblings was 4 before. There will be no change, because there is a gap at sort position 4 now.


What I would expect using this function

If I set sort I would expect a zero based position related to the page siblings.


Example

$pages->sort($page, 2);

3 page siblings having the sort values 4, 7, and 11. If I assign a sort value 2 to the page with the former sort value 7 I would expect the page moves to the end (last index number under 3 siblings), but currently it moves in the opposit direction and will be the first one related to its 2 siblings.

Nice to have in the future
(already available via PageMove module)

// move page to the first or last position related to its siblings
$page->moveFirst;
$page->moveLast;
$page->moveFirst($newParent = null);
$page->moveLast($newParent = null);

// move the page to an index position relative to its siblings, optionally change the parent too in one step
$page->move($parent, $newIndex = null, $selector = 'all')

// or simply
$page->setIndex($newIndex, $selector = 'all');


Expecting a core update related to this I will not push the module to the modules directory. Feel free to pull it from github directly until this is available from core.

 

 



 

Edited by kixe
module update 1.0.2
  • Like 2

Share this post


Link to post
Share on other sites

$page->sort always worked this way, which is why most people simply avoided changing it most of the time. It might need some clarification now that there is a function which does prevent duplicate sort values.

13 minutes ago, kixe said:

What I would expect using this function

If I set sort I would expect a zero based position related to the page siblings.

Rebuilding all sort values is a waist of db computation. Imagine you've 10000 siblings and you reorder just the 9999 to be the last one. If there are gaps in the first siblings sort values it'll rebuild all 10000 instead of just a few pages.

16 minutes ago, kixe said:

Example


$pages->sort($page, 2);

3 page siblings having the sort values 4, 7, and 11. If I assign a sort value 2 to the page with the former sort value 7 I would expect the page moves to the end (last index number under 3 siblings), but currently it moves in the opposit direction and will be the first one related to its 2 siblings.

I think you expect to set the new index of the page not the sort value, which in ProcessWire are currently different things. I see that it might be confusing, but mostly because people rarely dealt with that issue by now as sorting was mostly done via the GUI / modules.

It would be great if you could formulate those issue you had in a github issue, so that ryan can improve on them. 

  • Like 2

Share this post


Link to post
Share on other sites
19 hours ago, LostKobrakai said:

Rebuilding all sort values is a waist of db computation. Imagine you've 10000 siblings and you reorder just the 9999 to be the last one. If there are gaps in the first siblings sort values it'll rebuild all 10000 instead of just a few pages.

I completely agree. I don't mind about the absolute sort value, but the page index should be in the right place. In my example it is enough if the page with previous sort value 7 get the index 2, which is the fact if a sort value of 12 is assigned (one more than 11)


For handling pages the absolute sort value is not of interest it does say nothing, the index position is the important value.
So it would be nice in addition to get the index via $page->index  having a setting method.

19 hours ago, LostKobrakai said:

It would be great if you could formulate those issue you had in a github issue, so that ryan can improve on them. 

Of course I will do that.
https://github.com/processwire/processwire-issues/issues/225

  • Like 1

Share this post


Link to post
Share on other sites

I pushed a revision 1.0.2 of the module with extended capability and more delimited from core functions (still using them if possible).

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Gadgetto
      Status update links (inside this thread) for SnipWire development will be always posted here:
      2019-08-08
      2019-06-15
      2019-06-02
      2019-05-25
      If you are interested, you can test the current state of development:
      https://github.com/gadgetto/SnipWire
      Please note that the software is not yet intended for use in a production system (alpha version).
      If you like, you can also submit feature requests and suggestions for improvement. I also accept pull requests.
      ---- INITIAL POST FROM 2019-05-25 ----
      I wanted to let you know that I am currently working on a new ProcessWire module that fully integrates the Snipcart Shopping Cart System into ProcessWire. (this is a customer project, so I had to postpone the development of my other module GroupMailer).
      The new module SnipWire offers full integration of the Snipcart Shopping Cart System into ProcessWire.
      Here are some highlights:
      simple setup with (optional) pre-installed templates, product fields, sample products (quasi a complete shop system to get started immediately) store dashboard with all data from the snipcart system (no change to the snipcart dashboard itself required) Integrated REST API for controlling and querying snipcart data webhooks to trigger events from Snipcart (new order, new customer, etc.) multi currency support self-defined/configurable tax rates etc. Development is already well advanced and I plan to release the module in the next 2-3 months.
      I'm not sure yet if this will be a "Pro" module or if it will be made available for free.
      I would be grateful for suggestions and hints!
      (please have a look at the screenshots to get an idea what I'm talking about)
       




    • By VeiJari
      Hello forum, this is my first security related post, so I'm a bit of a newbie.
      I understand that when I have direct front-input from user I should sanitize the input, but how about when I use a secret key for showing a API for a third-party supplier? Should I sanitize the input->get() key?
      I've tested this issue and I tried ?key=<?php echo $page->field; ?> And without adding any sanitization it comes back: /?key=<?php%20echo%20$page->field;%20?>
      So can I rely on this, or should I still use $sanitizer just in case?
       
      Thanks for the help!
    • By apeisa
      Update 31.7.2019: AdminBar is now maintained by @teppo. Modules directory entry has been updated, as well as the "grab the code" link below.
      ***
      Latest screencast: http://www.screencas...73-ab3ba1fea30c
      Grab the code: https://github.com/teppokoivula/AdminBar
      ***
      I put this Adminbar thingy (from here: http://processwire.c...topic,50.0.html) to modules section and to it's own topic.
      I recorded quick and messy screencast (really, my first screencast ever) to show what I have made so far. You can see it from here: http://www.screencas...18-1bc0d49841b4
      When the modal goes off, I click on the "dark side". I make it so fast on screencast, so it might seem a little bit confusing. Current way is, that you can edit, go back to see the site (without saving anything), continue editing and save. After that you still have the edit window, but if you click "dark side" after saving, then the whole page will be reloaded and you see new edits live.
      I am not sure if that is best way: there are some strengths in this thinking, but it is probably better that after saving there shouldn't be a possibility to continue editing. It might confuse because then if you make edits, click on dark side -> *page refresh* -> You lose your edits.
      ***
      When I get my "starting module" from Ryan, I will turn this into real module. Now I had to make some little tweaks to ProcessPageEdit.module (to keep modal after form submits). These probably won't hurt anything:
      if($this->redirectUrl) $this->session->redirect($this->redirectUrl); if(!empty($_GET['modal'])) $this->session->redirect("./?id={$this->page->id}&modal=true"); // NEW LINE else $this->session->redirect("./?id={$this->page->id}");   and...
      if(!empty($_GET['modal'])) { $form->attr('action', './?id=' . $this->id . '&modal=true'); } else { $form->attr('action', './?id=' . $this->id); // OLD LINE }  
    • By Mike Rockett
      Jumplinks for ProcessWire
      Release: 1.5.54
      Composer: rockett/jumplinks
      Jumplinks is an enhanced version of the original ProcessRedirects by Antti Peisa.
      The Process module manages your permanent and temporary redirects (we'll call these "jumplinks" from now on, unless in reference to redirects from another module), useful for when you're migrating over to ProcessWire from another system/platform. Each jumplink supports wildcards, shortening the time needed to create them.
      Unlike similar modules for other platforms, wildcards in Jumplinks are much easier to work with, as Regular Expressions are not fully exposed. Instead, parameters wrapped in curly braces are used - these are described in the documentation.
      Under Development: 2.0, to be powered by FastRoute
      As of version 1.5.0, Jumplinks requires at least ProcessWire 2.6.1 to run.
      View on GitLab
      Download via the Modules Directory
      Read the docs
      Features
      The most prominent features include:
      Basic jumplinks (from one fixed route to another) Parameter-based wildcards with "Smart" equivalents Mapping Collections (for converting ID-based routes to their named-equivalents without the need to create multiple jumplinks) Destination Selectors (for finding and redirecting to pages containing legacy location information) Timed Activation (activate and/or deactivate jumplinks at specific times) 404-Monitor (for creating jumplinks based on 404 hits) Additionally, the following features may come in handy:
      Stale jumplink management Legacy domain support for slow migrations An importer (from CSV or ProcessRedirects) Feedback & Feature Requests
      I’d love to know what you think of this module. Please provide some feedback on the module as a whole, or even regarding smaller things that make it whole. Also, please feel free to submit feature requests and their use-cases.
      Note: Features requested so far have been added to the to-do list, and will be added to 2.0, and not the current dev/master branches.
      Open Source

      Jumplinks is an open-source project, and is free to use. In fact, Jumplinks will always be open-source, and will always remain free to use. Forever. If you would like to support the development of Jumplinks, please consider making a small donation via PayPal.
      Enjoy! :)
×
×
  • Create New...