materio_search_api.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * @file
  4. * This is the file description for Materiobasemod module.
  5. *
  6. * In this more verbose, multi-line description, you can specify what this
  7. * file does exactly. Make sure to wrap your documentation in column 78 so
  8. * that the file can be displayed nicely in default-sized consoles.
  9. */
  10. // define(MATERIO_SEARCH_API_RESULTS_PATH, 'explore');
  11. /**
  12. * Implements hook_permission().
  13. */
  14. function materio_search_api_permission() {
  15. return array(
  16. 'use materio search api' => array(
  17. 'title' => t('Use materio search api'),
  18. 'description' => t('Use materio search api.'),
  19. ),
  20. 'use materio search api autocomplete' => array(
  21. 'title' => t('Use materio search api autocomplete'),
  22. 'description' => t('Use materio search api autocomplete.'),
  23. ),
  24. 'use materio search api viewmode selection' => array(
  25. 'title' => t('Use materio search api viewmode selection'),
  26. 'description' => t('Use materio search api viewmode selection.'),
  27. ),
  28. 'administer materio_search_api' => array(
  29. 'title' => t('administer Materio search api'),
  30. 'description' => t('Administer materio search api.'),
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements hook_menu().
  36. */
  37. function materio_search_api_menu() {
  38. $items = array();
  39. $base = array(
  40. 'type' => MENU_CALLBACK,
  41. 'file' => 'materio_search_api.pages.inc',
  42. );
  43. $items['admin/config/search/search_api/materiosearchapi'] = array(
  44. 'title' => 'Materio Search Api',
  45. 'page callback' => 'drupal_get_form',
  46. 'page arguments' => array('materio_search_api_settings'),
  47. 'access arguments' => array('administer materio_search_api'),
  48. 'file' => 'materio_search_api.admin.inc',
  49. 'type' => MENU_LOCAL_TASK,
  50. );
  51. $items['materiosearchapi/autocomplete/dbselect'] = $base+array(
  52. 'access arguments' => array('use materio search api autocomplete'),
  53. 'page callback' => 'materio_search_api_autocomplete_dbselect',
  54. );
  55. $items['materiosearchapi/autocomplete/searchapi'] = $base+array(
  56. 'access arguments' => array('use materio search api autocomplete'),
  57. 'page callback' => 'materio_search_api_autocomplete_searchapi',
  58. );
  59. $items['explore'] = $base+array(
  60. 'access arguments' => array('use materio search api'),
  61. 'page callback' => 'materio_search_api_results_search',
  62. //'page argument' => array(1,2,3),
  63. );
  64. $items['materiosearchapi/viewmode/change'] = $base+array(
  65. 'access arguments' => array('use materio search api'),
  66. 'page callback' => 'materio_search_api_viewmode_change',
  67. 'page argument' => array(3),
  68. );
  69. return $items;
  70. }
  71. /**
  72. * Implements hook_block_info().
  73. */
  74. function materio_search_api_block_info() {
  75. // This example comes from node.module.
  76. $blocks['materio_search_api_search'] = array(
  77. 'info' => t('Materio search api search'),
  78. 'cache' => DRUPAL_NO_CACHE
  79. );
  80. $blocks['materio_search_api_viewmode'] = array(
  81. 'info' => t('Materio search api view mode selection'),
  82. 'cache' => DRUPAL_NO_CACHE
  83. );
  84. return $blocks;
  85. }
  86. /**
  87. * Implements hook_block_view().
  88. */
  89. function materio_search_api_block_view($delta = '') {
  90. // This example comes from node.module. Note that you can also return a
  91. // renderable array rather than rendered HTML for 'content'.
  92. $block = array();
  93. switch ($delta) {
  94. case 'materio_search_api_search':
  95. if (user_access('use materio search api')) {
  96. $block['subject'] = t('Search');
  97. $block['content'] = theme('materio_search_api_search_block', array());
  98. }
  99. break;
  100. case 'materio_search_api_viewmode':
  101. if (user_access('use materio search api viewmode selection')) {
  102. $block['subject'] = t('View mode');
  103. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  104. }
  105. break;
  106. }
  107. return $block;
  108. }
  109. /**
  110. * Implements hook_entity_info_alter().
  111. */
  112. function materio_search_api_entity_info_alter(&$entity_info) {
  113. $entity_info['node']['view modes']['cardsmall'] = array(
  114. 'label' => t('Small cards'),
  115. 'custom settings' => TRUE,
  116. );
  117. $entity_info['node']['view modes']['cardmedium'] = array(
  118. 'label' => t('Medium cards'),
  119. 'custom settings' => TRUE,
  120. );
  121. $entity_info['node']['view modes']['cardbig'] = array(
  122. 'label' => t('Big cards'),
  123. 'custom settings' => TRUE,
  124. );
  125. $entity_info['node']['view modes']['cardfull'] = array(
  126. 'label' => t('Full cards'),
  127. 'custom settings' => TRUE,
  128. );
  129. }
  130. /**
  131. * Implements hook_preprocess_node().
  132. */
  133. function materio_search_api_preprocess_node(&$vars) {
  134. // $vars['theme_hook_suggestions'][] = 'node__'.$vars['view_mode'];
  135. // $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__' . $vars['view_mode'];
  136. // dsm($vars, '$vars');
  137. }
  138. /**
  139. * Implements hook_preprocess_field().
  140. */
  141. function materio_search_api_preprocess_field(&$vars) {
  142. //$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#view_mode'];
  143. //$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_type'] . '__' . $vars['element']['#view_mode'];
  144. //$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_name'] . '__' . $vars['element']['#view_mode'];
  145. // dsm($vars, '$vars');
  146. }
  147. /**
  148. * materiobase_search_form()
  149. */
  150. function materio_search_api_search_form($form, &$form_state){
  151. $form = array();
  152. $args = arg();
  153. $path = array_shift($args);
  154. $keys = implode('/', $args);
  155. $form['searchfield'] = array(
  156. '#type' => 'textfield',
  157. '#default_value' => $path == 'explore' ? $keys : "", // TODO: set the search page path global or a variable in settings
  158. // '#value' => $keys,
  159. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  160. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  161. '#size' => 30,
  162. '#maxlength' => 1024,
  163. );
  164. $form['create'] = array(
  165. '#type' => 'image_button',
  166. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  167. '#value' => t('Search'),
  168. );
  169. return $form;
  170. }
  171. function materio_search_api_search_form_validate($form, &$form_state){
  172. // dsm($form, '$form');
  173. // dsm($form_state, '$form_state');
  174. // dsm(strlen($form_state['values']['searchfield']));
  175. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  176. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  177. }
  178. }
  179. function materio_search_api_search_form_submit($form, &$form_state){
  180. $keys = $form_state['values']['searchfield'];
  181. // drupal_go_to('base/explore/'.$keys);
  182. $form_state['redirect'] = 'explore/'.$keys;
  183. }
  184. /**
  185. * Implements hook_form_alter().
  186. *
  187. * Adds language fields to user forms.
  188. */
  189. // TODO: user can choose preferred view mode
  190. // function materio_search_api_form_alter(&$form, &$form_state, $form_id) {
  191. // if (user_access('use materio search api viewmode selection')) {
  192. // if ($form_id == 'user_register_form' || ($form_id == 'user_profile_form' && $form['#user_category'] == 'account')) {
  193. // materio_search_api_viewmode_selector_form($form, $form_state, $form['#user']);
  194. // }
  195. // }
  196. // }
  197. // function materio_search_api_viewmode_selector_form(&$form, &$form_state, $user) {
  198. // }
  199. /**
  200. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  201. */
  202. /**
  203. * Implements hook_theme().
  204. */
  205. function materio_search_api_theme($existing, $type, $theme, $path) {
  206. return array(
  207. 'materio_search_api_search_block' => array(
  208. 'arguments' => array(),
  209. 'template' => 'materio-search-api-search-block',
  210. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  211. ),
  212. 'materio_search_api_select_viewmode_block' => array(
  213. 'arguments' => array(),
  214. 'template' => 'materio-search-api-select-viewmode-block',
  215. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  216. ),
  217. 'materio_search_api_results' => array(
  218. 'arguments' => array(),
  219. 'template' => 'materio-search-api-results',
  220. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  221. 'variables' => array(
  222. 'index' => NULL,
  223. 'results' => array('result count' => 0),
  224. 'items' => array(),
  225. 'view_mode' => 'teaser',
  226. 'keys' => '',
  227. // 'page_machine_name' => NULL,
  228. 'spellcheck' => NULL,
  229. 'pager' => NULL,
  230. ),
  231. ),
  232. );
  233. }
  234. /**
  235. * template_preprocess_materiobase_search_block();
  236. */
  237. function template_preprocess_materio_search_api_search_block(&$vars){
  238. // dsm($vars, '$vars');
  239. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  240. }
  241. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  242. global $user;
  243. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  244. $availableviewmodes = variable_get('availableviewmodes', -1);
  245. // dsm($availableviewmodes);
  246. $entity_infos = entity_get_info();
  247. // dsm($entity_infos, 'entity_infos');
  248. $content = '<div class="btn-group btn-group-vertical">';
  249. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  250. if(in_array($viewmode, $availableviewmodes)){
  251. $content .= '<div class="viewmode-link viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'" rel="'.$viewmode.'"><span class="inner">'.$value['label'].'</span></div>';
  252. }
  253. }
  254. $content .= '</div>';
  255. $vars['content'] = $content;
  256. }
  257. /**
  258. * Function for preprocessing the variables for the search_api_page_results
  259. * template.
  260. *
  261. * @param array $variables
  262. * An associative array containing:
  263. * - $index: The index this search was executed on.
  264. * - $results: An array of search results, as returned by
  265. * SearchApiQueryInterface::execute().
  266. * - $keys: The keywords of the executed search.
  267. *
  268. * @see materio-search-api-results.tpl.php
  269. */
  270. function template_preprocess_materio_search_api_results(array &$variables) {
  271. // dsm($variables, '$variables');
  272. $results = $variables['results'];
  273. $keys = $variables['keys'];
  274. $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  275. $variables['result_count'] = $results['result count'];
  276. $variables['sec'] = round($results['performance']['complete'], 3);
  277. $variables['search_performance'] = array(
  278. '#theme' => 'search_performance',
  279. '#markup' => format_plural(
  280. $results['result count'],
  281. 'The search found 1 result in @sec seconds.',
  282. 'The search found @count results in @sec seconds.',
  283. array('@sec' => $variables['sec'])
  284. ),
  285. '#prefix' => '<p class="search-performance">',
  286. '#suffix' => '</p>',
  287. );
  288. // $variables['search_results'] = array(
  289. // '#theme' => 'search_results_list',
  290. // 'results' => $variables['results']['results'],
  291. // 'items' => $variables['items'],
  292. // 'index' => $variables['index'],
  293. // 'type' => 'ul',
  294. // );
  295. // dsm($variables, 'variables');
  296. }
  297. /**
  298. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  299. */
  300. /**
  301. * Implements hook_block_view_alter().
  302. */
  303. function materio_search_api_block_view_alter(&$data, $block) {
  304. // this alter prepopulate the search form provide by search_api_page
  305. if ($block->module == 'search_api_page') {
  306. $page = search_api_page_load($block->delta);
  307. $item = menu_get_item();
  308. if (isset($page->path) && $page->path == $item['path']) {
  309. $keys = arg(count(arg(NULL, $page->path)));
  310. if ($keys) {
  311. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  312. $data['content']['keys_' . $page->id]['#value'] = $keys;
  313. }
  314. }
  315. }
  316. }