|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of SeAT |
| 5 | + * |
| 6 | + * Copyright (C) 2015 to present Leon Jacobs |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License along |
| 19 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + */ |
| 22 | + |
| 23 | +namespace Seat\Web\Http\Controllers\Corporation; |
| 24 | + |
| 25 | +use Seat\Eveapi\Models\Corporation\CorporationInfo; |
| 26 | +use Seat\Eveapi\Models\CorporationProjects\CorporationProject; |
| 27 | +use Seat\Web\Http\Controllers\Controller; |
| 28 | +use Seat\Web\Http\DataTables\Corporation\Financial\ProjectDataTable; |
| 29 | +use Seat\Web\Http\DataTables\Scopes\CorporationScope; |
| 30 | + |
| 31 | +/** |
| 32 | + * Class StructureController. |
| 33 | + * |
| 34 | + * @package Seat\Web\Http\Controllers\Corporation |
| 35 | + */ |
| 36 | +class ProjectController extends Controller |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @param \Seat\Eveapi\Models\Corporation\CorporationInfo $corporation |
| 40 | + * @param \Seat\Web\Http\DataTables\Corporation\Military\StructureDataTable $dataTable |
| 41 | + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 42 | + */ |
| 43 | + public function getProjects(CorporationInfo $corporation, ProjectDataTable $dataTable) |
| 44 | + { |
| 45 | + |
| 46 | + return $dataTable->addScope(new CorporationScope('corporation.projects', [$corporation->corporation_id])) |
| 47 | + ->render('web::corporation.projects.list', compact('corporation')); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param \Seat\Eveapi\Models\Corporation\CorporationInfo $corporation |
| 52 | + * @param string $project_id |
| 53 | + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 54 | + */ |
| 55 | + public function getProject(CorporationInfo $corporation, string $project_id) |
| 56 | + { |
| 57 | + $project = CorporationProject::with('contributors', 'creator') |
| 58 | + ->where('id', $project_id) |
| 59 | + ->first(); |
| 60 | + $config = $this->normalizeProjectConfiguration($project->configuration); |
| 61 | + |
| 62 | + return view('web::corporation.projects.modals.content', compact('corporation', 'project', 'config')); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Normalize project configuration into an array of items: |
| 67 | + * [ ['key'=>'...', 'label'=>'...', 'value'=>'...', 'description'=>null], ... ] |
| 68 | + */ |
| 69 | + protected function normalizeProjectConfiguration($configuration): array |
| 70 | + { |
| 71 | + if (is_string($configuration)) { |
| 72 | + // sometimes stored as JSON string |
| 73 | + $decoded = json_decode($configuration, true); |
| 74 | + if (json_last_error() === JSON_ERROR_NONE) { |
| 75 | + $configuration = $decoded; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // If null or empty |
| 80 | + if (empty($configuration)) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + $items = []; |
| 85 | + |
| 86 | + // Case: configuration is an array of option objects |
| 87 | + if (is_array($configuration) && array_values($configuration) === $configuration) { |
| 88 | + foreach ($configuration as $i => $opt) { |
| 89 | + if (is_array($opt)) { |
| 90 | + $items[] = [ |
| 91 | + 'key' => $opt['name'] ?? "option_{$i}", |
| 92 | + 'label' => $opt['display_name'] ?? $opt['name'] ?? "Option {$i}", |
| 93 | + 'value' => isset($opt['value']) ? $opt['value'] : (isset($opt['default']) ? $opt['default'] : null), |
| 94 | + 'description' => $opt['description'] ?? null, |
| 95 | + ]; |
| 96 | + } else { |
| 97 | + // primitive in array |
| 98 | + $items[] = [ |
| 99 | + 'key' => "option_{$i}", |
| 100 | + 'label' => "Option {$i}", |
| 101 | + 'value' => (string) $opt, |
| 102 | + 'description' => null, |
| 103 | + ]; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $items; |
| 108 | + } |
| 109 | + |
| 110 | + // Case: configuration is an associative object/array of named options |
| 111 | + if (is_array($configuration)) { |
| 112 | + foreach ($configuration as $k => $v) { |
| 113 | + if (is_array($v) || is_object($v)) { |
| 114 | + $vArr = (array) $v; |
| 115 | + $items[] = [ |
| 116 | + 'key' => $k, |
| 117 | + 'label' => $vArr['label'] ?? $vArr['name'] ?? $k, |
| 118 | + 'value' => $vArr['value'] ?? $vArr['default'] ?? json_encode($vArr), |
| 119 | + 'description' => $vArr['description'] ?? null, |
| 120 | + ]; |
| 121 | + } else { |
| 122 | + $items[] = [ |
| 123 | + 'key' => $k, |
| 124 | + 'label' => $k, |
| 125 | + 'value' => $v, |
| 126 | + 'description' => null, |
| 127 | + ]; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return $items; |
| 132 | + } |
| 133 | + |
| 134 | + // Case: object (stdClass) |
| 135 | + if (is_object($configuration)) { |
| 136 | + $arr = (array) $configuration; |
| 137 | + foreach ($arr as $k => $v) { |
| 138 | + if (is_object($v) || is_array($v)) { |
| 139 | + $vArr = (array) $v; |
| 140 | + $items[] = [ |
| 141 | + 'key' => $k, |
| 142 | + 'label' => $vArr['label'] ?? $vArr['name'] ?? $k, |
| 143 | + 'value' => $vArr['value'] ?? $vArr['default'] ?? json_encode($vArr), |
| 144 | + 'description' => $vArr['description'] ?? null, |
| 145 | + ]; |
| 146 | + } else { |
| 147 | + $items[] = [ |
| 148 | + 'key' => $k, |
| 149 | + 'label' => $k, |
| 150 | + 'value' => $v, |
| 151 | + 'description' => null, |
| 152 | + ]; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return $items; |
| 157 | + } |
| 158 | + |
| 159 | + // Fallback: stringify whatever it is |
| 160 | + return [ |
| 161 | + [ |
| 162 | + 'key' => 'raw', |
| 163 | + 'label' => 'Configuration', |
| 164 | + 'value' => is_scalar($configuration) ? (string) $configuration : json_encode($configuration), |
| 165 | + 'description' => null, |
| 166 | + ], |
| 167 | + ]; |
| 168 | + } |
| 169 | +} |
0 commit comments