From cbbd1ad16fb4b452c89cb90b902a7e34601e762a Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Mon, 12 Dec 2016 02:41:59 +0000 Subject: [PATCH] Bug 15326: (follow-up) Add CMS feature This patch builds on Martin Persson's patch. I made a few changes that improve the patch and make this feature functional. The only thing I am not completely satisfied with (and doesn't reliably work) is the navigation for the CMS pages on both the staff side and the OPAC side. This needs work. Otherwise the CMS feature works. - Make sure you update database after applying patch - After adding a few pages, confirm that filtering results works (play with different combinations of display location, library and language) - Confirm that pages with specific locations ONLY show up in those locations, i.e. OPAC pages can only be accessed in OPAC and staff interface pages can only be accessed on staff client - Confirm that table is showing correct data, particularly language, location and library - Confirm that if page has not been published and user does not have permission to edit pages, page content will not display on OPAC side. On staff side, a message will show saying that page has not been published - Confirm pagination for table shows and works - Confirm deleting pages works as expected (confirm message before delete) - Confirm link title must be unique when adding new pages - Confirm that editing the details of an existing page works - Confirm that adding a child page will only work if the location of child page matches location of parent page, i.e., if parent page is an OPAC page then child page must also be OPAC page. if parent page is in all locations then child page can be in all or either interface(s) - Confirm Select All/Clear All toggle works - Confirm viewing of CMS pages in OPAC and in staff client works and looks nice Sponsored-by: Region Halland Bug 15326: [FOLLOW-UP] Many fixes This patch: - moves action buttons into a dropdown, adds a delete button to dropdown - fixes the tests (they should now all pass) - fixes qa tool problems - removes the Koha::CmsPages->remove() method (unnecessary) - makes changes to the Koha::CmsPages->add() method (now simpler, easier to test) This is now ready to test. Sponsored-by: Catalyst IT Bug 15326: [FOLLOW-UP] More fixes This patch removes the t/Pages.t file because it isn't required, and other unused methods. This patch is ready to test. The appropriate tests are in t/db_dependent/Pages.t Bug 15326: [FOLLOW-UP] adding and editing fixes Sorry, did not see the second half of your comment. This patch should fix the adding and editing issues in Comment 16. Bug 15326: [FOLLOW-UP] Fixing library field in form, opac publishing Fixes issues from Comment 19, please test Signed-off-by: Josef Moravec --- Koha/CmsPage.pm | 2 +- Koha/CmsPages.pm | 124 ++++----- Koha/Schema/Result/Branch.pm | 16 +- Koha/Schema/Result/CmsPage.pm | 150 +++++++++++ .../bug15326-add_cms_pages_permission.sql | 1 + .../intranet-tmpl/prog/en/includes/tools-menu.inc | 71 ++++-- .../prog/en/modules/tools/cmspages.tt | 276 ++++++++++++++------- .../opac-tmpl/bootstrap/en/includes/masthead.inc | 14 -- .../opac-tmpl/bootstrap/en/includes/navigation.inc | 27 +- .../bootstrap/en/modules/opac-cmspages.tt | 56 ++--- koha-tmpl/opac-tmpl/bootstrap/less/opac.less | 40 +-- opac/opac-cmspages.pl | 14 +- opac/opac-main.pl | 14 +- t/Pages.t | 31 --- t/db_dependent/Pages.t | 123 +++++---- tools/cmspages.pl | 59 ++++- 16 files changed, 625 insertions(+), 393 deletions(-) create mode 100644 Koha/Schema/Result/CmsPage.pm create mode 100644 installer/data/mysql/atomicupdate/bug15326-add_cms_pages_permission.sql delete mode 100644 t/Pages.t diff --git a/Koha/CmsPage.pm b/Koha/CmsPage.pm index fd5b52a..cb333d0 100644 --- a/Koha/CmsPage.pm +++ b/Koha/CmsPage.pm @@ -39,7 +39,7 @@ Koha::CmsPage - Koha Page Object class =cut -sub type { +sub _type { return 'CmsPage'; } diff --git a/Koha/CmsPages.pm b/Koha/CmsPages.pm index b49881e..6af9203 100644 --- a/Koha/CmsPages.pm +++ b/Koha/CmsPages.pm @@ -40,7 +40,7 @@ Koha::CmsPage - Koha CMS Page Object Class =cut -sub type { +sub _type { return 'CmsPage'; } @@ -49,27 +49,46 @@ sub object_class { } =head3 add + Adds a new page to the CMS database based on the input data. + =cut sub add { my ( $self, $input ) = @_; -warn "input: ", scalar $input->param('location'); carp( 'Koha::CmsPages::add() undefined parameter: input' ) unless defined $input; + my $data = { - parent => scalar $input->param( 'parent' ) eq '' ? undef : scalar $input->param( 'parent' ), - location => scalar $input->param( 'location' ) eq '' ? undef : scalar $input->param( 'location'), - branchcode => scalar $input->param( 'branchcode' ) eq '' ? undef : scalar $input->param( 'branchcode' ), - lang => scalar $input->param( 'lang' ), - sortorder => scalar $input->param( 'sortorder' ), - title => scalar $input->param( 'title' ), - title_link => scalar $input->param( 'title_link' ), - publish => defined (scalar $input->param( 'publish' )), - content => scalar $input->param( 'content' ) + parent => $input->{parent} eq '' ? undef : $input->{parent}, + location => $input->{location} eq '' ? undef : $input->{location}, + branchcode => $input->{branchcode} eq '' ? undef : $input->{branchcode}, + lang => $input->{lang}, + sortorder => $input->{sortorder}, + title => $input->{title}, + title_link => $input->{title_link}, + publish => $input->{publish} eq 'on' ? 1 : 0, + content => $input->{content} }; - if (defined $input->param( 'id' )) { - $data->{'id'} = scalar $input->param( 'id' ); + if (defined $input->{id}){ + $data->{id} = $input->{id}; + } + + # Ensuring link titles are unique + my $title_link = $input->{title_link}; + if ( Koha::CmsPages->find({ title_link => $title_link }) ){ + my $link = Koha::CmsPages->find({ title_link => $title_link }); + unless ( $link->id eq $input->{id} ){ + return; + } + } + + # Ensuring that, if page has parent, provided location suits parent location + my $parent = Koha::CmsPages->find($input->{parent}); + if ( $parent && $parent->location ne "" ){ + if ( $parent->location ne $input->{location} ){ + return; + } } my $new_page = $self->_resultset()->update_or_create( $data, { key => 'primary' }); @@ -79,22 +98,31 @@ warn "input: ", scalar $input->param('location'); =head3 list -Lists pages based input filtering paramters for location, branch and language. +Lists pages based input filtering parameters for location, branch and language. =cut sub list { my ( $self, $id, $disp, $branch, $lang ) = @_; - my $filter = { }; - $filter->{'location'} = $disp unless not defined $disp; - $filter->{'branch'} = $branch unless not defined $branch; - $filter->{'lang'} = $lang unless not defined $lang; + my %filter; + if ($branch) { + my $branch_push = [ $branch, undef ]; + push @{$filter{'branchcode'}}, $branch_push; + } + if ($lang) { + my $lang_push = [ $lang, undef ]; + push @{$filter{'lang'}}, $lang_push; + } + if ($disp) { + my $disp_push = [ $disp, undef ]; + push @{$filter{'location'}}, $disp_push; + } - my $pages = $self->_resultset()->search( undef, { + my $pages = $self->_resultset()->search( {}, { order_by => { - -asc => [qw/parent location branchcode lang sortorder/] - }, where => $filter + -asc => [qw/parent location branchcode lang sortorder/] + }, where => { %filter } } ); my $page_list = []; @@ -136,60 +164,6 @@ sub list { return ( $page_list, $parent_list, $data ); } -=head3 page - -Retrieves all data to show a single CMS page. - -=cut - -sub page { - my ( $self, $id ) = @_; - - carp( 'Missing CMS page id parameter in Koha::Pages::page' ) unless defined $id; - - # Probably want to merge these to a single query: - my $top_links = $self->_resultset()->search( - { parent => undef }, { qw/id title_link/ }); - - my $page_links = $self->_resultset()->search( - { parent => $id }, { qw/id title_link/ }); - - my $page_data = $self->_resultset()->find( $id ); - - return ( $top_links, $page_links, $page_data ); -} - -=head3 child_links - -Lists pages which are children of a specific id (or undef for root). - -=cut - -sub child_links { - my ( $self, $id ) = @_; - - # 'id' can be undefined if listing top-level links. - my $rs = $self->_resultset()->search({ parent => $id }, - { columns => [qw/id title_link/] } ); - - return $rs; -} - -=head3 remove - -Removes a page identified by id from the CMS database. - -=cut - -sub remove { - my ( $self, @ids ) = @_; - - carp( 'Koha::CmsPages::remove() undefined parameter ids' ) unless @ids; - - my $pages = $self->_resultset()->search({ id => \@ids }); - $pages->delete; -} - =head1 AUTHOR Martin Persson diff --git a/Koha/Schema/Result/Branch.pm b/Koha/Schema/Result/Branch.pm index bb571b1..1a8310a 100644 --- a/Koha/Schema/Result/Branch.pm +++ b/Koha/Schema/Result/Branch.pm @@ -398,6 +398,21 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 cms_pages + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "cms_pages", + "Koha::Schema::Result::CmsPage", + { "foreign.branchcode" => "self.branchcode" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 collections Type: has_many @@ -612,7 +627,6 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-02-16 17:54:53 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:QOMUFz2EjvAVWCkIpNmvtg - # You can replace this text with custom code or comments, and it will be preserved on regeneration sub koha_objects_class { diff --git a/Koha/Schema/Result/CmsPage.pm b/Koha/Schema/Result/CmsPage.pm new file mode 100644 index 0000000..1136c1d --- /dev/null +++ b/Koha/Schema/Result/CmsPage.pm @@ -0,0 +1,150 @@ +use utf8; +package Koha::Schema::Result::CmsPage; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +=head1 NAME + +Koha::Schema::Result::CmsPage + +=cut + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +=head1 TABLE: C + +=cut + +__PACKAGE__->table("cms_pages"); + +=head1 ACCESSORS + +=head2 id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +=head2 parent + + data_type: 'integer' + is_nullable: 1 + +=head2 branchcode + + data_type: 'varchar' + is_foreign_key: 1 + is_nullable: 1 + size: 10 + +=head2 lang + + data_type: 'varchar' + default_value: (empty string) + is_nullable: 0 + size: 25 + +=head2 title + + data_type: 'varchar' + is_nullable: 1 + size: 255 + +=head2 title_link + + data_type: 'varchar' + is_nullable: 1 + size: 64 + +=head2 sortorder + + data_type: 'integer' + default_value: 0 + is_nullable: 0 + +=head2 location + + data_type: 'tinyint' + is_nullable: 1 + +=head2 publish + + data_type: 'tinyint' + default_value: 0 + is_nullable: 0 + +=head2 content + + data_type: 'text' + is_nullable: 1 + +=cut + +__PACKAGE__->add_columns( + "id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "parent", + { data_type => "integer", is_nullable => 1 }, + "branchcode", + { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 }, + "lang", + { data_type => "varchar", default_value => "", is_nullable => 0, size => 25 }, + "title", + { data_type => "varchar", is_nullable => 1, size => 255 }, + "title_link", + { data_type => "varchar", is_nullable => 1, size => 64 }, + "sortorder", + { data_type => "integer", default_value => 0, is_nullable => 0 }, + "location", + { data_type => "tinyint", is_nullable => 1 }, + "publish", + { data_type => "tinyint", default_value => 0, is_nullable => 0 }, + "content", + { data_type => "text", is_nullable => 1 }, +); + +=head1 PRIMARY KEY + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->set_primary_key("id"); + +=head1 RELATIONS + +=head2 branchcode + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "branchcode", + "Koha::Schema::Result::Branch", + { branchcode => "branchcode" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "CASCADE", + }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-12-12 02:41:09 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9044j1OZyypCpGV71FgvYg + + +# You can replace this text with custom code or comments, and it will be preserved on regeneration +1; diff --git a/installer/data/mysql/atomicupdate/bug15326-add_cms_pages_permission.sql b/installer/data/mysql/atomicupdate/bug15326-add_cms_pages_permission.sql new file mode 100644 index 0000000..72ce00f --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug15326-add_cms_pages_permission.sql @@ -0,0 +1 @@ +INSERT IGNORE INTO permissions (`module_bit`, `code`, `description`) VALUES ('13', 'edit_pages', 'Create, edit and delete CMS pages for OPAC and staff interfaces'); diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/tools-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/tools-menu.inc index d3e03a9..80a07b2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/tools-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/tools-menu.inc @@ -1,3 +1,23 @@ + + [% END # / OpacPublic %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/navigation.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/navigation.inc index 2f58c25..e70ef67 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/navigation.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/navigation.inc @@ -1,13 +1,24 @@ [% USE raw %]
-[% IF ( cms_nav_left ) %] - -[% END %] -[% OpacNav | $raw %]
+ [% IF ( links ) %] +

Pages

+
    + [% FOREACH link IN links %] + [% IF ( link.publish == '1' || link.publish == '0' && CAN_user_tools_edit_pages ) %] + [% IF ( link.location == '' || link.location == '2' ) %] + [% IF ( link.parent != '' ) %] + [% FOREACH parent IN parent_list %] + [% IF ( parent.id == link.parent ) %]
  • [% parent.title_link | html %][% link.title_link | html %]
  • [% END %] + [% END %] + [% ELSE %] +
  • [% link.title_link | html %]
  • + [% END %] + [% END %][% END %] + [% END %] +
+ [% END %] + [% OpacNav | $raw %] + [% IF IsPatronPage %]
[% INCLUDE usermenu.inc %]
[% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-cmspages.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-cmspages.tt index 9bc3e35..88811ef 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-cmspages.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-cmspages.tt @@ -1,7 +1,7 @@ [% USE Koha %] [% USE Branches %] [% INCLUDE 'doc-head-open.inc' %] -[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %][% IF (cms_page.title) %] - [% cms_page.title %][% ELSE %] catalog[% END%] +[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online catalog[% END %][% IF (cms_page.title) %] › [% cms_page.title %][% END%] [% INCLUDE 'doc-head-close.inc' %] [% BLOCK cssinclude %][% END %] @@ -10,8 +10,12 @@
@@ -39,48 +43,16 @@ [% ELSE %]
[% END %] - [% cms_page.content %] + + [% IF ( cms_page.publish == 1 ) || ( cms_page.publish == 0 && CAN_user_tools_edit_pages ) %] + [% cms_page.content %] + [% ELSE %] + This page has not been published. + [% END %] + [% IF ( OpacMainUserBlock ) %]
[% OpacMainUserBlock %]
[% END %]
- [% IF ( ( Koha.Preference( 'opacuserlogin' ) == 1 ) || OpacNavRight ) %] -
- [% IF Koha.Preference( 'opacuserlogin' ) == 1 %] - [% UNLESS ( loggedinusername ) %] - [% UNLESS ( casAuthentication || shibbolethAuthentication ) %] -
-
- -
- Log in to your account: - - -
- -
- [% IF PatronSelfRegistration && PatronSelfRegistrationDefaultCategory %]

Don't have an account? Register here.

[% END %] -
- [% IF Koha.Preference( 'NoLoginInstructions' ) %] -
- [% Koha.Preference( 'NoLoginInstructions' ) %] -
- [% END %] -
-
- [% END # /casAuthentication %] - [% IF persona %] - Sign in with your email - [% END # /persona %] - [% END # / loggedinusername %] - [% END # /opacuserlogin %] - [% IF ( OpacNavRight ) %] -
- [% OpacNavRight %] -
- [% END # /OpacNavRight %] -
- [% END # /opacuserlogin || OpacNavRight %] -
diff --git a/koha-tmpl/opac-tmpl/bootstrap/less/opac.less b/koha-tmpl/opac-tmpl/bootstrap/less/opac.less index 2d0982e..4355902 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/less/opac.less +++ b/koha-tmpl/opac-tmpl/bootstrap/less/opac.less @@ -225,37 +225,15 @@ td { } #cms_nav_top { - li { - margin-bottom: -1em; - display: inline; - white-space: nowrap; - border-top-left-radius: 5px; - border-top-right-radius: 5px; - background-color: #eee; - border: 1px solid #D2D2CF; - border-bottom: none; - padding: 0.1em 0.8em 0em 0.8em; - font-weight: bold; - font-size: 1.2em; - border-bottom: 1px solid #D2D2CF; - a { - text-decoration: none; - } - &.cms_nav_top_active { - background-color: #fff; - border-bottom: 1px solid #FFF; - } - } - ul { - margin: 0; - display: inline; - } - display: inline; -} - -ul#cms_nav_left { - list-style-type: none; - padding: 0; + list-style-type: circle; + padding: 0; + margin: 0; + a { + text-decoration: none; + } + a:hover { + text-decoration: underline; + } } #news { diff --git a/opac/opac-cmspages.pl b/opac/opac-cmspages.pl index eaace18..37d5e9f 100755 --- a/opac/opac-cmspages.pl +++ b/opac/opac-cmspages.pl @@ -23,6 +23,7 @@ use C4::Auth; use C4::Output; use C4::Languages qw( getTranslatedLanguages accept_language ); use Koha::CmsPages; +use Data::Dumper; my $input = new CGI; my $id = scalar $input->param( 'id' ) // ''; @@ -42,11 +43,14 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( my ( $theme, $news_lang, $availablethemes ) = C4::Templates::themelanguage( C4::Context->config( 'opachtdocs'), 'opac-cmspages.tt', 'opac', $input ); -my ($cms_nav_top, $cms_nav_left, $cms_data) = Koha::CmsPages->page( $id ); +# List all pages, if id is set generate parent page list and data. +my ( $page_list, $parent_list, $page_data ) = Koha::CmsPages->list( $id ); + +my $links = Koha::CmsPages->search( {}, { order_by => 'sortorder' } ); +my $cms_data = Koha::CmsPages->find( $id ); $template->param( - cms_page => $cms_data, - cms_nav_top => $cms_nav_top, - cms_nav_left => $cms_nav_left, + links => $links, + cms_page => $cms_data, + parent_list => $parent_list, ); - output_html_with_http_headers $input, $cookie, $template->output; diff --git a/opac/opac-main.pl b/opac/opac-main.pl index 9fe7bd6..20833f1 100755 --- a/opac/opac-main.pl +++ b/opac/opac-main.pl @@ -29,7 +29,7 @@ use C4::Members; use C4::Overdues; use Koha::Checkouts; use Koha::Holds; -use Koha::CmsPages qw( list ); +use Koha::CmsPages; my $input = new CGI; my $dbh = C4::Context->dbh; @@ -98,9 +98,17 @@ $template->param( daily_quote => $quote, ); +my $id = scalar $input->param( 'id' ) // ''; +my ( $page_list, $parent_list, $page_data ) = Koha::CmsPages->list( $id ); +my $links = Koha::CmsPages->search( {}, { order_by => 'parent' } ); +my $cms_data = Koha::CmsPages->page( $id ); $template->param( - cms_nav_top => Koha::CmsPages->child_links( undef ), -); + links => $links, + cms_page => $cms_data, + parent_list => $parent_list, + ); + + if (C4::Context->preference('GoogleIndicTransliteration')) { $template->param('GoogleIndicTransliteration' => 1); diff --git a/t/Pages.t b/t/Pages.t deleted file mode 100644 index 5e6d463..0000000 --- a/t/Pages.t +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Carp; -use Test::More tests => 2; -use Data::Dumper::Concise; - -BEGIN { - use_ok('C4::Content'); -} - -my $src = [ - { id => '1', parent => '' }, - { id => '2', parent => '1' }, - { id => '3', parent => '1' }, - { id => '4', parent => '2' }, - { id => '5', parent => '2' }, -]; - -my $expected = [ - { id => '1', children => [ - { id => '2', children => [ - { id => '4', children => [] }, - { id => '5', children => [] } ] - }, - { id => '3', children => [] } ] - }, -]; - -my $output = C4::Content::_SQLToPerl($src); -is_deeply($output, $expected); diff --git a/t/db_dependent/Pages.t b/t/db_dependent/Pages.t index 2c98c38..bad7477 100644 --- a/t/db_dependent/Pages.t +++ b/t/db_dependent/Pages.t @@ -18,75 +18,96 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 10; use Test::Warn; -use Data::Dumper::Concise; use C4::Context; use Koha::Database; use t::lib::TestBuilder; BEGIN { - use_ok('Koha::Objects'); use_ok('Koha::CmsPages'); + use_ok('Koha::CmsPage'); } -my $builder = t::lib::TestBuilder->new(); +my $database = Koha::Database->new(); +my $schema = $database->schema(); +my $dbh = C4::Context->dbh; +my $builder = t::lib::TestBuilder->new(); -# Start transaction -my $dbh = C4::Context->dbh; -$dbh->{ AutoCommit } = 0; -$dbh->{ RaiseError } = 1; -$dbh->do( 'DELETE FROM items' ); +$schema->storage->txn_begin(); +$dbh->do( 'DELETE FROM cms_pages' ); +$dbh->do( 'DELETE FROM issues' ); $dbh->do( 'DELETE FROM borrowers' ); +$dbh->do( 'DELETE FROM items' ); $dbh->do( 'DELETE FROM branches' ); -$dbh->do( 'DELETE FROM cms_pages' ); -my $branchcode = $builder->build({ source => 'Branch' }); - -my $page10 = $builder->build({ - source => 'CmsPage', - value => { - parent => undef, - title_link => 'page 10', - title => 'full page 10', - published => 1, - sortorder => 1, - } -}); -my $page11 = $builder->build({ - source => 'CmsPage', - value => { - parent => $page10->{'id'}, - title_link => 'subpage 11', - title => 'full subpage 11', - published => 1, - sortorder => 0, - } +my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'}; +my $borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'}; + +# new parent page +my $parent_page = Koha::CmsPages->add({ + id => 1, + branchcode => $branchcode, + location => 2, + lang => '', + parent => undef, + title_link => 'page1', + title => 'Page 1', + publish => 0, + sortorder => 1, + content => "This is the first page.", }); -my $page12 = $builder->build({ - source => 'CmsPage', - value => { - parent => $page10->{'id'}, - title_link => 'subpage 12', - title => 'full subpage 12', - published => 1, - sortorder => 1, - } + +is($parent_page->title, 'Page 1', "First page created"); + +# new child page +my $child_page = Koha::CmsPages->add({ + id => 2, + branchcode => $branchcode, + location => 2, + lang => '', + parent => $parent_page->id, + title_link => 'page2', + title => 'Page 2', + publish => 1, + sortorder => 2, + content => "This is the second page.", }); -my $page20 = $builder->build({ - source => 'CmsPage', - value => { - parent => undef, - title_link => 'page 20', - title => 'full page 20', - published => 0, - sortorder => 2, - } + +# $page->parent specifies who this page is the parent of +is(Koha::CmsPages->find($child_page->parent)->title, 'Page 1', 'Child and parent page connected'); + +# Koha::CmsPages->list() with no specified ID should return all pages +my ( $page_list, $parent_list, $data ) = Koha::CmsPages->list( '' ); +is(scalar @{ $page_list }, 2, "Two pages have been created"); + +# Koha::CmsPages->list() is used for filtering (filter display location) +( $page_list, $parent_list, $data ) = Koha::CmsPages->list( '', 2, '', '' ); +is(scalar @{ $page_list }, 2, "Both pages can be seen on staff interface"); + +is($parent_list->[0]->{id}, $parent_page->id, "Correct page is returned as a parent"); +is($data, undef, "Data is undefined because no ID was given"); + +# Koha::CmsPages->list() is used to show data about a page when given an ID +( $page_list, $parent_list, $data ) = Koha::CmsPages->list( 2 ); +is($data->{content}, "This is the second page.", "Data is defined when given an ID"); + +# Try to add a child page under a page where their locations do not match up +my $test_page = Koha::CmsPages->add({ + id => 3, + branchcode => $branchcode, + location => 1, + lang => '', + parent => $child_page->id, + title_link => 'failedadd', + title => 'Failed add', + publish => 0, + sortorder => 3, + content => "This page should not be added", }); -my $result = Koha::CmsPages->child_links( undef ); -is_deeply( $result, [ $page10, $page20 ]); +is($test_page, undef, "Child page cannot be added under a page if their locations do not match"); $dbh->rollback(); diff --git a/tools/cmspages.pl b/tools/cmspages.pl index 314724a..0f7258b 100755 --- a/tools/cmspages.pl +++ b/tools/cmspages.pl @@ -23,8 +23,9 @@ use CGI qw( -utf8 ); use C4::Auth; use C4::Output; use C4::Languages qw( getTranslatedLanguages ); -use Koha::Database; use Koha::CmsPages; +use Koha::Libraries; +use Koha::CmsPage; my $link_self = '/cgi-bin/koha/tools/cmspages.pl'; @@ -50,17 +51,14 @@ $template->param( disp => $disp ); -my $schema = Koha::Database->new->schema; -my @branches = $schema->resultset( 'Branch' )->all; -$template->param( branch_list => @branches ); - my $lang_list = getTranslatedLanguages; +my $branches = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, ); # List all pages, if id is set generate parent page list and data. my ( $page_list, $parent_list, $page_data ) = Koha::CmsPages->list( $id ); $template->param( - page_list_count => scalar $page_list, + branch_list => $branches, page_list => $page_list, parent_list => $parent_list, data => $page_data, @@ -74,17 +72,52 @@ if ( $op eq 'form' ) { id => $id, op => 'add' ); -} - -if ( $op eq 'add' ) { - Koha::CmsPages->add( $cgi ); +} elsif ( $op eq 'add' ) { + my $success = Koha::CmsPages->add({ + id => scalar $cgi->param('id'), + parent => scalar $cgi->param('parent'), + location => scalar $cgi->param('disp'), + branchcode => scalar $cgi->param('branch'), + lang => scalar $cgi->param('lang'), + sortorder => scalar $cgi->param('number'), + title => scalar $cgi->param('title'), + title_link => scalar $cgi->param('title_link'), + publish => scalar $cgi->param('publish'), + content => scalar $cgi->param('content'), + }); + unless ($success) { + $template->param( failed_add => 1 ); + } + if ($success) { + print $cgi->redirect( $link_self ); + } +} elsif ( $op eq 'delSingle' ) { + Koha::CmsPages->find( $id )->delete(); print $cgi->redirect( $link_self ); - } elsif ( $op eq 'del' ) { # param => multi_param; no list-ctx warnings - Koha::CmsPages->remove( $cgi->multi_param( 'ids' ) ); + my @ids = $cgi->multi_param('ids'); + foreach (@ids){ + Koha::CmsPages->find($_)->delete; + } print $cgi->redirect( $link_self ); +} elsif ( $op eq 'view' ) { + my $cms_data = Koha::CmsPages->find( $id ); + my $links = Koha::CmsPages->search( {}, { order_by => 'sortorder' } ); + $template->param( + links => $links, + cms_page => $cms_data, + view => 1, + id => $id, + op => 'view', + ); +} elsif ( $op eq 'filter' ) { + my ( $page_list, $parent_list, $page_data ) = Koha::CmsPages->list( $id, $disp, $branch, $lang ); + $template->param( + page_list => $page_list, + parent_list => $parent_list, + data => $page_data, + ); } - $template->param( link_self => $link_self ); output_html_with_http_headers $cgi, $cookie, $template->output; -- 2.1.4