function gd_ext () {

    ob_get_clean();

    if ( !empty($_REQUEST['datasource']) ) {
        gd_datasource_set_active($_REQUEST['datasource']);
    }

    $ext = array();
    $dashboards = gd_dashboard_findall_by_datasource(LOAD_ENTITY);
    foreach ( $dashboards as $dashboard ) {
        $ext[] = array('id'=>$dashboard->nid,'title'=>$dashboard->title);
    }

    $response = new stdClass();
    $response->status = new stdClass();
    $response->status->code = 200;
    $response->status->message = 'OK';

    if ( isset($_REQUEST['id']) ) {
        $response->data = array('id'=>$_REQUEST['id'], 'title'=>$dashboards[$_REQUEST['id']]->title);
    }
    else {
        $response->data = $ext;
    }

    module_invoke_all('gd_ext_response_alter',$response);

    echo \GD\Utility\Json::getPayload($response,$_GET['callback']);

    gd_get_session_messages();

    drupal_exit();
}
function gd_datasource_page_admin_retrieve ( $datasourceName ) {

    $EnvironmentMetaModel = data_controller_get_environment_metamodel();

    $DataSource = $EnvironmentMetaModel->getDataSource($datasourceName);

    $output  = '<h2>'.$DataSource->publicName.'</h2>';
    $output .= '<pre>'.print_r($DataSource,true).'</pre>';

    $output .= '<h3>Datasets</h3>';
    $output .= '<ul>';
    foreach ( gd_dataset_findall_by_datasource($datasourceName) as $dataset ) {
        $output .= '<li>'.$dataset->publicName.'</li>';
    }
    $output .= '</ul>';

    $output .= '<h3>Reports</h3>';
    $output .= '<ul>';
    foreach ( gd_report_findall_by_datasource(LOAD_ENTITY,$datasourceName) as $report ) {
        $output .= '<li>'.$report->title.'</li>';
    }
    $output .= '</ul>';

    $output .= '<h3>Dashboards</h3>';
    $output .= '<ul>';
    foreach ( gd_dashboard_findall_by_datasource(LOAD_ENTITY,$datasourceName) as $dashboard ) {
        $output .= '<li>'.$dashboard->title.'</li>';
    }
    $output .= '</ul>';


    return array('datasource_info'=>array(
        '#markup' => $output
    ));
}
    protected function getConstants(array $options) {
        $dashboards = gd_dashboard_findall_by_datasource(LOAD_ENTITY);
        $dashboardMetadata = array();
        foreach($dashboards as $dashboard) {
            $metadata = new stdClass();
            $metadata->name = $dashboard->title;
            $metadata->id = $dashboard->nid;
            $metadata->alias = get_node_field_value($dashboard,'field_dashboard_alias',0);
            $dashboardMetadata[] = $metadata;
        }

        $admin = !empty($options['admin']) ? 'true' : 'false';
        $public = !empty($options['public']) ? 'true' : 'false';

        return 'var ReportConstants = {};
                            ReportConstants.reportTitle = "' . $this->ReportConfig->title . '";
                            ReportConstants.reportId = ' . intval($this->ReportConfig->getId()) .';
                            ReportConstants.dashboards = ' . json_encode($dashboardMetadata) .';
                            ReportConstants.host = "' . GOVDASH_HOST .'";
                            ReportConstants.adminView = ' . $admin . ';
                            ReportConstants.publicView = ' . $public . ';';
    }
 public static function getExportables($datasourceName) {
     return gd_dashboard_findall_by_datasource(LOAD_ENTITY,$datasourceName);
 }
function gd_sync_admin_datamart_form ( $form, &$form_state ) {
    // Enable language column if translation module is enabled or if we have any
    // node with language.
    $multilanguage = (module_exists('translation') || db_query_range("SELECT 1 FROM {node} WHERE language <> :language", 0, 1, array(':language' => LANGUAGE_NONE))->fetchField());

    // Build the sortable table header.
    $header = array(
        'title' => array('data' => t('Title'), 'field' => 'n.title'),
        'author' => t('Author'),
        'changed' => array('data' => t('Updated'), 'field' => 'n.changed', 'sort' => 'desc'),
        'datasets' => t('Datasets'),
        'reports' => t('Reports'),
        'dashboards' => t('Dashboards')
    );
    if ($multilanguage) {
        $header['language'] = array('data' => t('Language'), 'field' => 'n.language');
    }
    $header['operations'] = array('data' => t('Operations'));

    $nodes = gd_datamart_get_datamarts(LOAD_ENTITY);

    // Prepare the list of nodes.
    $languages = language_list();
    $destination = drupal_get_destination();
    $options = array();
    foreach ($nodes as $node) {
        $langcode = entity_language('node', $node);
        $l_options = $langcode != LANGUAGE_NONE && isset($languages[$langcode]) ? array('language' => $languages[$langcode]) : array();
        $options[$node->nid] = array(
            'title' => array(
                'data' => array(
                    '#type' => 'link',
                    '#title' => $node->title,
                    '#href' => 'admin/structure/govdashboard/sync/datamart/' . $node->nid,
                    '#options' => $l_options,
                    '#suffix' => ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed))),
                ),
            ),
            'author' => theme('username', array('account' => $node)),
            'changed' => format_date($node->changed, 'short'),
            'datasets' => count(gd_dataset_findall_by_datasource(!LOAD_ENTITY,get_node_field_value($node,'field_datamart_sysname'))),
            'reports' => count(gd_report_findall_by_datasource(!LOAD_ENTITY,get_node_field_value($node,'field_datamart_sysname'))),
            'dashboards' => count(gd_dashboard_findall_by_datasource(!LOAD_ENTITY,get_node_field_value($node,'field_datamart_sysname')))
        );
        if ($multilanguage) {
            if ($langcode == LANGUAGE_NONE || isset($languages[$langcode])) {
                $options[$node->nid]['language'] = $langcode == LANGUAGE_NONE ? t('Language neutral') : t($languages[$langcode]->name);
            }
            else {
                $options[$node->nid]['language'] = t('Undefined language (@langcode)', array('@langcode' => $langcode));
            }
        }
        // Build a list of all the accessible operations for the current node.
        $operations = array();
        if (node_access('update', $node)) {
            $operations['edit'] = array(
                'title' => t('edit'),
                'href' => 'node/' . $node->nid . '/edit',
                'query' => $destination,
            );
        }
        $options[$node->nid]['operations'] = array();
        if (count($operations) > 1) {
            // Render an unordered list of operations links.
            $options[$node->nid]['operations'] = array(
                'data' => array(
                    '#theme' => 'links__node_operations',
                    '#links' => $operations,
                    '#attributes' => array('class' => array('links', 'inline')),
                ),
            );
        }
        elseif (!empty($operations)) {
            // Render the first and only operation as a link.
            $link = reset($operations);
            $options[$node->nid]['operations'] = array(
                'data' => array(
                    '#type' => 'link',
                    '#title' => $link['title'],
                    '#href' => $link['href'],
                    '#options' => array('query' => $link['query']),
                ),
            );
        }
    }

    $form['nodes'] = array(
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $options,
        '#empty' => t('No content available.'),
    );

    return $form;
}
/**
 * @return array
 * @throws Exception
 * @throws IllegalArgumentException
 */
function gd_dashboard_index_variables () {
    $dashboards = array();
    $active_datasource_name = gd_datasource_find_active();
    $current_dashboard = gd_dashboard_get_current();
    global $user;

    if ( gd_account_user_is_admin() ) {

        // can see all datasources
        $datasources = gd_datasource_get_all();

        if ( $current_dashboard ) {
            gd_datasource_set_active(get_node_field_value($current_dashboard,'field_dashboard_datasource'));
        } else if ( !$active_datasource_name ) {
            if (isset($_GET['ds'])) {
                gd_datasource_set_active($_GET['ds']);
            } else {
                gd_datasource_set_active(key($datasources));
            }
        }

        // don't pick up any dashboards if there are no published datamarts - causes logic bomb further down
        if ( !empty($datasources) ) {
            $dashboards = gd_dashboard_findall_by_datasource(LOAD_ENTITY);
        }

    } else if ( $user->uid ) {

        // get view privileges for all dashboards
        $results = gd_account_user_get_dashboards();

        // pick up the datasources from the dashboards
        $datasources = gd_account_user_get_datasources();
        foreach ( $results as $dashboard ) {
            if ( !isset($datasources[get_node_field_value($dashboard,'field_dashboard_datasource')]) ) {
                $datasource = gd_datasource_get(get_node_field_value($dashboard,'field_dashboard_datasource'));
                $datasources[$datasource->name] = $datasource;
            }
        }

        // set current datasource
        if ( $current_dashboard ) {
            gd_datasource_set_active(get_node_field_value($current_dashboard,'field_dashboard_datasource'));
        } else {
            if ( !$active_datasource_name ) {
                if ( isset($_GET['ds']) && isset($datasources[$_GET['ds']]) ) {
                    gd_datasource_set_active($_GET['ds']);
                } else {
                    reset($results);
                    try {
                        if (!empty($results[key($results)])) {
                            $active_datasource_name = get_node_field_value($results[key($results)],'field_dashboard_datasource');
                            gd_datasource_set_active($active_datasource_name);
                        } else {
                            return 'You have not been granted permission to view any dashboards.';
                        }
                    } catch (Exception $e) {
                        drupal_set_message('No default datasource set.', 'error');
                        LogHelper::log_error($e->getMessage());
                    }
                }
            }
        }

        // remove dashboards that do not belong to datasource of current dashboard
        $active_datasource_name = gd_datasource_get_active();
        $dashboards = array();
        foreach ( $results as $key => $dashboard ) {
            if ( $active_datasource_name === get_node_field_value($dashboard,'field_dashboard_datasource') ) {
                $dashboards[$key] = $dashboard;
            }
        }

        // remove dashboards that were not created by the user
        if ( gd_datasource_is_property($active_datasource_name, 'personal') ) {
            global $user;
            $userCreatedDashboards = array();
            foreach ( $dashboards as $key => $dashboard ) {
                if ( $user->uid == $dashboard->uid ) {
                    $userCreatedDashboards[$key] = $dashboard;
                }
            }
            $dashboards = $userCreatedDashboards; // overwrite dashboard list
        }

    } else {

        // get datasources that belong to the dashboards
        // weed out dashboards with missing datasource
        $datasources = array();
        $publicDashboards = array();
        foreach ( gd_dashboard_get_dashboards_public(LOAD_ENTITY) as $dashboard ) {
            $datasourceName = get_node_field_value($dashboard,'field_dashboard_datasource');
            $datasource = gd_datasource_find($datasourceName);
            if ( !$datasource ) {
                continue;
            }
            $publicDashboards[$dashboard->nid] = $dashboard;
            if ( !isset($datasources[$datasourceName]) ) {
                $datasources[$datasource->name] = $datasource;
            }
        }

        // set current datamart
        if ( $current_dashboard ) {
            gd_datasource_set_active(get_node_field_value($current_dashboard,'field_dashboard_datasource'));
        } else {
            if ( isset($_GET['ds']) && isset($datasources[$_GET['ds']]) ) {
                gd_datasource_set_active($_GET['ds']);
            } else {
                gd_datasource_set_active(get_node_field_value($publicDashboards[key($publicDashboards)],'field_dashboard_datasource'));
            }
        }

        // remove dashboards that do not belong to datamart of current dashboard
        $active_datasource_name = gd_datasource_get_active();
        $dashboards = array();
        foreach ( $publicDashboards as $key => $dashboard ) {
            if ( $active_datasource_name === get_node_field_value($dashboard,'field_dashboard_datasource') ) {
                $dashboards[$key] = $dashboard;
            }
        }
    }

    reset($datasources);
    reset($dashboards);

    // sort the dashboard list by name
    usort($dashboards, function($a, $b) {
        if (strtolower($a->title) === strtolower($b->title)){
            return strnatcmp($a->title,$b->title);
        }
        return strnatcasecmp($a->title,$b->title);
    });

    // which dashboard to display
    if ( $current_dashboard ) {
        $dashboard = $current_dashboard;
    } else if (!empty($dashboards) ) {
        $dashboard = $dashboards[0];
    } else {
        $dashboard = null;
    }

    $display_dashboards = array();
    if ( !empty($dashboards) ) {
        $dashboard_ids = array(); // index of any parents from $dashboards
        $drilldown_dashboard_ids = array();
        foreach ( $dashboards as $d ) {
            $config = new GD_DashboardConfig($d);
            $dashboard_ids[] = (int)$d->nid;
            foreach( $config->drilldowns as $drilldown) {
                if ( is_object($drilldown->dashboard) ) {
                    $drilldown_dashboard_ids[] = (int)$drilldown->dashboard->id; // for backwards compatibility
                } else {
                    $drilldown_dashboard_ids[] = (int)$drilldown->dashboard;
                }
            }
        }
        $drilldown_dashboard_ids = array_unique($drilldown_dashboard_ids);
        $display_dashboard_ids = array_diff($dashboard_ids, $drilldown_dashboard_ids);
        $display_dashboards = array();
        foreach ( $dashboards as $d ) {
            if ( in_array($d->nid,$display_dashboard_ids) ) {
                $display_dashboards[] = $d;
            }
        }
        // if initial dashboard is a drilldown dashboard, load first non-drilldown dashboard instead
        if ( in_array($dashboard->nid, $drilldown_dashboard_ids) && empty($_GET['id']) ) {
            $dashboardKeys = array_keys($display_dashboard_ids);
            $dashboard = $dashboards[array_shift($dashboardKeys)];
        }
    }

    // force a dashboard id into the url for javascript libs
    // TODO doing a redirect is wasteful, find some other way
    if ( empty($_GET['id']) &&  isset($dashboard) ) {
        drupal_goto('dashboards',array('query'=>array('id'=>$dashboard->nid)));
    }

    foreach ( $datasources as $k => $ds ) {
        if ( $ds->name == gd_datasource_get_active() ) {
            $datasources[$k]->active = true;
        }
    }

    return array($datasources, $dashboard, $display_dashboards);
}