Re: [rwfphp-developers] MultiContainer
Status: Beta
Brought to you by:
kaibab
|
From: Renaun E. <re...@rw...> - 2004-09-13 21:52:22
|
I am glad this code worked somewhat, the overloading of $controls must
have been the source of some of the problems.
For the button name problem with the Listing control. Add this line
"$this->{$control->Name} = &$control;" in the MultiContainer class file.
[code] in the MultiContainer.rwf.php class file
function addControl($control) {
$this->{$control->Name} = &$control;
$this->AddControls( array($control->Name => &$control));
// Sanity check: when we add the 1st control its automagically
activated...
if ($this->select == '') $this->setVisible($control->Name);
}
[/code]
That seems to make it work. I have some code that i used to test it if
you want to see it, let me know. I think i understand what you are
trying to do now. Its a nice approach, although I am not sure what
$this->{$control->Name} = &$control; will do in terms of references and
it pointing to another object's attribute. PHP is funny and you can do
crazy things like this just not sure what it will cause later, but worth
trying. This idea leads to some interesting ideas for some controls.
The issue is that the $control is created as an attribute to some other
control then MultiContainer. When the MultiContainer class goes through
its Controls attribute it finds that the added $control's are not an
attribute of MultiContainer. Thus I added the line to force an
attribute in the scope of MultiContainer. Rwfphp is very touchy on the
relationships between Controls and their parent controls. All the links
have to be correct so one control can find its parent control. Well I
am probably not making sense of this but I tried to explain it. I'll
have to write more up on it another day.
Other approaches to this problem are using the "SetTemplateMapping"
function in the TemplateControl class.
In you class that would create your "welkom" and "klantlisting"
controls, create the two controls like normal. Then set the mapping
like this:
$this->SetTemplateMapping( array('currentControl','klantlisting') );
or
$this->SetTemplateMapping( array('currentControl','welkom') );
Then in the the Smarty template just use "currentControl" instead of
"welkom" or "klantlisting".
Your approache lends it self to more specific abstraction and reuse for
future implementations. The approach above is a quick and dirty
approach i guess. But its good to bring this up as it points out
another function in the rwfphp classes. Need ppl to write some articles
that point out functionality that rwfphp has. The articles I have
written just don't do it justice.
Renaun
Berend Dekens wrote:
> Hi Renaun,
>
> I understand what you mean with the directory variable but thats not
> what I'm trying to do. I got a page with 2 menu controls and one main
> control that shows a listing or an entry detail (its a kind of
> database frontend). So I want one control variable to display one
> control but which control is not fixed. (I could add the desired
> control by using one name for every type but that can be confusing
> while programming).
>
> I'm now using this version which i modified to use the Controls array
> from the Control class. Allthough it seems to work (I can select a
> control to view) it still messes up the button name...
>
> class MultiContainer extends WebControl {
> /*
> Radical change: lets store the controls by name instead by
> index. Nobody cares as you'll allways use the name anyway :)
> */
> var $select; // This is the name of the selected control
> function MultiContainer() {
> WebControl::WebControl();
> $this->select = '';
> }
>
> function addControl($control) {
> $this->AddControls( array($control->Name => &$control));
>
> // Sanity check: when we add the 1st control its automagically
> activated...
> if ($this->select == '') $this->setVisible($control->Name);
> }
>
> /*
> Make a control active
> Note: when we get an invalid name we simply ignore it
> */
> function setVisible(/* Control name */ $name) {
> // $this->Controls is from the Control class
> if(isset($this->Controls[$name])) {
> // We know the selection is valid, now select it
> $this->select = $name;
> } else {
> $this->rwfERROR("MultiContainer error: could not select
> '{$name}' as active control as it does not exists",__FILE__,__LINE__);
> }
> }
>
> function GetTemplate($smarty) {
> if($this->select == '') {
> $this->rwfERROR("MultiContainer error: empty
> container",__FILE__,__LINE__);
> return "[Empty container]";
> }
> if (!isset($this->Controls[$this->select])) {
> $this->rwfERROR("MultiContainer error: could not find
> '{$this->name}'",__FILE__,__LINE__);
> return "[MultiContainer error: Object not found!]";
> }
> return $this->Controls[$this->select]->GetTemplate($smarty);
> }
> }
>
> When I run it i get a few errors that disappear when I use the menu to
> switch controls...
>
> [rwfphp error: Control does not exist. [Control: welkom] (File:
> Control.class.php Line: 164)]
> [rwfphp error: Control does not exist. [Control: klantlisting] (File:
> Control.class.php Line: 164)]
>
> Berend
|