<?php

/**
 * i-doit
 *
 * Dashboard widget class
 *
 * @package     i-doit
 * @subpackage  Modules
 * @author      Leonard Fischer <lfischer@i-doit.com>
 * @version     1.2
 * @copyright   synetics GmbH
 * @license     http://www.i-doit.com/license
 */
class isys_dashboard_widgets_statstable extends isys_dashboard_widgets
{
    /**
     * Path and Filename of the template.
     *
     * @var  string
     */
    protected $m_tpl_file = '';

    /**
     * Init method.
     *
     * @return  isys_dashboard_widgets_quicklaunch
     * @author  Leonard Fischer <lfischer@i-doit.com>
     */
    public function init($p_config = [])
    {
        $this->m_tpl_file = __DIR__ . '/templates/statstable.tpl';

        return parent::init();
    }

    /**
     * Render method.
     *
     * @param   string $p_unique_id
     *
     * @return  string
     * @author  Leonard Fischer <lfischer@i-doit.com>
     */
    public function render($p_unique_id)
    {
        $activeSessions = [];

        $session = isys_application::instance()->container->get('session');
        $database = isys_application::instance()->container->get('database');
        $cmdbDao = isys_application::instance()->container->get('cmdb_dao');
        $locales = isys_application::instance()->container->get('locales');
        $l_dashboard_dao = isys_dashboard_dao::instance($database);

        $l_sql = "SELECT * FROM isys_user_session
			INNER JOIN isys_cats_person_list ON isys_user_session__isys_obj__id = isys_cats_person_list__isys_obj__id
			WHERE isys_user_session__time_last_action IN (SELECT MAX(isys_user_session__time_last_action) FROM isys_user_session GROUP BY isys_user_session__isys_obj__id)
			AND ((UNIX_TIMESTAMP(isys_user_session__time_last_action) + 600)-UNIX_TIMESTAMP(NOW())) > 0
			AND isys_user_session__description NOT LIKE '%logout=1%' AND isys_user_session__description NOT LIKE '%api=jsonrpc%'
			GROUP BY isys_user_session__isys_obj__id;";

        $l_res = $cmdbDao->retrieve($l_sql);
        if (count($l_res) > 0) {
            while ($l_data = $l_res->get_row()) {
                $activeSessions[] = $l_data["isys_cats_person_list__title"];
            }
        }

        $l_data = [
            'LC__WIDGET__STATSTABLE__TOTAL_COUNT_OBJECTS'      => $l_total_objects = $cmdbDao->count_objects(),
            'LC__WIDGET__STATSTABLE__TOTAL_COUNT_OBJECT_TYPES' => $l_total_object_types = $cmdbDao->count_object_types(),
            'LC__WIDGET__STATSTABLE__OBJECTS_IN_TYPES'         => round($l_total_objects / $l_total_object_types, 2),
            'LC__WIDGET__STATSTABLE__LAST_IDOIT_UPDATE'        => date("d.m.Y H:i:s", filemtime("index.php")),
            'LC__WIDGET__STATSTABLE__ACTIVE_SESSION_COUNT'     => count($activeSessions) . ' (' . implode(', ', $activeSessions) . ')',
            'LC__WIDGET__STATSTABLE__ACTIVE_WIDGETS'           => count($l_dashboard_dao->get_widgets_by_user($session->get_user_id())),
        ];

        try {
            $lastLogin = isys_tenantsettings::get('gui.empty_value', '-');

            if ((int) isys_tenantsettings::get('logging.user.last-login', 0) !== 0) {
                $query = 'SELECT isys_cats_person_list__last_login AS lastLogin
                    FROM isys_cats_person_list
                    WHERE isys_cats_person_list__isys_obj__id = ' . $cmdbDao->convert_sql_id($session->get_user_id()) . '
                    LIMIT 1;';
                $l_last_login = $cmdbDao->retrieve($query)->get_row_value('lastLogin');

                $lastLogin = $locales->fmt_datetime($l_last_login, false, false);
            }

            $l_data['LC__WIDGET__STATSTABLE__LAST_LOGIN'] = $lastLogin;
        } catch (isys_exception_database $e) {
            $l_data['LC__WIDGET__STATSTABLE__LAST_LOGIN'] = '<p class="box-red p5">' . $e->getMessage() . '</p>';
        }

        return $this->m_tpl->assign('stats', $l_data)
            ->fetch($this->m_tpl_file);
    }
}
