/**
 * @param $dashboardId
 * @return array
 */
function gd_dashboard_admin_page_edit ( $dashboardId ) {
    $dashboardNode = gd_dashboard_load($dashboardId);
    if ( !$dashboardNode ) {
        return MENU_NOT_FOUND;
    }

    gd_datasource_set_active(get_node_field_value($dashboardNode,'field_dashboard_datasource'));

    if ( !gd_account_user_is_admin() && !gd_account_user_is_datasource_admin(null,gd_datasource_get_active()) ) {
        return MENU_ACCESS_DENIED;
    }

    drupal_add_library('gd_dashboard_admin', 'GD_Admin_DashboardSection_Builder');
    drupal_add_js(drupal_get_path('module','gd_dashboard_admin').'/js/builder/button/action/DashboardDeleteButton.js');

    drupal_add_library('gd','datatables');
    drupal_add_library('gd','highcharts');
    drupal_add_js('sites/all/libraries/sparkline/jquery.sparkline.min.js');

    $options = array('fields'=>array('filters','drilldowns','reports','css'));
    $dashboard = gd_dashboard_create_api_object_from_node($dashboardNode,$options);

    drupal_add_http_header('Cache-Control','no-cache, max-age=0, must-revalidate, no-store');

    return gd_dashboard_admin_page($dashboard);
}
/**
 * @return array
 */
function gd_dashboard_page_index () {
    global $user;
    $event = new DefaultEvent();

    if ( arg(0) == 'public' && !gd_dashboard_get_setting('public') ) {
        return MENU_NOT_FOUND;
    }

    if ( !empty($_GET['id']) ) {
        $dashboardNode = gd_dashboard_load($_GET['id']);
        if ( $dashboardNode ) {
            gd_datasource_set_active(get_node_field_value($dashboardNode,'field_dashboard_datasource'));
            if ( !gd_dashboard_access_view($dashboardNode) ) {
                return MENU_ACCESS_DENIED;
            }
        } else {
            return MENU_NOT_FOUND;
        }
    }

    if ( arg(0) == 'public' ) {
        list($datasources, $dashboardNode, $dashboards) = gd_dashboard_public_index_variables();
    } else {
        list($datasources, $dashboardNode, $dashboards) = gd_dashboard_index_variables();
    }

    ob_start();

    if ( arg(0) == 'public' ) {
        drupal_add_http_header('Cache-Control','no-transform,public,max-age=3600,s-maxage=3600');
        drupal_add_http_header('Expires',gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
    }

    if ( empty($datasources) ) {
        echo '<div id="dashboard-view" class="gd-container"><p>There are no topics. Please contact your site administrator to create a topic.</p></div>';
    } else if ( empty($dashboards) ) {
        echo '<div id="dashboard-view" class="gd-container"><p>There are no dashboards set up for this topic.</p></div>';
    } else {
        try {
            $DashboardConfig = new GD_DashboardConfig($dashboardNode, $_GET);

            drupal_add_library('gd_dashboard', 'GD_Dashboard_View');

            echo '<div id="dashboard-view" class="gd-container">';

            echo '<div class="row">';

            echo '  <div class="col-md-6">';
            echo '    <h2>'.$dashboardNode->title.'</h2>';
            if ( get_node_field_value($dashboardNode, 'field_dashboard_desc') ) {
                echo '<p>'.get_node_field_value($dashboardNode, 'field_dashboard_desc').'</p>';
            }
            echo '  </div>';
            echo '  <div class="col-md-6">';

            echo '<div class="pull-right">';
            if ( arg(0) != 'public' && (gd_account_user_is_admin() || gd_account_user_is_datasource_admin($user, gd_datasource_get_active())) ) {
                echo '<a role="button" type="button" id="editButton" tabindex="100" class="btn btn-default gd-dashboard-editbtn" href="/cp/dashboard/'.$dashboardNode->nid.'">Edit</a>';
            }

            if ( gd_dashboard_get_setting('export') && $DashboardConfig->isExportable() ) {
                echo ' <button role="button" type="button" id="exportButton" tabindex="100" class="btn btn-default btn-gd-dashboard-export" data-dashboard="'.$dashboardNode->nid.'">Export</button>';
            }
            if ( gd_dashboard_get_setting('print') && $DashboardConfig->isPrintable() ) {
                echo '<button role="button" type="button" style="margin-left:3px;" id="printButton" tabindex="100" class="btn btn-default btn-gd-dashboard-print" href='."javascript:void(0)".'>Print</button>';
            }

            echo '</div>';

            echo '  </div>';
            echo '</div>';

            $options = array();
            if ( $DashboardConfig->isPublic() && arg(0) == 'public' ) {
                $options['public'] = TRUE;
            }

            $configView = new GD_DashboardView($DashboardConfig);
            echo $configView->getView($options);
            echo '</div>';

            $DashboardConfig->attachRequiredLibs(); // must be called after building view, or libs won't be set yet

        } catch (Exception $e) {
            LogHelper::log_error($e);
            echo '<div id="dashboard-view" class="gd-container"><p class="messages error">Dashboard could not be rendered. '.$e->getMessage().'</p></div>';
        }
    }

    if ( arg(0) == 'public' ) {
        module_invoke_all('gd_dashboard_public_index_alter');
    } else {
        module_invoke_all('gd_dashboard_index_alter');
    }

    if (!empty($_REQUEST['export-view'])) {
        $page = array(
            '#show_messages' => false,
            '#theme' => 'page__dashboard__export__view',
            '#type' => 'page',
            'content' => array(
                'system_main' => array(
                    '#markup' => ob_get_clean()
                )
            ),
            'variables' => array(
                'datasources' => $datasources,
                'dashboard' => $dashboardNode,
                'dashboards' => $dashboards
            )
        );
    } else {
        $page = array(
            '#show_messages' => false,
            '#theme' => 'page__dashboard',
            '#type' => 'page',
            'content' => array(
                'system_main' => array(
                    '#markup' => ob_get_clean()
                )
            ),
            'variables' => array(
                'datasources' => $datasources,
                'dashboard' => $dashboardNode,
                'dashboards' => $dashboards
            )
        );
    }

    if (isset($dashboardNode->nid)) {
        $event->type = 1; // see gd_health_monitoring_database_install() for more details
        $event->owner = $dashboardNode->nid;

        EventRecorderFactory::getInstance()->record($event);
    }

    return $page;
}