Jump to content
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 quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
    • By MateThemes
      Hello everyone!
      I am working with Processwire since some time. But some topics are quite hard for me.
      I have a Portfolio (Gallery) Page.
      I am build a template with Portfolio Index and pages with portfolio entries.
      Structure:
      Portfolio Index
      -- Portfolio Entry
      -- Portfolio Entry
      and so on.
      Portfolio Entry has an Image field with max 12 images and are accessible Templates. 
      Now I want to display the single Portfolio Entry on the Portfolio Index and Paginate them. In the index page all images of a single Entry page should be displayed (I should not be organized as albums, where a random image of the portfolio entry should be displayed). 
      I have no clue to achieve this. May someone could give me an advice.
      Thank you in advance!
    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
×
×
  • Create New...