User.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // User.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 28/09/21.
  6. //
  7. import Foundation
  8. import FMDB
  9. public class User: Model {
  10. public var pin: String
  11. public var firstName: String
  12. public var lastName: String
  13. public var thumb: String
  14. public var official: String?
  15. public var userType: String?
  16. public var privacy_flag: String?
  17. public var offline_mode: String?
  18. public var ex_block: String?
  19. public var ex_offmp: String?
  20. public var device_id: String
  21. public var isSelected: Bool = false
  22. public var isMuted: Bool = false
  23. public init(pin: String) {
  24. self.pin = pin
  25. self.firstName = ""
  26. self.lastName = ""
  27. self.thumb = ""
  28. self.userType = ""
  29. self.privacy_flag = ""
  30. self.offline_mode = ""
  31. self.ex_block = ""
  32. self.ex_offmp = ""
  33. self.device_id = ""
  34. }
  35. 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 = "") {
  36. self.pin = pin
  37. self.firstName = firstName
  38. self.lastName = lastName
  39. self.thumb = thumb
  40. self.userType = userType
  41. self.privacy_flag = privacy_flag
  42. self.offline_mode = offline_mode
  43. self.official = official
  44. self.ex_block = ex_block
  45. self.ex_offmp = ex_offmp
  46. self.device_id = device_id
  47. }
  48. public static func == (lhs: User, rhs: User) -> Bool {
  49. return lhs.pin == rhs.pin
  50. }
  51. public var description: String {
  52. return "\(pin) \(firstName) \(lastName) \(thumb)"
  53. }
  54. public var fullName: String {
  55. return "\(firstName) \(lastName)".trimmingCharacters(in: .whitespaces)
  56. }
  57. public static func isOfficial(official_account: String) -> Bool {
  58. return official_account == "1"
  59. }
  60. public static func isVerified(official_account: String) -> Bool {
  61. return official_account == "2"
  62. }
  63. public static func isOfficialRegular(official_account: String) -> Bool {
  64. return official_account == "3"
  65. }
  66. public static func isInternal(userType: String) -> Bool {
  67. return userType == "23"
  68. }
  69. public static func isCallCenter(userType: String) -> Bool {
  70. return userType == "24"
  71. }
  72. public static func isAdmin(fmdb: FMDatabase? = nil) -> Bool {
  73. var position = ""
  74. if fmdb == nil {
  75. Database.shared.database?.inTransaction({ fmdb, rollback in
  76. do {
  77. var groupId = ""
  78. if let cursorGroup = Database.shared.getRecords(fmdb: fmdb, query: "SELECT group_id FROM GROUPZ where group_type = 1 AND official = 1"), cursorGroup.next() {
  79. groupId = cursorGroup.string(forColumnIndex: 0) ?? ""
  80. cursorGroup.close()
  81. }
  82. 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() {
  83. position = cursorIsAdmin.string(forColumnIndex: 0) ?? ""
  84. cursorIsAdmin.close()
  85. }
  86. } catch {
  87. rollback.pointee = true
  88. print("Access database error: \(error.localizedDescription)")
  89. }
  90. })
  91. } else {
  92. var groupId = ""
  93. if let cursorGroup = Database.shared.getRecords(fmdb: fmdb!, query: "SELECT group_id FROM GROUPZ where group_type = 1 AND official = 1"), cursorGroup.next() {
  94. groupId = cursorGroup.string(forColumnIndex: 0) ?? ""
  95. cursorGroup.close()
  96. }
  97. 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() {
  98. position = cursorIsAdmin.string(forColumnIndex: 0) ?? ""
  99. cursorIsAdmin.close()
  100. }
  101. }
  102. return position == "1"
  103. }
  104. public static func getMyPin() -> String? {
  105. if let value: String = SecureUserDefaults.shared.value(forKey: "me") {
  106. return value
  107. }
  108. return nil
  109. }
  110. public static func getData(pin: String?, fmdb: FMDatabase? = nil) -> User? {
  111. guard let pin = pin else {
  112. return nil
  113. }
  114. var user: User?
  115. if fmdb != nil {
  116. 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() {
  117. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  118. firstName: cursor.string(forColumnIndex: 1) ?? "",
  119. lastName: cursor.string(forColumnIndex: 2) ?? "",
  120. thumb: cursor.string(forColumnIndex: 3) ?? "",
  121. userType: cursor.string(forColumnIndex: 4) ?? "",
  122. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  123. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  124. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  125. official: cursor.string(forColumnIndex: 9) ?? "",
  126. device_id: cursor.string(forColumnIndex: 8) ?? "")
  127. cursor.close()
  128. } else {
  129. user = User(pin: pin,
  130. firstName: "User".localized(),
  131. lastName: "",
  132. thumb: "",
  133. userType: "",
  134. privacy_flag: "",
  135. offline_mode: "",
  136. ex_block: "")
  137. }
  138. } else {
  139. Database.shared.database?.inTransaction({ fmdb, rollback in
  140. do {
  141. 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() {
  142. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  143. firstName: cursor.string(forColumnIndex: 1) ?? "",
  144. lastName: cursor.string(forColumnIndex: 2) ?? "",
  145. thumb: cursor.string(forColumnIndex: 3) ?? "",
  146. userType: cursor.string(forColumnIndex: 4) ?? "",
  147. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  148. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  149. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  150. official: cursor.string(forColumnIndex: 9) ?? "",
  151. device_id: cursor.string(forColumnIndex: 8) ?? "")
  152. cursor.close()
  153. } else {
  154. user = User(pin: pin,
  155. firstName: "User".localized(),
  156. lastName: "",
  157. thumb: "",
  158. userType: "",
  159. privacy_flag: "",
  160. offline_mode: "",
  161. ex_block: "")
  162. }
  163. } catch {
  164. rollback.pointee = true
  165. print("Access database error: \(error.localizedDescription)")
  166. }
  167. })
  168. }
  169. return user
  170. }
  171. public static func getDataCanNil(pin: String?, fmdb: FMDatabase? = nil) -> User? {
  172. guard let pin = pin else {
  173. return nil
  174. }
  175. var user: User?
  176. if fmdb != nil {
  177. 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() {
  178. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  179. firstName: cursor.string(forColumnIndex: 1) ?? "",
  180. lastName: cursor.string(forColumnIndex: 2) ?? "",
  181. thumb: cursor.string(forColumnIndex: 3) ?? "",
  182. userType: cursor.string(forColumnIndex: 4) ?? "",
  183. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  184. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  185. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  186. official: cursor.string(forColumnIndex: 9) ?? "",
  187. device_id: cursor.string(forColumnIndex: 8) ?? "")
  188. cursor.close()
  189. }
  190. } else {
  191. Database.shared.database?.inTransaction({ fmdb, rollback in
  192. do {
  193. 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() {
  194. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  195. firstName: cursor.string(forColumnIndex: 1) ?? "",
  196. lastName: cursor.string(forColumnIndex: 2) ?? "",
  197. thumb: cursor.string(forColumnIndex: 3) ?? "",
  198. userType: cursor.string(forColumnIndex: 4) ?? "",
  199. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  200. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  201. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  202. official: cursor.string(forColumnIndex: 9) ?? "",
  203. device_id: cursor.string(forColumnIndex: 8) ?? "")
  204. cursor.close()
  205. }
  206. } catch {
  207. rollback.pointee = true
  208. print("Access database error: \(error.localizedDescription)")
  209. }
  210. })
  211. }
  212. return user
  213. }
  214. public static func getDataFromNameCanNil(name: String?, fmdb: FMDatabase? = nil) -> User? {
  215. guard let name = name else {
  216. return nil
  217. }
  218. let listName = name.components(separatedBy: " ")
  219. let firstName = listName[0]
  220. var lastName = ""
  221. //print("firstName: \(firstName) <> lastName: \(lastName)")
  222. if listName.count > 1 {
  223. for i in 1..<listName.count {
  224. if lastName.isEmpty {
  225. lastName = listName[i]
  226. } else {
  227. lastName = lastName + " " + listName[i]
  228. }
  229. }
  230. }
  231. var user: User?
  232. if fmdb != nil {
  233. 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() {
  234. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  235. firstName: cursor.string(forColumnIndex: 1) ?? "",
  236. lastName: cursor.string(forColumnIndex: 2) ?? "",
  237. thumb: cursor.string(forColumnIndex: 3) ?? "",
  238. userType: cursor.string(forColumnIndex: 4) ?? "",
  239. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  240. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  241. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  242. official: cursor.string(forColumnIndex: 9) ?? "",
  243. device_id: cursor.string(forColumnIndex: 8) ?? "")
  244. cursor.close()
  245. }
  246. } else {
  247. Database.shared.database?.inTransaction({ fmdb, rollback in
  248. do {
  249. 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() {
  250. user = User(pin: cursor.string(forColumnIndex: 0) ?? "",
  251. firstName: cursor.string(forColumnIndex: 1) ?? "",
  252. lastName: cursor.string(forColumnIndex: 2) ?? "",
  253. thumb: cursor.string(forColumnIndex: 3) ?? "",
  254. userType: cursor.string(forColumnIndex: 4) ?? "",
  255. privacy_flag: cursor.string(forColumnIndex: 5) ?? "",
  256. offline_mode: cursor.string(forColumnIndex: 6) ?? "",
  257. ex_block: cursor.string(forColumnIndex: 7) ?? "",
  258. official: cursor.string(forColumnIndex: 9) ?? "",
  259. device_id: cursor.string(forColumnIndex: 8) ?? "")
  260. cursor.close()
  261. }
  262. } catch {
  263. rollback.pointee = true
  264. print("Access database error: \(error.localizedDescription)")
  265. }
  266. })
  267. }
  268. return user
  269. }
  270. }