materio_search_api.module 18 KB

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