Glanced at your code, and you're right, it is pretty complex. Unfortunately I don't have time right now to really delve into it (have a rapid-fire series of meetings starting in about 15 minutes), so I'll show you the rough logic that I would use in this situation and maybe that will get you moving in the right direction.
First of all, I'd look at this as a sequence, not a tree. Yes, there are branches, but (at least the way you've described it) you'll always start at the same point and always end at the same point, with an indeterminate number of intermediate steps. (Okay, by strict letter-of-the-law interpretation, this may very well be a "tree" (I very nearly failed my Data Structures class), but I've found time and again that what works in my head doesn't work in formal language and vice-versa.)
So we end up with a list of steps. Walk through them one at a time. E.g.:
Code:
0: Page 1
1: Page 2
2: Page 3
Conditional branching, at it's simplest, would be an alternative page at one step, e.g.:
Code:
0: Page 1
1: condition?Page 2a:Page 2b
2: Page 3
The overall structure is unchanged - we still move from step 0 to step 1 and finally to step 2. The only difference is that at step 1 we check a condition and offer up alternative pages based on the outcome; the structure above could easily accommodate conditions with more than two possible outcomes (i.e. more than simple true/false conditions).
Of course, some branches might be longer than others, so we modify the above sequence to be as follows:
Code:
Sequence 0:
0: Page 1
1: condition?Sequence A:Sequence B
2: Page 3
Sequence A:
0: Page 2a1
1: Page 2a2
Sequence B:
0: Page 2b1
From this, we end up with two data structures: Pages and Sequences. A Page would hold all the data necessary to build a given page; we could easily apply inheritance to permit easily making minor differences in branched pages. A Sequence is slightly more tricky: it needs to hold Pages, Conditions (possibly a third data structure, but most likely just data within a Sequence), and Sequences. If we were doing this in C/C++, we'd be in trouble, but PHP's dynamic datatypes make this easy.
I'd implement a Sequence as a class with an array, and store each Page in sequence in that array; then when we need to stick in a Sequence instead of a Page, we just do that, no problem. We'd also need to store Conditions somehow, but that wouldn't be too hard.
I'll revisit this later, I gotta run to my meetings. (Where the hell is the Chandler room?? Why can't we all just use room numbers and be done with it????) PM me if you'd be interested in working more closely to make this project work (I also happen to use the Zend Framework, although only just started, and I also happen to be a current CS student).
Bookmarks