ElixirVault/www/system-brs-core/sys/sysSiteModules/samples/pubSampleController.php

500 lines
16 KiB
PHP

<?php
namespace BRSystems\Core\App\MODULE_NAME_SAMPLE\inc;
use BRSystems\Core\Library\cCore\cCore;
use BRSystems\Core\Library\cJSON\cJSON;
use BRSystems\Core\Library\cValidator\cValidator;
/**
* MODULE_NAME_SAMPLE Controller
*
* @category MODULE_NAME_SAMPLE Controller Classificators
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @since Version 2.0
*/
class MODULE_NAME_SAMPLEController extends cCore
{
public $dir;
public $name;
public $module;
public $url;
/**
* Class constructor
*/
public function __construct($uriData, $dir, $name) {
$this->dir = $dir;
$this->name = $name;
parent :: __construct($uriData);
$this->module = new MODULE_NAME_SAMPLEModule();
$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 MODULE_NAME_SAMPLE Controller
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
*/
public function run($initEditMode = false){
$this->oView->TABLE = true;
$this->oView->setAsJSON('initEdit', $initEditMode);
$this->oView->setAsJSON('baseUrl', array('url' => $this->gL(), 'lang' => $this->uri_data['lang_code']));
$this->oView->setAsJSON('gM', $this->getTranslations());
$this->oView->setAsJSON('vcfg', $this->getUserValCfg());
$this->oView->SYS = true;
$this->oView->PRE_BODY = $this->oView->genHelperView('pub/preBodyContent');
$this->oView->display();
}
/**
* Function to create cms table & return for direct & ajax input
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE 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);
if(!!$filter && is_array($filter))
$this->oView->DEFAULT_SEARCH_VALUES = prepForDisplayHtml($filter);
$this->oView->MODULE_TITLE = $this->gM("MODULE_TITLE_SAMPLE");
$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('pub/table/data-table-new'),
'pcount' => $pages,
'rlimit' => $this->pubTableRecordsPerPage
);
// Send data to client
if($headers)
$data['header'] = $this->oView->genHelperView('pub/table/data-table-search');
return $data;
}
/**
* Function to get module data
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE 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 = "mb.id DESC";
$offset = (($page?$page:1)-1) * $this->pubTableRecordsPerPage;
$tFields = $this->getTableFields();
// Create sort by
$sortBy = getP('sortBy');
if($sortBy && $sortBy = $this->module->db->genOrderByQuery($sortBy, getP('sortWay'), $tFields))
$sort = $sortBy;
// Create where clause
if(count($filter))
$where = $this->module->db->genSearchFieldQuery($filter, $tFields);
$subWhere = "";
// Fetch data
$data = $this->module->getModuleRecords($where . $subWhere, $sort, $offset, $this->pubTableRecordsPerPage);
/*if(!empty($data)){
foreach($data as &$val) {
if (!empty($val['a_date'])) $val['a_date'] = date($this->PUBDateFormat, $val['a_date']);
}
unset($val);
}*/
$this->oView->ROWS = $rows = $this->module->db->getSelectedRecordsCount();
$pages = floor($rows / $this->pubTableRecordsPerPage) + ($rows%$this->pubTableRecordsPerPage?1:0);
return $data;
}
/**
* Function to disable / enable enum records marked in table
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Controller
* @param array $baseField - basefield to handle
* @param string $rows - passed user ids in JSON string
* @return bool|array of data
*/
public function changeEnumFields($baseField, $rows) {
if($baseField && $rows = cJSON::decode($rows, true)){
$baseField = substr($baseField, 2);
$fields = $this->getTableFields();
if(!!$rows && 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 MODULE_NAME_SAMPLE Controller
* @param string $rows - passed user ids in JSON string
* @return bool|array of data
*/
public function delete($rows) {
if($rows = cJSON::decode($rows, true)){
if(!!$rows){
if($this->module->deleteRecords($this->module->db->prepData($rows)))
return true;
}
}
cJSON::sendError();
return false;
}
/**
* Function to get table record data for edit mode
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Controller
* @param int $id - record id
* @return array of data
*/
public function editCmsTableRecord($id){
if($id) $this->oView->DATA = $this->module->getRecordDataById($id);
return array(
'show' => array('buttons_top' => false, 'buttons_bottom' => true),
'btn' => $this->getFormButtons(),
'html' => $this->oView->genTemplate('edit'),
);
}
/**
* Function to save Record data
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Controller
* @param int $recordId - record id
* @param string $fields - param JSON string
*/
public function saveRecordData($recordId, $fields) {
if($fields) $fields = $this->getSubmittedDataAsArray($fields);
if($fields){
// Validate fields
$valid = new cValidator($this);
$errors = $valid->checkArrayAdvance($fields, $this->getUserValCfg(false));
if(!!$errors) cJSON::sendError(array('data' => $errors));
$time = time();
$update = array(
/* DATA */
'a_field_1' => (!empty($fields['pbase']['field_1']) ? $fields['pbase']['field_1'] : 0),
'a_field_2' => (!empty($fields['pbase']['field_2']) ? $fields['pbase']['field_2'] : ''),
'a_blocked' => (!empty($fields['psettings']['a_blocked']) ? 1 : 0),
/* INFO */
'a_modified' => $time,
'a_modified_by' => $this->oUser->getUser(),
);
if(!$recordId){
$update['a_created'] = $time;
$update['a_created_by'] = $this->oUser->getUser();
}
$oldId = $recordId;
if($recordId = $this->module->saveRecordDataById($recordId, $update))
cJSON::sendOk($oldId?"":array('newid' => $recordId));
cJSON::sendError($this->gM("Error occurred while trying to save base data"));
}
}
/**
* Function to get js validator cfg for user profile
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Edit
* @param bool $bPrepDataCfg - reconfigure cfg data array
* @return boolean|array of data
*/
public function getUserValCfg($bPrepDataCfg = true){
$valCfg = array(
// Additional validation config
'cfg' => array(
cValidator::CFG_CHK_FIELDS_SET => array(
'pbase' => cValidator::CFG_ALL_FIELDS,
),
cValidator::CFG_BIND_FIELD => 'data-validator',
),
'pbase' => array(
'a_field_1' => array(
cValidator::P_TYPE => cValidator::TY_DEC,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_NUM),
),
'a_field_2' => array(
cValidator::P_TYPE => cValidator::TY_STR,
cValidator::P_COMP => 0,
cValidator::P_TEST => array(cValidator::CHK_STR),
),
),
);
return ($bPrepDataCfg ? cValidator::prepValCfgForValidation($valCfg) : $valCfg);
}
/**
* Function to create table headers + search
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Controller
* @return bool|array of data
* @version 1.1
*/
public function getTableFields() {
if(!$this->chkP('SAMPLE_MODULE_SHOW_MANAGEMENT'))
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 mob-sh'
),
"id" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("ID"),
'class' => 'w8 center',
'db_field' => 'mb.id',
'type' => 'input',
),
"field_1" => array(
'sort' => true,
'search' => true,
'title' => $this->gM("field 1"),
'class' => 'w12 mob-sh mob-sh-main',
'db_field' => 'mb.a_field_1',
'type' => 'input',
),
"field_2" => array(
'sort' => true,
'search' => false,
'title' => $this->gM("field 2"),
'class' => 'w16',
'db_field' => 'mb.a_field_2',
'type' => 'input',
),
"created_by" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Created by"),
'class' => 'w10 mob-sh mob-sh-main center',
'db_field' => 'mb.a_created_by',
),
"modified_by" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Modified by"),
'class' => 'w10 mob-sh mob-sh-main center',
'db_field' => 'mb.a_modified_by',
),
"status" => array(
'sort' => false,
'search' => false,
'title' => $this->gM("Blocked"),
'class' => 'w8 center',
'field' => 'cl_blocked',
'db_field' => 'mb.a_blocked',
'type' => 'dropBox',
'data' => $statusData,
'default' => '',
),
"buttons" => array(
'sort' => false,
'class' => 'w18 mob-sh',
'title' => $this->gM("Actions"),
'btnset' => array(
'ledit' => array(
'edit' => true,
'title' => '',
),
'ldelete' => array(
'title' => '',
'class' => 'tree-ico ico-delete'
)
)
)
);
// Remove buttons/columns based on permissions;
if(!$this->chkP('SAMPLE_MODULE_DELETE'))
unset($tFields['buttons']['btnset']['ldelete']);
if(!$this->chkP('SAMPLE_MODULE_EDIT')){
unset($tFields['status']);
unset($tFields['buttons']['btnset']['ledit']);
}
if(!$this->chkP('APP_SHOW_TABLE_IDS')){
unset($tFields['id']);
}else{
// Add selected or default sort way
if(!getP('sortBy'))
$tFields['id']['class'] .= " sort sortDesc";
}
return $tFields;
}
/**
* Function to get js translations for view
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category pubPartners Controller
* @return array of data
*/
public function getTranslations(){
return cValidator::getJSTranslations(
$this, $this->getUserValCfg(), array(
'yes' => $this->gM('Yes'),
'no' => $this->gM('No'),
'delTitle' => $this->gM('Delete entry?'),
'delMessage' => $this->gM('Selected entries will be permanently deleted and cannot be recovered. Confirm delete?'),
'buttonYes' => $this->gM('Delete selected entries'),
'buttonCancel' => $this->gM('Cancel operation'),
'back' => $this->gM('Back'),
'msg_select' => $this->gM('-- Select --'),
)
);
}
/**
* Function to get table actions
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Controller
* @return array of data
*/
public function getTableActions(){
$actions = array(
'inew' => array(
'lbl' => $this->gM("Add new"),
'icon' => 'ico-add-new-l',
'p' => 'SAMPLE_MODULE_ADD_NEW',
),
'sepline',
'idelete' => array(
'lbl' => $this->gM("Delete selected"),
'icon' => 'ico-delete-l',
'p' => 'SAMPLE_MODULE_DELETE',
),
);
// Chk has permissions for actions
if(!!$actions && is_array($actions)){
foreach($actions as $k=>&$v){
if(is_array($v) && isset($v['p'])){
if(!$this->chkP($v['p']))
unset($actions[$k]);
unset($actions[$k]['p']);
}
}
unset($v);
}
// Reformat drop down
reformatDropDown($actions);
return $actions;
}
/**
* Function to get form buttons
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category MODULE_NAME_SAMPLE Controller
* @return array of data
*/
public function getFormButtons(){
return array(
1 => array(
'id' => 'fsave',
'lbl' => $this->gM('Save'),
'class' => 'tbtn tbtn-ico ico-ok'
),
2 => array(
'id' => 'fcancel',
'lbl' => $this->gM('Cancel'),
'class' => 'tbtn'
)
);
}
}