9
9
use \BNETDocs \Libraries \Packet ;
10
10
use \BNETDocs \Libraries \Product ;
11
11
use \BNETDocs \Libraries \User ;
12
- use \BNETDocs \Models \Packet \Create as PacketCreateModel ;
12
+ use \BNETDocs \Models \Packet \Form as FormModel ;
13
13
use \CarlBennett \MVC \Libraries \Common ;
14
14
use \CarlBennett \MVC \Libraries \Controller ;
15
15
use \CarlBennett \MVC \Libraries \DatabaseDriver ;
@@ -24,12 +24,12 @@ class Create extends Controller
24
24
{
25
25
public function &run (Router &$ router , View &$ view , array &$ args )
26
26
{
27
- $ model = new PacketCreateModel ();
27
+ $ model = new FormModel ();
28
28
$ model ->active_user = Authentication::$ user ;
29
29
30
30
if (!$ model ->active_user || !$ model ->active_user ->getOption (User::OPTION_ACL_PACKET_CREATE ))
31
31
{
32
- $ model ->error = PacketCreateModel ::ERROR_ACL_DENIED ;
32
+ $ model ->error = FormModel ::ERROR_ACL_DENIED ;
33
33
$ model ->_responseCode = 401 ;
34
34
$ view ->render ($ model );
35
35
return $ model ;
@@ -39,37 +39,64 @@ public function &run(Router &$router, View &$view, array &$args)
39
39
// Conflicting request query string fields will be overridden by POST-body fields
40
40
$ router ->getRequestQueryArray () ?? [], $ router ->getRequestBodyArray () ?? []
41
41
);
42
- $ model ->packet = new Packet (null ); // TODO : Refactor Packet class
42
+ $ model ->products = Product::getAllProducts ();
43
+ $ model ->packet = new Packet (null );
43
44
44
45
if ($ router ->getRequestMethod () == 'POST ' )
45
46
{
46
47
$ this ->handlePost ($ model );
47
48
}
48
49
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
+
49
60
$ view ->render ($ model );
50
61
$ model ->_responseCode = 200 ;
51
62
return $ model ;
52
63
}
53
64
54
- protected function handlePost (PacketCreateModel &$ model )
65
+ protected function handlePost (FormModel &$ model )
55
66
{
56
67
$ deprecated = $ model ->form_fields ['deprecated ' ] ?? null ;
68
+ $ direction = $ model ->form_fields ['direction ' ] ?? null ;
57
69
$ format = $ model ->form_fields ['format ' ] ?? null ;
58
70
$ markdown = $ model ->form_fields ['markdown ' ] ?? null ;
59
71
$ name = $ model ->form_fields ['name ' ] ?? null ;
60
72
$ packet_id = $ model ->form_fields ['packet_id ' ] ?? null ;
61
73
$ published = $ model ->form_fields ['published ' ] ?? null ;
62
74
$ remarks = $ model ->form_fields ['remarks ' ] ?? null ;
63
75
$ 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
+ }
65
87
66
88
try
67
89
{
68
90
$ model ->packet ->setPacketId ($ packet_id );
69
91
}
92
+ catch (InvalidArgumentException $ e )
93
+ {
94
+ $ model ->error = FormModel::ERROR_OUTOFBOUNDS_PACKET_ID ;
95
+ return ;
96
+ }
70
97
catch (OutOfBoundsException $ e )
71
98
{
72
- $ model ->error = PacketCreateModel:: ERROR_OUTOFBOUNDS_ID ;
99
+ $ model ->error = FormModel:: ERROR_OUTOFBOUNDS_PACKET_ID ;
73
100
return ;
74
101
}
75
102
@@ -79,7 +106,7 @@ protected function handlePost(PacketCreateModel &$model)
79
106
}
80
107
catch (OutOfBoundsException $ e )
81
108
{
82
- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_NAME ;
109
+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_NAME ;
83
110
return ;
84
111
}
85
112
@@ -89,7 +116,7 @@ protected function handlePost(PacketCreateModel &$model)
89
116
}
90
117
catch (OutOfBoundsException $ e )
91
118
{
92
- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_FORMAT ;
119
+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_FORMAT ;
93
120
return ;
94
121
}
95
122
@@ -99,7 +126,7 @@ protected function handlePost(PacketCreateModel &$model)
99
126
}
100
127
catch (OutOfBoundsException $ e )
101
128
{
102
- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_REMARKS ;
129
+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_REMARKS ;
103
130
return ;
104
131
}
105
132
@@ -109,34 +136,24 @@ protected function handlePost(PacketCreateModel &$model)
109
136
}
110
137
catch (OutOfBoundsException $ e )
111
138
{
112
- $ model ->error = PacketCreateModel ::ERROR_OUTOFBOUNDS_USED_BY ;
139
+ $ model ->error = FormModel ::ERROR_OUTOFBOUNDS_USED_BY ;
113
140
return ;
114
141
}
115
142
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 );
120
147
121
148
try
122
149
{
123
150
$ model ->packet ->commit ();
124
- $ model ->error = PacketCreateModel:: ERROR_CREATED ;
151
+ $ model ->error = FormModel:: ERROR_SUCCESS ;
125
152
}
126
153
catch (Exception $ e )
127
154
{
128
155
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 ;
140
157
}
141
158
}
142
159
}
0 commit comments