<?php
/**
 * Create flash tag
 *
 */
function flash_tag($flashFileName, $width, $height, $flashVars = array()) {
  if (is_array($flashVars)) $flashVars = http_build_query($flashVars);
  $basePath = pathinfo($flashFileName, PATHINFO_DIRNAME) . '/';
	return
	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' . $width . '" height=" ' . $height . '" align="middle">
	<param name=FlashVars VALUE="' . $flashVars . '">
	<param name="movie" value="' . $flashFileName . '" />
	<param name="quality" value="high" />
	<param name="allowScriptAccess" value="always" />
	<param name="wmode" value="transparent" />
	<param name="base" value="' . $basePath . '" />
	<embed	src="' . $flashFileName . '"
			quality="high"
			width="' .$width . '" height="' .$height . '"align="middle"
			type="application/x-shockwave-flash"
			allowScriptAccess="always"
			wmode="transparent"
			base="' . $basePath . '"
			pluginspage="http://www.macromedia.com/go/getflashplayer"
			flashVars="' . $flashVars . '" />
	</object>';
}

/**
 * @param unknown_type $pager
 * @param unknown_type $baseUri
 * @param $updateDiv: name of ajax update div (=null) if don't use ajax
 * @return html string for
 */
function showPageNav13($pager, $baseUri, $updateDiv = null) {
  $pageNav = '';
  $pages = $pager->getTotalPages();
  if ($pages <= 1) return $pageNav;

  $root = '/css/layout/'.blogStore::$moduleConfig['layout'].'/images/';
  $next_icon = image_tag($root.'next.png', array('align'=>'absmiddle'));
  $prev_icon = image_tag($root.'previous.png', array('align'=>'absmiddle'));
  $uriPage = $baseUri. (preg_match('/\?/', $baseUri)? '&': '?'). 'page=';
  $option = $updateDiv? array('onclick'=>"new Ajax.Updater('$updateDiv', this.href); return false;"): array();

  $page = $pager->getPrev();
  if ($page == false) $page = $pager->getFirstPage();
  $pageNav .= link_to($prev_icon, $uriPage.$page, $option);

  $links = array_merge(array(1, 2), $pager->getPrevLinks(), array($pager->getPage()), $pager->getNextLinks(), array($pages-1, $pages));
  $links = array_unique($links);
  $prevPage = 0;
  foreach ($links as $page) {
    if ($prevPage != $page - 1) $pageNav .= '...';
    $linkOption = ($page == $pager->getPage())? array('class'=>'selected'): array();
    $pageNav .= link_to_unless($page == $pager->getPage(), $page, $uriPage.$page, $linkOption + $option);
    $prevPage = $page;
  }

  $page = $pager->getNext();
  if ($page == false) $page = $pager->getLastPage();
  $pageNav .= link_to($next_icon, $uriPage.$page, $option);

  return content_tag('div', $pageNav, array('class'=>'page-nav'));
}

/**
 * Enter description here...
 *
 * @param unknown_type $tabs
 * @param unknown_type $htmlOptions
 * @param unknown_type $numTabsPerRow
 * @param unknown_type $tabCurrent
 * @param unknown_type $ajax
 * @return unknown
 */
function gridTab($tabs, $baseLink, $tabCurrent, $ajax = null, $htmlOptions = array('class'=>'csstab'), $numTabsPerRow = 6) {
  $tabs = array_chunk($tabs, $numTabsPerRow, true);
  $rows = array();
  $currentRow = null;

  foreach ($tabs as $i=>$tabRow) {
    $row = '';
    foreach ($tabRow as $j=>$tab) {
      if ($j === $tabCurrent) $currentRow = $i;
      if (is_string($tab)) $tab = array('text' => $tab);
      if ($baseLink != '') $tab['link'] = $baseLink.$j;
      if (!key_exists('options', $tab)) $tab['options'] = array();
      if ($ajax) $tab['options']['onclick'] = "new Ajax.Updater('$ajax', this.href, {asynchronous:true, evalScripts:true}); return false;";

      $liOptions = ($j == $tabCurrent)? array('id'=>'current'): array();
      $row .= content_tag('li', link_to(content_tag('span', $tab['text']), $tab['link'], $tab['options']), $liOptions);
    }
    $rows[] = content_tag('ul', $row);
  }

  if ($currentRow!==null && $currentRow < count($tabs)-1) {
    $oldRow = array_splice($rows, $currentRow, 1);
    $rows[] = $oldRow[0];
  }
  return content_tag('div', join('', $rows), $htmlOptions);
}

/**
 * Enter description here...
 *
 * @param unknown_type $menus
 * @param unknown_type $html_options
 * @return unknown
 */
function cssMenu($menus, $html_options = array()) {
  $rows = '';
  $extra = '';
  $user = sfContext::getInstance()->getUser();
  foreach ($menus as $key => $menu) {
    $allow = @$menu['allow'];
    if ($allow && !$user->hasCredential($allow)) continue;

    if (@$menu['link'] == null) $menu['link'] = '#';
    else {
      if (!preg_match('@://@', $menu['link'])) {
        $menu['link'] = preg_replace('@^/*@', url_for('/', true), $menu['link']);
      }
    }
    if (@$menu['child'] != null) {
      $subId = $html_options['id'] .'.'. $key;
      $extra .= cssMenu($menu['child'], array('id'=>$subId, 'class'=>'dropmenudiv'));
      $menu['options'] = array('rel'=>$subId);
    }
    $rows .= content_tag('li', link_to(content_tag('span', $menu['text']), $menu['link'], @$menu['options']));
  }
  return content_tag('div', content_tag('ul', $rows), $html_options) . $extra;
}

/**
 * retrun a html string of grid based table
 *
 * @param unknown_type $rows
 * @param array columnClasses array of extra style-sheet
 */
function gridTable($rows, $prefix = 'span-', $columnClasses = null, $lineOptions = array()) {
  $columns = array_shift($rows);
  $lines = array();
  foreach ($rows as $row) {
    $line = '';
    foreach ($row as $j => $col) {
      $class = $columnClasses? $columnClasses[$j]: '';
      $line .= content_tag('div', $col? $col: '&nbsp;', array('class'=>$class.' '.$prefix.$columns[$j]));
    }
    $lines[] = content_tag('div', $line, $lineOptions + array('class'=>'wfull nowrap'));
  }
  return join("\n", $lines);
}

  /**
   * functions for caching html object
   *
   */
  function put_html_cache($cacheName, $content, $timeout = 300) {
    $fileName = blogStore::$blogId.'/'.$cacheName.'.html';
    sfMemcacheCache::getInstance('block')->set($fileName, '', $content, $timeout);
  }

  function get_html_cache($cacheName) {
    $fileName = blogStore::$blogId.'/'.$cacheName.'.html';
    return sfMemcacheCache::getInstance('block')->getWithQueue($fileName, '');
  }

  function cache_include_component($moduleName, $componentName, $vars, $cache_time, $cache_id = '') {
    $cacheName = $moduleName.'_'.$componentName.$cache_id;
    if (preg_match('@^\D+@', $componentName, $match)) $componentName = $match[0];
    if ($cache_time === null) $cache_time = 300;
    if ($cache_time == 0) {
      echo get_component($moduleName, $componentName, $vars);
      return;
    }
    $content = get_html_cache($cacheName);
    if ($content == null) {
      $content = get_component($moduleName, $componentName, $vars);
      put_html_cache($cacheName, $content, $cache_time);
    }
    echo $content;
  }

  function cache_include_reset($blogId = null) {
    $blocks = sfConfig::get('sf_blocknocache');
    foreach ($blocks as $key => $timeout) {
      if ($timeout == 0) continue;
      $fileName = blogStore::$blogId.'/block_'.$key.'.html';
      sfMemcacheCache::getInstance('block')->remove($fileName, '');
    }
  }

  function begin_html_cache($cache_name) {
    $content = get_html_cache($cache_name);
    $isnew = ($content == null);
    if ($isnew) ob_start(); else echo $content;
    return $isnew;
  }

  function end_html_cache($cache_name) {
    put_html_cache($cache_name, ob_get_contents());
    ob_end_flush();
  }
