materio_search_api.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. 'use materio search api filters' => array(
  33. 'title' => t('use Materio search api filters'),
  34. 'description' => t('Use materio search api filters.'),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_menu().
  40. */
  41. function materio_search_api_menu() {
  42. $items = array();
  43. $base = array(
  44. 'type' => MENU_CALLBACK,
  45. 'file' => 'materio_search_api.pages.inc',
  46. );
  47. $items['admin/config/search/search_api/materiosearchapi'] = array(
  48. 'title' => 'Materio Search Api',
  49. 'page callback' => 'drupal_get_form',
  50. 'page arguments' => array('materio_search_api_settings'),
  51. 'access arguments' => array('administer materio_search_api'),
  52. 'file' => 'materio_search_api.admin.inc',
  53. 'type' => MENU_LOCAL_TASK,
  54. );
  55. $items['materiosearchapi/autocomplete/dbselect'] = $base+array(
  56. 'access arguments' => array('use materio search api autocomplete'),
  57. 'page callback' => 'materio_search_api_autocomplete_dbselect',
  58. );
  59. $items['materiosearchapi/autocomplete/searchapi'] = $base+array(
  60. 'access arguments' => array('use materio search api autocomplete'),
  61. 'page callback' => 'materio_search_api_autocomplete_searchapi',
  62. );
  63. $items['explore'] = $base+array(
  64. 'access arguments' => array('use materio search api'),
  65. 'page callback' => 'materio_search_api_results_search',
  66. 'title' => t('Explore'),
  67. //'page argument' => array(1,2,3),
  68. );
  69. $items['actuality'] = $base+array(
  70. // 'access arguments' => array(),
  71. 'page callback' => 'materio_search_api_actuality',
  72. // 'page argument' => array(),
  73. 'access callback' => TRUE,
  74. );
  75. $items['materiosearchapi/viewmode/change'] = $base+array(
  76. 'access arguments' => array('use materio search api'),
  77. 'page callback' => 'materio_search_api_viewmode_change',
  78. 'page argument' => array(3),
  79. );
  80. return $items;
  81. }
  82. /**
  83. * Implements hook_block_info().
  84. */
  85. function materio_search_api_block_info() {
  86. // This example comes from node.module.
  87. $blocks['materio_search_api_search'] = array(
  88. 'info' => t('Materio search api search'),
  89. 'cache' => DRUPAL_NO_CACHE
  90. );
  91. $blocks['materio_search_api_viewmode'] = array(
  92. 'info' => t('Materio search api view mode selection'),
  93. 'cache' => DRUPAL_NO_CACHE
  94. );
  95. // $blocks['materio_search_api_filters'] = array(
  96. // 'info' => t('Materio search api filters'),
  97. // 'cache' => DRUPAL_NO_CACHE
  98. // );
  99. return $blocks;
  100. }
  101. /**
  102. * Implements hook_block_view().
  103. */
  104. function materio_search_api_block_view($delta = '') {
  105. // This example comes from node.module. Note that you can also return a
  106. // renderable array rather than rendered HTML for 'content'.
  107. $block = array();
  108. switch ($delta) {
  109. case 'materio_search_api_search':
  110. if (user_access('use materio search api')) {
  111. $block['subject'] = t('Search');
  112. $block['content'] = theme('materio_search_api_search_block', array());
  113. }
  114. break;
  115. case 'materio_search_api_viewmode':
  116. if (user_access('use materio search api viewmode selection')) {
  117. $block['subject'] = t('View mode');
  118. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  119. }
  120. break;
  121. // case 'materio_search_api_filters':
  122. // if (user_access('use materio search api filters')) {
  123. // $block['subject'] = t('Filters');
  124. // $block['content'] = theme('materio_search_api_filters_block', array());
  125. // }
  126. // break;
  127. }
  128. return $block;
  129. }
  130. /**
  131. * Implements hook_entity_info_alter().
  132. */
  133. function materio_search_api_entity_info_alter(&$entity_info) {
  134. $entity_info['node']['view modes']['cardsmall'] = array(
  135. 'label' => t('Small cards'),
  136. 'custom settings' => TRUE,
  137. );
  138. $entity_info['node']['view modes']['cardmedium'] = array(
  139. 'label' => t('Medium cards'),
  140. 'custom settings' => TRUE,
  141. );
  142. $entity_info['node']['view modes']['cardbig'] = array(
  143. 'label' => t('Big cards'),
  144. 'custom settings' => TRUE,
  145. );
  146. $entity_info['node']['view modes']['cardfull'] = array(
  147. 'label' => t('Full cards'),
  148. 'custom settings' => TRUE,
  149. );
  150. }
  151. /**
  152. * materiobase_search_form()
  153. */
  154. function materio_search_api_search_form($form, &$form_state){
  155. // dsm($form_state, 'form_state');
  156. // dsm($form, 'form');
  157. global $user;
  158. $form = array();
  159. if(user_access('use materio search api filters')){
  160. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  161. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  162. foreach ($indexed_bundles as $bundle) {
  163. $bundles_options[$bundle] = $bundle;
  164. $default_bundles[] = $bundle;
  165. }
  166. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  167. $form['bundles_filter'] = array(
  168. '#type'=>'checkboxes',
  169. '#options' => $bundles_options,
  170. '#default_value' => $user_bundles_filter,
  171. '#attributes' => array('class'=>array('btn-group')),
  172. );
  173. }
  174. $args = arg();
  175. $path = array_shift($args);
  176. $keys = implode('/', $args);
  177. $form['searchfield'] = array(
  178. '#type' => 'textfield',
  179. '#default_value' => $path == 'explore' ? $keys : "", // TODO: set the search page path global or a variable in settings
  180. // '#value' => $keys,
  181. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  182. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  183. '#size' => 30,
  184. '#maxlength' => 1024,
  185. );
  186. $form['create'] = array(
  187. '#type' => 'image_button',
  188. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  189. '#value' => t('Search'),
  190. );
  191. return $form;
  192. }
  193. function materio_search_api_search_form_validate($form, &$form_state){
  194. // dsm($form, '$form');
  195. // dsm($form_state, '$form_state');
  196. // dsm(strlen($form_state['values']['searchfield']));
  197. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  198. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  199. }
  200. }
  201. function materio_search_api_search_form_submit($form, &$form_state){
  202. // dsm($form_state, 'form_state');
  203. global $user;
  204. if(user_access('use materio search api filters')){
  205. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  206. if($value)
  207. $bundles[] = $bundle;
  208. # if no filter checked we checked them all by default
  209. if(!isset($bundles))
  210. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  211. $bundles[] = $bundle;
  212. }else{
  213. # if user have no access to filters
  214. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  215. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  216. foreach ($indexed_bundles as $bundle) {
  217. $bundles[] = $bundle;
  218. }
  219. }
  220. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  221. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  222. }
  223. /**
  224. * Implements hook_form_alter().
  225. *
  226. * Adds language fields to user forms.
  227. */
  228. // TODO: user can choose preferred view mode
  229. // function materio_search_api_form_alter(&$form, &$form_state, $form_id) {
  230. // if (user_access('use materio search api viewmode selection')) {
  231. // if ($form_id == 'user_register_form' || ($form_id == 'user_profile_form' && $form['#user_category'] == 'account')) {
  232. // materio_search_api_viewmode_selector_form($form, $form_state, $form['#user']);
  233. // }
  234. // }
  235. // }
  236. // function materio_search_api_viewmode_selector_form(&$form, &$form_state, $user) {
  237. // }
  238. /**
  239. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  240. */
  241. /**
  242. * Implements hook_theme().
  243. */
  244. function materio_search_api_theme($existing, $type, $theme, $path) {
  245. return array(
  246. 'materio_search_api_search_block' => array(
  247. 'arguments' => array(),
  248. 'template' => 'materio-search-api-search-block',
  249. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  250. ),
  251. 'materio_search_api_select_viewmode_block' => array(
  252. 'arguments' => array(),
  253. 'template' => 'materio-search-api-select-viewmode-block',
  254. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  255. ),
  256. // 'materio_search_api_filters_block' => array(
  257. // 'arguments' => array(),
  258. // 'template' => 'materio-search-api-filters-block',
  259. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  260. // ),
  261. 'materio_search_api_results' => array(
  262. 'arguments' => array(),
  263. 'template' => 'materio-search-api-results',
  264. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  265. 'variables' => array(
  266. 'index' => NULL,
  267. 'results' => array('result count' => 0),
  268. 'items' => array(),
  269. 'view_mode' => 'teaser',
  270. 'keys' => '',
  271. // 'page_machine_name' => NULL,
  272. 'spellcheck' => NULL,
  273. 'pager' => NULL,
  274. ),
  275. ),
  276. // 'materio_search_api_performance' => array(
  277. // 'render element' => 'element',
  278. // ),
  279. 'materio_search_api_actuality' => array(
  280. 'template' => 'materio-search-api-actuality',
  281. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  282. 'arguments' => array(
  283. 'items' => array(),
  284. 'view_mode' => "teaser",
  285. 'pager' => NULL,
  286. 'count' => 0,
  287. )
  288. )
  289. );
  290. }
  291. /**
  292. * Implements theme for rendering search-performance
  293. */
  294. // function theme_materio_search_api_performance($variables) {
  295. // $element = array_shift($variables);
  296. // return $element['#markup'];
  297. // }
  298. /**
  299. * template_preprocess_materiobase_search_block();
  300. */
  301. function template_preprocess_materio_search_api_search_block(&$vars){
  302. // dsm($vars, '$vars');
  303. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  304. }
  305. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  306. global $user;
  307. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  308. $availableviewmodes = variable_get('availableviewmodes', -1);
  309. // dsm($availableviewmodes);
  310. $entity_infos = entity_get_info();
  311. // dsm($entity_infos, 'entity_infos');
  312. $content = '<div class="btn-group btn-group-vertical">';
  313. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  314. if(in_array($viewmode, $availableviewmodes)){
  315. $content .= '<div class="viewmode-link viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'" rel="'.$viewmode.'"><span class="inner">'.$value['label'].'</span></div>';
  316. }
  317. }
  318. $content .= '</div>';
  319. $vars['content'] = $content;
  320. }
  321. // function template_preprocess_materio_search_api_filters_block(&$vars){
  322. // $index_machine_name = variable_get('mainsearchindex', -1);
  323. // $index = search_api_index_load($index_machine_name);
  324. // dsm($index, 'index');
  325. // // $entity_infos = entity_get_info($index->item_type);
  326. // // dsm($entity_infos, 'entity_infos');
  327. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  328. // dsm($indexed_bundles, 'indexed_bundles');
  329. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  330. // }
  331. // function materio_search_api_filters_form($form, $form_state, $bundles){
  332. // $form = array();
  333. // dsm($bundles, 'bundles');
  334. // foreach ($bundles as $bundle) {
  335. // $form[$bundle . '_filter'] = array(
  336. // '#type'=>'checkbox',
  337. // '#default_value' => -1,
  338. // '#title' => $bundle,
  339. // );
  340. // }
  341. // return $form;
  342. // }
  343. /**
  344. * Function for preprocessing the variables for the search_api_page_results
  345. * template.
  346. *
  347. * @param array $variables
  348. * An associative array containing:
  349. * - $index: The index this search was executed on.
  350. * - $results: An array of search results, as returned by
  351. * SearchApiQueryInterface::execute().
  352. * - $keys: The keywords of the executed search.
  353. *
  354. * @see materio-search-api-results.tpl.php
  355. */
  356. function template_preprocess_materio_search_api_results(array &$variables) {
  357. // dsm($variables, '$variables');
  358. $results = $variables['results'];
  359. $keys = $variables['keys'];
  360. $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  361. $variables['result_count'] = $results['result count'];
  362. $variables['sec'] = round($results['performance']['complete'], 3);
  363. $variables['search_performance'] = format_plural(
  364. $results['result count'],
  365. 'The search found 1 result.', // in @sec seconds
  366. 'The search found @count results.', // in @sec seconds
  367. array('@sec' => $variables['sec'])
  368. );
  369. // $variables['search_results'] = array(
  370. // '#theme' => 'search_results_list',
  371. // 'results' => $variables['results']['results'],
  372. // 'items' => $variables['items'],
  373. // 'index' => $variables['index'],
  374. // 'type' => 'ul',
  375. // );
  376. //dsm($variables, 'variables');
  377. }
  378. function template_preprocess_materio_search_api_actuality(&$vars){
  379. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  380. // $items = array();
  381. // foreach ($vars['items'] as $nid => $item) {
  382. // $items[] = node_load($nid);
  383. // }
  384. // $vars['items'] = $items;
  385. }
  386. /**
  387. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  388. */
  389. /**
  390. * Implements hook_block_view_alter().
  391. */
  392. function materio_search_api_block_view_alter(&$data, $block) {
  393. // this alter prepopulate the search form provide by search_api_page
  394. if ($block->module == 'search_api_page') {
  395. $page = search_api_page_load($block->delta);
  396. $item = menu_get_item();
  397. if (isset($page->path) && $page->path == $item['path']) {
  398. $keys = arg(count(arg(NULL, $page->path)));
  399. if ($keys) {
  400. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  401. $data['content']['keys_' . $page->id]['#value'] = $keys;
  402. }
  403. }
  404. }
  405. }