88use \BNETDocs \Libraries \Exceptions \PacketNotFoundException ;
99use \BNETDocs \Libraries \Logger ;
1010use \BNETDocs \Libraries \Packet ;
11+ use \BNETDocs \Libraries \Product ;
1112use \BNETDocs \Libraries \User ;
1213use \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}
0 commit comments