Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 105
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Post
0.00% covered (danger)
0.00%
0 / 105
0.00% covered (danger)
0.00%
0 / 7
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 get
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
20
 update
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 updateAvailability
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getContactsArrayForNotification
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 sendOneSignalNotification
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\ResalePerks;
4
5class Post extends Base {
6
7
8    public $id;
9    public $imagePath;
10    public $typeNum;
11    public $comment;
12    public $nTitle;
13    public $nSubtext;
14    public $notifyType;
15    public $dateAdded;
16    public $availability;
17    public $price;
18
19    public function __construct()
20    {
21        parent::__construct();
22    }
23
24    public function create() {
25        try {
26            $stmt = $this->db->prepare("INSERT INTO rpPosts (typeNum, imagePath, comment, nTitle, nSubtext, notifyType, availability, price, dateAdded)
27                                                              VALUES (:typeNum,:imagePath,:comment,:nTitle,:nSubtext,:notifyType,:availability,:price, CURRENT_TIMESTAMP )");
28            $stmt->bindValue(":typeNum", $this->typeNum);
29            $stmt->bindValue(":imagePath", $this->imagePath);
30            $stmt->bindValue(":comment", $this->comment);
31            $stmt->bindValue(":nTitle", $this->nTitle);
32            $stmt->bindValue(":nSubtext", $this->nSubtext);
33            $stmt->bindValue(":notifyType", $this->notifyType);
34            $stmt->bindValue(":availability", $this->availability);
35            $stmt->bindValue(":price", $this->price);
36
37            if($stmt->execute()) {
38                $this->id = $this->db->lastInsertId();
39                return true;
40            } else {
41                $this->log->LogDebug("Failed Create");
42            }
43        } catch (\PDOException $e) {
44            $this->log->LogDebug($e->getMessage());
45        }
46        return false;
47    }
48
49    public function get($id) {
50        try {
51             $stmt = $this->db->prepare("SELECT * FROM rpPosts WHERE id = :id");
52             $stmt->bindValue(":id", $id);
53             if($stmt->execute()) {
54                 $row = $stmt->fetch(\PDO::FETCH_ASSOC);
55                 if(isset($row['typeNum'])) {
56                     $this->typeNum = $row['typeNum'];
57                     $this->imagePath = $row['imagePath'];
58                     $this->id = $row['id'];
59                     $this->comment = $row['comment'];
60                     $this->nTitle = $row['nTitle'];
61                     $this->nSubtext = $row['nSubtext'];
62                     $this->notifyType = $row['notifyType'];
63                     $this->dateAdded = $row['dateAdded'];
64                     $this->price = $row['price'];
65                     $this->availability = $row['availability'];
66                     return true;
67                 }
68                 $this->log->LogDebug("Failed TypeNum Check");
69             }
70            $this->log->LogDebug("Failed Get");
71        } catch (\PDOException $e) {
72            $this->log->LogDebug($e->getMessage());
73        }
74        return false;
75    }
76    public function update() {
77        try {
78            $stmt = $this->db->prepare("UPDATE rpPosts SET typeNum = :typeNum. imagePath=:imagePath, comment=:comment,nTitle=:nTitle, nSubtext = :nSubtext, notifyType = :notifyType, availability = :availability, price = :price WHERE id = :id");
79            $stmt->bindValue(":id", $this->id);
80            $stmt->bindValue(":typeNum", $this->typeNum);
81            $stmt->bindValue(":imagePath", $this->imagePath);
82            $stmt->bindValue(":comment", $this->comment);
83            $stmt->bindValue(":nTitle", $this->nTitle);
84            $stmt->bindValue(":nSubtext", $this->nSubtext);
85            $stmt->bindValue(":notifyType", $this->notifyType);
86            $stmt->bindValue(":availability", $this->availability);
87            $stmt->bindValue(":price", $this->price);
88            if($stmt->execute()) {
89                return true;
90            } else {
91                $this->log->LogDebug("Failed Update");
92            }
93        } catch (\PDOException $e) {
94            $this->log->LogDebug($e->getMessage());
95        }
96        return false;
97    }
98    public function updateAvailability() {
99        try {
100            $stmt = $this->db->prepare("UPDATE rpPosts SET availability = :availability WHERE id = :id");
101            $stmt->bindValue(":availability", $this->availability);
102            $stmt->bindValue(":id", $this->id);
103            if($stmt->execute()) {
104                return true;
105            } else {
106                $this->log->LogDebug("Failed Update Availability");
107            }
108        } catch (\PDOException $e) {
109            $this->log->LogDebug($e->getMessage());
110        }
111        return false;
112    }
113
114    public function getContactsArrayForNotification() {
115        $outputArray = [];
116        try {
117            $stmt = $this->db->prepare("SELECT oneSignalID FROM rpContacts JOIN rpSubscriptions ON rpContacts.id = rpSubscriptions.contact_id WHERE rpSubscriptions.active = 1 AND rpSubscriptions.typeNum = :typeNum AND oneSignalID IS NOT NULL");
118            $stmt->bindValue(":typeNum", $this->typeNum);
119            $stmt->execute();
120            while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
121                $this->log->LogDebug("One Signal ID: ".$row['oneSignalID']);
122                $outputArray[] = $row['oneSignalID'];
123            }
124            return $outputArray;
125
126        } catch (\PDOException $e) {
127
128        }
129        return null;
130    }
131
132    public function sendOneSignalNotification($contacts) {
133        $content      = array(
134            "en" => $this->nSubtext
135        );
136        $heading = array(
137            'en' => $this->nTitle
138        );
139        $fields = array(
140            'app_id' => "6466bfaf-7cca-4211-8c5c-1818d1892e59",
141            'include_player_ids' => $contacts,
142            'data' => array("PostID" => $this->id, "TypeNum" => $this->typeNum),
143            'contents' => $content,
144            "headings" => $heading
145        );
146        $fields = json_encode($fields);
147        print("\nJSON sent:\n");
148        print($fields);
149
150        $ch = curl_init();
151        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
152        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
153            'Authorization: Basic M2Q0ZWEyN2UtOTUyNi00MjhmLTk5NmMtOTNiOGNiYjViNDgz'));
154        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
155        curl_setopt($ch, CURLOPT_HEADER, FALSE);
156        curl_setopt($ch, CURLOPT_POST, TRUE);
157        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
158        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
159
160        $response = curl_exec($ch);
161        curl_close($ch);
162
163
164        $return["allresponses"] = $response;
165        $return = json_encode( $return);
166
167        $this->log->LogDebug($response);
168        return true;
169    }
170
171}