User.swift 14 KB

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