FrxMenu.inc 7.46 KB
Newer Older
metzlerd's avatar
metzlerd committed
1
<?php
metzlerd's avatar
metzlerd committed
2
3
4
5
6
7
/**
 * @file FrxMenu.inc
 * Drupal menu builder
 * @author davidmetzler
 *
 */
8
class FrxMenu {
metzlerd's avatar
metzlerd committed
9
10
11
12
13
  public $doc_defaults;  // Default Dcoument formats to inlcude
  public $doc_formats;  // Supported document formats
  public $name; // Report name wihout frx extention
  public $language; // Language
  public $format;  //format of report
14
15
16
  public $filename; //Name of file;
  public $ext; // Extention of file to be returned.
  public $directory; // name of directory;
metzlerd's avatar
metzlerd committed
17
18
  public $link;   //Link to report in current language
  public $i_Link; //Link to language chaning report name
19
  private $teng;
metzlerd's avatar
metzlerd committed
20

21
  public function __construct() {
22

metzlerd's avatar
metzlerd committed
23
    $docs = variable_get('forena_doc_defaults', array());
24

metzlerd's avatar
metzlerd committed
25
    // Load settings array into normal array
metzlerd's avatar
metzlerd committed
26
    if ($docs) foreach ($docs as $doc => $enabled) {
27
      if ($enabled) $this->doc_defaults[] = $doc;
metzlerd's avatar
metzlerd committed
28
29
    }
    else {
30
      $this->doc_formats = array('web');
metzlerd's avatar
metzlerd committed
31
    }
32

metzlerd's avatar
metzlerd committed
33
    // Load settings array into normal array
34
35

    $docs = variable_get('forena_doc_formats', Frx::documentTypes(TRUE));
metzlerd's avatar
metzlerd committed
36
    if ($docs) foreach ($docs as $doc => $enabled) {
37
      if ($enabled) $this->doc_formats[] = $doc;
metzlerd's avatar
metzlerd committed
38
    }
39

40
    $this->teng = Frx::SyntaxEngine(FRX_SQL_TOKEN, ':');
metzlerd's avatar
metzlerd committed
41
42

  }
43

metzlerd's avatar
metzlerd committed
44
  /**
45
   * Convert url into file paths based report name.  Load all link data for the
metzlerd's avatar
metzlerd committed
46
   * reprot.  Most report urls look like a java lassname, so urls are of the form
47
48
49
50
   * lang.subdir.anothersubdir.report.doctype.  This function parses the url
   * into it's components and store them in the menu object so that we can use
   * this name.
   * @param $url path style name
metzlerd's avatar
metzlerd committed
51
52
   */
  public function parseURL($url) {
metzlerd's avatar
metzlerd committed
53
54
    global $language;
    $name = $url;
metzlerd's avatar
metzlerd committed
55
    $tlang = '';
metzlerd's avatar
metzlerd committed
56
57
    // Determine if the report has an extention that is one of the docuemnt types
    $p = pathinfo($url);
58
59
60
    if (isset($p['extension']) && array_search($p['extension'], $this->doc_formats)!==FALSE) {
      $name = $p['filename'];
      $format = $p['extension'];
metzlerd's avatar
metzlerd committed
61
62
    }
    else {
63
64
      $format = 'web';
      $ext = '';
metzlerd's avatar
metzlerd committed
65
    }
66
67
68
69
70
71
72

    // Convert class names to directory names.
    $base_name = str_replace('.', '/', $name);
    $name = $base_name;
    $i_name = $base_name;

    // Determine lanugage from url or drupal language interface.
metzlerd's avatar
metzlerd committed
73
    $lang = $language->language;
metzlerd's avatar
metzlerd committed
74
    if (module_exists('locale')) {
75

metzlerd's avatar
metzlerd committed
76
77
78
79
80
81
82
83
84
      //First check to see if the report allready has a language in it
      @list($tlang,  $tbase_name) = explode('/', $base_name, 2);
      // FInd out if the starting name of the report is an installed language.
      $lang_list = language_list();
      if (array_key_exists($tlang, $lang_list )) {
        $base_name = $tbase_name;
        if ($lang != $tlang) {
          $lang = $tlang;
          $language = $lang_list[$lang];
85
          $i_name = $tlang . '/' . $base_name;
metzlerd's avatar
metzlerd committed
86
87
88
89
90
91
92
93
94
95
        }
        if ($tlang == 'en') {
          $name = $base_name = $tbase_name;
        }
      }
      else {
        if ($lang != 'en') {
          $name = $lang . '/' . $name;
        }
      }
metzlerd's avatar
metzlerd committed
96
    }
97

metzlerd's avatar
metzlerd committed
98
    //$name = trim(str_replace('.', '/', $base_name), '/');
99
100
    $filename = $name . '.frx';

metzlerd's avatar
metzlerd committed
101
    $desc['name'] = $this->name = $name;
102
103
104
105
    $desc['directory'] = Frx::File()->directory($filename);
    $desc['filename'] = Frx::File()->path($filename);
    $desc['base_name'] = $this->base_name = $base_name;
    $desc['exists'] = Frx::File()->exists($filename);
metzlerd's avatar
metzlerd committed
106
    $desc['link'] = $this->link = 'reports/' . str_replace('/' , '.', $name);
107
    $desc['i_link']= 'reports/' . $this->i_link = str_replace('/', '.', $i_name);
metzlerd's avatar
metzlerd committed
108
    $desc['language'] = $this->language = $lang;
109
110
111
    $desc['format'] = $this->format = $format;

    return $desc;
metzlerd's avatar
metzlerd committed
112
  }
113

metzlerd's avatar
metzlerd committed
114
  /**
115
   * Generate dcoument links based on report name.
metzlerd's avatar
metzlerd committed
116
117
   * @param unknown_type $docs
   */
118
119
120
121
  public function doclinks($docs = array()) {
    // Default documents.
    if (!$docs) {
      $docs = $this->doc_defaults;
metzlerd's avatar
metzlerd committed
122
123
    }
  }
124

125
126
127
128
  /**
   * Extract tokens from path
   * @param $path string path with Frx Tokens in them
   */
129
  public function tokens($path) {
metzlerd's avatar
metzlerd committed
130
    return $this->teng->tokens($path);
131
  }
132
133


134
135
  /**
   * Add menu items to the items array
136
   * @param $items array of menu items.
137
   */
138
  public function addMenuItems(&$items) {
139
    GLOBAL $language;
140

141
142
143
144
145
146
147
148
149
    $result = db_query("SELECT report_name, path, title, cache FROM {forena_reports} where path<>'' AND language='en' ORDER BY path asc");
    $reports = array();
    foreach ($result AS $row) {
      $access = TRUE;
      $cache = $row->cache;
      if ($cache) {

        $cache = unserialize($cache);
        // Load menu item defaults
150
        $menu = @$cache['menu'];
151
        $path = $menu['path'];
152
        $path_args = @$menu['args'];
153
154
        $type = @$menu['type'];
        $title = @$menu['title'] ? $menu['title']:  $row->title;
155
156
157
        if (module_exists('locale')) {

        }
158

159
        //Default type
160
161
162
163
164
165
166
167
168
        switch ($type) {
          case 'normal-item':
            $menu_type = MENU_NORMAL_ITEM;
            break;
          case 'default-local-task':
            $menu_type = MENU_DEFAULT_LOCAL_TASK;
            break;
          case 'local-task':
            $menu_type = MENU_LOCAL_TASK;
169
            break;
170
171
          default:
            $menu_type = MENU_CALLBACK;
172
        }
173

174
        //Replace the tokens with drupal menu wildcards
metzlerd's avatar
metzlerd committed
175
176
177
178
179
180
        $tokens = $this->tokens($path);
        $new_path = $path;
        foreach ($tokens as $i => $token) {
          $new_path = str_replace(':' . $token, '%', $new_path);
          $args[] = $i;
        }
181
        // Now generate the callback arguments
metzlerd's avatar
metzlerd committed
182
183
        $parts = explode( '/', $new_path);
        $page_args = array_keys($parts, '%');
184
185
        $path_args = $path_args ? rtrim($path,'/') . '/' . ltrim($path_args, '/') : $path;
        $page_args = array_merge(array($path_args, $row->report_name), $page_args);
186

187
        // Set the access callback
188
        $access_callback = isset($cache['access']) ? 'forena_check_all_access' : TRUE;
189

190
        if ($menu_type == MENU_DEFAULT_LOCAL_TASK) {
metzlerd's avatar
metzlerd committed
191
          $parts = explode('/', $new_path);
192
193
194
          array_pop($parts);
          $parent_path = implode('/', $parts);
          // build the parent menu because we are also building the local task
195
          // but onlu do so if another report doesn't define the parent.
metzlerd's avatar
metzlerd committed
196

197
          if (!isset($items[$parent_path])) {
198
            $items[$parent_path] = array(
metzlerd's avatar
metzlerd committed
199
             'type' => MENU_CALLBACK,
200
201
             'title' => $row->title,
             'access callback' => $access_callback,
202
             'access arguments' => array($cache['access']),
203
204
205
             'page callback' => 'forena_report_menu_callback',
             'page arguments' => $page_args,
            );
206
207
208
209
            if (module_exists('locale')) {
              $items[$parent_path]['title callback'] = 'forena_report_title_callback';
              $items[$parent_path]['title arguments'] = array($row->report_name, FALSE);
            }
210
211
            if ($access_callback === 'forena_check_all_access') $items[$parent_path]['access arguments'][] = $cache['access'];
          }
212
        }
213

metzlerd's avatar
metzlerd committed
214
215
216
217
        $items[$new_path] = array(
          'type' => $menu_type,
          'title' => $title,
          'access callback' => $access_callback,
218
          'access arguments' => array(@$cache['access']),
metzlerd's avatar
metzlerd committed
219
220
221
          'page callback' => 'forena_report_menu_callback',
          'page arguments' => $page_args,
        );
222
223
        if (module_exists('locale')) {
          $items[$new_path]['title callback'] = 'forena_report_title_callback';
metzlerd's avatar
metzlerd committed
224
          $items[$new_path]['title arguments'] = array($row->report_name, TRUE);
225
        }
metzlerd's avatar
metzlerd committed
226
        if ($access_callback === 'forena_check_all_access') $items[$new_path]['access arguments'][] = $cache['access'];
227
228
      }
    }
229

230
  }
231
232


metzlerd's avatar
metzlerd committed
233
}