<?php

if(!eregi("admin.php", $PHP_SELF))
{
  die ("Access Denied");
}

modules_get_language();
modules_get_manual();

/**
 * Family Resource Manager Functions
 */

function sections()
{
  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $currentlang = pnUserGetLang();

  $admart = pnConfigGetVar('admart');

  include 'header.php';
  GraphicAdmin();
  OpenTable();
  echo "<center><font class=\"pn-title\"><b>" . _SECTIONSADMIN . "</b></font></center>";
  CloseTable();

  $column = &$pntable['familyresourcesections_column'];
  $result = $dbconn->Execute( "SELECT $column[secid], $column[secname]
                               FROM $pntable[familyresourcesections] ORDER BY $column[secname]" );

  if (!$result->EOF)
  {
    // Current sections
    if ( pnSecAuthAction( 0, 'FamilyResource::Section', ':', ACCESS_EDIT ) )
    {
      OpenTable();
      echo "<center><b>"._ACTIVESECTIONS."</b><br><font class=\"pn-normal\">"._CLICK2EDITSEC."</font></center><br>"
          ."<table border=0 width=100% align=center cellpadding=1 align=\"center\"><tr><td align=left>";

      while(list($secid, $secname) = $result->fields)
      {
        if (pnSecAuthAction(0, 'FamilyResource::Section', "$secname:$secid", ACCESS_EDIT))
        {
          echo "<strong><big>&middot;</big></strong>&nbsp;&nbsp;<a href=\"admin.php?module=$GLOBALS[ModName]&op=sectionedit&amp;secid=$secid\">".pnVarPrepForDisplay($secname)."</a><br />";
        }
        $result->MoveNext();
      }
      echo "</td></tr></table>";
      CloseTable();
    }

    // Add article
    if ( pnSecAuthAction( 0, 'FamilyResource::Article', ":$secname:", ACCESS_ADD ) )
    {
      OpenTable();
      echo "<center><font class=\"pn-title\"><b>"._ADDSECARTICLE."</b></font></center><br>"
          ."<form action=\"admin.php\" method=\"post\">"
          ."<b>"._TITLE."</b><br>"
          ."<input type=\"text\" name=\"title\" size=\"60\"><br><br>"
          ."<b>"._SELSECTION.":</b><br>";
      $column = &$pntable['familyresourcesections_column'];
      $result = $dbconn->Execute("SELECT $column[secid], $column[secname]
                                  FROM $pntable[familyresourcesections] ORDER BY $column[secname]");

      while ( list($secid, $secname ) = $result->fields )
      {
        if (pnSecAuthAction(0, 'FamilyResource::Section', "$secname::$secid", ACCESS_ADD))
        {
          echo "<input type=\"radio\" name=\"secid\" value=\"$secid\"> ".pnVarPrepForDisplay($secname)."<br>";
        }
        $result->MoveNext();
      }
      echo "<br><font class=\"pn-normal\">"._DONTSELECT."</font><br>";
      echo "<br><br><b>"._LANGUAGE.": </b>"
          ."<select name=\"slanguage\" class=\"pn-text\">";

      $lang = languagelist();
      $sel_lang[$currentlang] = ' selected';
      print '<option value="">'._ALL.'</option>';
      $handle = opendir('language');
      while ($f = readdir($handle))
      {
        if (is_dir("language/$f") && (!empty($lang[$f])))
        {
          $langlist[$f] = $lang[$f];
        }
      }
      asort($langlist);

      // a bit ugly, but it works in E_ALL conditions (Andy Varganov)
      foreach ($langlist as $k=>$v)
      {
        echo '<option value="'.$k.'"';
        if (isset($sel_lang[$k])) echo ' selected';
        echo '>'. $v . '</option>\n';
      }

      echo '</select>';
      echo "<br><br><b>"._CONTENT."</b><br>"
          ."<textarea name=\"content\" cols=\"70\" rows=\"20\"></textarea><br>"
          ."<font class=\"pn-normal\">"._PAGEBREAK."</font><br><br>"
          ."<input type=\"hidden\" name=\"op\" value=\"secarticleadd\">"
          ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
          ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
          ."<input type=\"submit\" value=\""._ADDARTICLE."\">"
          ."</form>";
      CloseTable();
    }

    // Show current articles
    if ( pnSecAuthAction(0, 'FamilyResource::Article', '::', ACCESS_EDIT ) )
    {
      $admart = 10000;
      OpenTable();
      echo "<center><font class=\"pn-title\"><b>"._LAST." ".pnVarPrepForDisplay($admart)." "._ARTICLES."</b></font></center><br>"
          ."<ul>";       /* ML added slanguage for display */
      $column = &$pntable['familyresourcecontent_column'];
      $query = buildSimpleQuery ('familyresourcecontent', array ('artid', 'regionid', 'secid', 'title', 'content', 'slanguage' ), '', "$column[title],$column[regionid] ASC", $admart);

      $result = $dbconn->Execute($query);

      while(list($artid, $regionid, $secid, $title, $content, $slanguage) = $result->fields)
      {
        $column = &$pntable['familyresourcesections_column'];
        $result2 = $dbconn->Execute("SELECT $column[secid], $column[secname]
                                     FROM $pntable[familyresourcesections]
                                     WHERE $column[secid]='".pnVarPrepForStore($secid)."'");

        list($secid, $secname) = $result2->fields;
        if (pnSecAuthAction(0, 'FamilyResource::Article', "$title:$secname:$artid", ACCESS_EDIT))
        {
          echo "<li>$title - ($slanguage) - ($secname) [ <a href=\"admin.php?module=$GLOBALS[ModName]&op=secartedit&artid=$artid&regionid=$regionid&authid=".pnSecGenAuthKey()."\">"._EDIT."</a> ";
          if (pnSecAuthAction(0, 'FamilyResource::Article', "$title:$secname:$artid", ACCESS_DELETE))
          {
            echo "| <a href=\"admin.php?module=$GLOBALS[ModName]&op=secartdelete&artid=$artid&ok=0\">"._DELETE."</a> ";
          }
          echo "]";
        }
        $result->MoveNext();
      }
      echo "<br>";
      echo "</ul>"
          ."<form action=\"admin.php\" method=\"post\">"
          .""._EDITARTID.": <input type=\"text\" name=\"artid\" size=\"10\">&nbsp;&nbsp;"
          ."<input type=\"hidden\" name=\"op\" value=\"secartedit\">"
          ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
          ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
          ."<input type=\"submit\" value=\""._OK."\">"
          ."</form>";
      CloseTable();
    }
  }

  // Add section
  if (pnSecAuthAction(0, 'FamilyResource::Section', "::", ACCESS_ADD))
  {
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._ADDSECTION."</b></font></center><br>"
        ."<form action=\"admin.php\" method=\"post\"><br>"
        ."<b>"._SECTIONNAME.":</b><br>"
        ."<input type=\"text\" name=\"secname\" size=\"40\" maxlength=\"40\"><br><br>"
        ."<b>"._SECTIONIMG."</b><br><font class=\"pn-sub\">"._SECIMGEXAMPLE."</font><br>"
        ."<input type=\"text\" name=\"image\" size=\"40\" maxlength=\"50\"><br><br>"
        ."<input type=\"hidden\" name=\"op\" value=\"sectionmake\">"
        ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
        ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
        ."<INPUT type=\"submit\" value=\""._ADDSECTIONBUT."\">"
        ."</form>";
    CloseTable();
  }
  include 'footer.php';
}

function secartedit()
{
  $artid = pnVarCleanFromInput('artid');
  $regionid = pnVarCleanFromInput('regionid');

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $currentlang = pnUserGetLang();

  include 'header.php';

  GraphicAdmin();

  OpenTable();
  $che = "";
  echo "<center><font class=\"pn-title\"><b>"._SECTIONSADMIN."</b></font></center>";
  CloseTable();

  $column = &$pntable['familyresourcecontent_column'];
  $result = $dbconn->Execute("SELECT $column[artid], $column[regionid], $column[secid], $column[title], $column[content], $column[slanguage]
                              FROM $pntable[familyresourcecontent]
                              WHERE $column[artid]='" . pnVarPrepForStore($artid) . "' AND $column[regionid]='" . pnVarPrepForStore($regionid) . "'");

  if ( $result->EOF )
  {
    echo _SECTIONSEDITARTICLENOTEXIST;
    echo '<br /><br />'._GOBACK;
    include 'footer.php';
    return;
  }
  list($artid, $regionid, $secid, $title, $content, $slanguage) = $result->fields;

  $column = &$pntable['familyresourcesections_column'];
  $result = $dbconn->Execute( "SELECT $column[secname]
                               FROM $pntable[familyresourcesections]
                               WHERE $column[secid]=".pnVarPrepForStore($secid)."");

  list($secname) = $result->fields;
  if (!pnSecAuthAction(0, 'FamilyResource::Article', "$title:$secname:$artid", ACCESS_EDIT))
  {
    echo _SECTIONSEDITARTICLENOAUTH;
    include 'footer.php';
    return;
  }

  OpenTable();
  echo "<center><font class=\"pn-title\"><b>"._EDITARTICLE."</b></font></center><br>"
       ."<form action=\"admin.php\" method=\"post\">"
       ."<b>"._TITLE."</b><br>"
       ."<input type=\"text\" name=\"title\" size=\"60\" value=\"$title\"><br><br>"
       ."<b>"._SELSECTION.":</b><br>";
  $column = &$pntable['familyresourcesections_column'];
  $result2 = $dbconn->Execute("SELECT $column[secid], $column[secname]
                               FROM $pntable[familyresourcesections] ORDER BY $column[secname]");

  while( list( $secid2, $secname) = $result2->fields )
  {
    if ($secid2==$secid)
    {
      $che = "checked";
    }
    echo "<input type=\"radio\" name=\"secid\" value=\"$secid2\" $che>".pnVarPrepForDisplay($secname)."<br>";
    $che = "";
    $result2->MoveNext();
  }
  echo "<br><br><b>"._LANGUAGE.": </b>" /* ML added dropdown , currentlang is pre-selected */
              ."<select name=\"slanguage\" class=\"pn-text\">";
  $lang = languagelist();
  $sel_lang[$slanguage] = ' selected';

  echo '<option value="">'._ALL.'</option>';
  $handle = opendir('language');
  while ($f = readdir($handle))
  {
    if (is_dir("language/$f") && (!empty($lang[$f])))
    {
      $langlist[$f] = $lang[$f];
    }
  }
  asort($langlist);

  // a bit ugly, but it works in E_ALL conditions (Andy Varganov)
  foreach ($langlist as $k=>$v)
  {
    echo '<option value="'.$k.'"';
    //if  (!isset($sel_lang[$k]) || !is_numeric($sel_lang[$k])) echo ' selected';
    if (isset($sel_lang[$k])) echo ' selected';
    echo '>'. $v . '</option>\n';
  }

  echo '</select>';
  echo "<br><br><b>"._CONTENT."</b><br>"
  // keep the entities in the content
  // ."<textarea name=\"content\" cols=\"180\" rows=\"80\">".pnVarPrepHTMLDisplay($content)."</textarea><br><br>"
  ."<textarea name=\"content\" cols=\"180\" rows=\"80\">".htmlspecialchars($content)."</textarea><br><br>"

  ."<input type=\"hidden\" name=\"artid\" value=\"$artid\">"
  ."<input type=\"hidden\" name=\"regionid\" value=\"$regionid\">"
  ."<input type=\"hidden\" name=\"op\" value=\"secartchange\">"
  ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
  ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
  ."<input type=\"submit\" value=\""._SAVECHANGES."\">";

  if (pnSecAuthAction(0, 'FamilyResource::Article', "$secname:$secid:$artid", ACCESS_DELETE))
  {
    echo " [ <a href=\"admin.php?module=$GLOBALS[ModName]&op=secartdelete&amp;artid=$artid&amp;ok=0\">"._DELETE."</a> ]";
  }
  echo "</form>";

  CloseTable();
  include 'footer.php';
}


function secartchange()
{
  list($artid,
       $regionid,
       $secid,
       $title,
       $content,
       $slanguage) = pnVarCleanFromInput('artid',
                                         'regionid',
                                         'secid',
                                         'title',
                                         'content',
                                         'slanguage');
  if (!pnSecConfirmAuthKey()) 
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }
  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();


  $column = &$pntable['familyresourcesections_column'];
  $contcolumn = &$pntable['familyresourcecontent_column'];

  $result = $dbconn->Execute("SELECT $column[secname], $contcolumn[title]
                              FROM $pntable[familyresourcesections], $pntable[familyresourcecontent]
                              WHERE $contcolumn[artid]=".pnVarPrepForStore($artid)."
                                AND $contcolumn[regionid]=".pnVarPrepForStore($regionid)."
                                AND $column[secid] = ".pnVarPrepForStore($secid)."");

  list($secname, $orig_title) = $result->fields;
  $result->Close();

  if (!pnSecAuthAction(0, 'Sections::Article', "$title:$secname:$artid", ACCESS_EDIT)) 
  {
    include 'header.php';
    echo _SECTIONSEDITARTICLENOAUTH;
    include 'footer.php';
    return;
  }

  $column = &$pntable['familyresourcecontent_column'];
  $dbconn->Execute("UPDATE $pntable[familyresourcecontent]
                    SET $column[secid]='".pnVarPrepForStore($secid)."', 
                        $column[title]='".pnVarPrepForStore($title)."',
                        $column[content]='".pnVarPrepForStore($content)."',
                        $column[slanguage]='".pnVarPrepForStore($slanguage)."'
                    WHERE $column[artid]=" . pnVarPrepForStore($artid) . "
                      AND $column[regionid]=" . pnVarPrepForStore($regionid) . "
                      AND $column[regionid]=" . pnVarPrepForStore($regionid) );

  pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=sections');
}

function FamilyResource_admin_main($var)
{
  $op = pnVarCleanFromInput('op');
  extract($var);

  if ( ( !pnSecAuthAction( 0, 'FamilyResource::Section', '::', ACCESS_EDIT ) ) &&
       ( !pnSecAuthAction( 0, 'FamilyResource::Article', '::', ACCESS_EDIT ) ) )
  {
    include 'header.php';
    echo _SECTIONSNOAUTH;
    include 'footer.php';
  }
  else
  {
    switch ( $op )
    {
      case "sections":
        sections();
        break;

      case "sectionedit":
        sectionedit();
        break;

      case "sectionmake":
        sectionmake();
        break;

      case "sectiondelete":
        sectiondelete();
        break;

      case "sectionchange":
        sectionchange();
        break;

      case "secarticleadd":
        secarticleadd();
        break;

      case "secartedit":
        secartedit();
        break;

      case "secartchange":
        secartchange();
        break;

      case "secartdelete":
        secartdelete();
        break;

      default:
        sections();
        break;
    }
  }
}
?>