Index: top_searches.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/top_searches/top_searches.module,v retrieving revision 1.2 diff -u -p -r1.2 top_searches.module --- top_searches.module 7 Aug 2008 08:00:35 -0000 1.2 +++ top_searches.module 28 Sep 2008 11:26:03 -0000 @@ -1,6 +1,6 @@ t('Maximum number of items to show in Top searches block'), '#default_value' => variable_get('top_searches_block_items', 50), ); - - $form['clear_searches'] = array( - '#type' => 'button', + + $result = !empty($lang) ? db_query("SELECT * FROM {top_searches} WHERE data='%s' AND allowed=0 ORDER by counter DESC LIMIT 15",$lang) : + db_query("SELECT * FROM {top_searches} WHERE allowed=0 ORDER by counter DESC LIMIT 15") ; + + while ($data = db_fetch_object($result)) { + $search[$data->qid] = ''; + $form['title'][$data->qid] = array('#value' => $data->q); + $form['counter'][$data->qid] = array('#value' => $data->counter); + $form['data'][$data->qid] = array('#value' => $data->data); + } + $form['searches'] = array('#type' => 'checkboxes', '#options' => $search); + //$form['pager'] = array('#value' => theme('pager', NULL, 50, 0)); + $form['top_searches_allow'] = array ( + '#type' => 'submit', + '#button_type' => 'submit', + '#value' => t('Set Selected to allowed'), + ); + if (module_exists('i18n')) { + $form['languages'] = array( + '#type' => 'select', + '#title' => t('Language'), + '#options' => i18n_language_list(), + '#default_value' => empty($lang) ? i18n_default_language() : $lang, + ); + $form['filter_lang'] = array ( + '#type' => 'submit', + '#button_type' => 'submit', + '#value' => t('Filter by language'), + ); + } + $form['clear_searches'] = array( + '#type' => 'submit', '#value' => t('Reset search counters'), '#description' => top_searches_count_rows() . ' values' ); - return system_settings_form($form); + // return system_settings_form($form); + return $form; + +} + +function theme_top_searches_admin_form($form) { + // Overview table: + $output = drupal_render($form['languages']); + $output .= drupal_render($form['filter_lang']); + $header = array(theme('table_select_header_cell'), t('Search term'), t('Counter'), t('data')); + + //$output .= drupal_render($form['options']); + if (isset($form['title']) && is_array($form['title'])) { + foreach (element_children($form['title']) as $key) { + $row = array(); + $row[] = drupal_render($form['searches'][$key]); + $row[] = drupal_render($form['title'][$key]); + $row[] = drupal_render($form['counter'][$key]); + $row[] = drupal_render($form['data'][$key]); + + $rows[] = $row; + } + $rows[] = array( //array('data' => drupal_render($form['pager']), 'colspan' => 2), + array('data' => drupal_render($form['top_searches_allow']), 'colspan' => 1), + ); + } + else { + $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '4')); + } + $output .= theme('table', $header, $rows); + if ($form['pager']['#value']) { + $output .= drupal_render($form['pager']); + } + $output .= drupal_render($form); + return $output; +} + +function top_searches_admin_form_validate($form_id, $form_values) { +} + + function top_searches_admin_form_submit($form_id, $form_values) { + switch ($form_values['op']) { + case t('Set Selected to allowed'): + foreach ($form_values['searches'] as $item) { + if ($item!=0) { + db_query("UPDATE {top_searches} SET allowed=1 WHERE qid=%d",$item); + drupal_set_message(t("Updated search terms")); + } + } + break; + case t('Filter by language'): + drupal_goto('admin/settings/top_searches/'.$form_values['languages']); + break; + } } /** @@ -91,10 +172,10 @@ function top_searches_block($op = 'list' function top_searches_form_alter($form_id, &$form) { // Don't count admin searches (like admin/user/search) if (arg(0) == 'admin') return; - - // Only count node searches - // TODO: allow different blocks for different types of seraches - if ($form['module']['#value'] != 'node') return; + + // Only count node searches + // TODO: allow different blocks for different types of searches + //if ($form['module']['#value'] != 'node') return; if ($form_id == 'search_form' || $form_id == 'search_block_form') { $form['#submit'] = array_merge($form['#submit'], array('top_searches_catch_search_keys' => array())); @@ -102,7 +183,8 @@ function top_searches_form_alter($form_i } function top_searches_catch_search_keys($form_id, $form_values) { - switch ($form_id) { + + switch ($form_id) { case "search_block_form": $keys = $form_values['search_block_form_keys']; break; @@ -113,19 +195,29 @@ function top_searches_catch_search_keys( // Beautify the search phrase $keys = preg_replace("/[' ']{2,}/", ' ', ucwords(strtolower(trim($keys)))); - + + //make sure it's not blank + if ($keys=='') { + return; + } // Search the DB for existing keys: $results_qid = db_result(db_query("SELECT qid FROM {top_searches} WHERE q = '%s'", $keys)); if ($results_qid) { db_query("UPDATE {top_searches} SET counter = (counter + 1) WHERE qid = %d", $results_qid); } else { - db_query("INSERT INTO {top_searches} (q, counter) VALUES ('%s', %d)", $keys, 1); + $lang = module_exists('i18n') ? i18n_get_lang() : ''; + db_query("INSERT INTO {top_searches} (q, counter,data,allowed) VALUES ('%s', %d, '%s',%d)", $keys, 1, i18n_get_lang(),0); } } function top_searches_collect_results() { - $result = db_query("SELECT q, counter FROM {top_searches} ORDER by counter DESC LIMIT %d", variable_get('top_searches_block_items', 50)); + if (module_exists('i18n')) { + $result = db_query("SELECT q, counter FROM {top_searches} WHERE allowed=1 AND data='%s' ORDER by counter DESC LIMIT %d",i18n_get_lang(), variable_get('top_searches_block_items', 50)); + } + else { + $result = db_query("SELECT q, counter FROM {top_searches} WHERE allowed=1 ORDER by counter DESC LIMIT %d", variable_get('top_searches_block_items', 50)); + } $top_searches = array(); if (db_num_rows($result)) {