Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventCategory
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 17
506
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 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getByEventId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getByCategoryId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 updatePriority
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 syncCategories
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getEventId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setEventId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getCategoryId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCategoryId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getPriority
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPriority
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getNotes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setNotes
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\Backstock;
4
5class EventCategory extends Backstock
6{
7    public $id;
8    public $eventId;
9    public $categoryId;
10    public $priority;
11    public $notes;
12
13    public function __construct(\Store $store)
14    {
15        parent::__construct($store);
16    }
17
18    /**
19     * Create a new event-category link
20     * @return $this
21     */
22    public function create()
23    {
24        $stmt = $this->storeDB->prepare("
25            INSERT INTO bsEvent_Categories (eventId, categoryId, priority, notes)
26            VALUES (:eventId, :categoryId, :priority, :notes)
27        ");
28
29        $stmt->bindValue(":eventId", $this->eventId, \PDO::PARAM_INT);
30        $stmt->bindValue(":categoryId", $this->categoryId, \PDO::PARAM_INT);
31        $stmt->bindValue(":priority", $this->priority ?? 5, \PDO::PARAM_INT);
32        $stmt->bindValue(":notes", $this->notes, $this->notes === null ? \PDO::PARAM_NULL : \PDO::PARAM_STR);
33
34        $stmt->execute();
35        $this->id = $this->storeDB->lastInsertId();
36        return $this;
37    }
38
39    /**
40     * Delete event-category link
41     * @return $this
42     */
43    public function delete()
44    {
45        $stmt = $this->storeDB->prepare("DELETE FROM bsEvent_Categories WHERE id = :id");
46        $stmt->bindValue(":id", $this->id, \PDO::PARAM_INT);
47        $stmt->execute();
48        return $this;
49    }
50
51    /**
52     * Get all categories linked to an event
53     * @param int $eventId
54     * @return array
55     */
56    public function getByEventId($eventId)
57    {
58        $stmt = $this->storeDB->prepare("
59            SELECT ec.id, ec.eventId, ec.categoryId, ec.priority, ec.notes,
60                   c.name as category_name, c.color as category_color
61            FROM bsEvent_Categories ec
62            JOIN bsCategories c ON ec.categoryId = c.id
63            WHERE ec.eventId = :eventId
64            ORDER BY ec.priority DESC, c.name ASC
65        ");
66        $stmt->bindValue(":eventId", $eventId, \PDO::PARAM_INT);
67        $stmt->execute();
68        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
69    }
70
71    /**
72     * Get all events linked to a category
73     * @param int $categoryId
74     * @return array
75     */
76    public function getByCategoryId($categoryId)
77    {
78        $stmt = $this->storeDB->prepare("
79            SELECT ec.id, ec.eventId, ec.categoryId, ec.priority, ec.notes,
80                   e.name as event_name, e.start_date, e.end_date, e.event_type
81            FROM bsEvent_Categories ec
82            JOIN bsEvents e ON ec.eventId = e.id
83            WHERE ec.categoryId = :categoryId
84            ORDER BY e.start_date DESC
85        ");
86        $stmt->bindValue(":categoryId", $categoryId, \PDO::PARAM_INT);
87        $stmt->execute();
88        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
89    }
90
91    /**
92     * Update priority for this link
93     * @param int $priority
94     * @return $this
95     */
96    public function updatePriority($priority)
97    {
98        $stmt = $this->storeDB->prepare("
99            UPDATE bsEvent_Categories
100            SET priority = :priority
101            WHERE id = :id
102        ");
103        $stmt->bindValue(":priority", $priority, \PDO::PARAM_INT);
104        $stmt->bindValue(":id", $this->id, \PDO::PARAM_INT);
105        $stmt->execute();
106
107        $this->priority = $priority;
108        return $this;
109    }
110
111    /**
112     * Sync categories for an event (replace all existing links)
113     * @param int $eventId
114     * @param array $categoryIds Array of category IDs
115     * @param array $priorities Optional associative array [categoryId => priority]
116     * @return bool
117     */
118    public function syncCategories($eventId, $categoryIds, $priorities = [])
119    {
120        // Start transaction
121        $this->storeDB->beginTransaction();
122
123        try {
124            // Delete all existing links for this event
125            $deleteStmt = $this->storeDB->prepare("DELETE FROM bsEvent_Categories WHERE eventId = :eventId");
126            $deleteStmt->bindValue(":eventId", $eventId, \PDO::PARAM_INT);
127            $deleteStmt->execute();
128
129            // Insert new links
130            if (!empty($categoryIds)) {
131                $insertStmt = $this->storeDB->prepare("
132                    INSERT INTO bsEvent_Categories (eventId, categoryId, priority)
133                    VALUES (:eventId, :categoryId, :priority)
134                ");
135
136                foreach ($categoryIds as $categoryId) {
137                    $priority = isset($priorities[$categoryId]) ? $priorities[$categoryId] : 5;
138
139                    $insertStmt->bindValue(":eventId", $eventId, \PDO::PARAM_INT);
140                    $insertStmt->bindValue(":categoryId", $categoryId, \PDO::PARAM_INT);
141                    $insertStmt->bindValue(":priority", $priority, \PDO::PARAM_INT);
142                    $insertStmt->execute();
143                }
144            }
145
146            $this->storeDB->commit();
147            return true;
148        } catch (\Exception $e) {
149            $this->storeDB->rollBack();
150            $this->log->logError("EventCategory::syncCategories failed: " . $e->getMessage());
151            return false;
152        }
153    }
154
155    /**
156     * @return int
157     */
158    public function getId()
159    {
160        return $this->id;
161    }
162
163    /**
164     * @param int $id
165     * @return $this
166     */
167    public function setId($id)
168    {
169        $this->id = $id;
170        return $this;
171    }
172
173    /**
174     * @return int
175     */
176    public function getEventId()
177    {
178        return $this->eventId;
179    }
180
181    /**
182     * @param int $eventId
183     * @return $this
184     */
185    public function setEventId($eventId)
186    {
187        $this->eventId = $eventId;
188        return $this;
189    }
190
191    /**
192     * @return int
193     */
194    public function getCategoryId()
195    {
196        return $this->categoryId;
197    }
198
199    /**
200     * @param int $categoryId
201     * @return $this
202     */
203    public function setCategoryId($categoryId)
204    {
205        $this->categoryId = $categoryId;
206        return $this;
207    }
208
209    /**
210     * @return int
211     */
212    public function getPriority()
213    {
214        return $this->priority;
215    }
216
217    /**
218     * @param int $priority
219     * @return $this
220     */
221    public function setPriority($priority)
222    {
223        $this->priority = $priority;
224        return $this;
225    }
226
227    /**
228     * @return string|null
229     */
230    public function getNotes()
231    {
232        return $this->notes;
233    }
234
235    /**
236     * @param string|null $notes
237     * @return $this
238     */
239    public function setNotes($notes)
240    {
241        $this->notes = $notes;
242        return $this;
243    }
244}