materio_search_api.module 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. // 'term_tid' => array(
  118. // 'name' => t('Taxonomy term tid'),
  119. // 'fallback' => 'integer',
  120. // 'prefix' => 'im',
  121. // 'always multiValued' => TRUE,
  122. // ),
  123. );
  124. }
  125. /**
  126. * Implements hook_search_api_solr_dynamic_field_info().
  127. *
  128. * Tell Search API Solr how to index our new data type.
  129. */
  130. function materio_search_api_search_api_solr_dynamic_field_info() {
  131. return array(
  132. 'edge_n2_kw_text' => array(
  133. 'prefix' => 'tem',
  134. 'always multiValued' => TRUE,
  135. ),
  136. 'edge_n2_kw_mapped_text' => array(
  137. 'prefix' => 'temmap',
  138. 'always multiValued' => TRUE,
  139. ),
  140. // 'term_tid' => array(
  141. // 'prefix' => 'im',
  142. // 'always multiValued' => TRUE,
  143. // ),
  144. );
  145. }
  146. /**
  147. * hook_entity_property_info_alter().
  148. *
  149. * define own fields
  150. * - main taxonomy terms, the purpose is to be able to attribute differente boost on serach api depending on term order
  151. * - companies infos (address) : retrieve for materiau content type the companie info from the tode node
  152. *
  153. */
  154. function materio_search_api_entity_property_info_alter(&$info){
  155. // dsm($info, 'hook_entity_property_info_alter | info');
  156. // watchdog('materio solr', 'materio_search_api_entity_property_info_alter', array());
  157. $node_props = &$info['node']['properties'];
  158. for ($i=1; $i <= 5 ; $i++) {
  159. $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array(
  160. 'type'=>'text',
  161. 'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  162. 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
  163. );
  164. $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array(
  165. 'type'=>'text',
  166. 'label'=> t('Main onthologie term '.$i. ' as tid'),
  167. 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid',
  168. );
  169. }
  170. $node_props['materio_search_api_onthologie_term_others_text'] = array(
  171. 'type'=>'list<text>',
  172. 'label'=> t('Others onthologie terms as text (+ synonyms)'),
  173. 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
  174. );
  175. $node_props['materio_search_api_onthologie_term_others_tid'] = array(
  176. 'type'=>'list<text>',
  177. 'label'=> t('Others onthologie terms as tid'),
  178. 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid',
  179. );
  180. $node_props['materio_search_api_taglibres_text'] = array(
  181. 'type'=>'list<text>',
  182. 'label'=> t('Tag libres terms as text (+ synonyms)'),
  183. 'getter callback'=>'materio_search_api_get_taglibres_terms_text',
  184. );
  185. $node_materiau_props = &$info['node']['bundles']['materiau']['properties'];
  186. $node_materiau_props['materio_search_api_node_propertie_companie'] = array(
  187. 'type'=>'list<text>',
  188. 'label'=> t('Manufacturers and distributors companies localised (dont addresse)'),
  189. 'getter callback'=>'materio_search_api_get_node_propertie_companie',
  190. );
  191. $term_props = &$info['taxonomy_term']['properties'];
  192. $term_props['materio_search_api_term_property_dup_name'] = array(
  193. 'label' => t("Term name field dup"),
  194. 'description' => t("Duplicate the term name field to apply different filter in solr (remove accents for instance)"),
  195. 'type' => 'text',
  196. 'getter callback' => 'materio_search_api_term_property_dup_name',
  197. );
  198. }
  199. /**
  200. * taxonomy propoerties as text + synonymes
  201. */
  202. function materio_search_api_get_onthologie_term_1_text($item){
  203. // dsm($item, 'item');
  204. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 0);
  205. }
  206. function materio_search_api_get_onthologie_term_2_text($item){
  207. // dsm($item, 'item');
  208. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 1);
  209. }
  210. function materio_search_api_get_onthologie_term_3_text($item){
  211. // dsm($item, 'item');
  212. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 2);
  213. }
  214. function materio_search_api_get_onthologie_term_4_text($item){
  215. // dsm($item, 'item');
  216. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 3);
  217. }
  218. function materio_search_api_get_onthologie_term_5_text($item){
  219. // dsm($item, 'item');
  220. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 4);
  221. }
  222. function materio_search_api_get_taxonomy_terms_others_text($item){
  223. // dsm($item, 'item');
  224. $delta = 5;
  225. $terms = array();
  226. while( isset($item->field_onthologie['und'][$delta]) ){
  227. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_onthologie", $delta);
  228. $delta++;
  229. }
  230. return $terms;
  231. }
  232. function materio_search_api_get_taglibres_terms_text($item){
  233. // dsm($item, 'item');
  234. $delta = 0;
  235. $terms = array();
  236. while( isset($item->field_tags_libres['und'][$delta]) ){
  237. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_tags_libres", $delta);
  238. $delta++;
  239. }
  240. return $terms;
  241. }
  242. function materio_search_api_get_taxo_term_field_text($item, $field_name, $delta){
  243. // dsm($item, 'item');
  244. // dsm($delta, 'delta');
  245. if(isset($item->{$field_name}['und'][$delta])){
  246. // print '** item **'."\n";
  247. // print_r($item);
  248. $term = taxonomy_term_load($item->{$field_name}['und'][$delta]['tid']);
  249. // use entity metadata wrappers as they are SMART
  250. $wrapper = entity_metadata_wrapper('taxonomy_term', $term);
  251. // print '** wrapper **'."\n";
  252. // print_r($wrapper);
  253. // print "\n";
  254. $keywords[] = $wrapper->language($item->language)->name_field->raw();
  255. // print '** term_name **'."\n";
  256. // print_r($term->name);
  257. // print "\n";
  258. // print_r($term_name);
  259. // print "\n";
  260. $synonymes = array();
  261. foreach ($wrapper->language($item->language)->synonyms_synonym->value() as $index => $synonym) {
  262. // print '** synonym **'."\n";
  263. // print_r($synonym);
  264. // print "\n";
  265. $keywords[] = $synonym;
  266. }
  267. return implode(" ", $keywords);
  268. }
  269. return null;
  270. }
  271. /**
  272. * taxonomy properties as tid
  273. */
  274. function materio_search_api_get_onthologie_term_1_tid($item){
  275. // dsm($item, 'item');
  276. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 0);
  277. }
  278. function materio_search_api_get_onthologie_term_2_tid($item){
  279. // dsm($item, 'item');
  280. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 1);
  281. }
  282. function materio_search_api_get_onthologie_term_3_tid($item){
  283. // dsm($item, 'item');
  284. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 2);
  285. }
  286. function materio_search_api_get_onthologie_term_4_tid($item){
  287. // dsm($item, 'item');
  288. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 3);
  289. }
  290. function materio_search_api_get_onthologie_term_5_tid($item){
  291. // dsm($item, 'item');
  292. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 4);
  293. }
  294. function materio_search_api_get_taxonomy_terms_others_tid($item){
  295. // dsm($item, 'item');
  296. $delta = 5;
  297. $terms = array();
  298. while( isset($item->field_onthologie['und'][$delta]) ){
  299. $terms[] = materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", $delta);
  300. $delta++;
  301. }
  302. return $terms;
  303. }
  304. function materio_search_api_get_taxo_term_field_tid($item, $field_name, $delta){
  305. // dsm($item, 'item');
  306. // dsm($delta, 'delta');
  307. $tid = null;
  308. if(isset($item->{$field_name}['und'][$delta]))
  309. $tid = $item->{$field_name}['und'][$delta]['tid'];
  310. // print 'tid = '.$tid."\n";
  311. return $tid;
  312. }
  313. /**
  314. *
  315. */
  316. function materio_search_api_get_node_propertie_companie($item){
  317. // dsm($item, '$item');
  318. $lang = "fr";//$item->language;
  319. $languages = language_list();
  320. global $language;
  321. $language = $languages[$lang];
  322. $cies = array();
  323. $cie_fields = array("field_company_fab", "field_company_distrib");
  324. foreach ($cie_fields as $cie_field_name) {
  325. $delta = 0;
  326. while( isset($item->{$cie_field_name}['und'][$delta]) ){
  327. $tid = $item->{$cie_field_name}['und'][$delta]['tid'];
  328. $cie_term = taxonomy_term_load($tid);
  329. if($cie_node = company_get_tode_node($cie_term)){
  330. $cie_node->language = $lang;
  331. $cie_view = node_view($cie_node, "full", $lang);
  332. $cie_rendered = rip_tags(drupal_render($cie_view));
  333. // dsm($cie_rendered, 'cie_rendered');
  334. $cies[] = $cie_rendered;
  335. }
  336. $delta++;
  337. }
  338. }
  339. return $cies;
  340. }
  341. function company_get_tode_node($term){
  342. if(module_exists('tode'))
  343. if( $entitys = tode_get_nids_from_term($term))
  344. if(isset($entitys['node']))
  345. foreach ($entitys['node'] as $nid => $n)
  346. return node_load($nid);
  347. return false;
  348. }
  349. function rip_tags($string) {
  350. // ----- remove HTML TAGs -----
  351. $string = preg_replace ('/<[^>]*>/', ' ', $string);
  352. // ----- remove control characters -----
  353. $string = str_replace("\r", '', $string); // --- replace with empty space
  354. $string = str_replace("\n", ' ', $string); // --- replace with space
  355. $string = str_replace("\t", ' ', $string); // --- replace with space
  356. // $string = str_replace("&nbsp;", ' ', $string); // --- replace with space
  357. // $string = str_replace("&#039;", '\'', $string); // --- replace with space
  358. // ----- remove multiple spaces -----
  359. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  360. // ----- remove html entities
  361. preg_match_all('/&[^;]+;/', $string, $entities);
  362. foreach ($entities[0] as $entity) {
  363. $string = str_replace($entity, mb_convert_encoding($entity, 'UTF-8', 'HTML-ENTITIES'), $string);
  364. }
  365. return $string;
  366. }
  367. function materio_search_api_term_property_dup_name($term){
  368. $lang = $term->language;
  369. return $term->name_field[$lang][0]['value'];
  370. }
  371. /**
  372. * - - -- - - - - - - blocks - - - - - - - - - - -
  373. *
  374. */
  375. /**
  376. * Implements hook_block_info().
  377. */
  378. function materio_search_api_block_info() {
  379. $blocks['materio_search_api_search'] = array(
  380. 'info' => t('Materio search api search'),
  381. 'cache' => DRUPAL_NO_CACHE
  382. );
  383. $blocks['materio_sa_adv_search'] = array(
  384. 'info' => t('Materio search api advanced search'),
  385. 'cache' => DRUPAL_NO_CACHE
  386. );
  387. # NOT USED
  388. $blocks['materio_search_api_viewmode'] = array(
  389. 'info' => t('Materio search api view mode selection'),
  390. 'cache' => DRUPAL_NO_CACHE
  391. );
  392. // $blocks['materio_search_api_filters'] = array(
  393. // 'info' => t('Materio search api filters'),
  394. // 'cache' => DRUPAL_NO_CACHE
  395. // );
  396. return $blocks;
  397. }
  398. /**
  399. * Implements hook_block_view().
  400. */
  401. function materio_search_api_block_view($delta = '') {
  402. // This example comes from node.module. Note that you can also return a
  403. // renderable array rather than rendered HTML for 'content'.
  404. $block = array();
  405. switch ($delta) {
  406. case 'materio_search_api_search':
  407. if (user_access('use materio search api') || user_access('use materio search api for breves')) {
  408. $block['subject'] = t('Search');
  409. $block['content'] = theme('materio_search_api_search_block', array());
  410. }
  411. break;
  412. case 'materio_search_api_viewmode':
  413. // restrict display on node page for materiau and breve
  414. $block_is_visible = true;
  415. $q = parse_url($_GET['q']);
  416. $parts = explode('/', $q['path']);
  417. if($parts[0] == "node"){
  418. $node = node_load($parts[1]);
  419. $block_is_visible = in_array($node->type, array('materiau','breve')) ? true : false;
  420. }
  421. if (user_access('use materio search api viewmode selection') && $block_is_visible) {
  422. $block['subject'] = t('View mode');
  423. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  424. }
  425. break;
  426. // case 'materio_search_api_filters':
  427. // if (user_access('use materio search api filters')) {
  428. // $block['subject'] = t('Filters');
  429. // $block['content'] = theme('materio_search_api_filters_block', array());
  430. // }
  431. // break;
  432. # NOT USED
  433. case 'materio_sa_adv_search':
  434. if(user_access('use materio search api advanced search')){
  435. $block['subject'] = t('Advanced Search');
  436. $block['content'] = theme('materio_search_api_advanced_search_block', array());
  437. }
  438. break;
  439. }
  440. return $block;
  441. }
  442. /**
  443. * Implements hook_entity_info_alter().
  444. */
  445. function materio_search_api_entity_info_alter(&$entity_info) {
  446. $entity_info['node']['view modes']['cardsmall'] = array(
  447. 'label' => t('Small cards'),
  448. 'custom settings' => TRUE,
  449. );
  450. $entity_info['node']['view modes']['cardmedium'] = array(
  451. 'label' => t('Medium cards'),
  452. 'custom settings' => TRUE,
  453. );
  454. $entity_info['node']['view modes']['cardbig'] = array(
  455. 'label' => t('Big cards'),
  456. 'custom settings' => TRUE,
  457. );
  458. $entity_info['node']['view modes']['cardfull'] = array(
  459. 'label' => t('Full cards'),
  460. 'custom settings' => TRUE,
  461. );
  462. }
  463. /**
  464. * Implements hook_node_view_alter().
  465. */
  466. function materio_search_api_node_view_alter(&$build) {
  467. $node = $build['#node'];
  468. if (arg(0) == 'node' && arg(1) == $node->nid) {
  469. // dsm($build, 'build');
  470. global $user;
  471. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  472. // dsm($viewmode, 'viewmode');
  473. $node = $build['#node'];
  474. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  475. $build = node_view($node, $viewmode);
  476. }
  477. }
  478. }
  479. /**
  480. * Implements hook_node_view().
  481. */
  482. function materio_search_api_node_view($node, $view_mode, $langcode) {
  483. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  484. # PRINT 7.1
  485. print_node_view($node, 'full');
  486. print_pdf_node_view($node, 'full');
  487. # PRINT 7.2-beta
  488. //print_ui_node_view($node, 'full');
  489. // dsm($node, 'node');
  490. }
  491. }
  492. /**
  493. * materiobase_search_form()
  494. */
  495. function materio_search_api_search_form($form, &$form_state){
  496. // dsm($form_state, 'form_state');
  497. // dsm($form, 'form');
  498. global $user;
  499. $form = array();
  500. $args = arg();
  501. $path = array_shift($args);
  502. // dsm($args, 'args');
  503. if($args[0] == "advanced"){
  504. $advanced = true;
  505. array_shift($args);
  506. // dsm($args, 'shifted arsg');
  507. // foreach ($args as $arg) {
  508. // $typed[] = $arg;//(integer)$arg;
  509. // }
  510. $keys = implode(' +', $args);
  511. }else{
  512. $keys = implode('/', $args);
  513. }
  514. if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters
  515. $query = new EntityFieldQuery();
  516. $query->entityCondition('entity_type', 'node')
  517. ->entityCondition('bundle', 'materiau')
  518. ->propertyCondition('status', 1);
  519. $count = $query->count()->execute();
  520. $default_value = t("search among our !fiches cards", array("!fiches"=>$count));
  521. }else{
  522. $default_value = '';
  523. }
  524. $form['searchfield'] = array(
  525. '#type' => 'textfield',
  526. '#default_value' => $path == 'explore' ? $keys : $default_value, // TODO: set the search page path global or a variable in settings
  527. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  528. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  529. '#size' => 30,
  530. '#maxlength' => 1024,
  531. '#attributes' => array("default"=>$default_value),
  532. );
  533. if(user_access('use materio search api filters')){
  534. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  535. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  536. foreach ($indexed_bundles as $bundle) {
  537. $bundles_options[$bundle] = $bundle;
  538. $default_bundles[] = $bundle;
  539. }
  540. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  541. $form['bundles_filter'] = array(
  542. '#type'=>'checkboxes',
  543. '#options' => $bundles_options,
  544. '#default_value' => $user_bundles_filter,
  545. // '#attributes' => array('class'=>array('btn-group')),
  546. );
  547. }
  548. $form['create'] = array(
  549. '#type' => 'image_button',
  550. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  551. '#value' => t('Search'),
  552. );
  553. return $form;
  554. }
  555. function materio_search_api_search_form_validate($form, &$form_state){
  556. // dsm($form, '$form');
  557. // dsm($form_state, '$form_state');
  558. // dsm(strlen($form_state['values']['searchfield']));
  559. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  560. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  561. }
  562. }
  563. function materio_search_api_search_form_submit($form, &$form_state){
  564. // dsm($form_state, 'form_state');
  565. global $user;
  566. if(user_access('use materio search api filters')){
  567. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  568. if($value)
  569. $bundles[] = $bundle;
  570. # if no filter checked we checked them all by default
  571. if(!isset($bundles))
  572. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  573. $bundles[] = $bundle;
  574. }else{
  575. # if user have no access to filters
  576. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  577. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  578. foreach ($indexed_bundles as $bundle) {
  579. $bundles[] = $bundle;
  580. }
  581. }
  582. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  583. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  584. }
  585. /**
  586. * materiobase_advancedsearch_form()
  587. */
  588. function materio_search_api_advanced_search_form($form, &$form_state){
  589. // dsm($form_state, 'form_state');
  590. // dsm($form, 'form');
  591. // global $user;
  592. $as_tree = materio_serach_api_get_advancedsearch_tree();
  593. dsm($as_tree, 'as_tree');
  594. $keys = arg();
  595. $path = array_shift($keys);
  596. // dsm($keys, 'keys');
  597. if($path == 'explore' && $keys[0] == "advanced"){
  598. $advanced = true;
  599. array_shift($keys);
  600. $default_keys = $keys;
  601. }
  602. $form = array(
  603. 'title' => array(
  604. '#prefix' => "<h3>",
  605. '#markup' => t("Advanced Search"),
  606. '#suffix' => "</h3>",
  607. )
  608. );
  609. $form['slider'] = array(
  610. '#type' => 'fieldset',
  611. // '#title' => $voc->name,
  612. '#tree' => true,
  613. );
  614. foreach ($as_tree as $vid => $tree) {
  615. // $voc = taxonomy_vocabulary_load($vid);
  616. // dsm($voc, 'voc');
  617. $form['slider']['voc-'.$vid] = array(
  618. '#type' => 'fieldset',
  619. // '#title' => $voc->name,
  620. '#tree' => true,
  621. );
  622. foreach ($tree as $term) {
  623. // dsm($term, 'term');
  624. $form['slider']['voc-'.$vid]['tree-'.$term['tid']] = array(
  625. '#type' => 'fieldset',
  626. // '#title' => $term->name,
  627. '#attributes' => array("class"=>array("treelevel1")),
  628. '#tree' => true,
  629. );
  630. $placeholder = t("Select an option");
  631. $form['slider']['voc-'.$vid]['tree-'.$term['tid']]['children'] = array(
  632. '#type' => 'select',
  633. '#title' => $term['name'],
  634. '#attributes' => array(
  635. "class"=>array("treelevel1"),
  636. "title"=>$placeholder,
  637. "data-placeholder" => $placeholder,
  638. ), //, "form-control"
  639. '#theme_wrappers' => array('materio_search_api_form_element'),
  640. '#empty_option' => $placeholder,
  641. '#empty_value' => 1,
  642. );
  643. $options = array();
  644. foreach ($term['children'] as $tid => $child) {
  645. $options[$tid] = $child['name'];
  646. $classes2 = array("treelevel2", $child['tid']);
  647. $options2 = array();
  648. $dflt_values2 = array();
  649. foreach ($child['children'] as $tid2 => $child2) {
  650. $options2[$tid2] = $child2['name'];
  651. // default Values
  652. if(in_array($tid2, $default_keys))
  653. $dflt_values2[] = $tid2;
  654. }
  655. if(count($options2)){
  656. if(count($dflt_values2))
  657. $classes2[] = 'visible';
  658. $form['slider']['voc-'.$vid]['tree-'.$term['tid']]['tid-'.$child['tid']] = array(
  659. '#type' => 'select',
  660. '#title' => $child['name'],
  661. '#multiple' => true,
  662. '#attributes' => array(
  663. "class" => $classes2, // , "form-control"
  664. "title" => "-Please select-",
  665. "tid" => $child['tid'],
  666. ),
  667. '#theme_wrappers' => array('materio_search_api_form_element'),
  668. // '#empty_option' => '-Please select-',
  669. // '#empty_value' => 1,
  670. '#options' => $options2,
  671. '#default_value' => $dflt_values2,
  672. );
  673. }
  674. }
  675. if(count($options)){
  676. $form['slider']['voc-'.$vid]['tree-'.$term['tid']]['children']['#options'] = $options;
  677. }else{
  678. unset($form['slider']['voc-'.$vid]['tree-'.$term['tid']]);
  679. }
  680. }
  681. }
  682. $form['slider']['show'] = array(
  683. '#type' => 'image_button',
  684. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  685. '#value' => t('Find'),
  686. // '#attributes' => array("class"=>array("btn","btn-lg")),
  687. '#prefix' => '<div class="submits">',
  688. '#suffix' => '</div>',
  689. );
  690. return $form;
  691. }
  692. function materio_serach_api_get_advancedsearch_tree(){
  693. $as_tree = array();
  694. $vocabularies = variable_get('advancedsearchvocs', array());
  695. foreach ($vocabularies as $vid) {
  696. // dsm($vid, "vid");
  697. // $voc = taxonomy_vocabulary_load($vid);
  698. // dsm($voc, 'voc');
  699. $tree = taxonomy_get_tree($vid, 0, 1, TRUE);
  700. // dsm($tree, 'tree');
  701. if(!count($tree)) continue;
  702. foreach ($tree as $term) {
  703. // dsm($term, "term : ".$term->name);
  704. if(!isset($term->field_used_on_advanced_search['und'][0]['value'])
  705. || $term->field_used_on_advanced_search['und'][0]['value'] == 0)
  706. continue;
  707. $children = taxonomy_get_children($term->tid, $vid);
  708. // dsm($children, "children");
  709. if(!count($children)) continue;
  710. foreach ($children as $tid => $child) {
  711. $child = taxonomy_term_load($child->tid);
  712. // dsm($child, 'child : '.$child->name);
  713. if($child->field_used_on_advanced_search['und'][0]['value'] == 0)
  714. continue;
  715. // $children2 = taxonomy_get_children($child->tid, $vid);
  716. $children2 = taxonomy_get_children_all($child->tid, $vid);
  717. // dsm($children2, "children2");
  718. if(!count($children2)) continue;
  719. foreach ($children2 as $tid2 => $child2) {
  720. $child2 = taxonomy_term_load($tid2);
  721. // dsm($child2, 'child2 : '.$child2->name);
  722. if(!isset($child2->field_used_on_advanced_search['und'][0]['value'])
  723. || $child2->field_used_on_advanced_search['und'][0]['value'] == 0)
  724. continue;
  725. $as_tree[$vid][$term->tid]['children'][$child->tid]['children'][] = array('tid'=>$child2->tid,'name'=>$child2->name);
  726. }
  727. if(isset($as_tree[$vid][$term->tid]['children'][$child->tid]['children']))
  728. $as_tree[$vid][$term->tid]['children'][$child->tid] += array("tid"=>$child->tid,"name"=>$child->name);
  729. }
  730. if(isset($as_tree[$vid][$term->tid]['children']))
  731. $as_tree[$vid][$term->tid] += array("tid"=>$term->tid,"name"=>$term->name);
  732. }
  733. }
  734. return $as_tree;
  735. }
  736. function taxonomy_get_children_all($tid, $vid = 0, $key = 'tid'){
  737. $c = taxonomy_get_children($tid, $vid, $key);
  738. $result = array();
  739. foreach ($c as $t => $d){
  740. $result[$t] = $d;
  741. $below = taxonomy_get_children_all($t, $vid, $key);
  742. if (!empty($below)) {
  743. foreach ($below as $nt => $nd){
  744. $result[$nt] = $nd;
  745. }
  746. }
  747. }
  748. return $result;
  749. }
  750. function materio_search_api_advanced_search_form_validate($form, &$form_state){
  751. // dsm($form, '$form');
  752. // dsm($form_state, '$form_state');
  753. // dsm(strlen($form_state['values']['searchfield']));
  754. // if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  755. // form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  756. // }
  757. }
  758. function materio_search_api_advanced_search_form_submit($form, &$form_state){
  759. // dsm($form, 'form');
  760. // dsm($form_state, 'form_state');
  761. $fs_values = $form_state['values']['slider'];
  762. unset($fs_values['show']);
  763. // dsm($fs_values, 'values');
  764. foreach($fs_values as $voc => $trees){
  765. foreach($trees as $tree => $values){
  766. foreach ($values as $values => $selected) {
  767. if(is_array($selected) && count($selected)){
  768. foreach ($selected as $tid) {
  769. $tids[] = $tid;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. $form_state['redirect'] = 'explore/advanced/'.implode('/', $tids);
  776. }
  777. /**
  778. * viewmode
  779. */
  780. function _materio_search_api_change_viewmode($vm){
  781. // dsm($vm, '_materio_search_api_change_viewmode');
  782. global $user;
  783. // dsm($user, 'user');
  784. $entity_infos = entity_get_info();
  785. // dsm($entity_infos, 'entity_infos');
  786. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  787. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  788. $rep = array('statut'=>'saved');
  789. }else{
  790. $rep = array('statut'=>'viewmode not allowed');
  791. }
  792. return $rep;
  793. }
  794. /**
  795. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  796. */
  797. /**
  798. * Implements hook_theme().
  799. */
  800. function materio_search_api_theme($existing, $type, $theme, $path) {
  801. return array(
  802. 'materio_search_api_search_block' => array(
  803. 'arguments' => array(),
  804. 'template' => 'materio-search-api-search-block',
  805. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  806. ),
  807. 'materio_search_api_select_viewmode_block' => array(
  808. 'arguments' => array(),
  809. 'template' => 'materio-search-api-select-viewmode-block',
  810. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  811. ),
  812. // 'materio_search_api_filters_block' => array(
  813. // 'arguments' => array(),
  814. // 'template' => 'materio-search-api-filters-block',
  815. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  816. // ),
  817. 'materio_search_api_results' => array(
  818. 'arguments' => array(),
  819. 'template' => 'materio-search-api-results',
  820. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  821. 'variables' => array(
  822. 'index' => NULL,
  823. 'results' => array('result count' => 0),
  824. 'items' => array(),
  825. 'view_mode' => 'teaser',
  826. 'keys' => '',
  827. // 'page_machine_name' => NULL,
  828. 'spellcheck' => NULL,
  829. 'pager' => NULL,
  830. ),
  831. ),
  832. // 'materio_search_api_performance' => array(
  833. // 'render element' => 'element',
  834. // ),
  835. 'materio_search_api_actuality' => array(
  836. 'template' => 'materio-search-api-actuality',
  837. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  838. 'arguments' => array(
  839. 'items' => array(),
  840. 'view_mode' => "teaser",
  841. 'pager' => NULL,
  842. 'count' => 0,
  843. )
  844. ),
  845. 'materio_search_api_advanced_search_block' => array(
  846. 'template' => 'materio-search-api-advanced-search-block',
  847. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  848. 'arguments' => array(),
  849. ),
  850. 'materio_search_api_form_element' => array(
  851. 'render element' => 'element',
  852. ),
  853. );
  854. }
  855. /**
  856. * Implements theme for rendering search-performance
  857. */
  858. // function theme_materio_search_api_performance($variables) {
  859. // $element = array_shift($variables);
  860. // return $element['#markup'];
  861. // }
  862. /**
  863. * template_preprocess_materiobase_search_block();
  864. */
  865. function template_preprocess_materio_search_api_search_block(&$vars){
  866. // dsm($vars, '$vars');
  867. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  868. if(user_access('use materio search api advanced search')){
  869. $vars['advancedsearchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  870. }
  871. }
  872. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  873. global $user;
  874. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  875. $availableviewmodes = variable_get('availableviewmodes', -1);
  876. // dsm($availableviewmodes);
  877. $entity_infos = entity_get_info();
  878. // dsm($entity_infos, 'entity_infos');
  879. $content = '<div class="btn-group btn-group-vertical">';
  880. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  881. if(in_array($viewmode, $availableviewmodes)){
  882. $link = l(
  883. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  884. 'materiosearchapi/viewmode/change/'.$viewmode,
  885. array(
  886. 'query' => drupal_get_destination(),
  887. 'html' => true,
  888. 'attributes' => array(
  889. 'class' => array(
  890. 'viewmode-link',
  891. 'viewmode-'.$viewmode,
  892. $active == $viewmode ? " active" : ''
  893. ),
  894. 'rel' => $viewmode
  895. )
  896. )
  897. );
  898. $content .= $link;
  899. }
  900. }
  901. $content .= '</div>';
  902. $vars['content'] = $content;
  903. }
  904. // function template_preprocess_materio_search_api_filters_block(&$vars){
  905. // $index_machine_name = variable_get('mainsearchindex', -1);
  906. // $index = search_api_index_load($index_machine_name);
  907. // dsm($index, 'index');
  908. // // $entity_infos = entity_get_info($index->item_type);
  909. // // dsm($entity_infos, 'entity_infos');
  910. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  911. // dsm($indexed_bundles, 'indexed_bundles');
  912. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  913. // }
  914. // function materio_search_api_filters_form($form, $form_state, $bundles){
  915. // $form = array();
  916. // dsm($bundles, 'bundles');
  917. // foreach ($bundles as $bundle) {
  918. // $form[$bundle . '_filter'] = array(
  919. // '#type'=>'checkbox',
  920. // '#default_value' => -1,
  921. // '#title' => $bundle,
  922. // );
  923. // }
  924. // return $form;
  925. // }
  926. /**
  927. * Function for preprocessing the variables for the search_api_page_results
  928. * template.
  929. *
  930. * @param array $variables
  931. * An associative array containing:
  932. * - $index: The index this search was executed on.
  933. * - $results: An array of search results, as returned by
  934. * SearchApiQueryInterface::execute().
  935. * - $keys: The keywords of the executed search.
  936. *
  937. * @see materio-search-api-results.tpl.php
  938. */
  939. function template_preprocess_materio_search_api_results(array &$vars) {
  940. // dsm($vars, '$vars');
  941. $results = $vars['results'];
  942. if(isset($results['result count'])){
  943. // $vars['items'] = $vars['index']->loadItems(array_keys($vars['results']['results']));
  944. $vars['result_count'] = $results['result count'];
  945. $vars['sec'] = round($results['performance']['complete'], 3);
  946. $vars['keywords'] = $vars['keys'];
  947. if(isset($results['breves count'])){
  948. // for free users
  949. $vars['perfascard'] = true;
  950. $perfoutput = format_plural(
  951. $results['breves count'],
  952. 'The search found 1 news ',
  953. 'The search found @count news '
  954. );
  955. $perfoutput .= format_plural(
  956. $vars['result_count'] - $results['breves count'],
  957. 'with 1 associated matter.',
  958. 'with @count associated matters.'
  959. );
  960. $perfoutput .= '<br />';
  961. $perfoutput .= format_plural(
  962. $results['could results']['result count'],
  963. ' You could have found 1 result with a ',
  964. ' You could have found <strong>@count results</strong> with a '
  965. );
  966. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  967. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  968. $vars['search_performance'] = $perfoutput;
  969. }else{
  970. // for members
  971. $vars['perfascard'] = false;
  972. // $vars['search_performance'] = format_plural(
  973. // // $results['result count'],
  974. // $vars['result_count'],
  975. // 'The search found 1 result ',
  976. // 'The search found @count results '
  977. // )
  978. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  979. "@count"=>$vars['result_count'],
  980. "@keys"=>$vars['keywords'])
  981. );
  982. // TODO indicate how many materials and how many news
  983. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  984. // "@count"=>$vars['result_count'],
  985. // "@keys"=>$vars['keywords'],
  986. // "@mat"=>4,
  987. // "@new"=>7)
  988. // );
  989. }
  990. }else{
  991. //for anonymous
  992. $vars['perfascard'] = true;
  993. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  994. $perfoutput .= format_plural(
  995. $results['could results']['result count'],
  996. ' You could have found 1 result with a ',
  997. ' You could have found <strong>@count results</strong> with a '
  998. );
  999. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1000. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1001. $vars['search_performance'] = $perfoutput;
  1002. }
  1003. //dsm($vars, '$vars');
  1004. }
  1005. function template_preprocess_materio_search_api_actuality(&$vars){
  1006. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  1007. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  1008. $vars['actualities_infos'] = t('');
  1009. }
  1010. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  1011. // dsm($vars, "vars");
  1012. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1013. }
  1014. function theme_materio_search_api_form_element($variables) {
  1015. // dsm($variables, 'variables');
  1016. $element = &$variables['element'];
  1017. // This function is invoked as theme wrapper, but the rendered form element
  1018. // may not necessarily have been processed by form_builder().
  1019. $element += array(
  1020. '#title_display' => 'before',
  1021. );
  1022. // Add element #id for #type 'item'.
  1023. if (isset($element['#markup']) && !empty($element['#id'])) {
  1024. $attributes['id'] = $element['#id'];
  1025. }
  1026. // Add element's #type and #name as class to aid with JS/CSS selectors.
  1027. $attributes['class'] = array('form-item');
  1028. if (!empty($element['#type'])) {
  1029. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  1030. }
  1031. if (!empty($element['#name'])) {
  1032. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  1033. }
  1034. // Add a class for disabled elements to facilitate cross-browser styling.
  1035. if (!empty($element['#attributes']['disabled'])) {
  1036. $attributes['class'][] = 'form-disabled';
  1037. }
  1038. # adding element class to wrapper
  1039. foreach ($element['#attributes']['class'] as $value) {
  1040. $attributes['class'][] = $value;
  1041. }
  1042. foreach ($element['#attributes'] as $attr => $value) {
  1043. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  1044. $attributes[$attr] = $value;
  1045. }
  1046. }
  1047. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  1048. // If #title is not set, we don't display any label or required marker.
  1049. if (!isset($element['#title'])) {
  1050. $element['#title_display'] = 'none';
  1051. }
  1052. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  1053. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  1054. switch ($element['#title_display']) {
  1055. case 'before':
  1056. case 'invisible':
  1057. $output .= ' ' . theme('form_element_label', $variables);
  1058. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1059. break;
  1060. case 'after':
  1061. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  1062. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  1063. break;
  1064. case 'none':
  1065. case 'attribute':
  1066. // Output no label and no required marker, only the children.
  1067. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1068. break;
  1069. }
  1070. if (!empty($element['#description'])) {
  1071. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  1072. }
  1073. $output .= "</div>\n";
  1074. return $output;
  1075. }
  1076. /**
  1077. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  1078. */
  1079. /**
  1080. * Implements hook_block_view_alter().
  1081. */
  1082. function materio_search_api_block_view_alter(&$data, $block) {
  1083. // this alter prepopulate the search form provide by search_api_page
  1084. if ($block->module == 'search_api_page') {
  1085. $page = search_api_page_load($block->delta);
  1086. $item = menu_get_item();
  1087. if (isset($page->path) && $page->path == $item['path']) {
  1088. $keys = arg(count(arg(NULL, $page->path)));
  1089. if ($keys) {
  1090. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  1091. $data['content']['keys_' . $page->id]['#value'] = $keys;
  1092. }
  1093. }
  1094. }
  1095. }