Jump to content

Issue with module class


Recommended Posts

I am writing simple module to prestashop, but I put ajax stuff to other file- ajax.php in this file i create object of clas attributesArray and I run method $attrubutesarray->test(), which value should be 'test' but when I open ajax.php in browser I get blank document, any ideas?

 

File ajax.php

<?php

include(dirname(__FILE__) . '/../../config/config.inc.php');
include(dirname(__FILE__) . '/../../init.php');
include(dirname(__FILE__) . '/$attrubutesarray.php');

$attrubutesarray = new attributesArray();
echo $attrubutesarray->test();

File attributesarray.php

<?php

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


class attributesArray extends Module {
/* @var boolean error */

protected $_errors = false;

public function __construct() {
$this->name = 'attributesarray';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Dominik Wilk';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Zakładka z atrybutami produktów');
$this->description = $this->l('To jest moduł umozliwiający wprowadzanie dodatkowych atrybutów do produtków.');
}

public function install() {
if (!parent::install() OR
!$this->alterTable('add') OR
!$this->registerHook('actionAdminControllerSetMedia') OR
!$this->registerHook('actionProductUpdate') OR
!$this->registerHook('displayAdminProductsExtra'))
return false;
return true;
}

public function uninstall() {
if (!parent::uninstall() OR !$this->alterTable('remove'))
return false;
return true;
}

public function test() {
return 'test';
}

public function alterTable($method) {
switch ($method) {
case 'add':
$sql = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "attributesarray_rows`(
`id_row` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`row_title` VARCHAR(20) NOT NULL,
`id_product` INT(10) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "attributesarray_cols`(
`id_col` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`col_title` VARCHAR(20) NOT NULL,
`id_product` INT(10) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "attributesarray_cells`(
`id_cell` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_col` INT(20) NOT NULL,
`id_row` INT(20) NOT NULL,
`id_product` INT(10) NOT NULL,
`id_status` INT(10) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "attributesarray_status`(
`id_status` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`class` VARCHAR(30) NOT NULL,
`name` VARCHAR(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
break;

case 'remove':
$sql = "DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "attributesarray_rows`;
DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "attributesarray_cols`;
DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "attributesarray_cells`;
DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "attributesarray_status`;";
break;
}

if (!Db::getInstance()->Execute($sql))
return false;
return true;
}

public function prepareNewTab() {

$this->context->smarty->assign(array(
'custom_field' => $this->getCustomField((int) Tools::getValue('id_product')),
'languages' => $this->context->controller->_languages,
'default_language' => (int) Configuration::get('PS_LANG_DEFAULT')
));
}

public function hookActionAdminControllerSetMedia($params) {

// add necessary javascript to products back office
if ($this->context->controller->controller_name == 'AdminProducts' && Tools::getValue('id_product')) {
$this->context->controller->addJS($this->_path . '/js/attributesarray.js');
}
}

public function hookActionProductUpdate($params) {
$id_product = (int) Tools::getValue('id_product');

$this->addTableTitle('addColumn', 'Tytuł', 1);

//var_dump(Tools::getValue('attributesarray_row'));
//jeżeli został wysłany formularz dodania nowego wiersza bądź komumny pobieramy z $_POST wysłaną wartość,
//w przeciwnym razie ustawiamy wartość zmiennej na false
/* $attributesarray_row = (strlen(Tools::getValue('attributesarray_row')) > 0) ? Tools::getValue('attributesarray_row') : false;
$attributesarray_col = (strlen(Tools::getValue('attributesarray_col')) > 0) ? Tools::getValue('attributesarray_col') : false;

if (!Db::getInstance()->update('product_lang', array('custom_field' => pSQL(Tools::getValue('custom_field_' . $lang['id_lang']))), 'id_lang = ' . $lang['id_lang'] . ' AND id_product = ' . $id_product))
$this->context->controller->_errors[] = Tools::displayError('Error: ') . mysql_error(); */
}

public function addTableTitle($method, $title, $id_product) {

switch ($method) {
case 'addColumn':
if (!Db::getInstance()->insert('attributesarray_cols', array('col_title' => pSQL(Tools::getValue('attributesarray_col')), 'id_product' => (int) $id_product))) {
$this->context->controller->_errors[] = Tools::displayError('Error: ') . mysql_error();
}
break;

case 'addRow':
$sql = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "attributesarray_rows`(
`id_row` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`row_title` VARCHAR(20) NOT NULL,
`id_product` INT(10) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
break;
}

if (!Db::getInstance()->Execute($sql))
return false;
return true;
}

public function getCustomField($id_product) {
$result = Db::getInstance()->ExecuteS('SELECT custom_field, id_lang FROM ' . _DB_PREFIX_ . 'product_lang WHERE id_product = ' . (int) $id_product . ' LIMIT 1');
if (!$result)
return false;

foreach ($result as $field) {
$fields = $field['custom_field'];
}

return $fields;
}

public function hookDisplayAdminProductsExtra($params) {
if (Validate::isLoadedObject($product = new Product((int) Tools::getValue('id_product')))) {
$this->prepareNewTab();
return $this->display(__FILE__, 'attributesarray.tpl');
}
}

}
?>
Link to comment
Share on other sites

×
×
  • Create New...