materio_search_api.module 20 KB

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