361 lines
10 KiB
PHP
361 lines
10 KiB
PHP
<?php
|
|
|
|
namespace BRSystems\Core\Cms\cmsPermissions\inc;
|
|
|
|
use BRSystems\Core\Cms\cmsClassificators\inc\cmsClassificatorsModule;
|
|
use BRSystems\Core\Library\cDb\cDb;
|
|
|
|
/**
|
|
* sysUsers Module
|
|
*
|
|
* @category cmsPermissions Module
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @since Version 2.0
|
|
*/
|
|
class cmsPermissionsModule
|
|
{
|
|
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 cmsPermissions 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
|
|
mod_permissions pp
|
|
LEFT JOIN mod_classificators mc ON
|
|
mc.id = pp.p_type_id
|
|
AND mc.cl_type = ". cmsClassificatorsModule::CT_PUB_PERMISSION_TYPE ."
|
|
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 cmsPermissions 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('mod_permissions', $data, array('id' => $id))){
|
|
return false;
|
|
}
|
|
}else{
|
|
if(!($id = $this->db->_queryInsert('mod_permissions', $data))){
|
|
return false;
|
|
}
|
|
}
|
|
return $id;
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get record data by id
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions Module
|
|
* @param int $id - record id
|
|
* @return array - record data
|
|
* @version 1.1
|
|
*/
|
|
public function getRecordDataById($id){
|
|
|
|
return $this->db->_querySelectRow('mod_permissions', false, array('id' => $id));
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to get permission groups
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category CMS users
|
|
* @param int $id - permission id
|
|
* @return array of data
|
|
* @version 1.1
|
|
*/
|
|
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
|
|
mod_permissions_groups sug
|
|
LEFT JOIN mod_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.1
|
|
*/
|
|
public function rewriteLinkedPermissions($id, $vData){
|
|
|
|
if(!!$id){
|
|
$this->db->_queryDelete("mod_users_permissions", array('permissions_id' => $id, 'groups_id' => array('o' => '>', 'v' => 0)));
|
|
if(count($vData)) $this->db->_queryInsertArray("mod_users_permissions", $vData);
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to delete table records form db
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions 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 `mod_permissions` WHERE `id` IN (" . implode(',', $ids) . ")");
|
|
|
|
}
|
|
|
|
/**
|
|
* Function to change record status in db table
|
|
*
|
|
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
|
|
* @category cmsPermissions 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 mod_permissions pp SET {$field} = '1', p_modified = '{$time}' WHERE {$field} = '0' AND `id` IN ({$ids})");
|
|
if(!$status)
|
|
return $status;
|
|
$status = $this->db->query("UPDATE mod_permissions pp SET {$field} = '0', p_modified = '{$time}' WHERE {$field} = '1' AND `id` IN ({$ids}) AND p_modified <> '{$time}'");
|
|
return $status;
|
|
|
|
}
|
|
|
|
public function getClassificators(){
|
|
|
|
$sSql = "
|
|
SELECT
|
|
cc.id,
|
|
cc.cl_name as title
|
|
FROM
|
|
mod_classificators cc
|
|
WHERE
|
|
cl_type = " . cmsClassificatorsModule::CT_PUB_PERMISSION_TYPE . "
|
|
";
|
|
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getFieldBasedArrayFields('id');
|
|
}
|
|
|
|
/**
|
|
* 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 mod_permissions (id, p_modified, p_description)
|
|
VALUES (
|
|
( SELECT
|
|
COALESCE(MAX(tt.id),0)+{$resRowsCount}
|
|
FROM mod_permissions tt
|
|
),
|
|
-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('mod_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('mod_permissions_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 = 'mod_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
|
|
FROM
|
|
mod_permissions pp
|
|
{$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 - permission ids, if we need just few permissions
|
|
* @return bool|array of data
|
|
* @version 1.1
|
|
*/
|
|
public function getPermission2GroupLinkDataForSqlExport($ids) {
|
|
|
|
$sWhere = "";
|
|
if(!empty($ids) && is_array($ids))
|
|
$sWhere = "pp.permissions_id IN (". implode(',', $ids) .")";
|
|
|
|
|
|
$sSql = "
|
|
SELECT
|
|
pp.groups_id,
|
|
pp.permissions_id
|
|
FROM
|
|
mod_users_permissions pp
|
|
WHERE
|
|
pp.user_id = 0
|
|
AND {$sWhere}
|
|
";
|
|
|
|
$this->db->query($sSql);
|
|
return $this->db->getFieldBasedArrayFieldsAdv('groups_id', 'permissions_id', false);
|
|
}
|
|
|
|
/**
|
|
* 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('mod_permissions_groups', false, array('id' => $ids));
|
|
|
|
}
|
|
}
|