|
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
|
|
|
public static function getSessionKey() : string |
|
37
|
|
|
{ |
|
38
|
|
|
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
|
|
|
foreach (glob("{$this->cssDir}/*.css") as $file) { |
|
53
|
|
|
$filename = basename($file); |
|
54
|
|
|
$url = "{$this->cssUrl}/$filename"; |
|
55
|
|
|
$content = file_get_contents($file); |
|
56
|
|
|
$comment = strstr($content, "*/", true); |
|
57
|
|
|
$comment = preg_replace(["#\/\*!#", "#\*#"], "", $comment); |
|
58
|
|
|
$comment = preg_replace("#@#", "<br>@", $comment); |
|
59
|
|
|
$first = strpos($comment, "."); |
|
60
|
|
|
$short = substr($comment, 0, $first + 1); |
|
61
|
|
|
$long = substr($comment, $first + 1); |
|
62
|
|
|
$this->styles[$url] = [ |
|
63
|
|
|
"shortDescription" => $short, |
|
64
|
|
|
"longDescription" => $long, |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($this->styles as $key => $value) { |
|
69
|
|
|
$isMinified = strstr($key, ".min.css", true); |
|
70
|
|
|
if ($isMinified) { |
|
71
|
|
|
unset($this->styles["$isMinified.css"]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Display the stylechooser with details on current selected style. |
|
80
|
|
|
* |
|
81
|
|
|
* @return object |
|
82
|
|
|
*/ |
|
83
|
|
|
public function indexAction() : object |
|
84
|
|
|
{ |
|
85
|
|
|
$title = "Stylechooser"; |
|
86
|
|
|
|
|
87
|
|
|
$page = $this->di->get("page"); |
|
88
|
|
|
$session = $this->di->get("session"); |
|
89
|
|
|
|
|
90
|
|
|
$active = $session->get(self::$key, null); |
|
91
|
|
|
|
|
92
|
|
|
$page->add("anax/stylechooser/index", [ |
|
93
|
|
|
"styles" => $this->styles, |
|
94
|
|
|
"activeStyle" => $active, |
|
95
|
|
|
"activeShortDescription" => $this->styles[$active]["shortDescription"] ?? null, |
|
96
|
|
|
"activeLongDescription" => $this->styles[$active]["longDescription"] ?? null, |
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
return $page->render([ |
|
100
|
|
|
"title" => $title, |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Update current selected style. |
|
108
|
|
|
* |
|
109
|
|
|
* @return object |
|
110
|
|
|
*/ |
|
111
|
|
|
public function updateActionPost() : object |
|
112
|
|
|
{ |
|
113
|
|
|
$response = $this->di->get("response"); |
|
114
|
|
|
$request = $this->di->get("request"); |
|
115
|
|
|
$session = $this->di->get("session"); |
|
116
|
|
|
$key = $request->getPost("stylechooser"); |
|
117
|
|
|
|
|
118
|
|
|
if ($key === "none") { |
|
119
|
|
|
$session->set("flashmessage", "Unsetting the style and using deafult style."); |
|
120
|
|
|
$session->set(self::$key, null); |
|
121
|
|
|
} elseif (array_key_exists($key, $this->styles)) { |
|
122
|
|
|
$session->set("flashmessage", "Using the style '$key'."); |
|
123
|
|
|
$session->set(self::$key, $key); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $response->redirect("style"); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Update current selected style using a GET url and redirect to last |
|
133
|
|
|
* page visited. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $style the key to the style to use. |
|
136
|
|
|
* |
|
137
|
|
|
* @return object |
|
138
|
|
|
*/ |
|
139
|
|
|
public function updateActionGet($style) : object |
|
140
|
|
|
{ |
|
141
|
|
|
$response = $this->di->get("response"); |
|
142
|
|
|
$session = $this->di->get("session"); |
|
143
|
|
|
|
|
144
|
|
|
$key = $this->cssUrl . "/" . $style . ".css"; |
|
145
|
|
|
$keyMin = $this->cssUrl . "/" . $style . ".min.css"; |
|
146
|
|
|
|
|
147
|
|
|
if ($style === "none") { |
|
148
|
|
|
$session->set("flashmessage", "Unsetting the style and using the default style."); |
|
149
|
|
|
$session->set(self::$key, null); |
|
150
|
|
|
} elseif (array_key_exists($keyMin, $this->styles)) { |
|
151
|
|
|
$session->set("flashmessage", "Now using the style '$keyMin'."); |
|
152
|
|
|
$session->set(self::$key, $keyMin); |
|
153
|
|
|
} elseif (array_key_exists($key, $this->styles)) { |
|
154
|
|
|
$session->set("flashmessage", "Now using the style '$key'."); |
|
155
|
|
|
$session->set(self::$key, $key); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$url = $session->getOnce("redirect", "style"); |
|
159
|
|
|
return $response->redirect($url); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|