<domain name="papp" lang="en">

<description>
   access rights editor
   requires "admin" priviledges
</description>

<import src="macro/util"/>
<import src="macro/editform"/>

<import pm="PApp::UserObs"/>

<module name="">
<state keys="usearch gsearch" import="yes"/>
<state keys="ac_users ac_groups" preferences="yes"/>
<phtml><![CDATA[
#if $S{ac_users}
   <h2><?slink "&lt;&lt;&lt; ".__"Users", ac_users => 0, SURL_SAVE_PREFS:></h2>

   <:my ($cnt) = sql_fetch "select count(distinct uid) from prefs":>
   <?sprintf __"There are currently %d users in the database.", $cnt:>
   <ul>
   <:$S{usearch} = "_" unless exists $S{usearch}:>
   <li><?sform:>__"Search username/comment:" <?textfield "usearch", $S{usearch}:><?endform:>
   </ul>

   __"To see all users, enter nothing in the 'Search' entry"<br>

   <:
      my $st = sql_exec \my($id),
                        "select distinct uid
                         from prefs
                         where path = ''
                               and (name = 'papp_username' or name = 'papp_comment')
                               and value like ?
                         order by 1
                         limit 20",
                         "%$S{usearch}%";
   :>
#if $A{delete_user}
   <:user_delete $A{delete_user}:>
#endif
   __"Only the first 20 matches shown currently."<br />
   <table rules="all" frame="box" border="1">
   <tr><th>__"ID"</th><th>__"login"</th>
       <th>__"comment"</th><th>&nbsp;</th><th>__"access rights"</th>
   <: while ($st->fetch) {
         $comment ||= "&nbsp;"
         :>
            <tr><td><?$id:></td><td><?escape_html username $id:></td>
                <td><?escape_html substr $PApp::prefs->user_get($id, "papp_comment"), 0, 30:></td>
                <td>
                   <?slink __"[EDIT]", "edit_user", userid => $id:>
                   <?slink __"[DELETE]", -delete_user => $id:>
                </td>
                <td><?join ", ", sql_fetchall "select g.name
                                       from usergrp u, grp g
                                       where u.grpid = g.id and userid = ?", $id:></td>
            </tr><:
      }
   :>
   </table>
#else
   <h2><?slink __("Users")." &gt;&gt;&gt;", ac_users => 1, SURL_SAVE_PREFS:></h2>
#endif

#if $S{ac_groups}
   <h2><?slink "&lt;&lt;&lt; ".__"Groups", ac_groups => 0, SURL_SAVE_PREFS:></h2>

   <:my ($cnt) = sql_fetch "select count(*) from grp":>
   <?sprintf __"There are currently %d groups in the database.", $cnt:>
   <ul>
   <:$S{gsearch} = ""  unless defined $S{gsearch}:>
   <li><?sform:>__"Search groupname/comment:" <?textfield "gsearch", $S{gsearch}:><?endform:>
   <li><?slink __"[CREATE NEW GROUP]", "edit_grp", grpid => undef:>
   </ul>

#if $A{delete_grp}
   <:sql_exec "delete from grp where id = ?", $A{delete_grp}:>
#endif

   <:
      my $st = sql_exec \my($id, $user, $comment),
                        "select id, name, comment
                         from grp
                         where name like ? or comment like ?",
                         ("%$S{gsearch}%")x2;
   :>

   <table rules="all" frame="box" border="1">
   <tr><th>__"ID"</th><th>__"name"</th><th>__"comment"</th><th>&nbsp;</th>
   <:
      while ($st->fetch) {
         $comment ||= "&nbsp;"
         :>
            <tr><td><?$id:><td><?$user:><td><?substr $comment, 0, 30:></td>
                <td><?slink __"[EDIT]", "edit_grp", grpid => $id:>
                    <?slink __"[DROP]", -delete_grp => $id:></td>
         <:
      }
   :>
   </table>
#else
   <h2><?slink __("Groups")." &gt;&gt;&gt;", ac_groups => 1, SURL_SAVE_PREFS:></h2>
#endif
]]></phtml>
</module>

<module name="edit_user">
<state keys="userid" local="yes"/>
<phtml><![CDATA[
   <?slink __"Go Back", "":><p>

   <: BEGIN {
         use Data::Dumper;
         use Storable;
         use Compress::LZF;

         $fetch_prefs = create_callback {
            # this vv is just a hack to avoid an unexplainable segfault(!!)
            $_[1] eq ""
               ? "# uninitialized\n\n"
               : eval {
                     "# currently read-only\n\n".
                     Data::Dumper->new([Storable::mretrieve decompress $_[1]])
                        ->Indent(1)
                        ->Purity(1)
                        ->Useqq(1)
                        ->Terse(1)
                        ->Dump;
                 } || "# deserialization error ($@)"
               ;
         };
         $store_prefs = create_callback {
            ();
         };
   } :>

#if $A{delete_grp}
   <:sql_exec "delete from usergrp where userid = ? and grpid = ?", $S{userid}, $A{delete_grp}:>
#endif
#if $A{add_grp}
   <:sql_exec "replace into usergrp values (?, ?)", $S{userid}, $P{grpid}:>
#endif
   <?ef_begin:>
   <table>
   <:my $username = $PApp::prefs->user_ref($S{userid}, "papp_username"):>
   <tr><td>__"User"</td><td><?ef_string $username, 20:>
   <:my $password = $PApp::prefs->user_ref($S{userid}, "papp_password"):>
   <:$password = new PApp::DataRef 'Scalar', ref => $password, &PApp::DataRef::DB_row::filter_password:>
   <tr><td>__"Password"</td><td><?ef_password $password, 14:>
                                __"(leave this field empty for no change)"
   <:my $comment = $PApp::prefs->user_ref($S{userid}, "papp_comment"):>
   <tr><td>__"Comment"</td><td><?ef_text $comment, 60, 8:>
#if 0
   <tr><td>__"Preferences"</td><td><?ef_text \$row->{["prefs", fetch => $fetch_prefs, store => $store_prefs]}, 60, 14:>
#endif
   <:
      my $st = sql_exec \my($id, $name),
                        "select g.id, g.name from usergrp u, grp g
                         where u.grpid = g.id and userid = ?
                         order by 2",
                        $S{userid};
      while ($st->fetch) {
         :>
            <tr><td>__"User Group:"</td><td><?$name:> <?slink __"[DELETE]", -delete_grp => $id:>
         <:
      }
   :>
   <tr><td><td><?ef_submit:><?ef_reset:>
   </table>
   <?ef_end:>
   <h3>__"Add Group:"</h3>
      <?sform -add_grp => 1:>
      <select name="grpid">
      <:
         my $st = sql_exec \my($id, $name),
                           "select id, name from grp order by 2";
         while ($st->fetch) {
            :><option value="<?$id:>"><?$name:></option><:
         }
      :>
      </select>
      <?submit "add", "add":>
      <?endform:>
]]></phtml></module>

<module name="edit_grp">
<state keys="grpid" local="yes"/>
<phtml><![CDATA[
   <?slink __"Go Back", "":><p>

#if !defined $S{grpid}
   <:my $st = sql_exec "replace into grp (name) values ('<new grp>')":>
   <:$S{grpid} = sql_insertid $st:>
#endif

   <:my $row = new PApp::DataRef 'DB_row', table => "grp", where => [id => $S{grpid}], preload => 1, delay => 1:>
   <?ef_begin:>
   <table>
   <tr><td>__"Name"</td><td><?ef_string \$row->{name}, 20:>
   <tr><td>__"Comment"</td><td><?ef_text \$row->{comment}, 60, 8:>
   <tr><td><td><?ef_submit:><?ef_reset:>
   </table>
   <?ef_end:>
]]></phtml></module>

</domain>

