<?php

function  _phptemplate_variables($hook, $vars) {
  switch($hook) {
    case 'page':
      $vars['visszaszam'] = max(0, date('z', strtotime('2008-10-30')) - date('z'));
      break;
    case 'comment':
      static $count;
      $count = is_array($count) ? $count : array();
      $count[$hook] = is_int($count[$hook]) ? $count[$hook] : 1;
      $vars['zebra'] = ($count[$hook] % 2) ?'line1' : 'line2';//'odd' : 'even';
      $vars['seqid'] = $count[$hook]++;
      $vars['permalink'] = l(rich_format_date($vars['comment']->timestamp),
          $_GET['q'],
          array(
                'class' => 'permalink',
                'title' => 'Permalink a hozzászólásra. Beküldés ideje: ' .
                           format_date($vars['comment']->timestamp),
                #'name'  => 'comment-' . $comment->cid
                ),
          NULL, //query
          'comment-' . $vars['comment']->cid, //fragment
          TRUE /*abs*/);
      break;
    case 'node':
      if ($GLOBALS['user']->uid == $vars['node']->uid) {
        //$statistics = statistics_get($vars['node']->nid);
        if ($statistics) {
          $vars['node']->links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 read', '@count reads');
        }
      }
      break;
  }
  return $vars;
}

function phptemplate_forum_list($forums, $parents, $tid) {
  global $user;

  if ($forums) {

    $header = array(t('Forum'), t('Topics'), '<span title="Hozzászólások">Hsz.</span>', t('Last post'));

    foreach ($forums as $forum) {
      if ($forum->container) {
        $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
        $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";

        if ($forum->description) {
          $description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
        }
        $description .= "</div>\n";

        $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));
      }
      else {
        $new_topics = _forum_topics_unread($forum->tid, $user->uid);
        $forum->old_topics = $forum->num_topics - $new_topics;
        if (!$user->uid) {
          $new_topics = 0;
        }

        $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
        $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";

        if ($forum->description) {
          $description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
        }
        $description .= "</div>\n";

        $rows[] = array(
          array('data' => $description, 'class' => 'forum'),
          array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(format_plural($new_topics, '1 new', '@count new'), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'),
          array('data' => $forum->num_posts, 'class' => 'posts'),
          array('data' => _forum_format($forum->last_post), 'class' => 'last-reply'));
      }
    }

    return theme('table', $header, $rows);

  }

}

function rich_format_date ($t) {
  $c = time();
  define('egynap', 3600 * 24);
  setlocale(LC_TIME, 'hu_HU.UTF-8');

  if (($t - ($t % egynap)) == ($c - ($c % egynap))) { //ma
    return 'ma, ' . strftime('%H.%M', $t);
  }
  if (($t - ($t % egynap) + egynap) == ($c - ($c % egynap))) { //tegnap
    return 'tegnap, ' . strftime('%H.%M', $t);
  }
  if (date('Y', $t) == date('Y', $c)) { // iden
    if (date('W', $t) == date('W', $c)) { //a heten
      return strftime('%A, %H.%M', $t);
    }
    return strftime('%B %e. %H.%M', $t);
  }
  return strftime('%Y. %B %e. %H.%M', $t);
}

function phptemplate_username ($object, $long = false) {

  if ($object->uid && $object->name) {
    $user = user_load(array('uid' => (int) $object->uid));
    //profile_load_profile($user);
    $name = $user->name;
    if ($user->profile_teljesnev_toggle && drupal_strlen($user->name) > 1) {
      $name = $user->profile_teljesnev;
    }
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($name) > 20 && !$long) {
      $name = drupal_substr($name, 0, 15) .'…';
    }
    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.'), 'class' => 'username'));
    }
    else {
      $output = '<span class="username">' . check_plain($name). '</span>';
    }
    if ($long) {
/*      if ($user->profile_homepage) {
        $output .= ' ' . l('honlapja', $user->profile_homepage,
                           array('title' => $name . ' személyes honlapja',
                                 'class' => 'userhomepage',
                                 'rel' => 'nofollow'),
                          null, null, true, false);
      }*/
      if (isset($user->roles[3]) or isset($user->roles[5])) {
        $output .= ' ' . l('ubuntu.hu szerkesztő',
                           'konyvlap/kozosseg',
                           array('class' => 'user-roles-web1'), null, 'web');
      }
      elseif (isset($user->roles[6])) {
        $output .= ' ' . l('ubuntu.hu cikkíró',
                           'konyvlap/kozosseg',
                           array('class' => 'user-roles-web2'), null, 'web');
      }
      if (isset($user->roles[4])) {
        $output .= ' ' . l('Ubuntu honosító',
                           'konyvlap/kozosseg',
                           array('class' => 'user-roles-l10n'), null, 'l10n');
      }
      if (isset($user->roles[7])) {
        $output .= ' ' . l('dokumentációíró',
                           'konyvlap/kozosseg',
                           array('class' => 'user-roles-dok'), null, 'dok');
            }
      if (isset($user->roles[8])) {
        $output .= ' ' . l('moderátor',
                           'konyvlap/kozosseg',
                           array('class' => 'user-roles-mod'), null, 'mod');
      }
      if ($user->uid == 10) {//atya 
        $output .= ' <a href="http://barack.fsf.hu" title="Rendszeradminisztrátor">
	             <img src="/themes/website2007/barack.png" alt="barack" /></a>';
      }
      


    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }

  return $output;
}

function phptemplate_user_profile($account, $fields) {
  $output = '<div class="profile">';
  //$output .= theme('user_picture', $account);//rem
  $i = 0;//add
  foreach ($fields as $category => $items) {
    $output .= '<div id="profile-category-' . ++$i . '">';//add
    if (strlen($category) > 0) {
      $output .= '<h2 class="title">'. check_plain($category) .'</h2>';
    }
    $output .= '<dl>';
    foreach ($items as $item) {
      if (isset($item['title'])) {
        $output .= '<dt class="'. $item['class'] .'">'. $item['title'] .'</dt>';
      }
      $output .= '<dd class="'. $item['class'] .'">'. $item['value'] .'</dd>';
    }
    $output .= '</dl>';
    $output .= '</div>';//add
  }
  $output .= '</div>';

  return $output;
}
function phptemplate_links($links, $attributes = array('class' => 'links')) {
  $output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      if (!isset($link['title'])) {//add
        continue;//add
      }//add
      $class = '';

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<li class="'. $extra_class . $class .'">';

      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
        $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}


function _website2007_links($links = array()) {
  $folso = array();

  if (isset($links['comment_comments'])) {
    $folso[] = $links['comment_comments'];
    unset($links['comment_comments']);
  }
  if (isset($links['comment_new_comments'])) {
    $folso[] = $links['comment_new_comments'];
    unset($links['comment_new_comments']);
  }

  return array($folso, $links);
}

function _website2007_head($links, $submitted, $terms) {

  $head[] = array('title' => $submitted, 'html' => true);
  if ($node->type == 'forum' or user_access('administer content')) {
    $head[] = array('title' => $terms, 'html' => true);
  }
  foreach ($links as $l) {
    $head[] = $l;
  }
  return _website2007_comma_links($head);
}

function _website2007_comma_links($links, $attributes = array()) {
  $output = '';

  if (count($links) > 0) {
    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = '';

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<span class="'. $extra_class . $class .'">';

      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
        $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }
      if ($i < $num_links) {
        $output .= ", \n";
      }
      $i++;

    }

  }

  return $output;
}
drupal_add_js("misc/jquery.cookie.js", 'theme');
drupal_add_js("misc/jquery.plugin.js", 'theme');
drupal_add_js("misc/ubuntu2.js", 'theme');

function phptemplate_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();

  foreach ($files as $file) {
    $file = (object)$file;
    if ($file->list && !$file->remove) {
      // Generate valid URL for both existing attachments and preview of new attachments (these have 'upload' in fid)
      $href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
      $text = $file->description ? $file->description : $file->filename;
      $mime = explode('/', $file->filemime);
      $ext = explode('.', $file->filename);
      $ext = $ext[count($ext)-1];

      $rows[] = array(
        l($text, $href, array('class' => $mime[0] . ' ' . $mime[1] . ' ' . $ext)),
        format_size($file->filesize)
        );
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}