<?php
/**
 * Smarty plugin
 *
 * @category Smarty
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @copyright 2009 untermStrich
 * @package Smarty
 * @subpackage plugins
 * @version $Id$
*/

/**
 * Smarty widget include function plugin
 *
 * Purpose:  Includes a portlet into a template (Calls the view part)
 *
 * @param       array   $params     Parameters:
 *                                  <ul>
 *                                      <li>file    string  Name of the portlet to load</li>
 *                                  </ul>
 * @param       object  &$smarty    The smarty object
 * @return	string  The portlet HTML
 */
function smarty_function_widget(array $params, US_Smarty &$smarty)
{
    if (isset($params['file']) && isset($params['open'])) {
        $file = $params['file'];
        unset($params['file']);

        $CI = CI_Controller::get_instance();
        $CI->load->us_library('Languages');
        $class = $CI->load->portlet($file, $smarty->get_mode(), $msg, true);
        $is_widget = false;
        if ($class!==false) {
            $myReflection = new ReflectionClass($class);
            if ($myReflection->implementsInterface('Widget_interface') === false) {
                $is_widget = false;
            } else {
                $is_widget = true;
                $widget_information = call_user_func($class.'::get_widget_information', $params);
                
                $CI->login->log_call_action('butler', 'view', $class);
            }
            
            if ($class==='Updateinfo_view' || $class==='Webinar_view') {
                $CI->load->model('Portlets_model', 'portlets', true);
                $request_vars = new stdClass();
                    $request_vars->portlet_id = $class;
                    $request_vars->staff_id = $CI->login->get_user_id();
                $all_portlets_of_type =  $CI->portlets->get_list($request_vars);
                
                if (count($all_portlets_of_type)<=1) {
                    $widget_information->removable = false;
                }
            }
        }
        
        $auto_refresh = 0;
        if (isset($params['auto_refresh']) && !empty($params['auto_refresh'])) {
            $auto_refresh = intval($params['auto_refresh']);
        } elseif (isset($widget_information->auto_refresh) && $widget_information->auto_refresh !== false) {
            $auto_refresh = intval($widget_information->auto_refresh);
        }

        if (US_MAJOR_VERSION<15) {
            $reply = '<div class="portlet-header" data-auto_refresh="'.$auto_refresh.'">';
                $reply.= '<span class="dialog_make_btn16 portlet_config smallbtns ui-corner-all" title="'.$CI->lang->line('basic_config').'"></span>';//Add config button
                if (!isset($widget_information->updateable) || $widget_information->updateable === true) {
                    $reply.= '<span class="dialog_make_btn16 portlet_update smallbtns ui-corner-all" title="'.$CI->lang->line('basic_update').'"></span>';//Add update button
                }
                if (isset($widget_information->print) && $widget_information->print) {
                    $reply.= '<span class="dialog_make_btn16 portlet_print smallbtns ui-corner-all" title="'.$CI->lang->line('basic_print_portlet').'"></span>';//Add print button
                }
                $reply.= '<span class="dialog_make_btn16 maximize smallbtns ui-corner-all" title="'.$CI->lang->line('basic_fullscreen').'" onclick="main.portlets.fullscreen(this);"></span>';
                if (isset($widget_information->help_link) && $widget_information->help_link!==false && $widget_information->help_link!=='') {//Add help button
                    $link = $widget_information->help_link;
                    if ($link[0] === ':') { // help site
                        $lang = Languages::shortened($CI->web_settings->getLanguage());
                        $link = 'https://webservices.untermstrich.com/h4/'.$lang.$link;
                    }          
                    $reply.= '<span class="dialog_make_btn16 portlet_help smallbtns ui-corner-all" title="'.$CI->lang->line('basic_help').'" onmousedown="main.portlets.allow_link = true;" onclick="main.portlets.do_link(\''.$link.'\');"></span>';
                }
                if ($params['open']) {
                    $display = '';
                    $reply .= '<span class="ui-icon ui-icon-minusthick"></span>';
                } else {
                    $display = 'display:none';
                    $reply .= '<span class="ui-icon ui-icon-plusthick"></span>';
                }
                if ($class!==false && $is_widget) {
                    $link = '';
                    if (isset($widget_information->link) && $widget_information->link!==false && $widget_information->link!==''){
                        $link = 'style="cursor:pointer" onmousedown="main.portlets.allow_link = true;" onclick="main.portlets.do_link(\''.$widget_information->link.'\');"';
                    }
                    $title_title = $CI->lang->line($widget_information->title);
                    if (!empty($widget_information->main_title)) {
                        $title_title = $CI->lang->line($widget_information->main_title).': '.$title_title;
                    }
                    if (!empty($params['widget_title'])) {
                        $title = us_htmlentities($params['widget_title'], ENT_NOQUOTES);
                    } else {
                        $title = $title_title;
                    }
                    if ($class==='Com_untermstrich_psp') {
                        $title .= ' <span style="color:red; text-shadow: 0 0 1px rgba(180, 0, 0, 0.5);"> - Bitte nutzen Sie das integrierten Widget Schaltpult, statt dem Widget Projektschaltpult. Dieses Widget wird nicht mehr unterstützt.</span>';
                    }
                    if ($class==='Com_untermstrich_cost_overview') {
                        $title .= ' <span style="color:red; text-shadow: 0 0 1px rgba(180, 0, 0, 0.5);"> - Bitte nutzen Sie das integrierten Widget Resultate: Globale Projektauswertung, statt dem Widget Kostenübersicht. Dieses Widget wird nicht mehr unterstützt.</span>';
                    }
                    $reply .='<span '.$link.' title="'.$title_title.' - '.htmlentities($class).'">'.$title.'</span>';
                } else {
                    if ($class===false) {
                        $reply .= $file;
                    } else {
                        $reply .= $class;
                    }
                }
            $reply .= '</div>';
            if ($class==='Portlet_admin') {
                $reply = '';
            }
        } else {
            $reply = '';
            $display = '';
        }
        $reply .= '<div class="portlet-content" style="'.$display.'">';
            if ($class!==false && $is_widget) {
                try {
                    $reply .= call_user_func($class.'::view', $params);
                } catch (Exception $exc) {
                    $reply .= 'Fehler/Error in class "'.$class.'" '.$exc->getCode().': '.$exc->getMessage();
                }
            } else {
                if ($class===false) {
                    $reply .= 'Fehler/Error: '.$msg;
                } else {
                    $reply .= $CI->lang->line('portlet_no_widget');
                }
            }
        $reply .= '</div>';
        
        if($is_widget) {
            $reply .= '<div class="portlet-edit" style="display:none">';
            $reply .= '<form id="portlet-edit_'.$params['unique_id'].'" action="" method="post" onsubmit="main.portlets.save_config(this); return false;"></form>';
            $reply .= '</div>';
        }

        return $reply;
    } else {
        return '';
    }
}

