choppingblock

How to get children/subpages of a user

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...

Share this post


Link to post
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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

I'd say you're most often looking for orders of a user, so I'd add a page field to the user template, where all orders of that user are added to. Then you can always call something like $user->orders->find("price>50").

Share this post


Link to post
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...)?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I've always wondered what these are for... :)

I think I have to go with a custom module though. I'm sure I'll be back soon with more questions. ^_^

Thanks a lot for all the useful info!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Harmen
      I want to add a few pages to an AsmSelect Page field inside a repeater using the following code:
      $trialsPage = wire("pages")->get(28422); // Get the page $trialsPage->of(false); $newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater foreach ($selectedProducts as $selectedProduct){ $productPage = $pages->get("template=product, reference=$selectedProduct"); $newTrial->trial_selected_products->add($productPage); } $newTrial->save(); $trialsPage->save(); However, when I check the page where the field is located it doesn't have the new values as expected. The selected pages exist, the field is in the right location, made sure that the output formatting is turned off: $page->of(false); But it still doesn't work with a variable. No matter what I try, it doesn't work.
      It only works when I replace $selectedProduct with a hardcoded string. Am I doing something wrong here?
    • By louisstephens
      So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?
      So after rereading my post, I believe it is a bit vague so I will try to explain more. 
      The Goal:
      Create a page with a template "one". Once the page is created/saved => create a new child page with the template of "two" If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page) The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.
    • By Marcel
      Hey,
      I am about to use the tool Create Users Batcher. We have 450 users. When I tryed it with like 20 test users it worked but it took a while. Now my concerns are that 450 might be to stressful? Does anyone have experience with several hundreds of users? My thoughts are to split it in smaller goups and upload them one group after another.
      best wishes
      marcel
    • By Peter Knight
      Is there a way to make JPGs progressive by default via the API?
      I've added the following to my site/config.php file but user-uploaded images are often displayed as non progressive.
      $config->imageSizerOptions = array( 'upscaling' => true, // upscale if necessary to reach target size? 'cropping' => true, // crop if necessary to reach target size? 'autoRotation' => true, // automatically correct orientation? 'interlace' => true, // use interlaced JPEGs by default? (recommended) 'sharpening' => 'soft', // sharpening: none | soft | medium | strong 'quality' => 95, // quality: 1-100 where higher is better but bigger 'hidpiQuality' => 60, // Same as above quality setting, but specific to hidpi images 'defaultGamma' => 0.5, // defaultGamma: 0.5 to 4.0 or -1 to disable gamma correction (default=2.0) ); Thanks
    • By louisstephens
      From my last post, I was given a good idea on how to count the repeater items, and it worked wonderfully. I got my code working well and the columns (based on the count) all work well as well. Now, I have a head scratcher on my hands. 
      <?php $buttonsIncluded = $page->special_custom_buttons->find('special_custom_buttons_include=1'); $buttonsIncludedCount = count($buttonsIncluded); $buttonsIncludedCountAdditional = $buttonsIncludedCount +1; echo $buttonsIncludedCount; ?> <div class="row"> <?php foreach($buttonsIncluded as $button): ?> <?php if($button->custom_buttons_include): ?> <?php if($buttonsIncludedCountAdditional == 2): ?> <div class="col-6"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 3): ?> <div class="col-4"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 4): ?> <div class="col-3"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php endif; ?> <?php endif; ?> <?php endforeach; ?> </div> All of this is included in a larger foreach statement that is pulling in other data (like body copy etc etc) from a Page Table field. As you can see in my code above, I am adding "1" to the count, so I can have space in the grid layout for a new button.
      So, right now: it looks something like: 
      [repeater button] [repeater button] [repeater button] [space for new button] What I really need to do is to pull in the button from the Page Table and add it into the new space so it looks like:
      [repeater button] [repeater button] [repeater button] [button from Page Table] Is this even possible todo, or is there a better way to go about this? 
       
      *Edit*
      So, I really just overlooked something quite easy here. Since the grid is based on 12 columns, I could just take 12 and divide by $buttonsIncludedCountAdditional which would give me the remaining col width to use outside the foreach loop. I was trying to make this too complicated.