Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
insert_link_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 require_once SQ_FUDGE_PATH.'/general/general.inc';
29 
30 if (empty($GLOBALS['SQ_SYSTEM']->user) || !($GLOBALS['SQ_SYSTEM']->user->canAccessBackend() || $GLOBALS['SQ_SYSTEM']->user->type() == 'simple_edit_user')){
31  exit;
32 }
33 
35  $quick_search_for_text = translate('asset_search_default_keyword');
36 } else {
37  $quick_search_for_text = translate('asset_search_default');
38 }
39 
40 $search_for = trim(array_get_index($_GET, 'quick-search-for', ''));
41 
42 // If we are searching for something
43 if ($search_for != '') {
44  // check for a url first
45  $asset_by_url = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL('', strip_url($search_for, TRUE), TRUE, TRUE);
46  if (assert_valid_assetid($search_for, '', TRUE, FALSE)) {
47  $asset_by_id = $GLOBALS['SQ_SYSTEM']->am->getAsset($search_for, '', TRUE);
48  }
49 
50  $html = '';
51  $found_asset_line = '';
52  $results = Array();
53 
54  if (!empty($asset_by_url)) {
55  $found_asset =& $asset_by_url;
56  $found_asset_line .= '<strong>'.'Matched on URL:'.'</strong>';
57  }
58 
59  if (!empty($asset_by_id)) {
60  $found_asset =& $asset_by_id;
61  $found_asset_line .= '<strong>'.'Matched on Asset ID:'.'</strong>';
62  }
63 
64  // Make sure the user has read permission on the found asset
65  if (!empty($found_asset) && !$found_asset->readAccess()) {
66  $found_asset = NULL;
67  $found_asset_line = '';
68  }
69 
70  if (!empty($found_asset)) {
71  $asset_name = $found_asset->name;
72  $found_asset_line .= '<div class="search-result">';
73  $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\')');
74  $found_asset_line .= '</div>';
75  } else {
76 
77  $results = Backend_Search::processSearch($search_for);
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  }//end else
168 
169  // Are there any results? If not, put in a "search failed" box, otherwise
170  // build the results box
171  if (empty($results) && empty($found_asset_line)) {
172  $box_title = translate('search_failed');
173  $html = translate('failed_searching_for', addslashes($search_for));
174  $style_base = 'search-failed';
175  } else {
176  $box_title = translate('search_results');
177 
178  if (!empty($found_asset_line)) {
179  $html .= '<div class="search-result">'.$found_asset_line.'</div>';
180  }
181 
182  if (count($results) > 0) {
183 
184  $result_number = 0;
185  $results_per_page = $GLOBALS['SQ_SYSTEM']->getUserPrefs('search_manager', 'SQ_SEARCH_BACKEND_PAGE_SIZE');
186  $total_pages = ceil(count($results) / $results_per_page);
187 
188  $html .= '<div class="search-result-blurb">Matched on Keyword ('.count($results).' asset'.(count($results) == 1 ? '' : 's').'):</div>';
189 
190  if ($total_pages > 1) {
191  $html .= '<div class="search-result-pager">
192  <a href="" onclick="jump_to_search_results(1); return false;" title="Go back to first page">&lt;&lt;</a> &nbsp;
193  <a href="" onclick="jump_to_search_results(Math.max(current - 1, 1)); return false;" title="Go back one page">&lt;</a> &nbsp;
194  <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;
195  <a href="" onclick="jump_to_search_results(Math.min(current + 1, '.$total_pages.')); return false;" title="Go forward one page">&gt;</a> &nbsp;
196  <a href="" onclick="jump_to_search_results('.$total_pages.'); return false;" title="Go forward to last page">&gt;&gt;</a>
197  </div>';
198  }
199 
200  foreach ($result_list as $this_result) {
201  $page_number = floor($result_number / $results_per_page) + 1;
202 
203  // Start of a new page
204  if ($result_number % $results_per_page == 0) {
205  if ($page_number > 1) $html .= '</div>';
206  $html .= '<div class="search-result-page" id="search-result-page-'.$page_number.'"';
207  if ($page_number > 1) {
208  $html .= ' style="display: none"';
209  }
210  $html .= '>';
211  }
212 
213  $result_number++;
214 
215  $html .= '<div class="search-result" id="search-result-'.$result_number.'">';
216  $html .= '<div class="search-result-expand-div" id="search-result-'.$result_number.'-expand-div">';
217  $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>';
218  $html .= '</div>';
219  $html .= '<div class="search-result-entry" id="search-result-'.$result_number.'-entry">'.$this_result['tag_line'].'</div>';
220  $html .= '<div class="search-result-detail" id="search-result-'.$result_number.'-detail">'.$this_result['detail'].'</div>';
221  $html .= '</div> ';
222  }
223 
224  // End of last page
225  $html .= '</div>';
226 
227  }//end if
228 
229  $style_base = 'search-results';
230  }//end else
231 
232  $html = str_replace("\r", '', $html);
233  $html = str_replace("\n", ' ', $html);
234 
235 }//end if - we are searching for something
236 ?>
237 
238 <html>
239  <head>
240  <title>Insert Link - Search</title>
241  <style type="text/css">
242  html, body {
243  background: #402F48;
244  color: #FFFFFF;
245  font: 11px Tahoma,Verdana,sans-serif;
246  margin: 0px;
247  padding: 0px;
248  }
249 
250  form#main-form {
251  padding: 5px;
252  clear: right;
253  }
254 
255  #quick-search-for {
256  font: 11px Arial,Verdana,sans-serif;
257  border: 1px solid black;
258  padding: 1px 3px;
259  }
260 
261  #quick-search-for-label {
262  font: 11px Arial,Verdana,sans-serif;
263  color: #999;
264  }
265 
266  /* main popup title */
267  .title {
268  font-weight: bold;
269  font-size: 120%;
270  padding: 6px 10px;
271  margin-bottom: 10px;
272  border-bottom: 1px solid black;
273  text-align: right;
274  }
275 
276 
277  /* form and form fields */
278  form { padding: 0px; margin: 0px; }
279  </style>
280  <script type="text/javascript"><!--
289  function quick_search_for_onfocus(field)
290  {
291  if (field.value == '<?php echo addslashes($quick_search_for_text) ?>') {
292  field.value = '';
293  }
294 
295  return true;
296  }
297 
298 
310  function quick_search_for_onblur(field)
311  {
312  if (field.value == '') {
313  field.value = '<?php echo addslashes($quick_search_for_text) ?>';
314  }
315 
316  return true;
317  }
318 
319 
328  function quick_search_onsubmit(form)
329  {
330  if ((form.elements['quick-search-for'].value == '') || (form.elements['quick-search-for'].value == '<?php echo addslashes($quick_search_for_text) ?>')) {
331  return false;
332  }
333 
334  top.frames['sq_wysiwyg_popup_main'].document.getElementById('search-wait-popup').style.display = 'block';
335  top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup').style.display = 'none';
336 
337  return true;
338  }
339 <?php
340 // If something was being searched for, fill and show the search results
341 if ($search_for != '') {
342 ?>
343  var counter = 0;
344  var interval;
345 
346  // Reset variables in the main frame
347  top.frames['sq_wysiwyg_popup_main'].total_results = <?php echo count($results); ?>;
348  top.frames['sq_wysiwyg_popup_main'].current = 1;
349 
350  function show_search_results()
351  {
352  top.frames['sq_wysiwyg_popup_main'].document.getElementById('search-wait-popup').style.display = 'none';
353 
354  var results_box = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup');
355  var results_box_text = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup-details');
356  var results_box_title = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup-title');
357  var results_box_titlebar = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup-titlebar');
358 
359  var html = '<?php echo addslashes($html) ?>';
360  var title_html = '<?php echo addslashes($box_title) ?>';
361  var class_name = '<?php echo addslashes($style_base) ?>';
362 
363  results_box.className = 'sq-backend-' + class_name + '-table';
364  results_box_titlebar.className = 'sq-backend-' + class_name + '-heading';
365  results_box_text.className = 'sq-backend-' + class_name + '-body';
366 
367  results_box.style.visibility = 'hidden';
368 
369  results_box_title.innerHTML = title_html;
370  results_box_text.innerHTML = html;
371  results_box.style.display = 'block';
372 
373  height = results_box.offsetHeight;
374  counter = height;
375  results_box.style.top = -counter + 'px';
376 
377  results_box.style.visibility = 'visible';
378 
379  interval = setInterval('move_search_results();', 30);
380  }
381 
382  function move_search_results()
383  {
384  move = Math.max(Math.min(Math.abs(counter) * 0.08, 10), 2);
385  counter -= move;
386 
387  var results_box = top.frames['sq_wysiwyg_popup_main'].document.getElementById('new-message-popup');
388  results_box.style.top = -counter + 'px';
389 
390  if (counter <= 0) {
391  clearTimeout(interval);
392  }
393  }
394 <?php
395 }//end if - something was being searched for
396 ?>
397  // --></script>
398  </head>
399 
400  <body<?php if ($search_for != '') {
401  ?>
402  onload="show_search_results();"
403  <?php
404  }
405  ?>>
406  <?php
407 
408  ?>
409  <div class="title"><form action="" method="get" id="quick-search" onsubmit="return quick_search_onsubmit(this);">
410  <label id="quick-search-for-label" for="quick-search-for">Quick Search</label>
411  <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);">
412  </form>
413  </div>
414  </body>
415 </html>