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