ElixirVault/www/system-brs-core/sys/sysLanguages/inc/sysLanguagesController.php

569 lines
18 KiB
PHP

<?php
namespace BRSystems\Core\Sys\sysLanguages\inc;
use BRSystems\Core\Library\cCore\cCore;
use BRSystems\Core\Library\cJSON\cJSON;
use BRSystems\Core\Library\cValidator\cValidator;
/**
* sysLanguages Controller
*
* @category sysLanguages Controller
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @since Version 2.0
*/
class sysLanguagesController 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 sysLanguagesModule();
$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 sysLanguages Controller
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @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('gM', $this->getTranslations());
$this->oView->setAsJSON('vcfg', $this->getValCfg());
$this->oView->display();
}
/**
* Function to create cms table & return for direct & ajax input
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @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
* @version 1.1
*/
public function createCmsTable($page = 1, $filter_json = '', $headers = false) {
$pages = 0;
$filter = array();
if($filter_json){
$filter = cJSON::decode($filter_json, true);
}
$this->oView->MODULE_TITLE = $this->gM("System Languages");
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 = array(
'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
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @param int $page
* @param array $filter
* @param int $pages
* @return array of data
* @version 1.1
*/
public function getModuleData($page = 1, $filter = array(), &$pages) {
$where = "AND 1";
$sort = "id ASC";
$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);
$subWhere = "";
/* if(isset($filter['field_search'])){
$filter['field_search'] = $this->module->db->prepData($filter['field_search']);
$subWhere = "
AND (
ll.lang_code like '%{$filter['field_search']}%'
OR ll.lang_country like '%{$filter['field_search']}%'
OR ll.lang_title like '%{$filter['field_search']}%'
)";
unset($filter['field_search']);
} */
$data = $this->module->getModuleRecords($where . $subWhere, $sort, $offset, $this->cmsTableRecordsPerPage);
if($data && count($data)){
foreach($data as &$v){
$v['type'] = ($v['type']?'CMS':'PUB');
$v['order'] = ($v['order']?$v['order']:'-');
$v['default'] = ($v['default']?'&#10003;':'');
}
}
$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
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @param array $baseField - base field to handle
* @param string $rows - passed user ids in JSON string
* @return array of data
*/
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();
return false;
}
/**
* Function to delete records marked in table
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @param string $rows - passed user ids in JSON string
* @return array of data
*/
public function delete($rows) {
if($rows = cJSON::decode($rows, true)){
if($this->module->deleteRecords($this->module->db->prepData($rows))){
return true;
}else{
cJSON::sendError();
}
}
return false;
}
/**
* Function to get table record data for edit mode
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @param int $id - record id
* @return array of data
*/
public function editCmsTableRecord($id) {
if($id){
$LANG = $this->module->getRecordDataById($id);
if(date('d-m-Y', $LANG['lang_created']) == date('d-m-Y')){
$LANG['lang_created'] = date('H:i', $LANG['lang_created']);
}else{
$LANG['lang_created'] = date($this->CMSDateFormat, $LANG['lang_created']);
}
if(date('d-m-Y', $LANG['lang_modified']) == date('d-m-Y')){
$LANG['lang_modified'] = date('H:i', $LANG['lang_modified']);
}else{
$LANG['lang_modified'] = date($this->CMSDateFormat, $LANG['lang_modified']);
}
$this->oView->LANG = $LANG;
}
return array(
'show' => array('buttons_top' => false, 'buttons_bottom' => true),
'btn' => $this->getFormButtons(),
'html' => $this->oView->genTemplate('languageEdit'),
);
}
/**
* Function to save Record data
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @param int $recordId - record id
* @param string $fields - param JSON string
* @return array of data
*/
public function saveRecordData($recordId, $fields) {
if($fields){
$fields = $this->getSubmittedDataAsArray($fields);
}
// Start save process
if($fields){
// Validate fields
$valid = new cValidator($this);
$errors = $valid->checkArray($fields, $this->getValCfg());
if($errors !== true){
cJSON::sendError(array('data' => $errors));
}
$update = array(
'type' => (!empty($fields['psettings']['lang_type']) ? 1 : 0),
'order' => (!empty($fields['pbase']['lang_order']) ? $fields['pbase']['lang_order'] : 0),
'default' => (!empty($fields['psettings']['lang_default']) ? 1 : 0),
'lang_code' => (!empty($fields['pbase']['lang_code']) ? $fields['pbase']['lang_code'] : ''),
'lang_country' => $fields['pbase']['lang_country'],
'lang_title' => $fields['pbase']['lang_title'],
'lang_locale' => '',
'lang_blocked' => (!empty($fields['psettings']['lang_blocked']) ? 1 : 0),
'lang_cms_e' => (!empty($fields['psettings']['lang_cms_e']) ? 1 : 0),
'lang_modified' => time(),
);
if(!$recordId) $update['lang_created'] = time();
// No other default languages exists
if(!$update['default'] && !$this->module->chkDefaultExists($update['type'], $recordId)){
cJSON::sendError($this->gM('No other default languages exists!'));
}
// Update / Insert record data
$oldId = $recordId;
if(!($recordId = $this->module->saveRecordDataById($recordId, $update))){
cJSON::sendError($this->gM('Data not saved...'));
}
// Reset all translation data
if(!empty($fields['psettings']['lang_reset_e'])){
$this->module->updLanguageDefaultMessages($recordId, $update['type'], $update['default']);
$this->oApc->cleanup();
}
cJSON::sendOk($oldId?"":array('newid' => $recordId));
}
}
/**
* Function to create table headers + search
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @return array of data
* @version 1.1
*/
public function getTableFields() {
if(!$this->oUser->chkPermission('SYS_LANGUAGES_SHOW_MANAGEMENT', false, false)){
return false;
}
$statusData = array(
'' => array(
'title' => $this->gM("Select")
),
0 => array(
'title' => $this->gM("Active")
),
1 => array(
'title' => $this->gM("Inactive")
)
);
$tFields = array(
"checkbox" => array(
'sort' => false,
'title' => '',
'class' => 'w4 center rchk'
),
"id" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("ID"),
'class' => 'w8 center sort',
'db_field' => 'll.id',
'type' => 'input',
),
"lang_title" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("Language"),
'class' => 'w16',
'db_field' => 'll.lang_title',
'type' => 'input',
),
"lang_country" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("Country"),
'class' => 'w16',
'db_field' => array ('ll.lang_country', ' ll.lang_code'),
'db_link' => 'OR',
'type' => 'input',
),
"lang_code" => array(
'sort' => true,
'search' => false,
'title' => $this->gM("Code"),
'class' => 'w16',
'db_field' => 'll.lang_code',
'type' => 'input',
),
"order" => array(
'sort' => true,
'search' => false,
'title' => $this->gM("Order"),
'class' => 'w7 center',
'db_field' => 'll.order',
'type' => 'input',
),
"type" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Type"),
'class' => 'w7 center',
'db_field' => 'll.type',
'type' => 'input',
),
"default" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Default"),
'class' => 'w7 center',
'db_field' => 'll.default',
'type' => 'input',
),
"visible" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("CMS Enabled"),
'class' => 'w7 center',
'field' => 'lang_cms_e',
'db_field' => 'll.lang_cms_e',
'type' => 'input',
'checkbox' => true,
),
"status" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Blocked"),
'class' => 'w7 center',
'field' => 'lang_blocked',
'db_field' => 'll.lang_blocked',
'type' => 'dropBox',
'data' => $statusData,
'default' => '',
//'checkbox' => true,
),
"buttons" => array(
'sort' => false,
'class' => 'w20',
'title' => $this->gM("Actions"),
'btnset' => array(
'ledit' => array(
'edit' => true,
'title' => $this->gM("Edit"),
),
'ldelete' => array(
'title' => $this->gM("Delete"),
)
)
)
);
if(!$this->chkP('SYS_SHOW_TABLE_IDS'))
unset($tFields['id']);
return $tFields;
}
/**
* Function to get js translations for view
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @return array of data
*/
public function getTranslations(){
return array(
'yes' => $this->gM('Yes'),
'no' => $this->gM('No'),
'delTitle' => $this->gM('Delete languages?'),
'delMessage' => $this->gM('Selected languages will be permanently deleted and cannot be recovered. Confirm delete?'),
'buttonYes' => $this->gM('Delete selected languages'),
'buttonCancel' => $this->gM('Cancel operation'),
);
}
/**
* Function to get table actions
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @return array of data
*/
public function getTableActions(){
$actions = array(
'inew' => array(
'lbl' => $this->gM("Add new"),
'icon' => 'nico-add'
),
'istatus' => array(
'lbl' => $this->gM("Change item status"),
),
'sepline',
'idelete' => array(
'lbl' => $this->gM("Delete selected"),
'icon' => 'nico-delete'
),
);
// Reformat drop down
reformatDropDown($actions, false);
return $actions;
}
/**
* Function to get js validator cfg
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @return array of data
*/
public function getValCfg(){
$compWarn = $this->gM('Please fill in the field!');
return array(
'lang_title' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 1,
cValidator::P_TEST => array(cValidator::CHK_STR),
'comp-warning' => $compWarn
),
'lang_country' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 1,
cValidator::P_TEST => array(cValidator::CHK_STR),
'comp-warning' => $compWarn
),
'lang_code' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 1,
cValidator::P_TEST => array(cValidator::CHK_STR, cValidator::CHK_LEN),
cValidator::P_ADDS => array('max' => 2),
'comp-warning' => $compWarn
),
'lang_order' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
'comp-warning' => $compWarn
),
);
}
/**
* Function to get form buttons
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category sysLanguages Controller
* @return array of data
*/
public function getFormButtons(){
return array(
1 => array(
'id' => 'fsave',
'lbl' => $this->gM('Save')
),
2 => array(
'id' => 'fcancel',
'lbl' => $this->gM('Cancel')
)
);
}
/**
* Function to get languages Switch for node id
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Module
* @param int $mapId - site map id
* @param string $param - url params
* @return array or languages
*/
public function getLangSwitchForMapId($mapId, $param) {
// Get default links in languages & select current switch
if($languages = $this->module->getLangSwitchForId($mapId)){
foreach ($languages as &$v) {
$v['url'] = $this->getLinkForID($mapId, $v['lang_id'], true, $param);
// Set selected language
$v['selected'] = $this->uri_data['p_lang_id'] == $v['lang_id'] ? true : false;
}
unset($v);
}
return $languages;
}
}