Skip to content

Commit f121103

Browse files
committed
Initial packet create page in progress
1 parent 6ab2c77 commit f121103

File tree

4 files changed

+191
-3
lines changed

4 files changed

+191
-3
lines changed

src/controllers/Packet/Create.php

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
2+
namespace BNETDocs\Controllers\Packet;
3+
4+
use \BNETDocs\Libraries\Authentication;
5+
use \BNETDocs\Libraries\Comment;
6+
use \BNETDocs\Libraries\EventTypes;
7+
use \BNETDocs\Libraries\Exceptions\PacketNotFoundException;
8+
use \BNETDocs\Libraries\Logger;
9+
use \BNETDocs\Libraries\Packet;
10+
use \BNETDocs\Libraries\Product;
11+
use \BNETDocs\Libraries\User;
12+
use \BNETDocs\Models\Packet\Create as PacketCreateModel;
13+
use \CarlBennett\MVC\Libraries\Common;
14+
use \CarlBennett\MVC\Libraries\Controller;
15+
use \CarlBennett\MVC\Libraries\DatabaseDriver;
16+
use \CarlBennett\MVC\Libraries\Router;
17+
use \CarlBennett\MVC\Libraries\View;
18+
use \DateTime;
19+
use \DateTimeZone;
20+
use \InvalidArgumentException;
21+
use \OutOfBoundsException;
22+
23+
class Create extends Controller
24+
{
25+
public function &run(Router &$router, View &$view, array &$args)
26+
{
27+
$model = new PacketCreateModel();
28+
$model->active_user = Authentication::$user;
29+
30+
if (!$model->active_user || !$model->active_user->getOption(User::OPTION_ACL_PACKET_CREATE))
31+
{
32+
$model->error = PacketCreateModel::ERROR_ACL_DENIED;
33+
$model->_responseCode = 401;
34+
$view->render($model);
35+
return $model;
36+
}
37+
38+
$model->form_fields = array_merge(
39+
// Conflicting request query string fields will be overridden by POST-body fields
40+
$router->getRequestQueryArray() ?? [], $router->getRequestBodyArray() ?? []
41+
);
42+
$model->packet = new Packet(null); // TODO : Refactor Packet class
43+
44+
if ($router->getRequestMethod() == 'POST')
45+
{
46+
$this->handlePost($model);
47+
}
48+
49+
$view->render($model);
50+
$model->_responseCode = 200;
51+
return $model;
52+
}
53+
54+
protected function handlePost(PacketCreateModel &$model)
55+
{
56+
$deprecated = $model->form_fields['deprecated'] ?? null;
57+
$format = $model->form_fields['format'] ?? null;
58+
$markdown = $model->form_fields['markdown'] ?? null;
59+
$name = $model->form_fields['name'] ?? null;
60+
$packet_id = $model->form_fields['packet_id'] ?? null;
61+
$published = $model->form_fields['published'] ?? null;
62+
$remarks = $model->form_fields['remarks'] ?? null;
63+
$research = $model->form_fields['research'] ?? null;
64+
$used_by = $model->form_fields['used_by'] ?? null;
65+
66+
try
67+
{
68+
$model->packet->setPacketId($packet_id);
69+
}
70+
catch (OutOfBoundsException $e)
71+
{
72+
$model->error = PacketCreateModel::ERROR_OUTOFBOUNDS_ID;
73+
return;
74+
}
75+
76+
try
77+
{
78+
$model->packet->setPacketName($name);
79+
}
80+
catch (OutOfBoundsException $e)
81+
{
82+
$model->error = PacketCreateModel::ERROR_OUTOFBOUNDS_NAME;
83+
return;
84+
}
85+
86+
try
87+
{
88+
$model->packet->setPacketFormat($format);
89+
}
90+
catch (OutOfBoundsException $e)
91+
{
92+
$model->error = PacketCreateModel::ERROR_OUTOFBOUNDS_FORMAT;
93+
return;
94+
}
95+
96+
try
97+
{
98+
$model->packet->setPacketRemarks($remarks);
99+
}
100+
catch (OutOfBoundsException $e)
101+
{
102+
$model->error = PacketCreateModel::ERROR_OUTOFBOUNDS_REMARKS;
103+
return;
104+
}
105+
106+
try
107+
{
108+
$model->packet->setUsedBy($used_by);
109+
}
110+
catch (OutOfBoundsException $e)
111+
{
112+
$model->error = PacketCreateModel::ERROR_OUTOFBOUNDS_USED_BY;
113+
return;
114+
}
115+
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);
120+
$model->packet->incrementEdited();
121+
122+
try
123+
{
124+
$model->packet->commit();
125+
$model->error = PacketCreateModel::ERROR_CREATED;
126+
}
127+
catch (Exception $e)
128+
{
129+
Logger::logException($e);
130+
$model->error = PacketCreateModel::ERROR_INTERNAL;
131+
}
132+
133+
if ($model->error == PacketCreateModel::ERROR_CREATED)
134+
{
135+
Logger::logEvent(
136+
EventTypes::PACKET_CREATED,
137+
$model->active_user->getId(),
138+
getenv('REMOTE_ADDR'),
139+
json_encode(['model' => $model, 'view' => get_class($view)])
140+
);
141+
}
142+
}
143+
}

src/main.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ function main() {
162162
$router->addRoute( // URL: /packet/:id
163163
"#^/packet/(\d+)/?#", "Packet\\View", "Packet\\ViewHtml"
164164
);
165-
//$router->addRoute( // URL: /packet/create
166-
// "#^/packet/create/?$#", "Packet\\Create", "Packet\\CreateHtml"
167-
//);
165+
$router->addRoute( // URL: /packet/create
166+
"#^/packet/create/?$#", "Packet\\Create", "Packet\\CreateHtml"
167+
);
168168
$router->addRoute( // URL: /packet/delete
169169
"#^/packet/delete/?$#", "Packet\\Delete", "Packet\\DeleteHtml"
170170
);

src/models/Packet/Create.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
2+
namespace BNETDocs\Models\Packet;
3+
class Create extends \BNETDocs\Models\ActiveUser
4+
{
5+
const ERROR_ACL_DENIED = 'ACL_DENIED';
6+
const ERROR_CREATED = 'CREATED';
7+
const ERROR_INTERNAL = 'INTERNAL';
8+
const ERROR_NONE = 'NONE';
9+
const ERROR_OUTOFBOUNDS_FORMAT = 'OUTOFBOUNDS_FORMAT';
10+
const ERROR_OUTOFBOUNDS_ID = 'OUTOFBOUNDS_ID';
11+
const ERROR_OUTOFBOUNDS_NAME = 'OUTOFBOUNDS_NAME';
12+
const ERROR_OUTOFBOUNDS_REMARKS = 'OUTOFBOUNDS_REMARKS';
13+
const ERROR_OUTOFBOUNDS_USED_BY = 'OUTOFBOUNDS_USED_BY';
14+
15+
public $error;
16+
public $form_fields;
17+
public $packet;
18+
public $products;
19+
}

src/views/Packet/CreateHtml.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace BNETDocs\Views\Packet;
3+
4+
use \BNETDocs\Models\Packet\Create as PacketCreateModel;
5+
use \CarlBennett\MVC\Libraries\Exceptions\IncorrectModelException;
6+
use \CarlBennett\MVC\Libraries\Model;
7+
use \CarlBennett\MVC\Libraries\Template;
8+
use \CarlBennett\MVC\Libraries\View;
9+
10+
class CreateHtml extends View
11+
{
12+
public function getMimeType()
13+
{
14+
return 'text/html;charset=utf-8';
15+
}
16+
17+
public function render(Model &$model)
18+
{
19+
if (!$model instanceof PacketCreateModel)
20+
{
21+
throw new IncorrectModelException();
22+
}
23+
(new Template($model, 'Packet/Create'))->render();
24+
$model->_responseHeaders['Content-Type'] = $this->getMimeType();
25+
}
26+
}

0 commit comments

Comments
 (0)