ElixirVault/www/system-brs-core/cms/cmsDelivery/inc/cmsDeliveryController.php

411 lines
13 KiB
PHP

<?php
namespace BRSystems\Core\Cms\cmsDelivery\inc;
use BRSystems\Core\Library\cCore\cCore;
use BRSystems\Core\Library\cJSON\cJSON;
use BRSystems\Core\Library\cValidator\cValidator;
class cmsDeliveryController extends cCore
{
public $dir;
public $name;
public cmsDeliveryModule $module;
public function __construct($uriData, $dir, $name)
{
$this->dir = $dir;
$this->name = $name;
parent:: __construct($uriData);
$this->module = new cmsDeliveryModule();
$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));
}
/**
* Function to show main form in the module
*
* @category cmsDelivery Controller
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @returns bool true
*/
public function run($initEditMode = false)
{
$this->oView->TABLE = true;
$this->oView->setAsJSON('initEdit', $initEditMode);
$this->oView->setAsJSON('baseUrl', $this->gL());
$this->oView->setAsJSON('gL', $this->getJsTranslationsForView());
$this->oView->setAsJSON('vcfg', $this->getJsFieldValCfgForView());
$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 Mareks Keišs <keiss.mareks@gmail.com>
* @category cmsDelivery Controller
* @version 1.1
*/
public function createCmsTable($page = 1, $filter_json = '', $headers = false)
{
$pages = 0;
$filter = [];
if ($filter_json) {
$filter = cJSON::decode($filter_json, true);
}
$this->oView->MODULE_TITLE = $this->gM("Delivery classificators");
if (!!$filter && is_array($filter)) {
$this->oView->DEFAULT_SEARCH_VALUES = prepForDisplayHtml($filter);
}
$this->oView->LIST_HEADER = reOrderTableFields(
$this->getTableFields(),
$this->oUser->oCfg->getTableProperty($this->name),
);
$this->oView->LIST_DATA = $this->getModuleData($page, $filter, $pages);
$this->oView->LIST_ACTIONS = $this->getTableActions();
$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
* @return array of data
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @category cmsDelivery Controller
* @version 1.1
*/
public function getModuleData($page = 1, $filter = [], &$pages)
{
$where = 'AND 1';
$sort = "dc.id DESC";
$offset = (($page ? $page : 1) - 1) * $this->cmsTableRecordsPerPage;
$tFields = $this->getTableFields();
// Create sort by
if (getP('sortBy')) {
$sort = $this->module->db->genOrderByQuery(getP('sortBy'), getP('sortWay'), $tFields);
}
// Create where clause
if (count($filter)) {
$where = $this->module->db->genSearchFieldQuery($filter, $tFields);
}
$data = $this->module->getModuleRecords($where, $sort, $offset, $this->cmsTableRecordsPerPage);
$this->oView->ROWS = $rows = $this->module->db->getSelectedRecordsCount();
$pages = floor($rows / $this->cmsTableRecordsPerPage) + ($rows % $this->cmsTableRecordsPerPage ? 1 : 0);
return $data;
}
/**
* Function to disable / enable enum records marked in table
*
* @param array $baseField - base field to handle
* @param string $rows - passed user ids in cJSON string
* @return array of data
* @category cmsDelivery Controller
* @author Mareks Keišs <keiss.mareks@gmail.com>
*/
public function changeEnumFields($baseField, $rows)
{
if ($baseField && $rows = cJSON::decode($rows, true)) {
$baseField = substr($baseField, 2);
$fields = $this->getTableFields();
if (isset($fields[$baseField]['db_field'])) {
if ($this->module->changeRecordsStatus(
$this->module->db->prepData($rows),
$fields[$baseField]['db_field'],
)) {
return true;
}
}
}
cJSON::senderror();
}
/**
* Function to delete records marked in table
*
* @param string $rows - passed user ids in cJSON string
* @return array of data
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @category cmsDelivery 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();
}
}
}
public function editCmsTableRecord($id): array
{
if ($id) {
$this->oView->DATA = $this->module->getRecordDataById($id);
}
return [
'show' => ['buttons_top' => false, 'buttons_bottom' => true],
'btn' => $this->getFormButtons(),
'html' => $this->oView->genTemplate('deliveryEdit'),
];
}
public function saveRecordData($recordId, $fields)
{
$fields = $this->getSubmittedDataAsArray($fields ?? '');
if (empty($fields)) {
cJSON::senderror(['data' => $this->gM('No data')]);
}
// Validate fields
$valid = new cValidator($this);
$errors = $valid->checkArray($fields, $this->getJsFieldValCfgForView());
if ($errors !== true) {
cJSON::senderror(['data' => $errors]);
}
// Create base data array
$pbase = $fields['pbase'] ?? [];
$psettings = $fields['psettings'] ?? [];
$update = [
't_title' => $pbase['t_title'] ?? '',
't_cms' => $pbase['t_cms'] ?? '',
't_order' => $pbase['t_order'] ?? 0,
't_cost' => $pbase['t_cost'] ?? 0,
't_desc' => $pbase['t_desc'] ?? '',
't_enabled' => ($psettings['t_enabled'] ?? 0) ? 1 : 0,
't_default' => ($psettings['t_default'] ?? 0) ? 1 : 0,
];
if (!$recordId) {
$update['t_modified'] = time();
}
// Update / Insert record data
$oldId = $recordId;
if (!($recordId = $this->module->saveRecordDataById($recordId, $update))) {
cJSON::senderror(['data' => $this->gM('Error when saving data')]);
}
cJSON::sendok($oldId ? "" : ['newid' => $recordId]);
}
public function getTableFields(): array
{
if (!$this->oUser->chkPermission('CMS_APP_DELIVERY_SHOW_MANAGEMENT', false, false)) {
return [];
}
$statusData = [
'' => [
'title' => $this->gM("Select"),
],
0 => [
'title' => $this->gM("Active"),
],
1 => [
'title' => $this->gM("Inactive"),
],
];
$tFields = [
"checkbox" => [
'sort' => false,
'title' => '',
'class' => 'w4 center rchk',
],
"id" => [
'sort' => true,
'search' => true,
'title' => $this->gM("ID"),
'class' => 'w8 center sortDesc sort',
'db_field' => 'dc.id',
'type' => 'input',
],
"t_order" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Order"),
'class' => 'w8',
'db_field' => 'dc.t_order',
'type' => 'input',
],
"t_cms" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Title"),
'class' => 'w16',
'db_field' => 'dc.t_cms',
'type' => 'input',
],
"t_title" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Title"),
'class' => 'w16',
'db_field' => 'dc.t_title',
'type' => 'input',
],
"t_cost" => [
'sort' => true,
'search' => true,
'title' => $this->gM("Dev. cost"),
'class' => 'w8',
'db_field' => 'dc.t_cost',
'type' => 'input',
],
"status" => [
'sort' => false,
'search' => false,
'title' => $this->gM("Enabled"),
'class' => 'w8 center',
'field' => 't_enabled',
'db_field' => 'dc.t_enabled',
'type' => 'dropBox',
'data' => $statusData,
'default' => '',
'checkbox' => true,
],
"buttons" => [
'sort' => false,
'class' => 'w20',
'title' => $this->gM("Actions"),
'btnset' => [
'ledit' => [
'edit' => true,
'title' => $this->gM("Edit"),
],
'ldelete' => [
'title' => $this->gM("Delete"),
],
],
],
];
if (!$this->chkP('SYS_SHOW_TABLE_IDS')) {
unset($tFields['id']);
}
return $tFields;
}
public function getJsTranslationsForView(): array
{
return [
'yes' => $this->gM('Yes'),
'no' => $this->gM('No'),
'delTitle' => $this->gM('Delete deliver?'),
'delMessage' => $this->gM(
'Selected deliveries will be permanently deleted and cannot be recovered. Confirm delete?',
),
'buttonYes' => $this->gM('Delete selected deliveries'),
'buttonCancel' => $this->gM('Cancel operation'),
];
}
public function getTableActions(): array
{
return [
'inew' => [
'lbl' => $this->gM("Add new"),
'icon' => 'ico-addnew',
],
'istatus' => [
'lbl' => $this->gM("Change delivery status"),
'sepline' => true,
],
'idelete' => [
'lbl' => $this->gM("Delete selected"),
'sepline' => false,
'icon' => 'ico-delete2',
],
];
}
public function getJsFieldValCfgForView(): array
{
return [
't_title' => [
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => true,
cValidator::P_TEST => [cValidator::CHK_STR],
],
't_cms' => [
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => true,
cValidator::P_TEST => [cValidator::CHK_STR],
],
't_order' => [
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => false,
cValidator::P_TEST => [cValidator::CHK_NUM],
],
't_cost' => [
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => true,
cValidator::P_TEST => [cValidator::CHK_NUM],
],
't_desc' => [
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => true,
cValidator::P_TEST => [cValidator::CHK_STR],
],
];
}
public function getFormButtons(): array
{
return [
1 => [
'id' => 'fsave',
'lbl' => $this->gM('Save'),
],
2 => [
'id' => 'fcancel',
'lbl' => $this->gM('Cancel'),
],
];
}
}