bebeboutik/modules/categoryfamily/AdminCategoryFamily.php
Srv Bebeboutik 6c0978166c add modules
2016-01-04 12:49:26 +01:00

189 lines
7.2 KiB
PHP
Executable File

<?php
require_once _PS_ROOT_DIR_.'/modules/categoryfamily/models/CategoryFamily.php';
class AdminCategoryFamily extends AdminTab {
public function __construct() {
$this->table = 'category_family';
$this->className = 'CategoryFamilyCore';
$this->lang = true;
$this->edit = true;
$this->view = true;
$this->delete = true;
$this->fieldsDisplay = array(
'id_category_family' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'name' => array(
'title' => $this->l('Name'),
'width' => 100
),
'id_parent' => array(
'title' => $this->l('Parent'),
'width' => 100,
'callback' => 'getParentName'
),
);
parent::__construct();
}
public function getList($id_lang, $orderBy = NULL, $orderWay = NULL, $start = 0, $limit = NULL) {
global $cookie;
/* Manage default params values */
if (empty($limit))
$limit = ((!isset($cookie->{$this->table.'_pagination'})) ? $this->_pagination[1] : $limit = $cookie->{$this->table.'_pagination'});
if (!Validate::isTableOrIdentifier($this->table))
die (Tools::displayError('Table name is invalid:').' "'.$this->table.'"');
if (empty($orderBy))
$orderBy = $cookie->__get($this->table.'Orderby') ? $cookie->__get($this->table.'Orderby') : $this->_defaultOrderBy;
if (empty($orderWay))
$orderWay = $cookie->__get($this->table.'Orderway') ? $cookie->__get($this->table.'Orderway') : 'ASC';
$limit = (int)(Tools::getValue('pagination', $limit));
$cookie->{$this->table.'_pagination'} = $limit;
/* Check params validity */
if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay)
OR !is_numeric($start) OR !is_numeric($limit)
OR !Validate::isUnsignedId($id_lang))
die(Tools::displayError('get list params is not valid'));
/* Determine offset from current page */
if ((isset($_POST['submitFilter'.$this->table]) OR
isset($_POST['submitFilter'.$this->table.'_x']) OR
isset($_POST['submitFilter'.$this->table.'_y'])) AND
!empty($_POST['submitFilter'.$this->table]) AND
is_numeric($_POST['submitFilter'.$this->table]))
$start = (int)($_POST['submitFilter'.$this->table] - 1) * $limit;
/* Cache */
$this->_lang = (int)($id_lang);
$this->_orderBy = $orderBy;
$this->_orderWay = Tools::strtoupper($orderWay);
/* SQL table : orders, but class name is Order */
$sqlTable = $this->table == 'order' ? 'orders' : $this->table;
if (Tools::getIsset('viewcategory_family')) {
if (Tools::getValue('id_category_family')) {
$this->_where = ' AND id_parent = '. (int)Tools::getValue('id_category_family');
}
} else {
$this->_where = ' AND id_parent = 0';
}
/* Query in order to get results with all fields */
$sql = 'SELECT SQL_CALC_FOUND_ROWS
'.($this->_tmpTableFilter ? ' * FROM (SELECT ' : '').'
'.($this->lang ? 'b.*, ' : '').'a.*'.(isset($this->_select) ? ', '.$this->_select.' ' : '').'
FROM `'._DB_PREFIX_.$sqlTable.'` a
'.($this->lang ? 'LEFT JOIN `'._DB_PREFIX_.$this->table.'_lang` b ON (b.`'.$this->identifier.'` = a.`'.$this->identifier.'` AND b.`id_lang` = '.(int)($id_lang).')' : '').'
'.(isset($this->_join) ? $this->_join.' ' : '').'
WHERE 1 '.(isset($this->_where) ? $this->_where.' ' : '').($this->deleted ? 'AND a.`deleted` = 0 ' : '').(isset($this->_filter) ? $this->_filter : '').'
'.(isset($this->_group) ? $this->_group.' ' : '').'
'.((isset($this->_filterHaving) || isset($this->_having)) ? 'HAVING ' : '').(isset($this->_filterHaving) ? ltrim($this->_filterHaving, ' AND ') : '').(isset($this->_having) ? $this->_having.' ' : '').'
ORDER BY '.(($orderBy == $this->identifier) ? 'a.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).
($this->_tmpTableFilter ? ') tmpTable WHERE 1'.$this->_tmpTableFilter : '').'
LIMIT '.(int)($start).','.(int)($limit);
$this->_list = Db::getInstance()->ExecuteS($sql);
$this->_listTotal = Db::getInstance()->getValue('SELECT FOUND_ROWS() AS `'._DB_PREFIX_.$this->table.'`');
}
public function viewcategory_family() {
global $cookie;
$this->getList($cookie->id_lang);
$this->displayList();
}
public function postProcess() {
if (Tools::isSubmit('addFamily')) {
$family = new CategoryFamilyCore();
$family = $this->assocForm($family, $_POST);
if ($family->add()) {
return TRUE;
}
} elseif (Tools::isSubmit('editFamily')) {
$id_category_family = Tools::getValue('id_category_family');
$family = new CategoryFamilyCore($id_category_family, $cookie->id_lang);
$family = $this->assocForm($family, $_POST);
$family->update();
}
parent::postProcess();
}
public function assocForm($obj, $data) {
$obj->id_parent = ($data['parent'] ? $data['parent'] : 0);
foreach (Language::getLanguages(FALSE) as $language) {
$id_lang = $language['id_lang'];
if ( isset($data['name_'.$id_lang]) ) {
$obj->name[$id_lang] = $data['name_'.$id_lang];
} else {
$obj->name[$id_lang] = '';
}
}
return $obj;
}
public function displayForm() {
global $currentIndex, $cookie;
parent::displayForm();
$parents = CategoryFamilyCore::getAll($cookie->id_lang);
if (Tools::getIsset('updatecategory_family')) {
$id_category_family = Tools::getValue('id_category_family');
$obj = new CategoryFamilyCore($id_category_family);
if(!Validate::isLoadedObject($obj)) {
echo '<div class="alert">
<p>This Category not exist</p>
</div>';
return FALSE;
}
}
echo '
<form action="'.$currentIndex.'&token='.($token!=NULL ? $token : $this->token).'" method="post" enctype="multipart/form-data">
'.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" />' : '').'
<fieldset><legend><img src="../img/admin/tab-categories.gif" />'.$this->l('Category Family').'</legend>
<label>'.$this->l('Name:').' </label>
<div class="margin-form translatable">';
foreach ($this->_languages AS $language)
echo '
<div class="lang_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
<input type="text" style="width: 260px" name="name_'.$language['id_lang'].'" id="name_'.$language['id_lang'].'" value="'.htmlentities($this->getFieldValue($obj, 'name', (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8').'" /><sup> *</sup>
<span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}<span class="hint-pointer">&nbsp;</span></span>
</div>';
echo '<p class="clear"></p>
</div>
<label>'.$this->l('Parent:').' </label>
<select name="parent" id="parent">
<option value="0">'.$this->l('-- Root Family --').'</option>';
foreach ($parents as $parent) {
echo '<option '.(isset($obj) && $obj->id_parent == $parent['id_category_family'] ? 'selected=selected' : '').' value="'.$parent['id_category_family'].'">'.$parent['name'].'</option>';
}
echo '</select>';
echo '<p class="clear"></p>
<div class="margin-form">
<input type="submit" '.(isset($obj->id)? 'disabled=disabled' : '' ).' class="button" name="addFamily" value="'.$this->l('Save').'"/>';
if (isset($obj->id)) {
echo ' <input type="submit" class="button" name="editFamily" value="'.$this->l('Edit').'"/>';
}
echo '</div>
<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
</fieldset>
</form>';
}
}