bebeboutik/modules/purchaseguide/AdminGuideCategories.php
Michael RICOIS 1c5205a2a8 Admin
2017-11-17 11:42:47 +01:00

389 lines
12 KiB
PHP

<?php
require_once(__DIR__.'/classes/GuideCategory.php');
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;
$this->edit = true;
$this->delete = true;
$this->view = true;
$this->fieldsDisplay = array(
'id_guide_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 30
),
'name' => array(
'title' => $this->l('Title'),
'width' => 100
),
'description' => array(
'title' => $this->l('Description'),
'width' => 500,
'maxlength' => 90,
'orderby' => false
),
'position' => array(
'title' => $this->l('Position'),
'width' => 40,
'filter_key' => 'position',
'align' => 'center',
'position' => 'position'
),
'active' => array(
'title' => $this->l('Displayed'),
'active' => 'status',
'align' => 'center',
'type' => 'bool',
'orderby' => false
)
);
if (isset($_GET['delete'.$this->table])) {
self::$current_category = new GuideCategory((int)(Tools::getValue('id_guide_category_parent', 0)));
}
else {
self::$current_category = new GuideCategory((int)(Tools::getValue('id_guide_category', Tools::getValue('id_guide_category_parent', 0))));
}
$this->_filter = 'AND a.`id_parent` = '.(int)(self::$current_category->id);
$this->_select = 'position ';
$this->identifiersDnd['id_guide_category'] = 'id_guide_category';
parent::__construct();
}
public function postProcess()
{
if (isset($_GET['delete'.$this->table]) || Tools::getValue('submitDel'.$this->table)) {
$this->postProcessDelete();
return;
}
if (self::$current_category->id != 0) {
$this->noLink = true;
}
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()
{
global $currentIndex;
if ($this->tabAccess['delete'] === '1')
{
if (isset($_GET['delete'.$this->table])) {
$object = $this->loadObject();
if (Validate::isLoadedObject($object)) {
$result = $object->delete();
if (!$result) {
if ($object->getDeleteError()==1) {
$this->_errors[] = Tools::displayError($this->l('The category has at least one sub category'));
}
else if ($object->getDeleteError()==2) {
$this->_errors[] = Tools::displayError($this->l('The category contains at least one page'));
}
else {
$this->_errors[] = Tools::displayError('An error occurred during deletion.');
}
}
else {
Tools::redirectAdmin($currentIndex.'&id_guide_category_parent='.self::$current_category->id.'&conf=1&token='.$this->token);
}
}
else {
$this->_errors[] = Tools::displayError('An error occurred while deleting object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
}
return;
}
elseif (Tools::getValue('submitDel'.$this->table)) {
$object = new GuideCategory();
$result = $object->deleteSelection(Tools::getValue($this->table.'Box'));
if (!$result) {
if ($object->getDeleteError()==1) {
$this->_errors[] = Tools::displayError($this->l('One of the selected categories has at least one sub category'));
}
elseif ($object->getDeleteError()==2) {
$this->_errors[] = Tools::displayError($this->l('One of the selected categories contains at least one page'));
}
else {
$this->_errors[] = Tools::displayError('An error occurred while deleting selection.');
}
}
else {
Tools::redirectAdmin($currentIndex.'&id_guide_category_parent='.self::$current_category->id.'&conf=1&token='.$this->token);
}
return;
}
}
}
protected function _displayEditLink($token = NULL, $id)
{
global $currentIndex;
$_cacheLang['Edit'] = $this->l('Edit');
echo '
<a href="'.$currentIndex.'&'.$this->identifier.'='.$id.'&update'.$this->table.
'&token='.($token!=NULL ? $token : $this->token).
(self::$current_category->id==0 ? '' : '&id_guide_category_parent='.self::$current_category->id).'">
<img src="../img/admin/edit.gif" alt="" title="'.$_cacheLang['Edit'].'" /></a>';
}
protected function _displayDeleteLink($token = NULL, $id)
{
global $currentIndex;
$_cacheLang['Delete'] = $this->l('Delete');
$_cacheLang['DeleteItem'] = $this->l('Delete item #', __CLASS__, TRUE, FALSE);
echo '
<a href="'.$currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&token='.($token!=NULL ? $token : $this->token).'&id_guide_category_parent='.self::$current_category->id.'" onclick="return confirm(\''.$_cacheLang['DeleteItem'].$id.' ?'.
(!is_null($this->specificConfirmDelete) ? '\r'.$this->specificConfirmDelete : '').'\');">
<img src="../img/admin/delete.gif" alt="'.$_cacheLang['Delete'].'" title="'.$_cacheLang['Delete'].'" /></a>';
}
protected function _displayViewLink($token = NULL, $id)
{
$category = new GuideCategory($id);
if ($category->id_parent != 0) {
$this->_displayPostLink($token, $id);
}
global $currentIndex;
$_cacheLang['View'] = $this->l('View');
echo '
<a href="'._PS_BASE_URL_SSL_.'/modules/purchaseguide/post.php?cid='.$id.'" target="_blank">
<img src="../img/admin/details.gif" alt="'.$_cacheLang['View'].'" title="'.$_cacheLang['View'].'" /></a>';
}
protected function _displayPostLink($token = NULL, $id)
{
$_cacheLang['Post'] = $this->l('View Post');
$currentIndex = '?tab=AdminGuidePosts';
$token = Tools::getAdminTokenLite('AdminGuidePosts');
echo '
<a href="'.$currentIndex.'&'.$this->identifier.'='.$id.
'&token='.($token!=NULL ? $token : $this->token).'">
<img src="../img/admin/arrow-right.png" alt="" title="'.$_cacheLang['Post'].'" /></a>';
}
public static function getCurrentCategory()
{
return self::$current_category;
}
public function displayForm($token = NULL)
{
global $currentIndex, $cookie, $smarty;
parent::displayForm();
if (!($obj = $this->loadObject(true))) {
return;
}
$langs = Language::getLanguages(false);
$form = new FormBuilder('AdminGuideCategories', $this->table, 'id_guide_category', 'Add');
$this->fieldsForm = array(
'id_parent' => array(
'title' => $this->l('Catégorie parent'),
'type' => 'select',
'options_raw' => GuideCategory::findCategoriesTree($cookie->id_lang, $obj->id),
'options_map' => ['id_guide_category', 'name', 'level'],
'empty_option' => ['value'=>'0', 'label'=>$this->l('No parent category')],
'initial_value' => $obj->id_parent?$obj->id_parent:self::$current_category->id
),
'id_category_family' => array(
'title' => $this->l('Famille'),
'type' => 'select',
'options_raw' => GuideCategory::findFamiliesTree($cookie->id_lang),
'options_map' => ['id_category_family', 'name', 'level'],
'initial_value' => $obj->id_category_family
),
'name' => array(
'title' => $this->l('Title'),
'type' => 'text',
'required' => true,
'translatable' => true,
'attrs' => [
'onkeyup' => 'copy2friendlyURL();'
]
),
'description' => array(
'title' => $this->l('Description'),
'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().'?rand='.time()
]
),
'active' => array(
'title' => $this->l('Displayed'),
'type' => 'yesno',
'required' => true,
),
'link_rewrite' => array(
'title' => $this->l('Simplified URL'),
'type' => 'text',
'required' => true,
),
'meta_title' => array(
'title' => $this->l('Title (META)'),
'type' => 'text',
'translatable' => true,
),
'meta_description' => array(
'title' => $this->l('Description (META)'),
'type' => 'textarea',
'translatable' => true,
),
);
$form->setTitle($this->l('Purchase guide category'));
$form->setFields($this->fieldsForm);
$form->enableSubmitAndBackToParent();
$form->setSubmitButton($this->l('Save'));
$form->display($smarty, $langs, $obj);
$currentIndex .= '&id_guide_category='.($obj->id?$obj->id_parent:self::$current_category->id);
}
public function displayTop()
{
global $currentIndex, $cookie;
$clean_currentIndex = $currentIndex;
$parent_category = 0;
if (Validate::isLoadedObject(self::$current_category)) {
$parent_category = new GuideCategory(self::$current_category->id_parent);
if (Validate::isLoadedObject($parent_category)) {
echo '<a href="'.$clean_currentIndex.'&token='.$this->token.'&id_guide_category='.$parent_category->id.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('back to').' '.$parent_category->name[$cookie->id_lang].'</a><br /><br />';
}
else {
echo '<a href="'.$clean_currentIndex.'&token='.$this->token.'"><img src="../img/admin/arrow2.gif" /> '.$this->l('back').'</a><br /><br />';
}
echo $this->l('Current category').' : '.self::$current_category->name[$cookie->id_lang].'<br /><br />';
}
}
public function displayList()
{
global $currentIndex;
$this->displayTop();
if ($this->edit AND (!isset($this->noAdd) OR !$this->noAdd)) {
echo '<br /><a href="'.$currentIndex.'&id_guide_category_parent='.self::$current_category->id.'&add'.$this->table.'&token='.$this->token.'"><img src="../img/admin/add.gif" border="0" /> '.$this->l('Add new').'</a><br /><br />';
}
// Append when we get a syntax error in SQL query
if ($this->_list === false) {
$this->displayWarning($this->l('Bad SQL query'));
return false;
}
// Display list header (filtering, pagination and column names)
$this->displayListHeader();
if (!sizeof($this->_list)) {
echo '<tr><td class="center" colspan="'.(sizeof($this->fieldsDisplay) + 2).'">'.$this->l('No items found').'</td></tr>';
}
$this->_orderBy = 'position';
// Show the content of the table
$this->displayListContent();
// Close list table and submit button
$this->displayListFooter();
}
public function viewguide_category()
{
global $cookie;
$this->getList((int)($cookie->id_lang));
$this->displayList();
$this->displayOptionsList();
$this->displayRequiredFields();
$this->includeSubTab('display');
}
}