Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
embed_youtube_search.php
1 <?php
25 require_once dirname(__FILE__).'/../../../../core/include/init.inc';
26 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
27 require_once SQ_LIB_PATH.'/backend_search/backend_search.inc';
28 
29 if (empty($GLOBALS['SQ_SYSTEM']->user) || !($GLOBALS['SQ_SYSTEM']->user->canAccessBackend() || $GLOBALS['SQ_SYSTEM']->user->type() == 'simple_edit_user')){
30  exit;
31 }
32 
34  $quick_search_for_text = translate('asset_search_default_keyword');
35 } else {
36  $quick_search_for_text = translate('asset_search_default');
37 }
38 
39 $search_for = trim(array_get_index($_GET, 'quick-search-for', ''));
40 
41 // If we are searching for something
42 if ($search_for != '') {
43  // check for a url first
44  $asset_by_url = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL('', strip_url($search_for, TRUE), TRUE, TRUE);
45  if (!empty($asset_by_url) && ($asset_by_url->type() != 'file' || !$asset_by_url->readAccess())) {
46  $asset_by_url = NULL;
47  }
48 
49  if (assert_valid_assetid($search_for, '', TRUE, FALSE)) {
50  $asset_by_id = $GLOBALS['SQ_SYSTEM']->am->getAsset($search_for, '', TRUE);
51  if (!empty($asset_by_id) && ($asset_by_id->type() != 'file' || !$asset_by_id->readAccess())) {
52  $asset_by_id = NULL;
53  }
54  }
55 
56  // Only search for files (strict type check)
57  $results = Backend_Search::processSearch($search_for, Array(), Array('file' => FALSE));
58 
59  $html = '';
60  $found_asset_line = '';
61 
62  if (!empty($asset_by_url)) {
63  $found_asset =& $asset_by_url;
64  $found_asset_line .= '<strong>'.'Matched on URL:'.'</strong>';
65  }
66 
67  if (!empty($asset_by_id)) {
68  $found_asset =& $asset_by_id;
69  $found_asset_line .= '<strong>'.'Matched on Asset ID:'.'</strong>';
70  }
71 
72  if (!empty($found_asset)) {
73  $asset_name = $found_asset->name;
74  $found_asset_line .= '<div class="search-result">';
75  $found_asset_line .= get_asset_tag_line($found_asset->id, 'javascript:set_asset_finder_from_search(\''.$found_asset->id.'\', \''.htmlspecialchars($asset_name, ENT_QUOTES).'\', \'\', \'0\');');
76  $found_asset_line .= '</div>';
77  }
78 
79  if (!empty($results)) {
80  $result_list = Array();
81 
82  foreach ($results as $result_assetid => $result_detail) {
83  $tag_line = get_asset_tag_line($result_assetid);
84 
85  $this_detail = Array();
86 
87  foreach ($result_detail as $result_component_name => $result_component) {
88  foreach ($result_component as $name => $value) {
89 
90  $name_detail = '';
91  switch ($result_component_name) {
92  case 'contents':
93  $name_detail = 'Asset Contents';
94  break;
95 
96  case 'metadata':
97  case 'schema':
98  $name_detail = ($result_component_name == 'schema' ? 'Default ' : '').'Metadata: ';
99 
100  // Find a friendly name for the metadata field, if there
101  // is none then use the standard name of the field itself
102  $attr_values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('friendly_name', 'metadata_field', Array($name));
103  if (empty($attr_values)) {
104  $name_detail .= $value['name'];
105  } else {
106  $name_detail .= $attr_values[$name];
107  }
108 
109  $value = $value['value'];
110  break;
111 
112  case 'attributes':
113  $name_detail = 'Attribute: '.ucwords(str_replace('_', ' ', $name));
114  break;
115  }
116 
117  $words = explode(' ', $search_for);
118  $value = strip_tags($value);
119 
120  preg_match_all('/('.addslashes(implode('|', $words)).')/i', $value, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
121 
122  // We go backwards, because that way we don't invalidate
123  // our offsets. This section ellipsisises the bits between
124  // matches so that there's 15 characters either side of
125  // matches.
126 
127  // Last match position
128  if ($matches[count($matches) - 1][0][1] < strlen($value) - 15) {
129  $value = substr_replace($value, '...', $matches[count($matches) - 1][0][1] + 15);
130  }
131 
132  for ($i = count($matches) - 1; $i > 0; $i--) {
133  $previous_match = $matches[$i - 1][0];
134  $this_match = $matches[$i][0];
135 
136  $prev_pos = $previous_match[1] + strlen($previous_match[0]);
137  $next_pos = $this_match[1];
138 
139  if (($next_pos - $prev_pos) > 30) {
140  $value = substr_replace($value, '...', $prev_pos + 15, ($next_pos - $prev_pos) - 30);
141  }
142  }
143 
144  // First match position
145  if ($matches[0][0][1] > 15) {
146  $value = substr_replace($value, '...', 0, $matches[0][0][1] - 15);
147  }
148 
149  // Cut it down to a certain number of characters anyway
150  $value = ellipsisize($value, 120);
151 
152  $value = preg_replace('/('.addslashes(implode('|', $words)).')/i', '<span class="sq-backend-search-results-highlight">$1</span>', $value);
153 
154  // remove \r and replace \n with line breaks
155  $this_detail[] = $name_detail.'<br/><em>'.str_replace("\r", '', str_replace("\n", '<br/>', $value)).'</em>';
156  }//end foreach
157  }//end foreach
158 
159  $asset_name = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($result_assetid, 'asset', FALSE, 'name');
160 
161  $result_list[] = Array(
162  'tag_line' => get_asset_tag_line($result_assetid, 'javascript:set_asset_finder_from_search(\''.$result_assetid.'\', \''.htmlspecialchars($asset_name[$result_assetid], ENT_QUOTES).'\', \'\', \'0\');'),
163  'detail' => implode($this_detail, '<br/>'),
164  );
165  }//end foreach
166  }//end if
167 
168  // Are there any results? If not, put in a "search failed" box, otherwise
169  // build the results box
170  if (empty($results) && empty($found_asset_line)) {
171  $box_title = translate('search_failed');
172  $html = translate('failed_searching_for', addslashes($search_for));
173  $style_base = 'search-failed';
174  } else {
175  $box_title = translate('search_results');
176 
177  if (!empty($found_asset_line)) {
178  $html .= '<div class="search-result">'.$found_asset_line.'</div>';
179  }
180 
181  if (count($results) > 0) {
182 
183  $result_number = 0;
184  $results_per_page = $GLOBALS['SQ_SYSTEM']->getUserPrefs('search_manager', 'SQ_SEARCH_BACKEND_PAGE_SIZE');
185  $total_pages = ceil(count($results) / $results_per_page);
186 
187  $html .= '<div class="search-result-blurb">Matched on Keyword ('.count($results).' asset'.(count($results) == 1 ? '' : 's').'):</div>';
188 
189  if ($total_pages > 1) {
190  $html .= '<div class="search-result-pager">
191  <a href="" onclick="jump_to_search_results(1); return false;" title="Go back to first page">&lt;&lt;</a> &nbsp;
192  <a href="" onclick="jump_to_search_results(Math.max(current - 1, 1)); return false;" title="Go back one page">&lt;</a> &nbsp;
193  <strong>( <span id="sq-search-results-page-start">1</span> - <span id="sq-search-results-page-end">'.min(count($results), $results_per_page).'</span> )</strong> &nbsp;
194  <a href="" onclick="jump_to_search_results(Math.min(current + 1, '.$total_pages.')); return false;" title="Go forward one page">&gt;</a> &nbsp;
195  <a href="" onclick="jump_to_search_results('.$total_pages.'); return false;" title="Go forward to last page">&gt;&gt;</a>
196  </div>';
197  }
198 
199  foreach ($result_list as $this_result) {
200  $page_number = floor($result_number / $results_per_page) + 1;
201 
202  // Start of a new page
203  if ($result_number % $results_per_page == 0) {
204  if ($page_number > 1) $html .= '</div>';
205  $html .= '<div class="search-result-page" id="search-result-page-'.$page_number.'"';
206  if ($page_number > 1) {
207  $html .= ' style="display: none"';
208  }
209  $html .= '>';
210  }
211 
212  $result_number++;
213 
214  $html .= '<div class="search-result" id="search-result-'.$result_number.'">';
215  $html .= '<div class="search-result-expand-div" id="search-result-'.$result_number.'-expand-div">';
216  $html .= '<a href="#" id="search-result-'.$result_number.'-expand-link" class="search-result-expand-link" onclick="if (this.innerHTML == \'+\') {document.getElementById(\'search-result-'.$result_number.'-detail\').style.display = \'block\'; this.innerHTML = \'-\';} else {document.getElementById(\'search-result-'.$result_number.'-detail\').style.display = \'none\'; this.innerHTML = \'+\';} return false;">+</a>';
217  $html .= '</div>';
218  $html .= '<div class="search-result-entry" id="search-result-'.$result_number.'-entry">'.$this_result['tag_line'].'</div>';
219  $html .= '<div class="search-result-detail" id="search-result-'.$result_number.'-detail">'.$this_result['detail'].'</div>';
220  $html .= '</div> ';
221  }
222 
223  // End of last page
224  $html .= '</div>';
225 
226  }//end if
227 
228  $style_base = 'search-results';
229  }//end else
230 
231  $html = str_replace("\r", '', $html);
232  $html = str_replace("\n", ' ', $html);
233 
234 }//end if - we are searching for something
235 ?>
236 
237 <html>
238  <head>
239  <title>Insert YouTube - Search</title>
240  <style type="text/css">
241  html, body {
242  background: #402F48;
243  color: #FFFFFF;
244  font: 11px Tahoma,Verdana,sans-serif;
245  margin: 0px;
246  padding: 0px;
247  }
248 
249  form#main-form {
250  padding: 5px;
251  clear: right;
252  }
253 
254  #quick-search-for {
255  font: 11px Arial,Verdana,sans-serif;
256  border: 1px solid black;
257  padding: 1px 3px;
258  }
259 
260  #quick-search-for-label {
261  font: 11px Arial,Verdana,sans-serif;
262  color: #999;
263  }
264 
265  /* main popup title */
266  .title {
267  font-weight: bold;
268  font-size: 120%;
269  padding: 6px 10px;
270  margin-bottom: 10px;
271  border-bottom: 1px solid black;
272  text-align: right;
273  }
274 
275 
276  /* form and form fields */
277  form { padding: 0px; margin: 0px; }
278  </style>
279  <script type="text/javascript"><!--
288  function quick_search_for_onfocus(field)
289  {
290  if (field.value == '<?php echo addslashes($quick_search_for_text) ?>') {
291  field.value = '';
292  }
293 
294  return true;
295  }
296 
297 
309  function quick_search_for_onblur(field)
310  {
311  if (field.value == '') {
312  field.value = '<?php echo addslashes($quick_search_for_text) ?>';
313  }
314 
315  return true;
316  }
317 
318 
327  function quick_search_onsubmit(form)
328  {
329  if ((form.elements['quick-search-for'].value == '') || (form.elements['quick-search-for'].value == '<?php echo addslashes($quick_search_for_text) ?>')) {
330  return false;
331  }
332 
333  top.frames['sq_wysiwyg_popup_main'].document.getElementById('search-wait-popup').style.display = 'block';
334  top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup').style.display = 'none';
335 
336  return true;
337  }
338 <?php
339 // If something was being searched for, fill and show the search results
340 if ($search_for != '') {
341 ?>
342  var counter = 0;
343  var interval;
344 
345  // Reset variables in the main frame
346  top.frames['sq_wysiwyg_popup_main'].total_results = <?php echo count($results); ?>;
347  top.frames['sq_wysiwyg_popup_main'].current = 1;
348 
349  function show_search_results()
350  {
351  top.frames['sq_wysiwyg_popup_main'].document.getElementById('search-wait-popup').style.display = 'none';
352 
353  var results_box = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup');
354  var results_box_text = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup-details');
355  var results_box_title = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup-title');
356  var results_box_titlebar = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup-titlebar');
357 
358  var html = '<?php echo addslashes($html) ?>';
359  var title_html = '<?php echo addslashes($box_title) ?>';
360  var class_name = '<?php echo addslashes($style_base) ?>';
361 
362  results_box.className = 'sq-backend-' + class_name + '-table';
363  results_box_titlebar.className = 'sq-backend-' + class_name + '-heading';
364  results_box_text.className = 'sq-backend-' + class_name + '-body';
365 
366  results_box.style.visibility = 'hidden';
367 
368  results_box_title.innerHTML = title_html;
369  results_box_text.innerHTML = html;
370  results_box.style.display = 'block';
371 
372  height = results_box.offsetHeight;
373  counter = height;
374  results_box.style.top = -counter + 'px';
375 
376  results_box.style.visibility = 'visible';
377 
378  interval = setInterval('move_search_results();', 30);
379  }
380 
381  function move_search_results()
382  {
383  move = Math.max(Math.min(Math.abs(counter) * 0.08, 10), 2);
384  counter -= move;
385 
386  var results_box = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup');
387  results_box.style.top = -counter + 'px';
388 
389  if (counter <= 0) {
390  clearTimeout(interval);
391  }
392  }
393 <?php
394 }//end if - something was being searched for
395 ?>
396  // --></script>
397  </head>
398 
399  <body<?php if ($search_for != '') {
400  ?>
401  onload="show_search_results();"
402  <?php
403  }
404  ?>>
405  <?php
406 
407  ?>
408  <div class="title"><form action="" method="get" id="quick-search" onsubmit="return quick_search_onsubmit(this);">
409  <label id="quick-search-for-label" for="quick-search-for">Quick Search for Files</label>
410  <input type="text" size="30" value="<?php echo htmlspecialchars(empty($search_for) ? $quick_search_for_text : $search_for); ?>" name="quick-search-for" id="quick-search-for" onfocus="return quick_search_for_onfocus(this);" onblur="return quick_search_for_onblur(this);" onkeypress="if (event.keyCode == 13) return quick_search_onsubmit(this.form);">
411  </form>
412  </div>
413  </body>
414 </html>