materio_search_api.module 32 KB

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