materio_search_api.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. // restrict display on node page for materiau and breve
  341. $block_is_visible = true;
  342. $q = parse_url($_GET['q']);
  343. $parts = explode('/', $q['path']);
  344. if($parts[0] == "node"){
  345. $node = node_load($parts[1]);
  346. $block_is_visible = in_array($node->type, array('materiau','breve')) ? true : false;
  347. }
  348. if (user_access('use materio search api viewmode selection') && $block_is_visible) {
  349. $block['subject'] = t('View mode');
  350. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  351. }
  352. break;
  353. // case 'materio_search_api_filters':
  354. // if (user_access('use materio search api filters')) {
  355. // $block['subject'] = t('Filters');
  356. // $block['content'] = theme('materio_search_api_filters_block', array());
  357. // }
  358. // break;
  359. }
  360. return $block;
  361. }
  362. /**
  363. * Implements hook_entity_info_alter().
  364. */
  365. function materio_search_api_entity_info_alter(&$entity_info) {
  366. $entity_info['node']['view modes']['cardsmall'] = array(
  367. 'label' => t('Small cards'),
  368. 'custom settings' => TRUE,
  369. );
  370. $entity_info['node']['view modes']['cardmedium'] = array(
  371. 'label' => t('Medium cards'),
  372. 'custom settings' => TRUE,
  373. );
  374. $entity_info['node']['view modes']['cardbig'] = array(
  375. 'label' => t('Big cards'),
  376. 'custom settings' => TRUE,
  377. );
  378. $entity_info['node']['view modes']['cardfull'] = array(
  379. 'label' => t('Full cards'),
  380. 'custom settings' => TRUE,
  381. );
  382. }
  383. /**
  384. * Implements hook_node_view_alter().
  385. */
  386. function materio_search_api_node_view_alter(&$build) {
  387. $node = $build['#node'];
  388. if (arg(0) == 'node' && arg(1) == $node->nid) {
  389. // dsm($build, 'build');
  390. global $user;
  391. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  392. // dsm($viewmode, 'viewmode');
  393. $node = $build['#node'];
  394. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  395. $build = node_view($node, $viewmode);
  396. }
  397. }
  398. }
  399. /**
  400. * Implements hook_node_view().
  401. */
  402. function materio_search_api_node_view($node, $view_mode, $langcode) {
  403. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  404. # PRINT 7.1
  405. print_node_view($node, 'full');
  406. print_pdf_node_view($node, 'full');
  407. # PRINT 7.2-beta
  408. //print_ui_node_view($node, 'full');
  409. // dsm($node, 'node');
  410. }
  411. }
  412. /**
  413. * materiobase_search_form()
  414. */
  415. function materio_search_api_search_form($form, &$form_state){
  416. // dsm($form_state, 'form_state');
  417. // dsm($form, 'form');
  418. global $user;
  419. $form = array();
  420. $args = arg();
  421. $path = array_shift($args);
  422. $keys = implode('/', $args);
  423. if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters
  424. $query = new EntityFieldQuery();
  425. $query->entityCondition('entity_type', 'node')
  426. ->entityCondition('bundle', 'materiau')
  427. ->propertyCondition('status', 1);
  428. $count = $query->count()->execute();
  429. $default_value = t("search among our !fiches cards", array("!fiches"=>$count));
  430. }else{
  431. $default_value = '';
  432. }
  433. $form['searchfield'] = array(
  434. '#type' => 'textfield',
  435. '#default_value' => $path == 'explore' ? $keys : $default_value, // TODO: set the search page path global or a variable in settings
  436. // '#value' => $keys,
  437. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  438. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  439. '#size' => 30,
  440. '#maxlength' => 1024,
  441. '#attributes' => array("default"=>$default_value),
  442. );
  443. if(user_access('use materio search api filters')){
  444. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  445. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  446. foreach ($indexed_bundles as $bundle) {
  447. $bundles_options[$bundle] = $bundle;
  448. $default_bundles[] = $bundle;
  449. }
  450. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  451. $form['bundles_filter'] = array(
  452. '#type'=>'checkboxes',
  453. '#options' => $bundles_options,
  454. '#default_value' => $user_bundles_filter,
  455. // '#attributes' => array('class'=>array('btn-group')),
  456. );
  457. }
  458. $form['create'] = array(
  459. '#type' => 'image_button',
  460. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  461. '#value' => t('Search'),
  462. );
  463. return $form;
  464. }
  465. function materio_search_api_search_form_validate($form, &$form_state){
  466. // dsm($form, '$form');
  467. // dsm($form_state, '$form_state');
  468. // dsm(strlen($form_state['values']['searchfield']));
  469. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  470. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  471. }
  472. }
  473. function materio_search_api_search_form_submit($form, &$form_state){
  474. // dsm($form_state, 'form_state');
  475. global $user;
  476. if(user_access('use materio search api filters')){
  477. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  478. if($value)
  479. $bundles[] = $bundle;
  480. # if no filter checked we checked them all by default
  481. if(!isset($bundles))
  482. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  483. $bundles[] = $bundle;
  484. }else{
  485. # if user have no access to filters
  486. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  487. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  488. foreach ($indexed_bundles as $bundle) {
  489. $bundles[] = $bundle;
  490. }
  491. }
  492. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  493. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  494. }
  495. /**
  496. * viewmode
  497. */
  498. function _materio_search_api_change_viewmode($vm){
  499. // dsm($vm, '_materio_search_api_change_viewmode');
  500. global $user;
  501. // dsm($user, 'user');
  502. $entity_infos = entity_get_info();
  503. // dsm($entity_infos, 'entity_infos');
  504. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  505. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  506. $rep = array('statut'=>'saved');
  507. }else{
  508. $rep = array('statut'=>'viewmode not allowed');
  509. }
  510. return $rep;
  511. }
  512. /**
  513. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  514. */
  515. /**
  516. * Implements hook_theme().
  517. */
  518. function materio_search_api_theme($existing, $type, $theme, $path) {
  519. return array(
  520. 'materio_search_api_search_block' => array(
  521. 'arguments' => array(),
  522. 'template' => 'materio-search-api-search-block',
  523. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  524. ),
  525. 'materio_search_api_select_viewmode_block' => array(
  526. 'arguments' => array(),
  527. 'template' => 'materio-search-api-select-viewmode-block',
  528. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  529. ),
  530. // 'materio_search_api_filters_block' => array(
  531. // 'arguments' => array(),
  532. // 'template' => 'materio-search-api-filters-block',
  533. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  534. // ),
  535. 'materio_search_api_results' => array(
  536. 'arguments' => array(),
  537. 'template' => 'materio-search-api-results',
  538. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  539. 'variables' => array(
  540. 'index' => NULL,
  541. 'results' => array('result count' => 0),
  542. 'items' => array(),
  543. 'view_mode' => 'teaser',
  544. 'keys' => '',
  545. // 'page_machine_name' => NULL,
  546. 'spellcheck' => NULL,
  547. 'pager' => NULL,
  548. ),
  549. ),
  550. // 'materio_search_api_performance' => array(
  551. // 'render element' => 'element',
  552. // ),
  553. 'materio_search_api_actuality' => array(
  554. 'template' => 'materio-search-api-actuality',
  555. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  556. 'arguments' => array(
  557. 'items' => array(),
  558. 'view_mode' => "teaser",
  559. 'pager' => NULL,
  560. 'count' => 0,
  561. )
  562. )
  563. );
  564. }
  565. /**
  566. * Implements theme for rendering search-performance
  567. */
  568. // function theme_materio_search_api_performance($variables) {
  569. // $element = array_shift($variables);
  570. // return $element['#markup'];
  571. // }
  572. /**
  573. * template_preprocess_materiobase_search_block();
  574. */
  575. function template_preprocess_materio_search_api_search_block(&$vars){
  576. // dsm($vars, '$vars');
  577. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  578. }
  579. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  580. global $user;
  581. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  582. $availableviewmodes = variable_get('availableviewmodes', -1);
  583. // dsm($availableviewmodes);
  584. $entity_infos = entity_get_info();
  585. // dsm($entity_infos, 'entity_infos');
  586. $content = '<div class="btn-group btn-group-vertical">';
  587. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  588. if(in_array($viewmode, $availableviewmodes)){
  589. $link = l(
  590. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  591. 'materiosearchapi/viewmode/change/'.$viewmode,
  592. array(
  593. 'query' => drupal_get_destination(),
  594. 'html' => true,
  595. 'attributes' => array(
  596. 'class' => array(
  597. 'viewmode-link',
  598. 'viewmode-'.$viewmode,
  599. $active == $viewmode ? " active" : ''
  600. ),
  601. 'rel' => $viewmode
  602. )
  603. )
  604. );
  605. $content .= $link;
  606. }
  607. }
  608. $content .= '</div>';
  609. $vars['content'] = $content;
  610. }
  611. // function template_preprocess_materio_search_api_filters_block(&$vars){
  612. // $index_machine_name = variable_get('mainsearchindex', -1);
  613. // $index = search_api_index_load($index_machine_name);
  614. // dsm($index, 'index');
  615. // // $entity_infos = entity_get_info($index->item_type);
  616. // // dsm($entity_infos, 'entity_infos');
  617. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  618. // dsm($indexed_bundles, 'indexed_bundles');
  619. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  620. // }
  621. // function materio_search_api_filters_form($form, $form_state, $bundles){
  622. // $form = array();
  623. // dsm($bundles, 'bundles');
  624. // foreach ($bundles as $bundle) {
  625. // $form[$bundle . '_filter'] = array(
  626. // '#type'=>'checkbox',
  627. // '#default_value' => -1,
  628. // '#title' => $bundle,
  629. // );
  630. // }
  631. // return $form;
  632. // }
  633. /**
  634. * Function for preprocessing the variables for the search_api_page_results
  635. * template.
  636. *
  637. * @param array $variables
  638. * An associative array containing:
  639. * - $index: The index this search was executed on.
  640. * - $results: An array of search results, as returned by
  641. * SearchApiQueryInterface::execute().
  642. * - $keys: The keywords of the executed search.
  643. *
  644. * @see materio-search-api-results.tpl.php
  645. */
  646. function template_preprocess_materio_search_api_results(array &$vars) {
  647. // dsm($vars, '$vars');
  648. $results = $vars['results'];
  649. $keys = $vars['keys'];
  650. // $vars['items'] = $vars['index']->loadItems(array_keys($vars['results']['results']));
  651. $vars['result_count'] = $results['result count'];
  652. $vars['sec'] = round($results['performance']['complete'], 3);
  653. $vars['keywords'] = implode(', ', $vars['keys']);
  654. if(isset($results['breves count'])){
  655. $vars['perfascard'] = true;
  656. $vars['search_performance'] = format_plural(
  657. $results['breves count'],
  658. 'The search found 1 news ',
  659. 'The search found @count news '
  660. );
  661. $vars['search_performance'] .= format_plural(
  662. $vars['result_count'] - $results['breves count'],
  663. 'with 1 associated matter.',
  664. 'with @count associated matters.'
  665. );
  666. $vars['search_performance'] .= format_plural(
  667. $results['could results']['result count'],
  668. ' You could have found 1 result with a ',
  669. ' You could have found <strong>@count results</strong> with a '
  670. );
  671. $vars['search_performance'] .= l(t('full access to materiO\'.'), 'node/11187');
  672. $vars['search_performance'] .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  673. }else{
  674. $vars['perfascard'] = false;
  675. // $vars['search_performance'] = format_plural(
  676. // // $results['result count'],
  677. // $vars['result_count'],
  678. // 'The search found 1 result ',
  679. // 'The search found @count results '
  680. // )
  681. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  682. "@count"=>$vars['result_count'],
  683. "@keys"=>$vars['keywords'],
  684. "@mat"=>4,
  685. "@new"=>7)
  686. );
  687. }
  688. //dsm($vars, '$vars');
  689. }
  690. function template_preprocess_materio_search_api_actuality(&$vars){
  691. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  692. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  693. $vars['actualities_infos'] = t('');
  694. }
  695. /**
  696. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  697. */
  698. /**
  699. * Implements hook_block_view_alter().
  700. */
  701. function materio_search_api_block_view_alter(&$data, $block) {
  702. // this alter prepopulate the search form provide by search_api_page
  703. if ($block->module == 'search_api_page') {
  704. $page = search_api_page_load($block->delta);
  705. $item = menu_get_item();
  706. if (isset($page->path) && $page->path == $item['path']) {
  707. $keys = arg(count(arg(NULL, $page->path)));
  708. if ($keys) {
  709. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  710. $data['content']['keys_' . $page->id]['#value'] = $keys;
  711. }
  712. }
  713. }
  714. }