materio_search_api.module 24 KB

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