Issues (67)

src/User/UserController.php (15 issues)

1
<?php
2
3
namespace Alfs18\User;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Alfs18\User\HTMLForm\UserLoginForm;
8
use Alfs18\User\HTMLForm\CreateUserForm;
9
use Alfs18\User\HTMLForm\CreateQuestionForm;
10
use Alfs18\User\HTMLForm\CreateCommentsForm;
11
// use Alfs18\User\HTMLForm\CreateAnswerForm;
12
use Alfs18\User\HTMLForm\CreateAnswerCommentsForm;
13
use Alfs18\User\HTMLForm\FormModelCheckboxMultiple;
14
use Alfs18\User\HTMLForm\FormElementFile;
15
use Alfs18\User\HTMLForm\SearchForm;
16
use Alfs18\User\Functions;
17
use Anax\TextFilter;
18
19
// use Anax\Route\Exception\ForbiddenException;
20
// use Anax\Route\Exception\NotFoundException;
21
// use Anax\Route\Exception\InternalErrorException;
22
23
/**
24
 * A sample controller to show how a controller class can be implemented.
25
 */
26
class UserController implements ContainerInjectableInterface
27
{
28
    use ContainerInjectableTrait;
29
30
31
32
    /**
33
     * @var $data description
34
     */
35
    //private $data;
36
37
38
39
    /**
40
     * The initialize method is optional and will always be called before the
41
     * target method/action. This is a convienient method where you could
42
     * setup internal properties that are commonly used by several methods.
43
     *
44
     * @return object or void
45
     */
46
    public function initialize()
47
    {
48
        $page = $this->di->get("page");
49
        $request = $this->di->get("request");
50
        // var_dump($page);
51
        $route = $request->getRoute();
52
        // var_dump($route);
53
        $acronym = $_SESSION["acronym"] ?? null;
54
        // var_dump($_SESSION);
55
56
        if ($route == "user/login" || $route == "user/create") {
57
            return;
58
        } elseif (!$acronym) {
59
            $page = $this->di->get("page");
60
61
            $page->add("anax/v2/article/default", [
62
                "content" => "Vänligen logga in för att ta del av innehållet.",
63
            ]);
64
65
            return $page->render([
66
                "title" => "Failed",
67
            ]);
68
        }
69
70
        // if ($route !="user/viewQuestion") {
71
        //     $baseURL = $request->getBaseUrl();
72
        //     $pic = [
73
        //         // "content" => "<img src='../../htdocs/image/snail2.jpg' width='1100px'></img>",
74
        //         "content" => "<img src='{$baseURL}/image/snail2.jpg' width='1100px'></img>",
75
        //     ];
76
        //
77
        //     $page->add("anax/v2/article/default", $pic, "flash");
78
        // }
79
80
        $baseURL = $request->getBaseUrl();
81
        $content = [
82
            "pic" => "<img src='{$baseURL}/image/snail3.jpg' width='1100px'></img>",
83
            "text" => "<h1>Allt om trädgård</h1>",
84
        ];
85
86
        $page->add("test", $content, "flash");
87
88
        // $pic = [
89
        //         // "content" => "<img src='../../htdocs/image/snail2.jpg' width='1100px'></img>",
90
        //         "content" => "<img src='{$baseURL}/image/snail2.jpg' width='1100px'></img>",
91
        //     ];
92
        //
93
        //     $page->add("anax/v2/article/default", $pic, "flash");
94
    }
95
96
97
98
    /**
99
     * Description.
100
     *
101
     * @param datatype $variable Description
0 ignored issues
show
The type Alfs18\User\datatype was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
102
     *
103
     * @throws Exception
104
     *
105
     * @return object as a response object
106
     */
107
    public function indexActionGet() : object
108
    {
109
        $page = $this->di->get("page");
110
111
        $page->add("anax/v2/article/default", [
112
            "content" => "An index page",
113
        ]);
114
115
        return $page->render([
116
            "title" => "A index page",
117
        ]);
118
    }
119
120
121
122
    /**
123
     * Description.
124
     *
125
     * @param datatype $variable Description
126
     *
127
     * @throws Exception
128
     *
129
     * @return object as a response object
130
     */
131
    public function loginAction() : object
132
    {
133
        $page = $this->di->get("page");
134
        $form = new UserLoginForm($this->di);
135
        $form->check();
136
137
        // var_dump($_SESSION);
138
139
        if ($_SESSION["acronym"] ?? null) {
140
            $response = $this->di->get("response");
141
            return $response->redirect("user/questions");
142
        }
143
144
        $page->add("anax/v2/article/default", [
145
            "content" => $form->getHTML(),
146
        ]);
147
148
        // $data = [
149
        //     "content" => "Inget konto?<br>Skapa ett <a href='create'>här</a>",
150
        // ];
151
152
        $pic = [
153
            "content" => "<img src='../../htdocs/image/garden2.jpg' width='1000px'></img>",
154
        ];
155
156
        $page->add("login-side", [], "sidebar-right");
157
        $page->add("anax/v2/article/default", $pic, "flash");
158
159
        return $page->render([
160
            "title" => "A login page",
161
        ]);
162
    }
163
164
165
    /**
166
     * Description.
167
     *
168
     * @param datatype $variable Description
169
     *
170
     * @throws Exception
171
     *
172
     * @return object as a response object
173
     */
174
    public function profileAction() : object
175
    {
176
        $page = $this->di->get("page");
177
        $request = $this->di->get("request");
178
179
        $res = new Functions();
180
        $acronym = $_SESSION["acronym"];
181
        $rows = "id, question, tags";
182
        $questions = $res->getProfileInfo($acronym, $this->di, "Question", $rows);
183
        $rows2 = "id, questionId, answer";
184
        $answers = $res->getProfileInfo($acronym, $this->di, "Answers", $rows2);
185
        $rows3 = "id, questionId, answerId, comment";
0 ignored issues
show
The assignment to $rows3 is dead and can be removed.
Loading history...
186
        $comments = $res->getProfileInfo($acronym, $this->di, "Comments", "*");
187
        $rows4 = "points, info, picture, created";
188
        $user = $res->getProfileInfo($acronym, $this->di, "User", $rows4);
189
        // var_dump($comments);
190
191
        $form = new FormElementFile("img", ["image/*"]);
192
        $baseURL = $request->getBaseUrl();
193
        // var_dump($baseURL);
194
195
        $filter = new \Anax\TextFilter\TextFilter();
196
197
        $page->add("profile", [
198
            "content" => $questions,
199
            "acronym" => $acronym,
200
            "answers" => $answers,
201
            "comments" => $comments,
202
            "user" => $user,
203
            "form" => $form->getHTML(),
204
            "baseURL" => $baseURL,
205
            "filter" => $filter,
206
        ]);
207
208
        // $pic = [
209
        //     "content" => "<img src='../../htdocs/image/car.png' width='1000px'></img>",
210
        // ];
211
        //
212
        // $page->add("login-side", [], "sidebar-right");
213
        // $page->add("anax/v2/article/default", $pic, "flash");
214
215
        return $page->render([
216
            "title" => "Profile",
217
        ]);
218
    }
219
220
221
    /**
222
     * Description.
223
     *
224
     * @param datatype $variable Description
225
     *
226
     * @throws Exception
227
     *
228
     * @return object as a response object
229
     */
230
    public function profileEditAction(string $acronym, string $toUpdate) : object
231
    {
232
        $page = $this->di->get("page");
233
        $request = $this->di->get("request");
234
        $response = $this->di->get("response");
235
236
        $res = new Functions();
237
        $rows = "points, info, picture, created";
238
        $user = $res->getProfileInfo($acronym, $this->di, "User", $rows);
239
        $pictures = $res->getAllProfilePictures($this->di, $acronym);
240
241
        $info = $_POST["info"] ?? null;
242
        $picture = $_POST["picture"] ?? null;
243
        if ($info) {
244
            var_dump($info);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($info) looks like debug code. Are you sure you do not want to remove it?
Loading history...
245
            $res->setProfileInfo($this->di, $acronym, $info);
246
247
            $response->redirect("user/profile");
248
        } elseif ($picture) {
249
            var_dump($picture);
250
            $res->setProfilePicture($this->di, $acronym, $picture);
251
252
            $response->redirect("user/profile");
253
        }
254
255
        // update table User, with toUpdate
256
257
        $baseURL = $request->getBaseUrl();
258
259
        $page->add("profile-edit", [
260
            "acronym" => $acronym,
261
            "user" => $user,
262
            "baseURL" => $baseURL,
263
            "pictures" => $pictures,
264
            "toUpdate" => $toUpdate,
265
        ]);
266
267
        return $page->render([
268
            "title" => "Profile",
269
        ]);
270
    }
271
272
273
    /**
274
     * Description.
275
     *
276
     * @param datatype $variable Description
277
     *
278
     * @throws Exception
279
     *
280
     * @return object as a response object
281
     */
282
    public function uploadAction() : object
283
    {
284
        $page = $this->di->get("page");
285
        $request = $this->di->get("request");
286
287
        $res = new Functions();
288
289
        $post = $request->getPost("submit");
290
        var_dump($post);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($post) looks like debug code. Are you sure you do not want to remove it?
Loading history...
291
        // var_dump($_POST["submit"]);
292
        // var_dump($_POST);
293
        var_dump($_FILES);
294
295
        if ($post) {
296
            // $res->setProfilePicture($this->di, $acronym, $post);
297
            $target_dir = "C:/cygwin64/home/Lichn/dbwebb-kurser/ramverk1/me/kmom10/module/htdocs/img/profile/";
298
            $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
299
            $uploadOk = 1;
300
            $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
0 ignored issues
show
It seems like pathinfo($target_file, A...ser\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

300
            $imageFileType = strtolower(/** @scrutinizer ignore-type */ pathinfo($target_file,PATHINFO_EXTENSION));
Loading history...
301
            // Check if image file is a actual image or fake image
302
            if(isset($_POST["submit"])) {
303
                var_dump("smask");
304
             $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
305
              if($check !== false) {
306
                echo "File is an image - " . $check["mime"] . ".";
307
                $uploadOk = 1;
308
              } else {
309
                echo "File is not an image.";
310
                $uploadOk = 0;
311
              }
312
            }
313
            // Check if file already exists
314
            if (file_exists($target_file)) {
315
              echo "Sorry, file already exists.";
316
              $uploadOk = 0;
317
            }
318
319
            // Check file size
320
            if ($_FILES["fileToUpload"]["size"] > 500000) {
321
              echo "Sorry, your file is too large.";
322
              $uploadOk = 0;
323
            }
324
325
            // Allow certain file formats
326
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
327
            && $imageFileType != "gif" ) {
328
              echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
329
              $uploadOk = 0;
330
            }
331
332
            // Check if $uploadOk is set to 0 by an error
333
            if ($uploadOk == 0) {
334
              echo "Sorry, your file was not uploaded.";
335
            // if everything is ok, try to upload file
336
            } else {
337
              if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
338
                echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
339
                $res->setProfilePicture($this->di, $acronym, $_FILES["fileToUpload"]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $acronym seems to be never defined.
Loading history...
340
                $res->addProfilePicture($this->di, $acronym, $_FILES["fileToUpload"]);
341
              } else {
342
                echo "Sorry, there was an error uploading your file.";
343
              }
344
            }
345
        }
346
347
        $page->add("anax/v2/article/default", [
348
            "content" => "Hello",
349
        ]);
350
351
        // $pic = [
352
        //     "content" => "<img src='../../htdocs/image/car.png' width='1000px'></img>",
353
        // ];
354
        //
355
        // $page->add("login-side", [], "sidebar-right");
356
        // $page->add("anax/v2/article/default", $pic, "flash");
357
358
        return $page->render([
359
            "title" => "Profile",
360
        ]);
361
    }
362
363
364
    /**
365
     * Description.
366
     *
367
     * @param datatype $variable Description
368
     *
369
     * @throws Exception
370
     *
371
     * @return object as a response object
372
     */
373
    public function viewProfileAction(string $acronym) : object
374
    {
375
        $page = $this->di->get("page");
376
        $request = $this->di->get("request");
377
378
        $res = new Functions();
379
        $rows = "id, question, tags";
380
        $questions = $res->getProfileInfo($acronym, $this->di, "Question", $rows);
381
        foreach ($questions as $question) {
382
            $answered = $res->checkIfAnswered($this->di, $question->id);
383
            $question->answered = $answered;
384
        }
385
        $answers = $res->getProfileInfo($acronym, $this->di, "Answers", "*");
386
        $comments = $res->getProfileInfo($acronym, $this->di, "Comments", "*");
387
        $user = $res->getProfileInfo($acronym, $this->di, "User", "*");
388
        $baseURL = $request->getBaseUrl();
389
        $filter = new \Anax\TextFilter\TextFilter();
390
391
        $page->add("view-profile", [
392
            "content" => $questions,
393
            "answers" => $answers,
394
            "comments" => $comments,
395
            "acronym" => $acronym,
396
            "user" => $user,
397
            "baseURL" => $baseURL,
398
            "filter" => $filter,
399
        ]);
400
401
        return $page->render([
402
            "title" => "Profile",
403
        ]);
404
    }
405
406
407
    /**
408
     * Description.
409
     *
410
     * @param datatype $variable Description
411
     *
412
     * @throws Exception
413
     *
414
     * @return object as a response object
415
     */
416
    public function askAction() : object
417
    {
418
        $page = $this->di->get("page");
419
        // $acronym = $_SESSION["acronym"];
420
        $acronym = $_SESSION["acronym"];
421
        // var_dump($_SESSION);
422
        $form = new CreateQuestionForm($this->di, $acronym);
423
        $form->check();
424
425
        $page->add("ask-question", [
426
            "content" => $form->getHTML(),
427
        ]);
428
429
        // $page->add("ask-question", [
430
        //     "content" => "Hello",
431
        // ]);
432
433
        // $pic = [
434
        //     "content" => "<img src='../../htdocs/image/car.png' width='1000px'></img>",
435
        // ];
436
437
        $page->add("ask-tip", [], "sidebar-right");
438
        $page->add("ask-tag", [], "sidebar-right");
439
        // $page->add("anax/v2/article/default", $pic, "flash");
440
441
        return $page->render([
442
            "title" => "Ställ en fråga",
443
        ]);
444
    }
445
446
447
    /**
448
     * Description.
449
     *
450
     * @param datatype $variable Description
451
     *
452
     * @throws Exception
453
     *
454
     * @return object as a response object
455
     */
456
    public function createAction() : object
457
    {
458
        $page = $this->di->get("page");
459
        $form = new CreateUserForm($this->di);
460
        $form->check();
461
462
463
        $page->add("anax/v2/article/default", [
464
            "content" => $form->getHTML(),
465
        ]);
466
467
        return $page->render([
468
            "title" => "A create user page",
469
        ]);
470
    }
471
472
473
    /**
474
     * Description.
475
     *
476
     * @param datatype $variable Description
477
     *
478
     * @throws Exception
479
     *
480
     * @return object as a response object
481
     */
482
    public function logoutAction() : object
483
    {
484
        $page = $this->di->get("page");
0 ignored issues
show
The assignment to $page is dead and can be removed.
Loading history...
485
        $response = $this->di->get("response");
486
        $user = new Functions();
487
        $user->sessionDestroy($this->di);
0 ignored issues
show
The call to Alfs18\User\Functions::sessionDestroy() has too many arguments starting with $this->di. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

487
        $user->/** @scrutinizer ignore-call */ 
488
               sessionDestroy($this->di);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
488
489
        // $page->add("anax/v2/article/default", [
490
        //     "content" => "Du har loggats ut",
491
        // ]);
492
        //
493
        // return $page->render([
494
        //     "title" => "A create user page",
495
        // ]);
496
        $response->redirect("index");
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return object. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
497
    }
498
499
500
    /**
501
     * Description.
502
     *
503
     * @param datatype $variable Description
504
     *
505
     * @throws Exception
506
     *
507
     * @return object as a response object
508
     */
509
    public function questionsAction() : object
510
    {
511
        $page = $this->di->get("page");
512
513
        $res = new Functions();
514
        $questions = $res->getAllQuestions($this->di);
515
516
        rsort($questions);
517
        $questions2 = array_slice($questions, 0, 3);
518
519
        // Get list of all users.
520
        $users = $res->getAllUsers($this->di);
521
        $sums = [];
522
        foreach ($users as $user) {
523
            $acronym = $user->acronym;
524
            // get all questions made by user
525
            $quests = $res->getProfileInfo($acronym, $this->di, "Question", "question");
526
            // get all comments made by user
527
            $com = $res->getProfileInfo($acronym, $this->di, "Comments", "comment");
528
            // get all answers made by user
529
            $ans = $res->getProfileInfo($acronym, $this->di, "Answers", "answer");
530
            // sum up everything
531
            $sums[$acronym] = sizeof($quests) + sizeof($com) + sizeof($ans);
532
        }
533
534
        arsort($sums);
535
536
        // get all tags and count how many times they occur
537
        $tags = $res->getAllTags($this->di);
538
        $tags2 = $res->countTagsFrequency($tags);
539
        arsort($tags2);
540
541
        $data = [
542
            "questions" => $questions2,
543
            "size" => sizeof($questions),
544
            "userStatus" => array_slice($sums, 0, 3),
545
            "tags" => array_slice($tags2, 0, 3),
546
        ];
547
548
        $page->add("questions", $data);
549
550
        return $page->render([
551
            "title" => "A create user page",
552
        ]);
553
    }
554
555
556
    /**
557
     * Description.
558
     *
559
     * @param datatype $variable Description
560
     *
561
     * @throws Exception
562
     *
563
     * @return object as a response object
564
     */
565
    public function tagsAction() : object
566
    {
567
        $page = $this->di->get("page");
568
569
        // Get all from comments where id = $commentId.
570
        $res = new Functions();
571
        $tags = $res->getAllTags($this->di);
572
        $tags2 = $res->getTagsOnce($tags);
573
        sort($tags2);
574
        // var_dump($tags2);
575
576
        $page->add("tags", [
577
            "tags" => $tags2,
578
        ]);
579
580
        return $page->render([
581
            "title" => "A create user page",
582
        ]);
583
    }
584
585
586
    /**
587
     * Description.
588
     *
589
     * @param datatype $variable Description
590
     *
591
     * @throws Exception
592
     *
593
     * @return object as a response object
594
     */
595
    public function usersAction() : object
596
    {
597
        $page = $this->di->get("page");
598
599
        // Get all from comments where id = $commentId.
600
        $res = new Functions();
601
        $users = $res->getAllUsers($this->di);
602
603
        sort($users);
604
605
        $page->add("users", [
606
            "users" => $users,
607
        ]);
608
609
        return $page->render([
610
            "title" => "A create user page",
611
        ]);
612
    }
613
614
615
    /**
616
     * Description.
617
     *
618
     * @param datatype $variable Description
619
     *
620
     * @throws Exception
621
     *
622
     * @return object as a response object
623
     */
624
    public function acceptAnswerAction(int $questionId, int $commentId) : object
625
    {
626
        $response = $this->di->get("response");
627
        $page = $this->di->get("page");
0 ignored issues
show
The assignment to $page is dead and can be removed.
Loading history...
628
629
        // Get all from comments where id = $commentId.
630
        $res = new Functions();
631
        $comment = $res->getOneQuestionComment($this->di, $commentId);
632
        var_dump($comment);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($comment) looks like debug code. Are you sure you do not want to remove it?
Loading history...
633
634
        $acronym = $comment[0]->acronym;
635
        $answer = $comment[0]->comment;
636
        $created = $comment[0]->created;
637
638
        // Save as answer.
639
        $res->saveAnswer($this->di, $questionId, $acronym, $answer, $created);
640
641
        // Delete comment.
642
        $res->deleteComment($this->di, $commentId);
643
644
        var_dump($comment[0]->comment);
645
646
        // $page->add("anax/v2/article/default", [
647
        //     "content" => "Hello",
648
        // ]);
649
        //
650
        // return $page->render([
651
        //     "title" => "A create user page",
652
        // ]);
653
654
655
        $response->redirect("user/viewQuestion/{$questionId}");
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return object. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
656
    }
657
658
659
    /**
660
     * Description.
661
     *
662
     * @param datatype $variable Description
663
     *
664
     * @throws Exception
665
     *
666
     * @return object as a response object
667
     */
668
    public function showQuestionsAction() : object
669
    {
670
        $page = $this->di->get("page");
671
        $request = $this->di->get("request");
672
673
        // get search form
674
        $formSearch = new SearchForm($this->di);
675
        $formSearch->check();
676
677
        // get all questions to show in the view.
678
        $res = new Functions();
679
        $questions = $res->getAllQuestions($this->di);
680
681
        // get all tags, and only once
682
        $tags = $res->getAllTags($this->di);
683
        $tags2 = $res->getTagsOnce($tags);
684
685
        $baseURL = $request->getBaseUrl();
686
        $size = "?width=50&height=50&crop-to-fit&area=0,0,0,0";
687
688
        foreach ($questions as $question) {
689
            $acronym = $res->changeCharacter($question->acronym);
690
            $questionId = $question->id;
691
692
            // get profile picture of the user who made a question
693
            $picture = $res->getProfileInfo($acronym, $this->di, "User", "picture");
0 ignored issues
show
$acronym of type array is incompatible with the type string expected by parameter $acronym of Alfs18\User\Functions::getProfileInfo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

693
            $picture = $res->getProfileInfo(/** @scrutinizer ignore-type */ $acronym, $this->di, "User", "picture");
Loading history...
694
695
            $question->picture = "<img src='{$baseURL}/image/profile/{$picture[0]->picture}{$size}'></img>";
696
            // get all answers
697
            $question->answers = $res->getQuestionAnswers($this->di, $questionId);
698
        }
699
700
701
702
        // show all tags as a checkbox form
703
        $form = new FormModelCheckboxMultiple($this->di, $tags2);
704
        $form->check();
705
706
        // get variables from posted checkbox form
707
        $items = $request->getPost();
708
        if ($items["items"] ?? null) {
709
            // visa endast info gällande de ikryssade taggarna
710
            foreach ($items["items"] as $val) {
711
                // var_dump($val);
712
                $questions = $res->getSomeQuestionsTags($this->di, $val);
713
                // var_dump($questions);
714
            }
715
        }
716
717
718
        $page->add("show-all-questions", [
719
            "content" => $formSearch->getHTML(),
720
            "res" => $questions,
721
            "tags" => $tags2,
722
        ]);
723
724
        // $page->add("anax/v2/article/default", [
725
        //     "content" => $form->getHTML(),
726
        // ], "sidebar-right");
727
728
        asort($tags2);
729
        $page->add("tags", [
730
            "tags" => $tags2,
731
        ], "sidebar-right");
732
733
        return $page->render([
734
            "title" => "A create user page",
735
        ]);
736
    }
737
738
739
    /**
740
     * Description.
741
     *
742
     * @param datatype $variable Description
743
     *
744
     * @throws Exception
745
     *
746
     * @return object as a response object
747
     */
748
    public function viewQuestionAction(int $id) : object
749
    {
750
        $page = $this->di->get("page");
751
        $request = $this->di->get("request");
752
753
        // get all questions to show in the view.
754
        $res = new Functions();
755
        $question = $res->getOneQuestion($this->di, $id);
756
        $submit = $request->getPost("submit-sort") ?? null;
757
        $answers = $res->getAnswersOrdered($this->di, $id, "created");
758
        if ($submit) {
759
            $sort = $request->getPost("sort") ?? null;
760
            // var_dump($sort);
761
            $answers = $res->getAnswersOrdered($this->di, $id, $sort);
762
        }
763
        $comments = $res->getQuestionComments($this->di, $id);
764
765
        // get answerId and make an array of all
766
        // comments connected to all answers...?
767
        // Lägg till alla aComments som tillhör $answers[0]
768
        // till $answers.
769
        $aComments = $res->getAnswerComments($this->di, 1);
770
        // $smask = $answers;
771
772
        foreach ($answers as $answer) {
773
            // var_dump($answer->id);
774
            $aComments2 = $res->getAnswerComments($this->di, $answer->id);
775
            $answer->comments = $aComments2;
776
            // $smask->comments = $aComments2;
777
        }
778
        // var_dump($answers);
779
780
        // var_dump($comments);
781
        $qId = $question[0]->id;
782
783
        $user = $_SESSION["acronym"];
784
785
        $form = new CreateCommentsForm($this->di, $user, $qId);
786
        $form->check();
787
788
        $acronym = $res->changeCharacter($question[0]->acronym);
789
        // $aForm = new CreateAnswerCommentsForm($this->di, $acronym, $aId ?? 1);
790
        // $aForm->check();
791
792
        // $textfilter = new TextFilter();
793
        $filter = new \Anax\TextFilter\TextFilter();
794
        // var_dump($form);
795
        $up = "<img src='../../../htdocs/image/up.png' width='30px'></img>";
796
        $down = "<img src='../../../htdocs/image/down.png' width='30px'></img>";
797
798
799
800
        $page->add("view-question", [
801
            "res" => $question,
802
            "answers" => $answers,
803
            "qComments" => $comments,
804
            "aComments" => $aComments,
805
            "qComForm" => $form->getHTML(),
806
            // "aComForm" => $aForm->getHTML(),
807
            "acronym" => $acronym,
808
            "filter" => $filter,
809
            "up" => $up,
810
            "down" => $down,
811
            "user" => $user,
812
            "qId" => $id,
813
        ]);
814
815
        $tags = $question[0]->tags ?? "Grönsaker, Plantor";
816
        $tagsArray = explode("; ", $tags);
817
818
        $page->add("show-tags", [
819
            "res" => $tagsArray,
820
        ], "sidebar-right");
821
822
        $pic = [
823
            // "content" => "<img src='../../../htdocs/image/snail2.jpg' width='1100px'></img>",
824
            "content" => "<h1>Allt om trädgård</h1>",
825
        ];
826
827
        $page->add("anax/v2/article/default", $pic, "flash");
828
829
        $content = "<h2>Info</h2><p>Betygsätt en fråga, svar eller kommentar genom att klicka pil upp, om du tyckte den var bra, eller pil ner, om den var dålig.</p>";
830
        $page->add("anax/v2/article/default", [
831
            "content" => $content,
832
        ], "sidebar-right");
833
834
        $page->add("ask-tip", [], "sidebar-right");
835
836
        return $page->render([
837
            "title" => "View question",
838
        ]);
839
    }
840
841
842
    /**
843
     * Description.
844
     *
845
     * @param datatype $variable Description
846
     *
847
     * @throws Exception
848
     *
849
     * @return object as a response object
850
     */
851
    public function editPostAction(int $id, string $table) : object
852
    {
853
        $page = $this->di->get("page");
854
        $request = $this->di->get("request");
855
        $response = $this->di->get("response");
856
857
        if ($table == "Question") {
858
            $rows = "question, tags";
859
        } elseif ($table == "Answers") {
860
            $rows = "answer";
861
        } else {
862
            $rows = "comment";
863
        }
864
865
        // get the post to update.
866
        $res = new Functions();
867
        $post = $res->getOnePost($this->di, $id, $table, $rows);
868
869
        $submit = $request->getPost("submit") ?? null;
870
        $delete = $request->getPost("delete") ?? null;
871
        if ($submit) {
872
            $altered = $request->getPost("info");
873
            if ($table == "Question") {
874
                $tags = $request->getPost("tags");
875
                $res->updatePost($this->di, $id, $table, "tags", $tags);
876
                $rows = "question";
877
            }
878
            $res->updatePost($this->di, $id, $table, $rows, $altered);
879
            $response->redirect("user/editPost/{$id}/{$table}");
880
        } elseif ($delete) {
881
            if ($table == "Question") {
882
                // remove question
883
                $res->deletePost($this->di, $id, $table);
884
885
                // remove comments
886
                $res->deleteCommentOrAnswer($this->di, $id, "Comments");
887
888
                // get answers
889
                $answers = $res->getQuestionAnswers($this->di, $id);
890
891
                // remove answers
892
                foreach ($answers as $answer) {
893
                    $res->deleteCommentOrAnswer($this->di, $id, "Answers");
894
895
                    // remove answer comments
896
                    $res->deleteAnswerComments($this->di, $answer->id);
897
                }
898
            } elseif ($table == "Answers") {
899
                // remove answer comments
900
                $res->deleteAnswerComments($this->di, $id);
901
            }
902
            $res->deletePost($this->di, $id, $table);
903
904
            $page->add("anax/v2/article/default", [
905
                "content" => "<h2>Inlägg borttaget!</h2>",
906
            ]);
907
908
            return $page->render([
909
                "title" => "Edit",
910
            ]);
911
        }
912
913
        $page->add("edit-post", [
914
            "post" => $post,
915
            "table" => $table,
916
            "rows" => $rows,
917
            // "qId" => $id,
918
        ]);
919
920
        return $page->render([
921
            "title" => "View question",
922
        ]);
923
    }
924
925
926
    /**
927
     * Description.
928
     *
929
     * @param datatype $variable Description
930
     *
931
     * @throws Exception
932
     *
933
     * @return object as a response object
934
     */
935
    public function viewTagQuestionsAction(string $tag) : object
936
    {
937
        $page = $this->di->get("page");
938
        $request = $this->di->get("request");
939
940
        // get all questions to show in the view.
941
        $res = new Functions();
942
        $questions = $res->getSomeQuestions($this->di, $tag);
943
944
        $baseURL = $request->getBaseUrl();
945
        $size = "?width=50&height=50&crop-to-fit&area=0,0,0,0";
946
947
        foreach ($questions as $question) {
948
            $acronym = $res->changeCharacter($question->acronym);
949
            $questionId = $question->id;
950
951
            // get profile picture of the user who made a question
952
            $picture = $res->getProfileInfo($acronym, $this->di, "User", "picture");
0 ignored issues
show
$acronym of type array is incompatible with the type string expected by parameter $acronym of Alfs18\User\Functions::getProfileInfo(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

952
            $picture = $res->getProfileInfo(/** @scrutinizer ignore-type */ $acronym, $this->di, "User", "picture");
Loading history...
953
954
            $question->picture = "<img src='{$baseURL}/image/profile/{$picture[0]->picture}{$size}'></img>";
955
            // get all answers
956
            $question->answers = $res->getQuestionAnswers($this->di, $questionId);
957
        }
958
959
        $filter = new \Anax\TextFilter\TextFilter();
960
961
        $user = $_SESSION["acronym"];
962
963
        $page->add("view-tag-questions", [
964
            "questions" => $questions,
965
            "filter" => $filter,
966
            "user" => $user,
967
            "tag" => $tag,
968
        ]);
969
970
        return $page->render([
971
            "title" => "View question",
972
        ]);
973
    }
974
975
976
    /**
977
     * Description.
978
     *
979
     * @param string $table  Informs if points should be changed
980
     *                      for question/answer/comment.
981
     * @param int $qId       The id of the question previously shown.
982
     * @param int $id       The id of the question/answer/comment.
983
     *
984
     * @throws Exception
985
     *
986
     * @return
987
     */
988
    public function pointsAction(string $table, int $qId, int $id, int $points)
989
    {
990
        // $page = $this->di->get("page");
991
        $response = $this->di->get("response");
992
993
        $res = new Functions();
994
995
        // Get the current points from database.
996
        $currentPoints = $res->getPoints($this->di, $table, $id);
997
998
        // Add new points.
999
        $result = intval($currentPoints->points) + $points;
1000
        $smask = $res->setPoints($this->di, $table, $id, $result);
0 ignored issues
show
The assignment to $smask is dead and can be removed.
Loading history...
1001
1002
        return $response->redirect("user/viewQuestion/{$qId}");
1003
    }
1004
}
1005