Skip to content

Commit

Permalink
Per-account CSS editing for those in the HR group
Browse files Browse the repository at this point in the history
We are using codemirror as an example
  • Loading branch information
Alexx Roche committed Feb 5, 2013
1 parent 7ca99ca commit 07e7467
Show file tree
Hide file tree
Showing 22 changed files with 7,061 additions and 3 deletions.
245 changes: 242 additions & 3 deletions lib/Notice/C/Config.pm
Expand Up @@ -10,6 +10,9 @@ my %submenu = (
'1.0' => [
'1' => { peer => 1, name=> 'Modules', rm => 'modules', class=> 'navigation'},
'2' => { peer => 1, name=> 'Security', rm => 'security', class=> 'navigation'},
'3' => { peer => 0, name=> 'Greeting', rm => 'greet', class=> 'navigation'},
'4' => { peer => 0, name=> 'CSS', rm => 'css', class=> 'navigation'},
'5' => { peer => 0, name=> 'Logo', rm => 'logo', class=> 'navigation'},
],
);

Expand All @@ -20,7 +23,7 @@ Notice::C::Config - Template controller subclass for Notice
=head1 ABSTRACT
This is where Notice is configured from. Only those in the Notice_Admin group
This is where Notice is 0onfigured from. Only those in the Notice_Admin group
should have access here. The user equivelent is "Your Details"
=head1 DESCRIPTION
Expand All @@ -41,6 +44,7 @@ Override or add to configuration supplied by Notice::cgiapp_init.
sub setup {
my ($self) = @_;
$self->authen->protected_runmodes(':all');
$self->tt_params({ submenu => \%submenu });
}

=head2 RUN MODES
Expand Down Expand Up @@ -89,13 +93,248 @@ sub main: StartRunmode {
$self->tt_params({
action => "$surl/aliases",
domains => \@domains,
submenu => \%submenu,
message => $message,
body => $body
});
return $self->tt_process();
}


=head3 css
set the CSS for this account
=cut

sub css: Runmode {
my ($self) = @_;
my ($css,$message,$error,$page);
my $q = $self->query;
my $surl;
$surl = ($self->query->self_url);
my ($ac_id);
if($self->param('ef_acid')){ $ac_id = $self->param('ef_acid'); }
elsif($self->param('ac_id')){ $ac_id = $self->param('ac_id'); }

if( $self->in_group('HR',$self->param('pe_id')) || $self->param('pe_id') == 1){

$self->tt_params({ is_admin => 1 });

my $acc_css = '';
if(defined $self->cfg("www_path")){
my $css_path = $self->cfg("www_path") . "/css";
if(-f "$css_path/${ac_id}.css"){
$acc_css = "${ac_id}.css";
open(CSS, "<$css_path/${ac_id}.css" );
while(my $line = <CSS>){
$css .= $line;
}
close(CSS);
}else{ $css = "/* css for Account: $ac_id */"; }
$self->tt_params({ name => "${ac_id}.css" });
}

my $code;
my $old_css_name;
my $css_name;
if( $q->param('update') && (
$q->param('update') eq "Add" ||
$q->param('update') eq "Save" ||
$q->param('update') eq "Update"
)
){
#use DateTime qw /now/;
#my $now = DateTime->now(); # these three lines work but why bother?
$code = $q->param('code');
$old_css_name = $q->param('id');
$css_name = $q->param('name');

if($css && $code ne $css){
# NTS you are here writing this back to
if(length($css) < length($code)){
$message = "Increaed CSS by " . ( length($code) - length($css) ) . ' characters';
}else{
$message = "CSS reduced by " . ( length($css) - length($code) ) . ' bytes';
}
if(defined $self->cfg("www_path")){
my $css_path = $self->cfg("www_path") . "/css";
unless(-d "$css_path"){
`mkdir -p "$css_path"`;
}
if(-w "$css_path/${ac_id}.css"){
$acc_css = "${ac_id}.css";
open(CSS, ">$css_path/${ac_id}.css" ) or $message = 'Could not write CSS to file';
print CSS $code;
close(CSS);
$css = $code;
}else{
$self->tt_params({ error => 'CSS path does not exist'});
}
}

}else{
warn "no css or code eq css";
}

}

if($acc_css){
$page .= "this account has a default layout called " . $acc_css;
}else{
$page .= "this account FALLS back to the default css of main.css";
$acc_css = 'main.css';
}

#if($self->param('css')){
# $page .= " but THIS user has a personal override of " . $self->param('css');
#}else{
# $page .= ' - you can edit the account\'s CSS here';
#}

# we don't check per-page CSS

$self->tt_params({
action => "$surl/css",
message => $message,
msg => $page,
editor1 => $css,
});
#return $self->tt_process('default.tmpl');
return $self->tt_process();
}else{
return $self->tt_process('default.tmpl', {error => 'You have to be in the HR group to edit the account CSS' });
warn "You " . $self->param('pe_id') . " are not in the HR group";
}
}


=head3 logo
set the logo at the top
=cut

sub logo: Runmode {
my ($self) = @_;
my ($welcome,$message);
my $q = $self->query;
my $surl;
$surl = ($self->query->self_url);
my ($ac_id);
if($self->param('ef_acid')){ $ac_id = $self->param('ef_acid'); }
elsif($self->param('ac_id')){ $ac_id = $self->param('ac_id'); }
$message = 'Here you will be able to upload a small Logo for this account';

if( $self->in_group('HR',$self->param('pe_id')) || $self->param('pe_id') == 1){
$self->tt_params({ is_admin => 1 });

$self->tt_params({
action => "$surl/aliases",
page => $message,
});
return $self->tt_process('default.tmpl');
}
}


=head3 greet
* The message that users get on the front page AFTER logging in
- if they do not have a default homepage
=cut

sub greet: Runmode {
my ($self) = @_;
my ($welcome,$message);
my $q = $self->query;
my $surl;
$surl = ($self->query->self_url);
my ($ac_id);
if($self->param('ef_acid')){ $ac_id = $self->param('ef_acid'); }
elsif($self->param('ac_id')){ $ac_id = $self->param('ac_id'); }

if( $self->in_group('HR',$self->param('pe_id')) || $self->param('pe_id') == 1){
$self->tt_params({ is_admin => 1 });

my $cf_id = 12; # this should be pulled from Conf!

if($q->param('update') || $q->param('add') ){
my $wel_rc = $self->resultset('ConfData')->search({
-and => [
cfd_key => 'welcome',
cfd_acid => "$ac_id"
]})->first;
my $msg_rc = $self->resultset('ConfData')->search({
-and => [
cfd_key => 'message',
cfd_acid => "$ac_id"
]})->first;
#if($q->param('update')){
# warn "we are doing an " . $q->param('update') . ' for acc: ' . $ac_id;
#}elsif( $q->param('add') ){
# warn "we are doing an " . $q->param('add') . ' for account: ' . $ac_id;
# warn " Welcome: " . $q->param('welcome') . ' with a message of length ' . length($q->param('message'));
#}
# NOTE we should only update if it has changed
if($wel_rc){
$wel_rc->update({ cfd_value => $q->param('welcome') })->update;
$message .= " Welcome updated ";
}else{
$self->resultset('ConfData')->create({ cfd_cfid => $cf_id, cfd_acid => $ac_id, cfd_key => 'welcome', cfd_value => $q->param('welcome') })->update;
$message .= " Welcome created ";
}
if($msg_rc){
$msg_rc->update({ cfd_value => $q->param('message') })->update;
$message .= " message updated ";
}else{
$self->resultset('ConfData')->create({ cfd_cfid => $cf_id, cfd_acid => $ac_id, cfd_key => 'message', cfd_value => $q->param('message') })->update;
$message .= " message created ";
}
$self->tt_params({ message => $message });
}
}

if($ac_id){
# NTS need to join the group table so that we only list domains that are not in the
# "no email" domains group
my $conf_rc = $self->resultset('ConfData')->search({
-and => [
cf_name => 'greeting',
cfd_acid => "$ac_id"
]
},{
join => ['config']
});
my $ac = $self->resultset('Account')->search({ ac_id => "$ac_id" })->first;

if($ac && ( $ac->ac_name || $ac->ac_id )){
if($ac->ac_name){
$self->tt_params({ ac_name => $ac->ac_name });
}
$self->tt_params({ ac_id => $ac->ac_id });
}

if($conf_rc){

while(my $cfd = $conf_rc->next){
if($cfd->cfd_key eq 'welcome'){
$welcome = $cfd->cfd_value;
}elsif($cfd->cfd_key eq 'message'){
$message = $cfd->cfd_value;
}
}

$self->tt_params({
main_welcome => $welcome,
main_message => $message
});
}
# if we have an update then we should do that here
}
return $self->tt_process();
}

=head3 ajax_alter_div1
runmodes called via ajax
Expand All @@ -109,7 +348,7 @@ sub ajax_alter_div1 : Runmode {
# foreach my $sp ( $self->param ){ warn "$sp = " . $self->param($sp); } #debug
return $self->redirect($self->query->url . '/' . $self->param('mod'));
}else{
scalar localtime . $self->query->p('Ajax example, (Look Ma, no page reload!)');
scalar localtime() . $self->query->p('Ajax example, (Look Ma, no page reload!)');
}

}
Expand Down
99 changes: 99 additions & 0 deletions t/cgi-bin/templates/Notice/C/Config/css.tmpl
@@ -0,0 +1,99 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>[%- title OR 'Notice::Config::css' -%]</title>
[%- IF is_admin -%]
<link rel=stylesheet href="/css/pages.css" />
[%- UNLESS no_editor -%]
<link rel=stylesheet href="/codemirror/css/codemirror.css" />
<script src="/codemirror/js/codemirror.js">
</script>
<script src="/codemirror/js/css.js"></script>
<link rel="stylesheet" href="/codemirror/doc/docs.css">
<link rel="stylesheet" href="/codemirror/theme/neat.css">
<link rel="stylesheet" href="/codemirror/theme/elegant.css">
<link rel="stylesheet" href="/codemirror/theme/erlang-dark.css">
<link rel="stylesheet" href="/codemirror/theme/night.css">
<link rel="stylesheet" href="/codemirror/theme/monokai.css">
<link rel="stylesheet" href="/codemirror/theme/cobalt.css">
<link rel="stylesheet" href="/codemirror/theme/eclipse.css">
<link rel="stylesheet" href="/codemirror/theme/rubyblue.css">
<link rel="stylesheet" href="/codemirror/theme/lesser-dark.css">
<link rel="stylesheet" href="/codemirror/theme/xq-dark.css">
<link rel="stylesheet" href="/codemirror/theme/ambiance.css">
<link rel="stylesheet" href="/codemirror/theme/blackboard.css">
<link rel="stylesheet" href="/codemirror/theme/vibrant-ink.css">
<link rel="stylesheet" href="/codemirror/theme/solarized.css">
<link rel="stylesheet" href="/codemirror/theme/twilight.css">
<link rel="stylesheet" href="/codemirror/theme/twilight.css">
<style type="text/css">
.CodeMirror { background: #f8f8f8; border: 1px solid black; font-size:13px; min-width:100%; min-height: 497px;}
</style>
<script type="text/javascript" language="javascript">
window.onload=function() { selectTheme(); };
//document.addEventListener('DOMContentLoaded',function(){ slectTheme(); })
//$(document).ready(function() { selectTheme(); });
</script>
[%- END # no_editor -%]
[%- END # is_admin -%]
</head>
<body>
[%- warning || warn || error -%]
[%- message -%]
[%- USE Dumper -%]
[%- IF is_admin -%]
<form method="post">
<input type="hidden" name="id" value="[%- name -%]" />
<span class="small">Name: <input type="text" name="name" title="It has to have a name" value="[%- name -%]" required [% IF name; 'readonly'; END %] />
&nbsp; <a tabindex=99 class="red button" href="[%- c.query.url -%]/Config/"><span class="white">Abandon or return</span></a> &nbsp;
[%- END #IF is_admin -%]
[%- msg -%]
[%- IF is_admin -%]
<br /><br />
</span>
<textarea id="code" name="code" rows="30" cols="152" >[%- editor1 -%]</textarea>
<br />
<input class="small [%- IF editor1; 'orange'; ELSE; 'green'; END -%] button" type="submit" title="Go live right now" value="[%- IF editor1; 'Update'; ELSE; 'Add'; END -%]" name="update" />

&nbsp; &nbsp; &nbsp;
Select a theme: <select onchange="selectTheme()" id=select>
<option>default</option>
<option>ambiance</option>
<option>blackboard</option>
<option>cobalt</option>
<option>eclipse</option>
<option>elegant</option>
<option>erlang-dark</option>
<option>lesser-dark</option>
<option>monokai</option>
<option>neat</option>
<option>night</option>
<option selected>rubyblue</option>
<option>solarized dark</option>
<option>solarized light</option>
<option>twilight</option>
<option>vibrant-ink</option>
<option>xq-dark</option>
</select>

<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true
});
var input = document.getElementById("select");
function selectTheme() {
var theme = input.options[input.selectedIndex].innerHTML;
editor.setOption("theme", theme);
}
var choice = document.location.search &&
decodeURIComponent(document.location.search.slice(1));
if (choice) {
input.value = choice;
editor.setOption("theme", choice);
}
</script>
</form>
[%- END # admin check -%]
</body>
</html>

0 comments on commit 07e7467

Please sign in to comment.