<?php

defined('HOSTCMS') || exit('HostCMS: access denied.');

/**
 * Guestbook.
 *
 * @package HostCMS 6\Skin
 * @version 6.x
 * @author Hostmake LLC
 * @copyright © 2005-2013 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
class Skin_Default_Module_Informationsystem_Module extends Informationsystem_Module
{
	/**
	 * Name of the skin
	 * @var string
	 */
	protected $_skinName = 'default';

	/**
	 * Name of the module
	 * @var string
	 */
	protected $_moduleName = 'informationsystem_items';

	/**
	 * Constructor.
	 */
	public function __construct()
	{
		parent::__construct();

		$this->_adminPages = array(
			77 => array('title' => 'Гостевая книга')
		);
	}

	/**
	 * Show admin widget
	 * @param int $type
	 * @param boolean $ajax
	 * @return self
	 */
	public function adminPage($type = 0, $ajax = FALSE)
	{
		$iInformationsistemId = 3;
		$oInformationsystem_Item = Core_Entity::factory('Informationsystem_Item', $iInformationsistemId);
		$oInformationsystem_Item->queryBuilder()
			->orderBy('informationsystem_items.datetime', 'DESC')
			->limit(3);

		$aInformationsystem_Items = $oInformationsystem_Item->findAll();

		$windowId = 'modalInformationsystemItems';
		$shortcutImg = "/modules/skin/{$this->_skinName}/images/module/{$this->_moduleName}.png";
		$shortcutTitle = 'Гостевая книга';

		if (count($aInformationsystem_Items) > 0)
		{
			if (!$ajax)
			{
				$oModalWindow = Core::factory('Core_Html_Entity_Div')
					->id($windowId)
					->class('widget')
					->title($shortcutTitle)
					->add($oModalWindowSub = Core::factory('Core_Html_Entity_Div')
						->class('sub')
					);
			}
			else
			{
				$oModalWindowSub = Core::factory('Core_Html_Entity_Div');
			}

			$iAdmin_Form_Id = 12;
			$oAdmin_Form = Core_Entity::factory('Admin_Form', $iAdmin_Form_Id);
			$oAdmin_Form_Controller = new Admin_Form_Controller($oAdmin_Form);

			$sSiteuserHref = '/admin/siteuser/siteuser/index.php';

			foreach ($aInformationsystem_Items as $key => $oSiteuser)
			{
				$oCore_Html_Entity_Div_Action = Core::factory('Core_Html_Entity_Div')
					->class('action')
					->add(
							Core::factory('Core_Html_Entity_A')
							->href(
								$oAdmin_Form_Controller->getAdminActionLoadHref($sSiteuserHref, 'changeActive', NULL, 0, $oSiteuser->id)
							)
							->title(
								Core::_('Siteuser.change_active')
							)
							->onclick(
								"$.widgetRequest({path: '" . $oAdmin_Form_Controller->getAdminActionLoadHref($sSiteuserHref, 'changeActive', NULL, 0, $oSiteuser->id) . "', context: $('#{$windowId}')}); return false"
							)
							->add(
								Core::factory('Core_Html_Entity_Img')
									->src('/modules/skin/' . $this->_skinName . '/images/action/' . ($oSiteuser->active ? 'checked' : 'unchecked') . '.png')
							)
						)
					->add(
						Core::factory('Core_Html_Entity_A')
						->href(
							$oAdmin_Form_Controller->getAdminActionLoadHref($sSiteuserHref, 'edit', NULL, 0, $oSiteuser->id)
						)
						->title(
							Core::_('Siteuser.edit')
						)
						->onclick(
							"$.openWindowAddTaskbar({path: '" . $oAdmin_Form_Controller->getAdminActionLoadHref($sSiteuserHref, 'edit', NULL, 0, $oSiteuser->id) . "', shortcutImg: '{$shortcutImg}', shortcutTitle: '{$shortcutTitle}', Minimize: true, Closable: true}); return false"
						)
						->add(
							Core::factory('Core_Html_Entity_Img')
								->src('/modules/skin/' . $this->_skinName . '/images/action/edit.png')
							)
					)
					->add(
						Core::factory('Core_Html_Entity_A')
						->href(
							$oAdmin_Form_Controller->getAdminActionLoadHref($sSiteuserHref, 'markDeleted', NULL, 0, $oSiteuser->id)
						)
						->title(
							Core::_('Siteuser.delete')
						)
						->onclick(
							"res = confirm('".Core::_('Admin_Form.confirm_dialog', htmlspecialchars(Core::_('Admin_Form.msg_information_alt_delete')))."'); if (res) { $.widgetRequest({path: '" . $oAdmin_Form_Controller->getAdminActionLoadHref($sSiteuserHref, 'markDeleted', NULL, 0, $oSiteuser->id) . "', context: $('#{$windowId}')});} return false"
						)
						->add(
							Core::factory('Core_Html_Entity_Img')
								->src('/modules/skin/' . $this->_skinName . '/images/action/delete.png')
						)
					);

				$oDiv = Core::factory('Core_Html_Entity_Div')
					->class('event event' . ($oSiteuser->active == 1 ? 1 : 4))
					->add(
						Core::factory('Core_Html_Entity_Div')
							->class('corner')
					)
					->add(
						Core::factory('Core_Html_Entity_Span')
							->value(Core::_('Siteuser.login') . trim(htmlspecialchars(Core_Str::cut(strip_tags(html_entity_decode($oSiteuser->login, ENT_COMPAT, 'UTF-8')), 70))))
					)
					->add(
						Core::factory('Core_Html_Entity_Div')
							->class('clear')
					);

				$sEmail = strip_tags(html_entity_decode($oSiteuser->email, ENT_COMPAT, 'UTF-8'));
					
				strlen($oSiteuser->email) && $oDiv
					->add(
						Core::factory('Core_Html_Entity_Span')
							->value(Core::_('Siteuser.email'))
					)
					->add(
						Core::factory('Core_Html_Entity_A')
							->href('mailto:' . $sEmail)
							->value(trim(htmlspecialchars(Core_Str::cut($sEmail, 70))))
					)
					
					->add(
						Core::factory('Core_Html_Entity_Div')
						->class('clear')
					);

				$oDiv->add($oCore_Html_Entity_Div_Action)
					->add(
						Core::factory('Core_Html_Entity_Div')
							->class('widget_date')
							->value(htmlspecialchars(Core_Date::sql2date($oSiteuser->datetime)))
					);

				$oModalWindowSub->add($oDiv);
			}


			$oModalWindowSub->add(
				Core::factory('Core_Html_Entity_Div')
					->class('widgetDescription')
					->add(
						Core::factory('Core_Html_Entity_Img')
							->src('/modules/skin/' . $this->_skinName . '/images/widget/counter.png')
					)->add(
						Core::factory('Core_Html_Entity_Div')
							->add(
								Core::factory('Core_Html_Entity_A')
									->id('widgetSiteuserOther')
									->href($sSiteuserHref)
									->value(Core::_('Siteuser.widget_other_users'))
							)
							->add(
								Core::factory('Core_Html_Entity_Script')
									->type('text/javascript')
									->value("$('#widgetSiteuserOther').linkShortcut({path: '{$sSiteuserHref}', shortcutImg: '{$shortcutImg}', shortcutTitle: '{$shortcutTitle}', Minimize: true, Closable: true});")
							)
					)
			);

			if (!$ajax)
			{
				$oUser = Core_Entity::factory('User')->getCurrent();

				$oModule = Core_Entity::factory('Module')->getByPath($this->_moduleName);
				$module_id = $oModule->id;
				$oUser_Setting = $oUser->User_Settings->getByModuleIdAndTypeAndEntityId($module_id, 77, 0);

				if (is_null($oUser_Setting))
				{
					$oUser_Setting = Core_Entity::factory('User_Setting');
					$oUser_Setting->position_x = "'right'";
					$oUser_Setting->position_y = 85;
					$oUser_Setting->width = 250;
					$oUser_Setting->height = 260;
				}

				$oModalWindow
					->add(
						Core::factory('Core_Html_Entity_Script')
							->type('text/javascript')
							->value("$(function(){
								$('#{$windowId}').widgetWindow({
									position: [{$oUser_Setting->position_x}, {$oUser_Setting->position_y}],
									width: {$oUser_Setting->width},
									height: {$oUser_Setting->height},
									moduleId: '{$module_id}',
									path: '/admin/index.php?ajaxWidgetLoad&moduleId={$module_id}&type=0'
								});
							});")
					)
					->execute();
			}
			else
			{
				$oModalWindowSub->execute();
			}
		}

		return $this;
	}
}