1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Search;
13
14 use ICanBoogie\Exception;
15 use ICanBoogie\I18n;
16
17 use Brickrouge\Element;
18 use Brickrouge\Form;
19 use Brickrouge\Searchbox;
20
21 class Hooks
22 {
23 static public function markup_form(array $args, \Patron\Engine $patron, $template)
24 {
25 global $core, $document;
26
27 $page = $core->site->resolve_view_target('search/home');
28
29 if (!$page)
30 {
31 throw new Exception\Config($core->modules['search']);
32 }
33
34 $label = I18n\t('search.label.search');
35
36 $tags = array
37 (
38 Form::VALUES => $_GET,
39
40 Element::CHILDREN => array
41 (
42 'q' => new Searchbox
43 (
44 array
45 (
46 Form::LABEL => $label,
47 'placeholder' => $label
48 )
49 )
50 ),
51
52 'class' => 'navbar-search',
53 'method' => \ICanBoogie\HTTP\Request::METHOD_GET,
54 'action' => $page->url
55 );
56
57 return $template ? new \WdTemplatedForm($tags, $patron($template)) : (string) new Form($tags);
58 }
59
60
61
62
63 static protected $config = array
64 (
65 'url' => 'http://ajax.googleapis.com/ajax/services/search/web',
66 'options' => array
67 (
68 'gl' => 'fr',
69 'hl' => 'fr',
70 'rsz' => 'large'
71 )
72 );
73
74 static public function search($query, $start=0, array $options=array())
75 {
76 global $registry;
77
78 $site = $registry->get('siteSearch.host');
79
80 if (!$site)
81 {
82 $site = $_SERVER['SERVER_NAME'];
83 $site = str_replace('www.', '', $site);
84 }
85
86 $options += self::$config['options'];
87
88
89
90 $query = self::$config['url'] . '?' . http_build_query
91 (
92 array
93 (
94 'q' => $query . ' site:' . $site,
95 'start' => $start,
96 'v' => '1.0'
97 )
98
99 + $options
100 );
101
102
103
104 $rc = file_get_contents($query);
105
106 $response = json_decode($rc)->responseData;
107
108 foreach ($response->results as $result)
109 {
110 $shortUrl = $result->unescapedUrl;
111 $shortUrl = substr($shortUrl, strpos($shortUrl, $site) + strlen($site));
112
113 $result->shortUrl = $shortUrl;
114 }
115
116 return $response;
117 }
118
119 static public function matches(array $args, \Patron\Engine $patron, $template)
120 {
121 $_GET += array
122 (
123 'q' => null,
124 'start' => 0
125 );
126
127 $search = $_GET['q'];
128 $start = $_GET['start'];
129
130 if (!$search)
131 {
132 return;
133 }
134
135 $response = self::search($search, $start);
136 $count = count($response->results);
137 $total = isset($response->cursor->estimatedResultCount) ? $response->cursor->estimatedResultCount : 0;
138 $page = 0;
139 $pageIndex = 0;
140 $pager = null;
141
142 if ($total && count($response->cursor->pages) > 1)
143 {
144 $pageIndex = $response->cursor->currentPageIndex;
145 $pages = array();
146
147 foreach ($response->cursor->pages as $i => $page)
148 {
149 $pages[] = ($pageIndex == $i) ? '<strong>' . $page->label . '</strong>' : '<a href="?start=' . $page->start . '&q=' . \ICanBoogie\escape(urlencode($search)) . '">' . $page->label . '</a>';
150 }
151
152 $pager = '<div class="pager">' . implode('<span class="separator">, </span>', $pages) . '</div>';
153 }
154
155 $patron->context['self']['q'] = $search;
156 $patron->context['self']['response'] = $response;
157 $patron->context['self']['pager'] = $pager;
158 $patron->context['self']['range'] = array
159 (
160 'lower' => $start + 1,
161 'upper' => $start + $count,
162 'start' => $start,
163 'page' => $pageIndex,
164 'count' => $total
165 );
166
167 return $patron($template, $response->results);
168 }
169 }