<?php
class AdvancedReports_ShowWidget_View extends Vtiger_IndexAjax_View{
    /**
     * Function to get the list of Script models to be included
     * @param Vtiger_Request $request
     * @return <Array> - List of Vtiger_JsScript_Model instances
     */
    public function getHeaderScripts(Vtiger_Request $request) {
        $moduleName = $request->getModule();
        $jsFileNames = array(
            "modules.$moduleName.resources.Widget",
        );

        $jsScriptInstances = $this->checkAndConvertJsScripts($jsFileNames);
        return $jsScriptInstances;
    }

    public function process(Vtiger_Request $request){
        $currentUser = Users_Record_Model::getCurrentUserModel();
        $viewer = $this->getViewer($request);
        $viewer->force_compile = true; // Disable smarty caching
        $viewer->caching = 0; // Disable smarty caching
        $moduleName = $request->getModule();

        //
        $linkId = $request->get('linkid');
        $widgetId = $request->get('widgetid');
        $reportId = $request->get('reportId');

        // Moved from "modules/Vtiger/views/ShowWidget.php"
        $widget = NULL;
        if(!empty($linkId)) {
            $widget = new Vtiger_Widget_Model();
            $widget->set('linkid', $linkId);
            $widget->set('userid', $currentUser->getId());
            $widget->set('filterid', $request->get('filterid', NULL));

            // In Vtiger7, we need to pin this report widget to first tab of that user
            global $vtiger_current_version;
            if(version_compare($vtiger_current_version, '7.0.0', '>=')) {
                $dashBoardModel = Vtiger_DashBoard_Model::getInstance($moduleName);
                $defaultTab = $dashBoardModel->getUserDefaultTab($currentUser->getId());
                $widget->set('tabid', $request->get('tab', $defaultTab['id']));
            }

            if ($request->has('data')) {
                $widget->set('data', $request->get('data'));
            }

            $chart = array();
            if ($reportId) { // get chart title for vTiger dashboard
                include "modules/AdvancedReports/controller.php";
                $ctrl = new ARReportController();
                $report = $ctrl->getReportById($reportId);
                $chart = $report['chart'] ? json_decode($report['chart'], true) : array();
            }

            if (isset($chart['chartTitle'])) {
                $widget->set('title', $chart['chartTitle']);
            }

            $widget->add();
            // Update variable with  newly added widget id
            $widgetId = $widget->get("id");
        }

        if(!$reportId){
            $content = "Pick report: <select></select>";
        }else{
            $url = "index.php?module=AdvancedReports&view=AdvancedReportsAjax&file=AdvancedReportsWidget&record={$reportId}";
            $content = '<iframe style="height:100%;width:100%;border:0px;" src="'.$url.'" data-id="'.$linkId.'" />';
        }

//        require_once "modules/AdvancedReports/models/VtigerAdvancedReportsModel.php";
//        $widget = Vtiger_AdvancedReports_Model::getInstanceWithWidgetId($widgetId, $currentUser->getId());
        $widget = Vtiger_Widget_Model::getInstanceWithWidgetId($widgetId, $currentUser->getId());


        $viewer->assign('SCRIPTS',$this->getHeaderScripts($request));
        $viewer->assign('WIDGET', $widget);
        $viewer->assign('MODULE_NAME', $moduleName);
        $viewer->assign('CURRENTUSER', $currentUser);
        $viewer->assign('WIDGETCONTENT', $content);
        $viewer->assign('WIDGETREPORTID', $reportId);

        $content = $request->get('content');
        if(!empty($content)) {
            $viewer->view('dashboards/DashBoardWidgetContents.tpl', $moduleName);
        } else {
            $templateFilePath = 'dashboards/dashboardWidget.tpl';
            $viewer->clear_cache($templateFilePath); // Clear template file cache
            $viewer->view($templateFilePath, $moduleName);
        }
    }
}