Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2013 01:36
Show Gist options
  • Save anonymous/4471601 to your computer and use it in GitHub Desktop.
Save anonymous/4471601 to your computer and use it in GitHub Desktop.
<?php
/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $em;
/**
* Path to theme templates.
*
* @var string
*/
private $themePath;
protected function beforeRender()
{
parent::beforeRender();
if ($this->isAjax()) {
$this->invalidateControl('flash');
}
}
/**
* Set a theme.
* @param type $themePath
*/
public function setThemePath($themePath)
{
$this->themePath = $themePath;
}
/**
* @param Nette\DI\Container $context
* @param \Doctrine\ORM\EntityManager $em
*/
public function __construct(Nette\DI\Container $context, \Doctrine\ORM\EntityManager $em)
{
parent::__construct($context);
$this->em = $em;
$this->themePath = APP_DIR . '/themes/pokerzpravy/';
}
protected function createTemplate($class = NULL)
{
$template = parent::createTemplate($class);
$template->registerHelper('splitLongWords', function ($s, $maxLength = 30, $splitter = ' ') {
return wordwrap($s, $maxLength, $splitter, true);
});
// <a n:class="$isCurrent('items:', 'items:detail') ? 'active'" ...>
$presenter = $this;
$template->isCurrent = function () use ($presenter) {
foreach (func_get_args() as $link) {
if ($presenter->isLinkCurrent($link)) {
return true;
}
}
return false;
};
return $template;
}
protected function createComponentItemCrud()
{
return new ItemCrudControl($this->em, $this->user);
}
protected function createComponentFeedCrud()
{
return new FeedCrudControl($this->em, $this->user);
}
public function createComponentFormatCrud()
{
return new FormatCrudControl($this->em, $this->user);
}
public function createComponentCategoryCrud()
{
return new CategoryCrudControl($this->em, $this->user);
}
public function createComponentWeblinkCrud()
{
return new WeblinkCrudControl($this->em, $this->user);
}
/**
* Control cssLoader
* @return \WebLoader\Nette\CssLoader
*/
protected function createComponentCssLoader()
{
$files = new \WebLoader\FileCollection(WWW_DIR . '/css');
$files->addFile('screen.css');
// jQuery UI
$files->addRemoteFile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/smoothness/jquery-ui.css');
// jQuery UI Timepicker Addon
$files->addFile(WWW_DIR . '/../libs/jQuery-Timepicker-Addon-submodule/jquery-ui-timepicker-addon.css');
// DateInput
$files->addFile(WWW_DIR . '/../libs/DateInput-submodule/dateInput.css');
// Calendar
$files->addFile(WWW_DIR . '/../libs/VS/UI/CalendarControl.css');
$compiler = \WebLoader\Compiler::createCssCompiler($files, WWW_DIR . '/webtemp');
return new \WebLoader\Nette\CssLoader($compiler, $this->template->basePath . '/webtemp');
}
/**
* Control jsLoader
* @return \WebLoader\Nette\JavaScriptLoader
*/
protected function createComponentJsLoader()
{
$files = new \WebLoader\FileCollection(WWW_DIR . '/js');
$files->addRemoteFile('http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js');
$files->addRemoteFile('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js');
$files->addFiles(array('netteForms.js'));
// jQuery Timepicker Addon
$files->addFile(WWW_DIR . '/../libs/jQuery-Timepicker-Addon-submodule/jquery-ui-timepicker-addon.js');
$files->addFile(WWW_DIR . '/../libs/jQuery-Timepicker-Addon-submodule/localization/jquery-ui-timepicker-cs.js');
// DateInput
$files->addFile(WWW_DIR . '/../libs/DateInput-submodule/dateInput.js');
// Nette Ajax (Vojtech Dobes script)
$files->addFile(WWW_DIR . '/../libs/nette.ajax-submodule/nette.ajax.js');
$files->addFile(WWW_DIR . '/../libs/nette.ajax-submodule/example.js');
$compiler = \WebLoader\Compiler::createJsCompiler($files, WWW_DIR . '/webtemp');
return new \WebLoader\Nette\JavaScriptLoader($compiler, $this->template->basePath . '/webtemp');
}
/**
* Get the list of possible default view template files.
* @return array
*/
public function formatDefaultViewTemplateFileNames()
{
return parent::formatTemplateFiles();
}
/**
* Get the list of default layout templates.
* @return array
*/
public function formatDefaultLayoutTemplateFileNames()
{
return parent::formatLayoutTemplateFiles();
}
/**
* Get the list of possible view templates from the current theme.
* @return array
*/
public function formatThemeViewTemplateFileNames()
{
if (empty($this->themePath)) {
return array();
}
$defaultPathLength = strlen(dirname(dirname($this->getReflection()->getFileName())) . '/templates');
$themePath = $this->themePath;
return array_map(function ($filename) use ($defaultPathLength, $themePath) {
return $themePath . substr($filename, $defaultPathLength);
}, $this->formatDefaultViewTemplateFileNames());
}
/**
* Get the list of possible theme layout template files.
* @return array
*/
public function formatThemeLayoutTemplateFileNames()
{
if (empty($this->themePath)) {
return array();
}
$defaultPathLength = strlen(dirname(dirname($this->getReflection()->getFileName())) . '/templates');
$themePath = $this->themePath;
return array_map(function ($filename) use ($defaultPathLength, $themePath) {
return $themePath . substr($filename, $defaultPathLength);
}, $this->formatDefaultLayoutTemplateFileNames());
}
/**
* @return array
*/
public function formatTemplateFiles()
{
return array_merge(
$this->formatThemeViewTemplateFileNames(),
$this->formatDefaultViewTemplateFileNames()
);
}
/**
* @return array
*/
public function formatLayoutTemplateFiles()
{
return array_merge(
$this->formatThemeLayoutTemplateFileNames(),
$this->formatDefaultLayoutTemplateFileNames()
);
}
public function sendTemplate()
{
$template = $this->getTemplate();
foreach ($this->formatDefaultViewTemplateFileNames() as $defaultViewFileName) {
if (is_file($defaultViewFileName)) {
$template->baseViewTemplate = $defaultViewFileName;
}
}
foreach ($this->formatDefaultLayoutTemplateFileNames() as $defaultLayoutFileName) {
if (is_file($defaultLayoutFileName)) {
$template->baseLayoutTemplate = $defaultLayoutFileName;
}
}
return parent::sendTemplate();
}
/**
* @param string|Nette\Security\IResource $resource
* @param string $action
* @throws Nette\Application\ForbiddenRequestException
*/
protected function authorize($resource, $action)
{
if (!$this->user->isAllowed($resource, $action)) {
throw new \Nette\Application\ForbiddenRequestException;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment