Item
[
class tree: Item
] [
index: Item
] [
all elements
]
README.txt
Todo List
Packages:
Formagic
Item
Renderer
Rule
Source for file Container.php
Documentation is available at
Container.php
<?php
/**
* Formagic
*
* LICENCE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at
* http://formagic.weasle.de/licence.txt
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to licence@weasle.de so we can send you a copy immediately.
*
*
@category
Formagic
*
@package
Item
*
@author
Florian Sonnenburg
*
@copyright
Copyright (c) 2007 Florian Sonnenburg
*
@license
http://formagic.weasle.de/licence.txt New BSD License
*
@revision
$Revision: 11 $
*/
/**
* Formagic Container Item class
*
* Changes on following flag will be applied recursively to all items added to
* the container object:
* - _isHidden
* - _isBlocked
* - _isDisabled
*
* Changes on the flag _isPostItem will only be applied to the container item
* itself (and defaults to false as there is no use posting a container item).
*
*
@category
Formagic
*
@package
Item
*
@author
Florian Sonnenburg
*
@copyright
Copyright (c) 2007 Florian Sonnenburg
*
@version
$Id: Container.php 11 2007-08-12 20:02:57Z meweasle $
*/
class
Formagic_Item_Container
extends
Formagic_Item
{
/**
* Pointer to items array section of formagic object
*
@var
string
**
*/
protected
$_items
=
array
(
)
;
/**
* Container headline
*
@var
string
**
*/
public
$headline
=
''
;
/**
* Tells renderer if container has its own surrounding template
*
@var
boolean
**
*/
public
$hasHtml
=
true
;
/**
* Generic argument handler
*
*
@param
string
$key
*
@param
mixed
$arg
*
@return
void
*/
protected
function
_handleArg
(
$key
,
$arg
)
{
switch
(
$key
)
{
case
'headline'
:
$this
->
headline
=
$arg
;
break
;
default
:
throw
new
Exception
(
"
Argument
type
'
$key
'
not
supported
"
)
;
}
// switch
}
/**
* OnCreate-Handler implementation
*
*
@return
boolean
*/
protected
function
_onCreate
(
)
{
parent
::
_onCreate
(
)
;
$this
->
isPostItem
=
false
;
$this
->
wide
=
true
;
$this
->
attributes
[
'id'
]
=
$this
->
id
;
return
true
;
}
/**
* Adds formagic item object to array of items.
*
* Creates new item object and adds this, or adds passed formagicItem object
*
*
@param
mixed
$type
String with item type or formagicItem object
*
@param
string
$name
String with item name. NULL if $type is formagicItem
* object
*
@param
array
$args
Array with additional item information. NULL if $type
* is formagicItem object
*
@return
object
formagicItem
object
*/
public
function
addItem
(
$type
,
$name
,
$args
=
null
)
{
try
{
// hand down status flags to added items
if
(
$this
->
_isHidden
)
{
$args
[
'hidden'
]
=
true
;
}
if
(
$this
->
_isBlocked
)
{
$args
[
'blocked'
]
=
true
;
}
$obj
=
&
$this
->
_caller
->
createItem
(
$type
,
$name
,
$args
)
;
$this
->
_items
[
$name
]
=
&
$obj
;
}
catch
(
Formagic_Exception
$e
)
{
$this
->
_caller
->
setMessage
(
$e
->
getMessage
(
)
,
Formagic
::
ERROR
)
;
return
false
;
}
return
$obj
;
}
/**
* Counts items added to self and all sub-containers
*
*
@return
integer
*/
public
function
countItems
(
)
{
$count
=
0
;
foreach
(
$this
->
_items
as
$item
)
{
if
(
$item
instanceOf
Formagic_Item_Container
)
{
$count
+=
$item
->
countItems
(
)
;
}
else
{
$count
+=
1
;
}
}
return
$count
;
}
/**
* Returns pointer to items array
*
*
@return
array
*/
public
function
&
getItems
(
)
{
return
$this
->
_items
;
}
/**
* Returns added item $name
*
* Throws Formagic_Exception if item does not exist
*
*
@throws
Formagic_Exception
*
@param
integer
$name
*
@return
Formagic_Item
*/
public
function
getItem
(
$name
)
{
if
(
isset
(
$this
->
_items
[
$name
]
))
{
return
$this
->
_items
[
$name
]
;
}
throw
new
Formagic_Exception
(
"
Item
'
$name
'
does
not
exist
in
container
"
.
$this
->
name
)
;
}
/**
* Blocks container item and all its descendants
*
*
@return
boolean
*/
public
function
block
(
)
{
$this
->
_isBlocked
=
true
;
$items
=
&
$this
->
getItems
(
)
;
foreach
(
$items
as
&
$item
)
{
$item
->
block
(
)
;
}
return
true
;
}
/**
* Sets hidden flag on container item and all its descendants
*
*
@return
boolean
*/
public
function
setHidden
(
$flag
)
{
$this
->
_isHidden
=
$flag
;
$items
=
&
$this
->
getItems
(
)
;
foreach
(
$items
as
&
$item
)
{
$item
->
setHidden
(
$flag
)
;
}
return
$flag
;
}
/**
* Disables container item and all its descendants and thus removes all
* involved items from form
*
*
@return
boolean
*/
public
function
disable
(
)
{
$this
->
_isDisabled
=
true
;
$items
=
&
$this
->
getItems
(
)
;
foreach
(
$items
as
&
$item
)
{
$item
->
disable
(
)
;
}
return
true
;
}
/**
* Returns values of all stored items.
*
*
@return
array
*/
public
function
&
getValue
(
)
{
$res
=
array
(
)
;
foreach
(
$this
->
getItems
(
)
as
$item
)
{
$res
[
$item
->
name
]
=
$item
->
getValue
(
)
;
}
return
$res
;
}
/**
* Returns HTML attributes as one assembled string
*
*
@return
string
*/
public
function
getAttributeStr
(
)
{
return
$this
->
_getAttributeStr
(
)
;
}
/**
* Validates containing items
*
* Iterates through all containing items. If any rule is violated, sub-item
* will return violated Formagic_Rule item and the container will pass it
* on.
*
* If all items pass violation, validate() calls onValidate-handler and
* returns its result.
*
*
@return
boolean
*/
public
function
validate
(
)
{
if
(
!
$this
->
_onPreValidate
(
))
{
return
false
;
}
foreach
(
$this
->
getItems
(
)
as
$item
)
{
$res
=
$item
->
validate
(
)
;
if
(
$res
instanceOf
Formagic_Rule
)
{
return
$res
;
}
}
return
$this
->
_onPostValidate
(
)
;
}
/**
* OnFetch-handler implementation
*
*
@return
void
*/
protected
function
_onFetch
(
)
{
foreach
(
$this
->
getItems
(
)
as
$item
)
{
$item
->
_onFetch
(
)
;
}
}
}
Documentation generated on Thu, 23 Aug 2007 00:29:35 +0200 by
phpDocumentor 1.4.0