| 1 | package AxKit::App::TABOO::Data::Plurals::Stories; | ||
| 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::Story; | ||
| 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.18'; | ||
| 20 | |||
| 21 | AxKit::App::TABOO::Data::Plurals::Stories->dbtable("stories"); | ||
| 22 | AxKit::App::TABOO::Data::Plurals::Stories->dbfrom("stories"); | ||
| 23 | |||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | AxKit::App::TABOO::Data::Plurals::Stories - Data objects to handle multiple Stories in TABOO | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Often, you want to retrieve many different stories from the data store, for example all belonging to a certain category or a certain section. This is a typical situation where this class shoule be used. | ||
| 32 | |||
| 33 | =head2 Methods | ||
| 34 | |||
| 35 | =over | ||
| 36 | |||
| 37 | =item C<new(@dbconnectargs)> | ||
| 38 | |||
| 39 | The constructor. Nothing special. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $that = shift; | ||
| 45 | my $class = ref($that) || $that; | ||
| 46 | my $self = { | ||
| 47 | ENTRIES => [], # Internally, some methods finds it useful that the entries are stored in a array of this name. | ||
| 48 | DBCONNECTARGS => \@_, | ||
| 49 | XMLELEMENT => undef, | ||
| 50 | XMLPREFIX => undef, | ||
| 51 | XMLNS => undef, | ||
| 52 | }; | ||
| 53 | bless($self, $class); | ||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | =item C<load(what =E<gt> fields, limit =E<gt> {key =E<gt> value, [...]}, orderby =E<gt> fields, entries =E<gt> number)> | ||
| 59 | |||
| 60 | This load method can be used to retrieve a number of entries from a | ||
| 61 | data store. It uses named parameters, the first C<what> is used to | ||
| 62 | determine which fields to retrieve. It is a string consisting of a | ||
| 63 | commaseparated list of fields, as specified in the data store. The | ||
| 64 | C<limit> argument is to be used to determine which records to | ||
| 65 | retrieve, these will be combined by logical AND. You may also supply a | ||
| 66 | C<orderby> argument, which is an expression used to determine the | ||
| 67 | order of entries returned. Usually, it would be a simple string with | ||
| 68 | the field name to use, e.g. C<'timestamp'>, but you might want to | ||
| 69 | append the keyword "C<DESC>" to it for descending order. Finally, you | ||
| 70 | may supply a C<entries> argument, which is the maximum number of | ||
| 71 | entries to retrieve. | ||
| 72 | |||
| 73 | It will retrieve the data, and then call C<populate()> for each of the | ||
| 74 | records retrieved to ensure that the plural data objects actually | ||
| 75 | consists of an array of L<AxKit::App::TABOO::Data::Story>s. But it | ||
| 76 | calls the internal C<_load()>-method to do the hard work (and that's | ||
| 77 | in the parent class). | ||
| 78 | |||
| 79 | If there is no data that corresponds to the given arguments, this method will return C<undef>. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub load { | ||
| 84 | my ($self, %args) = @_; | ||
| 85 | my $data = $self->_load(%args); # Does the hard work | ||
| 86 | return undef unless (@{$data}); | ||
| 87 | foreach my $entry (@{$data}) { | ||
| 88 | my $story = AxKit::App::TABOO::Data::Story->new($self->dbconnectargs()); | ||
| 89 | $story->populate($entry); | ||
| 90 | $story->onfile; | ||
| 91 | $self->Push($story); | ||
| 92 | } | ||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | =item C<addcatinfo> | ||
| 98 | |||
| 99 | =item C<adduserinfo> | ||
| 100 | |||
| 101 | These two methods are implemented in a plurals context, and can be called on a plurals object just like a singular object. Each entry will have their data structure extended with user and category information. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | sub addcatinfo { | ||
| 107 | my $self = shift; | ||
| 108 | foreach my $story (@{${$self}{ENTRIES}}) { | ||
| 109 | $story->addcatinfo; | ||
| 110 | } | ||
| 111 | return $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub adduserinfo { | ||
| 116 | my $self = shift; | ||
| 117 | foreach my $story (@{${$self}{ENTRIES}}) { | ||
| 118 | $story->adduserinfo; | ||
| 119 | } | ||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | =item C<exists(key =E<gt> value, [...])> | ||
| 124 | |||
| 125 | This checks if there exists a story with the limits specified as a | ||
| 126 | hash, by the template of the C<limit> argument of the C<load> method. | ||
| 127 | |||
| 128 | It will return the number of such stories. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | |||
| 133 | sub exists { | ||
| 134 | my ($self, %limit) = @_; | ||
| 135 | if (%limit) { | ||
| 136 | return scalar(@{$self->_load(what => '1', limit => \%limit)}); | ||
| 137 | } else { | ||
| 138 | return scalar(@{$self->_load(what => '1')}); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | =back | ||
| 146 | |||
| 147 | =head1 BUGS/TODO | ||
| 148 | |||
| 149 | Not anything particular at the moment... | ||
| 150 | |||
| 151 | |||
| 152 | =head1 FORMALITIES | ||
| 153 | |||
| 154 | See L<AxKit::App::TABOO>. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 |