materio_search_api.module 14 KB

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