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
@@ -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}
0 commit comments