Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/controllers/components/amo.php
blob: a8b61b4a2d99d776a4cb6a4c6be099389862776f (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is addons.mozilla.org site.
 *
 * The Initial Developer of the Original Code is
 * Justin Scott <fligtar@gmail.com>.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Frederic Wenzel <fwenzel@mozilla.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
class AmoComponent extends Object {
    var $controller;
    var $platforms;
    var $applications;
    var $versionIds;
    var $navCategories;

   /**
    * Save a reference to the controller on startup
    * @param object &$controller the controller using this component
    */
    function startup(&$controller) {
        $this->controller =& $controller;
    }
    
   /**
    * Checks if user has permissions for an addon
    * @param int $id the add-on id
    * @param array $addonInfo array of add-on data so we don't have to pull it
    * @param bool $requireOwner whether we're checking for actual ownership
    */
    function checkOwnership($id, $addonInfo = array(), $requireOwner = false) {
        $session = $this->controller->Session->read('User');
        if (empty($session['id'])) return false;
        
        //Check if user is an admin
        if ($this->controller->SimpleAcl->actionAllowed('Admin', 'EditAnyAddon', $session) && !$requireOwner) {
            return true;
        }
        
        //See if add-on data was passed; if not, retrieve it
        if (empty($addonInfo['status'])) {
            $addon = $this->controller->Addon->findById($id, null, null, -1);
            $addonInfo = $addon['Addon'];
        }
        
        //Check if add-on is disabled
        if ($addonInfo['status'] == STATUS_DISABLED) {
            return false;
        }
        
        //check if user is an author of the add-on
        if ($this->controller->Addon->query("SELECT * FROM addons_users WHERE addon_id={$id} AND user_id={$session['id']}")) {
            return true;
        }
        else {
            return false;
        }
    }
    
    /**
     * Gets the author role of the current user for the given add-on
     * @param int $addon_id id of the add-on
     */
    function getAuthorRole($addon_id) {
        $session = $this->controller->Session->read('User');
        if (empty($session['id'])) return AUTHOR_ROLE_NONE;
        
        // Get role from database
        $role = $this->controller->Addon->query("SELECT role FROM addons_users WHERE addon_id={$addon_id} AND user_id={$session['id']}");
        if (!empty($role)) {
            $role = $role[0]['addons_users']['role'];
        }
        
        // Check if user has permissions to edit any add-on
        if ($this->controller->SimpleAcl->actionAllowed('Admin', 'EditAnyAddon', $session)) {
            if ($role == AUTHOR_ROLE_OWNER) {
                return AUTHOR_ROLE_ADMINOWNER;
            }
            else {
                return AUTHOR_ROLE_ADMIN;
            }
        }
        
        // Check if add-on is disabled
        $status = $this->controller->Addon->findById($addon_id, array('Addon.status'), null, -1);
        if (!empty($status['Addon']['status'])) {
            if ($status['Addon']['status'] == STATUS_DISABLED) {
                return AUTHOR_ROLE_NONE;
            }
        }
        
        // If not an admin and not disabled, return db role if we found one
        if (!empty($role)) {
            return $role;
        }
        
        return AUTHOR_ROLE_NONE;
    }
    
   /**
    * Cleans an array or string for SQL and HTML, by reference
    *
    * @param mixed &$subject
    */
    function clean(&$subject, $stripTags = true) {
        if (isset($subject)) {
            if (is_array($subject)) {
                foreach ($subject as $k => $v) {
                    $this->clean($subject[$k], $stripTags);
                }
            }
            else {
                if ($stripTags)
                    $subject = strip_tags($subject);
                $subject = mysql_real_escape_string($subject);
            }
        }
    }

   /**
    * Strips slashes from subject in return (NOT by reference)
    * @param string $subject
    */
    function unclean($subject) {
        if (is_array($subject)) {
            $uncleaned = array();
            foreach ($subject as $k => $v) {
                $uncleaned[$k] = $this->unclean($v);
            }
        } else {
            $uncleaned = stripslashes($subject);
        }
        
        return $uncleaned;
    }

   /**
    * Ensures that any quotes are turned into entities
    * @param mixed &$subject
    */
    function convertQuotes(&$subject) {
        if (isset($subject)) {
            if (is_array($subject)) {
                foreach ($subject as $k => $v) {
                    $this->convertQuotes($subject[$k]);
                }
            }
            else {
                $subject = str_replace("'", "&#39;", $subject);
            }
        }
    }
    
   /**
    * Returns a string representation of the Approval Status id
    * @param int $status
    * @deprecated since 3.5 - use Amo->getStatusNames()
    */
    function getApprovalStatus($status = '') {
        //If a status id is specified, return the string
        if ($status != '') {
            switch ($status) {
                case STATUS_NULL:       $string = ___('addons_status_incomplete', 'Incomplete Version');
                                        break;
                case STATUS_SANDBOX:    $string = ___('addons_status_sandbox', 'In Sandbox');
                                        break;
                case STATUS_PENDING:    $string = ___('addons_status_pending', 'In Sandbox; Pending Review');
                                        break;
                case STATUS_NOMINATED:  $string = ___('addons_status_nominated', 'In Sandbox; Public Nomination');
                                        break;
                case STATUS_PUBLIC:     $string = ___('addons_status_public', 'Public');
                                        break;
                case STATUS_DISABLED:   $string = ___('addons_status_disabled', 'Disabled');
                                        break;
                default:                $string = ___('addons_status_unknown', 'Unknown');
                                        break;
            }
            return $string;
        }
        else {
            //If no status was passed, return an array of all statuses
            $array = array();
            for ($s = 0; $s <= 6; $s++) {
                //The id must be passed as a string
                $array[$s] = $this->getApprovalStatus("$s");
            }
            return $array;
        }
    }
    
    /**
     * Returns an array of possible add-on and file statuses and names
     */
    function getStatusNames() {
        return array(
            STATUS_NULL      => ___('addons_status_incomplete', 'Incomplete'),
            STATUS_SANDBOX   => ___('addons_status_sandbox', 'In Sandbox'),
            STATUS_PENDING   => ___('addons_status_pending', 'In Sandbox; Pending Review'),
            STATUS_NOMINATED => ___('addons_status_nominated', 'In Sandbox; Public Nomination'),
            STATUS_PUBLIC    => ___('addons_status_public', 'Public'),
            STATUS_DISABLED  => ___('addons_status_disabled', 'Admin Disabled')
        );
    }
    
   /**
    * Returns the name of a platform by Id, or an array of platforms
    * The purpose of this is to reduce unnecessary database queries
    * @param int $platform
    * @deprecated since 3.5 - use Platform->getNames()
    */
    function getPlatformName($platform = '', $shortnames = false) {
        //If platform id is set, get the name for it
        if ($platform != '') {
            //If the array of platforms was already retrieved, use it
            if (!empty($this->platforms)) {
                return $this->platforms[$platform];
            }
            //Otherwise, retrieve the array and save it
            else {
                $this->platforms = $this->getPlatformName();
                return $this->platforms[$platform];
            }
        }
        //If no id is set, return the array of platforms
        else {
            $model =& new Platform();
            $model->useDbConfig = 'shadow';
            $platforms = $model->findAll(null, null, null, null, null, -1);

            $platformArray = array();
            foreach ($platforms as $platform) {
                if ($shortnames == true) {
                    $platformArray[$platform['Platform']['id']]['name'] = $platform['Translation']['name']['string'];
                    $platformArray[$platform['Platform']['id']]['shortname'] = $platform['Translation']['shortname']['string'];
                }
                else {
                    $platformArray[$platform['Platform']['id']] = $platform['Translation']['name']['string'];
                }
            }

            return $platformArray;
        }
    }

   /**
    * Returns the name of an application by Id, or an array of applications
    * The purpose of this is to reduce unnecessary database queries
    * @param int $application
    */
    function getApplicationName($application = '', $shortnames = false) {
        //If application id is set, get the name for it
        if ($application != '') {
            //If the array of applications was already retrieved, use it
            if (!empty($this->applications)) {
                return $this->applications[$application];
            }
            //Otherwise, retrieve the array and save it
            else {
                $this->applications = $this->getApplicationName();
                return $this->applications[$application];
            }
        }
        //If no id is set, return the array of applications
        else {
            $model =& new Application();
            $model->useDbConfig = 'shadow';
            $applications = $model->findAll(null, null, null, null, null, -1);

            $applicationArray = array();
            foreach ($applications as $application) {
                if ($shortnames == true) {
                    $applicationArray[$application['Application']['id']]['name'] = $application['Translation']['name']['string'];
                    $applicationArray[$application['Application']['id']]['shortname'] = $application['Translation']['shortname']['string'];
                }
                else {
                    $applicationArray[$application['Application']['id']] = $application['Translation']['name']['string'];
                }
            }

            return $applicationArray;
        }
    }

   /**
    * Returns the versions of an application by Id, or an array versions for all applications
    * The purpose of this is again to reduce unnecessary database queries <br />
    * so is on practically every page (used in advance search in search view)
    * This code also has the side effect of memo-izing the version id's for each version of an application
    * @param int $application -- the application id to get versions for
    */
    function getApplicationVersions($application = '') {
        // If application id is set, get the name for it
        if ($application != '') {
            // If the array of applications was already retrieved, use it
            if (!empty($this->applications)) {
                return $this->applications[$application];
            }
            // Otherwise, retrieve the array and save it
            else {
                $this->applications = $this->getApplicationVersions();
                return $this->applications[$application];
            }
        }
        // If no id is set, return the array of applications
        else {
            $applicationModel =& new Application();
            $applicationModel->useDbConfig = 'shadow';
            
            loadComponent('Versioncompare');
            $versionCompare =& new VersioncompareComponent();

            $applicationModel->unbindModel(array('hasAndBelongsToMany' => array('Version'), 'hasMany' => array('Tag')));
            $applications = $applicationModel->findAll('Application.supported=1', null, null, null, null, 2);
            $appvids = array();
            foreach ($applications as $application) {    
                if (!empty($application['Appversion'])) {
                    $appversions = array();
                    //Change array structure for sorting
                    foreach ($application['Appversion'] as $appversion) {
                        $appversions[]['Appversion']['version'] = $appversion['version'];
                        $appvids[$appversion['application_id']][$appversion['version']] = $appversion['id'];
                    }
                    $versionCompare->sortAppversionArray($appversions);      
                    foreach ($appversions as $appversion) {
                        $versions[$application['Application']['id']][] = $appversion['Appversion']['version'];
                    }
                }
            }
            $this->versionIds = $appvids; 
            return $versions;
        }
    }
    
   /**
     * Returns the version => versionIds pairs corresponding to a particular application
     * It is used by the SearchController
     * @param $appid -- the application id of the application
     * @return -- arrays of version => versionIds pairs
     */
    function getVersionIdsByApp($appid) {
        if (!empty($this->versionIds)) {
            return $this->versionIds[$appid];
        } else {
            $this->getApplicationVersions();
            return $this->versionIds[$appid];
        }
    }
    
    /**
     * check if the user is logged in. If not, refer them to the login page,
     * optionally passing on where they wanted to go to in the first place.
     * @param string cake-relative path to refer to after login
     * @return mixed bool true if logged in, void otherwise 
     */
    function checkLoggedIn($whereTo = '') {
        $session = $this->controller->Session->read('User');
        if (!empty($session)) {
            return true;
        } else {
            if ($whereTo) {
                $_get_part = $whereTo;
            } else {
                $_get_part = $this->controller->params['url']['url'];
                // strip locale and app
                $_get_part = preg_replace('|^' . LANG . '/' . APP_SHORTNAME . '|', '', $_get_part);
            }
            $_get_part = '?to='.urlencode($_get_part);
            $this->controller->redirect('/users/login'.$_get_part);
            exit;
        }
    }
    
   /**
    * Returns information on the min and max versions for a version because Cake
    * does not consider non-key fields in HasAndBelongsToMany tables.
    * @param int $version The version id
    * @return array
    * @deprecated since 3.5 - use Version->getCompatibleApps()
    */
    function getMinMaxVersions($version) {
        return $this->controller->Application->query("
            SELECT * FROM `applications_versions`
            LEFT JOIN `applications` ON `applications_versions`.`application_id`=`applications`.`id`
            JOIN `translations` ON `applications`.`name`=`translations`.`id`
            JOIN `appversions` AS `min` ON `applications_versions`.`min`=`min`.`id`
            JOIN `appversions` AS `max` ON `applications_versions`.`max`=`max`.`id`
            WHERE
                `applications_versions`.`version_id`='{$version}' AND
                `translations`.`locale`='en-US'
        ",true);
    }

   /**
    * Updates min/max version information manually
    * @param int $version The version id
    * @param array $data The array of targetApp information
    * @return boolean true
    * @deprecated since 3.5 - use Version->saveCompatibleApps()
    */
    function saveMinMaxVersions($version, $data) {
        if (!empty($data)) {
            foreach ($data['id'] as $id => $application_id) {
                $this->controller->Application->execute("
                    UPDATE `applications_versions` SET
                        `min`='{$data['minVersion'][$id]}',
                        `max`='{$data['maxVersion'][$id]}'
                    WHERE
                        `application_id`='{$application_id}' AND
                        `version_id`='{$version}'
                    LIMIT 1
                ");
            }
        }

        return true;
    }
    
   /**
    * Return the install trigger string for the specified addontype
    * @param int $addontype The addontype of the file
    * @param string $uri The url of the file
    * @param string $name The name of the item
    * @param string $icon The url of the icon
    * @param string $hash The hash of the file
    */
    function installTrigger($addontype, $uri, $name = '', $icon = '', $hash = '') {
        $xpi = array(ADDON_EXTENSION, ADDON_DICT, ADDON_LPAPP, ADDON_LPADDON);
        $chrome = array(ADDON_THEME);
        $search = array(ADDON_SEARCH);
        
        $uri = str_replace("'", "\'", $uri);
        $name = str_replace("'", "\'", $name);
        $icon = str_replace("'", "\'", $icon);
        $hash = str_replace("'", "\'", $hash);
        
        if (in_array($addontype, $xpi)) {
            return "InstallTrigger.install('{$uri}', '{$name}', '{$icon}', '{$hash}');";
        }
        elseif (in_array($addontype, $chrome)) {
            return "InstallTrigger.installChrome(InstallTrigger.SKIN, '{$uri}', '{$name}');";
        }
        elseif (in_array($addontype, $search)) {
            return "window.external.AddSearchProvider('{$uri}');";
        }
    }

   /**
    * Copies an array, returning only specifically allowed fields to be saved
    * @param array $array the array to filter
    * @param array $allowedFields the fields to be allowed
    * @param array $disallowedFields the fields disallowed
    * @return array
    */
    function filterFields($array, $allowedFields = array(), $disallowedFields = array()) {
        $newArray = array();

        if (!empty($array)) {
            foreach ($array as $k => $item) {
                if ((empty($allowedFields) || in_array($k, $allowedFields, true)) && !in_array($k, $disallowedFields, true)) {
                    if (is_array($item)) {
                        $newArray[$k] = $this->filterFields($item, $allowedFields, $disallowedFields);
                    }
                    else {
                        $newArray[$k] = $item;
                    }
                }
            }
        }

        return $newArray;
    }
    
    /**
     * @deprecated
     */
    function LEGACY_describeVersionStatus($files) {
        if (count($files) == 0) {
            return ___('error_no_files_in_addon', 'Incomplete - No Files');
        }
        elseif (count($files) == 1) {
            return $this->getApprovalStatus($files[0]['File']['status']);
        }
        else {
            $statuses = array();
            foreach ($files as $file) {
                if (empty($statuses[$file['File']['status']])) {
                    $statuses[$file['File']['status']] = 1;
                }
                else {
                    $statuses[$file['File']['status']]++;
                }
            }
            if (count($statuses) == 1) {

            }

            return 'Multiple Files';
        }
    }
    
    function describeVersionStatus($files) {
        if (count($files) == 0) {
            return ___('error_no_files_in_addon', 'No Files');
        }
        else {
            $statuses = $this->getStatusNames();
            $fileStatuses = array();
            $counts = array();
            
            foreach ($files as $file) {
                if (!empty($fileStatuses[$file['status']])) {
                    $fileStatuses[$file['status']]++;
                }
                else {
                    $fileStatuses[$file['status']] = 1;
                }
            }
            
            foreach ($fileStatuses as $status => $count) {
                if ($count > 1) {
                    $counts[] = "{$count} {$statuses[$status]} files";
                }
                else {
                    $counts[] = "{$count} {$statuses[$status]} file";
                }
            }
            
            return implode('; ', $counts);
        }
    }
    
   /**
    * Returns the date in MySQL NOW() format
    */
    function getNOW() {
        return date('Y-m-d H:i:s');
    }
    
   /**
    * Copy a file to the rsync location for updates
    * @param int $addon_id the add-on id
    * @param string $filename the filename
    * @param boolean $overwrite whether to overwrite the destination file
    * @return boolean
    */
    function copyFileToPublic($addon_id, $filename, $overwrite = true) {
        // Only copy if the path has been defined
        if (!defined('PUBLIC_STAGING_PATH')) {
            // return true because false indicates error
            return true;
        }
        
        $currentFile = REPO_PATH."/{$addon_id}/{$filename}";
        $newDir = PUBLIC_STAGING_PATH."/{$addon_id}";
        $newFile = $newDir."/{$filename}";
        
        // Make sure source file exists
        if (!file_exists($currentFile)) {
            return false;
        }
        
        // If we don't want to overwrite, make sure we don't
        if (!$overwrite && file_exists($newFile)) {
            // return true because this is not treated as an error
            return false;
        }
        
        // Make directory if necessary
        if (!file_exists($newDir)) {
            if (!mkdir($newDir)) {
                return false;
            }
        }
        
        return copy($currentFile, $newFile);
    }
    
    function accessDenied() {
        header('HTTP/1.1 401 Unauthorized');
        
        $this->controller->layout = 'mozilla';
        $this->controller->pageTitle = _('error_access_denied') . ' :: ' . sprintf(_('addons_home_pagetitle'), APP_PRETTYNAME);
        $this->controller->set('breadcrumbs', _('error_access_denied'));
        $this->controller->set('subpagetitle', _('error_access_denied'));
        $this->controller->viewPath = 'errors';            
        $this->controller->render('error401');
        
        exit;
    }
    
   /**
    * Logs detailed information to a specific logfile
    * @param string $message Log message
    * @param bool $dumpData whether to dump the controller's data in the entry
    * @deprecated
    */
    function detailedLog($message, $dumpData = true) {
        if (!defined('DETAILED_LOG_PATH')) {
            return false;
        }
        
        $logfile = DETAILED_LOG_PATH.'/'.date('Y-m-d');
        
        $fp = fopen($logfile, 'a');
        fwrite($fp, "[".date('r')."] (".php_uname('n').") {$message}\n");
        if ($dumpData) {
            fwrite($fp, print_r($this->controller->data, true)."\n");
        }
        fclose($fp);
    }


    /**
     * Get a list of tags in alphabetical order.
     */
    function getTags($app=APP_ID,$type=ADDON_EXTENSION) {

        if (!isset($this->controller->Tag)) {
            loadModel('Tag');
            $this->controller->Tag =& new Tag();
            // for CakePHP 1.2 this would be:
            // $this->controller->loadModel('Tag');
        }
    
        $this->controller->Tag->unbindFully();
        
        return $this->controller->Tag->findAll(
            array(
                'application_id' => $app,
                'addontype_id'   => $type
            ),
            null,
            'Tag.weight, Translation.name'
        );
    }
    
    /**
     * Get categories/addon types list for global navigation menu
     * @return array Category list, style:
     * array(
     *   array(name=>"abc", link="browse/type:1/cat:1"),
     *   ...
     * )
     */
    function getNavCategories() {
        global $hybrid_categories, $app_listedtypes;
        
        if (!empty($this->navCategories)) return $this->navCategories;
        
        if(APP_ID == APP_SUGAR) {
            $catlist = array();
        } else
        // addon type list to be added to regular categories list
        // @partial translation fallback, 4/23/08
        if (_('nav_category_dicts_langpacks') != 'nav_category_dicts_langpacks') {
            $catlist = array(
                array('name' => _('nav_category_dicts_langpacks'),
                      'type' => ADDON_DICT,
                      'cat' => 0,
                      'weight' => 0),
                array('name' => _('nav_category_themes'),
                      'type' => ADDON_THEME,
                      'cat' => 0,
                      'weight' => 0)
            );
        } else {
            // get "Themes" category name
            if (!isset($this->controller->Addontype)) {
                loadModel('Addontype');
                $this->controller->Addontype = new Addontype();
            }

            $_themes_name = $this->controller->Addontype->getName(ADDON_THEME);
            
            $catlist = array(
                array('name' => _('langtools_header_dicts_and_langpacks'),
                      'type' => ADDON_DICT,
                      'cat' => 0,
                      'weight' => 0),
                array('name' => $_themes_name,
                      'type' => ADDON_THEME,
                      'cat' => 0,
                      'weight' => 0)
            );
        }
        
        // add plugins where appropriate
        // @partial translation fallback, 4/27/08
        if (in_array(ADDON_PLUGIN, $app_listedtypes[APP_ID])) {
            if (_('nav_category_plugins') != 'nav_category_plugins') {
                $catlist[] = array(
                    'name' => _('nav_category_plugins'),
                    'type' => ADDON_PLUGIN,
                    'cat' => 0,
                    'weight' => 0
                );
            } else {
                $catlist[] = array(
                    'name' => _('addons_plugins_pagetitle'),
                    'type' => ADDON_PLUGIN,
                    'cat' => 0,
                    'weight' => 0
                );
            }
        }
        
        // create two sort arrays that we can use with array_multisort later
        $_weights = array();
        $_names = array();
        foreach ($catlist as $_item) {
            $_weights[] = $_item['weight'];
            $_names[] = strtolower($_item['name']);
        }
        
        // add regular categories to list
        $tags = $this->getTags(APP_ID, ADDON_ACTIVITY);
        foreach ($tags as $_tag) {
            /* support hybrid categories */
            if (isset($hybrid_categories[APP_ID][$_tag['Tag']['id']])) {
                $_type = $hybrid_categories[APP_ID][$_tag['Tag']['id']];
                $_cat = 0;
            } else {
                $_type = $_tag['Tag']['addontype_id'];
                $_cat = $_tag['Tag']['id'];
            }
            
            $_name = $_tag['Translation']['name']['string'];
            $_weight = $_tag['Tag']['weight'];
            
            // add item to results array
            $catlist[] = array(
                'name' => $_name,
                'type' => $_type,
                'cat' => $_cat,
                'weight' => $_weight
            );
            
            // add item to sort arrays too
            $_names[] = strtolower($_name);
            $_weights[] = $_weight;
        }
        
        // sort results array by weight and name, then return.
        array_multisort($_weights, SORT_ASC, SORT_NUMERIC,
                        $_names, SORT_ASC, SORT_STRING, $catlist);
        $this->navCategories = $catlist; // cache result for subsequent calls
        return $catlist;
    }
}
?>