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