Foorum

 view release on metacpan or  search on metacpan

lib/Foorum/ResultSet/Forum.pm  view on Meta::CPAN


    my $forum;    # return value
    my $forum_id = 0;
    if ( $forum_code =~ /^\d+$/ ) {
        $forum_id = $forum_code;
    } else {
        my $mem_key = 'global|forum_code_to_id';
        my $mem_val = $cache->get($mem_key);
        if ( $mem_val and $mem_val->{$forum_code} ) {
            $forum_id = $mem_val->{$forum_code};
        } else {
            $forum = $self->search( { forum_code => $forum_code } )->first;
            return unless $forum;
            $forum_id = $forum->forum_id;
            $mem_val->{$forum_code} = $forum_id;
            $cache->set( $mem_key, $mem_val, 36000 );    # 10 hours

            # set cache
            $forum = $forum->{_column_data};             # hash for cache
            $forum->{settings} = $schema->resultset('ForumSettings')
                ->get_basic( $forum->{forum_id} );
            $forum->{forum_url} = $self->get_forum_url($forum);
            $cache->set( "forum|forum_id=$forum_id",
                { val => $forum, 1 => 2 }, 7200 );
        }
    }

    return unless ($forum_id);

    unless ($forum) {    # do not get from convert forum_code to forum_id
        my $cache_key = "forum|forum_id=$forum_id";
        my $cache_val = $cache->get($cache_key);

        if ( $cache_val and $cache_val->{val} ) {
            $forum = $cache_val->{val};
        } else {
            $forum = $self->find( { forum_id => $forum_id } );
            return unless ($forum);

            # set cache
            $forum = $forum->{_column_data};    # hash for cache
            $forum->{settings} = $schema->resultset('ForumSettings')
                ->get_basic( $forum->{forum_id} );
            $forum->{forum_url} = $self->get_forum_url($forum);
            $cache->set( "forum|forum_id=$forum_id",
                { val => $forum, 1 => 2 }, 7200 );
        }
    }

    return $forum;
}

sub get_forum_url {
    my ( $self, $forum ) = @_;

    my $forum_url = '/forum/' . $forum->{forum_code};

    return $forum_url;
}

sub update_forum {
    my ( $self, $forum_id, $update ) = @_;

    my $schema = $self->result_source->schema;
    my $cache  = $schema->cache();

    $self->search( { forum_id => $forum_id } )->update($update);

    $cache->remove("forum|forum_id=$forum_id");

    if ( $update->{forum_code} ) {
        my $mem_key = 'global|forum_code_to_id';
        my $mem_val = $cache->get($mem_key);
        $mem_val->{ $update->{forum_code} } = $forum_id;
        $cache->set( $mem_key, $mem_val, 36000 );    # 10 hours
    }
}

sub remove_forum {
    my ( $self, $forum_id ) = @_;

    my $schema = $self->result_source->schema;
    my $cache  = $schema->cache();

    $self->search( { forum_id => $forum_id, } )->delete;
    $schema->resultset('LogAction')->search( { forum_id => $forum_id } )
        ->delete;

    # remove user_forum
    $schema->resultset('UserForum')->search( { forum_id => $forum_id } )
        ->delete;

    # get all topic_ids
    my @topic_ids;
    my $tp_rs = $schema->resultset('Topic')
        ->search( { forum_id => $forum_id, }, { columns => ['topic_id'], } );
    while ( my $r = $tp_rs->next ) {
        push @topic_ids, $r->topic_id;
    }
    $schema->resultset('Topic')->search( { forum_id => $forum_id, } )->delete;

    # get all poll_ids
    my @poll_ids;
    my $pl_rs = $schema->resultset('Poll')
        ->search( { forum_id => $forum_id, }, { columns => ['poll_id'], } );
    while ( my $r = $pl_rs->next ) {
        push @poll_ids, $r->poll_id;
    }
    $schema->resultset('Poll')->search( { forum_id => $forum_id, } )->delete;
    if ( scalar @poll_ids ) {
        $schema->resultset('PollOption')
            ->search( { poll_id => { 'IN', \@poll_ids }, } )->delete;
        $schema->resultset('PollResult')
            ->search( { poll_id => { 'IN', \@poll_ids }, } )->delete;
    }

    # comment and star/share
    if ( scalar @topic_ids ) {
        $schema->resultset('Comment')->search(
            {   object_type => 'topic',
                object_id   => { 'IN', \@topic_ids },



( run in 0.790 second using v1.01-cache-2.11-cpan-9581c071862 )