<?php
// Theme-Class For Netjuke
// by Florian Maul <fmaul@users.sourceforge.net>

require_once(FS_PATH."/etc/locale/".LANG_PACK."/inc-lib_inc-theme.php");

class Theme {

  #--------------------------# VARIABLES #--------------------------------#

  var $settings = array();

  var $default_settings = array (

      'background' => array (
              'title'      => THEME_BACKGROUND_COLOR,
              'type'      => 'color',
              'default'   => 'FFFFFF' ),

      'text' => array (
             'title'       => THEME_TEXT_COLOR,
             'type'       => 'color',
             'default'    => '000000' ),

      'link' => array (
              'title'    => THEME_LINK_COLOR,
              'type'    => 'color',
              'default' => '0000FF' ),

      'linkactive' => array (
              'title'    => THEME_ACTIVE_LINK_COLOR,
              'type'    => 'color',
              'default' => '333333' ),

      'linkvisited' => array (
              'title'    => THEME_VISITED_LINK_COLOR,
              'type'    => 'color',
              'default' => '9900CC' ),

      'header' => array (
              'title'   => THEME_HEADER_BACKGROUND_COLOR,
              'type'    => 'color',
              'default' => '9999CC' ),

      'headertext' => array (
              'title'   => THEME_HEADER_FOREGROUND_COLOR,
              'type'    => 'color',
              'default' => 'EEEEEE' ),

      'border' => array (
              'title'    => THEME_TABLE_BORDER_COLOR,
              'type'    => 'color',
              'default' => '666666' ),

      'content' => array (
              'title'   => THEME_CONTENT_COLOR,
              'type'    => 'color',
              'default' => 'EEEEEE' ),

      'font' => array (
              'title'   => THEME_FONT_FACE,
              'type'    => 'text',
              'default' => 'Verdana, Geneva, Arial, Helvetica, sans-serif',
              'size'    => 40
               ),

      'fontsize' => array (
              'title'   => THEME_FONT_SIZE,
              'type'    => 'text',
              'default' => '11',
              'size'    => 4 )
  );

  var $meta_data = array (
          'name'            => 'Theme Name',
          'author'          => 'The Author',
          'version'         => '1.0',
          'description'     => 'A descent description',
          'icon_invert'     => '',       // suggests using (non-)inverted icons with this theme
          'icon_set'        => '',       // suggests an icon-set to be used with this theme
          'icon_header_rev' => 't'       // suggests that icon inversion should be reversed in header
  );

  #--------------------------# FUNCTIONS #--------------------------------#

  #-------------------------------------------------------------
  # Constructor Theme()
  # creates a Theme by reseting the settings
  #-------------------------------------------------------------
  function Theme() {
    $this->resetSettings();
  }

  #-------------------------------------------------------------
  # resetSettings()
  # initialisizes $settings array from defaults
  #-------------------------------------------------------------
  function resetSettings() {
    # use the default values as basis
    foreach ($this->default_settings as $name => $ds) {
      $this->settings[$name] = $ds['default'];
    }
  }

  #-------------------------------------------------------------
  # setDefault($setting, $value)
  # sets a value in the default_settings-array. It can be used by
  # themes which are based on this class to alter the default
  # values easily.
  #-------------------------------------------------------------
  function setDefault($setting, $value) {

    $this->default_settings[$setting]['default'] = $value;
  }

  #-------------------------------------------------------------
  # readUserSettings($themeid, $usermail)
  # determines default and user-settings from the database
  #-------------------------------------------------------------
  function readUserSettings($themeid, $usermail) {
    global $dbconn;

    # define common sql-query
    $sql = "SELECT setting, value FROM netjuke_themeprefs WHERE ".
           "theme = '$themeid' AND ".
           "us_email = '";

    # look in the database for default settings (us_email = '')
    $dbrs = $dbconn->Execute($sql."'");

    while (!$dbrs->EOF) {
      $this->settings[$dbrs->fields[0]] = $dbrs->fields[1];
      $dbrs->MoveNext();
    }

    # look in the database for user-settings
    if (USER_THEMES != 'f' && $usermail != '') {

   	  $dbrs = $dbconn->Execute($sql.$usermail."'");

       while (!$dbrs->EOF) {
         $this->settings[$dbrs->fields[0]] = $dbrs->fields[1];
         $dbrs->MoveNext();
       }
     }

  }

  #-------------------------------------------------------------
  # saveUserSettings($themeid, $usermail )
  # saves the user-settings to the database
  #-------------------------------------------------------------
  function saveUserSettings($themeid, $usermail = '') {
    global $dbconn;

    # delete old settings for this theme by this user
    $dbconn->Execute("DELETE FROM netjuke_themeprefs WHERE "
      ."theme = '$themeid' AND us_email='$usermail'");

    # save all user setting for this theme which differ from default
    foreach ($this->getDefaultSettings() as $id => $option) {
      if ($this->settings[$id] != $option['default']) {
        $sql = "INSERT INTO netjuke_themeprefs "
          ."(us_email, theme, setting, value) VALUES "
          ."('$usermail','$themeid','$id','".$this->settings[$id]."')";
        $dbconn->Execute($sql);
      }
    }
  }

  #-------------------------------------------------------------
  # setMetaData($item, $value)
  # sets the specified meta-data item to $value
  #-------------------------------------------------------------
  function setMetaData($item, $value = '') {

    $this->meta_data[$item] = $value;
  }

  #-------------------------------------------------------------
  # getMetaData($item)
  # returns a specified meta-data item if $item is specified
  # else an array with all the data will be returned
  #-------------------------------------------------------------
  function getMetaData($item = '') {

    if ($item == '') return $this->meta_data;

    else if (isset ($this->meta_data[$item]))

            return $this->meta_data[$item];

         else return '';
  }

  #-------------------------------------------------------------
  # getDefaultSettings()
  # returns the default settings array
  #-------------------------------------------------------------
  function getDefaultSettings() {
    return $this->default_settings;
  }

  #-------------------------------------------------------------
  # getDefaultSettingsCount()
  # returns the amount of settings that are available to
  # the user
  #-------------------------------------------------------------
  function getDefaultSettingsCount() {
    return count($this->default_settings);
  }

  #-------------------------------------------------------------
  # getSettings()
  # returns the current settings array
  #-------------------------------------------------------------
  function getSettings() {
    return $this->settings;
  }

  #-------------------------------------------------------------
  # setSetting($item, $value)
  # sets the setting $item with $value
  #-------------------------------------------------------------
  function setSetting($item, $value = '') {
    $this->settings[$item] = $value;
  }

  #-------------------------------------------------------------
  # getCSS()
  # returns CSS definitions depending on the values of the
  # settings array
  #-------------------------------------------------------------
  function getCSS() {
    ob_start();

  // --- CSS code follows here ------------------------ ?>

    body {
      background:#<?php echo $this->settings['background']?>;
      color:#<?php echo $this->settings['text']?>;
    }

    body,td {
      font-family:<?php echo $this->settings['font']?>;
      font-size:<?php echo $this->settings['fontsize']?>px;
    }

    a {
       text-decoration: none;
    }
    a:link { color: #<?php echo $this->settings['link']?>}
    a:visited { color: #<?php echo $this->settings['linkvisited']?>}
    a:active { color: #<?php echo $this->settings['linkactive']?>}
    a:hover {
       text-decoration: underline;
       color: #<?php echo $this->settings['linkactive']?>
    }

    input, select, textarea  {
       font-family: <?php echo $this->settings['font']?>;
       font-size: <?php echo ($this->settings['fontsize'] - 1)?>px;
    }

    .border {
       background: #<?php echo $this->settings['border']?>;
    }

    .no-border {
       border: none;
    }

    td.header {
       background: #<?php echo $this->settings['header']?>;
       color: #<?php echo $this->settings['headertext']?>;
       font-family: <?php echo $this->settings['font']?>;
       font-size: <?php echo $this->settings['fontsize']?>px;
       font-weight: bold;
    }

    td.headeractive {
       background: #<?php echo $this->settings['content']?>;
       font-family: <?php echo $this->settings['font']?>;
       font-size: <?php echo $this->settings['fontsize']?>px;
    }

    td.line1, td.line2 {
      background:#<?php echo $this->settings['content']?>;
    }

    td.header a         { color: #<?php echo $this->settings['headertext']?>; }
    td.header a:link    { color: #<?php echo $this->settings['headertext']?>; }
    td.header a:active  { color: #<?php echo $this->settings['headertext']?>; }
    td.header a:visited { color: #<?php echo $this->settings['headertext']?>; }
    td.header a:hover   { color: #<?php echo $this->settings['headertext']?>; }

    td.headeractive a         { color: #<?php echo $this->settings['link']?>; }
    td.headeractive a:link    { color: #<?php echo $this->settings['link']?>; }
    td.headeractive a:aktive  { color: #<?php echo $this->settings['link']?>; }
    td.headeractive a:visited { color: #<?php echo $this->settings['link']?>; }
    td.headeractive a:hover   { color: #<?php echo $this->settings['link']?>; }

    .content {
       background: #<?php echo $this->settings['content']?>;
       font-family: <?php echo $this->settings['font']?>;
       font-size: <?php echo $this->settings['fontsize']?>px;
    }

    .bgblend {
      background: #<?php echo $this->settings['background']?>;
      border: none;
    }

    .input_content {
        background-color:#FFFFFF;
        color: #333333;
        border:1px #<?php echo $this->settings['border']?> inset;
        padding-top: 0.1em;
        padding-left: 0.1em;
        padding-right: 0.1em;
    }

    .btn_content {
        background-color: #<?php echo $this->settings['header']?>;
        color: #<?php echo $this->settings['headertext']?>;
        font-weight: bold;
        border:1px #<?php echo $this->settings['border']?> outset;
        padding-top: 0.1em;
        padding-bottom: 0.1em;
        padding-left: 0.2em;
        padding-right: 0.2em;
    }

    .btn_content:hover {
        background-color: #<?php echo $this->settings['content']?>;
        color: #<?php echo $this->settings['text']?>;
        font-weight: bold;
        border:1px #<?php echo $this->settings['border']?> inset;
        padding-top: 0.1em;
        padding-bottom: 0.1em;
        padding-left: 0.2em;
        padding-right: 0.2em;
    }

    .btn_header {
        background-color: #<?php echo $this->settings['content']?>;
        color: #<?php echo $this->settings['text']?>;
        font-weight: bold;
        border:1px #<?php echo $this->settings['border']?> outset;
        padding-top: 0.1em;
        padding-bottom: 0.1em;
        padding-left: 0.2em;
        padding-right: 0.2em;
    }

    .btn_header:hover {
        background-color: #<?php echo $this->settings['header']?>;
        color: #<?php echo $this->settings['headertext']?>;
        font-weight: bold;
        border:1px #<?php echo $this->settings['border']?> inset;
        padding-top: 0.1em;
        padding-bottom: 0.1em;
        padding-left: 0.2em;
        padding-right: 0.2em;
    }

    .error-row {
       background: #FFCCCC;
       font-family: <?php echo $this->settings['font']?>;
       font-size: <?php echo $this->settings['fontsize']?>px;
    }

    input.nomargin, img.icon {
       margin:0px;
       padding:0px;
    }

    div.subheader {
      background-color: #<?php echo $this->settings['header']?>;
       color: #<?php echo $this->settings['headertext']?>;
      text-align: center;
      margin: 5px;
      padding: 1px;
    }

  <?php // ------------------------------------------------

    $css = ob_get_contents();
    ob_end_clean();
    return $css;
  }

  #-------------------------------------------------------------

} // end of class Theme

