| 1 | # First, a dead simple object that can be fed a hashref of params from the | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # Dancer params() keyword, and returns then when its param() method is called, | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # so that we can feed it to CGI::FormBuilder: | ||||||||||||||||||||||||||||||||||||||||||
| 4 | package Dancer::Plugin::SimpleCRUD::ParamsObject; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 0 | ▶ | my ($class, $params) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 0 | return bless { params => $params }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | sub param { | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
param: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # If called with no args, return all param names | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | if (!@args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | return $self->{params} if !$paramname; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # With one arg, act as an accessor | ||||||||||||||||||||||||||||||||||||||||||
| 19 | } elsif (@args == 1) { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | return $self->{params}{ $args[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # With two args, act as a mutator | ||||||||||||||||||||||||||||||||||||||||||
| 23 | } elsif ($args == 2) { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | return $self->{params}{ $args[0] } = $args[1]; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # Now, on to the real stuff | ||||||||||||||||||||||||||||||||||||||||||
| 29 | package Dancer::Plugin::SimpleCRUD; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | use Dancer qw(:syntax); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | use Dancer::Plugin::Database; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | use HTML::Table::FromDatabase; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | use CGI::FormBuilder; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use List::MoreUtils qw( first_index uniq ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | our $VERSION = '1.15'; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | Dancer::Plugin::SimpleCRUD - very simple CRUD (create/read/update/delete) | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | A plugin for Dancer web applications, to use a few lines of code to create | ||||||||||||||||||||||||||||||||||||||||||
| 54 | appropriate routes to support creating/editing/deleting/viewing records within a | ||||||||||||||||||||||||||||||||||||||||||
| 55 | database table. Uses L<CGI::FormBuilder> to generate, process and validate forms, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | L<Dancer::Plugin::Database> for database interaction and | ||||||||||||||||||||||||||||||||||||||||||
| 57 | L<HTML::Table::FromDatabase> to display lists of records. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | Setting up forms and code to display and edit database records is a very common | ||||||||||||||||||||||||||||||||||||||||||
| 60 | requirement in web apps; this plugin tries to make something basic trivially | ||||||||||||||||||||||||||||||||||||||||||
| 61 | easy to set up and use. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | The following assumes that you already have a working L<Dancer> app and have | ||||||||||||||||||||||||||||||||||||||||||
| 67 | put your database connection details in your C<config.yml> to be read by | ||||||||||||||||||||||||||||||||||||||||||
| 68 | L<Dancer::Plugin::Database>, which this plugin uses in order to obtain a database | ||||||||||||||||||||||||||||||||||||||||||
| 69 | connection. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # In your Dancer app, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | use Dancer::Plugin::SimpleCRUD; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # Simple example: | ||||||||||||||||||||||||||||||||||||||||||
| 75 | simple_crud( | ||||||||||||||||||||||||||||||||||||||||||
| 76 | record_title => 'Widget', | ||||||||||||||||||||||||||||||||||||||||||
| 77 | prefix => '/widgets', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | db_table => 'widgets', | ||||||||||||||||||||||||||||||||||||||||||
| 79 | editable => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # The above would create a route to handle C</widgets>, listing all widgets, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # with options to add/edit entries (linking to C</widgets/add> and | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # C</widgets/edit/:id> respectively) where a form to add a new entry or edit | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # an existing entry will be created. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # All fields in the database table would be editable. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # There is also a view route, C</widgets/view/:id>, which shows all the values | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # for the fields of a single database entry. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | # A more in-depth synopsis, using all options (of course, usually you'd only | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # need to use a few of the options where you need to change the default | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # behaviour): | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | simple_crud( | ||||||||||||||||||||||||||||||||||||||||||
| 96 | record_title => 'Team', | ||||||||||||||||||||||||||||||||||||||||||
| 97 | prefix => '/teams', | ||||||||||||||||||||||||||||||||||||||||||
| 98 | db_table => 'team', | ||||||||||||||||||||||||||||||||||||||||||
| 99 | labels => { # More human-friendly labels for some columns | ||||||||||||||||||||||||||||||||||||||||||
| 100 | venue_id => 'Home Venue', | ||||||||||||||||||||||||||||||||||||||||||
| 101 | name => 'Team Name', | ||||||||||||||||||||||||||||||||||||||||||
| 102 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | validation => { # validate values entered for some columns | ||||||||||||||||||||||||||||||||||||||||||
| 104 | division => qr/\d+/, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | input_types => { # overriding form input type for some columns | ||||||||||||||||||||||||||||||||||||||||||
| 107 | supersecret => 'password', | ||||||||||||||||||||||||||||||||||||||||||
| 108 | lotsoftext' => 'textarea', | ||||||||||||||||||||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | key_column => 'id', # id is default anyway | ||||||||||||||||||||||||||||||||||||||||||
| 111 | editable_columns => [ qw( venue_id name division ) ], | ||||||||||||||||||||||||||||||||||||||||||
| 112 | display_columns => [ qw( id venue_id name division ) ], | ||||||||||||||||||||||||||||||||||||||||||
| 113 | deleteable => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | editable => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | addable => 0, # does not allow adding rows | ||||||||||||||||||||||||||||||||||||||||||
| 116 | sortable => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | paginate => 300, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | template => 'simple_crud.tt', | ||||||||||||||||||||||||||||||||||||||||||
| 119 | query_auto_focus => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | downloadable => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | foreign_keys => { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | columnname => { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | table => 'venues', | ||||||||||||||||||||||||||||||||||||||||||
| 124 | key_column => 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 125 | label_column => 'name', | ||||||||||||||||||||||||||||||||||||||||||
| 126 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | table_class => 'table table-bordered', | ||||||||||||||||||||||||||||||||||||||||||
| 129 | paginate_table_class => 'table table-borderless', | ||||||||||||||||||||||||||||||||||||||||||
| 130 | custom_columns => [ | ||||||||||||||||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | name => "division_news", | ||||||||||||||||||||||||||||||||||||||||||
| 133 | raw_column => "division", | ||||||||||||||||||||||||||||||||||||||||||
| 134 | transform => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 135 | my $division_name = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 136 | my $label = "News about $division_name"; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | $division_name =~ s/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg; | ||||||||||||||||||||||||||||||||||||||||||
| 138 | my $search = qq{http://news.google.com/news?q="$division_name"}; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | return "<a href='$search'>$label</a>"; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | column_class => "column-class", | ||||||||||||||||||||||||||||||||||||||||||
| 142 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 144 | auth => { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | view => { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | require_login => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 147 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | edit => { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | require_role => 'Admin', | ||||||||||||||||||||||||||||||||||||||||||
| 150 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | This plugin provides a C<simple_crud> keyword, which takes a hash of options as | ||||||||||||||||||||||||||||||||||||||||||
| 159 | described below, and sets up the appropriate routes to present add/edit/delete | ||||||||||||||||||||||||||||||||||||||||||
| 160 | options. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | The options you can pass to simple_crud are: | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | =item C<record_title> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | What we're editing, for instance, if you're editing widgets, use 'Widget'. Will | ||||||||||||||||||||||||||||||||||||||||||
| 171 | be used in form titles (for instance "Add a ...", "Edit ..."), and button | ||||||||||||||||||||||||||||||||||||||||||
| 172 | labels. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =item C<prefix> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | The prefix for the routes which will be created. Given a prefix of C</widgets>, | ||||||||||||||||||||||||||||||||||||||||||
| 177 | then you can go to C</widgets/new> to create a new Widget, and C</widgets/42> to | ||||||||||||||||||||||||||||||||||||||||||
| 178 | edit the widget with the ID (see key_column) 42. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | Don't confuse this with Dancer's C<prefix> setting, which would be prepended | ||||||||||||||||||||||||||||||||||||||||||
| 181 | before the prefix you pass to this plugin. For example, if you used: | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | prefix '/foo'; | ||||||||||||||||||||||||||||||||||||||||||
| 184 | simple_crud( | ||||||||||||||||||||||||||||||||||||||||||
| 185 | prefix => 'bar', | ||||||||||||||||||||||||||||||||||||||||||
| 186 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | ... then you'd end up with e.g. C</foo/bar> as the record listing page. | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =item C<db_table> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | The name of the database table. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | =item C<key_column> (optional, default: 'id') | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | Specify which column in the table is the primary key. If not given, defaults to | ||||||||||||||||||||||||||||||||||||||||||
| 198 | id. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =item C<where_filter> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | Specify one or more 'where' clauses to use to filter the table. For example: | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | simple_crud( | ||||||||||||||||||||||||||||||||||||||||||
| 205 | prefix => 'bar', | ||||||||||||||||||||||||||||||||||||||||||
| 206 | where_filter => {user_id => 1000}, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | This would cause only rows with an user_id of 1000 to be displayed in listings | ||||||||||||||||||||||||||||||||||||||||||
| 211 | and search results, viewed, edited etc. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | The C<where_filter> parameter takes a hashref describing the WHERE clause, as | ||||||||||||||||||||||||||||||||||||||||||
| 214 | used by L<Dancer::Plugin::Database>'s C<quick_select> convenience method for | ||||||||||||||||||||||||||||||||||||||||||
| 215 | example - see the | ||||||||||||||||||||||||||||||||||||||||||
| 216 | L<where clause documentation in Dancer::Plugin::Database::Core::Handle|Dancer::Plugin::Database::Core::Handle/WHERE-clauses-as-hashrefs>. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | Alternatively, if the filter condition needs to be calculated at runtime (for | ||||||||||||||||||||||||||||||||||||||||||
| 219 | example, based on the logged in user calling it), then you can provide a coderef | ||||||||||||||||||||||||||||||||||||||||||
| 220 | which returns the WHERE clause hashref - for instance: | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | where_filter => sub { { customer_id => logged_in_user()->{customer_id} } }, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | =item C<db_connection_name> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | We use L<Dancer::Plugin::Database> to obtain database connections. This option | ||||||||||||||||||||||||||||||||||||||||||
| 227 | allows you to specify the name of a connection defined in the config file to | ||||||||||||||||||||||||||||||||||||||||||
| 228 | use. See the documentation for L<Dancer::Plugin::Database> for how multiple | ||||||||||||||||||||||||||||||||||||||||||
| 229 | database configurations work. If this is not supplied or is empty, the default | ||||||||||||||||||||||||||||||||||||||||||
| 230 | database connection details in your config file will be used - this is often | ||||||||||||||||||||||||||||||||||||||||||
| 231 | what you want, so unless your app is dealing with multiple DBs, you probably | ||||||||||||||||||||||||||||||||||||||||||
| 232 | won't need to worry about this option. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | =item C<labels> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | A hashref of field_name => 'Label', if you want to provide more user-friendly | ||||||||||||||||||||||||||||||||||||||||||
| 237 | labels for some or all fields. As we're using CGI::FormBuilder, it will do a | ||||||||||||||||||||||||||||||||||||||||||
| 238 | reasonable job of figuring these out for itself usually anyway - for instance, a | ||||||||||||||||||||||||||||||||||||||||||
| 239 | field named C<first_name> will be shown as C<First Name>. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | =item C<input_types> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | A hashref of field_name => input type, if you want to override the default type | ||||||||||||||||||||||||||||||||||||||||||
| 244 | of input which would be selected by L<CGI::FormBuilder> or by our DWIMmery (by | ||||||||||||||||||||||||||||||||||||||||||
| 245 | default, password fields will be used for field names like 'password', 'passwd' | ||||||||||||||||||||||||||||||||||||||||||
| 246 | etc, and text area inputs will be used for columns with type 'TEXT'). | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | Valid values include anything allowed by HTML, e.g. C<text>, C<select>, | ||||||||||||||||||||||||||||||||||||||||||
| 249 | C<textarea>, C<radio>, C<checkbox>, C<password>, C<hidden>. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | input_types => { | ||||||||||||||||||||||||||||||||||||||||||
| 254 | first_name => 'text', | ||||||||||||||||||||||||||||||||||||||||||
| 255 | secret => 'password', | ||||||||||||||||||||||||||||||||||||||||||
| 256 | gender => 'radio', | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | =item C<validation> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | A hashref of field_name => validation criteria which should be passed to | ||||||||||||||||||||||||||||||||||||||||||
| 262 | L<CGI::FormBuilder>. | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | validation => { | ||||||||||||||||||||||||||||||||||||||||||
| 267 | email_address => 'EMAIL', | ||||||||||||||||||||||||||||||||||||||||||
| 268 | age => '/^\d+$/', | ||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =item C<message> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | A hashref of field_name => messages to show if validation failed. | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | Default is "Invalid entry". | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | message => { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | age => 'Please enter your age in years', | ||||||||||||||||||||||||||||||||||||||||||
| 282 | email => 'That is not a valid email address', | ||||||||||||||||||||||||||||||||||||||||||
| 283 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =item C<jsmessage> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | A hashref of field_name => message to show when Javascript validation fails. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | Default message is "- Invalid entry for the "$fieldname" field". See above for | ||||||||||||||||||||||||||||||||||||||||||
| 290 | example. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | =item C<sort_options> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | A hashref of field_name => optionspec indicating how select options should be sorted | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | This is currently a passthrough to L<CGI::FormBuilder>'s L<sortopts|CGI::FormBuilder/sortopts>. There are several | ||||||||||||||||||||||||||||||||||||||||||
| 297 | built-in values: | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | NAME Sort option values by name | ||||||||||||||||||||||||||||||||||||||||||
| 300 | NUM Sort option values numerically | ||||||||||||||||||||||||||||||||||||||||||
| 301 | LABELNAME Sort option labels by name | ||||||||||||||||||||||||||||||||||||||||||
| 302 | LABELNUM Sort option labels numerically | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | See the documentation for L<CGI::FormBuilder/sortopts> for more. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =item C<acceptable_values> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | A hashref of arrayrefs to declare that certain fields can take only a set of | ||||||||||||||||||||||||||||||||||||||||||
| 309 | acceptable values. | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | acceptable_values => { | ||||||||||||||||||||||||||||||||||||||||||
| 314 | gender => ['Male', 'Female'], | ||||||||||||||||||||||||||||||||||||||||||
| 315 | status => [qw(Alive Dead Zombie Unknown)], | ||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | You can automatically create option groups (on a field of type C<select>) by specifying | ||||||||||||||||||||||||||||||||||||||||||
| 319 | the acceptable values in CGI::FormBuilder's C<[value, label, category]> format, like this: | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | acceptable_values => { | ||||||||||||||||||||||||||||||||||||||||||
| 322 | gender => ['Male', 'Female'], | ||||||||||||||||||||||||||||||||||||||||||
| 323 | status => [qw(Alive Dead Zombie Unknown)], | ||||||||||||||||||||||||||||||||||||||||||
| 324 | threat_level => [ | ||||||||||||||||||||||||||||||||||||||||||
| 325 | [ 'child_puke', 'Regurgitation', 'Child'], | ||||||||||||||||||||||||||||||||||||||||||
| 326 | [ 'child_knee', 'Knee Biter', 'Child'], | ||||||||||||||||||||||||||||||||||||||||||
| 327 | [ 'teen_eye', 'Eye Roll', 'Adolescent'], | ||||||||||||||||||||||||||||||||||||||||||
| 328 | [ 'teen_lip', 'Withering Sarcasm', 'Adolescent'], | ||||||||||||||||||||||||||||||||||||||||||
| 329 | [ 'adult_silent', 'Pointedly Ignore', 'Adult'], | ||||||||||||||||||||||||||||||||||||||||||
| 330 | [ 'adult_freak', 'Become Very Put Out', 'Adult'], | ||||||||||||||||||||||||||||||||||||||||||
| 331 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | If you are letting FormBuilder choose the field type, you won't see these categories | ||||||||||||||||||||||||||||||||||||||||||
| 335 | unless you have enough options that it makes the field into a select. If you want to | ||||||||||||||||||||||||||||||||||||||||||
| 336 | see the categories all the time, you can use the L</input_types> option to force your | ||||||||||||||||||||||||||||||||||||||||||
| 337 | field to be rendered as a select. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =item C<default_value> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | A hashref of default values to have pre-selected on the add form. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | default_value => { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | gender => 'Female', | ||||||||||||||||||||||||||||||||||||||||||
| 347 | status => 'Unknown', | ||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | =item C<editable_columns> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | Specify an arrayref of fields which the user can edit. By default, this is all | ||||||||||||||||||||||||||||||||||||||||||
| 354 | columns in the database table, with the exception of the key column. | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | =item C<not_editable_columns> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | Specify an arrayref of fields which should not be editable. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =item C<required> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | Specify an arrayref of fields which must be completed. If this is not provided, | ||||||||||||||||||||||||||||||||||||||||||
| 363 | DWIMmery based on whether the field is set to allow null values in the database | ||||||||||||||||||||||||||||||||||||||||||
| 364 | will be used - i.e. if that column can contain null, then it doesn't have to be | ||||||||||||||||||||||||||||||||||||||||||
| 365 | completed, otherwise, it does. | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | =item C<deletable> | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | Specify whether to support deleting records. If set to a true value, a route | ||||||||||||||||||||||||||||||||||||||||||
| 370 | will be created for C</prefix/delete/:id> to delete the record with the ID | ||||||||||||||||||||||||||||||||||||||||||
| 371 | given, and the edit form will have a "Delete $record_title" button. | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | =item C<editable> | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | Specify whether to support editing records. Defaults to true. If set to a | ||||||||||||||||||||||||||||||||||||||||||
| 376 | false value, it will not be possible to add or edit rows in the table. See also C<addable>. | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =item C<addable> | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | Specify whether to support adding records. Defaults to the value of C<editable> if set, | ||||||||||||||||||||||||||||||||||||||||||
| 381 | or true otherwise. If set to a false value, it will not be possible to add rows in the table. | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | =item C<sortable> | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | Specify whether to support sorting the table. Defaults to false. If set to a | ||||||||||||||||||||||||||||||||||||||||||
| 386 | true value, column headers will become clickable, allowing the user to sort | ||||||||||||||||||||||||||||||||||||||||||
| 387 | the output by each column, and with ascending/descending order. | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =item C<paginate> | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | Specify whether to show results in pages (with next/previous buttons). Defaults | ||||||||||||||||||||||||||||||||||||||||||
| 392 | to undef, meaning all records are shown on one page (not useful for large | ||||||||||||||||||||||||||||||||||||||||||
| 393 | tables). When defined as a number, only this number of results will be shown. | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | =item C<display_columns> | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | Specify an arrayref of columns that should show up in the list. Defaults to all. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =item C<template> | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | Specify a template that will be applied to all output. This template must have | ||||||||||||||||||||||||||||||||||||||||||
| 402 | a "simple_crud" placeholder defined or you won't get any output. This template | ||||||||||||||||||||||||||||||||||||||||||
| 403 | must be located in your "views" directory. | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | Any global layout will be applied automatically because this option causes the | ||||||||||||||||||||||||||||||||||||||||||
| 406 | module to use the C<template> keyword. If you don't use this option, the | ||||||||||||||||||||||||||||||||||||||||||
| 407 | C<template> keyword is not used, which implies that any | ||||||||||||||||||||||||||||||||||||||||||
| 408 | C<before_template_render> and C<after_template_render> hooks won't be called. | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | =item C<query_auto_focus> | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | Specify whether to automatically set input focus to the query input field. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | Defaults to true. If set to a false value, focus will not be set. | ||||||||||||||||||||||||||||||||||||||||||
| 414 | The focus is set using a simple inlined javascript. | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | =item C<downloadable> | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | Specify whether to support downloading the results. Defaults to false. If set | ||||||||||||||||||||||||||||||||||||||||||
| 419 | to a true value, The results show on the HTML page can be downloaded as | ||||||||||||||||||||||||||||||||||||||||||
| 420 | CSV/TSV/JSON/XML. The download links will appear at the top of the page. | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | =item C<foreign_keys> | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | A hashref to specify columns in the table which are foreign keys; for each one, | ||||||||||||||||||||||||||||||||||||||||||
| 425 | the value should be a hashref containing the keys C<table>, C<key_column> and | ||||||||||||||||||||||||||||||||||||||||||
| 426 | C<label_column>. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =item C<custom_columns> | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | An arrayref of hashrefs to specify custom columns to appear in the list view | ||||||||||||||||||||||||||||||||||||||||||
| 431 | of an entity. (Previously, this was just a hashref of column names and specs, | ||||||||||||||||||||||||||||||||||||||||||
| 432 | and this style is still supported for backwards compatibility, but is deprecated | ||||||||||||||||||||||||||||||||||||||||||
| 433 | because it leaves the order of the columns unpredictable.) | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | The keys of each hash are C<name>, the name to use for this custom column, | ||||||||||||||||||||||||||||||||||||||||||
| 436 | C<raw_column> indicating a column from the table that should be selected to | ||||||||||||||||||||||||||||||||||||||||||
| 437 | build the custom column from, C<transform>, a subref to be used as a | ||||||||||||||||||||||||||||||||||||||||||
| 438 | HTML::Table::FromDatabase callback on the resulting column, and C<column_class>, | ||||||||||||||||||||||||||||||||||||||||||
| 439 | to specify a CSS class for the the column. C<column_class> is optional, and if | ||||||||||||||||||||||||||||||||||||||||||
| 440 | no C<transform> is provided, sub { return shift; } will be used. | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | If your custom column has the same name as an existing column, your customizations | ||||||||||||||||||||||||||||||||||||||||||
| 443 | will be used in-place to override the display of the content in that column. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | If sorting is enabled, the column will be sorted by the | ||||||||||||||||||||||||||||||||||||||||||
| 445 | underlying database content for that row, and not by the output of your transform function. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | For a somewhat spurious example: | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 450 | custom_columns => [ | ||||||||||||||||||||||||||||||||||||||||||
| 451 | { | ||||||||||||||||||||||||||||||||||||||||||
| 452 | name => 'email_provider', | ||||||||||||||||||||||||||||||||||||||||||
| 453 | raw_column => 'email', | ||||||||||||||||||||||||||||||||||||||||||
| 454 | transform => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 455 | my $value = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 456 | return (split /@/, 1)[1]; | ||||||||||||||||||||||||||||||||||||||||||
| 457 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 458 | column_class => 'column-class', | ||||||||||||||||||||||||||||||||||||||||||
| 459 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 460 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 461 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | The C<transform> code ref is passed to L<HTML::Table::FromDatabase> as a | ||||||||||||||||||||||||||||||||||||||||||
| 465 | callback for that column, so it can do anything a | ||||||||||||||||||||||||||||||||||||||||||
| 466 | L<HTML::Table::FromDatabase callback|HTML::Table::FromDatabase/CALLBACKS> | ||||||||||||||||||||||||||||||||||||||||||
| 467 | can do. In particular, the coderef will receive the value of the | ||||||||||||||||||||||||||||||||||||||||||
| 468 | column as the first parameter, but also a reference to the whole row hashref | ||||||||||||||||||||||||||||||||||||||||||
| 469 | as the second parameter, so you can do a variety of cunning things. | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | An example of a custom column whose C<transform> coderef uses the row | ||||||||||||||||||||||||||||||||||||||||||
| 472 | hashref to get other values for the same row could be: | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 475 | custom_columns => [ | ||||||||||||||||||||||||||||||||||||||||||
| 476 | { | ||||||||||||||||||||||||||||||||||||||||||
| 477 | name => 'salutation', | ||||||||||||||||||||||||||||||||||||||||||
| 478 | raw_column => 'name', | ||||||||||||||||||||||||||||||||||||||||||
| 479 | transform => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 480 | my ($name_value, $row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 481 | return "Hi, $row->{title} $name_value!"; | ||||||||||||||||||||||||||||||||||||||||||
| 482 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||
| 484 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 485 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | =item C<auth> | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | You can require that users be authenticated to view/edit records using the C<auth> | ||||||||||||||||||||||||||||||||||||||||||
| 490 | option to enable authentication powered by L<Dancer::Plugin::Auth::Extensible>. | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | You can set different requirements for viewing and editing, for example: | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | auth => { | ||||||||||||||||||||||||||||||||||||||||||
| 495 | view => { | ||||||||||||||||||||||||||||||||||||||||||
| 496 | require_login => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 497 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 498 | edit => { | ||||||||||||||||||||||||||||||||||||||||||
| 499 | require_role => 'Admin', | ||||||||||||||||||||||||||||||||||||||||||
| 500 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 501 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | The example above means that any logged in user can view records, but only users | ||||||||||||||||||||||||||||||||||||||||||
| 504 | with the 'Admin' role are able to create/edit/delete records. | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | Or, to just require login for anything (same requirements for both viewing and | ||||||||||||||||||||||||||||||||||||||||||
| 507 | editing), you can use the shorthand: | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | auth => { | ||||||||||||||||||||||||||||||||||||||||||
| 510 | require_login => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 511 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | =item C<table_class> | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | This provides a CSS class for the tables. | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | =item C<paginate_table_class> | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | This provides a CSS class for the tables paginate buttons. | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | sub simple_crud { | ||||||||||||||||||||||||||||||||||||||||||
| 525 | 8 | ▶ | my (%args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
simple_crud: called CC=21 SCAR=40.1
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | # Get a database connection to verify that the table name is OK, etc. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 8 | my $dbh = database($args{db_connection_name}); | |||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | 8 | ▶ | if (!$dbh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | warn "No database handle"; | |||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | 8 | ▶ | if (!$args{prefix}) { die "Need prefix to create routes!"; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 8 | ▶ | if ($args{prefix} !~ m{^/}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | $args{prefix} = '/' . $args{prefix}; | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | # If there's a Dancer prefix in use, as well as a prefix we're told about, | ||||||||||||||||||||||||||||||||||||||||||
| 541 | # then _construct_url() will need to be told about that later so it can | ||||||||||||||||||||||||||||||||||||||||||
| 542 | # construct URLs. It can't just call Dancer::App->current->prefix itself, | ||||||||||||||||||||||||||||||||||||||||||
| 543 | # though, as the prefix may have changed by the time the code is actually | ||||||||||||||||||||||||||||||||||||||||||
| 544 | # running. (See RT #73620.) So, we need to grab it here and add it to | ||||||||||||||||||||||||||||||||||||||||||
| 545 | # %args, so it can see it later. | ||||||||||||||||||||||||||||||||||||||||||
| 546 | 8 | ▶ | $args{dancer_prefix} = Dancer::App->current->prefix || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'Dancer::App'->current->prefix || ''
MC/DC0%0/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table50%1/2
A
A= 'Dancer::App'->current->prefix
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | 8 | ▶ | if (!$args{db_table}) { die "Need table name!"; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | # Accept deleteable as a synonym for deletable | ||||||||||||||||||||||||||||||||||||||||||
| 551 | $args{deletable} = delete $args{deleteable} | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 8 | ▶ | if !exists $args{deletable} && exists $args{deleteable}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not exists $args{"deletable"} and exists $args{"deleteable"}
MC/DC0%0/2
not exists $args{"deletable"} and exists $args{"deleteable"}
not exists $args{"deletable"}
exists $args{"deleteable"}
Truth table33%1/3
not(A) and B
A= not exists $args{"deletable"}B= exists $args{"deleteable"}
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | # Sane default values: | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 8 | ▶ | $args{key_column} ||= 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"key_column"} ||= "id"
MC/DC0%0/1
$args{"key_column"} ||= "id"
$args{"key_column"}
Truth table50%1/2
A
A= $args{"key_column"}
| |||||||||||||||||||||||||||||||||||||||||||
| 556 | 8 | ▶ | $args{record_title} ||= 'record'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"record_title"} ||= "record"
MC/DC0%0/1
$args{"record_title"} ||= "record"
$args{"record_title"}
Truth table50%1/2
A
A= $args{"record_title"}
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 8 | ▶ | $args{editable} = 1 unless exists $args{editable}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | 8 | ▶ | $args{addable} = $args{editable} unless exists $args{addable}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | 8 | ▶ | $args{query_auto_focus} = 1 unless exists $args{query_auto_focus}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | # Sanitise things we'll have to interpolate into queries (yes, that makes me | ||||||||||||||||||||||||||||||||||||||||||
| 562 | # feel bad, but you can't use params for field/table names): | ||||||||||||||||||||||||||||||||||||||||||
| 563 | 8 | my $table_name = $args{db_table}; | |||||||||||||||||||||||||||||||||||||||||
| 564 | 8 | my $key_column = $args{key_column}; | |||||||||||||||||||||||||||||||||||||||||
| 565 | 8 | for ($table_name, $key_column) { | |||||||||||||||||||||||||||||||||||||||||
| 566 | 16 | ▶ | die "Invalid table name/key column - SQL injection attempt?" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | if /--/; | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 16 | s/[^a-zA-Z0-9_-]//g; | |||||||||||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | # OK, create a route handler to deal with adding/editing: | ||||||||||||||||||||||||||||||||||||||||||
| 572 | my $handler | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 8 | ▶ | = sub { _create_add_edit_route(\%args, $table_name, $key_column); }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | 8 | ▶ | if ($args{editable}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 576 | 3 | for ('/edit/:id') { | |||||||||||||||||||||||||||||||||||||||||
| 577 | 3 | my $url = _construct_url($args{prefix}, $_); | |||||||||||||||||||||||||||||||||||||||||
| 578 | 3 | Dancer::Logger::debug("Setting up route for $url"); | |||||||||||||||||||||||||||||||||||||||||
| 579 | 3 | any ['get', 'post'] => $url => _ensure_auth( | |||||||||||||||||||||||||||||||||||||||||
| 580 | 'edit', $handler, \%args, | ||||||||||||||||||||||||||||||||||||||||||
| 581 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | 8 | ▶ | if ($args{addable}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | 2 | for ('/add') { | |||||||||||||||||||||||||||||||||||||||||
| 586 | 2 | my $url = _construct_url($args{prefix}, $_); | |||||||||||||||||||||||||||||||||||||||||
| 587 | 2 | Dancer::Logger::debug("Setting up route for $url"); | |||||||||||||||||||||||||||||||||||||||||
| 588 | 2 | any ['get', 'post'] => $url => _ensure_auth( | |||||||||||||||||||||||||||||||||||||||||
| 589 | 'edit', $handler, \%args | ||||||||||||||||||||||||||||||||||||||||||
| 590 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | # And a route to list records already in the table: | ||||||||||||||||||||||||||||||||||||||||||
| 595 | my $list_handler | ||||||||||||||||||||||||||||||||||||||||||
| 596 | = _ensure_auth( | ||||||||||||||||||||||||||||||||||||||||||
| 597 | 'view', | ||||||||||||||||||||||||||||||||||||||||||
| 598 | 14 | ▶ | sub { _create_list_handler(\%args, $table_name, $key_column); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 599 | 8 | \%args, | |||||||||||||||||||||||||||||||||||||||||
| 600 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 601 | get _construct_url( | ||||||||||||||||||||||||||||||||||||||||||
| 602 | $args{prefix}, | ||||||||||||||||||||||||||||||||||||||||||
| 603 | 8 | ) => $list_handler; | |||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | # If we should allow deletion of records, set up routes to handle that, | ||||||||||||||||||||||||||||||||||||||||||
| 606 | # too. | ||||||||||||||||||||||||||||||||||||||||||
| 607 | 8 | ▶ | if ($args{editable} && $args{deletable}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"editable"} and $args{"deletable"}
MC/DC0%0/2
$args{"editable"} and $args{"deletable"}
$args{"editable"}
$args{"deletable"}
Truth table66%2/3
A and B
A= $args{"editable"}B= $args{"deletable"}
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | # A route for GET requests, to present a "Do you want to delete this" | ||||||||||||||||||||||||||||||||||||||||||
| 610 | # message with a form to submit (this is only for browsers which didn't | ||||||||||||||||||||||||||||||||||||||||||
| 611 | # support Javascript, otherwise the list page will have POSTed the ID | ||||||||||||||||||||||||||||||||||||||||||
| 612 | # to us) (or they just came here directly for some reason) | ||||||||||||||||||||||||||||||||||||||||||
| 613 | get _construct_url( | ||||||||||||||||||||||||||||||||||||||||||
| 614 | $args{prefix}, "/delete/:id" | ||||||||||||||||||||||||||||||||||||||||||
| 615 | ) => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | ▶ | return _apply_template(<<CONFIRMDELETE, $args{'template'}, $args{'record_title'}); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | <p> | ||||||||||||||||||||||||||||||||||||||||||
| 618 | Do you really wish to delete this record? | ||||||||||||||||||||||||||||||||||||||||||
| 619 | </p> | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | <form method="post"> | ||||||||||||||||||||||||||||||||||||||||||
| 622 | <input type="button" value="Cancel" onclick="history.back();"> | ||||||||||||||||||||||||||||||||||||||||||
| 623 | <input type="submit" value="Delete record"> | ||||||||||||||||||||||||||||||||||||||||||
| 624 | </form> | ||||||||||||||||||||||||||||||||||||||||||
| 625 | CONFIRMDELETE | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | # A route for POST requests, to actually delete the record | ||||||||||||||||||||||||||||||||||||||||||
| 630 | my $del_url_stub = _construct_url( | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | $args{prefix}, '/delete' | |||||||||||||||||||||||||||||||||||||||||
| 632 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 633 | my $delete_handler = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | ▶ | my ($id) = params->{record_id} || splat; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
Condition0%0/3
params()->{'record_id'} || splat()
MC/DC0%0/2
params()->{'record_id'} || splat()
params()->{'record_id'}
splat()
Truth table0%0/3
A || B
A= params()->{'record_id'}B= splat()
| |||||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | my $dbh = database($args{db_connection_name}); | |||||||||||||||||||||||||||||||||||||||||
| 636 | 0 | my $where = _get_where_filter_from_args(\%args); | |||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | $where->{$key_column} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | my %params = params; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | my $meta_for_hook = { | |||||||||||||||||||||||||||||||||||||||||
| 641 | args => \%args, | ||||||||||||||||||||||||||||||||||||||||||
| 642 | params => \%params, | ||||||||||||||||||||||||||||||||||||||||||
| 643 | table_name => $table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 644 | key_column => $key_column, | ||||||||||||||||||||||||||||||||||||||||||
| 645 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 646 | # fire the pre-delete hook, in case user wants to perform trickery before the delete | ||||||||||||||||||||||||||||||||||||||||||
| 647 | 0 | execute_hook('delete_row_pre_delete', $meta_for_hook ); | |||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | my $rows_deleted = $dbh->quick_delete($table_name, $where) | ||||||||||||||||||||||||||||||||||||||||||
| 650 | or return _apply_template("<p>Failed to delete!</p>", | ||||||||||||||||||||||||||||||||||||||||||
| 651 | 0 | ▶ | $args{'template'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||
| 653 | 0 | $meta_for_hook->{success} = $rows_deleted; | |||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | ▶ | if ($rows_deleted) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 655 | # post-delete hook, in case user wants to do cunning things after the delete | ||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | execute_hook('delete_row_post_delete', $meta_for_hook ); | |||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | redirect _external_url($args{dancer_prefix}, $args{prefix}); | |||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | post qr[$del_url_stub/?(.+)?$] => _ensure_auth( | |||||||||||||||||||||||||||||||||||||||||
| 662 | 'edit', $delete_handler, \%args | ||||||||||||||||||||||||||||||||||||||||||
| 663 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||||||
| 665 | my $view_url_stub = _construct_url( | ||||||||||||||||||||||||||||||||||||||||||
| 666 | 8 | $args{prefix}, '/view' | |||||||||||||||||||||||||||||||||||||||||
| 667 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 668 | my $view_handler = _ensure_auth( | ||||||||||||||||||||||||||||||||||||||||||
| 669 | 'view', | ||||||||||||||||||||||||||||||||||||||||||
| 670 | 2 | ▶ | sub { _create_view_handler(\%args, $table_name, $key_column); }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 671 | 8 | \%args, | |||||||||||||||||||||||||||||||||||||||||
| 672 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||
| 674 | 8 | get $view_url_stub.'/:id' => $view_handler; | |||||||||||||||||||||||||||||||||||||||||
| 675 | } | ||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||
| 677 | sub _create_view_handler { | ||||||||||||||||||||||||||||||||||||||||||
| 678 | 2 | ▶ | my ($args, $table_name, $key_column) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_view_handler: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 679 | 2 | my $params = params; | |||||||||||||||||||||||||||||||||||||||||
| 680 | 2 | ▶ | my $id = $params->{id} or return _apply_template("<p>Need id to view!</p>", $args->{'template'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||
| 682 | 2 | my $dbh = database($args->{db_connection_name}); | |||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||
| 684 | # a hash containing the current values in the database. Take where_filter | ||||||||||||||||||||||||||||||||||||||||||
| 685 | # into account, so we can't fetch a row if it doesn't match the filter | ||||||||||||||||||||||||||||||||||||||||||
| 686 | # (otherwise people could load any record they wished just by changing the | ||||||||||||||||||||||||||||||||||||||||||
| 687 | # ID in the URL, which would be considered a Bad Thing) | ||||||||||||||||||||||||||||||||||||||||||
| 688 | 2 | my $where = _get_where_filter_from_args($args); | |||||||||||||||||||||||||||||||||||||||||
| 689 | 2 | $where->{$key_column} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 690 | 2 | my $values_from_database = $dbh->quick_select($table_name, $where); | |||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||
| 692 | # Find out about table columns: | ||||||||||||||||||||||||||||||||||||||||||
| 693 | 2 | my $all_table_columns = _find_columns($dbh, $args->{db_table}); | |||||||||||||||||||||||||||||||||||||||||
| 694 | 2 | my @rows = (['Column Name', 'Value']); | |||||||||||||||||||||||||||||||||||||||||
| 695 | 2 | my $table = HTML::Table->new( -border=>1 ); | |||||||||||||||||||||||||||||||||||||||||
| 696 | 2 | $table->addSectionRow('thead', 0, 'Column Name', 'Value'); | |||||||||||||||||||||||||||||||||||||||||
| 697 | 2 | $table->setSectionCellHead('thead', 0, 1, 1, 1); | |||||||||||||||||||||||||||||||||||||||||
| 698 | 2 | $table->setSectionCellHead('thead', 0, 1, 2, 1); | |||||||||||||||||||||||||||||||||||||||||
| 699 | 2 | foreach my $col (@$all_table_columns) { | |||||||||||||||||||||||||||||||||||||||||
| 700 | 6 | $table->addSectionRow('tbody', 0, $col->{COLUMN_NAME}, $values_from_database->{$col->{COLUMN_NAME}}); | |||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||
| 702 | 2 | ▶ | my $html = $table->getTable || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$table->getTable || ''
MC/DC0%0/1
$table->getTable || ''
$table->getTable
Truth table50%1/2
A
A= $table->getTable
| |||||||||||||||||||||||||||||||||||||||||||
| 703 | 2 | return _apply_template($html, $args->{'template'}); | |||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||
| 706 | 3 | register simple_crud => \&simple_crud; | |||||||||||||||||||||||||||||||||||||||||
| 707 | 3 | register_hook(qw( | |||||||||||||||||||||||||||||||||||||||||
| 708 | add_edit_row | ||||||||||||||||||||||||||||||||||||||||||
| 709 | add_edit_row_pre_save | ||||||||||||||||||||||||||||||||||||||||||
| 710 | add_edit_row_post_save | ||||||||||||||||||||||||||||||||||||||||||
| 711 | delete_row_pre_delete | ||||||||||||||||||||||||||||||||||||||||||
| 712 | delete_row_post_delete | ||||||||||||||||||||||||||||||||||||||||||
| 713 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 714 | 3 | register_plugin; | |||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||
| 716 | sub _create_add_edit_route { | ||||||||||||||||||||||||||||||||||||||||||
| 717 | 6 | ▶ | my ($args, $table_name, $key_column) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_add_edit_route: called CC=41 SCAR=55.0
| |||||||||||||||||||||||||||||||||||||||||||
| 718 | 6 | my $params = params; | |||||||||||||||||||||||||||||||||||||||||
| 719 | 6 | my $id = $params->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||
| 721 | 6 | my $dbh = database($args->{db_connection_name}); | |||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | # a hash containing the current values in the database | ||||||||||||||||||||||||||||||||||||||||||
| 724 | 6 | my $values_from_database; | |||||||||||||||||||||||||||||||||||||||||
| 725 | 6 | ▶ | if (defined $id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 726 | 5 | my $where = _get_where_filter_from_args($args); | |||||||||||||||||||||||||||||||||||||||||
| 727 | 5 | $where->{$key_column} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 728 | 5 | $values_from_database | |||||||||||||||||||||||||||||||||||||||||
| 729 | = $dbh->quick_select($table_name, $where); | ||||||||||||||||||||||||||||||||||||||||||
| 730 | 5 | ▶ | if (!$values_from_database) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 731 | 3 | send_error "$args->{record_title} $id not found", 404; | |||||||||||||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||
| 736 | # Find out about table columns: | ||||||||||||||||||||||||||||||||||||||||||
| 737 | 3 | my $all_table_columns = _find_columns($dbh, $args->{db_table}); | |||||||||||||||||||||||||||||||||||||||||
| 738 | 3 | my @editable_columns; | |||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||
| 740 | # Now, find out which ones we can edit. | ||||||||||||||||||||||||||||||||||||||||||
| 741 | 3 | ▶ | if ($args->{editable_columns}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | # We were given an explicit list of fields we can edit, so this is | ||||||||||||||||||||||||||||||||||||||||||
| 744 | # easy: | ||||||||||||||||||||||||||||||||||||||||||
| 745 | 0 | @editable_columns = @{ $args->{editable_columns} }; | |||||||||||||||||||||||||||||||||||||||||
| 746 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||
| 748 | # OK, take all the columns from the table, except the key field: | ||||||||||||||||||||||||||||||||||||||||||
| 749 | 9 | @editable_columns = grep { $_ ne $key_column } | |||||||||||||||||||||||||||||||||||||||||
| 750 | 3 | map { $_->{COLUMN_NAME} } @$all_table_columns; | |||||||||||||||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | 3 | ▶ | if ($args->{not_editable_columns}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | for my $col (@{ $args->{not_editable_columns} }) { | |||||||||||||||||||||||||||||||||||||||||
| 755 | 0 | @editable_columns = grep { $_ ne $col } @editable_columns; | |||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||
| 757 | } | ||||||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||||||
| 759 | # Some DWIMery: if we don't have a validation rule specified for a | ||||||||||||||||||||||||||||||||||||||||||
| 760 | # field, and it's pretty clear what it is supposed to be, just do it: | ||||||||||||||||||||||||||||||||||||||||||
| 761 | 3 | ▶ | my $validation = $args->{validation} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'validation'} || {}
MC/DC100%1/1
$args->{'validation'} || {}
$args->{'validation'}
Truth table100%2/2
A
A= $args->{'validation'}
| |||||||||||||||||||||||||||||||||||||||||||
| 762 | 3 | for my $field (grep { $_ ne $key_column } @editable_columns) { | |||||||||||||||||||||||||||||||||||||||||
| 763 | 6 | ▶ | next if $validation->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 764 | 6 | ▶ | if ($field =~ /email/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 765 | 0 | $validation->{$field} = 'EMAIL'; | |||||||||||||||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||||||||||||||||
| 767 | } | ||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | # More DWIMmery: if the user hasn't supplied a list of required fields, | ||||||||||||||||||||||||||||||||||||||||||
| 770 | # work out what fields are required by whether they're nullable in the | ||||||||||||||||||||||||||||||||||||||||||
| 771 | # DB: | ||||||||||||||||||||||||||||||||||||||||||
| 772 | 3 | my %required_fields; | |||||||||||||||||||||||||||||||||||||||||
| 773 | 3 | ▶ | if (exists $args->{required}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 774 | 0 | $required_fields{$_}++ for @{ $args->{required} }; | |||||||||||||||||||||||||||||||||||||||||
| 775 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 776 | $_->{NULLABLE} || $required_fields{ $_->{COLUMN_NAME} }++ | ||||||||||||||||||||||||||||||||||||||||||
| 777 | 3 | ▶ | for @$all_table_columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||
| 780 | # If the user didn't supply a list of acceptable values for a field, but | ||||||||||||||||||||||||||||||||||||||||||
| 781 | # it's an ENUM column, use the possible values declared in the ENUM. | ||||||||||||||||||||||||||||||||||||||||||
| 782 | # Also remember field types for easy reference later | ||||||||||||||||||||||||||||||||||||||||||
| 783 | 3 | my %constrain_values; | |||||||||||||||||||||||||||||||||||||||||
| 784 | my %field_type; | ||||||||||||||||||||||||||||||||||||||||||
| 785 | 3 | for my $field (@$all_table_columns) { | |||||||||||||||||||||||||||||||||||||||||
| 786 | 9 | my $name = $field->{COLUMN_NAME}; | |||||||||||||||||||||||||||||||||||||||||
| 787 | 9 | $field_type{$name} = $field->{TYPE_NAME}; | |||||||||||||||||||||||||||||||||||||||||
| 788 | 9 | ▶ | if (my $values_specified = $args->{acceptable_values}->{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||
| 790 | # It may have been given to us as a coderef; if so, execute it to | ||||||||||||||||||||||||||||||||||||||||||
| 791 | # get the results | ||||||||||||||||||||||||||||||||||||||||||
| 792 | 0 | ▶ | if (ref $values_specified eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 793 | 0 | $values_specified = $values_specified->(); | |||||||||||||||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||||||||||||||||
| 795 | 0 | $constrain_values{$name} = $values_specified; | |||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | } elsif (my $foreign_key = $args->{foreign_keys}{$name}) { | ||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | # Find out the possible values for this column from the other table: | ||||||||||||||||||||||||||||||||||||||||||
| 800 | 0 | my %possible_values; | |||||||||||||||||||||||||||||||||||||||||
| 801 | 0 | debug "Looking for rows for foreign relation: " => $foreign_key; | |||||||||||||||||||||||||||||||||||||||||
| 802 | 0 | for my $row ($dbh->quick_select($foreign_key->{table}, {})) { | |||||||||||||||||||||||||||||||||||||||||
| 803 | 0 | debug "Row from foreign relation: " => $row; | |||||||||||||||||||||||||||||||||||||||||
| 804 | $possible_values{ $row->{ $foreign_key->{key_column} } } | ||||||||||||||||||||||||||||||||||||||||||
| 805 | 0 | = $row->{ $foreign_key->{label_column} }; | |||||||||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||||||||||
| 807 | 0 | $constrain_values{$name} = \%possible_values; | |||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||
| 809 | } elsif (my $values_from_db = $field->{mysql_values}) { | ||||||||||||||||||||||||||||||||||||||||||
| 810 | 0 | $constrain_values{$name} = $values_from_db; | |||||||||||||||||||||||||||||||||||||||||
| 811 | } | ||||||||||||||||||||||||||||||||||||||||||
| 812 | } | ||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||
| 814 | # Only give CGI::FormBuilder our fake CGI object if the form has been | ||||||||||||||||||||||||||||||||||||||||||
| 815 | # POSTed to us already; otherwise, it will ignore default values from | ||||||||||||||||||||||||||||||||||||||||||
| 816 | # the DB, it seems. | ||||||||||||||||||||||||||||||||||||||||||
| 817 | my $paramsobj | ||||||||||||||||||||||||||||||||||||||||||
| 818 | 3 | ▶ | = request->{method} eq 'POST' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 819 | ? Dancer::Plugin::SimpleCRUD::ParamsObject->new({ params() }) | ||||||||||||||||||||||||||||||||||||||||||
| 820 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | my $form = CGI::FormBuilder->new( | ||||||||||||||||||||||||||||||||||||||||||
| 823 | fields => \@editable_columns, | ||||||||||||||||||||||||||||||||||||||||||
| 824 | params => $paramsobj, | ||||||||||||||||||||||||||||||||||||||||||
| 825 | values => $values_from_database, | ||||||||||||||||||||||||||||||||||||||||||
| 826 | validate => $validation, | ||||||||||||||||||||||||||||||||||||||||||
| 827 | method => 'post', | ||||||||||||||||||||||||||||||||||||||||||
| 828 | action => _external_url( | ||||||||||||||||||||||||||||||||||||||||||
| 829 | $args->{dancer_prefix}, | ||||||||||||||||||||||||||||||||||||||||||
| 830 | $args->{prefix}, | ||||||||||||||||||||||||||||||||||||||||||
| 831 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 832 | params->{id} | ||||||||||||||||||||||||||||||||||||||||||
| 833 | ? '/edit/' . params->{id} | ||||||||||||||||||||||||||||||||||||||||||
| 834 | 3 | ▶ | : '/add' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 835 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 836 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 837 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 838 | 3 | for my $field (@editable_columns) { | |||||||||||||||||||||||||||||||||||||||||
| 839 | # first check if there's data from the database for this field, | ||||||||||||||||||||||||||||||||||||||||||
| 840 | # then if there's a value in params() for this field, | ||||||||||||||||||||||||||||||||||||||||||
| 841 | # then if args->{default_value} was set for this field via the | ||||||||||||||||||||||||||||||||||||||||||
| 842 | # 'default_value' hash when the route was created. | ||||||||||||||||||||||||||||||||||||||||||
| 843 | my $default = | ||||||||||||||||||||||||||||||||||||||||||
| 844 | exists $values_from_database->{$field} | ||||||||||||||||||||||||||||||||||||||||||
| 845 | ? $values_from_database->{$field} | ||||||||||||||||||||||||||||||||||||||||||
| 846 | : exists params->{$field} | ||||||||||||||||||||||||||||||||||||||||||
| 847 | ? uri_unescape(params->{$field}) | ||||||||||||||||||||||||||||||||||||||||||
| 848 | : exists $args->{default_value}->{$field} | ||||||||||||||||||||||||||||||||||||||||||
| 849 | 6 | ▶ | ? $args->{default_value}->{$field} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 850 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 851 | 6 | my %field_params = ( | |||||||||||||||||||||||||||||||||||||||||
| 852 | name => $field, | ||||||||||||||||||||||||||||||||||||||||||
| 853 | value => $default, | ||||||||||||||||||||||||||||||||||||||||||
| 854 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||||||||||
| 856 | 6 | $field_params{required} = $required_fields{$field}; | |||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||
| 858 | 6 | ▶ | if ($constrain_values{$field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 859 | 0 | $field_params{options} = $constrain_values{$field}; | |||||||||||||||||||||||||||||||||||||||||
| 860 | } | ||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | # Certain options in $args simply cause that value to be added to the | ||||||||||||||||||||||||||||||||||||||||||
| 863 | # params for this field we'll pass to $form->field: | ||||||||||||||||||||||||||||||||||||||||||
| 864 | 6 | my %option_map = ( | |||||||||||||||||||||||||||||||||||||||||
| 865 | labels => 'label', | ||||||||||||||||||||||||||||||||||||||||||
| 866 | validation => 'validate', | ||||||||||||||||||||||||||||||||||||||||||
| 867 | message => 'message', | ||||||||||||||||||||||||||||||||||||||||||
| 868 | jsmessage => 'jsmessage', | ||||||||||||||||||||||||||||||||||||||||||
| 869 | sort_options => 'sortopts', | ||||||||||||||||||||||||||||||||||||||||||
| 870 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 871 | 6 | ▶ | while (my ($arg_name, $field_param_name) = each(%option_map)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 872 | 30 | ▶ | if (my $val = $args->{$arg_name}{$field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 873 | 0 | $field_params{$field_param_name} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 874 | } | ||||||||||||||||||||||||||||||||||||||||||
| 875 | } | ||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||
| 877 | # Normally, CGI::FormBuilder can guess the type of field perfectly, | ||||||||||||||||||||||||||||||||||||||||||
| 878 | # but give it some extra DWIMmy help: | ||||||||||||||||||||||||||||||||||||||||||
| 879 | 6 | ▶ | if ($field =~ /pass(?:wd|word)?$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 880 | 3 | $field_params{type} = 'password'; | |||||||||||||||||||||||||||||||||||||||||
| 881 | } | ||||||||||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||||||||||
| 883 | # use a <textarea> for large text fields. | ||||||||||||||||||||||||||||||||||||||||||
| 884 | 6 | ▶ | if ($field_type{$field} eq 'TEXT') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 885 | 0 | $field_params{type} = 'textarea'; | |||||||||||||||||||||||||||||||||||||||||
| 886 | } | ||||||||||||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||||||||||
| 888 | # ... unless the user specified a type for this field, in which case, | ||||||||||||||||||||||||||||||||||||||||||
| 889 | # use what they said | ||||||||||||||||||||||||||||||||||||||||||
| 890 | 6 | ▶ | if (my $override_type = $args->{input_types}{$field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 891 | 0 | $field_params{type} = $override_type; | |||||||||||||||||||||||||||||||||||||||||
| 892 | } | ||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||
| 894 | # if the constraint on this is an array of arrays, | ||||||||||||||||||||||||||||||||||||||||||
| 895 | # and there are three elements in the first array in that list, | ||||||||||||||||||||||||||||||||||||||||||
| 896 | # (which will be intepreted as: value, label, category) | ||||||||||||||||||||||||||||||||||||||||||
| 897 | # we are going to assume you want optgroups, with the | ||||||||||||||||||||||||||||||||||||||||||
| 898 | # third element in each being the category. | ||||||||||||||||||||||||||||||||||||||||||
| 899 | # | ||||||||||||||||||||||||||||||||||||||||||
| 900 | # (See the optgroups option in CGI::FormBuilder) | ||||||||||||||||||||||||||||||||||||||||||
| 901 | 6 | ▶ | if (ref($field_params{options}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 902 | 0 | ▶ | if (ref( $field_params{options}->[0] ) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 903 | 0 | ▶ | if (@{ $field_params{options}->[0] } == 3) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 904 | 0 | $field_params{optgroups} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 905 | } | ||||||||||||||||||||||||||||||||||||||||||
| 906 | } | ||||||||||||||||||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||
| 910 | # OK, add the field to the form: | ||||||||||||||||||||||||||||||||||||||||||
| 911 | 6 | $form->field(%field_params); | |||||||||||||||||||||||||||||||||||||||||
| 912 | } | ||||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||
| 914 | # Now, if all is OK, go ahead and process: | ||||||||||||||||||||||||||||||||||||||||||
| 915 | 3 | ▶ | if (request->{method} eq 'POST' && $form->submitted && $form->validate) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
request()->{"method"} eq "POST" and $form->submitted and $form->validate
request()->{"method"} eq "POST" and $form->submitted
MC/DC0%0/3
request()->{"method"} eq "POST" and $form->submitted and $form->validate
request()->{"method"} eq "POST"
$form->submitted
$form->validate
Truth table25%1/4
A and B and C
A= request()->{"method"} eq "POST"B= $form->submittedC= $form->validate
| |||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | # Assemble a hash of only fields from the DB (if other fields were | ||||||||||||||||||||||||||||||||||||||||||
| 918 | # submitted with the form which don't belong in the DB, ignore them) | ||||||||||||||||||||||||||||||||||||||||||
| 919 | 0 | my %params; | |||||||||||||||||||||||||||||||||||||||||
| 920 | 0 | $params{$_} = params('body')->{$_} for @editable_columns; | |||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||
| 922 | 0 | my $meta_for_hook = { | |||||||||||||||||||||||||||||||||||||||||
| 923 | args => $args, | ||||||||||||||||||||||||||||||||||||||||||
| 924 | params => \%params, | ||||||||||||||||||||||||||||||||||||||||||
| 925 | table_name => $table_name, | ||||||||||||||||||||||||||||||||||||||||||
| 926 | key_column => $key_column, | ||||||||||||||||||||||||||||||||||||||||||
| 927 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 928 | # Fire a hook so the user can manipulate the data in a whole range of | ||||||||||||||||||||||||||||||||||||||||||
| 929 | # cunning ways, if they wish | ||||||||||||||||||||||||||||||||||||||||||
| 930 | 0 | execute_hook('add_edit_row', \%params); | |||||||||||||||||||||||||||||||||||||||||
| 931 | 0 | execute_hook('add_edit_row_pre_save', $meta_for_hook); | |||||||||||||||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||||||||||||||||
| 933 | 0 | my $verb; | |||||||||||||||||||||||||||||||||||||||||
| 934 | my $success; | ||||||||||||||||||||||||||||||||||||||||||
| 935 | 0 | ▶ | if (exists params('route')->{id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||||||||||||
| 937 | # We're editing an existing record - make sure the WHERE clause | ||||||||||||||||||||||||||||||||||||||||||
| 938 | # hashref incorporates where_filter, if in use, so that users can't | ||||||||||||||||||||||||||||||||||||||||||
| 939 | # edit stuff they shouldn't be able to | ||||||||||||||||||||||||||||||||||||||||||
| 940 | 0 | my $where = _get_where_filter_from_args($args); | |||||||||||||||||||||||||||||||||||||||||
| 941 | 0 | $where->{$key_column} = params('route')->{id}; | |||||||||||||||||||||||||||||||||||||||||
| 942 | 0 | $success = $dbh->quick_update($table_name, $where, \%params); | |||||||||||||||||||||||||||||||||||||||||
| 943 | 0 | $verb = 'update'; | |||||||||||||||||||||||||||||||||||||||||
| 944 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 945 | 0 | $success = $dbh->quick_insert($table_name, \%params); | |||||||||||||||||||||||||||||||||||||||||
| 946 | # pass them *this* dbh instance so that they can call last_insert_id() | ||||||||||||||||||||||||||||||||||||||||||
| 947 | # against it if they need to. last_insert_id in some instances requires | ||||||||||||||||||||||||||||||||||||||||||
| 948 | # catalog, schema, etc args, so we can't just call it and save the result. | ||||||||||||||||||||||||||||||||||||||||||
| 949 | # important that we don't do any more database operations that would change | ||||||||||||||||||||||||||||||||||||||||||
| 950 | # last_insert_id between here and the hook, or this won't work. | ||||||||||||||||||||||||||||||||||||||||||
| 951 | 0 | $meta_for_hook->{dbh} = $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 952 | 0 | $verb = 'create new'; | |||||||||||||||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||||||||||||
| 955 | 0 | $meta_for_hook->{success} = $success; | |||||||||||||||||||||||||||||||||||||||||
| 956 | 0 | $meta_for_hook->{verb} = $verb; | |||||||||||||||||||||||||||||||||||||||||
| 957 | 0 | ▶ | if ($success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||
| 959 | # Redirect to the list page | ||||||||||||||||||||||||||||||||||||||||||
| 960 | # TODO: pass a param to cause it to show a message? | ||||||||||||||||||||||||||||||||||||||||||
| 961 | 0 | execute_hook('add_edit_row_post_save', $meta_for_hook); | |||||||||||||||||||||||||||||||||||||||||
| 962 | 0 | redirect _external_url($args->{dancer_prefix}, $args->{prefix}); | |||||||||||||||||||||||||||||||||||||||||
| 963 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 964 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 965 | 0 | execute_hook('add_edit_row_post_save', $meta_for_hook); | |||||||||||||||||||||||||||||||||||||||||
| 966 | # TODO: better error handling - options to provide error templates | ||||||||||||||||||||||||||||||||||||||||||
| 967 | # etc | ||||||||||||||||||||||||||||||||||||||||||
| 968 | # (below is one approach to that TODO--this, or perhaps the hook could return a hash | ||||||||||||||||||||||||||||||||||||||||||
| 969 | # that would specify these overrides? Probably best to come up with a complete mechanism | ||||||||||||||||||||||||||||||||||||||||||
| 970 | # consistent across hooks before we implement.) | ||||||||||||||||||||||||||||||||||||||||||
| 971 | # return _apply_template( | ||||||||||||||||||||||||||||||||||||||||||
| 972 | # $meta_for_hook->{return}{error_message} || "<p>Unable to $verb $args->{record_title}</p>", | ||||||||||||||||||||||||||||||||||||||||||
| 973 | # $meta_for_hook->{return}{error_template} || $args->{error_template} || $args->{'template'} | ||||||||||||||||||||||||||||||||||||||||||
| 974 | #); | ||||||||||||||||||||||||||||||||||||||||||
| 975 | return _apply_template( | ||||||||||||||||||||||||||||||||||||||||||
| 976 | "<p>Unable to $verb $args->{record_title}</p>", | ||||||||||||||||||||||||||||||||||||||||||
| 977 | 0 | $args->{'template'}, $args->{'record_title'}); | |||||||||||||||||||||||||||||||||||||||||
| 978 | } | ||||||||||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||||||||||
| 980 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 981 | 3 | return _apply_template($form->render, $args->{'template'}, $args->{'record_title'}); | |||||||||||||||||||||||||||||||||||||||||
| 982 | } | ||||||||||||||||||||||||||||||||||||||||||
| 983 | } | ||||||||||||||||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||||||||||||||||
| 985 | sub _create_list_handler { | ||||||||||||||||||||||||||||||||||||||||||
| 986 | 14 | ▶ | my ($args, $table_name, $key_column) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_list_handler: called CC=82 SCAR=64.2
| |||||||||||||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||||||||||||
| 988 | 14 | my $dbh = database($args->{db_connection_name}); | |||||||||||||||||||||||||||||||||||||||||
| 989 | 14 | my $columns = _find_columns($dbh, $table_name); | |||||||||||||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||||||||||||
| 991 | 14 | my $display_columns = $args->{'display_columns'}; | |||||||||||||||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||||||||||||||||
| 993 | 14 | ▶ | my $table_class = $args->{'table_class'} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'table_class'} || ''
MC/DC0%0/1
$args->{'table_class'} || ''
$args->{'table_class'}
Truth table50%1/2
A
A= $args->{'table_class'}
| |||||||||||||||||||||||||||||||||||||||||||
| 994 | 14 | ▶ | my $paginate_table_class = $args->{'paginate_table_class'} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args->{'paginate_table_class'} || ''
MC/DC0%0/1
$args->{'paginate_table_class'} || ''
$args->{'paginate_table_class'}
Truth table50%1/2
A
A= $args->{'paginate_table_class'}
| |||||||||||||||||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||||||||||||||||
| 996 | # If display_columns argument was passed, filter the column list to only | ||||||||||||||||||||||||||||||||||||||||||
| 997 | # have the ones we asked for. | ||||||||||||||||||||||||||||||||||||||||||
| 998 | 14 | ▶ | if (ref $display_columns eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 999 | 0 | my @filtered_columns; | |||||||||||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||||||||||||
| 1001 | 0 | foreach my $col (@$columns) { | |||||||||||||||||||||||||||||||||||||||||
| 1002 | 0 | ▶ | if (grep { $_ eq $col->{'COLUMN_NAME'} } @$display_columns) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1003 | 0 | push @filtered_columns, $col; | |||||||||||||||||||||||||||||||||||||||||
| 1004 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||||||
| 1007 | 0 | ▶ | if (@filtered_columns) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1008 | 0 | $columns = \@filtered_columns; | |||||||||||||||||||||||||||||||||||||||||
| 1009 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1010 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||
| 1012 | 14 | ▶ | my $searchfield = params->{searchfield} || $key_column; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
params()->{'searchfield'} || $key_column
MC/DC0%0/2
params()->{'searchfield'} || $key_column
params()->{'searchfield'}
$key_column
Truth table66%2/3
A || B
A= params()->{'searchfield'}B= $key_column
| |||||||||||||||||||||||||||||||||||||||||||
| 1013 | my $searchfield_options = join( | ||||||||||||||||||||||||||||||||||||||||||
| 1014 | "\n", | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 1016 | 14 | my $friendly_name = $_->{COLUMN_NAME}; | |||||||||||||||||||||||||||||||||||||||||
| 1017 | 42 | ▶ | if ($args->{labels}{$_->{COLUMN_NAME}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1018 | 0 | $friendly_name = $args->{labels}{$_->{COLUMN_NAME}}; | |||||||||||||||||||||||||||||||||||||||||
| 1019 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1020 | my $sel | ||||||||||||||||||||||||||||||||||||||||||
| 1021 | = ($searchfield eq $_->{COLUMN_NAME}) | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | 42 | ▶ | ? "selected" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1023 | : ""; | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | 42 | "<option $sel value='$_->{COLUMN_NAME}'>$friendly_name</option>" | |||||||||||||||||||||||||||||||||||||||||
| 1025 | } @$columns | ||||||||||||||||||||||||||||||||||||||||||
| 1026 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1027 | 14 | my $default_searchtype = "e"; | |||||||||||||||||||||||||||||||||||||||||
| 1028 | 14 | my @searchtypes = ( | |||||||||||||||||||||||||||||||||||||||||
| 1029 | [ e => { name => "Equals", cmp => "=" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | [ c => { name => "Contains", cmp => "like" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1031 | [ b => { name => "Begins With", cmp => "like" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | [ ne => { name => "Does Not Equal", cmp => "!=" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1033 | [ nc => { name => "Does Not Contain", cmp => "not like" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||||||||||||
| 1035 | [ lt => { name => "Less Than", cmp => "<" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1036 | [ lte => { name => "Less Than or Equal To", cmp => "<=" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | [ gt => { name => "Greater Than", cmp => ">" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1038 | [ gte => { name => "Greater Than or Equal To", cmp => ">=" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||||||||||||
| 1040 | [ like => { name => "Like", cmp => "LIKE" } ], | ||||||||||||||||||||||||||||||||||||||||||
| 1041 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1042 | my $searchtype_options = join( "\n", | ||||||||||||||||||||||||||||||||||||||||||
| 1043 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 1044 | 14 | my ($search_code, $hashref) = @$_; | |||||||||||||||||||||||||||||||||||||||||
| 1045 | 140 | my $name = $hashref->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 1046 | 140 | ▶ | my $sel = (params->{searchtype} || $default_searchtype) eq $search_code; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
params()->{'searchtype'} || $default_searchtype
MC/DC0%0/2
params()->{'searchtype'} || $default_searchtype
params()->{'searchtype'}
$default_searchtype
Truth table66%2/3
A || B
A= params()->{'searchtype'}B= $default_searchtype
| |||||||||||||||||||||||||||||||||||||||||||
| 1047 | 140 | ▶ | sprintf("<option value='%s'%s>%s</option>", $search_code, $sel ? " selected" : "", $name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | } @searchtypes | ||||||||||||||||||||||||||||||||||||||||||
| 1049 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||||||||||||||||
| 1051 | 14 | ▶ | my $order_by_param = params->{'o'} || ""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
params()->{'o'} || ''
MC/DC0%0/1
params()->{'o'} || ''
params()->{'o'}
Truth table50%1/2
A
A= params()->{'o'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1052 | 14 | ▶ | my $order_by_direction = params->{'d'} || ""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
params()->{'d'} || ''
MC/DC0%0/1
params()->{'d'} || ''
params()->{'d'}
Truth table50%1/2
A
A= params()->{'d'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1053 | 14 | my $q = _defined_or_empty(params->{'q'}); | |||||||||||||||||||||||||||||||||||||||||
| 1054 | 14 | my $display_q = encode_entities( $q ); | |||||||||||||||||||||||||||||||||||||||||
| 1055 | 14 | my $html = <<"SEARCHFORM"; | |||||||||||||||||||||||||||||||||||||||||
| 1056 | <p><form name="searchform" method="get"> | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | Field: <select name="searchfield">$searchfield_options</select> | ||||||||||||||||||||||||||||||||||||||||||
| 1058 | <select name="searchtype">$searchtype_options</select> | ||||||||||||||||||||||||||||||||||||||||||
| 1059 | <input name="q" id="searchquery" type="text" size="30" value="$display_q" /> | ||||||||||||||||||||||||||||||||||||||||||
| 1060 | <input name="o" type="hidden" value="$order_by_param"/> | ||||||||||||||||||||||||||||||||||||||||||
| 1061 | <input name="d" type="hidden" value="$order_by_direction"/> | ||||||||||||||||||||||||||||||||||||||||||
| 1062 | <input name="searchsubmit" type="submit" value="Search"/> | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | </form></p> | ||||||||||||||||||||||||||||||||||||||||||
| 1064 | SEARCHFORM | ||||||||||||||||||||||||||||||||||||||||||
| 1065 | |||||||||||||||||||||||||||||||||||||||||||
| 1066 | 14 | ▶ | if ($args->{query_auto_focus}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1067 | 14 | $html | |||||||||||||||||||||||||||||||||||||||||
| 1068 | .= "<script>document.getElementById(\"searchquery\").focus();</script>"; | ||||||||||||||||||||||||||||||||||||||||||
| 1069 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||||||||||||||||
| 1071 | # Explicitly select the columns we are displaying. (May have been filtered | ||||||||||||||||||||||||||||||||||||||||||
| 1072 | # by display_columns above.) | ||||||||||||||||||||||||||||||||||||||||||
| 1073 | |||||||||||||||||||||||||||||||||||||||||||
| 1074 | 14 | my @select_cols = map { $_->{COLUMN_NAME} } @$columns; | |||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||
| 1076 | # If we have some columns declared as foreign keys, though, we don't want to | ||||||||||||||||||||||||||||||||||||||||||
| 1077 | # see the raw values in the result; we'll add JOIN clauses to fetch the info | ||||||||||||||||||||||||||||||||||||||||||
| 1078 | # from the related table, so for now just select the defined label column | ||||||||||||||||||||||||||||||||||||||||||
| 1079 | # from the related table instead of the raw ID value. | ||||||||||||||||||||||||||||||||||||||||||
| 1080 | |||||||||||||||||||||||||||||||||||||||||||
| 1081 | # This _as_simplecrud_fk_ mechanism is clearly a bit of a hack. At some point we | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | # might want to pull in an existing solution for this--this is simple and | ||||||||||||||||||||||||||||||||||||||||||
| 1083 | # may have pitfalls that have already been solved in Catalyst/DBIC code. | ||||||||||||||||||||||||||||||||||||||||||
| 1084 | # For now, we're going with simple. git show 14cec4ea647 to see the | ||||||||||||||||||||||||||||||||||||||||||
| 1085 | # basic change (that's previous to the add of LEFT to the JOIN, though), if you want | ||||||||||||||||||||||||||||||||||||||||||
| 1086 | # to know exactly what to pull out when replacing this | ||||||||||||||||||||||||||||||||||||||||||
| 1087 | |||||||||||||||||||||||||||||||||||||||||||
| 1088 | 14 | my @foreign_cols; | |||||||||||||||||||||||||||||||||||||||||
| 1089 | my %fk_alias; # foreign key aliases for cases where we might have collisions | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | 14 | ▶ | if ($args->{foreign_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1091 | 1 | my $seen_table = {$table_name=>1}; | |||||||||||||||||||||||||||||||||||||||||
| 1092 | 1 | ▶ | while (my ($col, $foreign_key) = each(%{ $args->{foreign_keys} })) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1093 | 0 | @select_cols = grep { $_ ne $col } @select_cols; | |||||||||||||||||||||||||||||||||||||||||
| 1094 | 0 | my $raw_ftable = $foreign_key->{table}; | |||||||||||||||||||||||||||||||||||||||||
| 1095 | 0 | my $ftable_alias; | |||||||||||||||||||||||||||||||||||||||||
| 1096 | 0 | ▶ | if ($seen_table->{$raw_ftable}++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1097 | 0 | $ftable_alias = $fk_alias{ $col } = $dbh->quote_identifier($raw_ftable. "_as_simplecrud_fk_$seen_table->{$raw_ftable}"); | |||||||||||||||||||||||||||||||||||||||||
| 1098 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1099 | 0 | my $ftable = $dbh->quote_identifier($raw_ftable); | |||||||||||||||||||||||||||||||||||||||||
| 1100 | my $fcol | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | 0 | = $dbh->quote_identifier($foreign_key->{label_column}); | |||||||||||||||||||||||||||||||||||||||||
| 1102 | my $lcol | ||||||||||||||||||||||||||||||||||||||||||
| 1103 | 0 | ▶ | = $dbh->quote_identifier($args->{labels}{$col} || $col); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$args->{'labels'}{$col} || $col
MC/DC0%0/2
$args->{'labels'}{$col} || $col
$args->{'labels'}{$col}
$col
Truth table0%0/3
A || B
A= $args->{'labels'}{$col}B= $col
| |||||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||
| 1105 | 0 | ▶ | my $table_or_alias = $fk_alias{ $col } || $ftable; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$fk_alias{$col} || $ftable
MC/DC0%0/2
$fk_alias{$col} || $ftable
$fk_alias{$col}
$ftable
Truth table0%0/3
A || B
A= $fk_alias{$col}B= $ftable
| |||||||||||||||||||||||||||||||||||||||||||
| 1106 | 0 | push @foreign_cols, "$table_or_alias.$fcol AS $lcol"; | |||||||||||||||||||||||||||||||||||||||||
| 1107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||
| 1110 | 14 | my @custom_cols; | |||||||||||||||||||||||||||||||||||||||||
| 1111 | |||||||||||||||||||||||||||||||||||||||||||
| 1112 | # For backwards compatibility, understand custom_columns being a hashref, | ||||||||||||||||||||||||||||||||||||||||||
| 1113 | # and translate it | ||||||||||||||||||||||||||||||||||||||||||
| 1114 | 14 | ▶ | if (ref $args->{custom_columns} eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1115 | 0 | my @custom_cols_list; | |||||||||||||||||||||||||||||||||||||||||
| 1116 | 0 | for my $column_alias (keys %{ $args->{custom_columns} }) { | |||||||||||||||||||||||||||||||||||||||||
| 1117 | push @custom_cols_list, { | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | name => $column_alias, | ||||||||||||||||||||||||||||||||||||||||||
| 1119 | 0 | %{ $args->{custom_columns}{$column_alias} } | |||||||||||||||||||||||||||||||||||||||||
| 1120 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | 0 | $args->{custom_columns} = \@custom_cols_list; | |||||||||||||||||||||||||||||||||||||||||
| 1123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1124 | |||||||||||||||||||||||||||||||||||||||||||
| 1125 | # If we're not overriding a column with the same name, then add custom column | ||||||||||||||||||||||||||||||||||||||||||
| 1126 | 14 | ▶ | for my $custom_col_spec (@{ $args->{custom_columns} || [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1127 | 11 | my $column_alias = $custom_col_spec->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 1128 | 11 | ▶ | if( ! grep { $column_alias eq $_ } @select_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1129 | my $raw_column = $custom_col_spec->{raw_column} | ||||||||||||||||||||||||||||||||||||||||||
| 1130 | 4 | ▶ | or die "you must specify a raw_column that " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1131 | . "$column_alias will be built using"; | ||||||||||||||||||||||||||||||||||||||||||
| 1132 | 4 | ▶ | if ($raw_column =~ /^[\w_]+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1133 | 4 | push @custom_cols, "$table_name." | |||||||||||||||||||||||||||||||||||||||||
| 1134 | . $dbh->quote_identifier($raw_column) | ||||||||||||||||||||||||||||||||||||||||||
| 1135 | . " AS ". $dbh->quote_identifier($column_alias); | ||||||||||||||||||||||||||||||||||||||||||
| 1136 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1137 | 0 | push @custom_cols, "$raw_column AS $column_alias"; | |||||||||||||||||||||||||||||||||||||||||
| 1138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||||||||||||||||
| 1142 | my $col_list = join( | ||||||||||||||||||||||||||||||||||||||||||
| 1143 | ',', | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | map( | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | 14 | { $table_name . "." . $dbh->quote_identifier($_) } | |||||||||||||||||||||||||||||||||||||||||
| 1146 | @select_cols | ||||||||||||||||||||||||||||||||||||||||||
| 1147 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 1148 | @foreign_cols, # already assembled from quoted identifiers | ||||||||||||||||||||||||||||||||||||||||||
| 1149 | @custom_cols, | ||||||||||||||||||||||||||||||||||||||||||
| 1150 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1151 | my $add_actions | ||||||||||||||||||||||||||||||||||||||||||
| 1152 | = $args->{editable} | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | 14 | ▶ | ? ", $table_name.$key_column AS actions" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1154 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 1155 | 14 | my $query = "SELECT $col_list $add_actions FROM $table_name"; | |||||||||||||||||||||||||||||||||||||||||
| 1156 | 14 | my @binds; | |||||||||||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||||||||||||
| 1158 | # If we have foreign key relationship info, we need to join on those tables: | ||||||||||||||||||||||||||||||||||||||||||
| 1159 | 14 | ▶ | if ($args->{foreign_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1160 | 1 | ▶ | while (my ($col, $foreign_key) = each %{ $args->{foreign_keys} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1161 | 0 | my $ftable = $dbh->quote_identifier($foreign_key->{table}); | |||||||||||||||||||||||||||||||||||||||||
| 1162 | 0 | my $lkey = $dbh->quote_identifier($col); | |||||||||||||||||||||||||||||||||||||||||
| 1163 | 0 | my $rkey = $dbh->quote_identifier($foreign_key->{key_column}); | |||||||||||||||||||||||||||||||||||||||||
| 1164 | |||||||||||||||||||||||||||||||||||||||||||
| 1165 | # Identifiers quoted above, and $table_name quoted further up, so | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | # all safe to interpolate | ||||||||||||||||||||||||||||||||||||||||||
| 1167 | 0 | my $what_to_join = $ftable; | |||||||||||||||||||||||||||||||||||||||||
| 1168 | 0 | my $join_reference = $ftable; | |||||||||||||||||||||||||||||||||||||||||
| 1169 | 0 | ▶ | if (my $alias = $fk_alias{$col}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1170 | 0 | $what_to_join = " $ftable AS $alias "; | |||||||||||||||||||||||||||||||||||||||||
| 1171 | 0 | $join_reference = $alias; | |||||||||||||||||||||||||||||||||||||||||
| 1172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1173 | # If this join is not a left join, the list view only shows rows where the | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | # foreign key is defined and matching a row | ||||||||||||||||||||||||||||||||||||||||||
| 1175 | 0 | $query .= " LEFT JOIN $what_to_join ON $table_name.$lkey = $join_reference.$rkey "; | |||||||||||||||||||||||||||||||||||||||||
| 1176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1178 | |||||||||||||||||||||||||||||||||||||||||||
| 1179 | # If we have a query or a where_filter, we need to assemble a WHERE clause... | ||||||||||||||||||||||||||||||||||||||||||
| 1180 | 14 | my $where_filter = _get_where_filter_from_args($args); | |||||||||||||||||||||||||||||||||||||||||
| 1181 | 14 | ▶ | if (length $q || $where_filter) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $q or $where_filter
MC/DC50%1/2
length $q or $where_filter
length $q
$where_filter
Truth table66%2/3
A or B
A= length $qB= $where_filter
| |||||||||||||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||||||||||||
| 1183 | # Turn the $where_filter hashref into some SQL clauses and bind params, | ||||||||||||||||||||||||||||||||||||||||||
| 1184 | # which we'll add to with the user's search params shortly | ||||||||||||||||||||||||||||||||||||||||||
| 1185 | 4 | my ($where_filter_sql, @where_filter_binds) | |||||||||||||||||||||||||||||||||||||||||
| 1186 | = $dbh->generate_where_clauses($where_filter); | ||||||||||||||||||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||||||||||||||||
| 1188 | 4 | my (@search_wheres, @search_binds); | |||||||||||||||||||||||||||||||||||||||||
| 1189 | 4 | ▶ | if (length $q) { # this nested code is all for queries in $q | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1190 | my ($column_data) | ||||||||||||||||||||||||||||||||||||||||||
| 1191 | 12 | = grep { lc $_->{COLUMN_NAME} eq lc $searchfield } | |||||||||||||||||||||||||||||||||||||||||
| 1192 | 4 | @{$columns}; | |||||||||||||||||||||||||||||||||||||||||
| 1193 | 4 | debug( | |||||||||||||||||||||||||||||||||||||||||
| 1194 | "Searching on $column_data->{COLUMN_NAME} which is a " | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | . "$column_data->{TYPE_NAME}" | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1197 | 4 | ▶ | my $st = params->{searchtype} || $default_searchtype; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
params()->{'searchtype'} || $default_searchtype
MC/DC0%0/2
params()->{'searchtype'} || $default_searchtype
params()->{'searchtype'}
$default_searchtype
Truth table66%2/3
A || B
A= params()->{'searchtype'}B= $default_searchtype
| |||||||||||||||||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||||||||||||||||
| 1199 | 4 | ▶ | if ($column_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1200 | 4 | my $search_value = $q; | |||||||||||||||||||||||||||||||||||||||||
| 1201 | 4 | ▶ | if ($st eq 'c' || $st eq 'nc') { # contains or does not contain | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$st eq "c" or $st eq "nc"
MC/DC0%0/2
$st eq "c" or $st eq "nc"
$st eq "c"
$st eq "nc"
Truth table33%1/3
A or B
A= $st eq "c"B= $st eq "nc"
| |||||||||||||||||||||||||||||||||||||||||||
| 1202 | 0 | $search_value = '%' . $search_value . '%'; | |||||||||||||||||||||||||||||||||||||||||
| 1203 | } elsif ($st eq 'b') { # begins with | ||||||||||||||||||||||||||||||||||||||||||
| 1204 | 0 | $search_value = $search_value . '%'; | |||||||||||||||||||||||||||||||||||||||||
| 1205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||
| 1207 | 4 | my ($searchtype_row) = grep { $_->[0] eq $st } @searchtypes; | |||||||||||||||||||||||||||||||||||||||||
| 1208 | 4 | ▶ | my $cmp = $searchtype_row->[1]->{cmp} || '='; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$searchtype_row->[1]{'cmp'} || '='
MC/DC0%0/1
$searchtype_row->[1]{'cmp'} || '='
$searchtype_row->[1]{'cmp'}
Truth table50%1/2
A
A= $searchtype_row->[1]{'cmp'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1209 | 4 | push(@search_wheres, | |||||||||||||||||||||||||||||||||||||||||
| 1210 | "$table_name." | ||||||||||||||||||||||||||||||||||||||||||
| 1211 | . $dbh->quote_identifier($searchfield) | ||||||||||||||||||||||||||||||||||||||||||
| 1212 | . " $cmp ?" ); | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | 4 | push(@search_binds, $search_value); | |||||||||||||||||||||||||||||||||||||||||
| 1214 | |||||||||||||||||||||||||||||||||||||||||||
| 1215 | 4 | ▶ | my $matchtype = lc($searchtype_row->[1]->{name} || "equals"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$searchtype_row->[1]{'name'} || 'equals'
MC/DC0%0/1
$searchtype_row->[1]{'name'} || 'equals'
$searchtype_row->[1]{'name'}
Truth table50%1/2
A
A= $searchtype_row->[1]{'name'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1216 | 4 | $html | |||||||||||||||||||||||||||||||||||||||||
| 1217 | .= sprintf( | ||||||||||||||||||||||||||||||||||||||||||
| 1218 | "<p>Showing results from searching for '%s' %s '%s'", | ||||||||||||||||||||||||||||||||||||||||||
| 1219 | encode_entities($searchfield), $matchtype, encode_entities($q) | ||||||||||||||||||||||||||||||||||||||||||
| 1220 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1221 | $html .= sprintf '—<a href="%s">Reset search</a></p>', | ||||||||||||||||||||||||||||||||||||||||||
| 1222 | 4 | _external_url($args->{dancer_prefix}, $args->{prefix}); | |||||||||||||||||||||||||||||||||||||||||
| 1223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1225 | # add the 'where' clauses to $query and the binds to @binds | ||||||||||||||||||||||||||||||||||||||||||
| 1226 | 4 | $query .= " where " . join( " AND ", grep { length $_ } ($where_filter_sql, @search_wheres)); | |||||||||||||||||||||||||||||||||||||||||
| 1227 | 4 | push(@binds, @where_filter_binds, @search_binds); | |||||||||||||||||||||||||||||||||||||||||
| 1228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||||||||||||
| 1230 | 14 | ▶ | if ($args->{downloadable}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1231 | 0 | my $qt = uri_escape($q); | |||||||||||||||||||||||||||||||||||||||||
| 1232 | 0 | my $sf = uri_escape($searchfield); | |||||||||||||||||||||||||||||||||||||||||
| 1233 | 0 | ▶ | my $st = uri_escape(params->{searchtype} || $default_searchtype); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
params()->{'searchtype'} || $default_searchtype
MC/DC0%0/2
params()->{'searchtype'} || $default_searchtype
params()->{'searchtype'}
$default_searchtype
Truth table0%0/3
A || B
A= params()->{'searchtype'}B= $default_searchtype
| |||||||||||||||||||||||||||||||||||||||||||
| 1234 | 0 | ▶ | my $o = uri_escape(params->{'o'} || ""); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
params()->{'o'} || ''
MC/DC0%0/1
params()->{'o'} || ''
params()->{'o'}
Truth table0%0/2
A
A= params()->{'o'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1235 | 0 | ▶ | my $d = uri_escape(params->{'d'} || ""); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
params()->{'d'} || ''
MC/DC0%0/1
params()->{'d'} || ''
params()->{'d'}
Truth table0%0/2
A
A= params()->{'d'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1236 | 0 | ▶ | my $page = uri_escape(params->{'p'} || 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
params()->{'p'} || 0
MC/DC0%0/1
params()->{'p'} || 0
params()->{'p'}
Truth table0%0/2
A
A= params()->{'p'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||||||||||||||||
| 1238 | 0 | my @formats = qw/csv tabular json xml/; | |||||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||||||
| 1240 | my $url = _external_url($args->{dancer_prefix}, $args->{prefix}) | ||||||||||||||||||||||||||||||||||||||||||
| 1241 | 0 | . "?o=$o&d=$d&q=$qt&searchfield=$sf&searchtype=$st&p=$page"; | |||||||||||||||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||||||||||||||||
| 1243 | $html | ||||||||||||||||||||||||||||||||||||||||||
| 1244 | .= "<p>Download as: " | ||||||||||||||||||||||||||||||||||||||||||
| 1245 | 0 | . join(", ", map { "<a href=\"$url&format=$_\">$_</a>" } @formats) | |||||||||||||||||||||||||||||||||||||||||
| 1246 | . "<p>"; | ||||||||||||||||||||||||||||||||||||||||||
| 1247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||||||||||||||||
| 1249 | 14 | my %columns_sort_options; | |||||||||||||||||||||||||||||||||||||||||
| 1250 | 14 | ▶ | if ($args->{sortable}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1251 | 4 | my $qt = uri_escape($q); | |||||||||||||||||||||||||||||||||||||||||
| 1252 | 4 | my $sf = uri_escape($searchfield); | |||||||||||||||||||||||||||||||||||||||||
| 1253 | 4 | ▶ | my $st = uri_escape(params->{searchtype} || $default_searchtype); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
params()->{'searchtype'} || $default_searchtype
MC/DC0%0/2
params()->{'searchtype'} || $default_searchtype
params()->{'searchtype'}
$default_searchtype
Truth table33%1/3
A || B
A= params()->{'searchtype'}B= $default_searchtype
| |||||||||||||||||||||||||||||||||||||||||||
| 1254 | 4 | ▶ | my $order_by_column = uri_escape(params->{'o'}) || $key_column; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
uri_escape(params()->{'o'}) || $key_column
MC/DC0%0/2
uri_escape(params()->{'o'}) || $key_column
uri_escape(params()->{'o'})
$key_column
Truth table33%1/3
A || B
A= uri_escape(params()->{'o'})B= $key_column
| |||||||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||||||
| 1256 | # Invalid column name ? discard it | ||||||||||||||||||||||||||||||||||||||||||
| 1257 | 4 | my $valid = grep { $_->{COLUMN_NAME} eq $order_by_column } @$columns; | |||||||||||||||||||||||||||||||||||||||||
| 1258 | 4 | ▶ | $order_by_column = $key_column unless $valid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1259 | 4 | my $order_by_table = $table_name; | |||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||
| 1261 | my $order_by_direction | ||||||||||||||||||||||||||||||||||||||||||
| 1262 | 4 | ▶ | = (exists params->{'d'} && params->{'d'} eq "desc") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists params()->{"d"} and params()->{"d"} eq "desc"
MC/DC0%0/2
exists params()->{"d"} and params()->{"d"} eq "desc"
exists params()->{"d"}
params()->{"d"} eq "desc"
Truth table33%1/3
A and B
A= exists params()->{"d"}B= params()->{"d"} eq "desc"
| |||||||||||||||||||||||||||||||||||||||||||
| 1263 | ? "desc" | ||||||||||||||||||||||||||||||||||||||||||
| 1264 | : "asc"; | ||||||||||||||||||||||||||||||||||||||||||
| 1265 | 4 | ▶ | my $opposite_order_by_direction | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1266 | = ($order_by_direction eq "asc") ? "desc" : "asc"; | ||||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||
| 1268 | # Get a list of all columns (normal, and custom_columns), then assemble | ||||||||||||||||||||||||||||||||||||||||||
| 1269 | # the names and labels to pass to HTML::Table::FromDatabase | ||||||||||||||||||||||||||||||||||||||||||
| 1270 | 4 | my @all_cols = map { $_->{COLUMN_NAME} } @$columns; | |||||||||||||||||||||||||||||||||||||||||
| 1271 | %columns_sort_options = map { | ||||||||||||||||||||||||||||||||||||||||||
| 1272 | 4 | my $col_name = $_; | |||||||||||||||||||||||||||||||||||||||||
| 1273 | 12 | my $direction = $order_by_direction; | |||||||||||||||||||||||||||||||||||||||||
| 1274 | 12 | my $direction_char = ""; | |||||||||||||||||||||||||||||||||||||||||
| 1275 | 12 | my $friendly_name = $col_name; | |||||||||||||||||||||||||||||||||||||||||
| 1276 | 12 | ▶ | if ($args->{labels}{$col_name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1277 | 0 | $friendly_name = $args->{labels}{$col_name}; | |||||||||||||||||||||||||||||||||||||||||
| 1278 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1279 | 12 | $friendly_name = _prettify_column_name($friendly_name); | |||||||||||||||||||||||||||||||||||||||||
| 1280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1281 | 12 | ▶ | if ($col_name eq $order_by_column) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1282 | 4 | $direction = $opposite_order_by_direction; | |||||||||||||||||||||||||||||||||||||||||
| 1283 | 4 | ▶ | $direction_char = ($direction eq "asc") ? "↑" : "↓"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1285 | my $url = _external_url($args->{dancer_prefix}, $args->{prefix}) | ||||||||||||||||||||||||||||||||||||||||||
| 1286 | 12 | . "?o=$col_name&d=$direction&q=$q&searchfield=$sf&searchtype=$st"; | |||||||||||||||||||||||||||||||||||||||||
| 1287 | 12 | $col_name => | |||||||||||||||||||||||||||||||||||||||||
| 1288 | "<a href=\"$url\">$friendly_name $direction_char</a>"; | ||||||||||||||||||||||||||||||||||||||||||
| 1289 | } @all_cols; | ||||||||||||||||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||||||||||||||||
| 1291 | # And for custom columns, do the prettification, but don't include a | ||||||||||||||||||||||||||||||||||||||||||
| 1292 | # link for sorting unless we're overriding the display of an already | ||||||||||||||||||||||||||||||||||||||||||
| 1293 | # sortable column. We can't sort non-overridden custom columns (sorting is | ||||||||||||||||||||||||||||||||||||||||||
| 1294 | # done by SQL, and the custom column values are calculated after we get | ||||||||||||||||||||||||||||||||||||||||||
| 1295 | # the results from the SQL query, so to support sorting by them we'd | ||||||||||||||||||||||||||||||||||||||||||
| 1296 | # have to stop getting the database to sort the data and sort it | ||||||||||||||||||||||||||||||||||||||||||
| 1297 | # ourselves afterwards). | ||||||||||||||||||||||||||||||||||||||||||
| 1298 | 4 | ▶ | if (exists $args->{custom_columns}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1299 | 4 | for my $custom_column_name ( | |||||||||||||||||||||||||||||||||||||||||
| 1300 | 7 | map { $_->{name} } @{ $args->{custom_columns} } | |||||||||||||||||||||||||||||||||||||||||
| 1301 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1302 | 7 | ▶ | if ( !grep { $_ eq $custom_column_name } @all_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1303 | 2 | $columns_sort_options{$custom_column_name} | |||||||||||||||||||||||||||||||||||||||||
| 1304 | = _prettify_column_name($custom_column_name); | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1308 | |||||||||||||||||||||||||||||||||||||||||||
| 1309 | 4 | ▶ | if (exists $args->{foreign_keys} and exists $args->{foreign_keys}{$order_by_column}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args->{"foreign_keys"} and exists $args->{"foreign_keys"}{$order_by_column}
MC/DC0%0/2
exists $args->{"foreign_keys"} and exists $args->{"foreign_keys"}{$order_by_column}
exists $args->{"foreign_keys"}
exists $args->{"foreign_keys"}{$order_by_column}
Truth table33%1/3
A and B
A= exists $args->{"foreign_keys"}B= exists $args->{"foreign_keys"}{$order_by_column}
| |||||||||||||||||||||||||||||||||||||||||||
| 1310 | 0 | my $fk = $args->{foreign_keys}{$order_by_column}; | |||||||||||||||||||||||||||||||||||||||||
| 1311 | 0 | $order_by_column = $fk->{label_column}; | |||||||||||||||||||||||||||||||||||||||||
| 1312 | 0 | $order_by_table = $fk->{table}; | |||||||||||||||||||||||||||||||||||||||||
| 1313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||||||||||||||||
| 1315 | 4 | $query .= " ORDER BY " | |||||||||||||||||||||||||||||||||||||||||
| 1316 | . $dbh->quote_identifier($order_by_table) . "." | ||||||||||||||||||||||||||||||||||||||||||
| 1317 | . $dbh->quote_identifier($order_by_column) | ||||||||||||||||||||||||||||||||||||||||||
| 1318 | . " $order_by_direction "; | ||||||||||||||||||||||||||||||||||||||||||
| 1319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||||||
| 1321 | 14 | ▶ | if ($args->{paginate} && $args->{paginate} =~ /^\d+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"paginate"} and $args->{"paginate"} =~ /^\d+$/
MC/DC0%0/2
$args->{"paginate"} and $args->{"paginate"} =~ /^\d+$/
$args->{"paginate"}
$args->{"paginate"} =~ /^\d+$/
Truth table33%1/3
A and B
A= $args->{"paginate"}B= $args->{"paginate"} =~ /^\d+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 1322 | 0 | my $page_size = $args->{paginate}; | |||||||||||||||||||||||||||||||||||||||||
| 1323 | |||||||||||||||||||||||||||||||||||||||||||
| 1324 | 0 | my $qt = uri_escape($q); | |||||||||||||||||||||||||||||||||||||||||
| 1325 | 0 | my $sf = uri_escape($searchfield); | |||||||||||||||||||||||||||||||||||||||||
| 1326 | 0 | ▶ | my $st = uri_escape(params->{searchtype} || $default_searchtype); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
params()->{'searchtype'} || $default_searchtype
MC/DC0%0/2
params()->{'searchtype'} || $default_searchtype
params()->{'searchtype'}
$default_searchtype
Truth table0%0/3
A || B
A= params()->{'searchtype'}B= $default_searchtype
| |||||||||||||||||||||||||||||||||||||||||||
| 1327 | 0 | ▶ | my $o = uri_escape(params->{'o'} || ""); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
params()->{'o'} || ''
MC/DC0%0/1
params()->{'o'} || ''
params()->{'o'}
Truth table0%0/2
A
A= params()->{'o'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1328 | 0 | ▶ | my $d = uri_escape(params->{'d'} || ""); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
params()->{'d'} || ''
MC/DC0%0/1
params()->{'d'} || ''
params()->{'d'}
Truth table0%0/2
A
A= params()->{'d'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1329 | 0 | ▶ | my $page = uri_escape(params->{'p'} || 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
params()->{'p'} || 0
MC/DC0%0/1
params()->{'p'} || 0
params()->{'p'}
Truth table0%0/2
A
A= params()->{'p'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1330 | 0 | ▶ | $page = 0 unless $page =~ /^\d+$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1331 | |||||||||||||||||||||||||||||||||||||||||||
| 1332 | 0 | my $offset = $page_size * $page; | |||||||||||||||||||||||||||||||||||||||||
| 1333 | 0 | my $limit = $page_size; | |||||||||||||||||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||||||||||||||||
| 1335 | my $url = _external_url($args->{dancer_prefix}, $args->{prefix}) | ||||||||||||||||||||||||||||||||||||||||||
| 1336 | 0 | . "?o=$o&d=$d&q=$qt&searchfield=$sf&searchtype=$st"; | |||||||||||||||||||||||||||||||||||||||||
| 1337 | 0 | $html .= "<p>"; | |||||||||||||||||||||||||||||||||||||||||
| 1338 | 0 | $html .= "<table class=\"$paginate_table_class\"><tr>"; | |||||||||||||||||||||||||||||||||||||||||
| 1339 | |||||||||||||||||||||||||||||||||||||||||||
| 1340 | 0 | ▶ | if ($page > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1341 | 0 | $html | |||||||||||||||||||||||||||||||||||||||||
| 1342 | .= sprintf( | ||||||||||||||||||||||||||||||||||||||||||
| 1343 | "<td><a href=\"%s&p=%d\">← prev. page</a></td>", | ||||||||||||||||||||||||||||||||||||||||||
| 1344 | $url, $page - 1) | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1346 | 0 | $html .= "<td>← prev. page </td>"; | |||||||||||||||||||||||||||||||||||||||||
| 1347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1348 | 0 | $html .= sprintf( | |||||||||||||||||||||||||||||||||||||||||
| 1349 | "<td>Showing page %d (records %d to %d)", | ||||||||||||||||||||||||||||||||||||||||||
| 1350 | $page + 1, | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | $offset + 1, | ||||||||||||||||||||||||||||||||||||||||||
| 1352 | $offset + 1 + $limit | ||||||||||||||||||||||||||||||||||||||||||
| 1353 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1354 | 0 | $html .= "</td>"; | |||||||||||||||||||||||||||||||||||||||||
| 1355 | 0 | $html .= sprintf("<td><a href=\"%s&p=%d\">next page →</a>", | |||||||||||||||||||||||||||||||||||||||||
| 1356 | $url, $page + 1); | ||||||||||||||||||||||||||||||||||||||||||
| 1357 | 0 | $html .= "</td></tr></table>"; | |||||||||||||||||||||||||||||||||||||||||
| 1358 | |||||||||||||||||||||||||||||||||||||||||||
| 1359 | 0 | $query .= " LIMIT $limit OFFSET $offset "; | |||||||||||||||||||||||||||||||||||||||||
| 1360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||||||||||||||||
| 1362 | 14 | debug("Running query: $query"); | |||||||||||||||||||||||||||||||||||||||||
| 1363 | 14 | my $sth = $dbh->prepare($query); | |||||||||||||||||||||||||||||||||||||||||
| 1364 | 14 | ▶ | $sth->execute(@binds) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1365 | or die "Failed to query for records in $table_name - " | ||||||||||||||||||||||||||||||||||||||||||
| 1366 | . $dbh->errstr; | ||||||||||||||||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||||||||||||||||
| 1368 | 14 | ▶ | if ($args->{downloadable} && params->{format}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"downloadable"} and params()->{"format"}
MC/DC0%0/2
$args->{"downloadable"} and params()->{"format"}
$args->{"downloadable"}
params()->{"format"}
Truth table33%1/3
A and B
A= $args->{"downloadable"}B= params()->{"format"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||||||||||||||||
| 1370 | ##Return results as a downloaded file, instead of generating the HTML table. | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | 0 | return _return_downloadable_query($args, $sth, params->{format}); | |||||||||||||||||||||||||||||||||||||||||
| 1372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1373 | |||||||||||||||||||||||||||||||||||||||||||
| 1374 | 14 | my @custom_callbacks = (); | |||||||||||||||||||||||||||||||||||||||||
| 1375 | 14 | ▶ | for my $custom_col_spec (@{ $args->{custom_columns} || [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1376 | push @custom_callbacks, { | ||||||||||||||||||||||||||||||||||||||||||
| 1377 | column=>$custom_col_spec->{name}, | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | 11 | ▶ | transform=> ($custom_col_spec->{transform} or sub { return shift;}), | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$custom_col_spec->{'transform'} || sub {
return shift();
}
MC/DC0%0/2
$custom_col_spec->{'transform'} || sub {
return shift();
}
$custom_col_spec->{'transform'}
sub {
return shift();
}
Truth table33%1/3
A || B
A= $custom_col_spec->{'transform'}B= sub {
return shift();
}
| |||||||||||||||||||||||||||||||||||||||||||
| 1379 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1380 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1381 | |||||||||||||||||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||||||||||||||||
| 1383 | my $table = HTML::Table::FromDatabase->new( | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | -sth => $sth, | ||||||||||||||||||||||||||||||||||||||||||
| 1385 | -border => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 1386 | -callbacks => [ | ||||||||||||||||||||||||||||||||||||||||||
| 1387 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1388 | column => 'actions', | ||||||||||||||||||||||||||||||||||||||||||
| 1389 | transform => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1390 | 16 | ▶ | my $id = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=19.3
| |||||||||||||||||||||||||||||||||||||||||||
| 1391 | 16 | my $action_links; | |||||||||||||||||||||||||||||||||||||||||
| 1392 | 16 | ▶ | if ($args->{editable} && _has_permission('edit', $args)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"editable"} and _has_permission("edit", $args)
MC/DC0%0/2
$args->{"editable"} and _has_permission("edit", $args)
$args->{"editable"}
_has_permission("edit", $args)
Truth table33%1/3
A and B
A= $args->{"editable"}B= _has_permission("edit", $args)
| |||||||||||||||||||||||||||||||||||||||||||
| 1393 | my $edit_url | ||||||||||||||||||||||||||||||||||||||||||
| 1394 | = _external_url( | ||||||||||||||||||||||||||||||||||||||||||
| 1395 | $args->{dancer_prefix}, $args->{prefix}, | ||||||||||||||||||||||||||||||||||||||||||
| 1396 | 16 | "/edit/$id" | |||||||||||||||||||||||||||||||||||||||||
| 1397 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1398 | 16 | $action_links | |||||||||||||||||||||||||||||||||||||||||
| 1399 | .= qq[<a href="$edit_url" class="edit_link">Edit</a>]; | ||||||||||||||||||||||||||||||||||||||||||
| 1400 | 16 | ▶ | if ($args->{deletable} && _has_permission('edit', $args)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"deletable"} and _has_permission("edit", $args)
MC/DC0%0/2
$args->{"deletable"} and _has_permission("edit", $args)
$args->{"deletable"}
_has_permission("edit", $args)
Truth table33%1/3
A and B
A= $args->{"deletable"}B= _has_permission("edit", $args)
| |||||||||||||||||||||||||||||||||||||||||||
| 1401 | my $del_url =_external_url( | ||||||||||||||||||||||||||||||||||||||||||
| 1402 | $args->{dancer_prefix}, $args->{prefix}, | ||||||||||||||||||||||||||||||||||||||||||
| 1403 | 0 | "/delete/$id" | |||||||||||||||||||||||||||||||||||||||||
| 1404 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1405 | 0 | $action_links | |||||||||||||||||||||||||||||||||||||||||
| 1406 | .= qq[ / <a href="$del_url" class="delete_link"] | ||||||||||||||||||||||||||||||||||||||||||
| 1407 | . qq[ onclick="delrec('$id'); return false;">] | ||||||||||||||||||||||||||||||||||||||||||
| 1408 | . qq[Delete</a>]; | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1411 | 16 | return $action_links; | |||||||||||||||||||||||||||||||||||||||||
| 1412 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 1413 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | 14 | @custom_callbacks, | |||||||||||||||||||||||||||||||||||||||||
| 1415 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 1416 | -rename_headers => \%columns_sort_options, | ||||||||||||||||||||||||||||||||||||||||||
| 1417 | -html => 'escape', | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | -class => $table_class, | ||||||||||||||||||||||||||||||||||||||||||
| 1419 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | |||||||||||||||||||||||||||||||||||||||||||
| 1421 | # apply custom columns' column_classes as specified. Can this be done via HTML::Table::FromDatabase->new() above? | ||||||||||||||||||||||||||||||||||||||||||
| 1422 | 14 | my @all_column_names = ( (map { $_->{COLUMN_NAME} } @$columns), (map { $_->{name} } @{$args->{custom_columns}}) ); | |||||||||||||||||||||||||||||||||||||||||
| 1423 | 14 | ▶ | for my $custom_col_spec (@{ $args->{custom_columns} || [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1424 | 11 | ▶ | if (my $column_class = $custom_col_spec->{column_class}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1425 | 11 | ▶ | my $first_index = first_index { $_ eq $custom_col_spec->{name} } uniq @all_column_names; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1426 | 11 | ▶ | die "Cannot find index of column '$custom_col_spec->{name}'" if ($first_index == -1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1427 | 11 | $table->setColClass( 1 + $first_index, $column_class ); | |||||||||||||||||||||||||||||||||||||||||
| 1428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1429 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1430 | |||||||||||||||||||||||||||||||||||||||||||
| 1431 | 14 | ▶ | $html .= $table->getTable || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$table->getTable || ''
MC/DC100%1/1
$table->getTable || ''
$table->getTable
Truth table100%2/2
A
A= $table->getTable
| |||||||||||||||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||||||||||||||
| 1433 | 14 | ▶ | if ($args->{addable} && _has_permission('edit', $args)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"addable"} and _has_permission("edit", $args)
MC/DC50%1/2
$args->{"addable"} and _has_permission("edit", $args)
$args->{"addable"}
_has_permission("edit", $args)
Truth table66%2/3
A and B
A= $args->{"addable"}B= _has_permission("edit", $args)
| |||||||||||||||||||||||||||||||||||||||||||
| 1434 | $html .= sprintf '<a href="%s">Add a new %s</a></p>', | ||||||||||||||||||||||||||||||||||||||||||
| 1435 | _external_url($args->{dancer_prefix}, $args->{prefix}, '/add'), | ||||||||||||||||||||||||||||||||||||||||||
| 1436 | 1 | $args->{record_title}; | |||||||||||||||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||||||||||||||||
| 1438 | # Append a little Javascript which asks for confirmation that they'd | ||||||||||||||||||||||||||||||||||||||||||
| 1439 | # like to delete the record, then makes a POST request via a hidden | ||||||||||||||||||||||||||||||||||||||||||
| 1440 | # form. This could be made AJAXy in future. | ||||||||||||||||||||||||||||||||||||||||||
| 1441 | my $del_action = _external_url( | ||||||||||||||||||||||||||||||||||||||||||
| 1442 | 1 | $args->{dancer_prefix}, $args->{prefix}, '/delete' | |||||||||||||||||||||||||||||||||||||||||
| 1443 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1444 | 1 | $html .= <<DELETEJS; | |||||||||||||||||||||||||||||||||||||||||
| 1445 | <form name="deleteform" method="post" action="$del_action"> | ||||||||||||||||||||||||||||||||||||||||||
| 1446 | <input name="record_id" type="hidden"> | ||||||||||||||||||||||||||||||||||||||||||
| 1447 | </form> | ||||||||||||||||||||||||||||||||||||||||||
| 1448 | <script language="Javascript"> | ||||||||||||||||||||||||||||||||||||||||||
| 1449 | function delrec(record_id) { | ||||||||||||||||||||||||||||||||||||||||||
| 1450 | if (confirm('Confirm you wish to delete this record?')) { | ||||||||||||||||||||||||||||||||||||||||||
| 1451 | document.deleteform.rowid.value = record_id; | ||||||||||||||||||||||||||||||||||||||||||
| 1452 | document.deleteform.submit(); | ||||||||||||||||||||||||||||||||||||||||||
| 1453 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1455 | </script> | ||||||||||||||||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||||||||||||||||
| 1457 | DELETEJS | ||||||||||||||||||||||||||||||||||||||||||
| 1458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1459 | |||||||||||||||||||||||||||||||||||||||||||
| 1460 | 14 | return _apply_template($html, $args->{'template'}, $args->{'record_title'}); | |||||||||||||||||||||||||||||||||||||||||
| 1461 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||||||||||||||||
| 1463 | sub _apply_template { | ||||||||||||||||||||||||||||||||||||||||||
| 1464 | 19 | ▶ | my ($html, $template, $title) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_template: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 1465 | |||||||||||||||||||||||||||||||||||||||||||
| 1466 | 19 | ▶ | if ($template) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1467 | 0 | return template $template, { simple_crud => $html, record_title => $title }; | |||||||||||||||||||||||||||||||||||||||||
| 1468 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1469 | 19 | return engine('template')->apply_layout($html); | |||||||||||||||||||||||||||||||||||||||||
| 1470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||||||||||||||||
| 1473 | sub _return_downloadable_query { | ||||||||||||||||||||||||||||||||||||||||||
| 1474 | 0 | ▶ | my ($args, $sth, $format) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_return_downloadable_query: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||||||||||||||||
| 1476 | 0 | my $output; | |||||||||||||||||||||||||||||||||||||||||
| 1477 | |||||||||||||||||||||||||||||||||||||||||||
| 1478 | ## Generate an informative filename | ||||||||||||||||||||||||||||||||||||||||||
| 1479 | 0 | my $filename = $args->{db_table}; | |||||||||||||||||||||||||||||||||||||||||
| 1480 | 0 | ▶ | if (params->{'o'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1481 | 0 | my $order = params->{'o'}; | |||||||||||||||||||||||||||||||||||||||||
| 1482 | 0 | $order =~ s/[^\w\.\-]+/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 1483 | 0 | $filename .= "__sorted_by_" . $order; | |||||||||||||||||||||||||||||||||||||||||
| 1484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1485 | 0 | my $q = _defined_or_empty(params->{'q'}); | |||||||||||||||||||||||||||||||||||||||||
| 1486 | 0 | ▶ | if (length($q)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1487 | 0 | my $query = $q; | |||||||||||||||||||||||||||||||||||||||||
| 1488 | 0 | $query =~ s/[^\w\.\-]+/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 1489 | 0 | $filename .= "__query_" . $query; | |||||||||||||||||||||||||||||||||||||||||
| 1490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1491 | 0 | ▶ | if (params->{'p'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1492 | 0 | my $page = params->{'p'}; | |||||||||||||||||||||||||||||||||||||||||
| 1493 | 0 | $page =~ s/[^0-9]+/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 1494 | 0 | $filename .= "__page_" . $page; | |||||||||||||||||||||||||||||||||||||||||
| 1495 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1496 | |||||||||||||||||||||||||||||||||||||||||||
| 1497 | ## Generate data in the requested format | ||||||||||||||||||||||||||||||||||||||||||
| 1498 | 0 | ▶ | if ($format eq "tabular") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1499 | 0 | header('Content-Type' => 'text/tab-separated-values'); | |||||||||||||||||||||||||||||||||||||||||
| 1500 | 0 | header('Content-Disposition' => | |||||||||||||||||||||||||||||||||||||||||
| 1501 | "attachment; filename=\"$filename.txt\""); | ||||||||||||||||||||||||||||||||||||||||||
| 1502 | 0 | my $aref = $sth->{NAME}; | |||||||||||||||||||||||||||||||||||||||||
| 1503 | 0 | $output = join("\t", @$aref) . "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1504 | 0 | ▶ | while ($aref = $sth->fetchrow_arrayref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1505 | 0 | $output .= join("\t", @{$aref}) . "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1507 | } elsif ($format eq "csv") { | ||||||||||||||||||||||||||||||||||||||||||
| 1508 | 0 | eval { require Text::CSV }; | |||||||||||||||||||||||||||||||||||||||||
| 1509 | return | ||||||||||||||||||||||||||||||||||||||||||
| 1510 | 0 | ▶ | "Error: required module Text::CSV not installed. Can't generate CSV file." | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1511 | if $@; | ||||||||||||||||||||||||||||||||||||||||||
| 1512 | |||||||||||||||||||||||||||||||||||||||||||
| 1513 | 0 | header('Content-Type' => 'text/comma-separated-values'); | |||||||||||||||||||||||||||||||||||||||||
| 1514 | 0 | header('Content-Disposition' => | |||||||||||||||||||||||||||||||||||||||||
| 1515 | "attachment; filename=\"$filename.csv\""); | ||||||||||||||||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||||||||||||||||
| 1517 | 0 | my $csv = Text::CSV->new(); | |||||||||||||||||||||||||||||||||||||||||
| 1518 | 0 | my $aref = $sth->{NAME}; | |||||||||||||||||||||||||||||||||||||||||
| 1519 | 0 | $csv->combine(@{$aref}); | |||||||||||||||||||||||||||||||||||||||||
| 1520 | 0 | $output = $csv->string() . "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1521 | 0 | ▶ | while ($aref = $sth->fetchrow_arrayref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1522 | 0 | $csv->combine(@{$aref}); | |||||||||||||||||||||||||||||||||||||||||
| 1523 | 0 | $output .= $csv->string() . "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1525 | } elsif ($format eq "json") { | ||||||||||||||||||||||||||||||||||||||||||
| 1526 | 0 | header('Content-Type' => 'text/json'); | |||||||||||||||||||||||||||||||||||||||||
| 1527 | 0 | header('Content-Disposition' => | |||||||||||||||||||||||||||||||||||||||||
| 1528 | "attachment; filename=\"$filename.json\""); | ||||||||||||||||||||||||||||||||||||||||||
| 1529 | 0 | $output = to_json($sth->fetchall_arrayref({})); | |||||||||||||||||||||||||||||||||||||||||
| 1530 | } elsif ($format eq "xml") { | ||||||||||||||||||||||||||||||||||||||||||
| 1531 | 0 | header('Content-Type' => 'text/xml'); | |||||||||||||||||||||||||||||||||||||||||
| 1532 | 0 | header('Content-Disposition' => | |||||||||||||||||||||||||||||||||||||||||
| 1533 | "attachment; filename=\"$filename.xml\""); | ||||||||||||||||||||||||||||||||||||||||||
| 1534 | 0 | $output = to_xml($sth->fetchall_arrayref({})); | |||||||||||||||||||||||||||||||||||||||||
| 1535 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1536 | 0 | $output = "Error: unknown format $format"; | |||||||||||||||||||||||||||||||||||||||||
| 1537 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1538 | |||||||||||||||||||||||||||||||||||||||||||
| 1539 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||||||
| 1540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||||||||||||||||
| 1542 | # Given a table name, return an arrayref of hashrefs describing each column in | ||||||||||||||||||||||||||||||||||||||||||
| 1543 | # the table. | ||||||||||||||||||||||||||||||||||||||||||
| 1544 | # Expect to see the following keys: | ||||||||||||||||||||||||||||||||||||||||||
| 1545 | # COLUMN_NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1546 | # COLUMN_SIZE | ||||||||||||||||||||||||||||||||||||||||||
| 1547 | # NULLABLE | ||||||||||||||||||||||||||||||||||||||||||
| 1548 | # DATETIME ? | ||||||||||||||||||||||||||||||||||||||||||
| 1549 | # TYPE_NAME (e.g. INT, VARCHAR, ENUM) | ||||||||||||||||||||||||||||||||||||||||||
| 1550 | # MySQL-specific stuff includes: | ||||||||||||||||||||||||||||||||||||||||||
| 1551 | # mysql_type_name (e.g. "enum('One', 'Two', 'Three')" | ||||||||||||||||||||||||||||||||||||||||||
| 1552 | # mysql_is_pri_key | ||||||||||||||||||||||||||||||||||||||||||
| 1553 | # mysql_values (for an enum, ["One", "Two", "Three"] | ||||||||||||||||||||||||||||||||||||||||||
| 1554 | sub _find_columns { | ||||||||||||||||||||||||||||||||||||||||||
| 1555 | 19 | ▶ | my ($dbh, $table_name) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_columns: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1556 | 19 | ▶ | my $sth = $dbh->column_info(undef, undef, $table_name, undef) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1557 | or die "Failed to get column info for $table_name - " . $dbh->errstr; | ||||||||||||||||||||||||||||||||||||||||||
| 1558 | 19 | my @columns; | |||||||||||||||||||||||||||||||||||||||||
| 1559 | 19 | ▶ | while (my $col = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1560 | |||||||||||||||||||||||||||||||||||||||||||
| 1561 | # Push a copy of the hashref, as I think DBI re-uses them | ||||||||||||||||||||||||||||||||||||||||||
| 1562 | 57 | push @columns, {%$col}; | |||||||||||||||||||||||||||||||||||||||||
| 1563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1564 | |||||||||||||||||||||||||||||||||||||||||||
| 1565 | 19 | ▶ | die "no columns for table [$table_name]--are you sure this table exists in the database [$dbh->{Driver}->{Name}:$dbh->{Name}]?" unless @columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1566 | |||||||||||||||||||||||||||||||||||||||||||
| 1567 | # Return the columns, sorted by their position in the table: | ||||||||||||||||||||||||||||||||||||||||||
| 1568 | 19 | return [sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} } | |||||||||||||||||||||||||||||||||||||||||
| 1569 | @columns]; | ||||||||||||||||||||||||||||||||||||||||||
| 1570 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||||||||||||||||
| 1572 | # Given parts of an URL, assemble them together, prepending the current prefix | ||||||||||||||||||||||||||||||||||||||||||
| 1573 | # setting if needed, and taking care to get slashes right. | ||||||||||||||||||||||||||||||||||||||||||
| 1574 | # e.g. for the following example: | ||||||||||||||||||||||||||||||||||||||||||
| 1575 | # prefix '/foo'; | ||||||||||||||||||||||||||||||||||||||||||
| 1576 | # simple_crud( prefix => '/bar', ....); | ||||||||||||||||||||||||||||||||||||||||||
| 1577 | # calling: _construct_url($args{prefix}, '/baz') | ||||||||||||||||||||||||||||||||||||||||||
| 1578 | # would return: /foo/bar/baz | ||||||||||||||||||||||||||||||||||||||||||
| 1579 | sub _construct_url { | ||||||||||||||||||||||||||||||||||||||||||
| 1580 | 58 | ▶ | my @url_parts = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_construct_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||||||||||||||||
| 1582 | # Just concatenate all parts together, then deal with multiple slashes. | ||||||||||||||||||||||||||||||||||||||||||
| 1583 | # This could be problematic if any URL was ever supposed to contain multiple | ||||||||||||||||||||||||||||||||||||||||||
| 1584 | # slashes, but that shouldn't be an issue here. | ||||||||||||||||||||||||||||||||||||||||||
| 1585 | 58 | my $url = '/' . join '/', @url_parts; | |||||||||||||||||||||||||||||||||||||||||
| 1586 | 58 | $url =~ s{/{2,}}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 1587 | 58 | return $url; | |||||||||||||||||||||||||||||||||||||||||
| 1588 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1589 | |||||||||||||||||||||||||||||||||||||||||||
| 1590 | sub _external_url { | ||||||||||||||||||||||||||||||||||||||||||
| 1591 | 37 | ▶ | if ( plugin_setting()->{use_old_url_scheme} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_external_url: called CC=2 SCAR=8.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1592 | 0 | return _construct_url(@_); | |||||||||||||||||||||||||||||||||||||||||
| 1593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1594 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1595 | 37 | return uri_for(_construct_url(@_)); | |||||||||||||||||||||||||||||||||||||||||
| 1596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1597 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1598 | |||||||||||||||||||||||||||||||||||||||||||
| 1599 | # Given a mode ("view" or "edit", a handler coderef, and an args coderef, works | ||||||||||||||||||||||||||||||||||||||||||
| 1600 | # out if we need to wrap the handler coderef via | ||||||||||||||||||||||||||||||||||||||||||
| 1601 | # Dancer::Plugin::Auth::Extensible to ensure authorisation, and if so, does so. | ||||||||||||||||||||||||||||||||||||||||||
| 1602 | sub _ensure_auth { | ||||||||||||||||||||||||||||||||||||||||||
| 1603 | 21 | ▶ | my ($mode, $handler, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ensure_auth: called CC=8 SCAR=25.8
| |||||||||||||||||||||||||||||||||||||||||||
| 1604 | |||||||||||||||||||||||||||||||||||||||||||
| 1605 | 21 | ▶ | my $auth_settings = $args->{auth}{$mode} || $args->{auth} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
$args->{'auth'}{$mode} || $args->{'auth'} || {}
$args->{"auth"}{$mode} or $args->{"auth"}
MC/DC0%0/2
$args->{'auth'}{$mode} || $args->{'auth'} || {}
$args->{"auth"}{$mode}
$args->{"auth"}
Truth table33%1/3
A or B
A= $args->{"auth"}{$mode}B= $args->{"auth"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1606 | |||||||||||||||||||||||||||||||||||||||||||
| 1607 | 21 | ▶ | if (keys %$auth_settings) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1608 | 4 | ▶ | Dancer::ModuleLoader->load('Dancer::Plugin::Auth::Extensible') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1609 | or die "Can't use auth settings without" | ||||||||||||||||||||||||||||||||||||||||||
| 1610 | . " Dancer::Plugin::Auth::Extensible!"; | ||||||||||||||||||||||||||||||||||||||||||
| 1611 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1612 | # I think this can just be 'return;' given the way it is | ||||||||||||||||||||||||||||||||||||||||||
| 1613 | # used currently, but the other branch returns a $handler, | ||||||||||||||||||||||||||||||||||||||||||
| 1614 | # so this is more consistent | ||||||||||||||||||||||||||||||||||||||||||
| 1615 | 17 | return $handler; | |||||||||||||||||||||||||||||||||||||||||
| 1616 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1617 | |||||||||||||||||||||||||||||||||||||||||||
| 1618 | 4 | ▶ | if ($auth_settings->{require_login}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1619 | 4 | return $handler = | |||||||||||||||||||||||||||||||||||||||||
| 1620 | Dancer::Plugin::Auth::Extensible::require_login($handler); | ||||||||||||||||||||||||||||||||||||||||||
| 1621 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1622 | 0 | for my $keyword (qw(require_role require_any_role require_all_roles)) { | |||||||||||||||||||||||||||||||||||||||||
| 1623 | 0 | ▶ | if (my $val = $auth_settings->{$keyword}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1624 | 0 | return $handler = Dancer::Plugin::Auth::Extensible->can($keyword)->( | |||||||||||||||||||||||||||||||||||||||||
| 1625 | $val, $handler | ||||||||||||||||||||||||||||||||||||||||||
| 1626 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1627 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1628 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1629 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1630 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1631 | |||||||||||||||||||||||||||||||||||||||||||
| 1632 | # Given an action (view/edit) and an args coderef, returns whether the user has | ||||||||||||||||||||||||||||||||||||||||||
| 1633 | # permission to perform that action (e.g. if require_login is set, checks the | ||||||||||||||||||||||||||||||||||||||||||
| 1634 | # user is logged in; if require_role is set, checks they have that role, etc) | ||||||||||||||||||||||||||||||||||||||||||
| 1635 | sub _has_permission { | ||||||||||||||||||||||||||||||||||||||||||
| 1636 | 17 | ▶ | my ($mode, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_permission: called CC=8 SCAR=35.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1637 | |||||||||||||||||||||||||||||||||||||||||||
| 1638 | 17 | ▶ | my $auth_settings = $args->{auth}{$mode} || $args->{auth} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
$args->{'auth'}{$mode} || $args->{'auth'} || {}
$args->{"auth"}{$mode} or $args->{"auth"}
MC/DC0%0/2
$args->{'auth'}{$mode} || $args->{'auth'} || {}
$args->{"auth"}{$mode}
$args->{"auth"}
Truth table33%1/3
A or B
A= $args->{"auth"}{$mode}B= $args->{"auth"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1639 | 17 | ▶ | if (keys %$auth_settings) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1640 | 0 | ▶ | Dancer::ModuleLoader->load('Dancer::Plugin::Auth::Extensible') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1641 | or die "Can't use auth settings without" | ||||||||||||||||||||||||||||||||||||||||||
| 1642 | . " Dancer::Plugin::Auth::Extensible!"; | ||||||||||||||||||||||||||||||||||||||||||
| 1643 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1644 | # If no auth settings provided, they can do what they like | ||||||||||||||||||||||||||||||||||||||||||
| 1645 | 17 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1646 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1647 | |||||||||||||||||||||||||||||||||||||||||||
| 1648 | 0 | ▶ | if ($auth_settings->{require_login}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1649 | 0 | ▶ | return Dancer::Plugin::Auth::Extensible::logged_in_user() ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1650 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1651 | |||||||||||||||||||||||||||||||||||||||||||
| 1652 | 0 | ▶ | if (my $need_role = $auth_settings->{require_role}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1653 | 0 | return Dancer::Plugin::Auth::Extensible::user_has_role($need_role); | |||||||||||||||||||||||||||||||||||||||||
| 1654 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1655 | |||||||||||||||||||||||||||||||||||||||||||
| 1656 | # TODO: handle require_any_role / require_all_roles here | ||||||||||||||||||||||||||||||||||||||||||
| 1657 | 0 | warn "TODO: handle require_any_role / requires_all_roles"; | |||||||||||||||||||||||||||||||||||||||||
| 1658 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1659 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1660 | |||||||||||||||||||||||||||||||||||||||||||
| 1661 | sub _defined_or_empty { | ||||||||||||||||||||||||||||||||||||||||||
| 1662 | 14 | ▶ | my $v = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_defined_or_empty: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1663 | 14 | ▶ | return defined($v) ? $v : ""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1664 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1665 | |||||||||||||||||||||||||||||||||||||||||||
| 1666 | # where_filter "if it's a coderef, call it and check it gave us a hashref to | ||||||||||||||||||||||||||||||||||||||||||
| 1667 | # use, otherwise expect it to be a hashref" logic as we need this in several | ||||||||||||||||||||||||||||||||||||||||||
| 1668 | # places. Returns a hashref, ready for us to add other stuff to in most cases | ||||||||||||||||||||||||||||||||||||||||||
| 1669 | # (e.g. usually we'd call this, then add $key_column => ... to it) | ||||||||||||||||||||||||||||||||||||||||||
| 1670 | sub _get_where_filter_from_args { | ||||||||||||||||||||||||||||||||||||||||||
| 1671 | 21 | ▶ | my $args = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_where_filter_from_args: called CC=5 SCAR=29.4
| |||||||||||||||||||||||||||||||||||||||||||
| 1672 | 21 | ▶ | return unless $args->{where_filter}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1673 | |||||||||||||||||||||||||||||||||||||||||||
| 1674 | 0 | ▶ | if (ref $args->{where_filter} eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1675 | 0 | return $args->{where_filter}; | |||||||||||||||||||||||||||||||||||||||||
| 1676 | } elsif (ref $args->{where_filter} eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||
| 1677 | 0 | my $result = $args->{where_filter}->(); | |||||||||||||||||||||||||||||||||||||||||
| 1678 | 0 | ▶ | if (ref $result eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1679 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 1680 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1681 | # TODO: better error reporting, so we know which one caused | ||||||||||||||||||||||||||||||||||||||||||
| 1682 | # the problem | ||||||||||||||||||||||||||||||||||||||||||
| 1683 | 0 | die "where_filter coderef didn't return a hashref!"; | |||||||||||||||||||||||||||||||||||||||||
| 1684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1685 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1686 | 0 | die "Invalid where_filter"; | |||||||||||||||||||||||||||||||||||||||||
| 1687 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1688 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1689 | |||||||||||||||||||||||||||||||||||||||||||
| 1690 | sub _prettify_column_name { | ||||||||||||||||||||||||||||||||||||||||||
| 1691 | 14 | ▶ | my $name = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prettify_column_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1692 | 14 | for ($name) { | |||||||||||||||||||||||||||||||||||||||||
| 1693 | 14 | $_ = lc; | |||||||||||||||||||||||||||||||||||||||||
| 1694 | 14 | s{_}{ }g; | |||||||||||||||||||||||||||||||||||||||||
| 1695 | 14 | s{\b(\w)}{\u$1}g; | |||||||||||||||||||||||||||||||||||||||||
| 1696 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1697 | 14 | return $name; | |||||||||||||||||||||||||||||||||||||||||
| 1698 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1699 | |||||||||||||||||||||||||||||||||||||||||||
| 1700 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1701 | |||||||||||||||||||||||||||||||||||||||||||
| 1702 | =head1 DWIMmery | ||||||||||||||||||||||||||||||||||||||||||
| 1703 | |||||||||||||||||||||||||||||||||||||||||||
| 1704 | This module tries to do what you'd expect it to do, so you can rock up your web | ||||||||||||||||||||||||||||||||||||||||||
| 1705 | app with as little code and effort as possible, whilst still giving you control | ||||||||||||||||||||||||||||||||||||||||||
| 1706 | to override its decisions wherever you need to. | ||||||||||||||||||||||||||||||||||||||||||
| 1707 | |||||||||||||||||||||||||||||||||||||||||||
| 1708 | =head2 Field types | ||||||||||||||||||||||||||||||||||||||||||
| 1709 | |||||||||||||||||||||||||||||||||||||||||||
| 1710 | CGI::FormBuilder is excellent at working out what kind of field to use by | ||||||||||||||||||||||||||||||||||||||||||
| 1711 | itself, but we give it a little help where needed. For instance, if a field | ||||||||||||||||||||||||||||||||||||||||||
| 1712 | looks like it's supposed to contain a password, we'll have it rendered as a | ||||||||||||||||||||||||||||||||||||||||||
| 1713 | password entry box, rather than a standard text box. | ||||||||||||||||||||||||||||||||||||||||||
| 1714 | |||||||||||||||||||||||||||||||||||||||||||
| 1715 | If the column in the database is an ENUM, we'll limit the choices available for | ||||||||||||||||||||||||||||||||||||||||||
| 1716 | this field to the choices defined by the ENUM list. (Unless you've provided a | ||||||||||||||||||||||||||||||||||||||||||
| 1717 | set of acceptable values for this field using the C<acceptable_values> option to | ||||||||||||||||||||||||||||||||||||||||||
| 1718 | C<simple_crud>, in which case what you say goes.) | ||||||||||||||||||||||||||||||||||||||||||
| 1719 | |||||||||||||||||||||||||||||||||||||||||||
| 1720 | =head1 Hooks | ||||||||||||||||||||||||||||||||||||||||||
| 1721 | |||||||||||||||||||||||||||||||||||||||||||
| 1722 | Hooks are provided, which can be used in the normal Dancer way, using the | ||||||||||||||||||||||||||||||||||||||||||
| 1723 | C<hook> keyword. | ||||||||||||||||||||||||||||||||||||||||||
| 1724 | |||||||||||||||||||||||||||||||||||||||||||
| 1725 | =head2 add_edit_row (deprecated, use add_edit_row_pre_save) | ||||||||||||||||||||||||||||||||||||||||||
| 1726 | |||||||||||||||||||||||||||||||||||||||||||
| 1727 | You can use the same code from your add_edit_row hook in an add_edit_row_pre_save | ||||||||||||||||||||||||||||||||||||||||||
| 1728 | hook. The only modification is that the new hook passes the editable params | ||||||||||||||||||||||||||||||||||||||||||
| 1729 | as a key of the first argument (called C<params>), rather than as the first | ||||||||||||||||||||||||||||||||||||||||||
| 1730 | argument itself. So, if your hook had C<my $args = shift;>, it could just | ||||||||||||||||||||||||||||||||||||||||||
| 1731 | use C<< my $args = shift->{params}; >> and it should work the same way. | ||||||||||||||||||||||||||||||||||||||||||
| 1732 | |||||||||||||||||||||||||||||||||||||||||||
| 1733 | =head2 add_edit_row_pre_save, add_edit_row_post_save | ||||||||||||||||||||||||||||||||||||||||||
| 1734 | |||||||||||||||||||||||||||||||||||||||||||
| 1735 | These fire right before and after a row is added/edited; a hashref is | ||||||||||||||||||||||||||||||||||||||||||
| 1736 | passed with metadata such as the name of the table (in C<table_name>), the | ||||||||||||||||||||||||||||||||||||||||||
| 1737 | args from the original route setup (C<args>), the table's key column | ||||||||||||||||||||||||||||||||||||||||||
| 1738 | (C<key_column>), and the values of the editable params (C<params>). | ||||||||||||||||||||||||||||||||||||||||||
| 1739 | |||||||||||||||||||||||||||||||||||||||||||
| 1740 | In the post-save hook, you are also sent C<success> (the return value of | ||||||||||||||||||||||||||||||||||||||||||
| 1741 | quick_insert or quick_update) telling you if the save was successful | ||||||||||||||||||||||||||||||||||||||||||
| 1742 | (which is a little redundant because your post-save hook won't be called unless | ||||||||||||||||||||||||||||||||||||||||||
| 1743 | the insert or update was successful). You'll also get | ||||||||||||||||||||||||||||||||||||||||||
| 1744 | C<dbh> giving you the instance of the handle used to save the entity | ||||||||||||||||||||||||||||||||||||||||||
| 1745 | (so you can access last_insert_id()), and C<verb> (currently either | ||||||||||||||||||||||||||||||||||||||||||
| 1746 | 'create new' or 'update'). | ||||||||||||||||||||||||||||||||||||||||||
| 1747 | |||||||||||||||||||||||||||||||||||||||||||
| 1748 | For instance, if you were dealing with a users table, you could use the | ||||||||||||||||||||||||||||||||||||||||||
| 1749 | pre_save hook to hash the password before storing it - assuming for the sake | ||||||||||||||||||||||||||||||||||||||||||
| 1750 | of example that you have a C<hash_pw()> function to return a hashed password: | ||||||||||||||||||||||||||||||||||||||||||
| 1751 | |||||||||||||||||||||||||||||||||||||||||||
| 1752 | hook add_edit_row_pre_save => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 1753 | my $args = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 1754 | if ($args->{table_name} eq 'user') { | ||||||||||||||||||||||||||||||||||||||||||
| 1755 | $args->{params}{password} = hash_pw($args->{params}{password}); | ||||||||||||||||||||||||||||||||||||||||||
| 1756 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1757 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1758 | |||||||||||||||||||||||||||||||||||||||||||
| 1759 | =head2 delete_row_pre_delete, delete_row_post_delete | ||||||||||||||||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||||||||||||||||
| 1761 | These fire right before and after a row is deleted. As with the | ||||||||||||||||||||||||||||||||||||||||||
| 1762 | add_edit_row_pre_save and add_edit_row_post_save hooks, these are | ||||||||||||||||||||||||||||||||||||||||||
| 1763 | passed a hashref with metadata such as the name of the table | ||||||||||||||||||||||||||||||||||||||||||
| 1764 | (in C<table_name>), the args from the original route setup (C<args>), | ||||||||||||||||||||||||||||||||||||||||||
| 1765 | the table's key column (C<key_column>), and the values of the | ||||||||||||||||||||||||||||||||||||||||||
| 1766 | editable params (C<params>). As with the post-save hook, delete_row_post_delete hook won't be | ||||||||||||||||||||||||||||||||||||||||||
| 1767 | called if we weren't able to delete the row. | ||||||||||||||||||||||||||||||||||||||||||
| 1768 | |||||||||||||||||||||||||||||||||||||||||||
| 1769 | You could use these to clean up ancillary data associated with a | ||||||||||||||||||||||||||||||||||||||||||
| 1770 | database row when it was deleted, for example. | ||||||||||||||||||||||||||||||||||||||||||
| 1771 | |||||||||||||||||||||||||||||||||||||||||||
| 1772 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 1773 | |||||||||||||||||||||||||||||||||||||||||||
| 1774 | David Precious, C<< <davidp@preshweb.co.uk> >> | ||||||||||||||||||||||||||||||||||||||||||
| 1775 | |||||||||||||||||||||||||||||||||||||||||||
| 1776 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 1777 | |||||||||||||||||||||||||||||||||||||||||||
| 1778 | Alberto Simões (ambs) | ||||||||||||||||||||||||||||||||||||||||||
| 1779 | |||||||||||||||||||||||||||||||||||||||||||
| 1780 | WK | ||||||||||||||||||||||||||||||||||||||||||
| 1781 | |||||||||||||||||||||||||||||||||||||||||||
| 1782 | Johnathan Barber | ||||||||||||||||||||||||||||||||||||||||||
| 1783 | |||||||||||||||||||||||||||||||||||||||||||
| 1784 | saberworks | ||||||||||||||||||||||||||||||||||||||||||
| 1785 | |||||||||||||||||||||||||||||||||||||||||||
| 1786 | jasonjayr | ||||||||||||||||||||||||||||||||||||||||||
| 1787 | |||||||||||||||||||||||||||||||||||||||||||
| 1788 | Paul Johnson (pjcj) | ||||||||||||||||||||||||||||||||||||||||||
| 1789 | |||||||||||||||||||||||||||||||||||||||||||
| 1790 | Rahul Kotamaraju | ||||||||||||||||||||||||||||||||||||||||||
| 1791 | |||||||||||||||||||||||||||||||||||||||||||
| 1792 | Michael J South (msouth) | ||||||||||||||||||||||||||||||||||||||||||
| 1793 | |||||||||||||||||||||||||||||||||||||||||||
| 1794 | Martijn Lievaart | ||||||||||||||||||||||||||||||||||||||||||
| 1795 | |||||||||||||||||||||||||||||||||||||||||||
| 1796 | Josh Rabinowitz | ||||||||||||||||||||||||||||||||||||||||||
| 1797 | |||||||||||||||||||||||||||||||||||||||||||
| 1798 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 1799 | |||||||||||||||||||||||||||||||||||||||||||
| 1800 | Please report any bugs or feature requests to C<bug-dancer-plugin-simplecrud at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 1801 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-SimpleCRUD>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 1802 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 1803 | |||||||||||||||||||||||||||||||||||||||||||
| 1804 | |||||||||||||||||||||||||||||||||||||||||||
| 1805 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 1806 | |||||||||||||||||||||||||||||||||||||||||||
| 1807 | This module is developed on Github: | ||||||||||||||||||||||||||||||||||||||||||
| 1808 | |||||||||||||||||||||||||||||||||||||||||||
| 1809 | http://github.com/bigpresh/Dancer-Plugin-SimpleCRUD | ||||||||||||||||||||||||||||||||||||||||||
| 1810 | |||||||||||||||||||||||||||||||||||||||||||
| 1811 | Bug reports, ideas, suggestions, patches/pull requests all welcome. | ||||||||||||||||||||||||||||||||||||||||||
| 1812 | |||||||||||||||||||||||||||||||||||||||||||
| 1813 | Even just a quick "Hey, this is great, thanks" or "This is no good to me | ||||||||||||||||||||||||||||||||||||||||||
| 1814 | because..." is greatly appreciated. It's always good to know if people are | ||||||||||||||||||||||||||||||||||||||||||
| 1815 | using your code, and what they think. | ||||||||||||||||||||||||||||||||||||||||||
| 1816 | |||||||||||||||||||||||||||||||||||||||||||
| 1817 | |||||||||||||||||||||||||||||||||||||||||||
| 1818 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 1819 | |||||||||||||||||||||||||||||||||||||||||||
| 1820 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 1821 | |||||||||||||||||||||||||||||||||||||||||||
| 1822 | perldoc Dancer::Plugin::SimpleCRUD | ||||||||||||||||||||||||||||||||||||||||||
| 1823 | |||||||||||||||||||||||||||||||||||||||||||
| 1824 | You may find help with this module on the main Dancer IRC channel or mailing | ||||||||||||||||||||||||||||||||||||||||||
| 1825 | list - see http://www.perldancer.org/ | ||||||||||||||||||||||||||||||||||||||||||
| 1826 | |||||||||||||||||||||||||||||||||||||||||||
| 1827 | |||||||||||||||||||||||||||||||||||||||||||
| 1828 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 1829 | |||||||||||||||||||||||||||||||||||||||||||
| 1830 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1831 | |||||||||||||||||||||||||||||||||||||||||||
| 1832 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 1833 | |||||||||||||||||||||||||||||||||||||||||||
| 1834 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-SimpleCRUD> | ||||||||||||||||||||||||||||||||||||||||||
| 1835 | |||||||||||||||||||||||||||||||||||||||||||
| 1836 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 1837 | |||||||||||||||||||||||||||||||||||||||||||
| 1838 | L<http://annocpan.org/dist/Dancer-Plugin-SimpleCRUD> | ||||||||||||||||||||||||||||||||||||||||||
| 1839 | |||||||||||||||||||||||||||||||||||||||||||
| 1840 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 1841 | |||||||||||||||||||||||||||||||||||||||||||
| 1842 | L<http://cpanratings.perl.org/d/Dancer-Plugin-SimpleCRUD> | ||||||||||||||||||||||||||||||||||||||||||
| 1843 | |||||||||||||||||||||||||||||||||||||||||||
| 1844 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 1845 | |||||||||||||||||||||||||||||||||||||||||||
| 1846 | L<http://search.cpan.org/dist/Dancer-Plugin-SimpleCRUD/> | ||||||||||||||||||||||||||||||||||||||||||
| 1847 | |||||||||||||||||||||||||||||||||||||||||||
| 1848 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1849 | |||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||
| 1851 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 1852 | |||||||||||||||||||||||||||||||||||||||||||
| 1853 | Copyright 2010-16 David Precious. | ||||||||||||||||||||||||||||||||||||||||||
| 1854 | |||||||||||||||||||||||||||||||||||||||||||
| 1855 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 1856 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 1857 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 1858 | |||||||||||||||||||||||||||||||||||||||||||
| 1859 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 1860 | |||||||||||||||||||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||||||||||||||||
| 1862 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1863 | |||||||||||||||||||||||||||||||||||||||||||
| 1864 | 3 | 1; # End of Dancer::Plugin::SimpleCRUD | |||||||||||||||||||||||||||||||||||||||||