| 1 | package AxKit::App::TABOO::Data::Plurals::Comments; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | use AxKit::App::TABOO::Data; | ||
| 8 | use AxKit::App::TABOO::Data::Comment; | ||
| 9 | use AxKit::App::TABOO::Data::Plurals; | ||
| 10 | |||
| 11 | |||
| 12 | use vars qw/@ISA/; | ||
| 13 | @ISA = qw(AxKit::App::TABOO::Data::Plurals); | ||
| 14 | |||
| 15 | use DBI; | ||
| 16 | use Exception::Class::DBI; | ||
| 17 | |||
| 18 | |||
| 19 | our $VERSION = '0.093'; | ||
| 20 | |||
| 21 | AxKit::App::TABOO::Data::Plurals::Comments->dbtable("comments"); | ||
| 22 | AxKit::App::TABOO::Data::Plurals::Comments->dbfrom("comments"); | ||
| 23 | |||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | AxKit::App::TABOO::Data::Plurals::Comments - Data objects to handle | ||
| 28 | multiple Comments in TABOO | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | Usually, you want to retrieve many different comments from the data | ||
| 33 | store, for example all comments below a certain comment. You may use | ||
| 34 | this class to retrieve all comments, comments just below a specified | ||
| 35 | comment or a threaded tree. | ||
| 36 | |||
| 37 | =head2 Methods | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =item C<new(@dbconnectargs)> | ||
| 42 | |||
| 43 | The constructor. Nothing special. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my $that = shift; | ||
| 49 | my $class = ref($that) || $that; | ||
| 50 | my $self = { | ||
| 51 | ENTRIES => [], # Internally, some methods finds it useful that the entries are stored in a array of this name. | ||
| 52 | DBCONNECTARGS => \@_, | ||
| 53 | XMLELEMENT => 'reply', | ||
| 54 | XMLPREFIX => 'comm', | ||
| 55 | XMLNS => 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output', | ||
| 56 | |||
| 57 | }; | ||
| 58 | bless($self, $class); | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | =item C<load(what =E<gt> fields, limit =E<gt> {key =E<gt> value, [...]}, orderby =E<gt> fields)> | ||
| 65 | |||
| 66 | This load method can be used to retrieve a number of entries from a | ||
| 67 | data store. It uses named parameters, the first C<what> is used to | ||
| 68 | determine which fields to retrieve. It is a string consisting of a | ||
| 69 | commaseparated list of fields, as specified in the data store. The | ||
| 70 | C<limit> argument is to be used to determine which records to | ||
| 71 | retrieve, these will be combined by logical AND. You will most | ||
| 72 | certainly want to supply the C<sectionid>, the C<storyname> and | ||
| 73 | usually the C<commentpath>. | ||
| 74 | |||
| 75 | The latter is described in the | ||
| 76 | L<singular|AxKit::App::TABOO::Data::Comment> class documentation. It | ||
| 77 | is worth noting that if the commentpath ends with a C</>, this method | ||
| 78 | will return only the comments on the next level, if it doesn't, it | ||
| 79 | will return the whole tree starting with the given commentpath. If no | ||
| 80 | commentpath is given, a tree of all comments are retrieved. | ||
| 81 | |||
| 82 | You may also supply a C<orderby> argument, which is an expression used | ||
| 83 | to determine the order of entries returned. Usually, it would be a | ||
| 84 | simple string with the field name to use, e.g. C<'timestamp'>, but you | ||
| 85 | might want to append the keyword "C<DESC>" to it for descending | ||
| 86 | order. Finally, you may supply a C<entries> argument, which is the | ||
| 87 | maximum number of entries to retrieve. | ||
| 88 | |||
| 89 | It will retrieve the data, and then call C<populate()> for each of the | ||
| 90 | records retrieved to ensure that the plural data objects actually | ||
| 91 | consists of an array of L<AxKit::App::TABOO::Data::Comment>s. | ||
| 92 | |||
| 93 | If there is no data that corresponds to the given arguments, this | ||
| 94 | method will return C<undef>. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub load { | ||
| 99 | my ($self, %args) = @_; | ||
| 100 | my $array = 0; | ||
| 101 | my $basepath = $args{'limit'}{'commentpath'}; | ||
| 102 | if ($basepath =~ m|/$|) { | ||
| 103 | # If it ends with a / we will only retrieve the records on the | ||
| 104 | # next level, and they will be a single, non-threaded array | ||
| 105 | $array = 1; | ||
| 106 | $args{'limit'}{'commentpath'} = '^' . $basepath . '[a-z]+?_?[0-9]*/?$'; | ||
| 107 | } | ||
| 108 | my $data = $self->_load(%args, regex => ['commentpath']); | ||
| 109 | return undef unless (@{$data}); | ||
| 110 | if ($array) { | ||
| 111 | foreach my $entry (@{$data}) { | ||
| 112 | my $comment = AxKit::App::TABOO::Data::Comment->new($self->dbconnectargs()); | ||
| 113 | $comment->populate($entry); | ||
| 114 | $comment->onfile; | ||
| 115 | $self->Push($comment); | ||
| 116 | } | ||
| 117 | } else { | ||
| 118 | my %data; | ||
| 119 | foreach my $entry (@{$data}) { | ||
| 120 | $data{${$entry}{'commentpath'}} = $entry; | ||
| 121 | } | ||
| 122 | my $comment = AxKit::App::TABOO::Data::Comment->new($self->dbconnectargs()); | ||
| 123 | if (length($basepath) == 0) { | ||
| 124 | my @build = grep(m|^/[a-z]+?_?[0-9]*$|, keys(%data)); | ||
| 125 | return undef if ($#build < 0); | ||
| 126 | foreach (@build) { | ||
| 127 | # Apparently, I can't just modify $self straightforwardly. Quirky | ||
| 128 | my $comment = AxKit::App::TABOO::Data::Comment->new($self->dbconnectargs()); | ||
| 129 | $comment->populate($data{$_}); | ||
| 130 | $comment->onfile; | ||
| 131 | $comment->reply($self->_threadhelper($_, %data)); | ||
| 132 | $self->Push($comment); | ||
| 133 | } | ||
| 134 | } else { | ||
| 135 | $comment->populate($data{$basepath}); | ||
| 136 | $comment->onfile; | ||
| 137 | $comment->reply($self->_threadhelper($basepath, %data)); | ||
| 138 | $self->Push($comment); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | return $self; | ||
| 142 | } | ||
| 143 | |||
| 144 | # Internal helper method. Will walk all nodes in a tree to add replies | ||
| 145 | # recursively in the cases where a complete tree of comments are to be | ||
| 146 | # built. Takes as argument the commentpath we're starting out from, | ||
| 147 | # and a %data hash containing all comments with the commentpath as a | ||
| 148 | # key. | ||
| 149 | |||
| 150 | sub _threadhelper { | ||
| 151 | my $self = shift; | ||
| 152 | my $path = shift; | ||
| 153 | my %data = @_; | ||
| 154 | my @build = grep(m|^$path/[a-z]+?_?[0-9]*$|, keys(%data)); | ||
| 155 | return undef if ($#build < 0); | ||
| 156 | my $comments = AxKit::App::TABOO::Data::Plurals::Comments->new($self->dbconnectargs()); | ||
| 157 | foreach (@build) { | ||
| 158 | my $comment = AxKit::App::TABOO::Data::Comment->new($self->dbconnectargs()); | ||
| 159 | $comment->populate($data{$_}); | ||
| 160 | $comment->onfile; | ||
| 161 | $comment->reply($self->_threadhelper($_, %data)); | ||
| 162 | $comments->Push($comment); | ||
| 163 | } | ||
| 164 | return $comments; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | =item C<adduserinfo> | ||
| 170 | |||
| 171 | This methods is implemented in a plurals context, and can be called on | ||
| 172 | a plurals object just like a singular object. Each entry will have | ||
| 173 | their data structure extended with user information. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | |||
| 178 | sub adduserinfo { | ||
| 179 | my $self = shift; | ||
| 180 | foreach my $comment (@{${$self}{ENTRIES}}) { | ||
| 181 | $comment->adduserinfo; | ||
| 182 | if ((ref($comment->reply) eq 'AxKit::App::TABOO::Data::Comment') || | ||
| 183 | (ref($comment->reply) eq 'AxKit::App::TABOO::Data::Plurals::Comments')) { | ||
| 184 | $comment->reply->adduserinfo; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | return $self; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | =item C<exist(commentpath =E<gt> '/foo', storyname =E<gt> 'story', sectionid =E<gt> 'section')> | ||
| 192 | |||
| 193 | This method can be used to check if there are one or more comments | ||
| 194 | starting with the commentpath given. It takes a hash to identify the | ||
| 195 | comment. It will return a scalar with the number of found comments. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub exist { | ||
| 200 | my $self = shift; | ||
| 201 | my %args = @_; | ||
| 202 | $args{'commentpath'} = '^' . $args{'commentpath'} . '_?[0-9]*[^/]?$'; | ||
| 203 | my $data = $self->_load(what => 'commentpath', regex => ['commentpath'], limit => \%args); | ||
| 204 | return scalar(@{$data}); | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =head1 BUGS/TODO | ||
| 211 | |||
| 212 | It might be a good idea to return just a singular type object if there | ||
| 213 | is indeed just a single object that is returned from load. | ||
| 214 | |||
| 215 | |||
| 216 | =head1 FORMALITIES | ||
| 217 | |||
| 218 | See L<AxKit::App::TABOO>. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 |