json.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * Classe json
  4. *
  5. * Cette classe étend la classe db et fournit des méthodes pour gérer la création, la suppression et la manipulation de fichiers JSON.
  6. */
  7. class json extends db
  8. {
  9. /**
  10. * Crée un fichier JSON en fonction de la cible spécifiée.
  11. *
  12. * @param string $_target La cible pour laquelle le fichier JSON doit être créé.
  13. * @return int|mixed Résultat de la création du fichier JSON.
  14. */
  15. public static function create(string $_target)
  16. {
  17. if (isset($_target)) {
  18. if (preg_match('/^banque-lignes-(\d+)$/', $_target, $matches)) {
  19. return self::create_banque_lignes((int)$matches[1]);
  20. }
  21. switch ($_target) {
  22. case "salaries":
  23. return self::create_salaries();
  24. break;
  25. case "excel":
  26. return self::create_excel();
  27. break;
  28. case "excel-proweb":
  29. return self::create_excelProweb();
  30. break;
  31. case "excel-proweb-dossiers":
  32. return self::create_excelProwebDossiers();
  33. break;
  34. case "events":
  35. return self::create_events();
  36. break;
  37. case "users":
  38. return self::create_users();
  39. break;
  40. case "salaries-proweb":
  41. return self::create_salariesProweb();
  42. break;
  43. case "dossiers-proweb":
  44. return self::create_dossiersProweb();
  45. break;
  46. case "lotterys":
  47. return self::create_lotterys();
  48. break;
  49. case "banque-csv":
  50. return self::create_banque_csv();
  51. break;
  52. case "documents":
  53. return self::create_document();
  54. break;
  55. case "documents-limited":
  56. return self::create_document("limited");
  57. break;
  58. }
  59. } else {
  60. return 0;
  61. }
  62. }
  63. /**
  64. * Crée des fichiers JSON pour toutes les banques disponibles.
  65. */
  66. public static function createBanks()
  67. {
  68. foreach (banque::getAll() as $compte) {
  69. self::create_banque_lignes($compte["id"]);
  70. }
  71. }
  72. /**
  73. * Crée un fichier JSON pour les dossiers Proweb.
  74. *
  75. * @return int Succès ou échec de la création du fichier.
  76. */
  77. private static function create_dossiersProweb()
  78. {
  79. $row = prowebDossiers::all();
  80. if (file_put_contents(DIR_DATAS_JSON . "dossiers-proweb.json", json_encode($row))) {
  81. return 1;
  82. } else {
  83. return 0;
  84. }
  85. }
  86. /**
  87. * Crée un fichier JSON pour les salariés Proweb.
  88. *
  89. * @return int Succès ou échec de la création du fichier.
  90. */
  91. private static function create_salariesProweb()
  92. {
  93. $row = proweb::getBase();
  94. if (file_put_contents(DIR_DATAS_JSON . "salaries-proweb.json", json_encode($row))) {
  95. return 1;
  96. } else {
  97. return 0;
  98. }
  99. }
  100. /**
  101. * Crée un fichier JSON pour les salariés.
  102. *
  103. * @return int Succès ou échec de la création du fichier.
  104. */
  105. private static function create_salaries()
  106. {
  107. $row = salaries::getSalaries();
  108. if (file_put_contents(DIR_DATAS_JSON . "salaries.json", json_encode($row))) {
  109. return 1;
  110. } else {
  111. return 0;
  112. }
  113. }
  114. /**
  115. * Crée un fichier JSON pour les données Excel.
  116. *
  117. * @return int Succès ou échec de la création du fichier.
  118. */
  119. private static function create_excel()
  120. {
  121. db::query("SELECT
  122. " . DB_T_EXCEL . ".id,
  123. " . DB_T_FILES . ".name,
  124. " . DB_T_EXCEL . ".nbSalaries,
  125. CONCAT(ROUND((" . DB_T_FILES . ".size / 1024 / 1024), 2), ' Mo') AS size,
  126. " . DB_T_EXCEL . ".md5,
  127. " . DB_T_EXCEL . ".cree,
  128. CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user',
  129. dateData,
  130. goMysql,
  131. md5forSFTP,
  132. log
  133. FROM " . DB_T_EXCEL . "
  134. INNER JOIN " . DB_T_USER . " ON " . DB_T_EXCEL . ".id_user = " . DB_T_USER . ".id
  135. LEFT JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL . ".md5 = " . DB_T_FILES . ".id");
  136. $row = db::resultset();
  137. if (file_put_contents(DIR_DATAS_JSON . "excel.json", json_encode($row))) {
  138. return 1;
  139. } else {
  140. return 0;
  141. }
  142. }
  143. /**
  144. * Crée un fichier JSON pour les données Excel Proweb.
  145. *
  146. * @return int Succès ou échec de la création du fichier.
  147. */
  148. private static function create_excelProweb()
  149. {
  150. db::query("SELECT
  151. " . DB_T_EXCEL_PROWEB . ".id,
  152. " . DB_T_FILES . ".name,
  153. " . DB_T_EXCEL_PROWEB . ".nbSalaries,
  154. CONCAT(ROUND((" . DB_T_FILES . ".size / 1024 / 1024), 2), ' Mo') AS size,
  155. " . DB_T_EXCEL_PROWEB . ".md5,
  156. " . DB_T_EXCEL_PROWEB . ".cree,
  157. CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user',
  158. dateData
  159. FROM " . DB_T_EXCEL_PROWEB . "
  160. INNER JOIN " . DB_T_USER . " ON " . DB_T_EXCEL_PROWEB . ".id_user = " . DB_T_USER . ".id
  161. LEFT JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL_PROWEB . ".md5 = " . DB_T_FILES . ".id");
  162. $row = db::resultset();
  163. if (file_put_contents(DIR_DATAS_JSON . "excel-proweb.json", json_encode($row))) {
  164. return 1;
  165. } else {
  166. return 0;
  167. }
  168. }
  169. /**
  170. * Crée un fichier JSON pour les dossiers Excel Proweb.
  171. *
  172. * @return int Succès ou échec de la création du fichier.
  173. */
  174. private static function create_excelProwebDossiers(){
  175. db::query("SELECT
  176. " . DB_T_EXCEL_PROWEB_DOSSIERS . ".id,
  177. " . DB_T_FILES . ".name,
  178. " . DB_T_EXCEL_PROWEB_DOSSIERS . ".nbDossiers,
  179. CONCAT(ROUND((" . DB_T_FILES . ".size / 1024 / 1024), 2), ' Mo') AS size,
  180. " . DB_T_EXCEL_PROWEB_DOSSIERS . ".md5,
  181. " . DB_T_EXCEL_PROWEB_DOSSIERS . ".cree,
  182. CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user',
  183. dateData
  184. FROM " . DB_T_EXCEL_PROWEB_DOSSIERS . "
  185. INNER JOIN " . DB_T_USER . " ON " . DB_T_EXCEL_PROWEB_DOSSIERS . ".id_user = " . DB_T_USER . ".id
  186. LEFT JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL_PROWEB_DOSSIERS . ".md5 = " . DB_T_FILES . ".id");
  187. $row = db::resultset();
  188. if (file_put_contents(DIR_DATAS_JSON . "excel-proweb-dossiers.json", json_encode($row))) {
  189. return 1;
  190. } else {
  191. return 0;
  192. }
  193. }
  194. /**
  195. * Crée un fichier JSON pour les événements.
  196. *
  197. * @return int Succès ou échec de la création du fichier.
  198. */
  199. private static function create_events()
  200. {
  201. $row = event::getEvents();
  202. if (file_put_contents(DIR_DATAS_JSON . "events.json", json_encode($row))) {
  203. return 1;
  204. } else {
  205. return 0;
  206. }
  207. }
  208. /**
  209. * Crée un fichier JSON pour les loteries.
  210. *
  211. * @return int Succès ou échec de la création du fichier.
  212. */
  213. private static function create_lotterys()
  214. {
  215. $row = lottery::getLotterys();
  216. if (file_put_contents(DIR_DATAS_JSON . "lotterys.json", json_encode($row))) {
  217. return 1;
  218. } else {
  219. return 0;
  220. }
  221. }
  222. /**
  223. * Crée un fichier JSON pour les utilisateurs.
  224. *
  225. * @return int Succès ou échec de la création du fichier.
  226. */
  227. private static function create_users()
  228. {
  229. $row = user::getUsers();
  230. if (file_put_contents(DIR_DATAS_JSON . "users.json", json_encode($row))) {
  231. return 1;
  232. } else {
  233. return 0;
  234. }
  235. }
  236. /**
  237. * Crée un fichier JSON pour l'historique des banques au format CSV.
  238. *
  239. * @return int Succès ou échec de la création du fichier.
  240. */
  241. private static function create_banque_csv()
  242. {
  243. $row = banque::getHistoriqueCSV();
  244. if (file_put_contents(DIR_DATAS_JSON . "banque-csv.json", json_encode($row))) {
  245. return 1;
  246. } else {
  247. return 0;
  248. }
  249. }
  250. /**
  251. * Crée un fichier JSON pour les lignes d'une banque spécifique.
  252. *
  253. * @param int $_id L'identifiant de la banque.
  254. * @return int Succès ou échec de la création du fichier.
  255. */
  256. private static function create_banque_lignes(int $_id)
  257. {
  258. $row = array(banque::getInitial($_id));
  259. $row2 = banque::getLignes($_id, $row[0]["solde"]);
  260. $return = array_merge($row, $row2);
  261. if (file_put_contents(DIR_DATAS_JSON . "banque-lignes-" . $_id . ".json", json_encode($return))) {
  262. return 1;
  263. } else {
  264. return 0;
  265. }
  266. }
  267. /**
  268. * Supprime un fichier JSON spécifié.
  269. *
  270. * @param string $_target Le nom du fichier JSON à supprimer.
  271. */
  272. public static function delete(string $_target)
  273. {
  274. if (is_file(DIR_DATAS_JSON . "/" . $_target . ".json")) {
  275. unlink(DIR_DATAS_JSON . "/" . $_target . ".json");
  276. }
  277. }
  278. /**
  279. * Teste l'accès SFTP.
  280. *
  281. * @return string "OK" si l'accès est réussi, "KO" sinon.
  282. */
  283. public static function testSFTP()
  284. {
  285. return (sftp::testAccessHost()) ? "OK" : "KO";
  286. }
  287. /**
  288. * Crée un fichier JSON pour les documents.
  289. *
  290. * @param string|null $_limited Indique si seuls les documents limités doivent être inclus.
  291. * @return int Succès ou échec de la création du fichier.
  292. */
  293. private static function create_document(?string $_limited = NULL)
  294. {
  295. $where = ($_limited == TRUE) ? " WHERE " . DB_T_DOCUMENTS . ".id_type != 3" : NULL;
  296. if(is_null($_limited)){
  297. $where = NULL;
  298. $file = "documents";
  299. } else {
  300. $where = " WHERE " . DB_T_DOCUMENTS . ".id_type != 3";
  301. $file = "documents-limited";
  302. }
  303. db::query("SELECT
  304. " . DB_T_DOCUMENTS . ".id,
  305. " . DB_T_DOCUMENTS . ".titre,
  306. " . DB_T_DOCUMENTS . ".date,
  307. " . DB_T_DOCUMENTS . ".deadline,
  308. " . DB_T_DOCUMENTS . ".description,
  309. " . DB_T_CLIENTS . ".label AS attach,
  310. " . DB_T_DOCUMENTS . ".montant,
  311. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  312. FROM " . DB_T_DOCUMENT_TAGS . "
  313. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  314. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 2
  315. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS tags,
  316. ( SELECT GROUP_CONCAT(" . DB_T_TAGS . ".label SEPARATOR ', ')
  317. FROM " . DB_T_DOCUMENT_TAGS . "
  318. INNER JOIN " . DB_T_TAGS . " ON " . DB_T_TAGS . ".id = " . DB_T_DOCUMENT_TAGS . ".id_tags
  319. WHERE id_documents = " . DB_T_DOCUMENTS . ".id AND " . DB_T_DOCUMENT_TAGS . ".id_type_tags = 1
  320. ORDER BY " . DB_T_DOCUMENT_TAGS . ".creer) AS assign,
  321. IF(" . DB_T_DOCUMENTS . ".id_user_done IS NOT NULL, 'Traité', 'Non traité') AS done,
  322. " . DB_T_TYPE_DOCUMENT . ".label
  323. FROM " . DB_T_DOCUMENTS . "
  324. LEFT JOIN " . DB_T_CLIENTS . " ON " . DB_T_CLIENTS . ".id = " . DB_T_DOCUMENTS . ".id_client
  325. INNER JOIN " . DB_T_TYPE_DOCUMENT . " ON " . DB_T_TYPE_DOCUMENT . ".id = " . DB_T_DOCUMENTS . ".id_type" . $where);
  326. $row = db::resultset();
  327. if (file_put_contents(DIR_DATAS_JSON . $file . ".json", json_encode($row))) {
  328. return 1;
  329. } else {
  330. return 0;
  331. }
  332. }
  333. }