1 package SL::Controller::RequirementSpecPredefinedText;
5 use parent qw(SL::Controller::Base);
7 use List::MoreUtils qw(none);
9 use SL::DB::RequirementSpecPredefinedText;
10 use SL::Helper::Flash;
11 use SL::Locale::String;
13 use Rose::Object::MakeMethods::Generic
15 scalar => [ qw(requirement_spec_predefined_text) ],
18 __PACKAGE__->run_before('check_auth');
19 __PACKAGE__->run_before('setup');
20 __PACKAGE__->run_before('load_requirement_spec_predefined_text', only => [ qw(edit update destroy) ]);
29 $self->render('requirement_spec_predefined_text/list',
30 title => t8('Pre-defined Texts'),
31 REQUIREMENT_SPEC_PREDEFINED_TEXTS => SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted);
37 $self->{requirement_spec_predefined_text} = SL::DB::RequirementSpecPredefinedText->new(useable_for_text_blocks => 1);
38 $self->render('requirement_spec_predefined_text/form', title => t8('Create a new predefined text'));
43 $self->render('requirement_spec_predefined_text/form', title => t8('Edit predefined text'));
49 $self->{requirement_spec_predefined_text} = SL::DB::RequirementSpecPredefinedText->new;
50 $self->create_or_update;
55 $self->create_or_update;
61 if (eval { $self->{requirement_spec_predefined_text}->delete; 1; }) {
62 flash_later('info', t8('The predefined text has been deleted.'));
64 flash_later('error', t8('The predefined text is in use and cannot be deleted.'));
67 $self->redirect_to(action => 'list');
73 SL::DB::RequirementSpecPredefinedText->reorder_list(@{ $::form->{requirement_spec_predefined_text_id} || [] });
75 $self->render(\'', { type => 'json' });
83 $::auth->assert('config');
87 $::request->layout->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
94 sub create_or_update {
96 my $is_new = !$self->{requirement_spec_predefined_text}->id;
97 my $params = delete($::form->{requirement_spec_predefined_text}) || { };
98 my $title = $is_new ? t8('Create a new predefined text') : t8('Edit predefined text');
100 # Force presence of booleans for the useable_* flags.
101 my @useable_flags = qw(text_blocks sections);
102 $params->{"useable_for_${_}"} = !!$params->{"useable_for_${_}"} for @useable_flags;
104 # Force usage for text blocks if none of the check boxes are marked.
105 $params->{useable_for_text_blocks} = 1 if none { $params->{"useable_for_${_}"} } @useable_flags;
107 $self->{requirement_spec_predefined_text}->assign_attributes(%{ $params });
109 my @errors = $self->{requirement_spec_predefined_text}->validate;
112 flash('error', @errors);
113 $self->render('requirement_spec_predefined_text/form', title => $title);
117 $self->{requirement_spec_predefined_text}->save;
119 flash_later('info', $is_new ? t8('The predefined text has been created.') : t8('The predefined text has been saved.'));
120 $self->redirect_to(action => 'list');
123 sub load_requirement_spec_predefined_text {
125 $self->{requirement_spec_predefined_text} = SL::DB::RequirementSpecPredefinedText->new(id => $::form->{id})->load;