Jump to content

Controller in custom module (simlpe examlpe)


Bejkrools

Recommended Posts

Hi everyone! I learn PrestaShop developing and i did new basic module ./modules/mymodule/mymodule.php

<?php

if (!defined('_PS_VERSION_'))
{exit;
}

class MyModule extends Module
{
    public function __construct()
    {
        $this->name = 'mymodule';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'www12';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
        
        $this->bootstrap = true;
        
        parent::__construct();
        
        $this->displayName = $this->l('My module');
        $this->description = $this->l('Description of my module.');
        
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
        
        if (!Configuration::get('MYMODULE_NAME'))
        {$this->warning = $this->l('No name provided');}
    }
    
    public function install() {
        if (Shop::isFeatureActive()) {
            Shop::setContext(Shop::CONTEXT_ALL);
        }
        if (!parent::install() ||
                !$this->registerHook('top') || !$this->registerHook('header') || !Configuration::updateValue('MYMODULE_NAME', 'my friend')
        ) {
            return false;
        } else {
            return true;
        }
    }

    public function uninstall() {
        if (!parent::uninstall() ||
                !Configuration::deleteByName('MYMODULE_NAME')
        ) {
            return false;
        } {
            return true;
        }
    }
    
    public function getContent()
    {
        $output = null;

        if (Tools::isSubmit('submit' . $this->name)) {
            $my_module_name = strval(Tools::getValue('MYMODULE_NAME'));
            if (!$my_module_name || empty($my_module_name) || !Validate::isGenericName($my_module_name)) {
                $output .= $this->displayError($this->l('Invalid Configuration value'));
            } else {
                Configuration::updateValue('MYMODULE_NAME', $my_module_name);
                $output .= $this->displayConfirmation($this->l('Settings updated'));
            }
        }
        return $output . $this->displayForm();
    }
    
    public function displayForm()
    {
        // Get default language
        $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

        // Init Fields form array
        $fields_form[0]['form'] = array(
            'legend' => array(
                'title' => $this->l('Settings'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Configuration value'),
                    'name' => 'MYMODULE_NAME',
                    'size' => 20,
                    'required' => true
                )
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'class' => 'button'
            )
        );

        $helper = new HelperForm();

        // Module, token and currentIndex
        $helper->module = $this;
        $helper->name_controller = $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

        // Language
        $helper->default_form_language = $default_lang;
        $helper->allow_employee_form_lang = $default_lang;

        // Title and toolbar
        $helper->title = $this->displayName;
        $helper->show_toolbar = true;        // false -> remove toolbar
        $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
        $helper->submit_action = 'submit'.$this->name;
        $helper->toolbar_btn = array(
            'save' =>
            array(
                'desc' => $this->l('Save'),
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                '&token='.Tools::getAdminTokenLite('AdminModules'),
            ),
            'back' => array(
                'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                'desc' => $this->l('Back to list')
            )
        );

        // Load current value
        $helper->fields_value['MYMODULE_NAME'] = Configuration::get('MYMODULE_NAME');

        return $helper->generateForm($fields_form);
    }
    
    public function hookDisplayTop($params)
    {
      $this->context->smarty->assign(
          array(
              'my_module_name' => Configuration::get('MYMODULE_NAME'),
              'my_module_link' => $this->context->link->getModuleLink('mymodule', 'display')
          )
      );
      return $this->display(__FILE__, 'mymodule.tpl');
    }

    public function hookDisplayHeader()
    {
      $this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all');
    }
    
    public function displayModuleInformation()
    {
        $module_name = $this->name;
        $module_descrtiption = $this->description;
        $module_version = $this->version;
        $module_author = $this->author;
        $module_location = __FILE__;
        
        return array(
            'name' => $module_name,
            'description' => $module_descrtiption,
            'version' => $module_version,
            'author' => $module_author,
            'location' => $module_location);
        }
}

with tpl. file ./modules/mymodule/mymodule.tpl

<!-- Block mymodule -->
<div id="mymodule_block" class="block">
  <h4>Welcome!</h4>
  <div class="block_content">
    <p>Hello,
       {if isset($my_module_name) && $my_module_name}
           {$my_module_name}
       {else}
           World
       {/if}
       !       
    </p>   
    <ul>
      <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li>
    </ul>
  </div>
</div>
<!-- /Block mymodule -->

and additional controller file ./modules/mymodule/controllers/front/display.php

<?php
$mymodule = new MyModule();

$info = $mymodule->displayModuleInformation();

echo 'Name: '.$info['name'].'</br>';
echo 'Description: '.$info['description'].'</br>';
echo 'Version: '.$info['version'].'</br>';
echo 'Author: '.$info['author'].'</br>';
echo 'Location: '.$info['location'].'</br>';

Module seems to works right but when i call display controller then i get olny right data that i want to get. In new, clear page, no as part of my shop. Additionaly i get strange warning:

 

Name: mymodule
Description: Description of my module.
Version: 1.0.0
Author: www12
Location: C:\xampp\htdocs\prestashop1611\modules\mymodule\mymodule.php

 

Fatal error: Class 'mymoduledisplayModuleFrontController' not found in C:\xampp\htdocs\prestashop1611\classes\controller\Controller.php on line 134

 

 

  • Why fatal error appeared?
  • How can i make, that controller display() displays data for examlpe just below top hook?

 

In the meantime I'll try another way to display this content, but i'll indebdet for hints.

 

 

--------------------------------[ EDIT 1 ]------------------------------------------

 

I updated my code, but result is this same. Clear page with pure html.

 

modified mymodule.php

{...}
 
public function displayModuleInformation()
    {
        global $smarty;
        
        $module_name = $this->name;
        $module_descrtiption = $this->description;
        $module_version = $this->version;
        $module_author = $this->author;
        $module_location = __FILE__;
        
  
        $smarty->assign(array(
            'name' => $module_name,
            'description' => $module_descrtiption,
            'version' => $module_version,
            'author' => $module_author,
            'location' => $module_location)
        );

        return $this->display(__FILE__, 'tpl/display.tpl');
}

new tpl/display.tpl file

<!-- Block mymodule INFO -->
<div id="mymodule_block" class="block">
  <h4>Module's info!</h4>
  <div class="block_content">
    <p>
        Nazwa modułu: {$name} <br>
        Opis modułu: {$description} <br>
        Wersja modułu: {$version} <br>
        Autor modułu: {$author} <br>
        Lokalizacja modułu: {$location} <br>
    </p>   
  </div>
</div>
<!-- /Block mymodule INFO -->

and modyfied display.php controller

<?php
$mymodule = new MyModule();
echo $mymodule->displayModuleInformation();


Output after call http://localhost/prestashop1611/pl/module/mymodule/display


<!-- Block mymodule INFO -->
<div id="mymodule_block" class="block">
  <h4>Module's info!</h4>
  <div class="block_content">
    <p>
        Nazwa modułu: mymodule <br>
        Opis modułu: Description of my module. <br>
        Wersja modułu: 1.0.0 <br>
        Autor modułu: www12 <br>
        Lokalizacja modułu: C:\xampp\htdocs\prestashop1611\modules\mymodule\mymodule.php <br>
    </p>   
  </div>
</div>
<!-- /Block mymodule INFO --><br />
<b>Fatal error</b>:  Class 'mymoduledisplayModuleFrontController' not found in <b>C:\xampp\htdocs\prestashop1611\classes\controller\Controller.php</b> on line <b>134</b><br />
Edited by Bejkrools (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...