materio_search_api.module 36 KB

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