materio_search_api.module 28 KB

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