Class Gadget

Description

Gadget

The static Gadget class provides all methods you will usually need to perform the most tasks needed to be done while working with the Gadget WDK.

It provides methods for retreiving/manipulating configuration, cookies, user preferences and "the current user". There are two methods that need to be called at script start, respectively at script end, namely Gadget::start() and Gadget::finish(), which will initialize Gadget's infrastructure and render all instantiated widgets. See the documentation of the methods itself for more information.

Example: A basic Gadget powered page:

  1. require_once 'Gadget.php';
  2.  
  3. Gadget::start();
  4.  
  5. // display login box if
  6. // visitor is not logged in
  7. if (!Gadget::currentUser()) {
  8. Gadget::newWidget('LoginBox');
  9. }
  10.  
  11. // display intro if user is not
  12. // logged in or it's his preference
  13. if (!Gadget::currentUser() or Gadget::getPref('display_intro')) {
  14. Gadget::newWidget(array('template' => 'intro.html'));
  15. }
  16.  
  17. // display home page
  18. Gadget::newWidget(array('template' => 'index.html'));
  19.  
  20. // build a tiny menu
  21. // NOTE that the point when you have to build the menu
  22. // depends on your chosen layout;
  23. $menu = &Gadget::newWidget('Menu');
  24. $sect = &$menu->addSection('Navigation');
  25. $sect->addLink('Home', '/');
  26. $sect->addLink('About', '/about.php');
  27. $sect = &$menu->addSection('Downloads', '/downloads/');
  28. $sect->addLink('GNU/Linux', '/downloads/linux.php');
  29. $sect->addLink('MS Win32', '/downloads/wikndows.php');
  30.  
  31. Gadget::finish();

  • final:
  • static:
  • since: 1.0

Located in /Gadget.php (line 146)


	
			
Method Summary
object Breadcrumbs &breadcrumbs ([array $options = array()])
mixed callProviderGet (string $provider, [array $args = array()])
mixed callProviderSet (string $provider, [array $args = array()])
mixed currentUser ([mixed $get = false])
mixed &DAO ([string $DAO = null])
string delQ ([array $params = array()], [bool $persistent = true])
void finish ([bool $footer = true], [array $footerOptions = array()])
mixed getConf ([string $conf = null], [string $part = null])
object|false getCurrentUser ()
mixed getPref ([string $pref = null])
mixed getQ ([string $name = null], [string $type = null], [mixed $def = null], [bool $del = false], [mixed $str = 'strip_tags'])
void handlePearError (object PEAR_Error &$e)
object PageHeader &header ([array $options = array()])
object I18Nv2_Locale &locale ()
string location ()
void log (string $source, mixed $message)
void login (int $userId)
void logout ([mixed $redirect = '/'])
string modQ ([array $params = array()], [bool $persistent = true])
object &newWidget (mixed $widget, [array $options = array()])
int onError ([int $mode = null])
int onErrorAdd (int $mode)
int onErrorSub (int $mode)
object &persistentObject (string $class, [mixed $params = null], [string $file = null])
object &persistentWidget (string $widget, [mixed $options = null])
object Gadget_Provider &provider (string $provider)
mixed runService (string $service, mixed $args,)
object Gadget_Service &service (string $service)
mixed setConf (string $conf, array $parts)
mixed setCurrentUser (int $id)
mixed setPref (array $pref)
object PageHeader &start ([bool $header = true], [array $headerOptions = array()])
object PageHeader &startI18N ([bool $header = true], [array $headerOptions = array()])
float timer ([mixed $to_least = false])
Methods
breadcrumbs (line 724)

Breadcrumbs

This returns the Breadcrumbs widget instance.

object Breadcrumbs &breadcrumbs ([array $options = array()])
  • array $options: Options for the Breadcrumb widget.
callProviderGet (line 703)

Call Provider Get

This executes the get() method of a certain Gadget_Provider and returns the results of the method call. Note that the arguments will be passed to call_user_func_array().

mixed callProviderGet (string $provider, [array $args = array()])
  • string $provider: The provider to instantiate.
  • array $args: Argument to pass through to Gadget_Provider::get()
callProviderSet (line 681)

Call Provider Set

This executes the set() method of a certain Gadget_Provider and returns the results of the method call. Note that the arguments will be passed to call_user_func_array().

mixed callProviderSet (string $provider, [array $args = array()])
  • string $provider: The provider to instantiate.
  • array $args: Argument to pass through to Gadget_Provider::set()
currentUser (line 246)

Current User

Retreive an attribute of the current user (defaults to users id). If $get === true the users DataObject will be returned. Returns 0 if the user is anonym.

mixed currentUser ([mixed $get = false])
  • mixed $get: The name of the attribute or true for the whole user object.
DAO (line 783)

Setup a DB_DataObject

This calls DB_DataObject::factory() to create a new entity unless called without argument, then it just does the necessary work to properly setup DB_DataObject by retreiving its options through Gadget::getConf('Db_DataObject').

mixed &DAO ([string $DAO = null])
  • string $DAO: Name of the database table.
delQ (line 956)

Remove Params from QueryString

This operates directly on $_SERVER['QUERY_STRING'] and removes parameters specified by $params. This is very useful if you ise Apache's mod_rewrite or similar hooks to prettify URLs. It returns the modifed query string.

  1. // assume that the pretty URI /home/user/mike?a=1&b=3
  2. // will be rewritten to /home.php?user=mike&a=1&b=3
  3. Gadget::delQ(array('user'));
  4. // now the query string can be appended to

string delQ ([array $params = array()], [bool $persistent = true])
  • array $params: Array of URL parameters to remove.
  • bool $persistent: Whether to write the modified query string back to $_SERVER['QUERY_STRING'].
finish (line 484)

Finish

Gadget::finish() must be called at sript/page end. It renders all widgets remaining in the widget queue one after another (unless $widget->show is FALSE). By default the PageFooter widget will be instantiated right before the widget queue gets rendered.

void finish ([bool $footer = true], [array $footerOptions = array()])
  • bool $footer: Whether to instantiate the PageFooter widget.
  • array $footerOptions: Options for the PageFooter widget.
getConf (line 165)

Get Config

Retreive a configuration setting. In the default setup where Gadget_Provider_Configuration is used, the settings are stored within a plain INI file. Define the GADGET_CONFIG constant to point to your INI configuration file.

mixed getConf ([string $conf = null], [string $part = null])
  • string $conf: Configuration section
  • string $part: Configuration directive
getCurrentUser (line 279)

Get Current User

Retreive the current users DataObject.

object|false getCurrentUser ()
getPref (line 206)

Get Preferences

Retreive a preferences item or all preferences if $pref is NULL. If the user has no preferences yet or the user is anonym the default preferences (defined by configuration) are being returned.

mixed getPref ([string $pref = null])
  • string $pref: Preferences item
getQ (line 874)

Get Params from QueryString

  • return: Requested value of the query string or specified default.
  • access: public
  • static:
mixed getQ ([string $name = null], [string $type = null], [mixed $def = null], [bool $del = false], [mixed $str = 'strip_tags'])
  • string $name: The name of the GET parameter.
  • string $type: The type the value should be casted to.
  • mixed $def: The default value.
  • bool $del: Whether the param/value pair should be stripped from the query string.
  • mixed $str: Callback for sanitizing strings; Note that arrays and object members will be sanitized, too.
handlePearError (line 1096)

Handle PEAR Error

  • access: public
  • static:
void handlePearError (object PEAR_Error &$e)
  • object PEAR_Error $e
header (line 742)

PageHeader

This returns the PageHeader instance.

object PageHeader &header ([array $options = array()])
  • array $options: Options for the PageHeader widget.
locale (line 761)

Locale

This returns the I18Nv2_Locale instance configured with the locale/language retreived by Gadget::getPref('language').

  • access: public
  • static:
  • see: Gadget::persistenObject(), I18Nv2 (http://pear.php.net/I18Nv2)
object I18Nv2_Locale &locale ()
location (line 1009)

Location

Get the current location (being the request URI without query string).

string location ()
log (line 1139)

Log

  • access: public
  • static:
void log (string $source, mixed $message)
  • string $source
  • mixed $message
login (line 818)

Login

Performs the login task. It will regenerate the session id, set the "current user" and redirect to $_GET['uri'] if this parameter is present.

  • access: public
void login (int $userId)
  • int $userId: The id of the user to be logged in.
logout (line 839)

Logout

Performs the logout task. It'll regenerate the session id, delete all session variables and finally destroy the session. It will further disable autologin (delete the autologin cookie) and redirect to the home page.

  • access: public
void logout ([mixed $redirect = '/'])
modQ (line 975)

Modify QueryString

  • return: The modifed query string
  • access: public
  • static:
string modQ ([array $params = array()], [bool $persistent = true])
  • array $params: Array of URL parameters to remove.
  • bool $persistent: Whether to write the modified query string back to $_SERVER['QUERY_STRING'].
newWidget (line 531)

New Widget

This will instantiate and return a new widget $widget, configured by $options. You need to have a directory named "Widgets" in your include path where your custom widgets reside. If called only with one parameter being an array, a Gadget_Widget will be returned and the first parameter will be passed through to Gadget_Widget::setOptions().

object &newWidget (mixed $widget, [array $options = array()])
  • mixed $widget
  • array $options
onError (line 1040)

Set Error Handling

Configure what action to take if a PEAR error occurs. Returns the previous error mode.

  1. // display errors
  2. Gadget::onError(E_DISPLAY);
  3. // display and log errors
  4. Gadget::onError(E_DISPLAY | E_LOG);

int onError ([int $mode = null])
  • int $mode: New error mode (bitmask).
onErrorAdd (line 1060)

Add Error Mode

Add error mode to the actual error handling. Returns the previous error mode.

  • return: Previous error mode (bitmask).
  • access: public
  • static:
  • see: Gadget::onError()
int onErrorAdd (int $mode)
  • int $mode: Error mode to add (bitmask).
onErrorSub (line 1080)

Sub Error Mode

Substract error mode from the actual error handling. Returns the previous error mode.

  • return: Previous error mode (bitmask).
  • access: public
  • static:
  • see: Gadget::onError()
int onErrorSub (int $mode)
  • int $mode: Error mode to substract (bitmask).
persistentObject (line 556)

Create an persistent Object

This is a general purpose singleton, which can instantiate classes named after the PEAR coding standard or from file $file. Note that objects instantiated through this method are kept alive and may be used after their first instantiation. Also note that these classes may only require one construction parameter.

  • access: public
  • static:
object &persistentObject (string $class, [mixed $params = null], [string $file = null])
  • string $class
  • mixed $params
  • string $file
persistentWidget (line 585)

Create an persistent Widget

This created persitent widgets like the PageHeader. This is necessary to be able to add style sheets, javascripts, links etc. to the head section within any widget by retreiving the PageHeader widget through &Gadget::header();.

object &persistentWidget (string $widget, [mixed $options = null])
  • string $widget
  • mixed $options
provider (line 638)

Provider

Provider singleton. Returns *the* instance of a certain Gadget_Provider object. It's basically a shorthand for:

  1. require_once 'Gadget/Provider.php';
  2. Gadget_Provider::singleton($provider);

object Gadget_Provider &provider (string $provider)
  • string $provider: Name of the Provider.
runService (line 658)

Execute Service

This executes the run() method of a certain Gadget_Service and returns the results of the method call.

mixed runService (string $service, mixed $args,)
  • string $service: The service to run.
  • mixed $args,: ... Arguments to pass through to Service::run()
service (line 615)

Service

Service singleton. Returns *the* instance of a certain Gadget_Service object. It's basically a shorthand for:

  1. require_once 'Gadget/Service.php';
  2. Gadget_Service::singleton($service);
Note that it does *NOT* call the run() method of the service.

object Gadget_Service &service (string $service)
  • string $service: Name of the Service.
setConf (line 185)

Set Config

Set a configuration setting. In the default setup where Gadget_Provider_Configuration is used, this only overrides configuration for the time the current script is executed.

mixed setConf (string $conf, array $parts)
  • string $conf: Configuration section
  • array $parts: Configuration directives
setCurrentUser (line 263)

Set Current User

Set the current user through its id.

mixed setCurrentUser (int $id)
  • int $id: Users id.
setPref (line 224)

Set Preferences

Modify the users preferences. If the user is anonym the modified preferences will be stored in the session.

mixed setPref (array $pref)
  • array $pref: Preferences items.
start (line 326)

Start

This starts Gadget and is usually called at the very beginning of your page/script. It does the following in right this order:

  • define TIME constant holding the current unix timestamp
  • sets a cookie named "first" if not already set
  • filter multiple ampersands (&&&) from the query string
  • retreives the configured error modes (triggers the configuration provider to read in the configuration)
  • calls Gadget::onError() to set configured error handling actions (triggers PEAR::setErrorHandling(PEAR_ERROR_CALLBACK))
  • start a PHP session
  • runs the security service
  • runs the autologin service
  • negotiates I18N through HTTP
  • querys the caching provider
  • instantiates and returns the PageHeader widget unless $header is FALSE
Returns Gadget::header($headerOptions) by default.

  1. require_once 'Gadget.php';
  2. $header = &Gadget::start();
  3. // or
  4. // Gadget::start();
  5. // $header = &Gadget::header();
  6. $header->addStyleSheet('about');
  7. Gadget::newWidget(array('template' => 'about.html'));
  8. Gadget::finish();

object PageHeader &start ([bool $header = true], [array $headerOptions = array()])
  • bool $header
  • array $headerOptions
startI18N (line 421)

Start & Negotiate I18N

object PageHeader &startI18N ([bool $header = true], [array $headerOptions = array()])
  • bool $header
  • array $headerOptions
timer (line 1115)

Timer

Returns the time difference from Gadget::start() to the point of calling.

  • access: public
float timer ([mixed $to_least = false])

Documentation generated on Sun, 03 Jul 2005 12:45:46 +0200 by phpDocumentor 1.3.0RC3