Jump to content

Repeater, pages selector and "connected data"


moonwhaler
 Share

Recommended Posts

Hi there!

The title may be a bit misleading, or at least it's how I tried to achieve something which may not be possible in that combination. But let's start from the beginning: I'm trying to create a football team & game manager with processwire.

As someone who used Excel in all ways possible to manage every aspect of a football team (members, positions, games, etc.) and having used processwire for solely building websites in the past, I thought this has to work, too. And for the most part it does!

Yet, I have a few things which may work even better and that's where I'm stuck.

I currently manage:

  1. Team members (pages)
  2. Game positions (pages)
  3. Games (pages)

In "games" I have something like "location", "opponent", etc. and I have created a repeater field which includes "team member" (selector from page "team members") and "game position" (selector from "game positions"). I am able to map a "position" to a "team member" for each game. That's fine & well, BUT: it would nice, to have those selector being somehow connected to each other in a way, that if I am selecting a team member and assign him to a position, that this position & member will be deleted from other repeater "selectors".

This way I want to prevent a team member or game position to be used n-times in one game. It's just a usability thing, but a big one for me, since I may be not the person working with this in the future. :-[

Is this somehow possible?

Thank you!

  • Like 1
Link to comment
Share on other sites

I think you need to build a selector for your selection list with a pseudocode like: select all from teammembers that are not in games.position.teammembers

So, it depends on your fieldnames that you use in games.

Link to comment
Share on other sites

Do you need this to work on the frontend or in the admin part of processwire. On the frontend you can just use javascript to disable already selected players / positions and check on the backend, if nothing has gone wrong, before saving it. If you want to use the backend of processwire I think you've to build a custom InputfieldPage module, which does the job of disabling parts of the selectboxes to make positions / players unique to one game.

Link to comment
Share on other sites

Yep. It has to be some sort of "self-aware" selector. It should only display the names of the players / positions that not have been used yet.


Do you need this to work on the frontend or in the admin part of processwire. On the frontend you can just use javascript to disable already selected players / positions and check on the backend, if nothing has gone wrong, before saving it. If you want to use the backend of processwire I think you've to build a custom InputfieldPage module, which does the job of disabling parts of the selectboxes to make positions / players unique to one game.

Only in the admin part. :)

Link to comment
Share on other sites

Then you most likely need to write a own inputfield and/or whole fieldtype, as the only dependency current fieldtypes provide against other fields are dynamic required state and dynamic visibility. 

Edit: 

Another, less sophisticated, answer could also be to hook into page-save, check there if the requirements are met and just error if there are duplicate entries.

Link to comment
Share on other sites

After playing around a bit with PageTable I don't think this is a really good alternative to Repeater fields. I understand that it may be better performance wise, but Repeater are superior in terms of user interaction. A PageTable (as far as I understand it) is "just" another way of adding sub-pages to a page and then displaying them in a table on the parent page.

Repeaters, on the other side, are fast (looking at user input / UI wise) and responsive. Another thing is: I have to "name" a new page - in my case I have to name them "position1", "position2", etc. Repeaters don't need that (there's a automatic naming scheme #1, #2, ...)

Coming to the crux of my initial question: connected data. Since I really am a n00b, I didn't find the "setting" to create a custom code block for handling my "selector" drop downs. As far as I understand my situation, I have to create a new "field type" to reflect that. Am I right? :-[

Thank you!

Link to comment
Share on other sites

With you declaring yourself as pw noob I would strongly suggest, to give pagetables another try as this would be the far easier solution. 
 
From your post I assume that you're using the FieldtypeSelect module right now. This is a easy to begin fieldtype, but not very flexible. In your case you've to use the Page fieldtype. This is a type where you save a reference to the choosen page. This fieldtype can also be displayed as a select box, but also provides other input solutions.
 
The setup I would suggest would be:
 
Page Setup:
 
- Positions
    - …
- Players
    - …
- Games
    - Game 1 (game)

        - Line-Up Player 1 (lineup)

        - Line-Up Player 2

Template:

lineup

This template holds at least the first two fields below. With a custom naming convention you can define, you prevent the need of defining a custom name each time you add a page of this template.

 
Fields:
player
PageField

Here you can use the "custom php code for selectable pages" which is a option for that fieldtype:

$players = $pages->find("template=PLAYERTEMPLATE"); // All Players
$other_lineups = $page->parent->children; // All other Subpages, where Players are already choosen
// $page is one of the subpages, therefore the parent selector part

foreach($other_lineups as $lineup){
  $players->remove($lineup->player); // Remove unavailable Players
}

return $players; // Return the adjusted selection

position

PageField

Also use the custom php code, and just adapt my previous example for positions. 

lineup

PageTable

This pagetable is viewed in the game-template and let's you generate the lineup template, which should be used for the subpages of a game. 

The PageTable can show player.title and position.title in it's shown table, so you do have an overview in the game template, without the need to look into every subpage. 

With this setup everytime you add a new lineup subpage to a game, it removes already used positions and players from the fields.

  • Like 4
Link to comment
Share on other sites

Repeaters, on the other side, are fast (looking at user input / UI wise) and responsive. Another thing is: I have to "name" a new page - in my case I have to name them "position1", "position2", etc. Repeaters don't need that (there's a automatic naming scheme #1, #2, ...)

I agree that the PageTable field is not perfect in this sense, but there is a way around this:

Set up the Automatic Page Name Format option for the PageTable field and then go to the title field and adjust the settings in context of the template being used for the PageTable field content and uncheck required and set it to hidden. So it's still there, but at least it's not in the user's way. This way the page can be published without the title field.

Hope that helps a bit.

  • Like 3
Link to comment
Share on other sites

Hi guys,

here's a short list of my fields:

post-1971-0-08810500-1409259359_thumb.pn

and my current page structure:

post-1971-0-50693600-1409259359_thumb.pn

Sorry, it's german for the most part, but maybe you get the idea. Basically, there is a page "game list", "player list", "jersey list", "position list".

I already read that using simple "select" types is not recommended DB-wise, so I skipped that from the beginning. I mostly modelled the structure very similar to yours @LostKobrakai from the start.

Apart from that, I really like the "custom PHP code" approach. I didn't really saw that when using "Page" as field type. I will try that ASAP. So thank you for the kickstart! Also thanks to adrian for the heads up on the "Auto Page Name Format". I will definitely use that! ^-^

Link to comment
Share on other sites

Thank you LostKobrakai! This works like a charm.

One thing left though: when using "PageTable" and trying to add a new entry (Page) to the list, am I able to deny adding new "lineups" when there are no players / positions left to assign? Currently, I am able to add "blank" pages, since I have no more players in my Page selector.

Thanks again!

Link to comment
Share on other sites

This should be doable, but you need to hook into the pagecreation and if it's the "lineup" template, check if there are players/positions left for the current parent page.

I don't know how much you know about hooks, but this should provide a good introduction: http://processwire.com/api/hooks/. This should be the needed function to hook: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/Pages.php#L428

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