From: Chris W. <la...@us...> - 2005-03-02 15:21:55
|
Update of /cvsroot/openinteract/OpenInteract2/pkg/news/OpenInteract2/Action In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21616/news/OpenInteract2/Action Modified Files: News.pm Log Message: OIN-135: add support to news package for REST URLs, also adding an 'archive' feature to allow you to specify news articles to display by year + month Index: News.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/pkg/news/OpenInteract2/Action/News.pm,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** News.pm 5 Dec 2004 21:15:53 -0000 1.12 --- News.pm 2 Mar 2005 15:21:44 -0000 1.13 *************** *** 27,30 **** --- 27,45 ---- } + # only by date, but makes for nice URLs... + + sub archive { + my ( $self ) = @_; + my $year = $self->param( 'search_year' ); + my $month = $self->param( 'search_month' ); + $log ||= get_logger( LOG_APP ); + $log->info( "Got archive date [Year: $year] and [Month: $month]" ); + unless ( $year and $month ) { + $self->add_error_key( 'news.error.archive_date' ); + return $self->execute({ task => 'home' }); + } + return $self->search(); + } + sub latest { my ( $self ) = @_; *************** *** 41,47 **** my $where = "active_on <= ? AND active = ? "; my @values = ( OpenInteract2::Util->now, 'yes' ); - my $news_class = CTX->lookup_object( 'news' ); my $items = eval { ! $news_class->fetch_group({ where => $where, value => \@values, --- 56,61 ---- my $where = "active_on <= ? AND active = ? "; my @values = ( OpenInteract2::Util->now, 'yes' ); my $items = eval { ! OpenInteract2::News->fetch_group({ where => $where, value => \@values, *************** *** 74,80 **** unless ( $self->param( 'date' ) ) { ! my $p_day = $request->param( 'news_day' ); ! my $p_month = $request->param( 'news_month' ); ! my $p_year = $request->param( 'news_year' ); if ( $p_month and $p_year ) { $self->param( date => join( '-', $p_day, $p_month, $p_year ) ); --- 88,97 ---- unless ( $self->param( 'date' ) ) { ! my $p_day = $self->param( 'search_day' ) || ! $request->param( 'news_day' ); ! my $p_month = $self->param( 'search_month' ) || ! $request->param( 'news_month' ); ! my $p_year = $self->param( 'search_year' ) || ! $request->param( 'news_year' ); if ( $p_month and $p_year ) { $self->param( date => join( '-', $p_day, $p_month, $p_year ) ); *************** *** 138,147 **** $log->debug( "Quering news objects with: $where\n", "and values: ", join( ', ', @values ) ); - my $news_class = CTX->lookup_object( 'news' ); my $items = eval { ! $news_class->fetch_group({ where => $where, ! value => \@values, ! column_group => 'listing', ! order => 'posted_on DESC' }) }; $params{news_list} = $self->_massage_news_list( $items ); --- 155,165 ---- $log->debug( "Quering news objects with: $where\n", "and values: ", join( ', ', @values ) ); my $items = eval { ! OpenInteract2::News->fetch_group({ ! where => $where, ! value => \@values, ! column_group => 'listing', ! order => 'posted_on DESC', ! }) }; $params{news_list} = $self->_massage_news_list( $items ); *************** *** 268,281 **** 'ID/email is given.</p>'; } - my $news_class = CTX->lookup_object( 'news' ); my @news_list = (); foreach my $nid ( @news_id ) { ! my $news = eval { $news_class->fetch( $nid ) }; push @news_list, $news if ( $news ); } ! my $rv = $news_class->notify({ email => $email, ! subject => 'News notification', ! object => \@news_list, ! type => 'news' }); if ( $rv ) { return '<h2 align="center">Success!</h2>' . --- 286,300 ---- 'ID/email is given.</p>'; } my @news_list = (); foreach my $nid ( @news_id ) { ! my $news = eval { OpenInteract2::News->fetch( $nid ) }; push @news_list, $news if ( $news ); } ! my $rv = OpenInteract2::News->notify({ ! email => $email, ! subject => 'News notification', ! object => \@news_list, ! type => 'news', ! }); if ( $rv ) { return '<h2 align="center">Success!</h2>' . *************** *** 311,315 **** my $request = CTX->request; - my $news_class = CTX->lookup_object( 'news' ); my @do_edit = $request->param( 'do_edit' ); my ( $success, $attempt ); --- 330,333 ---- *************** *** 318,322 **** next unless ( $news_id ); $attempt++; ! my $news = eval { $news_class->fetch( $news_id ) }; if ( $@ ) { $log->error( "Cannot fetch news '$news_id': $@" ); --- 336,340 ---- next unless ( $news_id ); $attempt++; ! my $news = eval { OpenInteract2::News->fetch( $news_id ) }; if ( $@ ) { $log->error( "Cannot fetch news '$news_id': $@" ); *************** *** 341,344 **** --- 359,424 ---- } + # counts by month + sub archive_by_month { + my ( $self ) = @_; + $log ||= get_logger( LOG_APP ); + + my $dbh = OpenInteract2::News->global_datasource_handle(); + my $driver = $dbh->{Driver}{Name}; + + # selects: count, year, month + my $sql = $self->_get_count_by_month_sql( $driver ); + unless ( $sql ) { + $self->add_error_key( 'news.error.db_unsupported', $driver ); + return $self->execute({ task => 'home' }); + } + my ( $sth ); + eval { + $sth = $dbh->prepare( $sql ); + $sth->execute; + }; + if ( $@ ) { + $log->warn( "Error preparing/executing SQL for '$driver' DB: $@" ); + $self->add_error_key( 'news.error.fetch_multiple', "$@" ); + return undef; + } + my @counts = (); + while ( my $row = $sth->fetchrow_arrayref ) { + push @counts, { + count => $row->[0], + year => $row->[1], + month => $row->[2], + }; + } + return $self->generate_content({ + counts => \@counts, + }); + } + + sub _get_count_by_month_sql { + my ( $self, $driver ) = @_; + $driver = lc $driver; + my $table = OpenInteract2::News->table_name; + my $trailing = " FROM $table\n" . + " GROUP BY year, month\n" . + " ORDER BY year DESC, month DESC\n"; + if ( 'pg' eq $driver ) { + return "SELECT count(*), date_part( 'year', posted_on ) as year, " . + " date_part( 'month', posted_on ) as month\n" . + $trailing; + } + elsif ( 'mysql' eq $driver ) { + return "SELECT count(*), EXTRACT( YEAR FROM posted_on ) as year, " . + " EXTRACT( MONTH FROM posted_on ) as month\n" . + $trailing; + } + elsif ( 'sqlite' eq $driver ) { + return "SELECT count(*), substr( posted_on, 1, 4 ) as year, " . + " substr( posted_on, 6, 2 ) as month\n" . + $trailing; + } + return; + } + # Get all sections, or add an error message to the action |