materio_search_api.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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_onthologie_term_'.$i.'_text'] = array(
  126. 'type'=>'text',
  127. 'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  128. // 'query callback'=>'entity_metadata_table_query',
  129. 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
  130. );
  131. }
  132. $properties['materio_search_api_onthologie_term_others_text'] = array(
  133. 'type'=>'list<text>',
  134. 'label'=> t('Others onthologie terms as text (+ synonyms)'),
  135. // 'query callback'=>'entity_metadata_table_query',
  136. 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
  137. );
  138. $properties['materio_search_api_taglibres_text'] = array(
  139. 'type'=>'list<text>',
  140. 'label'=> t('Tag libres terms as text (+ synonyms)'),
  141. // 'query callback'=>'entity_metadata_table_query',
  142. 'getter callback'=>'materio_search_api_get_taglibres_terms_text',
  143. );
  144. $company_term_props = &$info['taxonomy_term']['bundles']['company']['properties'];
  145. $company_term_props['country'] = array(
  146. 'label' => t("Country"),
  147. 'description' => t("Company's Country get from tode node."),
  148. 'type' => 'text',
  149. 'getter callback' => 'company_term_property_country_get_props',
  150. );
  151. }
  152. function materio_search_api_get_onthologie_term_1_text($item){
  153. // dsm($item, 'item');
  154. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 0);
  155. }
  156. function materio_search_api_get_onthologie_term_2_text($item){
  157. // dsm($item, 'item');
  158. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 1);
  159. }
  160. function materio_search_api_get_onthologie_term_3_text($item){
  161. // dsm($item, 'item');
  162. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 2);
  163. }
  164. function materio_search_api_get_onthologie_term_4_text($item){
  165. // dsm($item, 'item');
  166. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 3);
  167. }
  168. function materio_search_api_get_onthologie_term_5_text($item){
  169. // dsm($item, 'item');
  170. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 4);
  171. }
  172. function materio_search_api_get_taxonomy_terms_others_text($item){
  173. // dsm($item, 'item');
  174. $delta = 5;
  175. $terms = array();
  176. while( isset($item->field_onthologie['und'][$delta]) ){
  177. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_onthologie", $delta);
  178. $delta++;
  179. }
  180. return $terms;
  181. }
  182. function materio_search_api_get_taglibres_terms_text($item){
  183. // dsm($item, 'item');
  184. $delta = 0;
  185. $terms = array();
  186. while( isset($item->field_tags_libres['und'][$delta]) ){
  187. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_tags_libres", $delta);
  188. $delta++;
  189. }
  190. return $terms;
  191. }
  192. function materio_search_api_get_taxo_term_field_text($item, $field_name, $delta){
  193. // dsm($item, 'item');
  194. // dsm($delta, 'delta');
  195. if(isset($item->{$field_name}['und'][$delta])){
  196. // print '** item **'."\n";
  197. // print_r($item);
  198. $term = taxonomy_term_load($item->{$field_name}['und'][$delta]['tid']);
  199. // use entity metadata wrappers as they are SMART
  200. $wrapper = entity_metadata_wrapper('taxonomy_term', $term);
  201. // print '** wrapper **'."\n";
  202. // print_r($wrapper);
  203. // print "\n";
  204. $keywords[] = $wrapper->language($item->language)->name_field->raw();
  205. // print '** term_name **'."\n";
  206. // print_r($term->name);
  207. // print "\n";
  208. // print_r($term_name);
  209. // print "\n";
  210. $synonymes = array();
  211. foreach ($wrapper->language($item->language)->synonyms_synonym->value() as $index => $synonym) {
  212. // print '** synonym **'."\n";
  213. // print_r($synonym);
  214. // print "\n";
  215. $keywords[] = $synonym;
  216. }
  217. return implode(" ", $keywords);
  218. }
  219. return null;
  220. }
  221. function company_term_property_country_get_props($term){
  222. // dsm($term, 'company_term_property_country_get_props : term');
  223. if( $node = company_get_tode_node($term) ){
  224. // $field_values = field_get_items('node',$node,'field_public_address');
  225. // dsm($field_values, 'field_values');
  226. $output = rip_tags(render(field_view_field('node',$node,'field_public_address')));
  227. // dsm($output, 'output');
  228. return $output;
  229. }
  230. return null;
  231. }
  232. function company_get_tode_node($term){
  233. if(module_exists('tode'))
  234. if( $entitys = tode_get_nids_from_term($term))
  235. if(isset($entitys['node']))
  236. foreach ($entitys['node'] as $nid => $n)
  237. return node_load($nid);
  238. return false;
  239. }
  240. function rip_tags($string) {
  241. // ----- remove HTML TAGs -----
  242. $string = preg_replace ('/<[^>]*>/', ' ', $string);
  243. // ----- remove control characters -----
  244. $string = str_replace("\r", '', $string); // --- replace with empty space
  245. $string = str_replace("\n", ' ', $string); // --- replace with space
  246. $string = str_replace("\t", ' ', $string); // --- replace with space
  247. // $string = str_replace("&nbsp;", ' ', $string); // --- replace with space
  248. // $string = str_replace("&#039;", '\'', $string); // --- replace with space
  249. // ----- remove multiple spaces -----
  250. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  251. // ----- remove html entities
  252. preg_match_all('/&[^;]+;/', $string, $entities);
  253. foreach ($entities[0] as $entity) {
  254. $string = str_replace($entity, mb_convert_encoding($entity, 'UTF-8', 'HTML-ENTITIES'), $string);
  255. }
  256. return $string;
  257. }
  258. /**
  259. * Implements hook_block_info().
  260. */
  261. function materio_search_api_block_info() {
  262. // This example comes from node.module.
  263. $blocks['materio_search_api_search'] = array(
  264. 'info' => t('Materio search api search'),
  265. 'cache' => DRUPAL_NO_CACHE
  266. );
  267. $blocks['materio_search_api_viewmode'] = array(
  268. 'info' => t('Materio search api view mode selection'),
  269. 'cache' => DRUPAL_NO_CACHE
  270. );
  271. // $blocks['materio_search_api_filters'] = array(
  272. // 'info' => t('Materio search api filters'),
  273. // 'cache' => DRUPAL_NO_CACHE
  274. // );
  275. return $blocks;
  276. }
  277. /**
  278. * Implements hook_block_view().
  279. */
  280. function materio_search_api_block_view($delta = '') {
  281. // This example comes from node.module. Note that you can also return a
  282. // renderable array rather than rendered HTML for 'content'.
  283. $block = array();
  284. switch ($delta) {
  285. case 'materio_search_api_search':
  286. if (user_access('use materio search api') || user_access('use materio search api for breves')) {
  287. $block['subject'] = t('Search');
  288. $block['content'] = theme('materio_search_api_search_block', array());
  289. }
  290. break;
  291. case 'materio_search_api_viewmode':
  292. if (user_access('use materio search api viewmode selection')) {
  293. $block['subject'] = t('View mode');
  294. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  295. }
  296. break;
  297. // case 'materio_search_api_filters':
  298. // if (user_access('use materio search api filters')) {
  299. // $block['subject'] = t('Filters');
  300. // $block['content'] = theme('materio_search_api_filters_block', array());
  301. // }
  302. // break;
  303. }
  304. return $block;
  305. }
  306. /**
  307. * Implements hook_entity_info_alter().
  308. */
  309. function materio_search_api_entity_info_alter(&$entity_info) {
  310. $entity_info['node']['view modes']['cardsmall'] = array(
  311. 'label' => t('Small cards'),
  312. 'custom settings' => TRUE,
  313. );
  314. $entity_info['node']['view modes']['cardmedium'] = array(
  315. 'label' => t('Medium cards'),
  316. 'custom settings' => TRUE,
  317. );
  318. $entity_info['node']['view modes']['cardbig'] = array(
  319. 'label' => t('Big cards'),
  320. 'custom settings' => TRUE,
  321. );
  322. $entity_info['node']['view modes']['cardfull'] = array(
  323. 'label' => t('Full cards'),
  324. 'custom settings' => TRUE,
  325. );
  326. }
  327. /**
  328. * Implements hook_node_view_alter().
  329. */
  330. function materio_search_api_node_view_alter(&$build) {
  331. $node = $build['#node'];
  332. if (arg(0) == 'node' && arg(1) == $node->nid) {
  333. // dsm($build, 'build');
  334. global $user;
  335. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  336. // dsm($viewmode, 'viewmode');
  337. $node = $build['#node'];
  338. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  339. $build = node_view($node, $viewmode);
  340. }
  341. }
  342. }
  343. /**
  344. * Implements hook_node_view().
  345. */
  346. function materio_search_api_node_view($node, $view_mode, $langcode) {
  347. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  348. # PRINT 7.1
  349. print_node_view($node, 'full');
  350. print_pdf_node_view($node, 'full');
  351. # PRINT 7.2-beta
  352. //print_ui_node_view($node, 'full');
  353. // dsm($node, 'node');
  354. }
  355. }
  356. /**
  357. * materiobase_search_form()
  358. */
  359. function materio_search_api_search_form($form, &$form_state){
  360. // dsm($form_state, 'form_state');
  361. // dsm($form, 'form');
  362. global $user;
  363. $form = array();
  364. $args = arg();
  365. $path = array_shift($args);
  366. $keys = implode('/', $args);
  367. $form['searchfield'] = array(
  368. '#type' => 'textfield',
  369. '#default_value' => $path == 'explore' ? $keys : "", // TODO: set the search page path global or a variable in settings
  370. // '#value' => $keys,
  371. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  372. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  373. '#size' => 30,
  374. '#maxlength' => 1024,
  375. );
  376. if(user_access('use materio search api filters')){
  377. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  378. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  379. foreach ($indexed_bundles as $bundle) {
  380. $bundles_options[$bundle] = $bundle;
  381. $default_bundles[] = $bundle;
  382. }
  383. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  384. $form['bundles_filter'] = array(
  385. '#type'=>'checkboxes',
  386. '#options' => $bundles_options,
  387. '#default_value' => $user_bundles_filter,
  388. // '#attributes' => array('class'=>array('btn-group')),
  389. );
  390. }
  391. $form['create'] = array(
  392. '#type' => 'image_button',
  393. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  394. '#value' => t('Search'),
  395. );
  396. return $form;
  397. }
  398. function materio_search_api_search_form_validate($form, &$form_state){
  399. // dsm($form, '$form');
  400. // dsm($form_state, '$form_state');
  401. // dsm(strlen($form_state['values']['searchfield']));
  402. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  403. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  404. }
  405. }
  406. function materio_search_api_search_form_submit($form, &$form_state){
  407. // dsm($form_state, 'form_state');
  408. global $user;
  409. if(user_access('use materio search api filters')){
  410. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  411. if($value)
  412. $bundles[] = $bundle;
  413. # if no filter checked we checked them all by default
  414. if(!isset($bundles))
  415. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  416. $bundles[] = $bundle;
  417. }else{
  418. # if user have no access to filters
  419. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  420. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  421. foreach ($indexed_bundles as $bundle) {
  422. $bundles[] = $bundle;
  423. }
  424. }
  425. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  426. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  427. }
  428. /**
  429. * viewmode
  430. */
  431. function _materio_search_api_change_viewmode($vm){
  432. // dsm($vm, '_materio_search_api_change_viewmode');
  433. global $user;
  434. // dsm($user, 'user');
  435. $entity_infos = entity_get_info();
  436. // dsm($entity_infos, 'entity_infos');
  437. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  438. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  439. $rep = array('statut'=>'saved');
  440. }else{
  441. $rep = array('statut'=>'viewmode not allowed');
  442. }
  443. return $rep;
  444. }
  445. /**
  446. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  447. */
  448. /**
  449. * Implements hook_theme().
  450. */
  451. function materio_search_api_theme($existing, $type, $theme, $path) {
  452. return array(
  453. 'materio_search_api_search_block' => array(
  454. 'arguments' => array(),
  455. 'template' => 'materio-search-api-search-block',
  456. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  457. ),
  458. 'materio_search_api_select_viewmode_block' => array(
  459. 'arguments' => array(),
  460. 'template' => 'materio-search-api-select-viewmode-block',
  461. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  462. ),
  463. // 'materio_search_api_filters_block' => array(
  464. // 'arguments' => array(),
  465. // 'template' => 'materio-search-api-filters-block',
  466. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  467. // ),
  468. 'materio_search_api_results' => array(
  469. 'arguments' => array(),
  470. 'template' => 'materio-search-api-results',
  471. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  472. 'variables' => array(
  473. 'index' => NULL,
  474. 'results' => array('result count' => 0),
  475. 'items' => array(),
  476. 'view_mode' => 'teaser',
  477. 'keys' => '',
  478. // 'page_machine_name' => NULL,
  479. 'spellcheck' => NULL,
  480. 'pager' => NULL,
  481. ),
  482. ),
  483. // 'materio_search_api_performance' => array(
  484. // 'render element' => 'element',
  485. // ),
  486. 'materio_search_api_actuality' => array(
  487. 'template' => 'materio-search-api-actuality',
  488. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  489. 'arguments' => array(
  490. 'items' => array(),
  491. 'view_mode' => "teaser",
  492. 'pager' => NULL,
  493. 'count' => 0,
  494. )
  495. )
  496. );
  497. }
  498. /**
  499. * Implements theme for rendering search-performance
  500. */
  501. // function theme_materio_search_api_performance($variables) {
  502. // $element = array_shift($variables);
  503. // return $element['#markup'];
  504. // }
  505. /**
  506. * template_preprocess_materiobase_search_block();
  507. */
  508. function template_preprocess_materio_search_api_search_block(&$vars){
  509. // dsm($vars, '$vars');
  510. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  511. }
  512. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  513. global $user;
  514. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  515. $availableviewmodes = variable_get('availableviewmodes', -1);
  516. // dsm($availableviewmodes);
  517. $entity_infos = entity_get_info();
  518. // dsm($entity_infos, 'entity_infos');
  519. $content = '<div class="btn-group btn-group-vertical">';
  520. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  521. if(in_array($viewmode, $availableviewmodes)){
  522. $link = l(
  523. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  524. 'materiosearchapi/viewmode/change/'.$viewmode,
  525. array(
  526. 'query' => drupal_get_destination(),
  527. 'html' => true,
  528. 'attributes' => array(
  529. 'class' => array(
  530. 'viewmode-link',
  531. 'viewmode-'.$viewmode,
  532. $active == $viewmode ? " active" : ''
  533. ),
  534. 'rel' => $viewmode
  535. )
  536. )
  537. );
  538. $content .= $link;
  539. }
  540. }
  541. $content .= '</div>';
  542. $vars['content'] = $content;
  543. }
  544. // function template_preprocess_materio_search_api_filters_block(&$vars){
  545. // $index_machine_name = variable_get('mainsearchindex', -1);
  546. // $index = search_api_index_load($index_machine_name);
  547. // dsm($index, 'index');
  548. // // $entity_infos = entity_get_info($index->item_type);
  549. // // dsm($entity_infos, 'entity_infos');
  550. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  551. // dsm($indexed_bundles, 'indexed_bundles');
  552. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  553. // }
  554. // function materio_search_api_filters_form($form, $form_state, $bundles){
  555. // $form = array();
  556. // dsm($bundles, 'bundles');
  557. // foreach ($bundles as $bundle) {
  558. // $form[$bundle . '_filter'] = array(
  559. // '#type'=>'checkbox',
  560. // '#default_value' => -1,
  561. // '#title' => $bundle,
  562. // );
  563. // }
  564. // return $form;
  565. // }
  566. /**
  567. * Function for preprocessing the variables for the search_api_page_results
  568. * template.
  569. *
  570. * @param array $variables
  571. * An associative array containing:
  572. * - $index: The index this search was executed on.
  573. * - $results: An array of search results, as returned by
  574. * SearchApiQueryInterface::execute().
  575. * - $keys: The keywords of the executed search.
  576. *
  577. * @see materio-search-api-results.tpl.php
  578. */
  579. function template_preprocess_materio_search_api_results(array &$variables) {
  580. // dsm($variables, '$variables');
  581. $results = $variables['results'];
  582. $keys = $variables['keys'];
  583. // $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  584. $variables['result_count'] = $results['result count'];
  585. $variables['sec'] = round($results['performance']['complete'], 3);
  586. if(isset($results['breves count'])){
  587. $variables['search_performance'] = format_plural(
  588. $results['breves count'],
  589. 'The search found 1 news ',
  590. 'The search found @count news '
  591. );
  592. $variables['search_performance'] .= format_plural(
  593. $variables['result_count'] - $results['breves count'],
  594. 'with 1 associated matter.',
  595. 'with @count associated matters.'
  596. );
  597. $variables['search_performance'] .= format_plural(
  598. $results['could results']['result count'],
  599. ' You could find 1 result with a ',
  600. ' You could find @count results with a '
  601. );
  602. $variables['search_performance'] .= l(t('full access to materiO\'.'), 'node/11187');
  603. }else{
  604. $variables['search_performance'] = format_plural(
  605. // $results['result count'],
  606. $variables['result_count'],
  607. 'The search found 1 result.',
  608. 'The search found @count results.'
  609. );
  610. }
  611. //dsm($variables, 'variables');
  612. }
  613. function template_preprocess_materio_search_api_actuality(&$vars){
  614. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  615. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  616. $vars['actualities_infos'] = t('');
  617. }
  618. /**
  619. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  620. */
  621. /**
  622. * Implements hook_block_view_alter().
  623. */
  624. function materio_search_api_block_view_alter(&$data, $block) {
  625. // this alter prepopulate the search form provide by search_api_page
  626. if ($block->module == 'search_api_page') {
  627. $page = search_api_page_load($block->delta);
  628. $item = menu_get_item();
  629. if (isset($page->path) && $page->path == $item['path']) {
  630. $keys = arg(count(arg(NULL, $page->path)));
  631. if ($keys) {
  632. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  633. $data['content']['keys_' . $page->id]['#value'] = $keys;
  634. }
  635. }
  636. }
  637. }