public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
midcom / midcom_core / helpers / toolbar.php
100644 306 lines (276 sloc) 9.916 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* @package midcom_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
 
/**
* Toolbar helper
*
* @package midcom_core
*/
class midcom_core_helpers_toolbar
{
    /**
* The CSS class that should be used for the toolbar.
* Set to null if non should be used.
*
* @var string
*/
    public $css_class;
 
    /**
* The items in the toolbar.
*
* The array consists of Arrays outlined in the class introduction.
* You can modify existing items in this collection but you should use
* the class methods to add or delete existing items. Also note that
* relative URLs are processed upon the invocation of add_item(), if
* you change URL manually, you have to ensure a valid URL by yourself
* or use update_item_url, which is recommended.
*
* @var Array
*/
    public $items;
    
    public $holder_attributes;
    
    /**
* Basic constructor, initializes the class and sets defaults for the
* CSS style if omitted.
*
* Note that the styles can be changed after construction by updating
* the css_class members.
*
* @param string $css_class The css class for the UL.
*/
    public function __construct($css_class = 'midcom_toolbar', $holder_attributes = '')
    {
        $this->css_class = $css_class;
        $this->holder_attributes = $holder_attributes;
        $this->items = array();
        
        $this->initialize();
    }
    
    protected function initialize() {}
    
    public function get_section_items($section=MIDCOM_TOOLBAR_NODE)
    {
        if ( !array_key_exists($section, $this->items)
            || !is_array($this->items[$section]))
        {
            return array();
        }
        
        return $this->items[$section];
    }
    
    /**
* This function will add an Item to the toolbar.
*
* Set before to the index of the element before which you want to insert
* the item or use -1 if you want to append an item. Alternatively,
* instead of specifying an index, you can specify a URL instead.
*
* This member will process the URL and append the anchor prefix in case
* the URL is a relative one.
*
* Invalid positions will result in a MidCOM Error.
*
* @param Array $item The item to add.
* @param mixed $before The index before which the item should be inserted.
* Use -1 for appending at the end, use a string to insert
* it before a URL, an integer will insert it before a
* given index.
* @see midcom_helper_toolbar::get_index_from_url()
* @see midcom_helper_toolbar::check_index()
* @see midcom_helper_toolbar::clean_item()
*/
    public function add_item($section=MIDCOM_TOOLBAR_NODE, $item, $before = -1)
    {
        if ( !array_key_exists($section, $this->items)
            || !is_array($this->items[$section]))
        {
            $this->items[$section] = array();
        }
        
        if ($before != -1)
        {
            $before = $this->check_index($section, $before, false);
        }
        
        $item = $this->clean_item($item);
 
        if ($before == -1)
        {
            $this->items[$section][] = $item;
        }
        else if ($before == 0)
        {
            array_unshift($this->items[$section], $item);
        }
        else
        {
            $start = array_slice($this->items[$section], 0, $before - 1);
            $start[] = $item;
            $this->items[$section] = array_merge($start, array_slice($this->items[$section], $before));
        }
    }
    
    /**
* Clean up an item that is added, making sure that the item has all the
* needed options and indexes.
* @param array the item to be cleaned
* @return array the cleaned item.
* @access public
*/
    public function clean_item($item)
    {
        static $used_access_keys = array();
        
        $item[MIDCOM_TOOLBAR__ORIGINAL_URL] = $item[MIDCOM_TOOLBAR_URL];
        if (! array_key_exists(MIDCOM_TOOLBAR_OPTIONS, $item))
        {
            $item[MIDCOM_TOOLBAR_OPTIONS] = array();
        }
        if (! array_key_exists(MIDCOM_TOOLBAR_HIDDEN, $item))
        {
            $item[MIDCOM_TOOLBAR_HIDDEN] = false;
        }
        if (! array_key_exists(MIDCOM_TOOLBAR_HELPTEXT, $item))
        {
            $item[MIDCOM_TOOLBAR_HELPTEXT] = '';
        }
        if (! array_key_exists(MIDCOM_TOOLBAR_ICON, $item))
        {
            $item[MIDCOM_TOOLBAR_ICON] = false;
        }
        else if($item[MIDCOM_TOOLBAR_ICON])
        {
            $item[MIDCOM_TOOLBAR_ICONURL] = MIDCOM_STATIC_URL . "/{$item[MIDCOM_TOOLBAR_ICON]}";
        }
        if (! array_key_exists(MIDCOM_TOOLBAR_ENABLED, $item))
        {
            $item[MIDCOM_TOOLBAR_ENABLED] = true;
        }
 
        if (! array_key_exists(MIDCOM_TOOLBAR_POST, $item))
        {
            $item[MIDCOM_TOOLBAR_POST] = false;
        }
        if (! array_key_exists(MIDCOM_TOOLBAR_POST_HIDDENARGS, $item))
        {
            $item[MIDCOM_TOOLBAR_POST_HIDDENARGS] = array();
        }
 
        // Check that access keys get registered only once
        if ( ! array_key_exists(MIDCOM_TOOLBAR_ACCESSKEY, $item)
            || array_key_exists($item[MIDCOM_TOOLBAR_ACCESSKEY], $used_access_keys))
        {
            $item[MIDCOM_TOOLBAR_ACCESSKEY] = null;
        }
        else
        {
            // We have valid access key, add it to help text
            if (strstr($_SERVER['HTTP_USER_AGENT'], 'Macintosh'))
            {
                // Mac users
                $hotkey = 'Ctrl-' . strtoupper($item[MIDCOM_TOOLBAR_ACCESSKEY]);
            }
            else
            {
                // Windows and Linux clients
                $hotkey = 'Alt-' . strtoupper($item[MIDCOM_TOOLBAR_ACCESSKEY]);
            }
 
            if ($item[MIDCOM_TOOLBAR_HELPTEXT] == '')
            {
                $item[MIDCOM_TOOLBAR_HELPTEXT] = $hotkey;
            }
            else
            {
                $item[MIDCOM_TOOLBAR_HELPTEXT] .= " ({$hotkey})";
            }
        }
 
        // Some items may want to keep their links unmutilated
        $direct_link = false;
        if ( array_key_exists(MIDCOM_TOOLBAR_OPTIONS, $item)
            && array_key_exists("rel", $item[MIDCOM_TOOLBAR_OPTIONS])
            && $item[MIDCOM_TOOLBAR_OPTIONS]["rel"] == "directlink")
        {
            $direct_link = true;
        }
 
        if (! $direct_link
            && substr($item[MIDCOM_TOOLBAR_URL], 0, 1) != '/'
            && ! preg_match('|^https?://|', $item[MIDCOM_TOOLBAR_URL]))
        {
            // $item[MIDCOM_TOOLBAR_URL] =
            // $_MIDCOM->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
            // . $item[MIDCOM_TOOLBAR_URL];
        }
        
        $item[MIDCOM_TOOLBAR_CLASSNAME] = 'disabled';
        if ($item[MIDCOM_TOOLBAR_ENABLED])
        {
            $item[MIDCOM_TOOLBAR_CLASSNAME] = 'enabled';
        }
        
        if (! array_key_exists(MIDCOM_TOOLBAR_HTMLLABEL, $item))
        {
            $item[MIDCOM_TOOLBAR_HTMLLABEL] = $item[MIDCOM_TOOLBAR_LABEL];
        }
        
        return $item;
    }
 
    /**
* This function will traverse all available items and return the first
* element whose URL matches the value passed to the function.
*
* Note, that if two items point to the same URL, only the first one
* will be reported.
*
* @param string $url The url to search in the list.
* @return int The index of the item or null, if not found.
*/
    public function get_index_from_url($section, $url)
    {
        for ($i = 0; $i < count ($this->items[$section]); $i++)
        {
            if ( $this->items[$section][$i][MIDCOM_TOOLBAR_URL] == $url
                || $this->items[$section][$i][MIDCOM_TOOLBAR__ORIGINAL_URL] == $url)
            {
                return $i;
            }
        }
        
        return null;
    }
    
    public function render() {}
    
    /**
* Private helper function which checks an index for validity.
* Upon any error, an Exception is thrown.
*
* It will automatically convert a string-based URL into an
* Index (if possible); if the URL can't be found, it will
* also trigger an error. The translated URL is returned by the
* function.
*
* @param mixed $index The integer index or URL to check
* @param boolean $raise_error Whether we should raise an error on missing item
* @return int $index The valid index (possibly translated from the URL) or null on missing index.
*/
    private function check_index($section=MIDCOM_TOOLBAR_NODE, $index, $raise_error = true)
    {
        if (is_string($index))
        {
            $url = $index;
            $index = $this->get_index_from_url($section, $url);
            if (is_null($index))
            {
                if ($raise_error)
                {
                    throw new Exception("midcom_core_helper_toolbar::check_index - Invalid URL '{$url}', URL not found.");
                    // This will exit.
                }
                else
                {
                    return null;
                }
            }
        }
        
        if ($index >= count($this->items[$section]))
        {
            throw new Exception("midcom_helper_toolbar::check_index - Invalid index {$index}, it is off-the-end.");
            // This will exit.
        }
        
        if ($index < 0)
        {
            throw new Exception("midcom_helper_toolbar::check_index - Invalid index {$index}, it is negative.");
            // This will exit.
        }
        
        return $index;
    }
}
 
?>