| 1 | package AxKit::App::TABOO::XSP::Category; | ||
| 2 | use 5.6.0; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Apache::AxKit::Language::XSP::SimpleTaglib; | ||
| 6 | use Apache::AxKit::Exception; | ||
| 7 | use AxKit; | ||
| 8 | use AxKit::App::TABOO; | ||
| 9 | use AxKit::App::TABOO::Data::Category; | ||
| 10 | use AxKit::App::TABOO::Data::Plurals::Categories; | ||
| 11 | use Session; | ||
| 12 | use Time::Piece ':override'; | ||
| 13 | use XML::LibXML; | ||
| 14 | |||
| 15 | |||
| 16 | use vars qw/$NS/; | ||
| 17 | |||
| 18 | |||
| 19 | our $VERSION = '0.4'; | ||
| 20 | |||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | AxKit::App::TABOO::XSP::Category - Category management tag library for TABOO | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | Add the category: namespace to your XSP C<E<lt>xsp:pageE<gt>> tag, e.g.: | ||
| 29 | |||
| 30 | <xsp:page | ||
| 31 | language="Perl" | ||
| 32 | xmlns:xsp="http://apache.org/xsp/core/v1" | ||
| 33 | xmlns:category="http://www.kjetil.kjernsmo.net/software/TABOO/NS/Category" | ||
| 34 | > | ||
| 35 | |||
| 36 | Add this taglib to AxKit (via httpd.conf or .htaccess): | ||
| 37 | |||
| 38 | AxAddXSPTaglib AxKit::App::TABOO::XSP::Category | ||
| 39 | |||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | This XSP taglib provides two tags to retrieve a structured XML | ||
| 44 | fragment with all information of a single category or all categories | ||
| 45 | of a certain type. | ||
| 46 | |||
| 47 | L<Apache::AxKit::Language::XSP::SimpleTaglib> has been used to write | ||
| 48 | this taglib. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | $NS = 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Category'; | ||
| 55 | |||
| 56 | sub _sanatize_catname { | ||
| 57 | my $tmp = lc shift; | ||
| 58 | $tmp =~ tr/a-z/_/cs; | ||
| 59 | return $tmp; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | package AxKit::App::TABOO::XSP::Category::Handlers; | ||
| 64 | |||
| 65 | |||
| 66 | =head1 Tag Reference | ||
| 67 | |||
| 68 | =head2 C<E<lt>get-category catname="foo"/E<gt>> | ||
| 69 | |||
| 70 | This tag will replace itself with some structured XML containing all | ||
| 71 | fields of categories of type C<foo>. It relates to the TABOO Data | ||
| 72 | object L<AxKit::App::TABOO::Data::Category>, and calls on that to do | ||
| 73 | the hard work. | ||
| 74 | |||
| 75 | The root element of the returned object is C<cat:categories> and each | ||
| 76 | category is wrapped in an element C<cat:category> and contains C<catname> | ||
| 77 | and C<name>. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub get_category : struct attribOrChild(catname) { | ||
| 82 | return << 'EOC' | ||
| 83 | my $cat = AxKit::App::TABOO::Data::Category->new(); | ||
| 84 | $cat->load(limit => {catname => $attr_catname}); | ||
| 85 | my $doc = XML::LibXML::Document->new(); | ||
| 86 | my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Category/Output', 'cat:categories'); | ||
| 87 | $doc->setDocumentElement($root); | ||
| 88 | $doc = $cat->write_xml($doc, $root); | ||
| 89 | $doc; | ||
| 90 | EOC | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | =head2 C<E<lt>get-categories type="foo" onlycontent="true"/E<gt>> | ||
| 95 | |||
| 96 | This tag will replace itself with some structured XML containing all | ||
| 97 | categories of type C<foo>. It relates to the TABOO Data object | ||
| 98 | L<AxKit::App::TABOO::Data::Plurals::Categories>, and calls on that to | ||
| 99 | do the hard work. See the documentation of that class to see the | ||
| 100 | available types. If a boolean C<onlycontent> attribute (or child | ||
| 101 | element) is set, it will check if there are articles or stories in the | ||
| 102 | C<categ> category types, and return only those. | ||
| 103 | |||
| 104 | The root element of the returned object is C<categories> and each | ||
| 105 | category is wrapped in an element (surprise!) C<category>. The type | ||
| 106 | will also be available in an attribute called C<type>, and ordered | ||
| 107 | alphabetically by name. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub get_categories : struct attribOrChild(type,onlycontent) { | ||
| 112 | return << 'EOC' | ||
| 113 | my $cats = AxKit::App::TABOO::Data::Plurals::Categories->new(); | ||
| 114 | $cats->load(limit => {type => $attr_type}, | ||
| 115 | onlycontent => $attr_onlycontent, | ||
| 116 | orderby => 'name'); | ||
| 117 | my $doc = XML::LibXML::Document->new(); | ||
| 118 | my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Category/Output', 'cat:categories'); | ||
| 119 | $root->setAttribute('type', $attr_type); | ||
| 120 | $doc->setDocumentElement($root); | ||
| 121 | $doc = $cats->write_xml($doc, $root); | ||
| 122 | $doc; | ||
| 123 | EOC | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | =head2 C<E<lt>store/E<gt>> | ||
| 128 | |||
| 129 | It will take whatever data it finds in the L<Apache::Request> object | ||
| 130 | held by AxKit, and hand it to a new | ||
| 131 | L<AxKit::App::TABOO::Data::Article> object, which will use whatever | ||
| 132 | data it finds useful. It will not store anything unless the user is | ||
| 133 | logged in and authenticated with an authorization level. It will | ||
| 134 | perform different sanity checks and throw exceptions if the user tries | ||
| 135 | to add data it is not authorized to do. | ||
| 136 | |||
| 137 | Finally, the Data object is instructed to save itself. | ||
| 138 | |||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | |||
| 143 | sub store { | ||
| 144 | return << 'EOC' | ||
| 145 | my %args = map { $_ => join('', $cgi->param($_)) } $cgi->param; | ||
| 146 | my $authlevel = AxKit::App::TABOO::authlevel(AxKit::App::TABOO::session($r)); | ||
| 147 | AxKit::Debug(9, "Logged in as $args{'username'} at level $authlevel"); | ||
| 148 | unless ($authlevel >= 1) { | ||
| 149 | throw Apache::AxKit::Exception::Retval( | ||
| 150 | return_code => AUTH_REQUIRED, | ||
| 151 | -text => "Not authenticated and authorized with an authlevel"); | ||
| 152 | } | ||
| 153 | my $cat = AxKit::App::TABOO::Data::Category->new(); | ||
| 154 | |||
| 155 | if (($args{'type'} eq 'stsec') && ($authlevel < 6)) { | ||
| 156 | throw Apache::AxKit::Exception::Retval( | ||
| 157 | return_code => FORBIDDEN, | ||
| 158 | -text => "Authlevel 6 is needed to make new sections. Your level: " . $authlevel); | ||
| 159 | } | ||
| 160 | if ((($args{'type'} eq 'categ') || ($args{'type'} eq 'angle')) && ($authlevel < 4)) { | ||
| 161 | throw Apache::AxKit::Exception::Retval( | ||
| 162 | return_code => FORBIDDEN, | ||
| 163 | -text => "Authlevel 4 is needed to make new categories. Your level: " . $authlevel); | ||
| 164 | } | ||
| 165 | |||
| 166 | $args{'catname'} = AxKit::App::TABOO::XSP::Category::_sanatize_catname($args{'catname'}); | ||
| 167 | $cat->populate(\%args); | ||
| 168 | $cat->save(); | ||
| 169 | EOC | ||
| 170 | } | ||
| 171 | |||
| 172 | =head2 C<E<lt>exists catname="foo"/E<gt>> | ||
| 173 | |||
| 174 | This tag will check if a category allready exists. It is a boolean | ||
| 175 | tag, which has child elements C<E<lt>trueE<gt>> and | ||
| 176 | C<E<lt>falseE<gt>>. It takes a catname, which may be given as an | ||
| 177 | attribute or a child element named C<catname>, and if the category is | ||
| 178 | found in the data store, the contents of C<E<lt>trueE<gt>> child | ||
| 179 | element is included, otherwise, the contents of C<E<lt>falseE<gt>> is | ||
| 180 | included. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub exists : attribOrChild(catname) { | ||
| 185 | return ''; # Gotta be something here | ||
| 186 | } | ||
| 187 | |||
| 188 | sub exists___true__open { | ||
| 189 | return << 'EOC'; | ||
| 190 | my $category = AxKit::App::TABOO::Data::Category->new(); | ||
| 191 | if (($attr_catname =~ m/submit/) || | ||
| 192 | ($category->load(what => '1', limit => {catname => $attr_catname}))) { | ||
| 193 | EOC | ||
| 194 | } | ||
| 195 | |||
| 196 | sub exists___true { | ||
| 197 | return '}' | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | sub exists___false__open { | ||
| 202 | return << 'EOC'; | ||
| 203 | my $category = AxKit::App::TABOO::Data::Category->new(); | ||
| 204 | unless (($attr_catname =~ m/submit/) || | ||
| 205 | ($category->load(what => '1', limit => {catname => $attr_catname}))) { | ||
| 206 | EOC | ||
| 207 | } | ||
| 208 | |||
| 209 | sub exists___false { | ||
| 210 | return '}' | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | 1; | ||
| 216 | |||
| 217 | |||
| 218 | =head1 FORMALITIES | ||
| 219 | |||
| 220 | See L<AxKit::App::TABOO>. | ||
| 221 | |||
| 222 | =cut |