materio_search_api.module 25 KB

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