add image upload in categories

This commit is contained in:
Rodney Figaro 2016-08-31 17:05:56 +02:00
parent 762a82825b
commit 8480abaee4
8 changed files with 232 additions and 6 deletions

2
.gitignore vendored
View File

@ -117,3 +117,5 @@ modules/logistics/carriers/laposte/summaries/*.pdf
modules/labelgenerate/img/
modules/product_vouchers/*.csv
modules/labelgenerate/img/*
modules/purchaseguide/img/*

View File

@ -3,5 +3,45 @@ class AdminGuide extends AdminTab
{
public function display()
{
global $smarty;
if(Configuration::get('PS_REWRITING_SETTINGS')) {
$module_name = Module::getModuleNameFromClass('AdminGuide');
$langs = Language::getLanguages(true);
$urls_i18n = array(
'fr' => 'guide',
'en' => 'guide',
);
$str_rewrite_rules = 'RewriteRule ^%s/([0-9]+)\-[a-zA-Z0-9-]*$ '.__PS_BASE_URI__.'modules/'.$module_name.'/page.php?id_guide_post=$1 [QSA,L]'."\n";
if(count($langs) > 1) {
$rewrite_rules = '';
foreach($langs as $lang) {
if(isset($urls_i18n[$lang['iso_code']])) {
$index = $lang['iso_code'];
} else {
$index = 'en';
}
$rewrite_rules .= sprintf($str_rewrite_rules, $lang['iso_code'].'/'.$urls_i18n[$index]);
}
} else {
if(isset($urls_i18n[$langs[0]['iso_code']])) {
$index = $langs[0]['iso_code'];
} else {
$index = 'en';
}
$rewrite_rules = sprintf($str_rewrite_rules, $urls_i18n[$index]);
}
$smarty->assign('rewrite_rules', htmlentities($rewrite_rules));
echo $smarty->fetch(__DIR__.'/templates/home_adminguide.tpl');
}
}
}

View File

@ -4,13 +4,18 @@ require_once(__DIR__.'/classes/FormBuilder.php');
class AdminGuideCategories extends AdminTab
{
const IMG_WIDTH = 300;
const IMG_HEIGHT = 200;
private static $current_category = 0;
private static $category_parent = 0;
private $maxImageBytes = 0;
public function __construct()
{
global $cookie;
$this->maxImageBytes = (Configuration::get('PS_LIMIT_UPLOAD_IMAGE_VALUE') * 1000000);
$this->table = 'guide_category';
$this->className = 'GuideCategory';
$this->lang = true;
@ -122,6 +127,16 @@ class AdminGuideCategories extends AdminTab
'type' => 'textarea',
'translatable' => true,
),
'image' => array(
'title' => $this->l('Image'),
'type' => 'file',
'file_attrs' => [
'max_bytes' => $this->maxImageBytes,
'allowed_format' => 'JPEG',
'html_file' => "img",
'path' => $obj->getImageFileUrl()
]
),
'active' => array(
'title' => $this->l('Displayed'),
'type' => 'yesno',
@ -238,10 +253,60 @@ class AdminGuideCategories extends AdminTab
<img src="../img/admin/delete.gif" alt="'.$_cacheLang['Delete'].'" title="'.$_cacheLang['Delete'].'" /></a>';
}
public function postProcess()
{
global $currentIndex;
public function postProcess()
{
if (isset($_GET['delete'.$this->table]) ||
Tools::getValue('submitDel'.$this->table)) {
$this->postProcessDelete();
return;
}
parent::postProcess();
}
protected function afterUpdate()
{
return $this->afterAdd();
}
protected function afterAdd()
{
if (isset($_FILES['image']['tmp_name']) &&
$_FILES['image']['tmp_name'] != NULL) {
$object = $this->loadObject();
$error = checkImage( $_FILES['image'], $this->maxImageSize);
if ($error) {
$this->_errors[] = $error;
return false;
}
$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS');
if (!$tmpName OR !move_uploaded_file($_FILES['image']['tmp_name'], $tmpName)) {
$this->_errors[] = Tools::displayError('An error occurred during the image upload');
return false;
}
$new_path = $object->getImageFilePath();
if (file_exists($new_path)) {
@unlink($new_path);
}
if (!imageResize($tmpName, $new_path, self::IMG_WIDTH, self::IMG_HEIGHT)) {
$this->_errors[] = Tools::displayError('An error occurred while copying image.');
return false;
}
@unlink($tmpName);
}
return true;
}
private function postProcessDelete()
{
if ($this->tabAccess['delete'] === '1')
{
if (isset($_GET['delete'.$this->table])) {
@ -291,7 +356,5 @@ class AdminGuideCategories extends AdminTab
return;
}
}
parent::postProcess();
}
}

View File

@ -70,9 +70,12 @@ class FormBuilder
$field['name'] = $key;
$field['value'] = '';
$field['html_attrs'] = '';
$field['allowed_format'] = '';
$field['max_size'] = '';
$this->prepareTemplatePath($field);
$this->prepareInputHtmlAttributes($field);
$this->prepareFileInput($field);
$this->prepareSelectOptions($field);
$this->prepareTinyMCE($field, $cookie);
$this->updateValueFields($field, $obj, $key, $langs);
@ -84,6 +87,35 @@ class FormBuilder
$field['template'] = self::FORM_DIR.$field['type'].'.tpl';
}
private function prepareFileInput(&$field)
{
if (isset($field['file_attrs']) && is_array($field['file_attrs'])) {
if (isset($field['file_attrs']['path'])) {
$field['file_path'] = $field['file_attrs']['path'];
}
if (isset($field['file_attrs']['html_file'])) {
$field['html_file'] = $field['file_attrs']['html_file'];
}
if (isset($field['file_attrs']['allowed_format'])) {
$field['allowed_format'] = $field['file_attrs']['allowed_format'];
}
if (isset($field['file_attrs']['max_bytes'])) {
$max_size = intval($field['file_attrs']['max_bytes'])/1000;
if ($max_size > 1000) {
$field['max_size'] = ($max_size/1000).' Mb';
}
else {
$field['max_size'] = $max_size.' Kb';
}
}
}
}
private function prepareInputHtmlAttributes(&$field)
{
if (isset($field['attrs']) && is_array($field['attrs'])) {

View File

@ -56,6 +56,26 @@ class GuideCategory extends ObjectModel
return $fields;
}
public static function getSubPath()
{
return 'modules/purchaseguide/img';
}
public function getImageFilePath()
{
if ($this->id) {
return _PS_ROOT_DIR_.'/'.self::getSubPath().'/'.$this->id.'.jpg';
}
return '';
}
public function getImageFileUrl()
{
if ($this->id) {
return __PS_BASE_URI__.self::getSubPath().'/'.$this->id.'.jpg';
}
return '';
}
public static function findCategoriesTree($id_lang, $exclude_id=0)
{

View File

@ -2,6 +2,8 @@
if (!defined('_PS_VERSION_'))
exit;
require_once(__DIR__.'/classes/GuideCategory.php');
class PurchaseGuide extends Module
{
const MODULE_NAME = 'purchaseguide';
@ -23,6 +25,7 @@ class PurchaseGuide extends Module
if(!parent::install()
|| !$this->installTabs()
|| !$this->createTables()
|| !$this->installImageAddon()
) {
$this->uninstall();
return FALSE;
@ -36,6 +39,7 @@ class PurchaseGuide extends Module
{
$this->uninstallTabs();
$this->dropTables();
$this->uninstallImageAddon();
return parent::uninstall();
}
@ -217,4 +221,31 @@ class PurchaseGuide extends Module
}
}
private function installImageAddon()
{
// Add image type
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'image_type` VALUES (
DEFAULT, "guide_category", 980, 480, 0, 0, 0, 0, 0, 0
)
');
// Add image folder
$path = GuideCategory::getImagePath();
if(!is_dir($path)) {
mkdir($path, 0775);
}
return true;
}
private function uninstallImageAddon()
{
// Remove image type
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'image_type` WHERE `name` = "guide_category"
');
}
}

View File

@ -0,0 +1,29 @@
<label>{$input.title} : </label>
<div class="margin-form">
{if $input.file_path != ''}
{if $input.html_file == 'img'}
<div style="margin-bottom:20px;width: 200px; height:150px; background:url('{$input.file_path}');background-size:cover;background-repeat: no-repeat;">
</div>
{/if}
{/if}
<input type="file" style="width: 260px" name="{$input.name}" id="{$input.name}" value="{$input.name}" {$input.html_attrs} >
{if isset($input.required) && $input.required}
<sup> *</sup>
{/if}
<p>
{if $input.allowed_format}
{l s='Format'} : {$input.allowed_format}.
{/if}
{if $input.max_size}
File size'} :
{$input.max_size} {l s='max'}.
{/if}
</p>
<p class="clear"></p>
</div>

View File

@ -0,0 +1,9 @@
<br /><br />
<fieldset>
<legend><img src="../img/admin/tab-tools.gif" alt="" />{l s='URL Rewriting settings'}</legend>
<p>{l s='Add the following text to the custom rewriting rules (Tools &gt; Generators'} :</p>
<p> </p>
<textarea style="width: 850px; height: 120px;" readonly="true">{$rewrite_rules}</textarea>
</fieldset>