Browse Source

Fixed json generation to work with campus mobile app

keplyx 1 year ago
parent
commit
d9e799a538
3 changed files with 28 additions and 2 deletions
  1. 0
    1
      assets/js/index.js
  2. 13
    0
      classes/dao.php
  3. 15
    1
      classes/postHandler.php

+ 0
- 1
assets/js/index.js View File

@@ -20,7 +20,6 @@ $('#uploadButton').on('click', function () {
20 20
                 btnClass: "btn-warning",
21 21
                 action: async function () {
22 22
                     let result = await sendRequest();
23
-                    console.log(result);
24 23
                     if (result !== 0) {
25 24
                         $.alert({
26 25
                             title: "Erreur",

+ 13
- 0
classes/dao.php View File

@@ -35,6 +35,19 @@ class Dao
35 35
         return $cursor->fetchAll(PDO::FETCH_ASSOC);
36 36
     }
37 37
 
38
+    public function get_categories_of_article($articleid)
39
+    {
40
+        $sql = 'SELECT category_id FROM article_categories WHERE article_id=?';
41
+        $cursor = $this->conn->prepare($sql);
42
+        $cursor->execute([$articleid]);
43
+        $result = $cursor->fetchAll(PDO::FETCH_ASSOC);
44
+        $final = [];
45
+        foreach ($result as $row) {
46
+            array_push($final, $row["category_id"]);
47
+        }
48
+        return $final;
49
+    }
50
+
38 51
     public function get_article_categories()
39 52
     {
40 53
         $sql = 'SELECT * FROM article_categories';

+ 15
- 1
classes/postHandler.php View File

@@ -14,6 +14,7 @@ class PostHandler
14 14
     private $dao;
15 15
     private $uploadBaseDir = '../uploaded_images/';
16 16
     private $stockFile = "../data/stock-v2.json";
17
+    private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
17 18
 
18 19
     private $responseArray = array(
19 20
         "status" => 0,
@@ -66,7 +67,7 @@ class PostHandler
66 67
         $fp = fopen($this->stockFile, "w");
67 68
         $array = array(
68 69
             "types" => $this->dao->get_categories(),
69
-            "articles" => $this->dao->get_articles(),
70
+            "articles" => $this->get_articles_json_list(),
70 71
         );
71 72
         fwrite($fp, json_encode($array));
72 73
         fclose($fp);
@@ -75,6 +76,19 @@ class PostHandler
75 76
         return $this->responseArray;
76 77
     }
77 78
 
79
+    public function get_articles_json_list()
80
+    {
81
+        $articles = $this->dao->get_articles();
82
+        $formatted_articles = [];
83
+        foreach ($articles as $article) {
84
+            $article["type"] = $this->dao->get_categories_of_article($article["id"]);
85
+            $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
86
+            array_push($formatted_articles, $article);
87
+        }
88
+
89
+        return $formatted_articles;
90
+    }
91
+
78 92
     private function save_image()
79 93
     {
80 94
         $success = true;

Loading…
Cancel
Save