Files
2019-11-17 19:14:07 +01:00

378 lines
13 KiB
PHP

<?php
if (!defined('_PS_VERSION_')) {
exit;
}
include_once(_PS_MODULE_DIR_.'cyb_concept/Concept.php');
class Cyb_Concept extends Module
{
protected $_html = '';
public function __construct()
{
$this->name = 'cyb_concept';
$this->tab = '';
$this->version = '1.0';
$this->author = 'Cyberscope Bastien';
$this->bootstrap = true;
$this->ps_versions_compliancy = array(
'min' => '1.7',
'max' => _PS_VERSION_,
);
$this->displayName = $this->l('Cyberscope Concept');
$this->confirmUninstall = $this->l('Etes vous sûr de vouloir supprimer le module ?');
parent::__construct();
}
public function install()
{
if (! parent::install()
|| ! Configuration::updateValue('c_texte', '')
|| ! Configuration::updateValue('c_lien', '')
|| ! Configuration::updateValue('c_btn', '')
|| ! Configuration::updateValue('c_image', '')
|| ! $this->registerHook('displayConcept')
|| ! $this->registerHook('header')
|| ! $this->createTables()) {
return false;
}
return true;
}
protected function createTables()
{
return (bool)Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'cyb_concept (
c_id int(10) unsigned NOT NULL AUTO_INCREMENT,
c_texte varchar(255) DEFAULT NULL,
c_lien varchar(255) DEFAULT NULL,
c_btn varchar(255) DEFAULT NULL,
c_image varchar(255) DEFAULT NULL,
PRIMARY KEY (c_id)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;
');
}
public function uninstall()
{
if (! parent::uninstall()
|| ! Configuration::deleteByName('c_texte')
|| ! Configuration::deleteByName('c_lien')
|| ! Configuration::deleteByName('c_btn')
|| ! Configuration::deleteByName('c_image')
|| ! $this->deleteTables()) {
return false;
}
return true;
}
protected function deleteTables()
{
return Db::getInstance()->execute('
DROP TABLE IF EXISTS `'._DB_PREFIX_.'cyb_concept`;
');
}
public function getContent()
{
$this->_html .= $this->headerHTML();
if (Tools::isSubmit('delete'.$this->name) || Tools::isSubmit('submit'.$this->name)) {
$this->postProcess();
$this->_html .= $this->renderList();
} else if (Tools::isSubmit('add'.$this->name) || (Tools::isSubmit('update'.$this->name) && Concept::exists((int)Tools::getValue('id')))) { // Add or Edit
$this->_html .= $this->renderForm();
} else {
$this->_html .= $this->renderList();
}
return $this->_html;
}
public function headerHTML()
{
if (Tools::getValue('controller') != 'AdminModules' && Tools::getValue('configure') != $this->name) {
return;
}
$this->context->controller->addJqueryUI('ui.sortable');
}
public function renderList()
{
// global $smarty;
$resultats = $this->getResultats();
$fields_list = array(
'id' => array(
'title' => 'Id',
'align' => 'center', // Align Col (not header)
'width' => 50, // header search : width
'type' => 'int',
'class' => 'fixed-width-xs', // class css
'search' => false, // header search : display
'orderby' => true, // header search : order
),
'texte' => array(
'title' => 'Texte',
'type' => 'string',
'search' => false,
),
'lien' => array(
'title' => 'Lien',
'type' => 'string',
'search' => false,
),
'btn' => array(
'title' => 'Bouton',
'type' => 'string',
'search' => false,
),
'fichier' => array(
'title' => 'Image',
'type' => 'image',
'search' => false,
),
);
// Init toolbar
$this->initToolbar();
$helper = new HelperList();
$helper->shopLinkType = '';
$helper->actions = array('edit', 'delete');
$helper->module = $this;
$helper->toolbar_btn = $this->toolbar_btn;
$helper->listTotal = count($resultats);
$helper->show_toolbar = true;
$helper->identifier = 'id';
$helper->table = 'cyb_concept';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
return $helper->generateList($resultats, $fields_list);
}
public function postProcess()
{
$errors = array();
if (Tools::isSubmit('submit'.$this->name)) {
if (Tools::getValue('id')) {
$obj = new Concept((int)Tools::getValue('id'));
if (! $obj->getId()) {
$this->_html .= $this->displayError($this->getTranslator()->trans('Invalid slide ID', array(), ''));
return false;
}
} else {
$obj = new Concept();
}
$obj->setBtn(Tools::getValue('btn'));
$obj->setLien(Tools::getValue('lien'));
$obj->setTexte(Tools::getValue('texte'));
if (isset($_FILES['fichier'], $_FILES['fichier']['name']) && $_FILES['fichier']['name']) {
if ($error = ImageManager::validateUpload($_FILES['fichier'], 4000000)) {
$errors[] = $error;
} else {
$ext = substr($_FILES['fichier']['name'], strrpos($_FILES['fichier']['name'], '.') + 1);
$file_name = md5($_FILES['fichier']['name']) . '.' . $ext;
if (! move_uploaded_file($_FILES['fichier']['tmp_name'],
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views/img' . DIRECTORY_SEPARATOR . $file_name)
) {
return $this->displayError($this->trans('An error occurred while attempting to upload the file.',
array(), 'Admin.Notifications.Error'));
} else {
$obj->setImage($file_name);
}
}
}
/* Adds */
if (! Tools::getValue('id')) {
$res = $obj->add();
if (! $res) {
$errors[] = $this->displayError("Une erreur est survenue lors de l'ajout du bloc");
} else {
$this->_html .= $this->displayConfirmation('Bloc ajouté');
}
} else {
$res = $obj->update();
if (! $res) {
$errors[] = $this->displayError("Une erreur est survenue lors de la modification du bloc");
} else {
$this->_html .= $this->displayConfirmation('Bloc mis à jour');
}
}
} else if (Tools::isSubmit('delete'.$this->name)) {
$obj = new Concept((int)Tools::getValue('id'));
$res = $obj->delete();
if (! $res) {
$this->_html .= $this->displayError('Could not delete.');
} else {
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=1&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name);
}
}
if (count($errors)) {
$this->_html .= $this->displayError(implode('<br />', $errors));
} elseif (Tools::isSubmit('submit'.$this->name) && Tools::getValue('id')) {
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=4&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name);
} elseif (Tools::isSubmit('submit'.$this->name)) {
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=3&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name);
}
}
public function renderForm()
{
$helper = new HelperForm();
$helper->submit_action = 'submit'.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->module = $this;
$fields_form = array(
'form' => array(
'legend' => array(
'title' => "Paramètre du bloc",
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'textarea',
'label' => 'Texte',
'name' => 'texte',
'required' => false,
'autoload_rte' => true,
'desc' => $this->l('Mettre en gras pour le format Titre'),
),
array(
'type' => 'text',
'label' => 'Lien',
'name' => 'lien',
'required' => true,
),
array(
'type' => 'text',
'label' => 'Bouton',
'name' => 'btn',
'required' => true,
),
array(
'type' => 'file',
'label' => 'Image',
'name' => 'fichier',
'required' => true,
'display_image' => true,
'desc' => $this->l('Premier (1000*1200px) / Deuxième (1000*576px) / Troisième (1000*694px)'),
),
),
'submit' => array(
'title' => 'Valider'
)
),
);
if (Tools::isSubmit('id') && Concept::exists((int)Tools::getValue('id'))) {
$obj = new Concept((int)Tools::getValue('id'));
$fields_form['form']['input'][] = array(
'type' => 'hidden',
'name' => 'id'
);
$fields_form['form']['images'] = $obj->getImage();
}
// Set var to helper
$helper->tpl_vars = array(
'fields_value' => $this->getAddFieldsValues(),
'image_baseurl' => $this->_path.'views/img/',
'base_url' => $this->context->shop->getBaseURL(),
);
return $helper->generateForm(array($fields_form));
}
public function initToolbar()
{
$this->toolbar_btn['new'] = array(
'short' => 'lorem ipsum',
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&add'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => 'Ajouter'
);
}
public function getAddFieldsValues()
{
$fields = array();
$obj = new Concept();
if (Tools::isSubmit('update'.$this->name) && Concept::exists((int)Tools::getValue('id'))) {
$obj = new Concept((int)Tools::getValue('id'));
$fields['id'] = (int)Tools::getValue('id', $obj->id);
}
$fields['btn'] = Tools::getValue('btn', $obj->getBtn());
$fields['lien'] = Tools::getValue('lien', $obj->getLien());
$fields['texte'] = Tools::getValue('texte', $obj->getTexte());
$fields['fichier'] = Tools::getValue('fichier', $obj->getImage());
return $fields;
}
public function hookHeader()
{
$this->context->controller->registerStylesheet('css-cyb-concept', 'modules/' . $this->name. '/views/css/style.css',
[
'media' => 'all',
'priority' => 0,
]
);
}
public function hookdisplayConcept()
{
global $smarty;
$a_resultats = $this->getResultats();
$smarty->assign('liste_resultats', $a_resultats);
return $this->display(__FILE__, 'cyb_concept.tpl');
}
public function getResultats()
{
$resultats = Db::getInstance(_PS_USE_SQL_SLAVE_)->executes('
SELECT c_id, c_btn, c_lien, c_texte, c_image
FROM '._DB_PREFIX_.'cyb_concept
ORDER BY c_id'
);
$res = array();
foreach ($resultats as $resultat) {
$res[] = array(
'id' => $resultat['c_id'],
'btn' => $resultat['c_btn'],
'lien' => $resultat['c_lien'],
'texte' => $resultat['c_texte'],
'image' => $resultat['c_image'] ? $this->context->link->getMediaLink(_MODULE_DIR_.$this->name.'/views/img/'.$resultat['c_image']) : '',
);
}
return $res;
}
}