| 1 | package AxKit::App::TABOO::XSP::Language; | ||
| 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::Data::Language; | ||
| 9 | use AxKit::App::TABOO::Data::Plurals::Languages; | ||
| 10 | use Time::Piece ':override'; | ||
| 11 | use XML::LibXML; | ||
| 12 | |||
| 13 | |||
| 14 | use vars qw/$NS/; | ||
| 15 | |||
| 16 | |||
| 17 | our $VERSION = '0.4'; | ||
| 18 | |||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | AxKit::App::TABOO::XSP::Language - Language management tag library for TABOO | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | Add the language: namespace to your XSP C<E<lt>xsp:pageE<gt>> tag, e.g.: | ||
| 27 | |||
| 28 | <xsp:page | ||
| 29 | language="Perl" | ||
| 30 | xmlns:xsp="http://apache.org/xsp/core/v1" | ||
| 31 | xmlns:language="http://www.kjetil.kjernsmo.net/software/TABOO/NS/Language" | ||
| 32 | > | ||
| 33 | |||
| 34 | Add this taglib to AxKit (via httpd.conf or .htaccess): | ||
| 35 | |||
| 36 | AxAddXSPTaglib AxKit::App::TABOO::XSP::Language | ||
| 37 | |||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This XSP taglib provides two tags to retrieve a structured XML | ||
| 42 | fragment with all information of a single language or all languages | ||
| 43 | of a certain type. | ||
| 44 | |||
| 45 | L<Apache::AxKit::Language::XSP::SimpleTaglib> has been used to write | ||
| 46 | this taglib. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | $NS = 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Language'; | ||
| 53 | |||
| 54 | package AxKit::App::TABOO::XSP::Language::Handlers; | ||
| 55 | |||
| 56 | |||
| 57 | =head1 Tag Reference | ||
| 58 | |||
| 59 | =head2 C<E<lt>get-language lang="foo"/E<gt>> | ||
| 60 | |||
| 61 | This tag will replace itself with some structured XML containing all | ||
| 62 | fields of languages of type C<foo>. It relates to the TABOO Data | ||
| 63 | object L<AxKit::App::TABOO::Data::Language>, and calls on that to do | ||
| 64 | the hard work. | ||
| 65 | |||
| 66 | The root element of the returned object is C<cat:languages> and each | ||
| 67 | language is wrapped in an element C<cat:language> and contains C<lang> | ||
| 68 | and C<name>. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub get_language : struct attribOrChild(lang) { | ||
| 73 | return << 'EOC' | ||
| 74 | my $lang = AxKit::App::TABOO::Data::Language->new(); | ||
| 75 | $lang->load(limit => {code => $attr_lang}); | ||
| 76 | my $doc = XML::LibXML::Document->new(); | ||
| 77 | my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Language/Output', 'lang:languages'); | ||
| 78 | $doc->setDocumentElement($root); | ||
| 79 | $doc = $lang->write_xml($doc, $root); | ||
| 80 | $doc; | ||
| 81 | EOC | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | =head2 C<E<lt>get-languages/E<gt>> | ||
| 86 | |||
| 87 | This tag will replace itself with some structured XML containing all | ||
| 88 | languages. It relates to the TABOO Data object | ||
| 89 | L<AxKit::App::TABOO::Data::Plurals::Languages>, and calls on that to | ||
| 90 | do the hard work. | ||
| 91 | |||
| 92 | The root element of the returned object is C<languages> and each | ||
| 93 | language is wrapped in an element C<language>, and ordered | ||
| 94 | alphabetically by local name. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | # TODO: The code will also be available in an attribute C<xml:lang>, | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | sub get_languages : struct attribOrChild(code,onlycontent) { | ||
| 103 | return << 'EOC' | ||
| 104 | my $langs = AxKit::App::TABOO::Data::Plurals::Languages->new(); | ||
| 105 | $langs->load(orderby => 'localname'); | ||
| 106 | my $doc = XML::LibXML::Document->new(); | ||
| 107 | my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Language/Output', 'lang:languages'); | ||
| 108 | $doc->setDocumentElement($root); | ||
| 109 | $doc = $langs->write_xml($doc, $root); | ||
| 110 | $doc; | ||
| 111 | EOC | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | =head2 C<E<lt>exists lang="foo"/E<gt>> | ||
| 116 | |||
| 117 | This tag will check if a language allready exists. It is a boolean | ||
| 118 | tag, which has child elements C<E<lt>trueE<gt>> and | ||
| 119 | C<E<lt>falseE<gt>>. It takes a lang parameter, which may be given as | ||
| 120 | an attribute or a child element named C<lang>, and if the language is | ||
| 121 | found in the data store, the contents of C<E<lt>trueE<gt>> child | ||
| 122 | element is included, otherwise, the contents of C<E<lt>falseE<gt>> is | ||
| 123 | included. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub exists : attribOrChild(lang) { | ||
| 128 | return ''; # Gotta be something here | ||
| 129 | } | ||
| 130 | |||
| 131 | sub exists___true__open { | ||
| 132 | return << 'EOC'; | ||
| 133 | my $lang = AxKit::App::TABOO::Data::Language->new(); | ||
| 134 | if ($lang->load(what => '1', limit => {lang => $attr_lang})) { | ||
| 135 | EOC | ||
| 136 | } | ||
| 137 | |||
| 138 | sub exists___true { | ||
| 139 | return '}' | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | sub exists___false__open { | ||
| 144 | return << 'EOC'; | ||
| 145 | my $lang = AxKit::App::TABOO::Data::Language->new(); | ||
| 146 | unless ($lang->load(what => '1', limit => {lang => $attr_lang})) { | ||
| 147 | EOC | ||
| 148 | } | ||
| 149 | |||
| 150 | sub exists___false { | ||
| 151 | return '}' | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | |||
| 159 | =head1 FORMALITIES | ||
| 160 | |||
| 161 | See L<AxKit::App::TABOO>. | ||
| 162 | |||
| 163 | =cut |