<?php // $Id: oldkpindex.php,v 1.3 2002/11/06 21:12:50 nunizgb Exp $ $Name:  $

if (!defined("LOADED_AS_MODULE"))
{
  die ("You can't access this file directly...");
}

// this is only here for the get language call
//$ModName = $GLOBALS['name'];
$ModName = basename( dirname( __FILE__ ) );

modules_get_language();

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

  include ('header.php');

  if (!pnSecAuthAction(0, 'FamilyResource::Section', '::', ACCESS_OVERVIEW))
  {
    echo _SECTIONSNOAUTH;
    include 'footer.php';
    return;
  }

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

  $sitename = pnConfigGetVar('sitename');

  OpenTable();
  echo "<center><font class=\"pn-title\">" . _SECWELCOME . " " . pnVarPrepForDisplay($sitename) . ".</font><br /><br />"
      ."<font class=\"pn-normal\">"._YOUCANFIND."</font></center><br /><br />"
      ."<table border=\"0\" align=\"center\"><tr>";
  $count = 0;
  while(list($secid, $secname, $image) = $result->fields)
  {
    $result->MoveNext();
    if (pnSecAuthAction(0, 'FamilyResource::Section', "$secname::$secid", ACCESS_READ))
    {
      if ( $count == 2 )
      {
        echo "</tr><tr>";
        $count = 0;
      }
      echo "<td><a class=\"pn-normal\" href=\"modules.php?op=modload&amp;name=$GLOBALS[ModName]&amp;file=index&amp;req=listarticles&amp;secid=$secid\">";
      if ( ($image == "transparent.gif") or ($image == "") or ($image== "none") )
      {
        echo $secname;
      }
      else
      {
        echo "<img src=\"images/".strtolower($GLOBALS[ModName])."/$image\" border=\"0\" Alt=\"$secname\">";
      }
      echo "</a></td>";
      $count++;
    }
  }

  $result->Close();
  echo "</tr></table>";
  CloseTable();
  include ('footer.php');
}

function viewarticle()
{
  global $page_title;

// // todo: region: set this
// setRegionIDCookie( 1 );

// $regionid = 0;

// echo "<h1>r0: $regionid</h1>";
// echo "<h1>a0: $artid</h1>";
// echo "<h1>getRegionIDCcookie()0: " . getRegionIDCcookie() . "</h1>";
//  list( $artid, $regionid ) = pnVarCleanFromInput( 'artid', 'regionid' );

  $artid = pnVarCleanFromInput( 'artid' );


// echo "<h1>a: |$artid|</h1>";
// $artid = $_GET['artid'];
// echo "<h1>a: |$artid|</h1>";


  if ( strcmp( $artid, 'coupons' ) == 0 )
  {
    $artid = 1000000;
  }

  $regionid = pnVarCleanFromInput( 'regionid' );
  if ( empty( $regionid ) )
  {
    $regionid = getRegionIDCcookie();
  }

// echo "<h1>r: $regionid</h1>";
// echo "<h1>a: $artid</h1>";
// echo "<h1>getRegionIDCcookie(): " . getRegionIDCcookie() . "</h1>";

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

  $column = &$pntable['familyresourcecontent_column'];
  $result = $dbconn->Execute("UPDATE $pntable[familyresourcecontent]
                              SET $column[counter]=$column[counter]+1
                              WHERE $column[artid]='".pnVarPrepForStore($artid) . "'" . " and $column[regionid]=" . pnVarPrepForStore( $regionid ) );

  $column = &$pntable['familyresourcecontent_column'];
  $result = $dbconn->Execute("SELECT $column[artid], $column[secid], $column[title],
                              $column[content], $column[counter]
                              FROM $pntable[familyresourcecontent]
                              WHERE $column[artid]=" . pnVarPrepForStore($artid) . " and $column[regionid]=" . pnVarPrepForStore( $regionid ) );
  list( $artid, $secid, $title, $content, $counter ) = $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;
  $words = sizeof(explode(" ", $content));

  $page_title = $title;
  include("header.php");

  echo '<!-- Family Resource Banner -->';
//  echo '<div style="width:790px;height:150px;"><img src="/themes/kp/images/titlebar3.gif" width="790" height="150"></div>';
//  echo '<div style="width:610px;height:150px;"><img src="/images/banners/red-pixel.gif" width="610" height="150"></div>';
  echo '<div style="width:610px;height:150px;">' . pnFamilyResourceBannerDisplay( 7, $secid, $artid ) . '</div>';

  echo "<center>";
  OpenTableWithNoBorder();

  if ( !$result || !$result2 )
  {
    CloseTable();
    include 'footer.php';
    return;
  }

  if (!pnSecAuthAction(0, 'FamilyResource::Article', "$title:$secname:$artid", ACCESS_READ))
  {
    // echo _SECTIONSARTICLENOAUTH;
    CloseTable();
    include 'footer.php';
    return;
  }

  echo "<font class=\"header\">".pnVarPrepForDisplay($title)."</font><br />";

  echo $content;

  echo "</td></tr>"
      ."<tr><td align=\"center\">"
      ."&nbsp;&nbsp;<br /><br />";

  CloseTable();
  echo "</center>";

  $result->Close();
  $result2->Close();

  // todo: test right side always on
  global $index;
  $index = 1;

/*
$googleAd = <<<EOT
<div align="center">
<br />

<script type="text/javascript"><!--
google_ad_client = "pub-2513317495040443";
google_alternate_ad_url = "http://kidspages.org/google_adsense_script.html";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
google_ad_channel ="";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</div>

EOT;

echo $googleAd;
*/

  include ('footer.php');
}

function PrintSecPage()
{
  $artid = pnVarCleanFromInput('artid');

  $datetime = &$GLOBALS['datetime'];

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

  $site_logo = pnConfigGetVar('site_logo');

  $column = &$pntable['familyresourcecontent_column'];
  $result=$dbconn->Execute("SELECT $column[title], $column[content] , $column[secid]
                          FROM $pntable[familyresourcecontent]
                          WHERE $column[artid]=".pnVarPrepForStore($artid)."");
  list($title, $content, $secid) = $result->fields;
  $column = &$pntable['familyresourcesections_column'];
  $result2 = $dbconn->Execute("SELECT $column[secname]
                             FROM $pntable[familyresourcesections]
                             WHERE $column[secid]=".pnVarPrepForStore($secid)."");
  list($secname) = $result2->fields;
  if (!pnSecAuthAction(0, 'FamilyResource::Article', "$title:$secname:$artid", ACCESS_READ)) {
      echo _SECTIONSARTICLENOAUTH;
      CloseTable();
      include 'footer.php';
      return;
  }
  $sitename = pnConfigGetVar('sitename');
  $search = array ("'<!--pagebreak-->'si");
  $replace = array ("");
  $content = preg_replace ($search, $replace, $content);

  echo "<html><head><title>$sitename</title>";

  if (defined("_CHARSET") && _CHARSET != "") {
      echo "<META HTTP-EQUIV=\"Content-Type\" "."CONTENT=\"text/html; charset="._CHARSET."\">\n";
  }

  echo "</head>"
      ."<body bgcolor=\"#FFFFFF\" text=\"#000000\">"
      ."<table border=\"0\"><tr><td>"
      ."<table border=\"0\" width=\"640\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#000000\"><tr><td>"
      ."<table border=\"0\" width=\"640\" cellpadding=\"20\" cellspacing=\"1\" bgcolor=\"#FFFFFF\"><tr><td>"
      ."<center>"
      ."<img src=\"images/$site_logo\" border=\"0\" alt=\"\"><br /><br />"
      ."<b>".pnVarPrepForDisplay($title)."</b><br />"
      ."</center>"
      ."".pnVarPrepHTMLDisplay(nl2br($content))."<br /><br />";
  echo "</td></tr></table></td></tr></table>"
      ."<br /><br /><center>"
      ."<font class=\"pn-normal\">"
      ._COMESFROM." ".pnVarPrepForDisplay($sitename)."<br />"
      ."<a class=\"pn-normal\" href=\"" . pnGetBaseURL() . "\">" . pnGetBaseURL() . "</a></font><br /><br />"
      ."</center>"
      ."</td></tr></table>"
      ."</body></html>";
}

if(empty($req))
{
  $req = '';
}

switch($req)
{
  case "viewarticle":
    viewarticle();
    break;
  case "listarticles":
    listarticles();
    break;
  case "printpage":
    PrintSecPage();
    break;
  default:
    listsections();
    break;
}
?>
