Issues (20)

src/StyleChooser/StyleChooserController.php (1 issue)

Severity
1
<?php
2
3
namespace Anax\StyleChooser;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Style chooser controller loads available stylesheets from a directory and
10
 * lets the user choose the stylesheet to use.
11
 */
12
class StyleChooserController implements ContainerInjectableInterface
13
{
14
    use ContainerInjectableTrait;
15
16
17
18
    /**
19
     * @var string $cssUrl The baseurl to where the css files are.
20
     * @var string $cssDir The path to the directory storing css files.
21
     * @var array  $styles The styles available in the style directory.
22
     * @var string $key    The session key used to store the active style.
23
     */
24
    private $cssUrl = "css";
25
    private $cssDir = ANAX_INSTALL_PATH . "/htdocs/css";
26
    private $styles = [];
27
    private static $key = "AnaxStyleChooser";
28
29
30
31
    /**
32
     * Get the session key to use to retrieve the active stylesheet.
33
     *
34
     * @return string
35
     */
36 2
    public static function getSessionKey() : string
37
    {
38 2
        return self::$key;
39
    }
40
41
42
43
    /**
44
     * The initialize method is optional and will always be called before the
45
     * target method/action. This is a convienient method where you could
46
     * setup internal properties that are commonly used by several methods.
47
     *
48
     * @return void
49
     */
50
    public function initialize() : void
51
    {
52
        $value = "";
0 ignored issues
show
The assignment to $value is dead and can be removed.
Loading history...
53
        foreach (glob("{$this->cssDir}/*.css") as $file) {
54
            $filename = basename($file);
55
            $url = "{$this->cssUrl}/$filename";
56
            $content = file_get_contents($file);
57
            $comment = strstr($content, "*/", true);
58
            $comment = preg_replace(["#\/\*!#", "#\*#"], "", $comment);
59
            $comment = preg_replace("#@#", "<br>@", $comment);
60
            $first = strpos($comment, ".");
61
            $short = substr($comment, 0, $first + 1);
62
            $long = substr($comment, $first + 1);
63
            $this->styles[$url] = [
64
                "shortDescription" => $short,
65
                "longDescription" => $long,
66
            ];
67
        }
68
69
        foreach ($this->styles as $key => $value) {
70
            $isMinified = strstr($key, ".min.css", true);
71
            if ($isMinified) {
72
                unset($this->styles["$isMinified.css"]);
73
            }
74
        }
75
    }
76
77
78
79
    /**
80
     * Display the stylechooser with details on current selected style.
81
     *
82
     * @return object
83
     */
84
    public function indexAction() : object
85
    {
86
        $title = "Stylechooser";
87
88
        $page = $this->di->get("page");
89
        $session = $this->di->get("session");
90
91
        $active = $session->get(self::$key, null);
92
93
        $page->add("anax/stylechooser/index", [
94
            "styles" => $this->styles,
95
            "activeStyle" => $active,
96
            "activeShortDescription" => $this->styles[$active]["shortDescription"] ?? null,
97
            "activeLongDescription" => $this->styles[$active]["longDescription"] ?? null,
98
        ]);
99
100
        return $page->render([
101
            "title" => $title,
102
        ]);
103
    }
104
105
106
107
    /**
108
     * Update current selected style.
109
     *
110
     * @return object
111
     */
112
    public function updateActionPost() : object
113
    {
114
        $response = $this->di->get("response");
115
        $request = $this->di->get("request");
116
        $session = $this->di->get("session");
117
        $key = $request->getPost("stylechooser");
118
119
        if ($key === "none") {
120
            $session->set("flashmessage", "Unsetting the style and using deafult style.");
121
            $session->set(self::$key, null);
122
        } elseif (array_key_exists($key, $this->styles)) {
123
            $session->set("flashmessage", "Using the style '$key'.");
124
            $session->set(self::$key, $key);
125
        }
126
127
        return $response->redirect("style");
128
    }
129
130
131
132
    /**
133
     * Update current selected style using a GET url and redirect to last
134
     * page visited.
135
     *
136
     * @param string $style the key to the style to use.
137
     *
138
     * @return object
139
     */
140
    public function updateActionGet($style) : object
141
    {
142
        $response = $this->di->get("response");
143
        $session = $this->di->get("session");
144
145
        $key = $this->cssUrl . "/" . $style . ".css";
146
        $keyMin = $this->cssUrl . "/" . $style . ".min.css";
147
148
        if ($style === "none") {
149
            $session->set("flashmessage", "Unsetting the style and using the default style.");
150
            $session->set(self::$key, null);
151
        } elseif (array_key_exists($keyMin, $this->styles)) {
152
            $session->set("flashmessage", "Now using the style '$keyMin'.");
153
            $session->set(self::$key, $keyMin);
154
        } elseif (array_key_exists($key, $this->styles)) {
155
            $session->set("flashmessage", "Now using the style '$key'.");
156
            $session->set(self::$key, $key);
157
        }
158
159
        $url = $session->getOnce("redirect", "style");
160
        return $response->redirect($url);
161
    }
162
}
163