[studs-devel] access to datasource from taglib
Status: Beta
Brought to you by:
mojavelinux
|
From: Timur V. <ti...@e-...> - 2005-06-30 08:48:42
|
Hello.
Let's assume, that it is required to create tag carrying out specific thing=
s in the project.
Tag should take data from from DB by interaction with a class Manager
which it is returned DataSource object reference.
Let's name ours tag HtmlAreaTag, which detching data by the key.
class HtmlAreaTag extends TagSupport
{
....
function getBlock()
{
$manager =3D& new HtmlAreaManager(TagUtils::getDataS=
ource($this->pageContext));
$block =3D& $manager->getRowByAlias($this->getAlia=
s());
=20
return is_null($block) ? '' : $block->getHtml();
}
}
For this purpose in TagUtils I suggest to add function
/**
* Return the default data source for the current module.
*
* @param request The HttpServletRequest we are processing
* @return DataSource
* @throws ServletException If the datasource with the specified ke=
y could not be found
*/
function &getDataSource(&$pageContext, $key =3D null)
{
if (is_null($key))
{
$key =3D c('StudsConstants::DATA_SOURCE_KEY');
}
// identify the current module
$request =3D& $pageContext->getRequest();
$servletContext =3D& $pageContext->getServletContext();
$moduleConfig =3D& ModuleUtils::getModuleConfig($request,=
$servletContext);
// return the requested data source instance
$ds =3D& $servletContext->getAttribute($key . $moduleConfig=
->getPrefix());
if ($ds =3D=3D null)
{
throw_exception(new ServletException('The datasourc=
e with the requested key \'' . $key . '\' has not been configured'));
return;
}
return $ds;
}
=20
--
Send plain text only to timurv at e-kazan dot ru
PGP key: http://wwwkeys.us.pgp.net:11371/pks/lookup?op=3Dget&search=3D0x42=
5B9F0F
--
Timur Vafin
|