materio_search_api.module 16 KB

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