* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
// @since 1.4.5.0
// add the following comment in a module file to skip it in translations
// IGNORE_THIS_FILE_FOR_TRANSLATION
abstract class AdminSelfTab
{
/** @var integer Tab id */
public $id = -1;
/** @var string Associated table name */
public $table;
/** @var string Object identifier inside the associated table */
protected $identifier = false;
/** @var string Tab name */
public $name;
/** @var string Security token */
public $token;
/** @var boolean Automatically join language table if true */
public $lang = false;
/** @var boolean Tab Automatically displays edit/delete icons if true */
public $edit = false;
/** @var boolean Tab Automatically displays view icon if true */
public $view = false;
/** @var boolean Tab Automatically displays delete icon if true */
public $delete = false;
/** @var boolean Table records are not deleted but marked as deleted */
public $deleted = false;
/** @var boolean Tab Automatically displays duplicate icon if true */
public $duplicate = false;
/** @var boolean Content line is clickable if true */
public $noLink = false;
/** @var boolean select other required fields */
public $requiredDatabase = false;
/** @var boolean Tab Automatically displays '$color' as background color on listing if true */
public $colorOnBackground = false;
/** @var string Add fields into data query to display list */
protected $_select;
/** @var string Join tables into data query to display list */
protected $_join;
/** @var string Add conditions into data query to display list */
protected $_where;
/** @var string Group rows into data query to display list */
protected $_group;
/** @var string Having rows into data query to display list */
protected $_having;
/** @var array Name and directory where class image are located */
public $fieldImageSettings = array();
/** @var string Image type */
public $imageType = 'jpg';
/** @var array Fields to display in list */
public $fieldsDisplay = array();
/** @var array Cache for query results */
protected $_list = array();
/** @var integer Number of results in list */
protected $_listTotal = 0;
/** @var string WHERE clause determined by filter fields */
protected $_filter;
/** @var string HAVING clause determined by filter fields */
protected $_filterHaving;
/** @var array Temporary SQL table WHERE clause determinated by filter fields */
protected $_tmpTableFilter = '';
/** @var array Number of results in list per page (used in select field) */
protected $_pagination = array(20, 50, 100, 300);
/** @var string ORDER BY clause determined by field/arrows in list header */
protected $_orderBy;
/** @var string Default ORDER BY clause when $_orderBy is not defined */
protected $_defaultOrderBy = false;
/** @var string Order way (ASC, DESC) determined by arrows in list header */
protected $_orderWay;
/** @var integer Max image size for upload */
protected $maxImageSize = 2000000;
/** @var array Errors displayed after post processing */
public $_errors = array();
/** @var array Confirmations displayed after post processing */
protected $_conf;
/** @var object Object corresponding to the tab */
protected $_object = false;
/** @var array tabAccess */
public $tabAccess;
/** @var string specificConfirmDelete */
public $specificConfirmDelete = null;
protected $identifiersDnd = array('id_product' => 'id_product', 'id_category' => 'id_category_to_move','id_cms_category' => 'id_cms_category_to_move', 'id_cms' => 'id_cms');
/** @var bool Redirect or not ater a creation */
protected $_redirect = true;
protected $_languages = null;
protected $_defaultFormLanguage = null;
protected $_includeObj = array();
protected $_includeVars = false;
protected $_includeContainer = true;
public $ajax = false;
public static $tabParenting = array(
'AdminProducts' => 'AdminCatalog',
'AdminCategories' => 'AdminCatalog',
'AdminCMS' => 'AdminCMSContent',
'AdminCMSCategories' => 'AdminCMSContent',
'AdminOrdersStates' => 'AdminStatuses',
'AdminAttributeGenerator' => 'AdminProducts',
'AdminAttributes' => 'AdminAttributesGroups',
'AdminFeaturesValues' => 'AdminFeatures',
'AdminReturnStates' => 'AdminStatuses',
'AdminStatsTab' => 'AdminStats'
);
public function __construct()
{
global $cookie;
$this->id = Tab::getCurrentTabId();
$this->_conf = array(
1 => $this->trans('Successful deletion.', array(), 'Admin.Notifications.Success'), 2 => $this->trans('Selection successfully deleted', array(), 'Modules.Autoupgrade.Admin'),
3 => $this->trans('Successful creation.', array(), 'Admin.Notifications.Success'), 4 => $this->trans('Successful update.', array(), 'Admin.Notifications.Success'),
5 => $this->trans('The new version check has been completed successfully', array(), 'Modules.Autoupgrade.Admin'), 6 => $this->trans('The settings have been successfully updated.', array(), 'Admin.Notifications.Success'),
7 => $this->trans('The image was successfully deleted.', array(), 'Admin.Notifications.Success'), 8 => $this->trans('The module was successfully downloaded.', array(), 'Admin.Notifications.Success'),
9 => $this->trans('The thumbnails were successfully regenerated.', array(), 'Admin.Notifications.Success'), 10 => $this->trans('Message sent to the customer', array(), 'Modules.Autoupgrade.Admin'),
11 => $this->trans('Comment successfully added.', array(), 'Admin.Notifications.Success'), 12 => $this->trans('Module(s) installed successfully.', array(), 'Admin.Modules.Notification'),
13 => $this->trans('Module(s) uninstalled successfully.', array(), 'Admin.Modules.Notification'), 14 => $this->trans('Language successfully copied', array(), 'Modules.Autoupgrade.Admin'),
15 => $this->trans('The translations have been successfully added.', array(), 'Admin.International.Notification'), 16 => $this->trans('The module transplanted successfully to the hook.', array(), 'Admin.Modules.Notification'),
17 => $this->trans('The module was successfully removed from the hook.', array(), 'Admin.Modules.Notification'), 18 => $this->trans('Successful upload.', array(), 'Admin.Notifications.Success'),
19 => $this->trans('Duplication was completed successfully.', array(), 'Admin.Notifications.Success'), 20 => $this->trans('The translation was added successfully, but the language has not been created.', array(), 'Admin.International.Notification'),
21 => $this->trans('Module reset successfully.', array(), 'Admin.Modules.Notification'), 22 => $this->trans('Module deleted successfully.', array(), 'Admin.Modules.Notification'),
23 => $this->trans('Localization pack imported successfully.', array(), 'Admin.International.Notification'), 24 => $this->trans('Refund Successful', array(), 'Modules.Autoupgrade.Admin'),
25 => $this->trans('Images successfully moved', array(), 'Modules.Autoupgrade.Admin'));
if (!$this->identifier) {
$this->identifier = 'id_'.$this->table;
}
if (!$this->_defaultOrderBy) {
$this->_defaultOrderBy = $this->identifier;
}
$className = get_class($this);
if ($className == 'AdminCategories' or $className == 'AdminProducts') {
$className = 'AdminCatalog';
}
$this->token = Tools14::getAdminToken($className.(int)$this->id.(int)$cookie->id_employee);
}
private function getConf($fields, $languages)
{
$tab = array();
foreach ($fields as $key => $field) {
if ($field['type'] == 'textLang') {
foreach ($languages as $language) {
$tab[$key.'_'.$language['id_lang']] = Tools14::getValue($key.'_'.$language['id_lang'], Configuration::get($key, $language['id_lang']));
}
} else {
$tab[$key] = Tools14::getValue($key, Configuration::get($key));
}
}
$tab['__PS_BASE_URI__'] = __PS_BASE_URI__;
$tab['_MEDIA_SERVER_1_'] = defined('_MEDIA_SERVER_1_')?_MEDIA_SERVER_1_:'';
$tab['PS_THEME'] = _THEME_NAME_;
if (defined('_DB_TYPE_')) {
$tab['db_type'] = _DB_TYPE_;
} else {
$tab['db_type'] = 'mysql';
}
$tab['db_server'] = _DB_SERVER_;
$tab['db_name'] = _DB_NAME_;
$tab['db_prefix'] = _DB_PREFIX_;
$tab['db_user'] = _DB_USER_;
$tab['db_passwd'] = '';
return $tab;
}
private function getDivLang($fields)
{
$tab = array();
foreach ($fields as $key => $field) {
if ($field['type'] == 'textLang' || $field['type'] == 'selectLang') {
$tab[] = $key;
}
}
return implode('ยค', $tab);
}
private function getVal($conf, $key)
{
return Tools14::getValue($key, (isset($conf[$key]) ? $conf[$key] : ''));
}
protected function _displayForm($name, $fields, $tabname, $size, $icon)
{
global $currentIndex;
$defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
$languages = Language::getLanguages(false);
$confValues = $this->getConf($fields, $languages);
$divLangName = $this->getDivLang($fields);
$required = false;
echo '
';
}
/**
* use translations files to replace english expression.
*
* @param mixed $string term or expression in english
* @param string $class
* @param boolan $addslashes if set to true, the return value will pass through addslashes(). Otherwise, stripslashes().
* @param boolean $htmlentities if set to true(default), the return value will pass through htmlentities($string, ENT_QUOTES, 'utf-8')
* @return string the translation if available, or the english default text.
*/
protected function l($string, $class = 'AdminTab', $addslashes = false, $htmlentities = true)
{
global $_LANGADM;
if (empty($_LANGADM)) {
$_LANGADM = array();
}
// if the class is extended by a module, use modules/[module_name]/xx.php lang file
$currentClass = get_class($this);
if (class_exists('Module') and method_exists('Module', 'getModuleNameFromClass')) {
if (Module::getModuleNameFromClass($currentClass)) {
$string = str_replace('\'', '\\\'', $string);
return Module::findTranslation(Module::$classInModule[$currentClass], $string, $currentClass);
}
}
if ($class == __CLASS__) {
$class = 'AdminTab';
}
$key = md5(str_replace('\'', '\\\'', $string));
$str = (key_exists(get_class($this).$key, $_LANGADM)) ? $_LANGADM[get_class($this).$key] : ((key_exists($class.$key, $_LANGADM)) ? $_LANGADM[$class.$key] : $string);
$str = $htmlentities ? htmlentities($str, ENT_QUOTES, 'utf-8') : $str;
return str_replace('"', '"', ($addslashes ? addslashes($str) : stripslashes($str)));
}
public function getTranslator()
{
return Context::getContext()->getTranslator();
}
protected function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
$parameters['legacy'] = 'htmlspecialchars';
return $this->getTranslator()->trans($id, $parameters, $domain, $locale);
}
/**
* ajaxDisplay is the default ajax return sytem
*
* @return void
*/
public function displayAjax()
{
}
/**
* Manage page display (form, list...)
*
* @global string $currentIndex Current URL in order to keep current Tab
*/
public function display()
{
global $currentIndex, $cookie;
// Include other tab in current tab
if ($this->includeSubTab('display', array('submitAdd2', 'add', 'update', 'view'))) {
}
// Include current tab
elseif ((Tools14::getValue('submitAdd'.$this->table) and sizeof($this->_errors)) or isset($_GET['add'.$this->table])) {
if ($this->tabAccess['add'] === '1') {
$this->displayForm();
if ($this->tabAccess['view']) {
echo '
';
}
$this->includeSubTab('displayErrors');
}
/**
* Display a warning message
*
* @param string $warn Warning message to display
*/
public function displayWarning($warn)
{
$str_output = '';
if (!empty($warn)) {
$str_output .= '
';
if (!is_array($warn)) {
if (file_exists(__PS_BASE_URI__.'img/admin/warn2.png')) {
$str_output .= '';
} else {
$str_output .= '';
}
$str_output .= $warn;
} else {
$str_output .= '';
if (count($warn) > 1) {
$str_output .= $this->trans('There are %count% warnings', array('%count%' => count($warn)), 'Modules.Autoupgrade.Admin');
} else {
$str_output .= $this->trans('There is %count% warning', array('%count%' => count($warn)), 'Modules.Autoupgrade.Admin');
}
$str_output .= ''.$this->trans('Click here to see more', array(), 'Modules.Autoupgrade.Admin').''.$this->trans('Hide warning', array(), 'Modules.Autoupgrade.Admin').'
';
foreach ($warn as $val) {
$str_output .= '
'.$val.'
';
}
$str_output .= '
';
}
$str_output .= '
';
}
echo $str_output;
}
/**
* Display confirmations
*/
public function displayConf()
{
if ($conf = Tools14::getValue('conf')) {
echo '
'.$this->_conf[(int)($conf)].'
';
}
}
public function displayTop()
{
}
protected function _displayEnableLink($token, $id, $value, $active, $id_category = null, $id_product = null)
{
global $currentIndex;
echo '';
}
protected function _displayDuplicate($token = null, $id)
{
global $currentIndex;
$_cacheLang['Duplicate'] = $this->trans('Duplicate', array(), 'Admin.Actions');
$_cacheLang['Copy images too?'] = $this->trans('This will copy the images too. If you wish to proceed, click "Yes". If not, click "No".', array(), 'Admin.Catalog.Notification');
$duplicate = $currentIndex.'&'.$this->identifier.'='.$id.'&duplicate'.$this->table;
echo '
';
}
protected function _displayViewLink($token = null, $id)
{
global $currentIndex;
$_cacheLang['View'] = $this->trans('View', array(), 'Admin.Actions');
echo '
';
}
protected function _displayEditLink($token = null, $id)
{
global $currentIndex;
$_cacheLang['Edit'] = $this->trans('Edit', array(), 'Admin.Actions');
echo '
';
}
protected function _displayDeleteLink($token = null, $id)
{
global $currentIndex;
$_cacheLang['Delete'] = $this->trans('Delete', array(), 'Admin.Actions');
$_cacheLang['DeleteItem'] = $this->trans('Delete item #', array(), 'Modules.Autoupgrade.Admin');
echo '
';
}
/**
* Close list table and submit button
*/
public function displayListFooter($token = null)
{
echo '';
if ($this->delete) {
echo '';
}
echo '
';
if (isset($this->_includeTab) and sizeof($this->_includeTab)) {
echo '
';
}
}
/**
* Options lists
*/
public function displayOptionsList()
{
global $currentIndex, $cookie, $tab;
if (!isset($this->_fieldsOptions) or !sizeof($this->_fieldsOptions)) {
return false;
}
$defaultLanguage = (int)Configuration::get('PS_LANG_DEFAULT');
$this->_languages = Language::getLanguages(false);
$tab = Tab::getTab((int)$cookie->id_lang, Tab::getIdFromClassName($tab));
echo '
';
echo(isset($this->optionTitle) ? '
'.$this->optionTitle.'
' : '');
echo '
';
}
/**
* Load class object using identifier in $_GET (if possible)
* otherwise return an empty object, or die
*
* @param boolean $opt Return an empty object if load fail
* @return object
*/
protected function loadObject($opt = false)
{
if ($id = (int)(Tools14::getValue($this->identifier)) and Validate::isUnsignedId($id)) {
if (!$this->_object) {
$this->_object = new $this->className($id);
}
if (Validate::isLoadedObject($this->_object)) {
return $this->_object;
}
$this->_errors[] = $this->trans('Object cannot be loaded (not found)', array(), 'Modules.Autoupgrade.Admin');
} elseif ($opt) {
$this->_object = new $this->className();
return $this->_object;
} else {
$this->_errors[] = $this->trans('Object cannot be loaded (identifier missing or invalid)', array(), 'Modules.Autoupgrade.Admin');
}
$this->displayErrors();
}
/**
* Return field value if possible (both classical and multilingual fields)
*
* Case 1 : Return value if present in $_POST / $_GET
* Case 2 : Return object value
*
* @param object $obj Object
* @param string $key Field name
* @param integer $id_lang Language id (optional)
* @return string
*/
protected function getFieldValue($obj, $key, $id_lang = null)
{
if ($id_lang) {
$defaultValue = ($obj->id and isset($obj->{$key}[$id_lang])) ? $obj->{$key}[$id_lang] : '';
} else {
$defaultValue = isset($obj->{$key}) ? $obj->{$key} : '';
}
return Tools14::getValue($key.($id_lang ? '_'.$id_lang : ''), $defaultValue);
}
/**
* Display form
*
* @global string $currentIndex Current URL in order to keep current Tab
*/
public function displayForm($firstCall = true)
{
global $cookie;
$allowEmployeeFormLang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
if ($allowEmployeeFormLang && !$cookie->employee_form_lang) {
$cookie->employee_form_lang = (int)(Configuration::get('PS_LANG_DEFAULT'));
}
$useLangFromCookie = false;
$this->_languages = Language::getLanguages(false);
if ($allowEmployeeFormLang) {
foreach ($this->_languages as $lang) {
if ($cookie->employee_form_lang == $lang['id_lang']) {
$useLangFromCookie = true;
}
}
}
if (!$useLangFromCookie) {
$this->_defaultFormLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
} else {
$this->_defaultFormLanguage = (int)($cookie->employee_form_lang);
}
// Only if it is the first call to displayForm, otherwise it has already been defined
if ($firstCall) {
echo '
';
}
}
/**
* Display object details
*
* @global string $currentIndex Current URL in order to keep current Tab
*/
public function viewDetails()
{
global $currentIndex;
}
/**
* Called before deletion
*
* @param object $object Object
* @return boolean
*/
protected function beforeDelete($object)
{
return true;
}
/**
* Called before deletion
*
* @param object $object Object
* @return boolean
*/
protected function afterDelete($object, $oldId)
{
return true;
}
protected function afterAdd($object)
{
return true;
}
protected function afterUpdate($object)
{
return true;
}
/**
* Check rights to view the current tab
*
* @return boolean
*/
protected function afterImageUpload()
{
return true;
}
/**
* Check rights to view the current tab
*
* @return boolean
*/
public function viewAccess($disable = false)
{
global $cookie;
if ($disable) {
return true;
}
$this->tabAccess = Profile::getProfileAccess($cookie->profile, $this->id);
if ($this->tabAccess['view'] === '1') {
return true;
}
return false;
}
/**
* Check for security token
*/
public function checkToken()
{
$token = Tools14::getValue('token');
return (!empty($token) and $token === $this->token);
}
/**
* Display flags in forms for translations
*
* @param array $languages All languages available
* @param integer $defaultLanguage Default language id
* @param string $ids Multilingual div ids in form
* @param string $id Current div id]
* #param boolean $return define the return way : false for a display, true for a return
*/
public function displayFlags($languages, $defaultLanguage, $ids, $id, $return = false)
{
if (sizeof($languages) == 1) {
return false;
}
$output = '