Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/views/elements/amo2009/search.thtml
blob: ba682b889e7c57ec25c8aefe7cd50c01690b6625 (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
<?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
 * The Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Mike Morgan <morgamic@mozilla.com> (Original Author)
 *   Cameron Roy <licensing@justcameron.com>
 *   Frederic Wenzel <fwenzel@mozilla.com>
 *   Chris Pollett <cpollett@gmail.com>
 *   l.m.orchard <lorchard@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 ***** */
?>
<?php
/**
 * This element uses the following local variables:
 * $query: query string to be displayed in the text box, defaults to "search for add-ons"
 * $category: category ID to be selected
 */

// set-up arrays for advance search select tags
// also set-up default values (or use previous search params) for each advance-search field
global $app_shortnames, $app_prettynames;
$applications = array();
foreach ($app_shortnames as $name => $key) {
    $applications[$key] =  $app_prettynames[$name];
}
if (!isset($appid)) {
    $appid = APP_ID;
}

$min_display_version_by_app = array( // eventually this information should be kept in DB
    APP_FIREFOX => "1.0",
    APP_THUNDERBIRD => "1.0",
    APP_SEAMONKEY => "1.0",
    APP_SUNBIRD => "0.2"
);

$new_versions = array();
foreach ($app_shortnames as $n => $k) {
// AmoVersions is from controller->beforeRender()
    if (!empty($AmoVersions[$k])) foreach ($AmoVersions[$k] as $version) {
        $versions_parts = explode(".", $version);
        $second_part = (isset($versions_parts[1])) ? ".".intval($versions_parts[1]) : "";
        if (!isset($min_display_version_by_app[$k])
            || $min_display_version_by_app[$k] <= $versions_parts[0].$second_part ) {

            if ($k != APP_FIREFOX || !in_array($versions_parts[0].$second_part, array('1.4','3.1'))) { // yuck. Baz didn't like FF 1.4.  Nick didn't like FF 3.1.
                $new_versions[$k][] = $versions_parts[0].$second_part;
            }
        }
    }
    if (!empty($new_versions[$k]))
        $new_versions[$k] = array_unique($new_versions[$k]);
}
$versions = $new_versions;

if(!isset($lver)) {
    $lver = "";
}

if(!isset($hver)) {
    $hver = "";
}

// AmoPlatforms is from controller->beforeRender()
array_shift($AmoPlatforms); // get rid of PLATFORM_ALL
$platforms = array_merge(array(PLATFORM_ANY => ___('advanced_search_form_any_type')), $AmoPlatforms); //add semantically happy PLATFORM_ANY
$pid = 0;
if (isset($this->params['url']['pid'])
    && in_array($this->params['url']['pid'], array_keys($platforms))) {

    $pid = $this->params['url']['pid'];
}

// AmoAddonTypes is from controller->beforeRender()
// for now we want to get rid of Plugin and Language packs since the former
// goes to a static page and the latter is empty
array_pop($AmoAddonTypes);
array_pop($AmoAddonTypes);
$addon_types = array_merge(array(ADDON_ANY => ___('advanced_search_form_any_type')), $AmoAddonTypes);
if (!isset($atype)) {
    $atype = ADDON_ANY;
}

$page_arr = $this->controller->Pagination->resultsPerPage;
$pages = array();
foreach ($page_arr as $page) {
    $pages[$page] = $page;
}

if (!isset($pp)) {
    $pp = $this->controller->Pagination->show;
}

$updates = array(
    "" => ___('advanced_search_form_any_time'),
    '- INTERVAL 1 DAY' => ___('advanced_search_form_past_day'),
    '- INTERVAL 1 WEEK' => ___('advanced_search_form_past_week'),
    '- INTERVAL 1 MONTH' => ___('advanced_search_form_past_month'),
    '- INTERVAL 3 MONTH' => ___('advanced_search_form_past_3_months'),
    '- INTERVAL 6 MONTH' => ___('advanced_search_form_past_6_months'),
    '- INTERVAL 1 YEAR' => ___('advanced_search_form_past_year')
);

$lup = "";
if (isset($this->params['url']['lup'])
    && in_array($this->params['url']['lup'], array_keys($updates))) {

    $lup = $this->params['url']['lup'];
}

$sort_orders = array(
    '' => ___('advanced_search_form_keyword_match'),
    'newest' => ___('advanced_search_form_newest'),
    'name' => ___('advanced_search_form_name'),
    'averagerating' => ___('advanced_search_form_rating'),
    'weeklydownloads' => ___('advanced_search_form_popularity')
);
if (!isset($sort)) {
    $sort = "";
}


// handles toggling advanced search form when JS not enabled

$toggle_uri = $_SERVER['REQUEST_URI'];

if (isset($this->params['url']['adv']) || isset($this->params['url']['as'])) {
    $toggle_uri = str_replace("adv=", "nor=", $toggle_uri);
    $toggle_uri = str_replace("as=", "nor=", $toggle_uri);
    $two_form = "";
} else {
    $toggle_uri = str_replace("nor=", "adv=", $toggle_uri);
    $delim = (stristr($toggle_uri,"?")) ? "&" : "?";
    if(!stristr($toggle_uri, "adv=")) { $toggle_uri .= "{$delim}adv=true"; }
    $two_form = "</form><form method=\"get\" action=\"\" id=\"hidden-form\" class=\"asclosed\">";
}
$toggle_uri = htmlspecialchars($toggle_uri);

// collection search flag
if (!isset($collectionSearch))
    $collectionSearch = false;

// prepare query string
if (!isset($query) || !is_string($query))
    $query = '';

// prepare selected category, "all" if not set
if (!isset($category)) $category = array(0, 0);
?>
<div class="search-form expanded-search-form">
    <form method="get" action="<?=$html->url("/search")?>" id="search-form">
        <div class="basic" id="search-query">
            <label for="query" class="query-label" title="<?=___('search_form_tooltip')?>"><?=($collectionSearch ? ___('search_form_default_text_collections', 'search for collections') : ___('search_form_default_text'))?></label>
            <input id="query" type="text" title="search" name="q" value="<?=$query?>" />
            <label for="cat"><?=___('search_form_within')?></label>
            <select name="cat" id="cat">
                    <option class="cat-all" value="all"<?=(!$collectionSearch && $category[0]==0 ? ' selected="selected"' : '')?>><?=_('search_form_all_addons')?></option>

                    <?php foreach ($AmoTags as $tag): ?>
                        <?php
                            // skip "plugins" as it is a static page at the moment and thus
                            // cannot be searched here
                            if ($tag['type'] == ADDON_PLUGIN) continue;

                            if ($category[0] == $tag['type'] && ($category[1]==$tag['cat'] || $tag['cat']==0))
                                $sel = ' selected="selected"';
                            else
                                $sel = '';
                        ?>
                        <option value="<?=$tag['type'].','.$tag['cat']?>"<?=$sel?>><?=$html->entities($tag['name'])?></option>
                    <?php endforeach; ?>

                    <option class="cat-all" value="collections"<?=($collectionSearch ? ' selected="selected"' : '')?>><?=___('search_form_all_collections', 'all collections')?></option>
            </select>
            <button type="submit"><img src="<?=$html->url('/img/amo2009/blank.gif', null, false, false)?>" alt="<?= ___('search_form_submit_tooltip') ?>"/></button>
        </div>

        <?=$two_form; ?>

        <div class="advanced" id="advanced-search">
            <fieldset class="important">
                <input type="hidden" name="as" value="true" />
                <input type="hidden" id="vfuz" name="vfuz" value="false" />

                <div class="container application">
                    <label for="appid"><?=___('advanced_search_form_application', 'Application'); ?></label>
                    <?php $html->simpleSelectTag('appid', $applications, $appid, array(), array(), false)?>
                </div>

                <div class="container">
                    <label for="lver"><?=___('advanced_search_form_version', 'version'); ?></label>
                    <span id="lver-td"><input name="lver" type="text" size="5" maxlength="10" value="<?=$lver ?>" /></span>
                </div>

                <div class="container">
                    <label for="hver"><?=___('advanced_search_form_to', 'to'); ?></label>
                    <span id="hver-td"><input name="hver" type="text" size="5" maxlength="10" value="<?=$hver ?>" /></span>
                </div>

            </fieldset>

            <fieldset class="subsidiary">
                <div class="container">
                    <label for="atype"><?=___('advanced_search_form_type', 'Type'); ?></label>
                    <?php $html->simpleSelectTag('atype', $addon_types, $atype, array(), array(), false); ?>
                </div>
                <div class="container">
                    <label for="pp"><?=___('advanced_search_form_perpage', 'Per Page'); ?></label>
                    <?php $html->simpleSelectTag('pp', $pages, $pp, array(), array(), false); ?>
                </div>
                <div class="container">
                    <label for="pid"><?=___('advanced_search_form_platform', 'Platform'); ?></label>
                    <?php $html->simpleSelectTag('pid', $platforms, $pid, array(), array(), false); ?>
                </div>
                <div class="container">
                    <label for="sort"><?=___('advanced_search_form_sortby', 'Sort By'); ?></label>
                    <?php $html->simpleSelectTag('sort', $sort_orders, $sort, array(), array(), false); ?>
                </div>
                <div class="container">
                    <label for="lup"><?=___('advanced_search_form_lastupdate', 'Last Updated'); ?></label>
                    <?php $html->simpleSelectTag('lup', $updates, $lup, array(), array(), false); ?>
                </div>
            </fieldset>
        </div>

        <div id="advanced-link" title="<?=___('advanced_search_form_toggle_tooltip');?>"><a href="?advancedsearch=<?=!$advancedSearch; ?>"><?= ___('advanced_search_form') ?></a></div>
    </form>
    <div id="search-bubble-inner"></div>

</div>

<script type="text/javascript">
// <![CDATA[

<?php
    echo "versions = new Array();\n";
    foreach($versions as $application_id => $ver_array) {
        echo "versions[{$application_id}] = {";
        echo "'".___('advanced_search_form_any_version')."' : 'any',\n";
        foreach ($ver_array as $version) {
            if($version !="") {
                echo "\"{$version}\": \"{$version}\", ";
            }
        }
        echo "};\n";
    }

   // to get default version value set up correctly needed to do after $appid, $lver and $hver set-up above in this element
   // that's why this code is not in mozilla.thtml

   //we re-set these up for the JS case (originally did non-JS case)

    $oldver = $lver;
    $lver = ($lver == "") ? "'any'" : '"'.$lver.'"' ;
    $hver = ($hver == "") ? "'any'" : '"'.$hver.'"' ;

    // JS to do browser detection to determine default low range to be current browser if applicable
?>

    var current_app_id = 0;
    var uapattern = /Mozilla.*(?:Firefox|Minefield|Shiretoko|GranParadiso|BonEcho|Iceweasel)\/([^\s]*).*$/;
    var ua = navigator.userAgent;
    var uamatch = uapattern.exec(ua);
    if (uamatch != null) {
        version = uamatch[1].split(".");
        low_appversion = version[0]+"."+parseInt(version[1]);
        current_app_id = <?=APP_FIREFOX ?>;
    }

    uapattern = /Mozilla.*(?:SeaMonkey|Iceape)\/([^\s]*).*$/;
    ua = navigator.userAgent;
    uamatch = uapattern.exec(ua);
    if (uamatch != null) {
        version = uamatch[1].split(".");
        low_appversion = version[0]+"."+parseInt(version[1]);
        current_app_id = <?=APP_SEAMONKEY ?>;
    }

    default_low = ( ('<?=$oldver ?>' == '-1' || '<?=$oldver?>' == '')  && current_app_id == <?=APP_ID; ?>) ? low_appversion : <?=$lver ?>;

    <?php
        /*
            Replace the non-JS textfields with select's. Since we only approximately list version names in the dropdown, we want to use fuzzy version
            matching on server. Add a hidden field to indicate this.
        */
    ?>

    $("#lver-td input").remove();
    $("#hver-td input").remove();

    var verArr =  versions[<?= $appid ?>];

    $("#vfuz").val(true);

    var lverSel = document.createElement("select");
    lverSel.id = "lver";
    lverSel.name = "lver";
    $("#lver-td").append(lverSel);
    replaceOptions("#lver", verArr, default_low);

    var hverSel = document.createElement("select");
    hverSel.id = "hver";
    hverSel.name = "hver";
    $("#hver-td").append(hverSel);
    replaceOptions("#hver", verArr, <?=$hver ?>);

    $("#appid").change(function () {
            selected_app_id = $("#appid option:selected").attr("value");
            default_low = (selected_app_id == current_app_id) ? low_appversion : 'any';
            verArr =  versions[selected_app_id];
            replaceOptions("#lver", verArr, default_low);
            replaceOptions("#hver", verArr, 'any');
        });

    <?php // JS for toggling advanced versus normal search. ?>
    var adv = $('#advanced-search');
    var advLink = $('#advanced-link a');
    advLink.isHidden = true;
    advLink.attr('href', '#'); // for ie6-7
    advLink.click(function(e) {
        if (advLink.isHidden) {
            adv.appendTo("#search-form");
            advLink.addClass("asopen");
            advLink.removeClass("asclosed");
            advLink.isHidden = false;
        } else {
            adv.appendTo("#hidden-form");
            advLink.addClass("asclosed");
            advLink.removeClass("asopen");
            advLink.isHidden = true;
        }
        e.preventDefault();
        return true;
    });

    <?php
    // to handle js-less case when a advanced query is sent there is one form,
    // so have to add back hidden form for js-aware case.
    if (isset($this->params['url']['as'])): ?>
        hiddenForm = document.createElement("form");
        hiddenForm.id = "hidden-form";
        var searchBubble = document.getElementById("search-bubble-inner");
        searchBubble.appendChild(hiddenForm);
        $("#hidden-form").addClass("asclosed");
        $("#advanced-search").appendTo("#hidden-form");
        advLink.click();
    <?php endif; ?>

    $(document).ready(function() {
        var q = $("#query");
        var l = $("#search-query label[for=query]");
        var c = $("#cat");
        l.show();
        if ( q.val() == "<?=_('search_form_default_text')?>"){ //initially q is set to search add-ons text for javascriptless browsing
		  q.val('');
		}
        if ( q.val() != "") { // if field has any value...
            l.hide(); // hide the label
        };
        l.click(function() { // for browsers with unclickable labels
            q.focus();
        });
        q.focus(function() { // when field gains focus...
            l.hide(); // hide the label
        });
        q.blur(function() { // when field loses focus...
            if ( q.val() == "" ) { // if field is empty...
                l.show(); // show the label again, else do nothing (label remains hidden)
            };
        });
        c.change(function() {
            if (c.val() == "collections") {
                l.text("<?=___('search_form_default_text_collections', 'search for collections')?>");
                // most advanced fields do not apply to collection search
                $('select:not(#appid,#pp),input', $('#advanced-search')).attr('disabled', 'disabled');
            } else {
                l.text("<?=_('search_form_default_text')?>");
                $('select,input', $('#advanced-search')).attr('disabled', '');
            }
        });
        c.change();
    });
  // ]]>
</script>