Use CalendR inside your symfony1 application thanks to fwCalendRPlugin
The fwCalendRPlugin
is only available via git (Go to hell, SVN !),
and isn’t available on the sf plugins PEAR channel (Go to hell, PEAR !). Sorry.
$ git clone https://github.com/yohang/fwCalendRPlugin.git plugins/fwCalendarPlugin
OR
$ git submodule add https://github.com/yohang/fwCalendRPlugin.git plugins/fwCalendRPlugin
Note : You must have CalendR installed and autoloaded to use this plugin. You can use
fwClassLoaderPlugin
and clone CalendR into lib/vendor/calendr
to autoload it.
Edit your project configuration file :
<?php
require_once __DIR__.'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugin('fwCalendRPlugin');
}
}
Basicaly, the fwCalendRPlugin adds a getCalendR()
to the sfContext
instance.
<?php
class someActions extends sfActions
{
public function executeIndex()
{
$this->month = $this->getContext()->getCalendR()->getMonth();
}
}
Note : The CalendR instance is lazy loaded, it will be constructed only on getCalendR()
call.
This plugins provides the sames functions (helpers) that the Twig Extension.
<?php
use_helper('calendr');
calendr_year(2012);
calendr_month(2012, 7);
calendr_week(2012, 32);
calendr_day(2012, 7, 12);
calendr_events($period);
?>
On Calendar construction, a fw_calendr.construct
event is thrown. You just have to listen it, and to add your
providers to the event manager, for example in your ProjectConfiguration class.
Note that CalendR isn’t constructed if you don’t call it, so your providers won’t be instantiated if not needed.
Example (for a doctrine table as provider):
<?php
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->dispatcher->connect('fw_calendr.construct', function(sfEvent $event) {
$event->getSubject()->getEventManager()->addProvider('my_awesome_event_provider', EventTable::getInstance());
});
}
}
Warning : You can access the CalendR instance from everywhere "thanks" to the sfContext singleton.
However, this is a bad practice, due to all the sfContext dependencies, and code will become untestable.
Prefer using the sfEventDispatcher to inject CalendR into your business classes.
The same goes for the template helpers, which use the sfContext singleton.