materio_search_api.module 27 KB

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