123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- //
- // User.swift
- // Qmera
- //
- // Created by Yayan Dwi on 28/09/21.
- //
- import Foundation
- import FMDB
- public class User: Model {
-
- public var pin: String
- public var firstName: String
- public var lastName: String
- public var thumb: String
- public var official: String?
- public var userType: String?
- public var privacy_flag: String?
- public var offline_mode: String?
- public var ex_block: String?
- public var ex_offmp: String?
- public var device_id: String
- public var status: String
-
- public var isSelected: Bool = false
- public var isMuted: Bool = false
-
- public init(pin: String) {
- self.pin = pin
- self.firstName = ""
- self.lastName = ""
- self.thumb = ""
- self.userType = ""
- self.privacy_flag = ""
- self.offline_mode = ""
- self.ex_block = ""
- self.ex_offmp = ""
- self.device_id = ""
- self.status = ""
- }
-
- public init(pin: String, firstName: String, lastName: String, thumb: String, userType: String = "0", privacy_flag: String = "", offline_mode: String = "", ex_block: String = "", official: String = "", ex_offmp: String = "", device_id: String = "", status: String = "") {
- self.pin = pin
- self.firstName = firstName
- self.lastName = lastName
- self.thumb = thumb
- self.userType = userType
- self.privacy_flag = privacy_flag
- self.offline_mode = offline_mode
- self.official = official
- self.ex_block = ex_block
- self.ex_offmp = ex_offmp
- self.device_id = device_id
- self.status = status
- }
-
- public static func == (lhs: User, rhs: User) -> Bool {
- return lhs.pin == rhs.pin
- }
-
- public var description: String {
- return "\(pin) \(firstName) \(lastName) \(thumb)"
- }
-
- public var fullName: String {
- return "\(firstName) \(lastName)".trimmingCharacters(in: .whitespaces)
- }
-
- public static func isOfficial(official_account: String) -> Bool {
- return official_account == "1"
- }
-
- public static func isVerified(official_account: String) -> Bool {
- return official_account == "2"
- }
-
- public static func isOfficialRegular(official_account: String) -> Bool {
- return official_account == "3"
- }
-
- public static func isInternal(userType: String) -> Bool {
- return userType == "23"
- }
-
- public static func isCallCenter(userType: String) -> Bool {
- return userType == "24"
- }
-
- public static func isAdmin(fmdb: FMDatabase? = nil) -> Bool {
- var position = ""
- if fmdb == nil {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- do {
- var groupId = ""
- if let cursorGroup = Database.shared.getRecords(fmdb: fmdb, query: "SELECT group_id FROM GROUPZ where group_type = 1 AND official = 1"), cursorGroup.next() {
- groupId = cursorGroup.string(forColumnIndex: 0) ?? ""
- cursorGroup.close()
- }
- if let cursorIsAdmin = Database.shared.getRecords(fmdb: fmdb, query: "SELECT position FROM GROUPZ_MEMBER where group_id = '\(groupId)' AND f_pin = '\(User.getMyPin()!)'"), cursorIsAdmin.next() {
- position = cursorIsAdmin.string(forColumnIndex: 0) ?? ""
- cursorIsAdmin.close()
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- } else {
- var groupId = ""
- if let cursorGroup = Database.shared.getRecords(fmdb: fmdb!, query: "SELECT group_id FROM GROUPZ where group_type = 1 AND official = 1"), cursorGroup.next() {
- groupId = cursorGroup.string(forColumnIndex: 0) ?? ""
- cursorGroup.close()
- }
- if let cursorIsAdmin = Database.shared.getRecords(fmdb: fmdb!, query: "SELECT position FROM GROUPZ_MEMBER where group_id = '\(groupId)' AND f_pin = '\(User.getMyPin()!)'"), cursorIsAdmin.next() {
- position = cursorIsAdmin.string(forColumnIndex: 0) ?? ""
- cursorIsAdmin.close()
- }
- }
- return position == "1"
- }
-
- public static func getMyPin() -> String? {
- if let value: String = SecureUserDefaults.shared.value(forKey: "me") {
- return value
- }
- return nil
- }
-
- public static func getData(pin: String?, fmdb: FMDatabase? = nil) -> User? {
- guard let pin = pin else {
- return nil
- }
- var user: User?
- if fmdb != nil {
- if let cursor = Database.shared.getRecords(fmdb: fmdb!, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account, quote from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "",
- userType: cursor.string(forColumnIndex: 4) ?? "",
- privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
- offline_mode: cursor.string(forColumnIndex: 6) ?? "",
- ex_block: cursor.string(forColumnIndex: 7) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- device_id: cursor.string(forColumnIndex: 8) ?? "",
- status: cursor.string(forColumnIndex: 10) ?? "")
- cursor.close()
- } else {
- user = User(pin: pin,
- firstName: "User".localized(),
- lastName: "",
- thumb: "",
- userType: "",
- privacy_flag: "",
- offline_mode: "",
- ex_block: "")
- }
- } else {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- do {
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account, quote from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "",
- userType: cursor.string(forColumnIndex: 4) ?? "",
- privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
- offline_mode: cursor.string(forColumnIndex: 6) ?? "",
- ex_block: cursor.string(forColumnIndex: 7) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- device_id: cursor.string(forColumnIndex: 8) ?? "",
- status: cursor.string(forColumnIndex: 10) ?? "")
- cursor.close()
- } else {
- user = User(pin: pin,
- firstName: "User".localized(),
- lastName: "",
- thumb: "",
- userType: "",
- privacy_flag: "",
- offline_mode: "",
- ex_block: "")
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- }
- return user
- }
-
- public static func getDataCanNil(pin: String?, fmdb: FMDatabase? = nil) -> User? {
- guard let pin = pin else {
- return nil
- }
- var user: User?
- if fmdb != nil {
- if let cursor = Database.shared.getRecords(fmdb: fmdb!, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "",
- userType: cursor.string(forColumnIndex: 4) ?? "",
- privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
- offline_mode: cursor.string(forColumnIndex: 6) ?? "",
- ex_block: cursor.string(forColumnIndex: 7) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- device_id: cursor.string(forColumnIndex: 8) ?? "")
- cursor.close()
- }
- } else {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- do {
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where f_pin = '\(pin)' OR device_id = '\(pin)'"), cursor.next() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "",
- userType: cursor.string(forColumnIndex: 4) ?? "",
- privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
- offline_mode: cursor.string(forColumnIndex: 6) ?? "",
- ex_block: cursor.string(forColumnIndex: 7) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- device_id: cursor.string(forColumnIndex: 8) ?? "")
- cursor.close()
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- }
- return user
- }
-
- public static func getDataFromNameCanNil(name: String?, fmdb: FMDatabase? = nil) -> User? {
- guard let name = name else {
- return nil
- }
- let listName = name.components(separatedBy: " ")
- let firstName = listName[0]
- var lastName = ""
- //print("firstName: \(firstName) <> lastName: \(lastName)")
- if listName.count > 1 {
- for i in 1..<listName.count {
- if lastName.isEmpty {
- lastName = listName[i]
- } else {
- lastName = lastName + " " + listName[i]
- }
- }
- }
- var user: User?
- if fmdb != nil {
- if let cursor = Database.shared.getRecords(fmdb: fmdb!, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where LOWER(first_name) = '\(firstName.lowercased())' AND LOWER(last_name) = '\(lastName.lowercased())'"), cursor.next() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "",
- userType: cursor.string(forColumnIndex: 4) ?? "",
- privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
- offline_mode: cursor.string(forColumnIndex: 6) ?? "",
- ex_block: cursor.string(forColumnIndex: 7) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- device_id: cursor.string(forColumnIndex: 8) ?? "")
- cursor.close()
- }
- } else {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- do {
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin, first_name, last_name, image_id, user_type, privacy_flag, offline_mode, ex_block, device_id, official_account from BUDDY where LOWER(first_name) = '\(firstName.lowercased())' AND LOWER(last_name) = '\(lastName.lowercased())'"), cursor.next() {
- user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
- firstName: cursor.string(forColumnIndex: 1) ?? "",
- lastName: cursor.string(forColumnIndex: 2) ?? "",
- thumb: cursor.string(forColumnIndex: 3) ?? "",
- userType: cursor.string(forColumnIndex: 4) ?? "",
- privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
- offline_mode: cursor.string(forColumnIndex: 6) ?? "",
- ex_block: cursor.string(forColumnIndex: 7) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- device_id: cursor.string(forColumnIndex: 8) ?? "")
- cursor.close()
- }
- } catch {
- rollback.pointee = true
- print("Access database error: \(error.localizedDescription)")
- }
- })
- }
- return user
- }
-
- }
|