Skip to content

Commit 6622771

Browse files
committed
Refactor allocation of objects and published check
1 parent 5cbc741 commit 6622771

File tree

2 files changed

+25
-57
lines changed

2 files changed

+25
-57
lines changed

src/controllers/User/View.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ protected function getUserInfo(UserViewModel &$model)
8282
);
8383

8484
// Process documents
85-
if ($model->documents) {
85+
if ($model->documents)
86+
{
8687
// Alphabetically sort the documents
8788
usort($model->documents, function($a, $b){
8889
$a1 = $a->getTitle();
@@ -93,17 +94,19 @@ protected function getUserInfo(UserViewModel &$model)
9394

9495
// Remove documents that are not published
9596
$i = count($model->documents) - 1;
96-
while ($i >= 0) {
97-
if (!($model->documents[$i]->getOptionsBitmask()
98-
& Document::OPTION_PUBLISHED)) {
97+
while ($i >= 0)
98+
{
99+
if (!($model->documents[$i]->isPublished()))
100+
{
99101
unset($model->documents[$i]);
100102
}
101103
--$i;
102104
}
103105
}
104106

105107
// Process news posts
106-
if ($model->news_posts) {
108+
if ($model->news_posts)
109+
{
107110
// Alphabetically sort the news posts
108111
usort($model->news_posts, function($a, $b){
109112
$a1 = $a->getTitle();
@@ -114,17 +117,19 @@ protected function getUserInfo(UserViewModel &$model)
114117

115118
// Remove news posts that are not published
116119
$i = count($model->news_posts) - 1;
117-
while ($i >= 0) {
118-
if (!($model->news_posts[$i]->getOptionsBitmask()
119-
& NewsPost::OPTION_PUBLISHED)) {
120+
while ($i >= 0)
121+
{
122+
if (!($model->news_posts[$i]->isPublished()))
123+
{
120124
unset($model->news_posts[$i]);
121125
}
122126
--$i;
123127
}
124128
}
125129

126130
// Process packets
127-
if ($model->packets) {
131+
if ($model->packets)
132+
{
128133
// Alphabetically sort the packets
129134
usort($model->packets, function($a, $b){
130135
$a1 = $a->getName();
@@ -135,9 +140,10 @@ protected function getUserInfo(UserViewModel &$model)
135140

136141
// Remove packets that are not published
137142
$i = count($model->packets) - 1;
138-
while ($i >= 0) {
139-
if (!($model->packets[$i]->getOptionsBitmask()
140-
& Packet::OPTION_PUBLISHED)) {
143+
while ($i >= 0)
144+
{
145+
if (!($model->packets[$i]->isPublished()))
146+
{
141147
unset($model->packets[$i]);
142148
}
143149
--$i;

src/libraries/Packet.php

+7-45
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ protected function allocateObject(StdClass $value)
201201
$this->setRemarks($value->packet_remarks);
202202
$this->setTransportLayerId($value->packet_transport_layer_id);
203203
$this->setUserId($value->user_id);
204-
if (!isset($value->used_by)) throw new \RuntimeException();
205204
$this->setUsedBy($value->used_by);
206205
}
207206

@@ -342,31 +341,14 @@ public static function &getAllPackets(?string $where_clause = null, ?array $orde
342341
Common::$database = DatabaseDriver::getDatabaseObject();
343342
}
344343

345-
$q = Common::$database->prepare(sprintf('
346-
SELECT
347-
`created_datetime`,
348-
`edited_count`,
349-
`edited_datetime`,
350-
`id`,
351-
`options_bitmask`,
352-
`packet_application_layer_id`,
353-
`packet_direction_id`,
354-
`packet_format`,
355-
`packet_id`,
356-
`packet_name`,
357-
`packet_remarks`,
358-
`packet_transport_layer_id`,
359-
`user_id`
360-
FROM `packets` %s ORDER BY %s;', $where_clause, $order_clause
361-
));
362-
344+
$q = Common::$database->prepare(sprintf('SELECT `id` FROM `packets` %s ORDER BY %s;', $where_clause, $order_clause));
363345
$r = $q->execute();
364346
if (!$r) return $r;
365347

366348
$r = [];
367-
while ($row = $q->fetch(PDO::FETCH_OBJ))
349+
while ($row = $q->fetch(PDO::FETCH_NUM))
368350
{
369-
$r[] = new self($row);
351+
$r[] = new self($row[0]);
370352
}
371353

372354
$q->closeCursor();
@@ -383,7 +365,6 @@ public static function getPacketsByLastEdited(int $count)
383365
$q = Common::$database->prepare(sprintf(
384366
'SELECT `id` FROM `packets` ORDER BY IFNULL(`edited_datetime`, `created_datetime`) DESC LIMIT %d;', $count
385367
));
386-
387368
$r = $q->execute();
388369
if (!$r) return $r;
389370

@@ -533,34 +514,15 @@ public static function getPacketsByUserId(int $user_id)
533514
Common::$database = DatabaseDriver::getDatabaseObject();
534515
}
535516

536-
$q = Common::$database->prepare('
537-
SELECT
538-
`created_datetime`,
539-
`edited_count`,
540-
`edited_datetime`,
541-
`id`,
542-
`options_bitmask`,
543-
`packet_application_layer_id`,
544-
`packet_direction_id`,
545-
`packet_format`,
546-
`packet_id`,
547-
`packet_name`,
548-
`packet_remarks`,
549-
`packet_transport_layer_id`,
550-
`user_id`
551-
FROM `packets`
552-
WHERE `user_id` = :user_id
553-
ORDER BY `id` ASC;
554-
');
555-
$q->bindParam(':user_id', $user_id, PDO::PARAM_INT);
556-
517+
$q = Common::$database->prepare('SELECT `id` FROM `packets` WHERE `user_id` = :id ORDER BY `id` ASC;');
518+
$q->bindParam(':id', $user_id, PDO::PARAM_INT);
557519
$r = $q->execute();
558520
if (!$r) return $r;
559521

560522
$r = [];
561-
while ($row = $q->fetch(PDO::FETCH_OBJ))
523+
while ($row = $q->fetch(PDO::FETCH_NUM))
562524
{
563-
$r[] = new self( $row );
525+
$r[] = new self($row[0]);
564526
}
565527

566528
$q->closeCursor();

0 commit comments

Comments
 (0)