Browse Source

top level module now installs and can display search block

d9
ppound 3 years ago
parent
commit
a59a9a7261
  1. 9
      roblib_search.info
  2. 9
      roblib_search.info.yml
  3. 44
      roblib_search.install
  4. 266
      roblib_search.module
  5. 7
      roblib_search.routing.yml
  6. 79
      src/Form/RoblibSearchForm.php
  7. 57
      src/Form/RoblibSearchSettingsForm.php
  8. 26
      src/Plugin/Block/RoblibSearchBoxBlock.php

9
roblib_search.info

@ -1,9 +0,0 @@
name = Roblib Search
configure = admin/roblib_search/configure
description = Provides a search form and fires a hook when the search is submitted. Other modules should implement the search functionality by implementing the correct hooks.
package = Roblib Search
dependencies[] = apachesolr_access
dependencies[] = views_ui
version = 7.x-dev
core = 7.x
stylesheets[all][] = css/roblib_search.base.css

9
roblib_search.info.yml

@ -0,0 +1,9 @@
name: Roblib Search
type: module
core_version_requirement: ^8.9 || ^9
configure: roblib_search.settings
description: Provides a search form and fires a hook when the search is submitted. Other modules should implement the search functionality by implementing the correct hooks.
package: Roblib Search
dependancies:
- apachesolr_access
- views_ui

44
roblib_search.install

@ -1,44 +0,0 @@
<?php
/**
* @file
* Installation hooks.
*/
/**
* Implements hook_schema().
*/
function roblib_search_schema() {
$schema = array();
$schema['roblib_search_log'] = array(
'description' => 'stores search data such as date/time ip etc.',
'fields' => array(
'sid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'unique id for each row',
),
'query' => array(
'description' => 'the search string',
'type' => 'varchar',
'not null' => TRUE,
'length' => 1024,
),
'searched_date' => array(
'description' => 'the date the search request occured',
'type' => 'varchar',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
'ipaddress' => array(
'description' => 'the ip or domain of the users computer ',
'type' => 'varchar',
'not null' => TRUE,
'length' => 255,
),
),
'primary key' => array('sid'),
);
return $schema;
}

266
roblib_search.module

@ -28,138 +28,6 @@ function roblib_search_menu() {
return $items; return $items;
} }
/**
* A form for managing the modules configuration.
*
* @param array $form
* A Drupal form array
* @param array $form_state
* A Drupal formstate array
*
* @return array
* A Drupal form
*/
function roblib_search_config_form($form, &$form_state) {
$form['roblib_search_panel_page'] = array(
'#type' => 'textfield',
'#title' => t('Path to the roblib search panel page'),
'#default_value' => variable_get('roblib_search_panel_page', '/roblib/panel'),
'#description' => t('The Path to the Roblib Search Panel, for example /roblib/panel'),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* Implements hook_admin_paths().
*/
function roblib_search_admin_paths() {
$paths = array(
'admin/roblib/search' => TRUE,
);
return $paths;
}
function roblib_search_views_data(){
$data['roblib_search_log']['table']['group'] = t('Roblib Search log');
$data['roblib_search_log']['table']['base'] = array(
'field' => 'sid', // This is the identifier field for the view.
'title' => t('Roblib Search log'),
'help' => t('Contains the Roblib Bento box search query strings.'),
'weight' => -10,
);
// The individual fields.
$data['roblib_search_log']['query'] = array(
'title' => t('the query string field'),
'help' => t('a plain text field.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE, // This is use by the table display plugin.
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data['roblib_search_log']['sid'] = array(
'title' => t('Numeric field'),
'help' => t('the unique identifier for each row'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
$data['roblib_search_log']['ipaddress'] = array(
'title' => t('the ipaddress of the user who submitted the search'),
'help' => t('a plain text field.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE, // This is use by the table display plugin.
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data['roblib_search_log']['searched_date'] = array(
'title' => t('Timestamp field'),
'help' => t('a timestamp field.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'argument' => array('handler' => 'views_handler_argument_date'),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'roblib_search_handler_filter_datetime',
),
);
return $data;
}
/**
* Implements hook_permission().
*/
function roblib_search_permission() {
return array(
'search roblib' => array(
'title' => t('Search all configured targets'),
'description' => t('Search all Roblib configured targets. This permission exposes the search blocks and allows you to see search results.'),
),
'administer roblib search' => array(
'title' => t('Administer Roblib Search'),
'description' => t('Administer settings for the Roblib search client.'),
),
);
}
/** /**
* Implements hook_block_info(). * Implements hook_block_info().
*/ */
@ -180,7 +48,7 @@ function roblib_search_block_info() {
/** /**
* Implements hook_theme(). * Implements hook_theme().
*/ */
function roblib_search_theme() { /*function roblib_search_theme() {
$path = drupal_get_path('module', 'roblib_search'); $path = drupal_get_path('module', 'roblib_search');
$file = 'theme.inc'; $file = 'theme.inc';
@ -192,32 +60,18 @@ function roblib_search_theme() {
'variables' => array('results' => NULL), 'variables' => array('results' => NULL),
) )
); );
} }*/
/**
* Hook the panels pane preprocessor so we can wrap it in a div for themeing.
*
* @param array $variables
* Drupal variables array
* @param $hook
*/
function roblib_search_preprocess_panels_pane(&$variables, $hook){
switch ($variables['pane']->subtype) {
case 'roblib_search-roblib_search_other':
$variables['title_prefix'] = '<div class="roblib-search-header roblib-search-other-sources-header">';
$variables['title_suffix'] = '</div>';
break;
}
}
/** /**
* Implements hook_init(). * Implements hook_init().
*/ */
function roblib_search_init() { /*function roblib_search_init() {
drupal_add_js(drupal_get_path('module', 'roblib_search') . '/js/imagesloaded.pkg.min.js'); drupal_add_js(drupal_get_path('module', 'roblib_search') . '/js/imagesloaded.pkg.min.js');
drupal_add_js(drupal_get_path('module', 'roblib_search') . '/js/roblib_search.js'); drupal_add_js(drupal_get_path('module', 'roblib_search') . '/js/roblib_search.js');
drupal_add_css(drupal_get_path('module', 'roblib_search') . '/css/jquery.qtip.min.css'); drupal_add_css(drupal_get_path('module', 'roblib_search') . '/css/jquery.qtip.min.css');
} }*/
/** /**
* Implements hook_block_view(). * Implements hook_block_view().
@ -236,82 +90,6 @@ function roblib_search_block_view($delta = '') {
return $block; return $block;
} }
/**
* Roblib search simple search form submit.
*
* @param array $form
* Drupal form array
* @param array $form_state
* Drupal formstate array
*/
function roblib_search_simple_form_submit($form, &$form_state) {
global $base_url;
$form_state['rebuild'] = TRUE;
$search_string = $form_state['values']['roblib_search_simple_search_query'];
$search_string = filter_xss($search_string);
roblib_search_log_query($search_string);
$redirect_url = variable_get('roblib_search_panel_page', 'roblib/panel');
$redirect_url = $base_url . '/' . $redirect_url;
drupal_goto($redirect_url, array('query' => array('roblib_query' => $search_string)));
}
/**
* Logs a search query to a database table.
*
* @param string $search_string
* The query to log
*/
function roblib_search_log_query($search_string) {
$eid = db_insert('roblib_search_log')
->fields(array(
'query' => $search_string,
'searched_date' => format_date(time(), 'custom', 'Y-m-d H:i:s'),
'ipaddress' => ip_address(),
))
->execute();
}
/**
* Roblib search simple search form.
*
* @param array $form
* Drupal form array
* @param array $form_state
* Drupal formstate
*
* @return array
* A Drupal formapi array
*/
function roblib_search_simple_form($form, &$form_state) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
$query = '';
}
$form['simple'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'container-inline'
)
)
);
$form['simple']["roblib_search_simple_search_query"] = array(
'#size' => '15',
'#type' => 'textfield',
'#title' => '',
'#required' => TRUE,
'#default_value' => $query,
);
$form['simple']['submit'] = array(
'#type' => 'submit',
'#value' => t('search')
);
return $form;
}
/** /**
* Implements hook_help(). * Implements hook_help().
*/ */
@ -327,37 +105,3 @@ function roblib_search_help($path, $arg) {
); );
} }
} }
class roblib_search_handler_filter_datetime extends views_handler_filter_date {
function op_between($field) {
if ($this->operator == 'between') {
$a = intval(strtotime($this->value['min'], 0));
$b = intval(strtotime($this->value['max'], 0));
}
else {
$a = intval(strtotime($this->value['max'], 0));
$b = intval(strtotime($this->value['min'], 0));
}
if ($this->value['type'] == 'offset') {
// changed from original
$a = (integer)time() + (integer)sprintf('%+d', $a); // keep sign
$b = (integer)time() + (integer)sprintf('%+d', $b); // keep sign
// changed from original ends
}
// %s is safe here because strtotime scrubbed the input and we might
// have a string if using offset.
$this->query->add_where_expression($this->options['group'], "$field >= '".date("Y-m-d H:i:s", $a)."'");
$this->query->add_where_expression($this->options['group'], "$field <= '".date("Y-m-d H:i:s", $b)."'");
}
function op_simple($field) {
$value = intval(strtotime($this->value['value'], 0));
if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
$this->query->add_where_expression($this->options['group'], "$field $this->operator DATE_ADD(NOW(), INTERVAL $value SECOND)");
} else {
$this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
}
}
}

7
roblib_search.routing.yml

@ -0,0 +1,7 @@
roblib_search.settings:
path: '/admin/config/system/roblib_search'
defaults:
_form: '\Drupal\roblib_search\Form\RoblibSearchSettingsForm'
_title: 'Roblib Search Settings Form'
requirements:
_permission: 'administer site configuration'

79
src/Form/RoblibSearchForm.php

@ -0,0 +1,79 @@
<?php
namespace Drupal\roblib_search\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
/**
* Config form for module
*
* @author ppound
*/
class RoblibSearchForm extends FormBase{
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'roblib_searchbox_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
$query = '';
}
$form['simple'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'container-inline'
)
)
);
$form['simple']["roblib_search_simple_search_query"] = array(
'#size' => '15',
'#type' => 'textfield',
'#title' => '',
'#required' => TRUE,
'#default_value' => $query,
);
$form['simple']['submit'] = array(
'#type' => 'submit',
'#value' => t('search')
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
global $base_url;
//$form_state->rebuild(TRUE);
$search_string = $form_state->getValue('roblib_search_simple_search_query');
$search_string = Html::escape($search_string);
//roblib_search_log_query($search_string);
$config = \Drupal::config('roblib_search.settings');
$redirect_url = $config->get('search_panel_page');
$redirect_url = $base_url . '/' . $redirect_url;
//TODO redirect to the search page
$form_state->setRedirect('<front>');
}
}

57
src/Form/RoblibSearchSettingsForm.php

@ -0,0 +1,57 @@
<?php
namespace Drupal\roblib_search\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Settings form for module
*
* @author ppound
*/
class RoblibSearchSettingsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'roblib_search_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('roblib_search.settings');
$form['search_panel_page'] = array(
'#type' => 'textfield',
'#title' => t('Path to the roblib search panel page'),
'#default_value' => $config->get('search_panel_page'),
'#description' => t('The Path to the Roblib Search Panel, for example /roblib/panel'),
'#required' => TRUE,
);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('roblib_search.settings');
$config->set('search_panel_page', $form_state->getValue('search_panel_page'))->save();
}
}

26
src/Plugin/Block/RoblibSearchBoxBlock.php

@ -0,0 +1,26 @@
<?php
namespace Drupal\roblib_search\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Hello' Block.
*
* @Block(
* id = "roblib_search_box",
* admin_label = @Translation("Roblib Search Block"),
* category = @Translation("Roblib Search"),
* )
*/
class RoblibSearchBoxBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('\Drupal\roblib_search\Form\RoblibSearchForm');
return $form;
}
}
Loading…
Cancel
Save