2121use Ivy \Shared \Core \Path ;
2222use Ivy \Plugin \Infrastructure \Registry \PluginRegistry ;
2323use Ivy \Template \Presentation \Tag \ButtonTag ;
24+ use Symfony \Component \HttpFoundation \Request ;
2425
2526class LatteEngine implements ViewEngineInterface
2627{
2728 private Engine $ latte ;
2829 private AuthService $ auth ;
30+ private Request $ request ;
2931
30-
31- public function __construct ()
32+ public function __construct (AuthService $ auth , Request $ request )
3233 {
3334 $ this ->latte = new Engine ();
34- }
35-
36- public function setAuth (AuthService $ auth ): void
37- {
3835 $ this ->auth = $ auth ;
36+ $ this ->request = $ request ;
3937 }
4038
4139 public function boot (): void
@@ -71,7 +69,7 @@ public function getEngine(): Engine
7169 private function registerFunctions (): void
7270 {
7371 $ this ->latte ->addFunction ('icon ' , fn ($ icon ) =>
74- file_get_contents (Path::get ('MEDIA_PATH ' ) . 'icons/ ' . $ icon )
72+ file_get_contents (Path::get ('MEDIA_PATH ' ) . 'icons/ ' . $ icon )
7573 );
7674
7775 $ this ->latte ->addFunction ('text ' , fn ($ key , $ vars = null ) =>
@@ -89,11 +87,7 @@ private function registerFunctions(): void
8987 );
9088
9189 $ this ->latte ->addFunction ('file ' , fn ($ key ) =>
92- TemplateManager::file ($ key )
93- );
94-
95- $ this ->latte ->addFunction ('render ' , fn ($ key , $ vars = []) =>
96- View::render ($ key , $ vars )
90+ TemplateManager::file ($ key )
9791 );
9892
9993 $ this ->latte ->addFunction ('info ' , fn ($ key ) =>
@@ -109,75 +103,99 @@ private function registerFunctions(): void
109103 );
110104
111105 $ this ->latte ->addFunction ('isPluginActive ' , fn (string $ key ): bool =>
112- PluginRegistry::isActive ($ key )
106+ PluginRegistry::isActive ($ key )
113107 );
114108
115109 $ this ->latte ->addFunction ('csrf ' , fn () =>
116- new Html ('<input type="hidden" name="csrf_token" value=" ' . CsrfManager::token () . '"> ' )
110+ new Html ('<input type="hidden" name="csrf_token" value=" ' . CsrfManager::token () . '"> ' )
117111 );
118112
119113 $ this ->latte ->addFunction ('canEditAsEditor ' , fn () =>
120- $ this ->auth ->canEditAsEditor ()
114+ $ this ->auth ->canEditAsEditor ()
121115 );
122116
123117 $ this ->latte ->addFunction ('canEditAsAdmin ' , fn () =>
124- $ this ->auth ->canEditAsAdmin ()
118+ $ this ->auth ->canEditAsAdmin ()
125119 );
126120
127121 $ this ->latte ->addFunction ('canEditAsSuperAdmin ' , fn () =>
128- $ this ->auth ->canEditAsSuperAdmin ()
122+ $ this ->auth ->canEditAsSuperAdmin ()
129123 );
130124
131125 $ this ->latte ->addFunction ('doesUserHaveRole ' , fn ($ user , $ role ) =>
132- $ this ->auth ->auth ()->admin ()->doesUserHaveRole ($ user , $ role )
126+ $ this ->auth ->auth ()->admin ()->doesUserHaveRole ($ user , $ role )
133127 );
134128
135129 $ this ->latte ->addFunction ('authRoles ' , fn () =>
136- $ this ->auth ->auth ()->getRoles ()
130+ $ this ->auth ->auth ()->getRoles ()
137131 );
138132
139133 $ this ->latte ->addFunction ('authUser ' , fn () =>
140- $ this ->auth ->authUser ()
134+ $ this ->auth ->authUser ()
141135 );
142136
143137 $ this ->latte ->addFunction ('authProfile ' , fn () =>
144- $ this ->auth ->authProfile ()
138+ $ this ->auth ->authProfile ()
145139 );
146140
147141 $ this ->latte ->addFunction ('isLoggedIn ' , fn () =>
148- $ this ->auth ->isLoggedIn ()
142+ $ this ->auth ->isLoggedIn ()
149143 );
150144
151145 $ this ->latte ->addFunction ('can ' , fn ($ action , $ model ) =>
152- (bool ) $ this ->auth ->can ($ action , $ model )
146+ (bool ) $ this ->auth ->can ($ action , $ model )
153147 );
154148
155149 $ this ->latte ->addFunction ('hook ' , fn ($ key ) =>
156- HookManager::do ($ key )
150+ HookManager::do ($ key )
157151 );
158152
159153 $ this ->latte ->addFunction ('csp ' , fn () =>
160- SecurityManager::getNonce ()
154+ SecurityManager::getNonce ()
161155 );
162156
163157 $ this ->latte ->addFunction ('cssFiles ' , fn () =>
164- AssetManager::getCSS ()
158+ AssetManager::getCSS ()
165159 );
166160
167161 $ this ->latte ->addFunction ('jsFiles ' , fn () =>
168- AssetManager::getJS ()
162+ AssetManager::getJS ()
169163 );
170164
171165 $ this ->latte ->addFunction ('moduleFiles ' , fn () =>
172- AssetManager::getModules ()
166+ AssetManager::getModules ()
173167 );
174168
175- $ this ->latte ->addFunction ('datetime ' , fn () =>
176- Carbon::class
169+ $ this ->latte ->addFunction ('now ' , fn () => Carbon::now ());
170+
171+ $ this ->latte ->addFunction ('parseDate ' , fn ($ v ) => Carbon::parse ($ v ));
172+
173+ $ this ->latte ->addFunction ('monthName ' , fn ($ i ) =>
174+ Carbon::create ()->month ($ i )->format ('F ' )
177175 );
178176
179- $ this ->latte ->addFunction ('value ' , fn ($ key , $ default = null ) => $ key ?? $ default
177+ $ this ->latte ->addFunction ('value ' , fn ($ key , $ default = null ) =>
178+ $ key ?? $ default
180179 );
180+
181+ $ this ->latte ->addFunction ('sortUrl ' , function (string $ column ) {
182+
183+ $ query = $ this ->request ->query ->all ();
184+
185+ $ currentSort = $ query ['sort ' ] ?? null ;
186+ $ currentDirection = $ query ['direction ' ] ?? 'asc ' ;
187+
188+ $ query ['sort ' ] = $ column ;
189+
190+ $ query ['direction ' ] =
191+ ($ currentSort === $ column && $ currentDirection === 'asc ' )
192+ ? 'desc '
193+ : 'asc ' ;
194+
195+ unset($ query ['page ' ]);
196+
197+ return '? ' . http_build_query ($ query );
198+ });
181199 }
182200
183201 private function registerExtensions (): void
@@ -187,13 +205,6 @@ private function registerExtensions(): void
187205
188206 private function registerProviders (): void
189207 {
190- $ this ->latte ->addProvider ('customButtonRender ' , function ($ args ) {
191- if ($ file = TemplateManager::file ('buttons/button. ' . $ args ['type ' ] . '.latte ' )) {
192- $ this ->latte ->render ($ file , $ args );
193- return ;
194- }
195-
196- throw new \Exception ("Button template for type ' {$ args ['type ' ]}' not found. " );
197- });
208+
198209 }
199210}
0 commit comments