%#============================================================================
%# ePortal - WEB Based daily organizer
%# Author - S.Rusakov <rusakov_sa@users.sourceforge.net>
%#
%# Copyright (c) 2000-2004 Sergey Rusakov.  All rights reserved.
%# This program is open source software
%#
%#
%# request to /index.htm:
%# - check for $session{DefaultPageView}
%# - if readable then redirect to index.htm?pv=#id#
%#
%# request to /index.htm?pv=default
%# - remove $session{DefaultPageView}
%#
%# request to /index.htm?pv=#id#
%# - if not found then redirect to /index.htm?pv=default
%# - store $session{DefaultPageView}
%#
%#----------------------------------------------------------------------------

%#=== @METAGS onStartRequest ====================================================
<%method onStartRequest><%perl>
  $PV = new ePortal::PageView;

  # ----------------------------------------------------------------------
  # Current PageView Selection algorithm
  # 
  if ( $ARGS{pv} eq '' ) {
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => $session{DefaultPageView}))
      if ( $session{DefaultPageView} > 0 ) and ( $PV->restore( $session{DefaultPageView} ));
  }

  $ARGS{pv} = 'default' if $ARGS{pv} == 0;
  if ( $ARGS{pv} eq 'default' ) {
    delete $session{DefaultPageView};
    $PV->restore_where(where => q{pvtype='default'} );
    $PV->restore_next();

  } else {  # A PageView id is present
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => 'default'))
      if ( ! $PV->restore( $ARGS{pv} ));

    # store $session{DefaultPageView} only for non-default PV
    if ( $PV->pvtype eq 'default' ) {
      delete $session{DefaultPageView};
    } else {
      $session{DefaultPageView} = $ARGS{pv};
    }
  }


  # ----------------------------------------------------------------------
  # Process some HomePage commands
  #
  return if ! $PV->xacl_check_update;

  # dummy objects
  my $us = new ePortal::UserSection;
  my $ps = new ePortal::PageSection;

  # ----------------------------------------------------------------------
  if ( $ARGS{minimize} ) {
    $us->restore_or_throw($ARGS{minimize});
    $us->minimized(1);
    $us->update;
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => $PV->id));

  # ----------------------------------------------------------------------
  } elsif ( $ARGS{maximize} ) {
    $us->restore_or_throw($ARGS{maximize});
    $us->minimized(0);
    $us->update;
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => $PV->id));

  # ----------------------------------------------------------------------
  } elsif ( $ARGS{addsection}) {
    $ARGS{colnum} = 1 if $ARGS{colnum} < 1 or $ARGS{colnum} > $PV->ColumnsCount;
    $ps->restore_or_throw($ARGS{addsection});

    my $S = new ePortal::UserSection;
    $S->pv_id( $PV->id );
    $S->ps_id( $ARGS{addsection} );
    $S->colnum( $ARGS{colnum});
    $S->setupinfo( $ps->setupinfo );
    $S->insert;
    # Set default order_id to new ID for sorting purposes
    $S->order_id( $S->id );
    $S->update;
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => $PV->id));
    
  # ----------------------------------------------------------------------
  } elsif ( $ARGS{remove}) {
    $us->restore_or_throw($ARGS{remove});
    $us->delete;
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => $PV->id));
  
  # ----------------------------------------------------------------------
  } elsif ( $ARGS{moveup}) {
    # This section move up
    $us->restore_or_throw($ARGS{moveup});
    # This section move down
    my $S = new ePortal::UserSection;

    $S->restore_where(where => 'pv_id=? and colnum=? and order_id<?', 
                      order_by => 'order_id desc',
                      bind => [$PV->id, $us->colnum, $us->order_id]);
    if ( $S->restore_next ) { # if a section exists above me
      my $dummy = $S->order_id;
      $S->order_id( $us->order_id );
      $us->order_id( $dummy );
      $S->update;
      $us->update;
    }
    throw ePortal::Exception::Abort(-text => href('/index.htm', pv => $PV->id));
  }

</%perl></%method>



%#=== @METAGS init =========================================================
<%init>
  my @width = $PV->ColumnsWidthPercent;
  my $columns = $PV->ColumnsCount;
  my $section = new ePortal::UserSection;
</%init>
%if ($ePortal::DEBUG) {
  <!-- start of /index.htm =======================================================  -->
%}
<& SELF:PageViewList &>
<& /pv/admin_section.mc &>

<table width="100%" border=0 cellpadding=0 cellspacing=0><tr>
  <%perl>
  for(my $column=1; $column <= $columns; $column++) {
    $section->restore_where(where => 'colnum=? and pv_id=?', order_by => 'order_id', bind => [$column, $PV->id]);
    </%perl>
    <!-- PV column start <% $column %> -->
    <td width="<% $width[$column-1] %>%" valign="top">
    <%perl>
    my $rownum = 1;
    while($section->restore_next) {
      # Some PageSections are not available due to ACL restrictions
      # Check it but don't warn
      next if ! $section->{ps}->check_id;
      $m->comp("SELF:Section", section => $section, rownum => $rownum++);
    }
    $m->print("<td>\n");

    if ( $column != $columns ) {
      </%perl>  
      <& /empty_td.mc, width=>3 &>
      <%perl>
    }
  }
  </%perl>
</tr></table>

<& SELF:SetupSection &>
%if ($ePortal::DEBUG) {
  <!-- end of /index.htm =======================================================  -->
%}
%# ===== END OF INDEX.HTM ====================================================


%#=== @METAGS PageViewList ===================================================
<%method PageViewList><%perl>
  my @PVlist;   # Сюда будем засовывать все доступные юзеру PageView в виде
                # [ID, название] . ИД==0 если это текущий PV
  my $pv = new ePortal::PageView;
  $pv->restore_where( order_by => 'pvtype, title' );
  return if ($pv->rows <= 1); # Nothing to show

</%perl>
<table width="100%" border=0 cellpadding=0 cellspacing=0 bgcolor="#EEEEEE">
 <tr><td class="pvlist">
 &nbsp;&middot;&nbsp;
  <%perl>
  while ($pv->restore_next ) {
    my $suffix;
    if ($ePortal->isAdmin) {
      $suffix = " (U)" if $pv->pvtype eq "user";
      $suffix = " (D)" if $pv->pvtype eq "default";
      $suffix = " (T)" if $pv->pvtype eq "template";
    }

    if ($pv->id == $PV->id) { # this is current PageView
      </%perl>
      &lt;&lt;<b><% $pv->Title |h %><% $suffix %></b>&gt;&gt;&nbsp;&middot
      <%perl>
    } else {
      </%perl>
      <a href="/index.htm?pv=<% $pv->id %>"><% $pv->title |h %><% $suffix %></a>&nbsp;&middot
      <%perl>
    }
  }
</%perl>
</td></tr></table>
</%method>







%#=== @METAGS attr =========================================================
<%attr>
Title => {rus => "ePortal: Домашняя страница", eng => "ePortal: Home page"}
FullPage => 1
</%attr>










%#=== @METAGS Section ====================================================
<%method Section><%perl>
    my $section = $ARGS{section}; # UserSection object
    my $rownum = $ARGS{rownum};
    my ($content, $comp, $setupable);

    # This is a section Mason component file name
    my $component_file = '/pv/sections/' . $section->component;
    if ( ! $m->comp_exists($component_file) ) {
      $content = undef;
      logline('error', "PageView section file $component_file does not exists");
      return;
    } else {
      $comp = $m->fetch_comp( $component_file );
      $setupable = $comp->method_exists('setup_dialog');
      $setupable = 0 if $comp->attr_exists('disable_user_setup_dialog');
      
      # Evaluate the content only if section is not minimized
      if (! $section->minimized) {
        $content = $m->scomp($comp, section => $section );
      }

      # Alwais show the section for user who may edit the PageView
      return if (! $content) and (! $PV->xacl_check_update);
    }  
</%perl>
%if ($ePortal::DEBUG) {
  <!-- start of index.htm:Section, rownum=<% $rownum %>, setupable=<% $setupable %> -->
%}
<&| /dialog.mc, 
        title => $section->title,
        align => '',
        width => '99%',
        title_url => $section->URL,
        objid => $section->id,
        $PV->xacl_check_update 
        ? (
          x_icon => href('/index.htm', pv => $PV->id, remove => $section->id),
          $rownum > 1 
            ? (up_icon => href('/index.htm', pv => $PV->id, moveup => $section->id)) 
            : (),
          $section->minimized 
            ? (max_icon => href('/index.htm', pv => $PV->id, maximize => $section->id)) 
            : (min_icon => href('/index.htm', pv => $PV->id, minimize => $section->id)),
          $setupable
            ? (edit_icon => href("/pv/setup.htm", objid => $section->id))
            : (),
          ) 
        : (),
        &>
% if ( not $section->minimized ) {
  <% $content %>
% }
</&>

<& /empty_table.mc, height => 5 &>
%if ($ePortal::DEBUG) {
  <!-- end of index.htm:Section -->
%}
</%method>






%#=== @metags SetupSection ====================================================
<%method SetupSection><%perl>
  my @width = $PV->ColumnsWidthPercent;
</%perl>
<&| /dialog.mc, 
      title => pick_lang(rus => "Настройка домашней страницы", eng => "Home page setup"),
      title_url => "/pv/pvhelp.htm",
      q_icon => "/pv/pvhelp.htm",
      width => "100%" &>

% if ($PV->xacl_check_update) {
  <span class="smallfont">
  <% pick_lang(
    rus => "Добавление нового раздела на свою домашнюю страницу:",
    eng => "Add new section to personal home page:") %>
  </span>

  <table width="100%" border=0 cellspacing=0 cellpadding=0><tr>

%foreach my $column (1 .. $PV->ColumnsCount) {
% my ($values, $labels) = $PV->AvailableSections($column);

    <form name="pv_column$column" action="<% $ENV{SCRIPT_NAME} %>" method="GET">
    <td width="<% $width[$column-1] %>%" nowrap valign="top" align="center">
    <% CGI::popup_menu( -name => "addsection", -class => 'dlgfield',
        -labels => $labels, -values => $values) %>
    <% CGI::submit( -name => "submit", -value => "+", -title => pick_lang(rus => "Добавить раздел", eng => "Add section"), -class=>'button') %>
    <% CGI::hidden( -name => "colnum", -value => $column) %>
    <% CGI::hidden( -name => "pv", -value => $PV->id) %>
    </td></form>

  <& /empty_td.mc, width=>3, skip_if => $column != $PV->ColumnsCount &>
%}  # end of foreach column

  </tr></table><!-- table with combo boxes -->

  <div align="center">
    <hr width="80%">

    <% plink({rus => "Изменить настройку страницы", eng => "Setup this home page"},
              href=> href("/pv/pv_edit.htm", objid => $PV->id)) %>
    <% plink({rus => "Удалить эту страницу", eng => "Delete this home page"},
              href => href("/delete.htm", objtype => ref($PV), objid => $PV->id)) %>
    <% $ePortal->isAdmin
       ? plink({rus => "Настройка секций", eng => "Sections setup"}, href => "/pv/ps_list.htm")
       : undef %>
  </div>
% } # if ($PV->xacl_check_update)
</&><!-- end of dialog -->

<& /empty_table.mc, height => 10 &>
</%method>




%#=== @metags once =========================================================
<%once>
  my ($PV);
</%once>

%#=== @METAGS cleanup =========================================================
<%cleanup>
 ($PV) = ();
</%cleanup>
