| 1 | package Dancer::Plugin::Interchange6::Routes; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin; | ||
| 5 | use Dancer::Plugin::Interchange6; | ||
| 6 | use Dancer::Plugin::Interchange6::Routes::Account; | ||
| 7 | use Dancer::Plugin::Interchange6::Routes::Cart; | ||
| 8 | use Dancer::Plugin::Interchange6::Routes::Checkout; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Dancer::Plugin::Interchange6::Routes - Routes for Interchange6 Shop Machine | ||
| 13 | |||
| 14 | =head2 ROUTES | ||
| 15 | |||
| 16 | The following routes are provided by this plugin. | ||
| 17 | |||
| 18 | Active routes are automatically installed by the C<shop_setup_routes> keyword: | ||
| 19 | |||
| 20 | =over 4 | ||
| 21 | |||
| 22 | =item cart (C</cart>) | ||
| 23 | |||
| 24 | Route for displaying and updating the cart. | ||
| 25 | |||
| 26 | =item checkout (C</checkout>) | ||
| 27 | |||
| 28 | Route for the checkout process (not B<active> and not recommended). | ||
| 29 | |||
| 30 | =item login (C</login>) | ||
| 31 | |||
| 32 | Login route. | ||
| 33 | |||
| 34 | =item logout (C</logout>) | ||
| 35 | |||
| 36 | Logout route. | ||
| 37 | |||
| 38 | =item navigation | ||
| 39 | |||
| 40 | Route for displaying navigation pages, for example | ||
| 41 | categories and menus. | ||
| 42 | |||
| 43 | The number of products shown on the navigation page can | ||
| 44 | be configured with the C<records> option: | ||
| 45 | |||
| 46 | plugins: | ||
| 47 | Interchange6::Routes: | ||
| 48 | navigation: | ||
| 49 | records: 20 | ||
| 50 | |||
| 51 | =item product | ||
| 52 | |||
| 53 | Route for displaying products. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =head2 CONFIGURATION | ||
| 58 | |||
| 59 | The template for each route type can be configured: | ||
| 60 | |||
| 61 | plugins: | ||
| 62 | Interchange6::Routes: | ||
| 63 | account: | ||
| 64 | login: | ||
| 65 | template: login | ||
| 66 | uri: login | ||
| 67 | success_uri: | ||
| 68 | logout: | ||
| 69 | template: logout | ||
| 70 | uri: logout | ||
| 71 | cart: | ||
| 72 | template: cart | ||
| 73 | uri: cart | ||
| 74 | active: 1 | ||
| 75 | checkout: | ||
| 76 | template: checkout | ||
| 77 | uri: checkout | ||
| 78 | active: 0 | ||
| 79 | navigation: | ||
| 80 | template: listing | ||
| 81 | records: 0 | ||
| 82 | product: | ||
| 83 | template: product | ||
| 84 | |||
| 85 | This sample configuration shows the current defaults. | ||
| 86 | |||
| 87 | =head2 HOOKS | ||
| 88 | |||
| 89 | The following hooks are available to manipulate the values | ||
| 90 | passed to the templates: | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item before_product_display | ||
| 95 | |||
| 96 | The hook sub receives a hash reference, where the Product object | ||
| 97 | is the value of the C<product> key. | ||
| 98 | |||
| 99 | =item before_cart_display | ||
| 100 | |||
| 101 | =item before_checkout_display | ||
| 102 | |||
| 103 | =item before_navigation_search | ||
| 104 | |||
| 105 | This hook is called if a navigation uri is requested and before product search | ||
| 106 | queries are generated. | ||
| 107 | |||
| 108 | The hook sub receives the navigation data as hash reference: | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item navigation | ||
| 113 | |||
| 114 | Navigation object. | ||
| 115 | |||
| 116 | =item page | ||
| 117 | |||
| 118 | Page number found at end of URI or 1 if no page number found. | ||
| 119 | |||
| 120 | =item template | ||
| 121 | |||
| 122 | Name of template. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | The navigation hash reference can be modified inside the hook and all changes | ||
| 127 | will be visible to the navigation route (and also the template) after the hook | ||
| 128 | returns. | ||
| 129 | |||
| 130 | =item before_navigation_display | ||
| 131 | |||
| 132 | The hook sub receives the navigation data as hash reference: | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item navigation | ||
| 137 | |||
| 138 | Navigation object. | ||
| 139 | |||
| 140 | =item products | ||
| 141 | |||
| 142 | Product listing for this navigation item. The product listing is generated | ||
| 143 | using L<Interchange6::Schema::Result::Product/listing>. | ||
| 144 | |||
| 145 | =item pager | ||
| 146 | |||
| 147 | L<Data::Page> object for L</products>. | ||
| 148 | |||
| 149 | To get the full count of products call C<total_entries> on the Data::Page | ||
| 150 | object. | ||
| 151 | |||
| 152 | =item template | ||
| 153 | |||
| 154 | Name of template. In order to use another template, change | ||
| 155 | the value in the hashref. | ||
| 156 | |||
| 157 | hook 'before_navigation_display' => sub { | ||
| 158 | my $navigation_data = shift; | ||
| 159 | |||
| 160 | if ($navigation_data->{navigation}->uri =~ /^admin/) { | ||
| 161 | $navigation_data->{template} = 'admin_listing'; | ||
| 162 | } | ||
| 163 | }; | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =item before_login_display | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =head3 EXAMPLES | ||
| 172 | |||
| 173 | Disable parts of layout on the login view: | ||
| 174 | |||
| 175 | hook 'before_login_display' => sub { | ||
| 176 | my $tokens = shift; | ||
| 177 | |||
| 178 | $tokens->{layout_noleft} = 1; | ||
| 179 | $tokens->{layout_noright} = 1; | ||
| 180 | }; | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | register shop_setup_routes => sub { | ||
| 185 | _setup_routes(); | ||
| 186 | }; | ||
| 187 | |||
| 188 | register_hook (qw/before_product_display before_navigation_search | ||
| 189 | before_navigation_display/); | ||
| 190 | register_plugin; | ||
| 191 | |||
| 192 | our $object_autodetect = 0; | ||
| 193 | |||
| 194 | our %route_defaults = ( | ||
| 195 | account => {login => {template => 'login', | ||
| 196 | uri => 'login', | ||
| 197 | success_uri => '', | ||
| 198 | }, | ||
| 199 | logout => {template => 'logout', | ||
| 200 | uri => 'logout', | ||
| 201 | }, | ||
| 202 | }, | ||
| 203 | cart => {template => 'cart', | ||
| 204 | uri => 'cart', | ||
| 205 | active => 1, | ||
| 206 | }, | ||
| 207 | checkout => {template => 'checkout', | ||
| 208 | uri => 'checkout', | ||
| 209 | active => 0, | ||
| 210 | }, | ||
| 211 | navigation => {template => 'listing', | ||
| 212 | records => 0, | ||
| 213 | }, | ||
| 214 | product => {template => 'product'}, | ||
| 215 | ); | ||
| 216 | |||
| 217 | sub _setup_routes { | ||
| 218 | my $sub; | ||
| 219 | my $plugin_config = plugin_setting; | ||
| 220 | |||
| 221 | # update settings with defaults | ||
| 222 | my $routes_config = _config_routes($plugin_config, \%route_defaults); | ||
| 223 | |||
| 224 | # display warnings | ||
| 225 | _config_warnings($routes_config); | ||
| 226 | |||
| 227 | # check whether template engine has object autodetect | ||
| 228 | if (config->{template} eq 'template_flute') { | ||
| 229 | $object_autodetect = 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | # account routes | ||
| 233 | my $account_routes = Dancer::Plugin::Interchange6::Routes::Account::account_routes($routes_config); | ||
| 234 | |||
| 235 | get '/' . $routes_config->{account}->{login}->{uri} | ||
| 236 | => $account_routes->{login}->{get}; | ||
| 237 | |||
| 238 | post '/' . $routes_config->{account}->{login}->{uri} | ||
| 239 | => $account_routes->{login}->{post}; | ||
| 240 | |||
| 241 | any ['get', 'post'] => '/' . $routes_config->{account}->{logout}->{uri} | ||
| 242 | => $account_routes->{logout}->{any}; | ||
| 243 | |||
| 244 | if ($routes_config->{cart}->{active}) { | ||
| 245 | # routes for cart | ||
| 246 | my $cart_sub = Dancer::Plugin::Interchange6::Routes::Cart::cart_route($routes_config); | ||
| 247 | get '/' . $routes_config->{cart}->{uri} => $cart_sub; | ||
| 248 | post '/' . $routes_config->{cart}->{uri} => $cart_sub; | ||
| 249 | } | ||
| 250 | |||
| 251 | if ($routes_config->{checkout}->{active}) { | ||
| 252 | # routes for checkout | ||
| 253 | my $checkout_sub = Dancer::Plugin::Interchange6::Routes::Checkout::checkout_route($routes_config); | ||
| 254 | get '/' . $routes_config->{checkout}->{uri} => $checkout_sub; | ||
| 255 | post '/' . $routes_config->{checkout}->{uri} => $checkout_sub; | ||
| 256 | } | ||
| 257 | |||
| 258 | # fallback route for flypage and navigation | ||
| 259 | get qr{/(?<path>.+)} => sub { | ||
| 260 | my $path = captures->{'path'}; | ||
| 261 | |||
| 262 | # check for a matching product by uri | ||
| 263 | my $product = shop_product->find({uri => $path}); | ||
| 264 | |||
| 265 | if (!defined $product) { | ||
| 266 | |||
| 267 | # check for a matching product by sku | ||
| 268 | $product = shop_product($path); | ||
| 269 | |||
| 270 | if ($product) { | ||
| 271 | if ($product->uri | ||
| 272 | && $product->uri ne $path) { | ||
| 273 | # permanent redirect to specific URL | ||
| 274 | debug "Redirecting permanently to product uri ", $product->uri, | ||
| 275 | " for $path."; | ||
| 276 | return redirect(uri_for($product->uri), 301); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | if ($product) { | ||
| 282 | if ($product->active) { | ||
| 283 | # flypage | ||
| 284 | my $tokens = {product => $product}; | ||
| 285 | |||
| 286 | execute_hook('before_product_display', $tokens); | ||
| 287 | |||
| 288 | my $output = template $routes_config->{product}->{template}, $tokens; | ||
| 289 | |||
| 290 | # temporary way to erase cart errors from missing variants | ||
| 291 | session shop_cart_error => undef; | ||
| 292 | |||
| 293 | return $output; | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | # discontinued | ||
| 297 | status 'not_found'; | ||
| 298 | forward 404; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | # check for page number | ||
| 303 | my $page; | ||
| 304 | |||
| 305 | if ($path =~ s%/([1-9][0-9]*)$%%) { | ||
| 306 | $page = $1; | ||
| 307 | } | ||
| 308 | else { | ||
| 309 | $page = 1; | ||
| 310 | } | ||
| 311 | |||
| 312 | # first check for navigation item | ||
| 313 | my $nav = shop_navigation->find({uri => $path}); | ||
| 314 | |||
| 315 | if (defined $nav) { | ||
| 316 | |||
| 317 | # navigation item found | ||
| 318 | |||
| 319 | # retrieve navigation attribute for template | ||
| 320 | my $template = $routes_config->{navigation}->{template}; | ||
| 321 | |||
| 322 | if ( my $attr_value = $nav->find_attribute_value('template') ) { | ||
| 323 | debug "Change template name from $template to $attr_value due to navigation attribute."; | ||
| 324 | $template = $attr_value; | ||
| 325 | } | ||
| 326 | |||
| 327 | my $tokens = { | ||
| 328 | navigation => $nav, | ||
| 329 | page => $page, | ||
| 330 | template => $template | ||
| 331 | }; | ||
| 332 | |||
| 333 | execute_hook('before_navigation_search', $tokens); | ||
| 334 | |||
| 335 | # we need to find all our active products before we call | ||
| 336 | # listing method on the resultset and we'll later need | ||
| 337 | # the pager for this resultset | ||
| 338 | |||
| 339 | my $products = | ||
| 340 | $tokens->{navigation} | ||
| 341 | ->navigation_products->search_related('product') | ||
| 342 | ->active->limited_page( $tokens->{page}, | ||
| 343 | $routes_config->{navigation}->{records} ); | ||
| 344 | |||
| 345 | # now get the HRI product listing | ||
| 346 | # alias 'me' refers to navigation_products | ||
| 347 | # NOTE: group_by must always contain product.sku and | ||
| 348 | # inventory.quantity plus all order_by columns | ||
| 349 | |||
| 350 | my $product_listing = | ||
| 351 | $products->listing( { users_id => session('logged_in_user_id') } ) | ||
| 352 | ->group_by( | ||
| 353 | [ | ||
| 354 | 'product.sku', 'inventory.quantity', | ||
| 355 | 'me.priority', 'product.priority' | ||
| 356 | ] | ||
| 357 | )->order_by('!me.priority,!product.priority'); | ||
| 358 | |||
| 359 | if (!$object_autodetect) { | ||
| 360 | $product_listing = [$product_listing->all]; | ||
| 361 | } | ||
| 362 | |||
| 363 | $tokens->{products} = $product_listing; | ||
| 364 | $tokens->{pager} = $products->pager; | ||
| 365 | |||
| 366 | execute_hook('before_navigation_display', $tokens); | ||
| 367 | |||
| 368 | return template $tokens->{template}, $tokens; | ||
| 369 | } | ||
| 370 | |||
| 371 | # display not_found page | ||
| 372 | status 'not_found'; | ||
| 373 | forward 404; | ||
| 374 | }; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub _config_routes { | ||
| 378 | my ($settings, $defaults) = @_; | ||
| 379 | my ($key, $vref, $name, $value, $set_value); | ||
| 380 | |||
| 381 | unless (ref($defaults)) { | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | |||
| 385 | while (($key, $vref) = each %$defaults) { | ||
| 386 | if (exists $settings->{$key}) { | ||
| 387 | # recurse | ||
| 388 | _config_routes($settings->{$key}, $defaults->{$key}); | ||
| 389 | } | ||
| 390 | elsif (! exists $defaults->{$key}) { | ||
| 391 | die "Invalid entry in routes configuration: $key."; | ||
| 392 | } | ||
| 393 | else { | ||
| 394 | $settings->{$key} = $defaults->{$key}; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | return $settings; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub _config_warnings { | ||
| 402 | my ($settings) = @_; | ||
| 403 | |||
| 404 | if ($settings->{navigation}->{records} == 0) { | ||
| 405 | warn __PACKAGE__, ": Maximum number of navigation records is zero.\n"; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; |