320 lines
9.1 KiB
PHP
320 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Sys\sysPermissions\inc;
|
|
|
|
use BRSystems\Core\Cms\cmsClassificators\inc\cmsClassificatorsModule;
|
|
use BRSystems\Core\Library\cDb\cDb;
|
|
|
|
/**
|
|
* sysUsers Module
|
|
*
|
|
* @category sysPermissions Module
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @since Version 2.0
|
|
*/
|
|
class sysPermissionsModule
|
|
{
|
|
public $db;
|
|
|
|
/**
|
|
* Class constructor
|
|
*/
|
|
public function __construct() {
|
|
|
|
$this->db = cDb::getInstance();
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get module records
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category sysPermissions Module
|
|
* @param string $where - sql query
|
|
* @param string $sort - sql query
|
|
* @param int $offset - db table offset
|
|
* @param int $limitPerPage - limit per page
|
|
* @return array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getModuleRecords($where, $sort, $offset = 0, $limitPerPage = 20){
|
|
|
|
$sSql = "
|
|
SELECT SQL_CALC_FOUND_ROWS
|
|
pp.id,
|
|
pp.p_name,
|
|
pp.p_description,
|
|
pp.p_blocked,
|
|
mc.cl_name as type_name
|
|
FROM
|
|
site_permissions pp
|
|
LEFT JOIN mod_classificators mc ON
|
|
mc.id = pp.p_type_id
|
|
AND mc.cl_type = ". cmsClassificatorsModule::CT_TYPE_PERMISSION_GROUPS ."
|
|
WHERE
|
|
1 {$where}
|
|
ORDER BY
|
|
{$sort}
|
|
LIMIT
|
|
{$offset}, {$limitPerPage}
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getArray();
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to add or save record data
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category sysPermissions Module
|
|
* @param int $id - record id for update
|
|
* @param array $data - record data
|
|
* @return bool|int record new/old id
|
|
* @version 1.1
|
|
*/
|
|
public function saveRecordDataById($id, $data){
|
|
|
|
if($id){
|
|
if(!$this->db->_queryUpdate('site_permissions', $data, array('id' => $id))){
|
|
return false;
|
|
}
|
|
}else{
|
|
if(!($id = $this->db->_queryInsert('site_permissions', $data))){
|
|
return false;
|
|
}
|
|
}
|
|
return $id;
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get record data by id
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category sysPermissions Module
|
|
* @param int $id - record id
|
|
* @return array - record data
|
|
* @version 1.1
|
|
*/
|
|
public function getRecordDataById($id){
|
|
|
|
return $this->db->_querySelectRow('site_permissions', false, array('id' => $id));
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to delete table records form db
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category sysPermissions Module
|
|
* @param array $ids - record ids to delete
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function deleteRecords($ids){
|
|
|
|
return $this->db->execute("DELETE FROM `site_permissions` WHERE `id` IN (" . implode(',', $ids) . ")");
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to change record status in db table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category sysPermissions Module
|
|
* @param array $ids - record ids to affect
|
|
* @param string $field - field to update for status ENUM
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
|
|
public function changeRecordsStatus($ids, $field){
|
|
|
|
$time = time();
|
|
$ids = implode(',', $ids);
|
|
$status = $this->db->query("UPDATE site_permissions pp SET {$field} = '1', p_modified = '{$time}' WHERE {$field} = '0' AND id IN ({$ids})");
|
|
if(!$status)
|
|
return $status;
|
|
$status = $this->db->query("UPDATE site_permissions pp SET {$field} = '0', p_modified = '{$time}' WHERE {$field} = '1' AND id IN ({$ids}) AND p_modified <> '{$time}'");
|
|
return $status;
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get permission groups
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category CMS users
|
|
* @param int $id - permission id
|
|
* @return array of data
|
|
* @version 1.0
|
|
*/
|
|
public function getPermissionGroups($id = 0){
|
|
|
|
$sSql = "
|
|
SELECT
|
|
DISTINCT sug.id,
|
|
sug.group_name,
|
|
sug.group_description,
|
|
CASE WHEN sup.permissions_id > 0 THEN 1 ELSE 0 END as sel
|
|
FROM
|
|
site_users_groups sug
|
|
LEFT JOIN site_users_permissions sup ON
|
|
sup.groups_id = sug.id
|
|
AND sup.permissions_id = '" . $this->db->prepData($id) . "'
|
|
AND sup.user_id = 0
|
|
ORDER BY
|
|
sug.group_name
|
|
";
|
|
$this->db->query($sSql);
|
|
return $this->db->getArray();
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to delete table records form db
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param int $id - permission id
|
|
* @param array $vData - data to insert
|
|
* @return bool true / false
|
|
* @version 1.0
|
|
*/
|
|
public function rewriteLinkedPermissions($id, $vData){
|
|
|
|
if(!!$id){
|
|
$this->db->_queryDelete("site_users_permissions", array('permissions_id' => $id, 'groups_id' => array('o' => '>', 'v' => 0)));
|
|
if(count($vData)) $this->db->_queryInsertArray("site_users_permissions", $vData);
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to reserve next x ids in table
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param int $resRowsCount - how many rows + 1 dummy
|
|
* @return bool record highest id to work with
|
|
* @version 1.1
|
|
*/
|
|
public function rsvTableRows($resRowsCount){
|
|
|
|
if($resRowsCount && $resRowsCount > 0){
|
|
$this->db->query("
|
|
INSERT INTO site_permissions (id, p_description, p_created, p_modified)
|
|
VALUES (
|
|
( SELECT
|
|
COALESCE(MAX(tt.id),0)+{$resRowsCount}
|
|
FROM site_permissions tt
|
|
), '', -1, -1)
|
|
");
|
|
return $this->db->lastInsertedId();
|
|
}
|
|
return false;
|
|
}
|
|
/**
|
|
* Function to delete table temp data rows
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category APP pubMunBuildings
|
|
* @return bool
|
|
* @version 1.1
|
|
*/
|
|
public function deleteTempRows(){
|
|
|
|
$this->db->_queryDelete('site_permissions', array('p_modified' => -1));
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to insert group
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param array $vData - data to insert
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function insertGroup($vData) {
|
|
|
|
return $this->db->_queryInsert('site_users_groups', $vData);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to insert multi array in db
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param array $vData - data to insert
|
|
* @param string $sTable - table, by default its module main table
|
|
* @return bool true / false
|
|
* @version 1.1
|
|
*/
|
|
public function insertMulti($vData, $sTable = 'site_permissions') {
|
|
|
|
return $this->db->_queryInsertArray($sTable, $vData);
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get permission data for sql export
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param array $ids - permission ids, if we need just few permissions
|
|
* @return bool|array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getPermissionDataForSqlExport($ids) {
|
|
|
|
$sWhere = "";
|
|
if(!empty($ids) && is_array($ids))
|
|
$sWhere = " WHERE pp.id IN (". implode(',', $ids) .")";
|
|
|
|
|
|
$sSql = "
|
|
SELECT
|
|
pp.id,
|
|
pp.p_name,
|
|
pp.p_description,
|
|
pp.p_type_id,
|
|
pp.p_blocked,
|
|
pp.p_modified,
|
|
pp.p_created,
|
|
uup.groups_id
|
|
FROM
|
|
site_permissions pp
|
|
LEFT JOIN site_users_permissions uup ON
|
|
pp.id = uup.permissions_id
|
|
AND uup.user_id = 0
|
|
{$sWhere}
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getFieldBasedArrayFieldsAdv('id');
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get permission data for sql export
|
|
*
|
|
* @author Aivars Žukovskis <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param array $ids - groups ids
|
|
* @return bool|array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getGroupsDataForSqlExport($ids) {
|
|
|
|
return $this->db->_querySelect('site_users_groups', false, array('id' => $ids));
|
|
|
|
}
|
|
}
|