materio_search_api.module 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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. $keys = arg();
  593. $path = array_shift($keys);
  594. // dsm($keys, 'keys');
  595. if($path == 'explore' && $keys[0] == "advanced"){
  596. $advanced = true;
  597. array_shift($keys);
  598. $default_keys = $keys;
  599. }
  600. $form = array(
  601. 'title' => array(
  602. '#prefix' => "<h3>",
  603. '#markup' => t("Advanced Search"),
  604. '#suffix' => "</h3>",
  605. )
  606. );
  607. $vocabularies = variable_get('advancedsearchvocs', array());
  608. $form['slider'] = array(
  609. '#type' => 'fieldset',
  610. // '#title' => $voc->name,
  611. '#tree' => true,
  612. );
  613. foreach ($vocabularies as $vid) {
  614. // dsm($vid, "vid");
  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. $tree = taxonomy_get_tree($vid, 0, 1, TRUE);
  623. // dsm($tree, 'tree');
  624. if(!count($tree))
  625. continue;
  626. foreach ($tree as $term) {
  627. // dsm($term, "term");
  628. $children = taxonomy_get_children($term->tid, $vid);
  629. // dsm($children, "children");
  630. if($term->field_used_on_advanced_search['und'][0]['value'] == 0
  631. || !count($children))
  632. continue;
  633. $form['slider']['voc-'.$vid]['tree-'.$term->tid] = array(
  634. '#type' => 'fieldset',
  635. // '#title' => $term->name,
  636. '#attributes' => array("class"=>array("treelevel1")),
  637. '#tree' => true,
  638. );
  639. $placeholder = t("Select an option");
  640. $form['slider']['voc-'.$vid]['tree-'.$term->tid]['children'] = array(
  641. '#type' => 'select',
  642. '#title' => $term->name,
  643. '#attributes' => array(
  644. "class"=>array("treelevel1"),
  645. "title"=>$placeholder,
  646. "data-placeholder" => $placeholder,
  647. ), //, "form-control"
  648. '#theme_wrappers' => array('materio_search_api_form_element'),
  649. '#empty_option' => $placeholder,
  650. '#empty_value' => 1,
  651. );
  652. $options = array();
  653. foreach ($children as $tid => $child) {
  654. // $child = taxonomy_term_load($child->tid);
  655. // if($child->field_used_on_advanced_search['und'][0]['value'] == 0){
  656. // continue;
  657. // }
  658. $children2 = taxonomy_get_children($child->tid, $vid);
  659. // dsm($children2, "children2");
  660. if(count($children2))
  661. $options[$tid] = $child->name;
  662. $classes2 = array("treelevel2", $child->tid);
  663. $options2 = array();
  664. $dflt_values2 = array();
  665. foreach ($children2 as $tid2 => $child2) {
  666. // $child2 = taxonomy_term_load($child2->tid);
  667. // if($child2->field_used_on_advanced_search['und'][0]['value'] == 0){
  668. // continue;
  669. // }
  670. $options2[$tid2] = $child2->name;
  671. // default Values
  672. if(in_array($tid2, $default_keys))
  673. $dflt_values2[] = $tid2;
  674. }
  675. if(count($options2)){
  676. if(count($dflt_values2))
  677. $classes2[] = 'visible';
  678. $form['slider']['voc-'.$vid]['tree-'.$term->tid]['tid-'.$child->tid] = array(
  679. '#type' => 'select',
  680. '#title' => $child->name,
  681. '#multiple' => true,
  682. '#attributes' => array(
  683. "class" => $classes2, // , "form-control"
  684. "title" => "-Please select-",
  685. "tid" => $child->tid,
  686. ),
  687. '#theme_wrappers' => array('materio_search_api_form_element'),
  688. // '#empty_option' => '-Please select-',
  689. // '#empty_value' => 1,
  690. '#options' => $options2,
  691. '#default_value' => $dflt_values2,
  692. );
  693. }
  694. }
  695. if(count($options)){
  696. $form['slider']['voc-'.$vid]['tree-'.$term->tid]['children']['#options'] = $options;
  697. }else{
  698. unset($form['slider']['voc-'.$vid]['tree-'.$term->tid]);
  699. }
  700. }
  701. }
  702. $form['slider']['show'] = array(
  703. '#type' => 'image_button',
  704. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  705. '#value' => t('Find'),
  706. // '#attributes' => array("class"=>array("btn","btn-lg")),
  707. '#prefix' => '<div class="submits">',
  708. '#suffix' => '</div>',
  709. );
  710. return $form;
  711. }
  712. function materio_search_api_advanced_search_form_validate($form, &$form_state){
  713. // dsm($form, '$form');
  714. // dsm($form_state, '$form_state');
  715. // dsm(strlen($form_state['values']['searchfield']));
  716. // if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  717. // form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  718. // }
  719. }
  720. function materio_search_api_advanced_search_form_submit($form, &$form_state){
  721. // dsm($form, 'form');
  722. // dsm($form_state, 'form_state');
  723. $fs_values = $form_state['values']['slider'];
  724. unset($fs_values['show']);
  725. // dsm($fs_values, 'values');
  726. foreach($fs_values as $voc => $trees){
  727. foreach($trees as $tree => $values){
  728. foreach ($values as $values => $selected) {
  729. if(is_array($selected) && count($selected)){
  730. foreach ($selected as $tid) {
  731. $tids[] = $tid;
  732. }
  733. }
  734. }
  735. }
  736. }
  737. $form_state['redirect'] = 'explore/advanced/'.implode('/', $tids);
  738. }
  739. /**
  740. * viewmode
  741. */
  742. function _materio_search_api_change_viewmode($vm){
  743. // dsm($vm, '_materio_search_api_change_viewmode');
  744. global $user;
  745. // dsm($user, 'user');
  746. $entity_infos = entity_get_info();
  747. // dsm($entity_infos, 'entity_infos');
  748. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  749. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  750. $rep = array('statut'=>'saved');
  751. }else{
  752. $rep = array('statut'=>'viewmode not allowed');
  753. }
  754. return $rep;
  755. }
  756. /**
  757. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  758. */
  759. /**
  760. * Implements hook_theme().
  761. */
  762. function materio_search_api_theme($existing, $type, $theme, $path) {
  763. return array(
  764. 'materio_search_api_search_block' => array(
  765. 'arguments' => array(),
  766. 'template' => 'materio-search-api-search-block',
  767. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  768. ),
  769. 'materio_search_api_select_viewmode_block' => array(
  770. 'arguments' => array(),
  771. 'template' => 'materio-search-api-select-viewmode-block',
  772. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  773. ),
  774. // 'materio_search_api_filters_block' => array(
  775. // 'arguments' => array(),
  776. // 'template' => 'materio-search-api-filters-block',
  777. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  778. // ),
  779. 'materio_search_api_results' => array(
  780. 'arguments' => array(),
  781. 'template' => 'materio-search-api-results',
  782. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  783. 'variables' => array(
  784. 'index' => NULL,
  785. 'results' => array('result count' => 0),
  786. 'items' => array(),
  787. 'view_mode' => 'teaser',
  788. 'keys' => '',
  789. // 'page_machine_name' => NULL,
  790. 'spellcheck' => NULL,
  791. 'pager' => NULL,
  792. ),
  793. ),
  794. // 'materio_search_api_performance' => array(
  795. // 'render element' => 'element',
  796. // ),
  797. 'materio_search_api_actuality' => array(
  798. 'template' => 'materio-search-api-actuality',
  799. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  800. 'arguments' => array(
  801. 'items' => array(),
  802. 'view_mode' => "teaser",
  803. 'pager' => NULL,
  804. 'count' => 0,
  805. )
  806. ),
  807. 'materio_search_api_advanced_search_block' => array(
  808. 'template' => 'materio-search-api-advanced-search-block',
  809. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  810. 'arguments' => array(),
  811. ),
  812. 'materio_search_api_form_element' => array(
  813. 'render element' => 'element',
  814. ),
  815. );
  816. }
  817. /**
  818. * Implements theme for rendering search-performance
  819. */
  820. // function theme_materio_search_api_performance($variables) {
  821. // $element = array_shift($variables);
  822. // return $element['#markup'];
  823. // }
  824. /**
  825. * template_preprocess_materiobase_search_block();
  826. */
  827. function template_preprocess_materio_search_api_search_block(&$vars){
  828. // dsm($vars, '$vars');
  829. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  830. if(user_access('use materio search api advanced search')){
  831. $vars['advancedsearchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  832. }
  833. }
  834. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  835. global $user;
  836. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  837. $availableviewmodes = variable_get('availableviewmodes', -1);
  838. // dsm($availableviewmodes);
  839. $entity_infos = entity_get_info();
  840. // dsm($entity_infos, 'entity_infos');
  841. $content = '<div class="btn-group btn-group-vertical">';
  842. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  843. if(in_array($viewmode, $availableviewmodes)){
  844. $link = l(
  845. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  846. 'materiosearchapi/viewmode/change/'.$viewmode,
  847. array(
  848. 'query' => drupal_get_destination(),
  849. 'html' => true,
  850. 'attributes' => array(
  851. 'class' => array(
  852. 'viewmode-link',
  853. 'viewmode-'.$viewmode,
  854. $active == $viewmode ? " active" : ''
  855. ),
  856. 'rel' => $viewmode
  857. )
  858. )
  859. );
  860. $content .= $link;
  861. }
  862. }
  863. $content .= '</div>';
  864. $vars['content'] = $content;
  865. }
  866. // function template_preprocess_materio_search_api_filters_block(&$vars){
  867. // $index_machine_name = variable_get('mainsearchindex', -1);
  868. // $index = search_api_index_load($index_machine_name);
  869. // dsm($index, 'index');
  870. // // $entity_infos = entity_get_info($index->item_type);
  871. // // dsm($entity_infos, 'entity_infos');
  872. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  873. // dsm($indexed_bundles, 'indexed_bundles');
  874. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  875. // }
  876. // function materio_search_api_filters_form($form, $form_state, $bundles){
  877. // $form = array();
  878. // dsm($bundles, 'bundles');
  879. // foreach ($bundles as $bundle) {
  880. // $form[$bundle . '_filter'] = array(
  881. // '#type'=>'checkbox',
  882. // '#default_value' => -1,
  883. // '#title' => $bundle,
  884. // );
  885. // }
  886. // return $form;
  887. // }
  888. /**
  889. * Function for preprocessing the variables for the search_api_page_results
  890. * template.
  891. *
  892. * @param array $variables
  893. * An associative array containing:
  894. * - $index: The index this search was executed on.
  895. * - $results: An array of search results, as returned by
  896. * SearchApiQueryInterface::execute().
  897. * - $keys: The keywords of the executed search.
  898. *
  899. * @see materio-search-api-results.tpl.php
  900. */
  901. function template_preprocess_materio_search_api_results(array &$vars) {
  902. // dsm($vars, '$vars');
  903. $results = $vars['results'];
  904. if(isset($results['result count'])){
  905. // $vars['items'] = $vars['index']->loadItems(array_keys($vars['results']['results']));
  906. $vars['result_count'] = $results['result count'];
  907. $vars['sec'] = round($results['performance']['complete'], 3);
  908. $vars['keywords'] = $vars['keys'];
  909. if(isset($results['breves count'])){
  910. // for free users
  911. $vars['perfascard'] = true;
  912. $perfoutput = format_plural(
  913. $results['breves count'],
  914. 'The search found 1 news ',
  915. 'The search found @count news '
  916. );
  917. $perfoutput .= format_plural(
  918. $vars['result_count'] - $results['breves count'],
  919. 'with 1 associated matter.',
  920. 'with @count associated matters.'
  921. );
  922. $perfoutput .= '<br />';
  923. $perfoutput .= format_plural(
  924. $results['could results']['result count'],
  925. ' You could have found 1 result with a ',
  926. ' You could have found <strong>@count results</strong> with a '
  927. );
  928. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  929. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  930. $vars['search_performance'] = $perfoutput;
  931. }else{
  932. // for members
  933. $vars['perfascard'] = false;
  934. // $vars['search_performance'] = format_plural(
  935. // // $results['result count'],
  936. // $vars['result_count'],
  937. // 'The search found 1 result ',
  938. // 'The search found @count results '
  939. // )
  940. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  941. "@count"=>$vars['result_count'],
  942. "@keys"=>$vars['keywords'])
  943. );
  944. // TODO indicate how many materials and how many news
  945. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  946. // "@count"=>$vars['result_count'],
  947. // "@keys"=>$vars['keywords'],
  948. // "@mat"=>4,
  949. // "@new"=>7)
  950. // );
  951. }
  952. }else{
  953. //for anonymous
  954. $vars['perfascard'] = true;
  955. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  956. $perfoutput .= format_plural(
  957. $results['could results']['result count'],
  958. ' You could have found 1 result with a ',
  959. ' You could have found <strong>@count results</strong> with a '
  960. );
  961. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  962. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  963. $vars['search_performance'] = $perfoutput;
  964. }
  965. //dsm($vars, '$vars');
  966. }
  967. function template_preprocess_materio_search_api_actuality(&$vars){
  968. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  969. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  970. $vars['actualities_infos'] = t('');
  971. }
  972. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  973. // dsm($vars, "vars");
  974. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  975. }
  976. function theme_materio_search_api_form_element($variables) {
  977. // dsm($variables, 'variables');
  978. $element = &$variables['element'];
  979. // This function is invoked as theme wrapper, but the rendered form element
  980. // may not necessarily have been processed by form_builder().
  981. $element += array(
  982. '#title_display' => 'before',
  983. );
  984. // Add element #id for #type 'item'.
  985. if (isset($element['#markup']) && !empty($element['#id'])) {
  986. $attributes['id'] = $element['#id'];
  987. }
  988. // Add element's #type and #name as class to aid with JS/CSS selectors.
  989. $attributes['class'] = array('form-item');
  990. if (!empty($element['#type'])) {
  991. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  992. }
  993. if (!empty($element['#name'])) {
  994. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  995. }
  996. // Add a class for disabled elements to facilitate cross-browser styling.
  997. if (!empty($element['#attributes']['disabled'])) {
  998. $attributes['class'][] = 'form-disabled';
  999. }
  1000. # adding element class to wrapper
  1001. foreach ($element['#attributes']['class'] as $value) {
  1002. $attributes['class'][] = $value;
  1003. }
  1004. foreach ($element['#attributes'] as $attr => $value) {
  1005. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  1006. $attributes[$attr] = $value;
  1007. }
  1008. }
  1009. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  1010. // If #title is not set, we don't display any label or required marker.
  1011. if (!isset($element['#title'])) {
  1012. $element['#title_display'] = 'none';
  1013. }
  1014. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  1015. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  1016. switch ($element['#title_display']) {
  1017. case 'before':
  1018. case 'invisible':
  1019. $output .= ' ' . theme('form_element_label', $variables);
  1020. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1021. break;
  1022. case 'after':
  1023. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  1024. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  1025. break;
  1026. case 'none':
  1027. case 'attribute':
  1028. // Output no label and no required marker, only the children.
  1029. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1030. break;
  1031. }
  1032. if (!empty($element['#description'])) {
  1033. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  1034. }
  1035. $output .= "</div>\n";
  1036. return $output;
  1037. }
  1038. /**
  1039. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  1040. */
  1041. /**
  1042. * Implements hook_block_view_alter().
  1043. */
  1044. function materio_search_api_block_view_alter(&$data, $block) {
  1045. // this alter prepopulate the search form provide by search_api_page
  1046. if ($block->module == 'search_api_page') {
  1047. $page = search_api_page_load($block->delta);
  1048. $item = menu_get_item();
  1049. if (isset($page->path) && $page->path == $item['path']) {
  1050. $keys = arg(count(arg(NULL, $page->path)));
  1051. if ($keys) {
  1052. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  1053. $data['content']['keys_' . $page->id]['#value'] = $keys;
  1054. }
  1055. }
  1056. }
  1057. }