99use \BNETDocs \Libraries \Packet ;
1010use \BNETDocs \Libraries \Product ;
1111use \BNETDocs \Libraries \User ;
12- use \BNETDocs \Models \Packet \Create as PacketCreateModel ;
12+ use \BNETDocs \Models \Packet \Form as FormModel ;
1313use \CarlBennett \MVC \Libraries \Common ;
1414use \CarlBennett \MVC \Libraries \Controller ;
1515use \CarlBennett \MVC \Libraries \DatabaseDriver ;
@@ -24,12 +24,12 @@ class Create extends Controller
2424{
2525 public function &run (Router &$ router , View &$ view , array &$ args )
2626 {
27- $ model = new PacketCreateModel ();
27+ $ model = new FormModel ();
2828 $ model ->active_user = Authentication::$ user ;
2929
3030 if (!$ model ->active_user || !$ model ->active_user ->getOption (User::OPTION_ACL_PACKET_CREATE ))
3131 {
32- $ model ->error = PacketCreateModel ::ERROR_ACL_DENIED ;
32+ $ model ->error = FormModel ::ERROR_ACL_DENIED ;
3333 $ model ->_responseCode = 401 ;
3434 $ view ->render ($ model );
3535 return $ model ;
@@ -39,37 +39,64 @@ public function &run(Router &$router, View &$view, array &$args)
3939 // Conflicting request query string fields will be overridden by POST-body fields
4040 $ router ->getRequestQueryArray () ?? [], $ router ->getRequestBodyArray () ?? []
4141 );
42- $ model ->packet = new Packet (null ); // TODO : Refactor Packet class
42+ $ model ->products = Product::getAllProducts ();
43+ $ model ->packet = new Packet (null );
4344
4445 if ($ router ->getRequestMethod () == 'POST ' )
4546 {
4647 $ this ->handlePost ($ model );
4748 }
4849
50+ if ($ model ->error === FormModel::ERROR_SUCCESS )
51+ {
52+ Logger::logEvent (
53+ EventTypes::PACKET_CREATED ,
54+ $ model ->active_user ->getId (),
55+ getenv ('REMOTE_ADDR ' ),
56+ json_encode (['model ' => $ model , 'view ' => get_class ($ view )])
57+ );
58+ }
59+
4960 $ view ->render ($ model );
5061 $ model ->_responseCode = 200 ;
5162 return $ model ;
5263 }
5364
54- protected function handlePost (PacketCreateModel &$ model )
65+ protected function handlePost (FormModel &$ model )
5566 {
5667 $ deprecated = $ model ->form_fields ['deprecated ' ] ?? null ;
68+ $ direction = $ model ->form_fields ['direction ' ] ?? null ;
5769 $ format = $ model ->form_fields ['format ' ] ?? null ;
5870 $ markdown = $ model ->form_fields ['markdown ' ] ?? null ;
5971 $ name = $ model ->form_fields ['name ' ] ?? null ;
6072 $ packet_id = $ model ->form_fields ['packet_id ' ] ?? null ;
6173 $ published = $ model ->form_fields ['published ' ] ?? null ;
6274 $ remarks = $ model ->form_fields ['remarks ' ] ?? null ;
6375 $ research = $ model ->form_fields ['research ' ] ?? null ;
64- $ used_by = $ model ->form_fields ['used_by ' ] ?? null ;
76+ $ used_by = $ model ->form_fields ['used_by ' ] ?? [];
77+
78+ try
79+ {
80+ $ model ->packet ->setDirection ((int ) $ direction );
81+ }
82+ catch (OutOfBoundsException $ e )
83+ {
84+ $ model ->error = FormModel::ERROR_OUTOFBOUNDS_DIRECTION ;
85+ return ;
86+ }
6587
6688 try
6789 {
6890 $ model ->packet ->setPacketId ($ packet_id );
6991 }
92+ catch (InvalidArgumentException $ e )
93+ {
94+ $ model ->error = FormModel::ERROR_OUTOFBOUNDS_PACKET_ID ;
95+ return ;
96+ }
7097 catch (OutOfBoundsException $ e )
7198 {
72- $ model ->error = PacketCreateModel:: ERROR_OUTOFBOUNDS_ID ;
99+ $ model ->error = FormModel:: ERROR_OUTOFBOUNDS_PACKET_ID ;
73100 return ;
74101 }
75102
@@ -79,7 +106,7 @@ protected function handlePost(PacketCreateModel &$model)
79106 }
80107 catch (OutOfBoundsException $ e )
81108 {
82- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_NAME ;
109+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_NAME ;
83110 return ;
84111 }
85112
@@ -89,7 +116,7 @@ protected function handlePost(PacketCreateModel &$model)
89116 }
90117 catch (OutOfBoundsException $ e )
91118 {
92- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_FORMAT ;
119+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_FORMAT ;
93120 return ;
94121 }
95122
@@ -99,7 +126,7 @@ protected function handlePost(PacketCreateModel &$model)
99126 }
100127 catch (OutOfBoundsException $ e )
101128 {
102- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_REMARKS ;
129+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_REMARKS ;
103130 return ;
104131 }
105132
@@ -109,34 +136,24 @@ protected function handlePost(PacketCreateModel &$model)
109136 }
110137 catch (OutOfBoundsException $ e )
111138 {
112- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_USED_BY ;
139+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_USED_BY ;
113140 return ;
114141 }
115142
116- $ model ->packet ->setOption (Packet::OPTION_DEPRECATED , $ deprecated );
117- $ model ->packet ->setOption (Packet::OPTION_MARKDOWN , $ markdown );
118- $ model ->packet ->setOption (Packet::OPTION_PUBLISHED , $ published );
119- $ model ->packet ->setOption (Packet::OPTION_RESEARCH , $ research );
143+ $ model ->packet ->setOption (Packet::OPTION_DEPRECATED , $ deprecated ? true : false );
144+ $ model ->packet ->setOption (Packet::OPTION_MARKDOWN , $ markdown ? true : false );
145+ $ model ->packet ->setOption (Packet::OPTION_PUBLISHED , $ published ? true : false );
146+ $ model ->packet ->setOption (Packet::OPTION_RESEARCH , $ research ? true : false );
120147
121148 try
122149 {
123150 $ model ->packet ->commit ();
124- $ model ->error = PacketCreateModel:: ERROR_CREATED ;
151+ $ model ->error = FormModel:: ERROR_SUCCESS ;
125152 }
126153 catch (Exception $ e )
127154 {
128155 Logger::logException ($ e );
129- $ model ->error = PacketCreateModel::ERROR_INTERNAL ;
130- }
131-
132- if ($ model ->error == PacketCreateModel::ERROR_CREATED )
133- {
134- Logger::logEvent (
135- EventTypes::PACKET_CREATED ,
136- $ model ->active_user ->getId (),
137- getenv ('REMOTE_ADDR ' ),
138- json_encode (['model ' => $ model , 'view ' => get_class ($ view )])
139- );
156+ $ model ->error = FormModel::ERROR_INTERNAL ;
140157 }
141158 }
142159}
0 commit comments