Nefunkční komponeta v podřízeném presenteru
- Petr Parolek
- Člen | 455
Ahoj, nefunuguje mi formulář v presenteru, který se volá z nadřazeného presenteru. Když přesunu metodu createComponentGuestbookForm do DefaultPresenteru, tak mi vše funuje správně.
DefaultPresenter:
<?php
namespace App\FrontModule\Presenters;
class DefaultPresenter extends BasePresenter
{
public function renderPage($seo_url_text)
{
$this->template->modules = $this->loadModules();
$url_array = array_reverse(explode("/",$seo_url_text));
$this->template->catId = (int)$url_array[0];
}
}
?>
ModuleBasePresenter
<?php
namespace App\FrontModule\Presenters;
use Nette,
App\Model;
/**
* Base presenter for all application presenters.
*/
abstract class ModuleBasePresenter extends BasePresenter
{
protected $module = NULL;
protected $oParentPresenter = NULL;
protected $iModuleId = NULL;
protected $templateDirectory;
protected $moduleTemplate;
protected $moduleContentTemplate;
protected $moduleTemplateDir = NULL;
protected $templateFile = "default.latte";
public function html_cut($s, $limit,$whole_word=1)
{
static $empty_tags = array('area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param');
$length = 0;
$tags = array(); //dosud neuzavrene znacky
for($i=0; ($i < strlen($s) && $length < $limit) || ($whole_word==1 && ($length >= $limit && (isset($s{$i}) && !preg_match('/\s/',$s{$i})))); $i++) {
switch ($s{$i}) {
case '<':
//nactení znacky
$start = $i+1;
while ($i < strlen($s) && $s{$i} != '>' && !ctype_space($s{$i})) {
$i++;
}
$tag = strtolower(substr($s, $start, $i - $start));
//preskoení pripadnych atributu
$in_quote = '';
while ($i < strlen($s) && ($in_quote || $s{$i} != '>')) {
if (($s{$i} == '"' || $s{$i} == "'") && !$in_quote) {
$in_quote = $s{$i};
}
elseif ($in_quote == $s{$i}) {
$in_quote = '';
}
$i++;
}
if ($s{$start} == '/') {//uzaviraci znacka
$tags = array_slice($tags, array_search(substr($tag, 1), $tags) + 1);
}
elseif ($s{$i-1} != '/' && !in_array($tag, $empty_tags)) { //oteviraci znacka
array_unshift($tags, $tag);
}
break;
case '&':
$length++;
while ($i < strlen($s) && $s{$i} != ';') {
$i++;
}
break;
default:
$length++;
}
}
$s = substr($s, 0, $i);
//odstraneni prazdnych znaku na konci retezce
$s=preg_replace('/[\s]$/is','',$s);
//pokud neni konec retezce uzavreni tagu nebo nejaky znak na konci vety, pridej tri tecky
if(!preg_match('/>[\s]{0,}$/is',$s) && !preg_match('/[\.!\?]$/is',$s)){$s .= '...';}
//uzavreni tagu
if ($tags) {
$s .= "</" . implode("></", $tags) . ">";
}
//navratova hodnota funkce
return $s;
}
private function loadTemplate()
{
if(isset($this->templateFile) && !empty($this->templateFile))
{
$classInfo = new \ReflectionClass($this);
$this->templateDirectory = dirname($classInfo->getFileName()) . "/../templates/";
// get the name of the main template directory
preg_match('/Module([a-zA-Z0-9]+)Presenter$/', get_class($this), $matches);
if($this->moduleTemplateDir === NULL){
$this->moduleTemplateDir = $matches[1];
}
/* create layout template for every module */
$this->moduleTemplate = $this->oParentPresenter->createTemplate();
$this->moduleTemplate->setFile(__DIR__ . "/../templates/Modules/layout.latte");
$this->moduleTemplate->module = $this->module;
$tmpl = $this->oParentPresenter->getParameter("tmpl");
if (!$tmpl) {
$tmpl = "default";
}
$this->moduleContentTemplate = $this->oParentPresenter->createTemplate();
//\Tracy\Debugger::barDump($this->templateDirectory . $this->moduleTemplateDir . "/" . $tmpl . ".latte");
$this->moduleContentTemplate->setFile($this->templateDirectory . $this->moduleTemplateDir . "/" . $tmpl . ".latte");
/* Useful variables */
$this->moduleContentTemplate->basePath = preg_replace('#https?://[^/]+#A', '', $this->oParentPresenter->template->baseUrl);
$this->moduleContentTemplate->seo_url_text = $this->oParentPresenter->getParameter("seo_url_text");
$this->moduleContentTemplate->moduleId = $this->iModuleId;
$this->moduleContentTemplate->module = $this->module;
$this->moduleContentTemplate->settings = $this->oParentPresenter->context->getService("page")->getSettings();;
}
}
protected function startup() {
parent::startup();
if($this->getParameter('id')){
$this->load($this->getParameter('id'));
}
}
public function load($iModuleId, $oParentPresenter = NULL){
$this->oParentPresenter = $oParentPresenter;
if($this->oParentPresenter != NULL) {
$this->iModuleId = $iModuleId;
$this->loadModuleFromDB();
$this->loadTemplate();
$this->setTemplateVariables();
$this->moduleTemplate->content = $this->moduleContentTemplate;
$this->moduleContentTemplate = $this->moduleContentTemplate->addFilter("shortify",array($this, "html_cut"));
return (string) $this->moduleTemplate;
}
}
private function loadModuleSettings()
{
if($this->module){
$this->module->settings = new \stdClass();
$settings = $this->oParentPresenter->context->getService("modulesSettings")->findBy(array("module_id" => $this->module->id));
foreach($settings as $setting){
$this->module->settings->{$setting->key} = $setting->value;
}
}
}
protected function loadModuleFromDB()
{
if($this->oParentPresenter){
$this->module = $this->oParentPresenter->context->getService("pageModuleRegister")->getModule($this->iModuleId);
$this->loadModuleSettings();
$this->loadModuleData();
}
else {
$this->module = $this->context->getService("pageModuleRegister")->getModule($this->iModuleId);
$this->loadModuleSettings();
}
}
protected function loadModuleData(){
return NULL;
}
}
?>
ModuleGuestBookPresenter:
<?php
namespace App\FrontModule\Presenters;
use Nette,
App\Model,
Nette\Application\UI;
class ModuleGuestBookPresenter extends ModuleBasePresenter
{
private $guestBookPosts = NULL;
protected function loadModuleData()
{
if($this->oParentPresenter){
$this->guestBookPosts = $this->oParentPresenter->context->getService("moduleGuestBookModel")->findBy( array('page_page_modules_id' => $this->iModuleId) )->order('id DESC');
}
}
public function renderDefault() {
$this->registerFilters($this->template);
}
public function renderAdd($id)
{
$form_values = $this->request->getPost();
$data = array();
$data['name'] = $form_values['name'];
$data['email'] = $form_values['email'];
$data['text'] = $form_values['text'];
$data['page_page_modules_id'] = $id;
\Tracy\Debugger::barDump($form_values);
$this->context->getService("moduleGuestBookModel")->insert($data);
$this->flashMessage("Zpráva byla úspěšně přidána.");
$this->redirectUrl($form_values['url']);
}
protected function createComponentGuestbookForm()
{
$form = new \CustomForm();
$form->addText("name", "Vaše jméno")
->addRule(\Nette\Application\UI\Form::FILLED, 'Zadejte prosím vaše jméno.');
$form->addSubmit('addPage', 'Zakoupit');
$form->onSuccess[] = array($this, 'buyFormSucceeded');
$form->setCustomRenderer();
return $form;
}
public function setTemplateVariables()
{
$this->moduleContentTemplate->addPostLink = $this->oParentPresenter->link(':Front:ModuleGuestBook:Add', array('id' => $this->iModuleId, 'parent_page_id' => $this->oParentPresenter->getParameter('id')));
$this->moduleContentTemplate->guestBookPosts = $this->guestBookPosts;
$this->moduleContentTemplate->url = $this->oParentPresenter->template->url;
}
}
?>
v template guestbooku mám:
<div class="module-inner">
<h3 id="guestBook{$moduleId}">Diskuzní knihahhh</h3>
{control guestbookForm}
<div n:foreach="$guestBookPosts as $post">
<h4 class="media-heading">{$post->name} • {$post->email} • <span class="datetime">{$post->timestamp}</span></h4>
<p>{$post->text}</p>
</div>
<div class="clearboth"></div>
</div>
Prosím poradte mi, kde je chyba.
- Petr Parolek
- Člen | 455
Tracy píše Component with name ‚guestbookForm‘ does not exist
Editoval ppar (5. 6. 2016 15:29)
- David Matějka
- Moderator | 6445
Dle kodu to vypada, ze tam delas neco hodne spatne. Co je nadrazeny presenter? ty tam nejak zanorujes presentery?
- Petr Parolek
- Člen | 455
Zanořuju. Router volá DefaultPresenter. zkrátka vycházím z tohoto https://github.com/Mirgen/web_cms
- Petr Parolek
- Člen | 455
jaaaj, to jsem dopadl. Jak bys koncipovat v CMS moduly na stránce, v adminu by bylo na výběr, co na které stránce chceš vložit.
Editoval ppar (5. 6. 2016 15:49)