Changeset 5
- Timestamp:
- 05/16/05 15:44:37 (3 years ago)
- Files:
-
- trunk/lib/Scrpbk.pm (modified) (8 diffs)
- trunk/lib/Scrpbk/C/Holder.pm (modified) (5 diffs)
- trunk/lib/Scrpbk/C/Person.pm (modified) (13 diffs)
- trunk/lib/Scrpbk/C/Scrap.pm (modified) (5 diffs)
- trunk/lib/Scrpbk/C/SearchEngine.pm (modified) (3 diffs)
- trunk/lib/Scrpbk/M/DBI.pm (modified) (2 diffs)
- trunk/lib/Scrpbk/M/Holder.pm (modified) (6 diffs)
- trunk/lib/Scrpbk/M/Person.pm (modified) (1 diff)
- trunk/lib/Scrpbk/M/Session.pm (modified) (2 diffs)
- trunk/lib/Scrpbk/V/Email.pm (modified) (3 diffs)
- trunk/lib/Scrpbk/V/TT.pm (modified) (1 diff)
- trunk/script/scrpbk_server.pl (modified) (1 diff)
- trunk/script/scrpbk_test.pl (modified) (1 diff)
- trunk/t/c/holder.t (modified) (9 diffs)
- trunk/t/c/scrap.t (modified) (5 diffs)
- trunk/t/helpers.pl (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/Scrpbk.pm
r4 r5 13 13 # Get the config. 14 14 my $yaml_file = -e 'setup.yaml' ? 'setup.yaml' : '../setup.yaml'; 15 my $yaml_config = YAML::LoadFile( $yaml_file)16 || die "Could not load the setup file";15 my $yaml_config = YAML::LoadFile($yaml_file) 16 || die "Could not load the setup file"; 17 17 Scrpbk->config( name => 'Scrpbk', %$yaml_config ); 18 18 Scrpbk->setup; … … 26 26 # Load user from cookie 27 27 $c->forward('load_session_from_cookie') 28 if $c->req->cookies->{session};28 if $c->req->cookies->{session}; 29 29 30 30 } … … 35 35 36 36 # Get user 'homepage' onto stash as 'target'. 37 $c->stash->{target} = Scrpbk::M::Person->retrieve 38 ( tag => 'homepage' ) 39 || warn "Could not load user 'homepage'"; 37 $c->stash->{target} = Scrpbk::M::Person->retrieve( tag => 'homepage' ) 38 || warn "Could not load user 'homepage'"; 40 39 } 41 40 … … 45 44 # If there is a redirect going change the template. 46 45 if ( $c->res->redirect ) { 47 $c->stash->{template} = 'redirect';46 $c->stash->{template} = 'redirect'; 48 47 } 49 48 … … 54 53 my ( $self, $c ) = @_; 55 54 my $token = $c->req->cookies->{session}->value 56 || return 0;55 || return 0; 57 56 58 my $session = Scrpbk::M::Session->retrieve( $token)59 || return 0;57 my $session = Scrpbk::M::Session->retrieve($token) 58 || return 0; 60 59 61 60 # Update the last visit tag. … … 68 67 return 1; 69 68 } 70 71 69 72 70 ################################################################################ … … 81 79 my $me = URI->new( 'http://' . $c->config->{http_host} ); 82 80 $me->path( $c->req->path ); 83 $me->query_form( %{ $c->req->parameters} );81 $me->query_form( %{ $c->req->parameters } ); 84 82 85 83 # Create the login URI. 86 84 my $login = URI->new( 'http://' . $c->config->{http_host} ); 87 $login->path( '/person/login');85 $login->path('/person/login'); 88 86 $login->query_form( redirect_to => $me->as_string ); 89 87 … … 122 120 123 121 # If there is no referer send to users homepage or site homepage 124 unless ( $referer) {125 my $person = $c->stash->{person};126 $referer ||= $person ? 127 '/~' . $person->tag : 128 '/';122 unless ($referer) { 123 my $person = $c->stash->{person}; 124 $referer ||= $person 125 ? '/~' . $person->tag 126 : '/'; 129 127 } 130 128 131 $c->res->redirect( $referer);129 $c->res->redirect($referer); 132 130 return 0; 133 131 } trunk/lib/Scrpbk/C/Holder.pm
r2 r5 10 10 # Redirect to login unless we have a user. 11 11 unless ( $c->stash->{person} ) { 12 $c->forward('/redirect_to_login');13 return 0;12 $c->forward('/redirect_to_login'); 13 return 0; 14 14 } 15 15 } … … 33 33 my $person = $c->stash->{person} || return 0; 34 34 35 my $holder = Scrpbk::M::Holder->retrieve 36 ( id => $holder_id, 37 person => $person ); 35 my $holder = Scrpbk::M::Holder->retrieve( 36 id => $holder_id, 37 person => $person 38 ); 38 39 39 unless ( $holder) {40 $c->stash->{fatal_error} = "Could not retrieve holder '$holder_id'.";41 return $c->forward('/fatal_error');40 unless ($holder) { 41 $c->stash->{fatal_error} = "Could not retrieve holder '$holder_id'."; 42 return $c->forward('/fatal_error'); 42 43 } 43 44 … … 59 60 $direction =~ s/[^a-z]//g; 60 61 61 my %directions = map { $_=>1} qw(up down left right);62 my %directions = map { $_ => 1 } qw(up down left right); 62 63 63 64 # Check that the direction is valid. 64 unless ( $directions{ $direction} ) {65 $c->stash->{fatal_error} = "Cannot move in direction '$direction'.";66 return $c->forward('/fatal_error');65 unless ( $directions{$direction} ) { 66 $c->stash->{fatal_error} = "Cannot move in direction '$direction'."; 67 return $c->forward('/fatal_error'); 67 68 } 68 69 69 my $holder = Scrpbk::M::Holder->retrieve 70 ( id => $holder_id, 71 person => $person ); 70 my $holder = Scrpbk::M::Holder->retrieve( 71 id => $holder_id, 72 person => $person 73 ); 72 74 73 unless ( $holder) {74 $c->stash->{fatal_error} = "Could not load the holder '$holder_id'.";75 return $c->forward('/fatal_error');75 unless ($holder) { 76 $c->stash->{fatal_error} = "Could not load the holder '$holder_id'."; 77 return $c->forward('/fatal_error'); 76 78 } 77 78 $holder->relocate( $direction);79 80 $holder->relocate($direction); 79 81 80 82 return $c->forward('/redirect_to_referer'); … … 91 93 92 94 my $holder = undef; 93 95 94 96 if ( $holder_id eq 'new' ) { 95 # Don't do anything.96 $holder = undef;97 97 98 } elsif ( $holder_id =~ m/^\d+$/ ) { 99 # Try to load the holder. 100 $holder = Scrpbk::M::Holder->retrieve 101 ( id => $holder_id, 102 person => $person ); 98 # Don't do anything. 99 $holder = undef; 103 100 104 unless ( $holder ) { 105 # Error loading the holder. 106 $c->stash->{fatal_error} = "Holder '$holder_id' not found."; 107 return $c->forward('/fatal_error'); 108 } 101 } 102 elsif ( $holder_id =~ m/^\d+$/ ) { 109 103 110 } else { 111 # Should not be here. 112 $c->stash->{fatal_error} = "Can't get the holder '$holder_id'."; 113 return $c->forward('/fatal_error'); 104 # Try to load the holder. 105 $holder = Scrpbk::M::Holder->retrieve( 106 id => $holder_id, 107 person => $person 108 ); 109 110 unless ($holder) { 111 112 # Error loading the holder. 113 $c->stash->{fatal_error} = "Holder '$holder_id' not found."; 114 return $c->forward('/fatal_error'); 115 } 116 117 } 118 else { 119 120 # Should not be here. 121 $c->stash->{fatal_error} = "Can't get the holder '$holder_id'."; 122 return $c->forward('/fatal_error'); 114 123 } 115 124 116 125 # Give the holder to the stash. 117 126 $c->stash->{holder} = $holder; 118 127 119 128 # Is there a form to do. 120 129 return unless $c->req->param('form_submitted'); … … 127 136 my @messages = (); 128 137 push @messages, "The name is too long, must be no more than than 40 chars" 129 if length( $name) > 40;138 if length($name) > 40; 130 139 push @messages, "The name is too short, must be at least 1 char" 131 if length( $name) < 1;140 if length($name) < 1; 132 141 133 142 # Return if errors found. 134 143 if ( scalar @messages ) { 135 $c->stash->{messages} = \@messages;136 $c->stash->{new} = {name => $name};137 return 1;144 $c->stash->{messages} = \@messages; 145 $c->stash->{new} = { name => $name }; 146 return 1; 138 147 } 139 148 140 149 # Update or create the holder. 141 if ( $holder ) { 142 $holder->name( $name ); 143 $holder->update; 144 } else { 145 $holder = Scrpbk::M::Holder->create 146 ({ person => $person, name => $name }); 150 if ($holder) { 151 $holder->name($name); 152 $holder->update; 153 } 154 else { 155 $holder = 156 Scrpbk::M::Holder->create( { person => $person, name => $name } ); 147 157 } 148 158 $holder->rerank_column; trunk/lib/Scrpbk/C/Person.pm
r2 r5 37 37 38 38 # Get the tag and password. 39 my $tag = lc( $c->req->param('tag'))|| '';40 my $password = $c->req->param('password') || '';39 my $tag = lc( $c->req->param('tag') ) || ''; 40 my $password = $c->req->param('password') || ''; 41 41 42 42 # Try to load the person. 43 my $person = Scrpbk::M::Person->retrieve44 ( tag => $tag, password => $password );45 43 my $person = 44 Scrpbk::M::Person->retrieve( tag => $tag, password => $password ); 45 46 46 # We have a person - do what is needed and return. 47 if ( $person ) { 48 $c->stash->{person} = $person; 49 $c->forward( 'create_session' ); 50 $c->res->redirect( $redirect_to || 51 $c->req->referer() || 52 '/~' . $person->tag 53 ); 54 return 1; 55 } 56 57 $c->stash->{messages} = 58 ["Could not log you in. Please check your details are correct."]; 47 if ($person) { 48 $c->stash->{person} = $person; 49 $c->forward('create_session'); 50 $c->res->redirect( $redirect_to 51 || $c->req->referer() 52 || '/~' . $person->tag ); 53 return 1; 54 } 55 56 $c->stash->{messages} = 57 ["Could not log you in. Please check your details are correct."]; 59 58 60 59 return 1; … … 76 75 # Unless a form has been submitted return. 77 76 return 1 unless $c->req->param('form_submitted'); 78 77 79 78 # Get the values. 80 79 my $tag = $c->req->param('tag') || ''; … … 82 81 my $password = $c->req->param('password') || ''; 83 82 my $password2 = $c->req->param('password2') || ''; 84 83 85 84 # Do some simple tidying up on the tag. 86 85 $tag =~ lc $tag; … … 88 87 89 88 # If both passwords are empty generate a random one to use. 90 unless ( length( $password ) || length( $password )) {91 my @chars = ( 'a'..'z', 'A'..'Z' );92 my $tmp = '';93 $tmp .= $chars[ rand scalar @chars ] for 1..9;94 $password = $password2 = $tmp;95 } 96 89 unless ( length($password) || length($password) ) { 90 my @chars = ( 'a' .. 'z', 'A' .. 'Z' ); 91 my $tmp = ''; 92 $tmp .= $chars[ rand scalar @chars ] for 1 .. 9; 93 $password = $password2 = $tmp; 94 } 95 97 96 # Errors will go on here. 98 97 my @messages = (); … … 100 99 # Check that the tag is long enough. 101 100 unless ( length($tag) >= 4 && length($tag) <= 20 ) { 102 push @messages, "The tag must be 4 to 20 characters long.";101 push @messages, "The tag must be 4 to 20 characters long."; 103 102 } 104 103 105 104 # Check that the tag is correct. 106 105 if ( $tag =~ m/[^a-z]/ ) { 107 push @messages, "The tag must be compased of letters 'a' to 'z' only.";106 push @messages, "The tag must be compased of letters 'a' to 'z' only."; 108 107 } 109 108 110 109 # Check that the tag is not already taken. 111 110 if ( Scrpbk::M::Person->retrieve( tag => $tag ) ) { 112 push @messages, "The tag '$tag' is already taken - please try another.";111 push @messages, "The tag '$tag' is already taken - please try another."; 113 112 } 114 113 115 114 # Check the email address is valid. 116 my $valid_email = Email::Valid->address( $email);117 unless ( $valid_email) {118 push @messages, "The email address was not valid, please check it.";119 } 120 115 my $valid_email = Email::Valid->address($email); 116 unless ($valid_email) { 117 push @messages, "The email address was not valid, please check it."; 118 } 119 121 120 # Check that the email address is not already used. 122 121 if ( Scrpbk::M::Person->retrieve( email => $valid_email ) ) { 123 push @messages, "The email address is already being used.";122 push @messages, "The email address is already being used."; 124 123 } 125 124 126 125 # Check that the email address is not too long. 127 if ( length( $valid_email ) >= 120 ) { 128 push @messages, "The email address cannot be longer than 120 characters."; 129 } 126 if ( length($valid_email) >= 120 ) { 127 push @messages, 128 "The email address cannot be longer than 120 characters."; 129 } 130 130 131 131 # Check that the passwords are long enough. 132 132 unless ( length($password) >= 6 && length($password) <= 20 ) { 133 push @messages, "The password must be 6 to 20 characters long.";133 push @messages, "The password must be 6 to 20 characters long."; 134 134 } 135 135 136 136 # Check the passwords match. 137 137 unless ( $password eq $password2 ) { 138 push @messages, "The passswords do not match.";138 push @messages, "The passswords do not match."; 139 139 } 140 140 141 141 # If there are any messages then there is a problem. 142 142 if ( scalar @messages ) { 143 $c->stash->{messages} = \@messages; 144 $c->stash->{details} = { tag => $tag, 145 email => $email, 146 password => $password, 147 password2 => $password2 }; 148 return 1; 143 $c->stash->{messages} = \@messages; 144 $c->stash->{details} = { 145 tag => $tag, 146 email => $email, 147 password => $password, 148 password2 => $password2 149 }; 150 return 1; 149 151 } 150 152 151 153 # Create the account. 152 my $person = Scrpbk::M::Person->create 153 ({ tag => $tag, 154 email => $valid_email, 155 password => $password }); 154 my $person = Scrpbk::M::Person->create( 155 { 156 tag => $tag, 157 email => $valid_email, 158 password => $password 159 } 160 ); 156 161 157 162 # Did we create a user? 158 unless ( $person) {159 $c->stash->{fatal_error} = "Could not create the user.";160 return $c->forward('/fatal_error');161 } 162 163 unless ($person) { 164 $c->stash->{fatal_error} = "Could not create the user."; 165 return $c->forward('/fatal_error'); 166 } 167 163 168 # Put the person on the template. 164 169 $c->stash->{person} = $person; … … 166 171 # send an email with account details. 167 172 $c->stash->{email_template} = 'emails/new_account'; 168 $c->stash->{email_to} = $person->email;169 $c->stash->{email_bcc} = 'scrpbk@ecclestoad.co.uk';173 $c->stash->{email_to} = $person->email; 174 $c->stash->{email_bcc} = 'scrpbk@ecclestoad.co.uk'; 170 175 $c->forward("Scrpbk::V::Email"); 171 176 172 177 # Copy template users stuff to here. 173 178 my $template = Scrpbk::M::Person->retrieve( tag => 'template' ); 174 if ( $template ) { 175 176 my %holder_id_lkup = (); 177 178 foreach my $holder ( $template->holders ) { 179 # Create the holder and add to the lookup table. 180 my $new_holder = $holder->copy({ person => $person }); 181 $holder_id_lkup{ $holder->id } = $new_holder; 182 } 183 184 foreach my $scrap ( $template->scraps ) { 185 # Copy the scrap. 186 my %args = (); 187 $args{holder} = $holder_id_lkup{ $scrap->holder->id } 188 if $scrap->holder; 189 190 my $new_scrap = $scrap->copy 191 ({ 192 person => $person, 193 %args 194 }); 195 } 196 197 198 } else { 199 $c->log->info("You should create the template user to copy from."); 179 if ($template) { 180 181 my %holder_id_lkup = (); 182 183 foreach my $holder ( $template->holders ) { 184 185 # Create the holder and add to the lookup table. 186 my $new_holder = $holder->copy( { person => $person } ); 187 $holder_id_lkup{ $holder->id } = $new_holder; 188 } 189 190 foreach my $scrap ( $template->scraps ) { 191 192 # Copy the scrap. 193 my %args = (); 194 $args{holder} = $holder_id_lkup{ $scrap->holder->id } 195 if $scrap->holder; 196 197 my $new_scrap = $scrap->copy( 198 { 199 person => $person, 200 %args 201 } 202 ); 203 } 204 205 } 206 else { 207 $c->log->info("You should create the template user to copy from."); 200 208 } 201 209 … … 211 219 my $person = $c->stash->{person}; 212 220 213 unless ( $person) {214 $c->stash->{fatal_error} = "Need a person to create a session.";215 return $c->forward('/fatal_error');221 unless ($person) { 222 $c->stash->{fatal_error} = "Need a person to create a session."; 223 return $c->forward('/fatal_error'); 216 224 } 217 225 218 226 # create the session. 219 my $session = Scrpbk::M::Session->create( { person => $person });220 221 unless ( $session) {222 $c->stash->{fatal_error} = "Could not create a session";223 return $c->forward('/fatal_error');227 my $session = Scrpbk::M::Session->create( { person => $person } ); 228 229 unless ($session) { 230 $c->stash->{fatal_error} = "Could not create a session"; 231 return $c->forward('/fatal_error'); 224 232 } 225 233 226 234 # Put the session token onto a cookie. 227 235 $c->stash->{session} = $session; 228 $c->res->cookies->{session} = { value => $session->token, 229 expires => '+10y' }; 236 $c->res->cookies->{session} = { 237 value => $session->token, 238 expires => '+10y' 239 }; 230 240 return 1; 231 241 } … … 236 246 my $session = $c->stash->{session}; 237 247 238 unless ( $session) {239 $c->stash->{fatal_error} = "Could not find a session to delete.";240 return $c->forward('/fatal_error');248 unless ($session) { 249 $c->stash->{fatal_error} = "Could not find a session to delete."; 250 return $c->forward('/fatal_error'); 241 251 } 242 252 … … 253 263 sub forgottenpassword : Local { 254 264 my ( $self, $c ) = @_; 255 265 256 266 # If there is no form there is nothing to do. 257 267 return unless $c->req->param('form_submitted'); … … 265 275 266 276 # Check that they are good. 267 if ( $email ) { 268 $person = Scrpbk::M::Person->retrieve( email => $email ); 269 push @messages, "Email not found - please check it." 270 unless $person; 271 272 } elsif ( $tag ) { 273 $person = Scrpbk::M::Person->retrieve( tag => $tag ); 274 push @messages, "Tag not found - please check it." 275 unless $person; 276 277 } else { 278 push @messages, "Please provide either an email or a tag."; 277 if ($email) { 278 $person = Scrpbk::M::Person->retrieve( email => $email ); 279 push @messages, "Email not found - please check it." 280 unless $person; 281 282 } 283 elsif ($tag) { 284 $person = Scrpbk::M::Person->retrieve( tag => $tag ); 285 push @messages, "Tag not found - please check it." 286 unless $person; 287 288 } 289 else { 290 push @messages, "Please provide either an email or a tag."; 279 291 } 280 292 281 293 # If there was a problem then return. 282 294 if ( scalar @messages ) { 283 $c->stash->{messages} = \@messages;284 $c->stash->{details} = { email => $email, tag => $tag }; 285 return 0;295 $c->stash->{messages} = \@messages; 296 $c->stash->{details} = { email => $email, tag => $tag }; 297 return 0; 286 298 } 287 299 288 300 # We have a person - send a reminder. 289 $c->stash->{remind_person} = $person;301 $c->stash->{remind_person} = $person; 290 302 $c->stash->{email_template} = 'emails/password_reminder'; 291 $c->stash->{email_to} = $person->email;292 $c->stash->{email_bcc} = 'scrpbk@ecclestoad.co.uk';303 $c->stash->{email_to} = $person->email; 304 $c->stash->{email_bcc} = 'scrpbk@ecclestoad.co.uk'; 293 305 $c->forward("Scrpbk::V::Email"); 294 306 … … 305 317 # Need someone logged in - otherwise redirect. 306 318 return $c->forward('/redirect_to_login') 307 unless $person;319 unless $person; 308 320 309 321 # Return if there is no form_submitted. … … 320 332 321 333 push @messages, "The old password was not correct." 322 unless $old_password eq $person->password;334 unless $old_password eq $person->password; 323 335 324 336 push @messages, "The new passwords are different." 325 unless $new_password eq $new_password2;337 unless $new_password eq $new_password2; 326 338 327 339 push @messages, "The new password must be between 6 and 20 characters" 328 unless length( $new_password ) <= 20 && 329 length( $new_password) >= 6;340 unless length($new_password) <= 20 341 && length($new_password) >= 6; 330 342 331 343 return 1 if scalar @messages; 332 344 333 345 # Try to change the pasword. 334 $person->password( $new_password);346 $person->password($new_password); 335 347 $person->update; 336 348 … … 339 351 } 340 352 341 342 353 1; 343 354 trunk/lib/Scrpbk/C/Scrap.pm
r2 r5 8 8 sub auto : Private { 9 9 my ( $self, $c ) = @_; 10 10 11 11 # Redirect to login unless we have a user. 12 12 unless ( $c->stash->{person} ) { 13 $c->forward('/redirect_to_login');14 return 0;13 $c->forward('/redirect_to_login'); 14 return 0; 15 15 } 16 16 } … … 31 31 32 32 my $person = $c->stash->{person} || return 0; 33 34 my $scrap = Scrpbk::M::Scrap->retrieve 35 ( person => $person, 36 id => $scrap_id ); 37 33 34 my $scrap = Scrpbk::M::Scrap->retrieve( 35 person => $person, 36 id => $scrap_id 37 ); 38 38 39 # Did we got a scrap. 39 unless ( $scrap) {40 $c->stash->{fatal_error} = "Scrap '$scrap_id' not found.";41 return $c->forward('/fatal_error');40 unless ($scrap) { 41 $c->stash->{fatal_error} = "Scrap '$scrap_id' not found."; 42 return $c->forward('/fatal_error'); 42 43 } 43 44 … … 71 72 # return if needed. 72 73 return $c->forward('/redirect_to_referer') 73 unless $scrap_id;74 75 unless ( $holder_id) {76 $c->stash->{fatal_error} = "Could not determine which holder to use.";77 return $c->forward('fatal_error');74 unless $scrap_id; 75 76 unless ($holder_id) { 77 $c->stash->{fatal_error} = "Could not determine which holder to use."; 78 return $c->forward('fatal_error'); 78 79 } 79 80 80 81 # Check the rank is usable. 81 82 unless ( $rank =~ m/^\d+$/ ) { 82 $c->stash->{fatal_error} = "Rank '$rank' is not usable.";83 return $c->forward('/fatal_error');83 $c->stash->{fatal_error} = "Rank '$rank' is not usable."; 84 return $c->forward('/fatal_error'); 84 85 } 85 86 86 87 # Get the scrap. 87 my $scrap = Scrpbk::M::Scrap->retrieve 88 (id => $scrap_id,89 person => $person90 );91 88 my $scrap = Scrpbk::M::Scrap->retrieve( 89 id => $scrap_id, 90 person => $person 91 ); 92 92 93 # Did we get a scrap. 93 unless ( $scrap) {94 $c->stash->{fatal_error} = "Scrap '$scrap_id' not found.";95 return $c->forward('/fatal_error');94 unless ($scrap) { 95 $c->stash->{fatal_error} = "Scrap '$scrap_id' not found."; 96 return $c->forward('/fatal_error'); 96 97 } 97 98 98 99 # Get the holder. 99 my $new_holder = Scrpbk::M::Holder->retrieve 100 (id => $holder_id,101 person => $person,102 );100 my $new_holder = Scrpbk::M::Holder->retrieve( 101 id => $holder_id, 102 person => $person, 103 ); 103 104 104 105 # Did we get a holder. 105 unless ( $new_holder) {106 $c->stash->{fatal_error} = "Holder '$holder_id' not found.";107 return $c->forward('/fatal_error');108 } 109 106 unless ($new_holder) { 107 $c->stash->{fatal_error} = "Holder '$holder_id' not found."; 108 return $c->forward('/fatal_error'); 109 } 110 110 111 # Rerank the scraps in this holder. 111 my @scraps = Scrpbk::M::Scrap->search 112 ( holder => $new_holder, 113 {order_by => 'rank, id'} ); 114 112 my @scraps = Scrpbk::M::Scrap->search( 113 holder => $new_holder, 114 { order_by => 'rank, id' } 115 ); 116 115 117 my $tmp_rank = 0; 116 foreach my $tmp_scrap ( @scraps) {117 $tmp_rank += 2;118 $tmp_scrap->rank( $tmp_rank);119 $tmp_scrap->update;118 foreach my $tmp_scrap (@scraps) { 119 $tmp_rank += 2; 120 $tmp_scrap->rank($tmp_rank); 121 $tmp_scrap->update; 120 122 } 121 123 122 124 # Move the scrap to the new holder. 123 $scrap->holder( $new_holder);124 $scrap->rank( ( $rank * 2) + 1 );125 $scrap->holder($new_holder); 126 $scrap->rank( ( $rank * 2 ) + 1 ); 125 127 $scrap->update; 126 128 … … 141 143 142 144 if ( $scrap_id eq 'new' ) { 143 # Don't do anything. 144 $scrap = undef; 145 } elsif ( $scrap_id =~ m/^\d+$/ ) { 146 # Try to load the scrap. 147 $scrap = Scrpbk::M::Scrap->retrieve 148 ( id => $scrap_id, 149 person => $person ); 150 151 unless ( $scrap ) { 152 # Error loading the scrap. 153 $c->stash->{fatal_error} = "Scrap '$scrap_id' not found."; 154 return $c->forward('/fatal_error'); 155 } 156 } else { 157 # Should not be here. 158 $c->stash->{fatal_error} = "Can't get the scrap '$scrap_id'."; 159 return $c->forward('/fatal_error'); 145 146 # Don't do anything. 147 $scrap = undef; 148 } 149 elsif ( $scrap_id =~ m/^\d+$/ ) { 150 151 # Try to load the scrap. 152 $scrap = Scrpbk::M::Scrap->retrieve( 153 id => $scrap_id, 154 person => $person 155 ); 156 157 unless ($scrap) { 158 159 # Error loading the scrap. 160 $c->stash->{fatal_error} = "Scrap '$scrap_id' not found."; 161 return $c->forward('/fatal_error'); 162 } 163 } 164 else { 165 166 # Should not be here. 167 $c->stash->{fatal_error} = "Can't get the scrap '$scrap_id'."; 168 return $c->forward('/fatal_error'); 160 169 } 161 170 162 171 # Put the scrap on the stash. 163 172 $c->stash->{scrap} = $scrap; 164 173 165 174 # Is there a form to do. 166 175 return unless $c->req->param('form_submitted'); … … 178 187 # Validate the inputs. 179 188 push @messages, "The name is too long, must be no more than than 40 chars" 180 if length( $name) > 40;189 if length($name) > 40; 181 190 push @messages, "The name is too short, must be at least 1 char" 182 if length( $name) < 1;191 if length($name) < 1; 183 192 push @messages, "The note is too long, must be no more than than 1000 chars" 184 if length( $note) > 1000;193 if length($note) > 1000; 185 194 push @messages, "The url is too long, must be no more than than 500 chars" 186 if length( $url) > 500;195 if length($url) > 500; 187 196 188 197 # Return if errors found. 189 198 if ( scalar @messages ) { 190 $c->stash->{messages} = \@messages; 191 $c->stash->{new} = { name => $name, 192 note => $note, 193 url => $url }; 194 return 1 ; 199 $c->stash->{messages} = \@messages; 200 $c->stash->{new} = { 201 name => $name, 202 note => $note, 203 url => $url 204 }; 205 return 1; 195 206 } 196 207 197 208 # Update or create the scrap. 198 if ( $scrap ) { 199 $scrap->set( name => $name, note => $note, url => $url ); 200 $scrap->update; 201 } else { 202 $scrap = Scrpbk::M::Scrap->create 203 ({ person => $person, name => $name, note => $note, url => $url }); 209 if ($scrap) { 210 $scrap->set( name => $name, note => $note, url => $url ); 211 $scrap->update; 212 } 213 else { 214 $scrap = 215 Scrpbk::M::Scrap->create( 216 { person => $person, name => $name, note => $note, url => $url } ); 204 217 } 205 218 trunk/lib/Scrpbk/C/SearchEngine.pm
r2 r5 3 3 use strict; 4 4 use base 'Catalyst::Base'; 5 6 5 7 6 sub default : Private { … … 11 10 # Validate the offset or set to zero. 12 11 $offset = 0 unless defined $offset && $offset =~ m/^\d+$/; 13 12 14 13 # Set up the variables to use in the sql. 15 my $number = 400;14 my $number = 400; 16 15 17 16 # Get the persons' tags and place them on the template. 18 my @persons = Scrpbk::M::Person->search_list_tags19 ( $number, $number * $offset );17 my @persons = 18 Scrpbk::M::Person->search_list_tags( $number, $number * $offset ); 20 19 21 my @tags = map { $_-> tag } @persons;20 my @tags = map { $_->tag } @persons; 22 21 23 22 $c->stash->{tags} = \@tags; … … 28 27 $c->stash->{template} = 'searchengine/tag_list'; 29 28 } 30 31 29 32 30 =head1 NAME trunk/lib/Scrpbk/M/DBI.pm
r2 r5 6 6 7 7 my $db_name = $ENV{DB_NAME} || 'scrpbk'; 8 __PACKAGE__->connection( "dbi:Pg:dbname=$db_name", '', '', { AutoCommit => 1 } );9 8 __PACKAGE__->connection( "dbi:Pg:dbname=$db_name", '', '', 9 { AutoCommit => 1 } ); 10 10 11 11 # Global sequence for all id numbers. 12 __PACKAGE__->sequence( 'global_id_sequence');12 __PACKAGE__->sequence('global_id_sequence'); 13 13 14 14 sub create_token { 15 my @A = ( 'a' ..'z', 'A'..'Z', 0..9 );15 my @A = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9 ); 16 16 my $seq = ''; 17 $seq .= $A[ rand scalar @A ] for 1 ..40;17 $seq .= $A[ rand scalar @A ] for 1 .. 40; 18 18 return $seq; 19 19 } … … 21 21 1; 22 22 23 trunk/lib/Scrpbk/M/Holder.pm
r2 r5 12 12 __PACKAGE__->has_many( scraps => 'Scrpbk::M::Scrap' ); 13 13 14 15 __PACKAGE__->add_trigger 16 ( before_create => sub { 14 __PACKAGE__->add_trigger( 15 before_create => sub { 17 16 my $self = shift; 18 17 19 # Set some sensible defaults;20 $self->col( $self->col|| 1 );21 $self->rank( $self->rank || 1 );18 # Set some sensible defaults; 19 $self->col( $self->col || 1 ); 20 $self->rank( $self->rank || 1 ); 22 21 23 22 return 1; 24 23 } 25 ); 26 24 ); 27 25 28 26 sub relocate { 29 27 my $self = shift; 30 my $dir = shift;28 my $dir = shift; 31 29 32 30 if ( $dir =~ m/^(up|down)$/ ) { 33 $self->relocate_vertically( $dir ); 34 } elsif ( $dir =~ m/^(left|right)$/ ) { 35 $self->relocate_horizontally( $dir ); 36 } else { die "Cannot move '$dir'"; } 37 31 $self->relocate_vertically($dir); 32 } 33 elsif ( $dir =~ m/^(left|right)$/ ) { 34 $self->relocate_horizontally($dir); 35 } 36 else { die "Cannot move '$dir'"; } 37 38 38 return 1; 39 39 } … … 41 41 sub relocate_vertically { 42 42 my $self = shift; 43 my $dir = shift;43 my $dir = shift; 44 44 45 45 # Change the position of this holder. … … 53 53 sub relocate_horizontally { 54 54 my $self = shift; 55 my $dir = shift;55 my $dir = shift; 56 56 57 57 # What will the new col be? 58 my $change = $dir eq 'left' ? -1 : 1;58 my $change = $dir eq 'left' ? -1 : 1; 59 59 my $old_col = $self->col; 60 60 my $new_col = $old_col + $change; … … 62 62 # Check that the position can be changed. 63 63 die "Cannot move to col '$new_col'" 64 if $new_col <= 0 || $new_col >= 3;64 if $new_col <= 0 || $new_col >= 3; 65 65 66 66 # Change the position of this holder. 67 $self->col( $new_col);68 $self->rank( 0);67 $self->col($new_col); 68 $self->rank(0); 69 69 $self->update; 70 70 71 71 &nb
