• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AJenbo / agcms / 20972217862

13 Jan 2026 08:53PM UTC coverage: 53.678% (+0.1%) from 53.541%
20972217862

Pull #74

github

web-flow
Merge 4fdfac7ee into 498ff829e
Pull Request #74: Add PHP versions 8.4 to 8.5 to CI matrix

248 of 345 new or added lines in 40 files covered. (71.88%)

6 existing lines in 5 files now uncovered.

2780 of 5179 relevant lines covered (53.68%)

13.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/application/inc/Http/Controllers/Admin/PageController.php
1
<?php
2

3
namespace App\Http\Controllers\Admin;
4

5
use App\Exceptions\Exception;
6
use App\Exceptions\InvalidInput;
7
use App\Http\Controllers\Base;
8
use App\Http\Request;
9
use App\Models\Brand;
10
use App\Models\Category;
11
use App\Models\File;
12
use App\Models\Page;
13
use App\Models\Requirement;
14
use App\Services\ConfigService;
15
use App\Services\DbService;
16
use App\Services\OrmService;
17
use App\Services\RenderService;
18
use App\Services\SiteTreeService;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Response;
21

22
class PageController extends AbstractAdminController
23
{
24
    /**
25
     * Create or edit pages.
26
     */
27
    public function index(Request $request, ?int $id = null): Response
28
    {
NEW
29
        $selectedId = valint($request->cookies->get('activekat', -1));
×
NEW
30
        $openCategories = explode('<', valstring($request->cookies->get('openkat', '')));
×
UNCOV
31
        $openCategories = array_map('intval', $openCategories);
×
32

33
        $orm = app(OrmService::class);
×
34

35
        $page = null;
×
36
        $bindings = [];
×
37
        $accessories = [];
×
38
        if (null !== $id) {
×
39
            $page = $orm->getOne(Page::class, $id);
×
40
            if (!$page) {
×
41
                throw new InvalidInput(_('Page not found.'), Response::HTTP_NOT_FOUND);
×
42
            }
43

44
            foreach ($page->getCategories() as $category) {
×
45
                $bindings[$category->getId()] = $category->getPath();
×
46
            }
47

48
            foreach ($page->getAccessories() as $accessory) {
×
49
                $path = '';
×
50
                if ($root = $accessory->getPrimaryCategory()) {
×
51
                    $path = $root->getPath();
×
52
                }
53

54
                $accessories[$accessory->getId()] = $path . '/' . $accessory->getTitle();
×
55
            }
56
        }
57

58
        $siteTreeService = new SiteTreeService();
×
59

60
        $data = [
×
61
            'textWidth'    => ConfigService::getInt('text_width'),
×
62
            'thumbWidth'   => ConfigService::getInt('thumb_width'),
×
63
            'siteTree'     => $siteTreeService->getSiteTreeData($openCategories, 'categories', $selectedId),
×
64
            'requirements' => $orm->getByQuery(Requirement::class, 'SELECT * FROM `krav` ORDER BY navn'),
×
65
            'brands'       => $orm->getByQuery(Brand::class, 'SELECT * FROM `maerke` ORDER BY navn'),
×
66
            'page'         => $page,
67
            'bindings'     => $bindings,
68
            'accessories'  => $accessories,
69
            'blank_image'  => ConfigService::getString('blank_image', Base::DEFAULT_ICON),
×
70
        ] + $this->basicPageData($request);
×
71

72
        return $this->render('admin/redigerside', $data);
×
73
    }
74

75
    /**
76
     * Create new page and attach it to a category.
77
     */
78
    public function createPage(Request $request): JsonResponse
79
    {
80
        $category = app(OrmService::class)->getOne(Category::class, $request->request->getInt('categoryId'));
×
81
        if (!$category) {
×
82
            throw new InvalidInput(_('Category not found.'), Response::HTTP_NOT_FOUND);
×
83
        }
84

85
        $page = new Page([
×
86
            'title'          => $request->getRequestString('title') ?? '',
×
87
            'keywords'       => $request->getRequestString('keywords') ?? '',
×
88
            'excerpt'        => $request->getRequestString('excerpt') ?? '',
×
89
            'html'           => purifyHTML($request->getRequestString('html') ?? ''),
×
90
            'sku'            => $request->getRequestString('sku') ?? '',
×
91
            'icon_id'        => $request->getRequestInt('iconId'),
×
92
            'requirement_id' => $request->getRequestInt('requirementId'),
×
93
            'brand_id'       => $request->getRequestInt('brandId'),
×
94
            'price'          => $request->request->get('price'),
×
95
            'old_price'      => $request->request->get('oldPrice'),
×
96
            'price_type'     => $request->request->get('priceType'),
×
97
            'old_price_type' => $request->request->get('oldPriceType'),
×
98
        ]);
99
        $page->save();
×
100
        $page->addToCategory($category);
×
101

102
        return new JsonResponse(['id' => $page->getId()]);
×
103
    }
104

105
    /**
106
     * Update existing page.
107
     */
108
    public function updatePage(Request $request, int $id): JsonResponse
109
    {
110
        $orm = app(OrmService::class);
×
111

112
        $icon = null;
×
113
        if ($request->request->has('iconId')) {
×
114
            $icon = $orm->getOne(File::class, $request->request->getInt('iconId'));
×
115
        }
116

117
        $page = $orm->getOne(Page::class, $id);
×
118
        if (!$page) {
×
119
            throw new InvalidInput(_('Page not found.'), Response::HTTP_NOT_FOUND);
×
120
        }
121

122
        $page->setKeywords($request->getRequestString('keywords') ?? '')
×
123
            ->setPrice($request->request->getInt('price'))
×
124
            ->setSku($request->getRequestString('sku') ?? '')
×
125
            ->setOldPrice($request->request->getInt('oldPrice'))
×
126
            ->setExcerpt($request->getRequestString('excerpt') ?? '')
×
127
            ->setRequirementId($request->getRequestInt('requirementId'))
×
128
            ->setBrandId($request->getRequestInt('brandId'))
×
129
            ->setIcon($icon)
×
130
            ->setPriceType($request->request->getInt('priceType'))
×
131
            ->setOldPriceType($request->request->getInt('oldPriceType'))
×
132
            ->setHtml(purifyHTML($request->getRequestString('html') ?? ''))
×
133
            ->setTitle($request->getRequestString('title') ?? '')
×
134
            ->save();
×
135

136
        return new JsonResponse(['success' => true]);
×
137
    }
138

139
    /**
140
     * Delete page.
141
     */
142
    public function delete(Request $request, int $id): JsonResponse
143
    {
144
        $page = app(OrmService::class)->getOne(Page::class, $id);
×
145
        if ($page) {
×
146
            $page->delete();
×
147
        }
148

149
        return new JsonResponse(['class' => 'side' . $id]);
×
150
    }
151

152
    /**
153
     * Attach a page to a category.
154
     */
155
    public function addToCategory(Request $request, int $id, int $categoryId): JsonResponse
156
    {
157
        $orm = app(OrmService::class);
×
158

159
        $page = $orm->getOne(Page::class, $id);
×
160
        if (!$page) {
×
161
            throw new InvalidInput(_('Page not found.'), Response::HTTP_NOT_FOUND);
×
162
        }
163

164
        $category = $orm->getOne(Category::class, $categoryId);
×
165
        if (!$category) {
×
166
            throw new InvalidInput(_('Category not found.'), Response::HTTP_NOT_FOUND);
×
167
        }
168

169
        $result = ['pageId' => $page->getId(), 'deleted' => [], 'added' => null];
×
170

171
        if ($page->isInCategory($category)) {
×
172
            return new JsonResponse($result);
×
173
        }
174

175
        $page->addToCategory($category);
×
176
        $result['added'] = ['categoryId' => $category->getId(), 'path' => $category->getPath()];
×
177

178
        $rootCategory = $category->getRoot();
×
179
        foreach ($page->getCategories() as $node) {
×
180
            if ($node->getRoot() === $rootCategory) {
×
181
                continue;
×
182
            }
183

184
            $page->removeFromCategory($node);
×
185
            $result['deleted'][] = $node->getId();
×
186
        }
187

188
        return new JsonResponse($result);
×
189
    }
190

191
    /**
192
     * Remove the page from category.
193
     */
194
    public function removeFromCategory(Request $request, int $id, int $categoryId): JsonResponse
195
    {
196
        $orm = app(OrmService::class);
×
197

198
        $page = $orm->getOne(Page::class, $id);
×
199
        if (!$page) {
×
200
            throw new InvalidInput(_('Page not found.'), Response::HTTP_NOT_FOUND);
×
201
        }
202

203
        $category = $orm->getOne(Category::class, $categoryId);
×
204
        if (!$category) {
×
205
            throw new InvalidInput(_('Category not found.'), Response::HTTP_NOT_FOUND);
×
206
        }
207

208
        $result = ['pageId' => $page->getId(), 'deleted' => [], 'added' => null];
×
209
        if ((-1 === $category->getId() && 1 === count($page->getCategories())) || !$page->isInCategory($category)) {
×
210
            return new JsonResponse($result);
×
211
        }
212

213
        if (1 === count($page->getCategories())) {
×
214
            $inactiveCategory = $orm->getOne(Category::class, -1);
×
215
            if (!$inactiveCategory) {
×
216
                throw new InvalidInput(_('Category not found.'), Response::HTTP_NOT_FOUND);
×
217
            }
218

219
            $page->addToCategory($inactiveCategory);
×
220
            $result['added'] = ['categoryId' => -1, 'path' => '/' . _('Inactive') . '/'];
×
221
        }
222

223
        $page->removeFromCategory($category);
×
224
        $result['deleted'][] = $category->getId();
×
225

226
        return new JsonResponse($result);
×
227
    }
228

229
    /**
230
     * Search page.
231
     */
232
    public function search(Request $request): Response
233
    {
NEW
234
        $text = valstring($request->get('text', ''));
×
235
        if (!$text) {
×
236
            throw new InvalidInput(_('You must enter a search word.'));
×
237
        }
238

239
        $pages = $this->findPages($text);
×
240
        $data = ['text' => $text, 'pages' => $pages];
×
241

242
        if ($request->isXmlHttpRequest()) {
×
243
            return new JsonResponse(['id' => 'canvas', 'html' => app(RenderService::class)->render('admin/partial-search', $data)]);
×
244
        }
245

246
        return $this->render('admin/search', $data);
×
247
    }
248

249
    /**
250
     * Add a page to page relation.
251
     */
252
    public function addAccessory(Request $request, int $pageId, int $accessoryId): JsonResponse
253
    {
254
        $orm = app(OrmService::class);
×
255

256
        $page = $orm->getOne(Page::class, $pageId);
×
257
        if (!$page) {
×
258
            throw new InvalidInput(_('Page not found.'), Response::HTTP_NOT_FOUND);
×
259
        }
260

261
        $accessory = $orm->getOne(Page::class, $accessoryId);
×
262
        if (!$accessory) {
×
263
            throw new InvalidInput(_('Accessory not found.'), Response::HTTP_NOT_FOUND);
×
264
        }
265

266
        $page->addAccessory($accessory);
×
267

268
        $path = '';
×
269
        if ($root = $accessory->getPrimaryCategory()) {
×
270
            $path = $root->getPath();
×
271
        }
272

273
        return new JsonResponse([
×
274
            'pageId'      => $page->getId(),
×
275
            'accessoryId' => $accessory->getId(),
×
276
            'title'       => $path . '/' . $accessory->getTitle(),
×
277
        ]);
278
    }
279

280
    /**
281
     * Remove a page to page relation.
282
     */
283
    public function removeAccessory(Request $request, int $pageId, int $accessoryId): JsonResponse
284
    {
285
        $orm = app(OrmService::class);
×
286

287
        $page = $orm->getOne(Page::class, $pageId);
×
288
        if (!$page) {
×
289
            throw new InvalidInput(_('Page not found.'), Response::HTTP_NOT_FOUND);
×
290
        }
291

292
        $accessory = $orm->getOne(Page::class, $accessoryId);
×
293
        if (!$accessory) {
×
294
            throw new InvalidInput(_('Accessory not found.'), Response::HTTP_NOT_FOUND);
×
295
        }
296

297
        $page->removeAccessory($accessory);
×
298

299
        return new JsonResponse(['id' => 'accessory' . $accessory->getId()]);
×
300
    }
301

302
    /**
303
     * Find pages.
304
     *
305
     * @return Page[]
306
     */
307
    private function findPages(string $text): array
308
    {
309
        //fulltext search dosn't catch things like 3 letter words and some other combos
310
        $simpleq = preg_replace(
×
311
            ['/\s+/u', "/'/u", '/ยด/u', '/`/u'],
312
            ['%', '_', '_', '_'],
313
            $text
314
        );
315
        if (null === $simpleq) {
×
316
            throw new Exception('preg_replace failed');
×
317
        }
318

319
        $db = app(DbService::class);
×
320

321
        return app(OrmService::class)->getByQuery(
×
322
            Page::class,
323
            '
324
            SELECT * FROM sider
325
            WHERE MATCH (navn, text, beskrivelse) AGAINST(' . $db->quote($text) . ') > 0
×
326
                OR `navn` LIKE ' . $db->quote('%' . $simpleq . '%') . '
×
327
                OR `text` LIKE ' . $db->quote('%' . $simpleq . '%') . '
×
328
                OR `beskrivelse` LIKE ' . $db->quote('%' . $simpleq . '%') . '
×
329
            ORDER BY MATCH (navn, text, beskrivelse) AGAINST(' . $db->quote($text) . ') DESC
×
330
            '
331
        );
332
    }
333
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc