db = cDb::getInstance(); } /** * Function to get module records * * @author Mareks Keišs * @category cmsPubUsers 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 *, CONCAT(mu.user_name, ' ', mu.user_surname) as user_title FROM mod_users mu WHERE mu.user_deleted = '0' HAVING 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 * @category cmsPubUsers Module * @param int $id - record id for update * @param array $data - record data * @return bool record new/old id * @version 1.1 */ public function saveRecordDataById($id, $data) { if ($id) { if (!$this->db->_queryUpdate('mod_users', $data, ['id' => $id])) { return false; } } else { if (!($id = $this->db->_queryInsert('mod_users', $data))) { return false; } } return $id; } public function getRecordDataById(int $id) { return $this->db->_querySelectRow('mod_users', false, ['id' => $id]); } /** * Function to delete table records form db * * @author Mareks Keišs * @category cmsPubUsers Module * @param mixed $ids - record ids to delete * @return bool true / false * @version 1.1 */ public function deleteRecords($ids): bool { if (!is_array($ids)) { return false; } $time = time(); $ids = implode(',', $ids); return $this->db->query("UPDATE mod_users SET user_deleted = '1', user_modified = '$time' WHERE id IN ($ids)"); } /** * Function to change record status in db table * * @author Mareks Keišs * @category cmsPubUsers 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){ if(count($ids) && $field){ $time = time(); $ids = implode(',', $ids); $status = $this->db->query("UPDATE mod_users mu SET {$field} = '1', user_modified = '{$time}' WHERE {$field} = '0' AND id IN ({$ids})".""); if(!$status) return $status; $status = $this->db->query("UPDATE mod_users mu SET {$field} = '0', user_modified = '{$time}' WHERE {$field} = '1' AND id IN ({$ids}) AND user_modified <> '{$time}'".""); return $status; } } /** * Function to get user permissions by userid * * @author Mareks Keišs * @category cmsPubUsers Module * @param int $userId - user id * @return array of data * @version 1.1 */ public function getUserPermissionsByUserId($userId): array { $userId = $userId ?: 0; $sSql = " SELECT DISTINCT sp.id, sp.p_name, sp.p_description, stp.user_id > 0 as sel FROM mod_permissions sp LEFT JOIN mod_users_permissions stp ON stp.permissions_id = sp.id AND stp.user_id = '" . $this->db->prepData($userId) . "' AND stp.groups_id = 0 ORDER BY sp.p_name "; $this->db->query($sSql); return $this->db->getArray() ?? []; } /** * Function to get user permission groups by user id * * @author Mareks Keišs * @category cmsPubUsers Module * @param int $userId - user id * @return array of data * @version 1.1 */ public function getUserPermissionGroupsByUserId($userId): array { $userId = $userId ?: 0; $sSql = " SELECT DISTINCT sug.id, sug.group_name, sug.group_description, sup.user_id > 0 as sel FROM mod_permissions_groups sug LEFT JOIN mod_users_permissions sup ON sup.groups_id = sug.id AND sup.user_id = '" . $this->db->prepData($userId) . "' AND sup.permissions_id = 0 ORDER BY sug.group_name "; $this->db->query($sSql); return $this->db->getArray() ?? []; } /** * Function to handle & rewrite user permissions * * @author Mareks Keišs * @category cmsPubUsers Module * @param int $userId - user id * @param array $permissions * @return bool true / false * @version 1.1 */ public function updateUserPermissions($userId, $permissions = []): bool { if (!$userId) { return false; } $this->db->_queryDelete("mod_users_permissions", ['user_id' => $userId]); if (count($permissions)) { $this->db->_queryInsertArray("mod_users_permissions", $permissions); } return true; } /** * Function to get user data by login * * @author Mareks Keišs * @category cmsPubUsers Module * @param int $userId - user id * @param string $login - user login * @return array user data * @version 1.1 */ public function getUserDataByLogin($userId, $login): array { $userId = $userId ?: 0; $sSql = " SELECT * FROM mod_users su WHERE su.user_deleted = '0' AND su.user_login = '" . $this->db->prepData($login) . "' AND su.id <> '" . $this->db->prepData($userId) . "' "; $this->db->query($sSql); return $this->db->getRow() ?? []; } }