Skip to content

Commit e8cc1ba

Browse files
committed
Add ability to edit the products a packet is used with
* Uses a series of checkboxes passed as an array of bnet product ID's * Adds Packet->setUsedBy($values) function, which actually updates the database. * Adds Product::getProductsFromIds($ids) function which returns an array of product objects from the given array of product ID's. This replaces most of the Packet\Edit controller's getUsedBy() function.
1 parent fd4330c commit e8cc1ba

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

src/controllers/Packet/Edit.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use \BNETDocs\Libraries\Logger;
1010
use \BNETDocs\Libraries\Packet;
1111
use \BNETDocs\Libraries\User;
12+
use \BNETDocs\Libraries\Product;
1213
use \BNETDocs\Models\Packet\Edit as PacketEditModel;
1314

1415
use \CarlBennett\MVC\Libraries\Common;
@@ -39,6 +40,7 @@ public function &run(Router &$router, View &$view, array &$args) {
3940
$model->published = null;
4041
$model->remarks = null;
4142
$model->user = Authentication::$user;
43+
$model->used_by = null;
4244

4345
$model->acl_allowed = ($model->user && $model->user->getAcl(
4446
User::OPTION_ACL_PACKET_MODIFY
@@ -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;
@@ -135,6 +139,8 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
135139
$model->packet->setEditedDateTime(
136140
new DateTime("now", new DateTimeZone("Etc/UTC"))
137141
);
142+
143+
$model->packet->setUsedBy($used_by);
138144

139145
$success = $model->packet->update();
140146

@@ -166,8 +172,14 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
166172
"name" => $model->packet->getPacketName(),
167173
"format" => $model->packet->getPacketFormat(),
168174
"remarks" => $model->packet->getPacketRemarks(false),
175+
"used_by" => $used_by
169176
])
170177
);
171178
}
172179

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

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
@@ -702,6 +702,49 @@ public function setPublished( $value ) {
702702
$this->options_bitmask &= ~self::OPTION_PUBLISHED;
703703
}
704704
}
705+
706+
public function setUsedBy( $value ) {
707+
if (!isset(Common::$database)) {
708+
Common::$database = DatabaseDriver::getDatabaseObject();
709+
}
710+
try {
711+
Common::$database->beginTransaction();
712+
$stmt = Common::$database->prepare('
713+
DELETE FROM `packet_used_by`
714+
WHERE `id` = :id;
715+
');
716+
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
717+
if (!$stmt->execute()) {
718+
throw new QueryException('Cannot update packet used by');
719+
}
720+
if ($value !== null && count($value) > 0) {
721+
$insert = [];
722+
$placeholders = [];
723+
foreach ($value as $v) {
724+
array_push($insert, $this->id, (int)$v);
725+
$placeholders[] = '(?, ?)';
726+
}
727+
$stmt = Common::$database->prepare('
728+
INSERT INTO `packet_used_by`
729+
(`id`, `bnet_product_id`)
730+
VALUES
731+
' . implode(', ', $placeholders) . ';
732+
');
733+
if (!$stmt->execute($insert)) {
734+
Common::$database->rollBack();
735+
throw new QueryException('Cannot update packet used by');
736+
}
737+
}
738+
739+
Common::$database->commit();
740+
741+
$cache_key = 'bnetdocs-packetusedby-' . $this->id;
742+
Common::$cache->set($cache_key, serialize($value), self::CACHE_TTL);
743+
} catch (PDOException $e) {
744+
Common::$database->rollBack();
745+
throw new QueryException('Cannot update packet used by', $e);
746+
}
747+
}
705748

706749
public function update() {
707750
if (!isset(Common::$database)) {

src/libraries/Product.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 Product($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
@@ -19,5 +19,6 @@ class Edit extends Model {
1919
public $published;
2020
public $remarks;
2121
public $user;
22+
public $used_by;
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)