Files
wyczarujprezent.pl/modules/ets_reviews/controllers/admin/AdminEtsRVStaffsController.php
2024-10-28 22:14:22 +01:00

681 lines
27 KiB
PHP

<?php
/**
* Copyright ETS Software Technology Co., Ltd
*
* NOTICE OF LICENSE
*
* This file is not open source! Each license that you purchased is only available for 1 website only.
* If you want to use this file on more websites (or projects), you need to purchase additional licenses.
* You are not allowed to redistribute, resell, lease, license, sub-license or offer our resources to any third party.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future.
*
* @author ETS Software Technology Co., Ltd
* @copyright ETS Software Technology Co., Ltd
* @license Valid for 1 website (or project) for each purchase of license
*/
if (!defined('_PS_VERSION_'))
exit;
require_once dirname(__FILE__) . '/AdminEtsRVBaseController.php';
class AdminEtsRVStaffsController extends AdminEtsRVBaseController
{
public $is_grand = 0;
public $tab;
/**
* @var EtsRVStaff | EtsRVProductCommentCustomer
*/
public $object;
public function __construct()
{
$this->table = 'ets_rv_staff';
$this->className = 'EtsRVStaff';
$this->allow_export = false;
$this->_redirect = false;
$this->list_no_link = true;
$this->lang = false;
parent::__construct();
$this->_conf[3] = $this->l('Saved', 'AdminEtsRVStaffsController');
$this->show_form_cancel_button = false;
$this->is_grand = $this->context->employee->id_profile == _PS_ADMIN_PROFILE_;
$this->tab = !$this->is_grand ? 'employee' : trim(Tools::getValue('tab', 'employee'));
if ($this->tab == 'customer') {
$this->table = 'ets_rv_product_comment_customer';
$this->className = 'EtsRVProductCommentCustomer';
$this->identifier = 'id_customer';
$this->_orderBy = 'id_customer';
} else {
$this->identifier = 'id_employee';
$this->_orderBy = 'id_employee';
}
$this->addRowAction('edit');
if ($this->tab == 'customer')
$this->addRowAction('delete');
if ($this->tab == 'customer') {
$this->_select .= '
c.id_customer
, c.email
, CONCAT(c.firstname," ", c.lastname) `customer`
';
$this->_join .= 'LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.id_customer = a.id_customer)';
$this->_where .= ' AND is_staff = 1';
} else {
$this->_select .= '
e.id_employee
, e.email
, CONCAT(e.firstname," ", e.lastname) `employee`
, IF(e.id_profile = ' . _PS_ADMIN_PROFILE_ . ', 1, IFNULL(a.enabled, 0)) `enabled`
, pl.name `profile`
';
$this->_join .= '
RIGHT JOIN `' . _DB_PREFIX_ . 'employee` e ON (e.id_employee = a.id_employee)
LEFT JOIN `' . _DB_PREFIX_ . 'profile_lang` pl ON (pl.id_profile = e.id_profile AND pl.id_lang=' . (int)$this->context->language->id . ')
';
}
$this->fields_list = $this->getFieldsList();
}
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme); // TODO: Change the autogenerated stub
$this->addJS(array(
_PS_JS_DIR_ . 'jquery/plugins/autocomplete/jquery.autocomplete.js',
));
}
public function initToolbarTitle()
{
$this->toolbar_title = trim($this->tab) == 'customer' ? $this->l('Customers', 'AdminEtsRVStaffsController') : $this->l('Employees', 'AdminEtsRVStaffsController');
}
public function getConfigs()
{
$id = Tools::getValue($this->identifier);
$field = [
$this->identifier => array(
'type' => 'hidden',
'label' => $this->l('ID', 'AdminEtsRVStaffsController'),
'name' => $this->identifier,
'required' => true,
'default_value' => $id,
)
];
if ($this->tab == 'customer' && $id <= 0) {
$field[$this->identifier]['type'] = 'text';
$field[$this->identifier]['label'] = $this->l('Customer', 'AdminEtsRVStaffsController');
$field[$this->identifier]['placeholder'] = $this->l('Search for customer by first name, last name, email or id', 'AdminEtsRVStaffsController');
}
$configs = array_merge(
$field,
$id > 0 ? [
'firstname' => array(
'type' => 'text',
'label' => $this->l('First name', 'AdminEtsRVStaffsController'),
'name' => 'firstname',
'disabled' => true,
'col' => 4,
),
'lastname' => array(
'type' => 'text',
'label' => $this->l('Last name', 'AdminEtsRVStaffsController'),
'name' => 'lastname',
'disabled' => true,
'col' => 4,
),
'email' => array(
'type' => 'text',
'label' => $this->l('Email', 'AdminEtsRVStaffsController'),
'name' => 'email',
'disabled' => true,
'col' => 4,
),
] : [],
[
'display_name' => array(
'type' => 'text',
'label' => $this->l('Display name', 'AdminEtsRVStaffsController'),
'name' => 'display_name',
'validate' => 'isCleanHtml',
'col' => 4,
),
'avatar' => array(
'type' => 'file',
'label' => $this->l('Avatar', 'AdminEtsRVStaffsController'),
'name' => 'avatar',
'form_group_class' => 'form-group-file',
'display_image' => true,
'hint' => $this->l('Upload an avatar from your computer.', 'AdminEtsRVStaffsController'),
'desc' => sprintf($this->l('Accepted formats: jpg, jpeg, png, gif. Limit: %s, recommended size: 150 x 150 px', 'AdminEtsRVStaffsController'), EtsRVTools::formatBytes(EtsRVTools::getPostMaxSizeBytes())),
),
'sendmail' => array(
'type' => 'switch',
'label' => $this->l('Send email', 'AdminEtsRVStaffsController'),
'name' => 'sendmail',
'values' => [
[
'id' => 'enabled_on',
'value' => 1,
'label' => $this->l('Yes', 'AdminEtsRVStaffsController')
],
[
'id' => 'enabled_off',
'value' => 0,
'label' => $this->l('No', 'AdminEtsRVStaffsController')
]
],
'default_value' => 1
)
]
);
if ($this->tab == 'employee' && $this->is_grand && !EtsRVStaff::isSupperAdmin($id)) {
$configs['enabled'] = array(
'type' => 'switch',
'label' => $this->l('Enabled', 'AdminEtsRVStaffsController'),
'name' => 'enabled',
'values' => [
[
'id' => 'enabled_on',
'value' => 1,
'label' => $this->l('Yes', 'AdminEtsRVStaffsController')
],
[
'id' => 'enabled_off',
'value' => 0,
'label' => $this->l('No', 'AdminEtsRVStaffsController')
]
],
'default_value' => 1
);
}
return $configs;
}
public function displayEditLink($token, $id)
{
if ($this->tab == 'employee') {
$employee = new Employee($id);
if ($employee->id != $this->context->employee->id && $this->context->employee->id_profile != _PS_ADMIN_PROFILE_)
return '';
}
if (!isset(self::$cache_lang['edit']) || trim(self::$cache_lang['edit']) == '')
self::$cache_lang['edit'] = $this->l('Edit', 'AdminEtsRVActivityController');
$this->context->smarty->assign(array(
'href' => self::$currentIndex . '&tab=' . $this->tab . '&update' . $this->table . '&' . $this->identifier . '=' . $id . '&token=' . $token,
'action' => self::$cache_lang['edit'],
));
return $this->context->smarty->fetch($this->module->getLocalPath() . 'views/templates/admin/etsrv_staffs/helpers/list/list_action_edit.tpl');
}
public function displayEnabled($enabled, $tr)
{
$tpl_vars = [
'href' => self::$currentIndex . '&tab=' . $this->tab . '&status' . $this->table . '&' . $this->identifier . '=' . $tr[$this->identifier] . '&token=' . $this->token,
'enabled' => $enabled
];
if ($this->tab === 'employee' && !empty($tr[$this->identifier])) {
$employee = new Employee($tr[$this->identifier]);
$tpl_vars['supper_admin'] = $employee->id_profile == _PS_ADMIN_PROFILE_;
if ($tpl_vars['supper_admin'])
$tpl_vars['href'] = '#';
}
$this->context->smarty->assign($tpl_vars);
return $this->context->smarty->fetch($this->module->getLocalPath() . 'views/templates/admin/etsrv_staffs/helpers/list/list_action_enable.tpl');
}
public function displaySendmail($sendmail, $tr)
{
$tpl_vars = [
'href' => self::$currentIndex . '&tab=' . $this->tab . '&sendmail' . $this->table . '&action=statusSendmail&' . $this->identifier . '=' . $tr[$this->identifier] . '&token=' . $this->token,
'sendmail' => $sendmail
];
$this->context->smarty->assign($tpl_vars);
return $this->context->smarty->fetch($this->module->getLocalPath() . 'views/templates/admin/etsrv_staffs/helpers/list/list_action_sendmail.tpl');
}
public function ajaxProcessSearchCustomer()
{
$query = ($q = Tools::getValue('q', false)) && Validate::isCleanHtml($q) ? $q : false;
if (!$query or $query == '' or Tools::strlen($query) < 1) {
die();
}
$searches = explode(' ', $query);
$searches = array_unique($searches);
foreach ($searches as $search) {
if (!empty($search) && $results = EtsRVProductCommentCustomer::searchByName($search, 50)) {
foreach ($results as $result) {
$customer = [];
if ($result['active']) {
$customer = [
$result['id_customer'],
$result['firstname'],
$result['lastname'],
$result['email'],
];
}
echo implode('|', $customer) . "\r\n";
}
}
}
die;
}
public function loadObject($opt = false)
{
if (!isset($this->className) || !$this->className) {
return true;
}
$id = (int)Tools::getValue($this->identifier);
if ($id && Validate::isUnsignedId($id)) {
if (!$this->object) {
$this->object = new $this->className($id);
}
return $this->object;
} elseif ($opt) {
if (!$this->object) {
$this->object = new $this->className();
}
return $this->object;
} else {
return false;
}
}
public function ajaxProcessDeleteImage()
{
$this->loadObject(true);
if ($this->object->id > 0) {
$avatar_file = _PS_IMG_DIR_ . $this->module->name . '/a/' . $this->object->avatar;
$this->object->avatar = '';
if (!$this->object->save())
$this->errors[] = $this->l('An error occurred while updating the status.', 'AdminEtsRVStaffsController');
elseif (file_exists($avatar_file))
@unlink($avatar_file);
} else
$this->errors[] = $this->l('An error occurred while updating the status for an object. Cannot load object', 'AdminEtsRVStaffsController');
$has_error = count($this->errors) > 0;
$this->jsonRender([
'errors' => $has_error ? implode(PHP_EOL, $this->errors) : false,
'msg' => $this->l('Delete avatar successfully.', 'AdminEtsRVStaffsController'),
]);
}
public function renderForm()
{
$this->loadObject(true);
if ($this->object instanceof EtsRVProductCommentCustomer) {
$customer = new Customer($this->object->id_customer);
$this->object->firstname = $customer->firstname;
$this->object->lastname = $customer->lastname;
$this->object->email = $customer->email;
}
$this->fields_form = [
'legend' => array(
'title' => ((int)$this->object->id > 0 || $this->tab == 'employee' ? $this->l('Edit', 'AdminEtsRVStaffsController') : $this->l('Add', 'AdminEtsRVStaffsController')) . ' ' . ($this->tab == 'customer' ? $this->l('customer', 'AdminEtsRVStaffsController') : $this->l('employee', 'AdminEtsRVStaffsController')),
'icon' => 'icon'
),
'input' => $this->getConfigs(),
];
if (trim($this->object->avatar) !== '') {
$image = _PS_IMG_DIR_ . $this->module->name . '/a/' . $this->object->avatar;
$image_url = ImageManager::thumbnail(
$image,
$this->table . '_' . $this->object->avatar,
250,
$this->imageType,
true,
true
);
$this->fields_form['input']['avatar']['image'] = $image_url;
$this->fields_form['input']['avatar']['delete_url'] = self::$currentIndex . '&field=avatar' . ($this->tab ? '&tab=' . $this->tab : '') . ($this->object->id ? '&' . $this->identifier . '=' . $this->object->id : '') . '&token=' . $this->token;
}
$this->fields_form['submit'] = array(
'title' => $this->l('Save', 'AdminEtsRVStaffsController'),
);
$this->fields_form['buttons']['cancel'] = array(
'title' => $this->l('Back to list', 'AdminEtsRVStaffsController'),
'href' => self::$currentIndex . ($this->tab != '' ? '&tab=' . $this->tab : '') . '&token=' . $this->token,
'class' => 'ets-rv-back-to-list'
);
$this->fields_form['buttons']['save-and-stay'] = [
'title' => $this->l('Save and stay', 'AdminEtsRVStaffsController'),
'name' => 'submitAdd' . $this->table . 'AndStay',
'type' => 'submit',
'class' => 'btn btn-default pull-right',
'icon' => 'process-icon-save',
];
return parent::renderForm();
}
public function setHelperDisplay(Helper $helper)
{
parent::setHelperDisplay($helper);
$helper->currentIndex = self::$currentIndex . '&tab=' . $this->tab;
}
public function processStatusSendmail()
{
if ($this->tab == 'employee') {
$id_employee = (int)Tools::getValue($this->identifier);
$this->loadObject(true);
if (!EtsRVStaff::itemExist($id_employee)) {
$this->object->id = null;
$this->object->sendmail = 1;
} else
$this->object->sendmail = !$this->object->sendmail;
if ($this->object->sendmail)
$this->object->id_last_activity = EtsRVActivity::getLastID();
if (!$this->object->save())
$this->errors[] = $this->l('An error occurred while updating the status.', 'AdminEtsRVStaffsController');
else {
$this->object->id = $id_employee;
if ($this->object->sendmail) {
EtsRVStaff::lastViewer($this->object->id, EtsRVActivity::getLastID());
}
}
return $this->object;
}
}
public function processStatus()
{
if ($this->tab == 'employee') {
$id_employee = (int)Tools::getValue($this->identifier);
$this->loadObject(true);
if (!EtsRVStaff::itemExist($id_employee)) {
$this->object->id = null;
$this->object->enabled = 1;
} else
$this->object->enabled = !$this->object->enabled;
if ($this->object->enabled)
$this->object->id_last_activity = EtsRVActivity::getLastID();
if (!$this->object->save())
$this->errors[] = $this->l('An error occurred while updating the status.', 'AdminEtsRVStaffsController');
else {
$this->object->id = $id_employee;
if ($this->object->enabled) {
EtsRVStaff::lastViewer($this->object->id, EtsRVActivity::getLastID());
}
}
return $this->object;
}
}
public function processDelete()
{
if ($this->tab == 'customer') {
$this->loadObject(true);
if ($this->object->id > 0) {
$this->object->is_staff = 0;
if (!$this->object->save())
$this->errors[] = $this->l('An error occurred during deletion.', 'AdminEtsRVStaffsController');
return $this->object;
} else
$this->errors[] = $this->l('An error occurred while deleting the object. Cannot load object', 'AdminEtsRVStaffsController');
}
}
public function ajaxProcessCustomerInfo()
{
$this->loadObject(true);
$json = [
'customer' => $this->object,
];
if (trim($this->object->avatar) !== '') {
$image = _PS_IMG_DIR_ . $this->module->name . '/a/' . $this->object->avatar;
$image_url = ImageManager::thumbnail(
$image,
$this->table . '_' . $this->object->avatar,
250,
$this->imageType,
true,
true
);
$image_size = file_exists($image) ? filesize($image) / 1000 : false;
$json['avatar'] = $image_url;
$json['size'] = $image_size;
$json['delete_url'] = self::$currentIndex . '&field=avatar' . ($this->tab ? '&tab=' . $this->tab : '') . ($this->object->id ? '&' . $this->identifier . '=' . $this->object->id : '') . '&token=' . $this->token;
}
$this->jsonRender($json);
}
public function processUpdate()
{
return $this->processAdd();
}
static $default_avatar_folder = '/a/';
public function processAdd()
{
$this->loadObject(true);
$display_name = trim(Tools::getValue('display_name'));
if ($display_name && !Validate::isCleanHtml($display_name))
$this->errors[] = $this->l('Display name is invalid');
$id_customer = (int)Tools::getValue('id_customer');
$id_employee = (int)Tools::getValue('id_employee');
if ($id_employee !== $this->context->employee->id && $this->context->employee->id_profile != _PS_ADMIN_PROFILE_)
$this->errors[] = $this->l('Permission denied. The account is only modified by Super admin', 'AdminEtsRVStaffsController');
if ($this->tab == 'customer') {
if ($id_customer < 1)
$this->errors[] = $this->l('Customer is required', 'AdminEtsRVStaffsController');
} elseif ($id_employee < 1)
$this->errors[] = $this->l('Employee is required', 'AdminEtsRVStaffsController');
if (!count($this->errors)) {
$files = EtsRVTools::getInstance()->processUploadImage('avatar', 'a', $this->errors, false, null, 150, 150, $error_post_maxsize);
if ($error_post_maxsize) {
$this->display = 'list';
return false;
}
}
if (count($this->errors) < 1) {
$this->object->display_name = $display_name;
if ($this->tab == 'customer') {
$this->object->id_customer = $id_customer;
$this->object->is_staff = $this->is_grand ? 1 : 0;
} else {
$this->object->id_employee = $id_employee;
if ($this->is_grand)
$this->object->enabled = (int)Tools::getValue('enabled') > 0 || EtsRVStaff::isSupperAdmin($id_employee) ? 1 : 0;
}
//Upload avatar:
list($image, $file_dest, $file_name) = !empty($files) ? $files : ['', '', ''];
$oldImage = $this->object->id && trim($this->object->avatar) !== '' ? $file_dest . $this->object->avatar : '';
if (trim($image) !== '' && file_exists($file_dest . $image))
$this->object->avatar = $image;
if ($this->tab == 'employee' && !EtsRVStaff::itemExist($id_employee))
$this->object->id = 0;
if ($this->tab == 'customer' && !EtsRVProductCommentCustomer::itemExist($id_customer))
$this->object->id = 0;
if (!$this->object->save()) {
$this->errors[] = $this->l('Saving failed', 'AdminEtsRVStaffsController');
} elseif (trim($image) !== '' && @file_exists($oldImage)) {
@unlink($oldImage);
}
if (count($this->errors) < 1) {
// Save and stay on same form
if (empty($this->redirect_after) && $this->redirect_after !== false && Tools::isSubmit('submitAdd' . $this->table . 'AndStay')) {
$this->redirect_after = self::$currentIndex . ($this->tab ? '&tab=' . $this->tab : '') . '&' . $this->identifier . '=' . ($this->tab == 'customer' ? $id_customer : $id_employee) . '&conf=3&update' . $this->table . '&token=' . $this->token;
}
// Default behavior (save and back)
if (empty($this->redirect_after) && $this->redirect_after !== false) {
$this->redirect_after = self::$currentIndex . ($this->tab ? '&tab=' . $this->tab : '') . '&conf=3&token=' . $this->token;
}
}
}
$this->errors = array_unique($this->errors);
if (!empty($this->errors)) {
$this->display = 'edit';
return false;
}
return $this->object;
}
public function getFieldsList()
{
$fields = [];
if (trim($this->tab) == 'customer') {
$fields['id_customer'] = array(
'title' => $this->l('ID', 'AdminEtsRVStaffsController'),
'type' => 'int',
'filter_key' => 'c!id_customer',
'class' => 'fixed-width-xs text-center',
'callback' => 'displayCustomer'
);
} else {
$fields['id_employee'] = [
'title' => $this->l('ID', 'AdminEtsRVStaffsController'),
'type' => 'int',
'filter_key' => 'e!id_employee',
'class' => 'fixed-width-xs text-center',
'callback' => 'displayEmployee'
];
}
$fields['avatar'] = [
'title' => $this->l('Avatar', 'AdminEtsRVStaffsController'),
'type' => 'text',
'filter_key' => 'a!avatar',
'search' => false,
'orderby' => false,
'callback' => 'displayAvatar'
];
if (trim($this->tab) == 'customer') {
$fields['customer'] = array(
'title' => $this->l('Customer name', 'AdminEtsRVStaffsController'),
'type' => 'text',
'filter_key' => 'customer',
'havingFilter' => true,
);
} else {
$fields['employee'] = array(
'title' => $this->l('Employee name', 'AdminEtsRVStaffsController'),
'type' => 'text',
'filter_key' => 'employee',
'havingFilter' => true,
);
$profile_array = [];
$profiles = Profile::getProfiles($this->context->language->id);
if ($profiles)
foreach ($profiles as $profile)
$profile_array[$profile['id_profile']] = $profile['name'];
$fields['profile'] = [
'title' => $this->l('Profile', 'AdminEtsRVStaffsController'),
'type' => 'select',
'list' => $profile_array,
'filter_key' => 'e!id_profile',
'class' => 'fixed-width-lg',
];
}
$fields = array_merge(
$fields,
[
'display_name' => array(
'title' => $this->l('Display name', 'AdminEtsRVStaffsController'),
'type' => 'text',
'filter_key' => 'a!display_name',
'callback' => 'displayDisplayName',
),
'email' => array(
'title' => $this->l('Email', 'AdminEtsRVStaffsController'),
'type' => 'text',
'havingFilter' => true,
)
]
);
if ($this->tab == 'employee' && $this->is_grand) {
$fields['sendmail'] = array(
'title' => $this->l('Send email', 'AdminEtsRVStaffsController'),
'type' => 'bool',
'class' => 'fixed-width-xs text-center',
'havingFilter' => true,
'callback' => 'displaySendmail'
);
$fields['enabled'] = array(
'title' => $this->l('Is staff', 'AdminEtsRVStaffsController'),
'type' => 'bool',
'class' => 'fixed-width-xs text-center',
'havingFilter' => true,
'callback' => 'displayEnabled'
);
}
return $fields;
}
public function displayDisplayName($display_name)
{
if (trim($display_name) == '')
return null;
return $display_name;
}
public function displayAvatar($avatar)
{
if (trim($avatar) == '' || !@file_exists(_PS_IMG_DIR_ . $this->module->name . '/a/' . $avatar))
return null;
$this->context->smarty->assign([
'avatar' => _PS_IMG_ . $this->module->name . '/a/' . $avatar,
'width' => 80,
]);
return $this->context->smarty->fetch($this->module->getLocalPath() . 'views/templates/admin/image.tpl');
}
public function initToolbar()
{
parent::initToolbar();
if ($this->tab == 'employee')
unset($this->toolbar_btn['new']);
elseif (isset($this->toolbar_btn['new']))
$this->toolbar_btn['new']['href'] .= '&tab=' . $this->tab;
}
public function displayEmployee($id_employee)
{
return $id_employee ?: '--';
}
public function displayCustomer($id_customer)
{
$attrs = [
'class' => 'ets_rv_customer_id_' . $id_customer
];
return EtsRVTools::displayText(($id_customer ?: '--'), 'span', $attrs);
}
}