<package name="xcms"><domain lang="eng">

<domain lang="deu"/>

<translate fields="content.text style.text" style="auto" lang="*"/>

<import src="macro/editform"/>
<import pm="PApp::UserObs"/>
<import src="macro/admin"/>
<import pm="PApp::ECMAScript"/>
<import src="macro/xpcse"/>

<perl><![CDATA[
   use PApp::XSLT;
   use PApp::PCode qw(pcode2perl pxml2pcode);
   use Convert::Scalar ':utf8';
]]></perl>

<callback type="request"><perl><![CDATA[
   # this hack is here to enable us to display
   # the components of specific pages
   if ($PApp::stateid != $xcms_laststate) {
      @xcms_viewed = ();
      $xcms_laststate = $PApp::stateid;
   }
]]></perl></callback>

<macro name="cins" args="$name"><phtml><![CDATA[<:
   sql_fetch \my($mtime, $content, $style),
              "select unix_timestamp(mtime), text, style from content where name = ?",
              $name;
   die "No such page: $name" unless $content;
   push @xcms_viewed, $name;
   $content = capture {
      eval pcode2perl pxml2pcode utf8_on $content;
      die if $@;
   };
   if ($style) {
      sql_fetch \$style,
                 "select text from style where id = ?",
                 $style;
      $style = capture {
         eval pcode2perl pxml2pcode utf8_on $style;
         die if $@;
      };
      $style = new PApp::XSLT stylesheet => "data:,$style";
      $content = $style->apply_string("<content page='$name'>$content</content>");
   }
   :><?$content:><:
:>]]></phtml></macro>

<module name="*"><perl><![CDATA[
   cins $PApp::module || "default";
]]></perl></module>

<module name="edit">
<phtml><![CDATA[
<:access_p "admin" or force_login __"admin access required":>
<html>
<:for $table ("content", "style") {:>

   <?ef_begin "edit_row", -table => $table:>
      <?$table:>
      <?
         ef_relation \$S{edit_id},
                     ["id, name from $table order by 1"],
                     0 => __"[NEW]";
      :>
   <?ef_submit __"Go":>
   <?ef_end:>

<:}:>
</html>
]]></phtml></module>

<module name="edit_row" nosession="edit">
<state keys="edit_table edit_id" local="yes"/>
<phtml><![CDATA[
<html>

<?slink __"[CANCEL/BACK]", "edit":>

<:
   $row = $A{row}
      || new PApp::DataRef 'DB_row',
                           table      => $A{table},
                           where      => [id => $S{edit_id}],
                           autocommit => 0,
                           utf8       => 1;
     
     my $text = new PApp::DataRef 'Scalar', 
        fetch => {: $_[0]{text} :}($row),
        store => {: my ($row, undef, $text) = @_;
                    $row->{text} = $text;
                    $row->{mtime} = undef;
                    $row->flush;
                 :}($row)
 
:>

<?ef_begin -row => $row:>
<?ef_submit __"Save", -save => 1:><br />
<?ef_submit __"Delete", -discard => 1:><br />
<?ef_string \$row->{name}, 60:><br />
#if exists $row->{style}
<?ef_relation \$row->{style},
              ["id, name from style order by 1"],
              0 => __"[NONE]":><br />
#endif
<?ef_end:>
<?client_edit_slink "[Seite bearbeiten (vi)]", $text, "text/tab-separated-values":><br/>
<?client_edit_slink "[Seite ansehen (notepad)]", $text, "text/plain":>

#if $A{save}
<:$row->{mtime} = undef:>
<:$row->flush:>
#endif
#if $A{discard}
<:$row->delete:>
<:abort_to "edit":>
#endif

</html>
]]></phtml></module>

</domain></package>

