Skip to content

Commit fb1de7d

Browse files
authored
Merge pull request #88 from Davnit/packet-edit-usedby
Add editor for the list of products a packet is used by
2 parents e3e0015 + c8a8dc0 commit fb1de7d

File tree

6 files changed

+89
-7
lines changed

6 files changed

+89
-7
lines changed

src/controllers/Packet/Edit.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use \BNETDocs\Libraries\Exceptions\PacketNotFoundException;
99
use \BNETDocs\Libraries\Logger;
1010
use \BNETDocs\Libraries\Packet;
11+
use \BNETDocs\Libraries\Product;
1112
use \BNETDocs\Libraries\User;
1213
use \BNETDocs\Models\Packet\Edit as PacketEditModel;
1314

@@ -38,6 +39,7 @@ public function &run(Router &$router, View &$view, array &$args) {
3839
$model->packet_id = (isset($data["id"]) ? $data["id"] : null);
3940
$model->published = null;
4041
$model->remarks = null;
42+
$model->used_by = null;
4143
$model->user = Authentication::$user;
4244

4345
$model->acl_allowed = ($model->user && $model->user->getAcl(
@@ -59,6 +61,7 @@ public function &run(Router &$router, View &$view, array &$args) {
5961
$model->remarks = $model->packet->getPacketRemarks(false);
6062
$model->markdown = ($flags & Packet::OPTION_MARKDOWN);
6163
$model->published = ($flags & Packet::OPTION_PUBLISHED);
64+
$model->used_by = $this->getUsedBy($model->packet);
6265

6366
if ($router->getRequestMethod() == "POST") {
6467
$this->handlePost($router, $model);
@@ -96,6 +99,7 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
9699
$content = (isset($data["content" ]) ? $data["content" ] : null);
97100
$publish = (isset($data["publish" ]) ? $data["publish" ] : null);
98101
$save = (isset($data["save" ]) ? $data["save" ] : null);
102+
$used_by = (isset($data["used_by" ]) ? $data["used_by" ] : null);
99103

100104
$model->id = $id;
101105
$model->name = $name;
@@ -138,6 +142,10 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
138142

139143
$success = $model->packet->update();
140144

145+
// Used-by is stored in a different table than packet data so it is
146+
// updated separately.
147+
$model->packet->setUsedBy($used_by);
148+
141149
} catch (QueryException $e) {
142150

143151
// SQL error occurred. We can show a friendly message to the user while
@@ -166,8 +174,14 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
166174
"name" => $model->packet->getPacketName(),
167175
"format" => $model->packet->getPacketFormat(),
168176
"remarks" => $model->packet->getPacketRemarks(false),
177+
"used_by" => $used_by
169178
])
170179
);
171180
}
172181

182+
protected function getUsedBy(Packet &$packet) {
183+
if (is_null($packet)) return null;
184+
return Product::getProductsFromIds($packet->getUsedBy());
185+
}
186+
173187
}

src/controllers/Packet/View.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ public function &run(Router &$router, ViewLib &$view, array &$args) {
4949

5050
protected function getUsedBy(Packet &$packet) {
5151
if (is_null($packet)) return null;
52-
$used_by = $packet->getUsedBy();
53-
$products = [];
54-
foreach ($used_by as $bnet_product_id) {
55-
$products[] = new Product($bnet_product_id);
56-
}
57-
return $products;
52+
return Product::getProductsFromIds($packet->getUsedBy());
5853
}
5954

6055
}

src/libraries/Packet.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,49 @@ public function setPublished( $value ) {
725725
$this->options_bitmask &= ~self::OPTION_PUBLISHED;
726726
}
727727
}
728+
729+
public function setUsedBy( $value ) {
730+
if (!isset(Common::$database)) {
731+
Common::$database = DatabaseDriver::getDatabaseObject();
732+
}
733+
try {
734+
Common::$database->beginTransaction();
735+
$stmt = Common::$database->prepare('
736+
DELETE FROM `packet_used_by`
737+
WHERE `id` = :id;
738+
');
739+
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
740+
if (!$stmt->execute()) {
741+
throw new QueryException('Cannot update packet used by');
742+
}
743+
if ($value !== null && count($value) > 0) {
744+
$insert = [];
745+
$placeholders = [];
746+
foreach ($value as $v) {
747+
array_push($insert, $this->id, (int)$v);
748+
$placeholders[] = '(?, ?)';
749+
}
750+
$stmt = Common::$database->prepare('
751+
INSERT INTO `packet_used_by`
752+
(`id`, `bnet_product_id`)
753+
VALUES
754+
' . implode(', ', $placeholders) . ';
755+
');
756+
if (!$stmt->execute($insert)) {
757+
Common::$database->rollBack();
758+
throw new QueryException('Cannot update packet used by');
759+
}
760+
}
761+
762+
Common::$database->commit();
763+
764+
$cache_key = 'bnetdocs-packetusedby-' . $this->id;
765+
Common::$cache->set($cache_key, serialize($value), self::CACHE_TTL);
766+
} catch (PDOException $e) {
767+
Common::$database->rollBack();
768+
throw new QueryException('Cannot update packet used by', $e);
769+
}
770+
}
728771

729772
public function update() {
730773
if (!isset(Common::$database)) {

src/libraries/Product.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function getAllProducts() {
6565
`bnls_product_id`,
6666
`label`,
6767
`sort`,
68-
`version_byte
68+
`version_byte`
6969
FROM `products`
7070
ORDER BY `sort` ASC;
7171
");
@@ -90,6 +90,16 @@ public static function getAllProducts() {
9090
return null;
9191
}
9292

93+
public static function getProductsFromIds($product_ids) {
94+
$products = [];
95+
if ($product_ids !== null) {
96+
foreach ($product_ids as $bnet_product_id) {
97+
$products[] = new self($bnet_product_id);
98+
}
99+
}
100+
return $products;
101+
}
102+
93103
public function getBnetProductId() {
94104
return $this->bnet_product_id;
95105
}

src/models/Packet/Edit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Edit extends Model {
1818
public $packet_id;
1919
public $published;
2020
public $remarks;
21+
public $used_by;
2122
public $user;
2223

2324
}

src/templates/Packet/Edit.phtml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace BNETDocs\Templates\Packet;
44

5+
use \BNETDocs\Libraries\Product;
6+
57
use \CarlBennett\MVC\Libraries\Common;
68
use \CarlBennett\MVC\Libraries\Pair;
79

@@ -82,6 +84,23 @@ require('./header.inc.phtml');
8284
filter_var( $this->getContext()->format, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
8385
?></textarea>
8486
</section>
87+
<section>
88+
<label for="usedby">Used by:</label>
89+
<table><?php
90+
$all_products = Product::getAllProducts();
91+
$used_by = $this->getContext()->used_by;
92+
$j = count($all_products);
93+
for ($i = 0; $i < $j; ++$i) { ?>
94+
<tr><td><input type="checkbox" name="used_by[]" value="<?php
95+
echo $all_products[$i]->getBnetProductId() . "\"" . ($used_by !== null && in_array($all_products[$i], $used_by) ? " checked> " : "> ") . filter_var($all_products[$i]->getLabel(), FILTER_SANITIZE_STRING) . "</td><td>";
96+
if ($i + 1 < $j) {
97+
++$i;
98+
?><input type="checkbox" name="used_by[]" value="<?php
99+
echo $all_products[$i]->getBnetProductId() . "\"" . ($used_by !== null && in_array($all_products[$i], $used_by) ? " checked> " : "> ") . filter_var($all_products[$i]->getLabel(), FILTER_SANITIZE_STRING); ?></td></tr>
100+
<?php }
101+
} ?>
102+
</table>
103+
</section>
85104
<section>
86105
<label for="remarks">Remarks:</label>
87106
<span style="float:right;">

0 commit comments

Comments
 (0)