<?php

class pluginArchives extends Plugin
{

	public function init()
	{
		$this->dbFields = array(
			'label' => 'Archives',
			'monthly' => false,
		);
	}

	public function form()
	{
		global $L;

		$html = '<div>';
		$html .= '<label>' . $L->get('Label') . '</label>';
		$html .= '<input id="jslabel" name="label" type="text" dir="auto" value="' . $this->getValue('label') . '">';
		$html .= '<span class="tip">' . $L->get('This title is almost always used in the sidebar of the site') . '</span>';
		$html .= '</div>';

		echo Bootstrap::formCheckbox(array(
			'name' => 'monthly',
			'label' => $L->g('Monthly'),
			'labelForCheckbox' => $L->g('Show archives by months.'),
			'placeholder' => '',
			'checked' => $this->getValue('monthly'),
			'tip' => ""
		));

		return $html;
	}

	public function siteSidebar()
	{
		global $L;
		global $archives;
		global $url;

		$filter = $url->filters('archive');

		$html  = '<div class="plugin plugin-archives">';
		$html .= '<h2 class="plugin-label">' . $this->getValue('label') . '</h2>';
		$html .= '<div class="plugin-content">';
		$html .= '<ul>';

		// By default the database of tags are alphanumeric sorted
		foreach ($archives->db as $key => $fields) {
			if (
				($this->getValue('monthly') && strlen($key) != 4) 
				|| (!$this->getValue('monthly') && strlen($key) == 4)
				) {
				$html .= '<li class="flat-list">';
				$html .= '<a href="' . DOMAIN_ARCHIVES . $key . '">';
				$html .= $fields['name'];
				$html .= '</a>';
				$html .= '</li>';
			}
		}

		$html .= '</ul>';
		$html .= '</div>';
		$html .= '</div>';

		return $html;
	}
}