Jump to content

How to get children/subpages of a user


choppingblock
 Share

Recommended Posts

I have started to use PW 3 in my latest project and it's working out really great (as expected).

I have one issue though which I can't figure out.

I am developing an order system, where users can log in, select a product, make some configurations to it and then order the product.

The orders are saved as child pages to each user. So far, no problem.

What I am trying to achieve is to put the orders in a subpage of the user called "orders", for better clarity in the users' structure. So i create a subpage "orders" with the first order of a user, and save the order as a child of this.

- user

- - orders

- - - order1

- - - order2

- - - order3

The problem occurs when the user submits more orders: I can't get the "orders" subpage to put the new order inside it, so an additional "orders" subpage is created with each new order.

- user

- - orders

- - - order1

- - orders

- - - order2

- - orders

- - - order3

This is my approach:

    // get the first child of the user page with the template "orders"
    // apparently this does not work as I expected and always returns a NullPage
    $parent = $user->child("template=orders"); 

    // if "orders" subpage is a NullPage, create new page
    if ($parent instanceof NullPage) {
        $orderContainer = new Page();
        $orderContainer->parent = $user;
        $orderContainer->template = "orders";
        $orderContainer->title = "Orders";
        $orderContainer->save();
        $parent = $orderContainer;
    }
    
    $order = new Page();
    $order->template = "product-configure";
    $order->title = "Order: " . $order_name;
    $order->parent = $parent;

    $order->country = $country;
    $order->publication = $publication;
    $order->comments = $comments;   

    $order->save();

Any hints are appreciated...

Link to comment
Share on other sites

You probably shouldn't store site content inside the Admin folder, as there are various pages, which are hidden, not viewable by non-superuser and such things. So you'd need to use include=hidden or check_access=0 just to make things work. I'm personally not a fan of this, as any errors made in selectors could lead to being a security hole. I'd rather use a page-field to link users to orders and store the orders under a different parent outside of the admin branch.

  • Like 3
Link to comment
Share on other sites

Good point.

So then I would create a page "orders" with restricted access, create reference-pages for each user who submits orders and store the orders as child pages of each reference page...

- orders

- - user_reference1

- - - order1

- - - order2

- - user_reference2

- - - order1

Now I just need to have these orders at hand when I open a user page... I think I'll figure that out.

Thanks!

Link to comment
Share on other sites

I think there should be an identifier field for the assigned user on the order ($page->user_id or something), so that each order knows to which user it belongs (many-to-one relation).

I could fetch all orders of a user by $pages->find("template=order, user_id=$user").

Now I would like to have these orders listed under a new tab in each users template (in addition to the "Content" and "Children" tabs I get when I edit a user).

I don't understand how the system templates are created. Except for the admin template, they don't even use template files. I can change the labels for existing tabs under "Advanced", but how can I add new tabs (and add custom content to them...)?

Link to comment
Share on other sites

Just use FieldsetTabOpen and FieldsetClose as normal on the user template, sandwiching the fields you want on a separate tab in between these two. Get to the user template by filtering  for 'show system templates'

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

Hello,

I am new with porcesswire. Could anybody explain how to get a page list to the user-tab.

I have a page abo with the childs for each ordered abo:

abo
-- order1
-- order2
-- .....

The order-pages (order1, order2,...) do have the following fields:

  • title (should I make it not global and remove?)
  • user (referenze to user-page)
  • course (reference to course (abo))
  • ... and some other

How can I show a list of orders related to the user in the user-tab? I made a tab with a page-reference field. But then I have to select the order-pages. What field type is to use to get a preselected list of all orders of this user and how can I do the preselection?

Thanks

Link to comment
Share on other sites

Hi @pout, welcome to the PW forums. 🙂

If I understand right you have a Page Reference field "user" in your "order" template, and a Page Reference field "orders" in your "user" template. You can use the Connect Page Fields module to link these two Page Reference fields together - see the module readme for more information.

Link to comment
Share on other sites

  • 1 month later...

Thanks, that module works good!

But I think I, don't need this two way binding. I only would need a linked list of the order-pages where the user is selected in the user field of the order-page at the user page.

Is this possible? Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...