Skip to content

Commit

Permalink
Merge pull request #8 from aleeks/dev
Browse files Browse the repository at this point in the history
Refacto blockreassurance
  • Loading branch information
Maxime Biloé authored Nov 4, 2016
2 parents fb29509 + eb2bf5f commit a86524e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 48 deletions.
31 changes: 23 additions & 8 deletions blockreassurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,28 @@
}

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

include_once _PS_MODULE_DIR_.'blockreassurance/reassuranceClass.php';

class Blockreassurance extends Module implements WidgetInterface
{
private $templateFile;

public function __construct()
{
$this->name = 'blockreassurance';
$this->tab = 'front_office_features';
$this->version = '1.0.4';
$this->author = 'PrestaShop';
$this->version = '1.0.4';

$this->bootstrap = true;
parent::__construct();

$this->displayName = $this->trans('Customer reassurance', array(), 'Modules.BlockReassurance');
$this->description = $this->trans('Adds an information block aimed at offering helpful information to reassure customers that your store is trustworthy.', array(), 'Modules.BlockReassurance');

$this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);

$this->templateFile = 'module:blockreassurance/views/templates/hook/blockreassurance.tpl';
}

public function install()
Expand Down Expand Up @@ -143,11 +148,13 @@ public function getContent()
} else {
$reassurance = new reassuranceClass();
}

$reassurance->copyFromPost();
$reassurance->id_shop = $this->context->shop->id;

if ($reassurance->validateFields(false) && $reassurance->validateFieldsLang(false)) {
$reassurance->save();

if (isset($_FILES['image']) && isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name'])) {
if ($error = ImageManager::validateUpload($_FILES['image'])) {
return false;
Expand All @@ -156,11 +163,12 @@ public function getContent()
} elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/img/reassurance-'.(int)$reassurance->id.'-'.(int)$reassurance->id_shop.'.jpg')) {
return false;
}

unlink($tmpName);
$reassurance->file_name = 'reassurance-'.(int)$reassurance->id.'-'.(int)$reassurance->id_shop.'.jpg';
$reassurance->save();
}
$this->_clearCache('blockreassurance.tpl');
$this->_clearCache('*');
} else {
$html .= '<div class="conf error">'.$this->trans('An error occurred while attempting to save.', array(), 'Admin.Notifications.Error').'</div>';
}
Expand Down Expand Up @@ -190,7 +198,7 @@ public function getContent()
unlink(dirname(__FILE__).'/img/'.$reassurance->file_name);
}
$reassurance->delete();
$this->_clearCache('blockreassurance.tpl');
$this->_clearCache('*');
Tools::redirectAdmin(AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'));
} else {
$content = $this->getListContent((int)Configuration::get('PS_LANG_DEFAULT'));
Expand Down Expand Up @@ -331,11 +339,18 @@ protected function initList()
return $helper;
}

protected function _clearCache($template, $cacheId = null, $compileId = null)
{
parent::_clearCache($this->templateFile);
}

public function renderWidget($hookName = null, array $configuration = [])
{
$this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
if (!$this->isCached($this->templateFile, $this->getCacheId('blockreassurance'))) {
$this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
}

return $this->fetch('module:blockreassurance/views/templates/hook/blockreassurance.tpl', $this->getCacheId());
return $this->fetch($this->templateFile, $this->getCacheId('blockreassurance'));
}

public function getWidgetVariables($hookName = null, array $configuration = [])
Expand All @@ -346,9 +361,9 @@ public function getWidgetVariables($hookName = null, array $configuration = [])
$element['image'] = $this->getImageURL($element['file_name']);
}

return [
return array(
'elements' => $elements,
];
);
}

public function installFixtures()
Expand Down
3 changes: 1 addition & 2 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<version><![CDATA[1.0.4]]></version>
<description><![CDATA[Adds an information block aimed at offering helpful information to reassure customers that your store is trustworthy.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[front_office_features]]></tab>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>
</module>
80 changes: 42 additions & 38 deletions reassuranceClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,53 @@

class reassuranceClass extends ObjectModel
{
/** @var integer reassurance id*/
public $id;
/** @var integer reassurance id*/
public $id;

/** @var integer reassurance id shop*/
public $id_shop;
/** @var integer reassurance id shop*/
public $id_shop;

/** @var string reassurance file name icon*/
public $file_name;
/** @var string reassurance file name icon*/
public $file_name;

/** @var string reassurance text*/
public $text;
/** @var string reassurance text*/
public $text;


/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'reassurance',
'primary' => 'id_reassurance',
'multilang' => true,
'fields' => array(
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
'file_name' => array('type' => self::TYPE_STRING, 'validate' => 'isFileName'),
// Lang fields
'text' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true),
)
);
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'reassurance',
'primary' => 'id_reassurance',
'multilang' => true,
'fields' => array(
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
'file_name' => array('type' => self::TYPE_STRING, 'validate' => 'isFileName'),
// Lang fields
'text' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true),
)
);

public function copyFromPost()
{
/* Classical fields */
foreach ($_POST AS $key => $value)
if (array_key_exists($key, $this) AND $key != 'id_'.$this->table)
$this->{$key} = $value;
public function copyFromPost()
{
/* Classical fields */
foreach ($_POST as $key => $value) {
if (array_key_exists($key, $this) and $key != 'id_'.$this->table) {
$this->{$key} = $value;
}
}

/* Multilingual fields */
if (sizeof($this->fieldsValidateLang))
{
$languages = Language::getLanguages(false);
foreach ($languages AS $language)
foreach ($this->fieldsValidateLang AS $field => $validation)
if (isset($_POST[$field.'_'.(int)($language['id_lang'])]))
$this->{$field}[(int)($language['id_lang'])] = $_POST[$field.'_'.(int)($language['id_lang'])];
}
}
/* Multilingual fields */
if (sizeof($this->fieldsValidateLang)) {
$languages = Language::getLanguages(false);
foreach ($languages as $language) {
foreach ($this->fieldsValidateLang as $field => $validation) {
if (isset($_POST[$field.'_'.(int)($language['id_lang'])])) {
$this->{$field}[(int)($language['id_lang'])] = $_POST[$field.'_'.(int)($language['id_lang'])];
}
}
}
}
}
}

0 comments on commit a86524e

Please sign in to comment.