Skip to content

Commit d95672f

Browse files
authored
Merge pull request #90 from BNETDocs/feature/packet-edit-usedby
Add feature to edit products a packet is used with
2 parents e3e0015 + 4ffc352 commit d95672f

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

src/controllers/Packet/Edit.php

Lines changed: 15 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

@@ -36,8 +37,10 @@ public function &run(Router &$router, View &$view, array &$args) {
3637
$model->name = null;
3738
$model->packet = null;
3839
$model->packet_id = (isset($data["id"]) ? $data["id"] : null);
40+
$model->products = Product::getAllProducts();
3941
$model->published = null;
4042
$model->remarks = null;
43+
$model->used_by = null;
4144
$model->user = Authentication::$user;
4245

4346
$model->acl_allowed = ($model->user && $model->user->getAcl(
@@ -59,6 +62,7 @@ public function &run(Router &$router, View &$view, array &$args) {
5962
$model->remarks = $model->packet->getPacketRemarks(false);
6063
$model->markdown = ($flags & Packet::OPTION_MARKDOWN);
6164
$model->published = ($flags & Packet::OPTION_PUBLISHED);
65+
$model->used_by = $this->getUsedBy($model->packet);
6266

6367
if ($router->getRequestMethod() == "POST") {
6468
$this->handlePost($router, $model);
@@ -96,6 +100,7 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
96100
$content = (isset($data["content" ]) ? $data["content" ] : null);
97101
$publish = (isset($data["publish" ]) ? $data["publish" ] : null);
98102
$save = (isset($data["save" ]) ? $data["save" ] : null);
103+
$used_by = (isset($data["used_by" ]) ? $data["used_by" ] : null);
99104

100105
$model->id = $id;
101106
$model->name = $name;
@@ -138,6 +143,10 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
138143

139144
$success = $model->packet->update();
140145

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

143152
// SQL error occurred. We can show a friendly message to the user while
@@ -166,8 +175,14 @@ protected function handlePost(Router &$router, PacketEditModel &$model) {
166175
"name" => $model->packet->getPacketName(),
167176
"format" => $model->packet->getPacketFormat(),
168177
"remarks" => $model->packet->getPacketRemarks(false),
178+
"used_by" => $used_by
169179
])
170180
);
171181
}
172182

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

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ class Edit extends Model {
1616
public $name;
1717
public $packet;
1818
public $packet_id;
19+
public $products;
1920
public $published;
2021
public $remarks;
22+
public $used_by;
2123
public $user;
2224

2325
}

src/templates/Packet/Edit.phtml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ require('./header.inc.phtml');
8282
filter_var( $this->getContext()->format, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
8383
?></textarea>
8484
</section>
85+
<section>
86+
<label for="usedby">Used by:</label>
87+
<table>
88+
<thead></thead><tbody>
89+
<?php function add_product_checkbox( $id, $name, $checked ) { ?>
90+
<td><input type="checkbox" id="used_by_<?php echo $id; ?>" name="used_by[]" value="<?php echo $id; ?>"<?php if ( $checked ) { ?> checked="checked"<?php } ?>/><label for="used_by_<?php echo $id; ?>"><?php echo $name; ?></label></td>
91+
<?php }
92+
$product_ubound = count( $this->getContext()->products );
93+
for ( $product_i = 0; $product_i < $product_ubound; ++$product_i ) { ?>
94+
<tr>
95+
<?php
96+
$p = $this->getContext()->products[ $product_i ];
97+
$checked = in_array( $p, $this->getContext()->used_by );
98+
add_product_checkbox( $p->getBnetProductId(), $p->getLabel(), $checked );
99+
if ( $product_i + 1 < $product_ubound ) {
100+
$p = $this->getContext()->products[ ++$product_i ];
101+
$checked = in_array( $p, $this->getContext()->used_by );
102+
add_product_checkbox( $p->getBnetProductId(), $p->getLabel(), $checked );
103+
}
104+
?>
105+
</tr>
106+
<?php } ?>
107+
</tbody>
108+
</table>
109+
</section>
85110
<section>
86111
<label for="remarks">Remarks:</label>
87112
<span style="float:right;">

0 commit comments

Comments
 (0)