Today we will learn how to use the class AdminCоntrоller and will create our own model. Our Admin class will use its data. It all will be based on our new module Frequently Asked Questions. Let’s first describe the tables which our new module will use.
There will be 3 of them:
- Belvg_faq
- Belvg_lang
- Belvg_shop
The fields of the table belvg_fаq:
- Id_belvg_faq
- Position
- Active
The fields of the table belvg_lаng allow you to use class data in multi language stores.
- Id_belvg_faq
- Id_lang
- Title
- Content
The fields of the table belvg_shоp are responsible for multi-store support
- Id_belvg_faq
- Id_shop
As soon as the database fields have been defined we can create a class-model. Let’s call this class FAQ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class FAQ extends ObjectModel { / ** Variables, which will be available during the class initialization * / public $content; public $title; public $position; public $active; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'belvg_faq', 'primary' => 'id_belvg_faq', 'multilang' => TRUE, 'fields' => array( 'position' => array('type' => self::TYPE_INT), 'active' => array('type' => self::TYPE_BOOL), // Lang fields 'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128), 'content' => array('type' => self::TYPE_HTML, 'lang' => TRUE, 'validate' => 'isString', 'size' => 3999999999999), ), ); …. //For full version see the archive |
So, the model has been created. With its help the data will be stored\edited in the database tables. In order to work with data, to initiate processes of creating new FAQ blocks, to sort and view the list of already existing blocks we need a class inherited from AdminCоntrоller. So we create a class with the name AdminFAQ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
require_once (dirname(__file__) . '/belvg_faq.php'); require_once (dirname(__file__) . '/classes/FAQ.php'); class AdminFAQ extends ModuleAdminController { protected $_module = NULL; protected $position_identifier = 'id_belvg_faq'; /this filed is required if you use position for sorting public function __construct() { $this->context = Context::getContext(); $this->table = 'belvg_faq'; //$this->identifier = 'id_belvg_faq'; $this->className = 'FAQ'; $this->_defaultOrderBy = 'id_belvg_faq'; $this->lang = TRUE; $this->addRowAction('edit'); $this->addRowAction('delete'); $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')), ); Shop::addTableAssociation($this->table, array('type' => 'shop')); $this->fields_list = array( 'id_belvg_faq' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), 'position' => array('title' => $this->l('Position'), 'width' => 40,'filter_key' => 'position', 'align' => 'center', 'position' => 'position'), 'title' => array('title' => $this->l('Title'), 'width' => '300', 'filter_key' => 'b!title'), 'active' => array('title' => $this->l('Displayed'), 'width' => 25, 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => FALSE) ); parent::__construct(); } public function l($string) { if (is_null($this->_module)) { $this->_module = new belvg_faq(); } return $this->_module->l($string, __class__); } public function renderForm() { $this->display = 'edit'; $this->initToolbar(); $this->fields_form = array( 'tinymce' => TRUE, 'legend' => array('title' => $this->l('Field'), 'image' => '../img/admin/tab-categories.gif'), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Title:'), 'name' => 'title', 'id' => 'title', 'lang' => TRUE, 'required' => TRUE, 'hint' => $this->l('Invalid characters:').' <>;=#{}', 'size' => 50), array( 'type' => 'textarea', 'label' => $this->l('Block content'), 'name' => 'content', 'autoload_rte' => TRUE, 'lang' => TRUE, 'required' => TRUE, 'rows' => 5, 'cols' => 40, 'hint' => $this->l('Invalid characters:').' <>;=#{}'), array( 'type' => 'radio', 'label' => $this->l('Displayed:'), 'name' => 'active', 'required' => FALSE, 'class' => 't', 'is_bool' => FALSE, 'values' => array(array( 'id' => 'require_on', 'value' => 1, 'label' => $this->l('Yes')), array( 'id' => 'require_off', 'value' => 0, 'label' => $this->l('No')))), ), 'submit' => array('title' => $this->l(' Save '), 'class' => 'button')); if (Shop::isFeatureActive()) { $this->fields_form['input'][] = array( 'type' => 'shop', 'label' => $this->l('Shop association:'), 'name' => 'checkBoxShopAsso', ); } return parent::renderForm(); } } |
The method renderForm draws this form:
The array $this- >fields_list, which is responsible for the data displayed in the controller’s grid, is initialized in the constructor:
Download Frequently Asked Questions module.
Dear Jerry,
unfortunately, we cannot provide a code review via the blog.
Please contact our support department in case you need an individual consideration of the matter: [email protected]
We’ll be happy to assist!
Hello,
I made a simple comment form, but I have a problem with the status: in the back office when I click on the comment the status updates without errors, even in the ps_comments table the status is updated, but when I update the page in the front office the disablited comment hides. Where am I doing wrong?
This is the code
class COMMENT extends ObjectModel
{
public static function getComments ($ active = true, $ p = false, $ n = false)
{
$ comments = Db :: getInstance (_PS_USE_SQL_SLAVE _) -> executeS (‘
SELECT com. *, Cust.
firstname
, cust.lastname
FROM
'._DB_PREFIX _.' Comments
comINNER JOIN
'._DB_PREFIX _.' Customer
cust ON (com.id_customer
= cust.id_customer
)‘. ($ active?’ WHERE com.
active
= 1 ‘:’ ‘).’‘. ($ p?’ LIMIT ‘. (((int) $ p – 1) * (int) $ n).’, ‘. (int) $ n:’ ‘));
if ($ comments === false)
{
return false;
}
$ counts = array ();
if (count ($ counts))
{
foreach ($ comments as $ key => $ comment)
{
if (array_key_exists ((int) $ comment [‘id’], $ counts))
{
$ comment [$ key] = $ counts [(int) $ comment [‘id’]];
}
else
{
$ comment [$ key] = 0;
}
}
}
}
}
}
Thank you
Hi, Jeff!
No, unfortenately, by fefault the $this->fields_list params can be displayed inside < table > only.
Hi !
In an admin module controller, is this possible to include $this->fields_list() in a bootsrap tabs grid ? Using something similar like we found in $this->fields_options = array(
‘appearance’ => array(
‘title’ => $this->l(‘Blabla’),
‘icon’ => ‘icon-html5’,
‘tabs’ => array(
‘tab1’ => $this->l(‘blabla tab1’),
‘tab2’ => $this->l(‘blabla tab2’),
‘tab3’ => $this->l(‘blabla tab3’),
),
achbe,
Please try to download the archive once again.
I can’t get the archive. Could you share it again !!
How to use where condition as _defaultOrderBy in controller constructor…..
Hi,
The admin side controller is not showing order by, search fields and Bulk dropdown. Its simple listing only, Can you please advise.
Thank You
Xavier,
Yes, of course. That’s how most of the controllers work. For example, lets have a look at AdminCategoriesController.php witch works with _lang table as with the second table.
Hi,
I would like to do the same as you, but with 2 table.
Is it possible ?
I have a table for some elements, and another table with all names/descriptions etc
Thanks
Simon,
It is pretty hard to say what exactly you are doing wrong without seeing the original version of your code. Perhaps, the problem is with the class of the model which interacts with your controller.
Hi,
I’ve created my AdminBlockQuoteController.php and BlockQuote.php (class model).
But when i click to edit a record in the fields_list. It loads the renderform, but not the data in the fields? The fields are blank.
Could you possibly suggest what I might be doing wrong?
Thanks!!
jQuery is already available in the backoffice right after Prestashop installation.
How to include jQuery on this module?