ElixirVault/www/system-brs-core/cms/cmsCountToClass/inc/cmsCountToClassController.php

435 lines
14 KiB
PHP

<?php
namespace BRSystems\Core\Cms\cmsCountToClass\inc;
use BRSystems\Core\Cms\cmsOrders\inc\cmsOrdersController;
use BRSystems\Core\Library\cCore\cCore;
use BRSystems\Core\Library\cJSON\cJSON;
use BRSystems\Core\Library\cValidator\cValidator;
class cmsCountToClassController extends cCore
{
public $dir;
public $name;
public $module;
/**
* Class constructor
*/
public function __construct($uriData, $dir, $name)
{
$this->dir = $dir;
$this->name = $name;
parent:: __construct($uriData);
$this->module = new cmsCountToClassModule();
$this->oView->setTemplateDir($this->dir . "/tmpl/");
if (!$this->oView->cacheUsed()) {
$this->oView->setJs(getCurrentFolder($this->dir) . '/js/' . $this->name . '.js');
$this->oView->setCss(getCurrentFolder($this->dir) . '/css/' . $this->name . '.css');
}
// Set JS module init title
$this->oView->setAsJSON(cCore::JS_MOD, ucfirst($name));
}
public function run($initEditMode = false)
{
$this->oView->TABLE = true;
$this->oView->MODULE_TITLE = $this->gM("Count to classificators");
$this->oView->setAsJSON('initEdit', $initEditMode);
$this->oView->setAsJSON('baseUrl', $this->gL());
$this->oView->setAsJSON('gL', $this->getJsTranslationsForView());
$this->oView->setAsJSON('vcfg', $this->getJsValCfgForView());
$this->oView->display();
}
/**
* Function to create cms table & return for direct & ajax input
*
* @param int $page - page to display
* @param string $filter_json - (optional) filter json string
* @param bool $headers - flag whether to send header data or not
* @return array of data
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
* @category cmsCountToClass Controller
* @version 1.1
*/
public function createCmsTable($page = 1, $filter_json = '', $headers = false)
{
$pages = 0;
$filter = ($filter_json ? cJSON::decode($filter_json, true) : []);
$this->oView->MODULE_TITLE = $this->gM("Product Count to Classificators / Orders / Storage");
if (!!$filter && is_array($filter)) {
$this->oView->DEFAULT_SEARCH_VALUES = prepForDisplayHtml($filter);
}
$vStatus = [
'' => [
'title' => $this->gM('Select'),
],
cmsOrdersController::PRODUCT_OPERATION_ORDER_SEND => [
'title' => $this->gM('Order send'),
],
cmsOrdersController::PRODUCT_OPERATION_BUYS_MODULE => [
'title' => $this->gM('Buys module'),
],
cmsOrdersController::PRODUCT_OPERATION_ORDER_DELETED => [
'title' => $this->gM('Order deleted'),
],
cmsOrdersController::PRODUCT_OPERATION_STORAGE_MODULE => [
'title' => $this->gM('Storage module'),
],
];
$this->oView->LIST_HEADER = reOrderTableFields(
$this->getTableFields($vStatus),
$this->oUser->oCfg->getTableProperty($this->name),
);
$this->oView->LIST_DATA = $this->getModuleData($page, $filter, $pages, $vStatus);
$this->oView->LIST_ACTIONS = $this->getTableActions();
$this->oView->LIST_BTN = true;
$data = [
'data' => $this->oView->genHelperView('cmsc/table/data-table'),
'pcount' => $pages,
'rlimit' => $this->cmsTableRecordsPerPage,
];
// Send data to client
if ($headers) {
$data['header'] = $this->oView->genHelperView('cmsc/table/data-table-search');
}
return $data;
}
/**
* Function to get module data
*
* @param int $page
* @param array $filter
* @param int $pages
* @param array $vStatus
* @return array of data
* @category cmsCountToClass Controller
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
* @version 1.1
*/
public function getModuleData($page = 1, $filter = [], &$pages, $vStatus)
{
$tFields = $this->getTableFields($vStatus);
// Create sort by
$sortBy = getP('sortBy');
$sort = $sortBy ? $this->module->db->genOrderByQuery($sortBy, getP('sortWay'), $tFields) : "id DESC";
// Create where clause
$where = 'AND cc.id > 0 ';
if (count($filter)) {
$where .= $this->module->db->genSearchFieldQuery($filter, $tFields, true, true);
}
$offset = (($page ?: 1) - 1) * $this->cmsTableRecordsPerPage;
$data = $this->module->getModuleRecords($where, $sort, $offset, $this->cmsTableRecordsPerPage);
$rows = $this->module->db->getSelectedRecordsCount();
$pages = floor($rows / $this->cmsTableRecordsPerPage,) + ($rows % $this->cmsTableRecordsPerPage ? 1 : 0);
$this->oView->ROWS = $rows;
if (!empty($data) && is_array($data)) {
foreach ($data as $k => $v) {
$today = date('d-m-Y', $v['date']) == date('d-m-Y');
$format = $today ? $this->timeDateFormat : $this->CMSDateFormat;
$data[$k]['op_date'] = date($format, $v['op_date']);
$data[$k]['date'] = date($format, $v['date']);
if (isset($vStatus[$v['op_type']])) {
$data[$k]['op_type'] = $vStatus[$v['op_type']]['title'];
}
}
}
return $data;
}
/**
* Function to delete records marked in table
*
* @param string $rows - passed user ids in cJSON string
* @return mixed of data
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @category cmsCountToClass Controller
*/
public function delete($rows)
{
if ($rows = cJSON::decode($rows, true)) {
if ($this->module->deleteRecords($this->module->db->prepData($rows))) {
return true;
} else {
cJSON::senderror();
}
}
}
/**
* Function to save Record data
*
* @param int $recordId - record id
* @param string $fields - param cJSON string
* @return array of data
* @category cmsCountToClass Controller
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
*/
public function saveRecordData($recordId, $fields)
{
$fields = $this->getSubmittedDataAsArray($fields ?? '');
if (empty($recordId) || empty($fields)) {
cJSON::senderror(['data' => $this->gM("No data to save")]);
}
// Validate fields
$valid = new cValidator($this);
$errors = $valid->checkArray($fields, $this->getJsValCfgForView());
if ($errors !== true) {
cJSON::senderror(['data' => $errors]);
}
$fields['modified'] = time();
$fields['modified_by'] = $this->oUser->getUser();
$this->module->saveData($recordId, $fields);
}
/**
* Function to create table headers + search
*
* @param array $statusData - status array
* @return bool|array of data
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
* @category cmsCountToClass Controller
* @version 1.1
*/
public function getTableFields($statusData)
{
if (!$this->oUser->chkPermission('CMS_APP_COUNT_TO_CLASS_SHOW_MANAGEMENT', false, false)) {
return false;
}
$tFields = [
"checkbox" => [
'sort' => false,
'title' => '',
'class' => 'w4 center rchk',
],
"id" => [
'sort' => true,
'search' => true,
'title' => $this->gM("CC id"),
'class' => 'w6 center sortdesc',
'db_field' => 'cc.id',
'type' => 'input',
],
"nr" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Prod nr"),
'class' => 'w12 sortdesc',
'db_field' => 'mp.prod_nr',
'type' => 'input',
],
"prod_name" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Product title"),
'class' => 'w30',
'db_field' => 'ii.prod_name',
'type' => 'input',
],
"cl1" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Product cl1"),
'class' => 'w12',
'db_field' => ['tr.t_title', 'tr2.t_title'],
'db_link' => 'OR',
'type' => 'input',
],
"cl2" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Product cl2"),
'class' => 'w12',
'db_field' => ['tr.t_title', 'tr2.t_title'],
'db_link' => 'OR',
'type' => 'input',
],
"count" => [
'sort' => true,
'search' => false,
'title' => $this->gM("Count"),
'class' => 'w8 right',
'db_field' => 'count',
'type' => 'date',
'edit' => true,
],
'p' => [
'sort' => true,
'search' => false,
'title' => $this->gM("Price"),
'class' => 'w8 right fright',
'db_field' => 'mcc.prod_price',
'type' => 'date',
],
'ip' => [
'sort' => true,
'search' => false,
'title' => $this->gM("Inc. Price"),
'class' => 'w8 right fright',
'db_field' => 'mcc.prod_incoming_price',
'type' => 'input',
],
'pv' => [
'sort' => true,
'search' => false,
'title' => $this->gM("Sold Price"),
'class' => 'w8 right fright',
'db_field' => 'cc.price_sold',
'type' => 'input',
],
'note' => [
'sort' => true,
'search' => true,
'title' => $this->gM("Note"),
'class' => 'w12',
'db_field' => 'cc.note',
'type' => 'input',
],
"op_date" => [
'sort' => true,
'search' => false,
'title' => $this->gM("Op. Date"),
'class' => 'w12 center',
'db_field' => 'cc.op_date',
'type' => 'date',
],
"date" => [
'sort' => true,
'search' => [
'input_class' => 'date',
'custom_title' => $this->gM("Date from"),
'db_field' => 'cc.date',
'db_between_2field' => 'op_user',
],
'title' => $this->gM("Date"),
'class' => 'w12 center',
'db_field' => 'cc.date',
'type' => 'date',
],
"op_user" => [
'sort' => true,
'search' => [
'input_class' => 'date',
'custom_title' => $this->gM("Date till"),
],
'title' => $this->gM("User"),
'class' => 'w12',
'db_field' => 'op_user',
'type' => 'input',
],
"op_type" => [
'sort' => false,
'search' => false,
'title' => $this->gM("Status"),
'class' => 'w12',
'db_field' => 'cc.op_type',
'type' => 'dropBox',
'data' => $statusData,
'default' => '',
],
"buttons" => [
'sort' => false,
'class' => 'w20',
'title' => $this->gM("Actions"),
'btnset' => [
'ldelete' => [
'title' => $this->gM("Delete"),
],
],
],
];
if (!$this->chkP('SYS_SHOW_TABLE_IDS')) {
unset($tFields['id']);
}
// Chk permission for edit
if (!$this->chkP('CMS_APP_COUNT_TO_CLASS_EDIT')) {
$tFields['count']['edit'] = false;
}
// Chk permission for delete
if (!$this->chkP('CMS_APP_COUNT_TO_CLASS_DELETE')) {
unset($tFields['buttons']['btnset']['ldelete']);
}
return $tFields;
}
public function getTableActions(): array
{
$data = [
'idelete' => [
'lbl' => $this->gM("Delete selected"),
'sepline' => false,
'icon' => 'nico-delete',
],
];
if (!$this->chkP('CMS_APP_COUNT_TO_CLASS_DELETE')) {
unset($data['idelete']);
}
// Reform drop down
reformatDropDown($data);
return $data;
}
public function getJsTranslationsForView(): array
{
return [
'yes' => $this->gM('Yes'),
'no' => $this->gM('No'),
'delTitle' => $this->gM('Delete record?'),
'delMessage' => $this->gM(
'Selected records will be permanently deleted and cannot be recovered. Confirm delete?',
),
'buttonYes' => $this->gM('Delete selected record'),
'buttonCancel' => $this->gM('Cancel operation'),
];
}
public function getJsValCfgForView($bPrepDataCfg = true): array
{
$valCfg = [
// Additional validation config
'cfg' => [
cValidator::CFG_BIND_FIELD => 'data-validator',
],
'count' => [
cValidator::P_TYPE => cValidator::TY_FLOAT,
cValidator::P_COMP => 1,
cValidator::P_TEST => [cValidator::CHK_NUM],
],
];
return ($bPrepDataCfg ? cValidator::prepValCfgForValidation($valCfg) : $valCfg);
}
}