materio_search_api.module 19 KB

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