materio_search_api.module 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. 'access to materio database' => array(
  17. 'title' => t('Access to materio database'),
  18. 'description' => t('Access to materio database.'),
  19. ),
  20. 'use materio search api' => array(
  21. 'title' => t('Use materio search api'),
  22. 'description' => t('Use materio search api.'),
  23. ),
  24. 'use materio search api for breves' => array(
  25. 'title' => t('Use materio search api for breves'),
  26. 'description' => t('Use materio search api for breves.'),
  27. ),
  28. 'use materio search api autocomplete' => array(
  29. 'title' => t('Use materio search api autocomplete'),
  30. 'description' => t('Use materio search api autocomplete.'),
  31. ),
  32. 'use materio search api viewmode selection' => array(
  33. 'title' => t('Use materio search api viewmode selection'),
  34. 'description' => t('Use materio search api viewmode selection.'),
  35. ),
  36. 'administer materio_search_api' => array(
  37. 'title' => t('administer Materio search api'),
  38. 'description' => t('Administer materio search api.'),
  39. ),
  40. 'use materio search api filters' => array(
  41. 'title' => t('use Materio search api filters'),
  42. 'description' => t('Use materio search api filters.'),
  43. ),
  44. 'use materio advanced search' => array(
  45. 'title' => t('Use materio advanced search'),
  46. 'description' => t('Use materio advanced search.'),
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_menu().
  52. */
  53. function materio_search_api_menu() {
  54. $items = array();
  55. $base = array(
  56. 'type' => MENU_CALLBACK,
  57. 'file' => 'materio_search_api.pages.inc',
  58. );
  59. $items['admin/config/search/search_api/materiosearchapi'] = array(
  60. 'title' => 'Materio Search Api',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('materio_search_api_settings'),
  63. 'access arguments' => array('administer materio_search_api'),
  64. 'file' => 'materio_search_api.admin.inc',
  65. 'type' => MENU_LOCAL_TASK,
  66. );
  67. $items['materiosearchapi/autocomplete/dbselect'] = $base+array(
  68. 'access arguments' => array('use materio search api autocomplete'),
  69. 'page callback' => 'materio_search_api_autocomplete_dbselect',
  70. );
  71. $items['materiosearchapi/autocomplete/searchapi'] = $base+array(
  72. 'access arguments' => array('use materio search api autocomplete'),
  73. 'page callback' => 'materio_search_api_autocomplete_searchapi',
  74. );
  75. $items['explore'] = $base+array(
  76. // 'access arguments' => array('use materio search api'),
  77. 'access callback' => 'materio_search_api_access_search',
  78. 'page callback' => 'materio_search_api_results_search',
  79. 'title' => t('Explore'),
  80. //'page argument' => array(1,2,3),
  81. );
  82. $items['actuality'] = $base+array(
  83. // 'access arguments' => array(),
  84. 'page callback' => 'materio_search_api_actuality',
  85. // 'page argument' => array(),
  86. 'access callback' => TRUE,
  87. );
  88. $items['materiosearchapi/viewmode/change'] = $base+array(
  89. 'access arguments' => array('use materio search api viewmode selection'),
  90. 'page callback' => 'materio_search_api_viewmode_change',
  91. 'page argument' => array(3),
  92. );
  93. return $items;
  94. }
  95. function materio_search_api_access_search(){
  96. return true;
  97. // return user_access('use materio search api for breves') || user_access('use materio search api');
  98. }
  99. /**
  100. * - - - - - - - - - - - - SOLR - - - - - - - - - - - -
  101. */
  102. /**
  103. * Implements hook_search_api_data_type_info().
  104. *
  105. * Declare our new type to Search API so it can be selected for a field.
  106. */
  107. function materio_search_api_search_api_data_type_info() {
  108. return array(
  109. 'edge_n2_kw_text' => array(
  110. 'name' => t('Fulltext (partial matching)'),
  111. 'fallback' => 'text',
  112. 'prefix' => 'tem',
  113. 'always multiValued' => TRUE,
  114. ),
  115. 'edge_n2_kw_mapped_text' => array(
  116. 'name' => t('Fulltext without accents (partial matching)'),
  117. 'fallback' => 'text',
  118. 'prefix' => 'temmap',
  119. 'always multiValued' => TRUE,
  120. ),
  121. // 'term_tid' => array(
  122. // 'name' => t('Taxonomy term tid'),
  123. // 'fallback' => 'integer',
  124. // 'prefix' => 'im',
  125. // 'always multiValued' => TRUE,
  126. // ),
  127. );
  128. }
  129. /**
  130. * Implements hook_search_api_solr_dynamic_field_info().
  131. *
  132. * Tell Search API Solr how to index our new data type.
  133. */
  134. function materio_search_api_search_api_solr_dynamic_field_info() {
  135. return array(
  136. 'edge_n2_kw_text' => array(
  137. 'prefix' => 'tem',
  138. 'always multiValued' => TRUE,
  139. ),
  140. 'edge_n2_kw_mapped_text' => array(
  141. 'prefix' => 'temmap',
  142. 'always multiValued' => TRUE,
  143. ),
  144. // 'term_tid' => array(
  145. // 'prefix' => 'im',
  146. // 'always multiValued' => TRUE,
  147. // ),
  148. );
  149. }
  150. /**
  151. * hook_entity_property_info_alter().
  152. *
  153. * define own fields
  154. * - main taxonomy terms, the purpose is to be able to attribute differente boost on serach api depending on term order
  155. * - companies infos (address) : retrieve for materiau content type the companie info from the tode node
  156. *
  157. */
  158. function materio_search_api_entity_property_info_alter(&$info){
  159. // dsm($info, 'hook_entity_property_info_alter | info');
  160. // watchdog('materio solr', 'materio_search_api_entity_property_info_alter', array());
  161. // NODE PROPERTIES
  162. $node_props = &$info['node']['properties'];
  163. $fields = msa_get_custom_onto_taxo_searchable_fields();
  164. foreach ($fields['termname'] as $field) {
  165. $node_props[$field['name']] = array(
  166. 'type'=>$field['type'],
  167. 'label'=> $field['label'],
  168. 'getter callback'=>$field['getter']
  169. );
  170. }
  171. foreach ($fields['tid'] as $field) {
  172. $node_props[$field['name']] = array(
  173. 'type'=>$field['type'],
  174. 'label'=> $field['label'],
  175. 'getter callback'=>$field['getter']
  176. );
  177. }
  178. // for ($i=1; $i <= 5 ; $i++) {
  179. // $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array(
  180. // 'type'=>'text',
  181. // 'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  182. // 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
  183. // );
  184. // $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array(
  185. // 'type'=>'text',
  186. // 'label'=> t('Main onthologie term '.$i. ' as tid'),
  187. // 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid',
  188. // );
  189. // }
  190. //
  191. // $node_props['materio_search_api_onthologie_term_others_text'] = array(
  192. // 'type'=>'list<text>',
  193. // 'label'=> t('Others onthologie terms as text (+ synonyms)'),
  194. // 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
  195. // );
  196. // $node_props['materio_search_api_onthologie_term_others_tid'] = array(
  197. // 'type'=>'list<text>',
  198. // 'label'=> t('Others onthologie terms as tid'),
  199. // 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid',
  200. // );
  201. $node_props['materio_search_api_taglibres_text'] = array(
  202. 'type'=>'list<text>',
  203. 'label'=> t('Tag libres terms as text (+ synonyms)'),
  204. 'getter callback'=>'materio_search_api_get_taglibres_terms_text',
  205. );
  206. $node_materiau_props = &$info['node']['bundles']['materiau']['properties'];
  207. $node_materiau_props['materio_search_api_node_propertie_companie'] = array(
  208. 'type'=>'list<text>',
  209. 'label'=> t('Manufacturers and distributors companies localised (dont addresse)'),
  210. 'getter callback'=>'materio_search_api_get_node_propertie_companie',
  211. );
  212. // TAXONOMIE TERMS PROPERTIES
  213. $term_props = &$info['taxonomy_term']['properties'];
  214. $term_props['materio_search_api_term_property_dup_name'] = array(
  215. 'label' => t("Term name field dup"),
  216. 'description' => t("Duplicate the term name field to apply different filter in solr (remove accents for instance)"),
  217. 'type' => 'text',
  218. 'getter callback' => 'materio_search_api_term_property_dup_name',
  219. );
  220. // $term_props['materio_search_api_term_property_name_translated'] = array(
  221. // 'label' => t("Term name translated"),
  222. // 'description' => t(""),
  223. // 'type' => 'text',
  224. // 'getter callback' => 'materio_search_api_term_property_name_translated',
  225. // );
  226. }
  227. function msa_get_custom_onto_taxo_searchable_fields(){
  228. for ($i=1; $i <= 5 ; $i++) {
  229. $fields_name['termname'][] = array(
  230. 'type' => 'text',
  231. 'name' => 'materio_search_api_onthologie_term_'.$i.'_text',
  232. 'label' => t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  233. 'getter' => 'materio_search_api_get_onthologie_term_'.$i.'_text'
  234. );
  235. $fields_name['tid'][] = array(
  236. 'type' => 'text',
  237. 'name' => 'materio_search_api_onthologie_term_'.$i.'_tid',
  238. 'label' => t('Main onthologie term '.$i. ' as tid'),
  239. 'getter' => 'materio_search_api_get_onthologie_term_'.$i.'_tid'
  240. );
  241. }
  242. $fields_name['termname'][] = array(
  243. 'type'=>'list<text>',
  244. 'name' => 'materio_search_api_onthologie_term_others_text',
  245. 'label' => t('Others onthologie terms as text (+ synonyms)'),
  246. 'getter' => 'materio_search_api_get_taxonomy_terms_others_text'
  247. );
  248. $fields_name['tid'][] = array(
  249. 'type'=>'list<text>',
  250. 'name' => 'materio_search_api_onthologie_term_others_tid',
  251. 'label' => t('Others onthologie terms as tid'),
  252. 'getter' => 'materio_search_api_get_taxonomy_terms_others_tid'
  253. );
  254. return $fields_name;
  255. }
  256. /**
  257. * taxonomy propoerties as text + synonymes
  258. */
  259. function materio_search_api_get_onthologie_term_1_text($item){
  260. // dsm($item, 'item');
  261. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 0);
  262. }
  263. function materio_search_api_get_onthologie_term_2_text($item){
  264. // dsm($item, 'item');
  265. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 1);
  266. }
  267. function materio_search_api_get_onthologie_term_3_text($item){
  268. // dsm($item, 'item');
  269. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 2);
  270. }
  271. function materio_search_api_get_onthologie_term_4_text($item){
  272. // dsm($item, 'item');
  273. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 3);
  274. }
  275. function materio_search_api_get_onthologie_term_5_text($item){
  276. // dsm($item, 'item');
  277. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 4);
  278. }
  279. function materio_search_api_get_taxonomy_terms_others_text($item){
  280. // dsm($item, 'item');
  281. $delta = 5;
  282. $terms = array();
  283. while( isset($item->field_onthologie['und'][$delta]) ){
  284. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_onthologie", $delta);
  285. $delta++;
  286. }
  287. return $terms;
  288. }
  289. function materio_search_api_get_taglibres_terms_text($item){
  290. // dsm($item, 'item');
  291. $delta = 0;
  292. $terms = array();
  293. while( isset($item->field_tags_libres['und'][$delta]) ){
  294. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_tags_libres", $delta);
  295. $delta++;
  296. }
  297. return $terms;
  298. }
  299. function materio_search_api_get_taxo_term_field_text($item, $field_name, $delta){
  300. // dsm($item, 'item');
  301. // dsm($delta, 'delta');
  302. if(isset($item->{$field_name}['und'][$delta])){
  303. // print '** item **'."\n";
  304. // print_r($item);
  305. $term = taxonomy_term_load($item->{$field_name}['und'][$delta]['tid']);
  306. // use entity metadata wrappers as they are SMART
  307. $wrapper = entity_metadata_wrapper('taxonomy_term', $term);
  308. // print '** wrapper **'."\n";
  309. // print_r($wrapper);
  310. // print "\n";
  311. $keywords[] = $wrapper->language($item->language)->name_field->raw();
  312. // print '** term_name **'."\n";
  313. // print_r($term->name);
  314. // print "\n";
  315. // print_r($term_name);
  316. // print "\n";
  317. $synonymes = array();
  318. foreach ($wrapper->language($item->language)->synonyms_synonym->value() as $index => $synonym) {
  319. // print '** synonym **'."\n";
  320. // print_r($synonym);
  321. // print "\n";
  322. $keywords[] = $synonym;
  323. }
  324. return implode(" ", $keywords);
  325. }
  326. return null;
  327. }
  328. /**
  329. * taxonomy properties as tid
  330. */
  331. function materio_search_api_get_onthologie_term_1_tid($item){
  332. // dsm($item, 'item');
  333. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 0);
  334. }
  335. function materio_search_api_get_onthologie_term_2_tid($item){
  336. // dsm($item, 'item');
  337. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 1);
  338. }
  339. function materio_search_api_get_onthologie_term_3_tid($item){
  340. // dsm($item, 'item');
  341. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 2);
  342. }
  343. function materio_search_api_get_onthologie_term_4_tid($item){
  344. // dsm($item, 'item');
  345. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 3);
  346. }
  347. function materio_search_api_get_onthologie_term_5_tid($item){
  348. // dsm($item, 'item');
  349. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 4);
  350. }
  351. function materio_search_api_get_taxonomy_terms_others_tid($item){
  352. // dsm($item, 'item');
  353. $delta = 5;
  354. $terms = array();
  355. while( isset($item->field_onthologie['und'][$delta]) ){
  356. $terms[] = materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", $delta);
  357. $delta++;
  358. }
  359. return $terms;
  360. }
  361. function materio_search_api_get_taxo_term_field_tid($item, $field_name, $delta){
  362. // dsm($item, 'item');
  363. // dsm($delta, 'delta');
  364. $tid = null;
  365. if(isset($item->{$field_name}['und'][$delta]))
  366. $tid = $item->{$field_name}['und'][$delta]['tid'];
  367. // print 'tid = '.$tid."\n";
  368. return $tid;
  369. }
  370. /**
  371. *
  372. */
  373. function materio_search_api_get_node_propertie_companie($item){
  374. // dsm($item, '$item');
  375. $lang = "fr";//$item->language;
  376. $languages = language_list();
  377. global $language;
  378. $language = $languages[$lang];
  379. $cies = array();
  380. $cie_fields = array("field_company_fab", "field_company_distrib");
  381. foreach ($cie_fields as $cie_field_name) {
  382. $delta = 0;
  383. while( isset($item->{$cie_field_name}['und'][$delta]) ){
  384. $tid = $item->{$cie_field_name}['und'][$delta]['tid'];
  385. $cie_term = taxonomy_term_load($tid);
  386. if($cie_node = company_get_tode_node($cie_term)){
  387. $cie_node->language = $lang;
  388. $cie_view = node_view($cie_node, "full", $lang);
  389. $cie_rendered = rip_tags(drupal_render($cie_view));
  390. // dsm($cie_rendered, 'cie_rendered');
  391. $cies[] = $cie_rendered;
  392. }
  393. $delta++;
  394. }
  395. }
  396. return $cies;
  397. }
  398. function company_get_tode_node($term){
  399. if(module_exists('tode'))
  400. if( $entitys = tode_get_nids_from_term($term))
  401. if(isset($entitys['node']))
  402. foreach ($entitys['node'] as $nid => $n)
  403. return node_load($nid);
  404. return false;
  405. }
  406. function rip_tags($string) {
  407. // ----- remove HTML TAGs -----
  408. $string = preg_replace ('/<[^>]*>/', ' ', $string);
  409. // ----- remove control characters -----
  410. $string = str_replace("\r", '', $string); // --- replace with empty space
  411. $string = str_replace("\n", ' ', $string); // --- replace with space
  412. $string = str_replace("\t", ' ', $string); // --- replace with space
  413. // $string = str_replace("&nbsp;", ' ', $string); // --- replace with space
  414. // $string = str_replace("&#039;", '\'', $string); // --- replace with space
  415. // ----- remove multiple spaces -----
  416. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  417. // ----- remove html entities
  418. preg_match_all('/&[^;]+;/', $string, $entities);
  419. foreach ($entities[0] as $entity) {
  420. $string = str_replace($entity, mb_convert_encoding($entity, 'UTF-8', 'HTML-ENTITIES'), $string);
  421. }
  422. return $string;
  423. }
  424. /**
  425. * Taxonomy terms properties
  426. */
  427. function materio_search_api_term_property_dup_name($term){
  428. $lang = $term->language;
  429. return $term->name_field[$lang][0]['value'];
  430. }
  431. // function materio_search_api_term_property_name_translated($term){
  432. // if($term->vid == 4){
  433. // print_r(get_defined_vars());
  434. //
  435. // // print_r($term);
  436. //
  437. // $trans = module_exists('i18n_taxonomy')
  438. // ? i18n_taxonomy_term_name($term, 'fr')
  439. // : $term->name;
  440. //
  441. // print_r($trans);
  442. // print "\n";
  443. // }
  444. // }
  445. // ____ __ __
  446. // / __ )/ /___ _____/ /_______
  447. // / __ / / __ \/ ___/ //_/ ___/
  448. // / /_/ / / /_/ / /__/ ,< (__ )
  449. // /_____/_/\____/\___/_/|_/____/
  450. /**
  451. * Implements hook_block_info().
  452. */
  453. function materio_search_api_block_info() {
  454. $blocks['materio_search_api_search'] = array(
  455. 'info' => t('Materio search api search'),
  456. 'cache' => DRUPAL_NO_CACHE
  457. );
  458. $blocks['mo_searchapi_advanced_search'] = array(
  459. 'info' => t('Materio search api advanced search'),
  460. 'cache' => DRUPAL_NO_CACHE
  461. );
  462. # NOT USED
  463. $blocks['materio_search_api_viewmode'] = array(
  464. 'info' => t('Materio search api view mode selection'),
  465. 'cache' => DRUPAL_NO_CACHE
  466. );
  467. // $blocks['materio_search_api_filters'] = array(
  468. // 'info' => t('Materio search api filters'),
  469. // 'cache' => DRUPAL_NO_CACHE
  470. // );
  471. return $blocks;
  472. }
  473. /**
  474. * Implements hook_block_view().
  475. */
  476. function materio_search_api_block_view($delta = '') {
  477. // This example comes from node.module. Note that you can also return a
  478. // renderable array rather than rendered HTML for 'content'.
  479. $block = array();
  480. switch ($delta) {
  481. case 'materio_search_api_search':
  482. if (user_access('use materio search api') && user_access('use materio advanced search')) {
  483. $block['subject'] = t('Search');
  484. $block['content'] = theme('materio_search_api_double_search_block', array());
  485. }else if (user_access('use materio search api') || user_access('use materio search api for breves')) {
  486. $block['subject'] = t('Search');
  487. $block['content'] = theme('materio_search_api_search_block', array());
  488. }
  489. break;
  490. case 'mo_searchapi_advanced_search':
  491. if (user_access('use materio advanced search')) {
  492. $block['subject'] = t('Advanced Search');
  493. $block['content'] = theme('materio_search_api_advanced_search_block', array());
  494. }
  495. break;
  496. case 'materio_search_api_viewmode':
  497. // restrict display on node page for materiau and breve
  498. $block_is_visible = true;
  499. $q = parse_url($_GET['q']);
  500. $parts = explode('/', $q['path']);
  501. if($parts[0] == "node"){
  502. $node = node_load($parts[1]);
  503. $block_is_visible = in_array($node->type, array('materiau','breve')) ? true : false;
  504. }
  505. if (user_access('use materio search api viewmode selection') && $block_is_visible) {
  506. $block['subject'] = t('View mode');
  507. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  508. }
  509. break;
  510. // case 'materio_search_api_filters':
  511. // if (user_access('use materio search api filters')) {
  512. // $block['subject'] = t('Filters');
  513. // $block['content'] = theme('materio_search_api_filters_block', array());
  514. // }
  515. // break;
  516. }
  517. return $block;
  518. }
  519. /**
  520. * Implements hook_entity_info_alter().
  521. */
  522. function materio_search_api_entity_info_alter(&$entity_info) {
  523. $entity_info['node']['view modes']['cardsmall'] = array(
  524. 'label' => t('Small cards'),
  525. 'custom settings' => TRUE,
  526. );
  527. $entity_info['node']['view modes']['cardmedium'] = array(
  528. 'label' => t('Medium cards'),
  529. 'custom settings' => TRUE,
  530. );
  531. $entity_info['node']['view modes']['cardbig'] = array(
  532. 'label' => t('Big cards'),
  533. 'custom settings' => TRUE,
  534. );
  535. $entity_info['node']['view modes']['cardfull'] = array(
  536. 'label' => t('Full cards'),
  537. 'custom settings' => TRUE,
  538. );
  539. }
  540. /**
  541. * Implements hook_node_view_alter().
  542. */
  543. function materio_search_api_node_view_alter(&$build) {
  544. $node = $build['#node'];
  545. if (arg(0) == 'node' && arg(1) == $node->nid) {
  546. // dsm($build, 'build');
  547. global $user;
  548. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  549. // dsm($viewmode, 'viewmode');
  550. $node = $build['#node'];
  551. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  552. $build = node_view($node, $viewmode);
  553. }
  554. }
  555. }
  556. /**
  557. * Implements hook_node_view().
  558. */
  559. function materio_search_api_node_view($node, $view_mode, $langcode) {
  560. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  561. # PRINT 7.1
  562. // print_node_view($node, 'full');
  563. // print_pdf_node_view($node, 'full');
  564. # PRINT 7.2-beta
  565. // call for hook_node_view of print module to insert link
  566. print_ui_node_view($node, 'full');
  567. // dsm($node, 'node');
  568. }
  569. }
  570. // _____ __ ______
  571. // / ___/___ ____ ___________/ /_ / ____/___ _________ ___
  572. // \__ \/ _ \/ __ `/ ___/ ___/ __ \ / /_ / __ \/ ___/ __ `__ \
  573. // ___/ / __/ /_/ / / / /__/ / / / / __/ / /_/ / / / / / / / /
  574. // /____/\___/\__,_/_/ \___/_/ /_/ /_/ \____/_/ /_/ /_/ /_/
  575. /**
  576. * materiobase_search_form()
  577. */
  578. function materio_search_api_search_form($form, &$form_state){
  579. // dsm($form_state, 'form_state');
  580. // dsm($form, 'form');
  581. global $user;
  582. $form = array();
  583. $args = arg();
  584. $path = array_shift($args);
  585. if (isset($args[0]) && $args[0] == "filters") {
  586. $path .= '/'.array_shift($args);
  587. }
  588. $is_explore = $path == 'explore' || $path == 'explore/filters';
  589. $keys = implode('/', $args);
  590. if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters
  591. $query = new EntityFieldQuery();
  592. $query->entityCondition('entity_type', 'node')
  593. ->entityCondition('bundle', 'materiau')
  594. ->propertyCondition('status', 1);
  595. $count = $query->count()->execute();
  596. $default_value = t("search among our !fiches cards", array("!fiches"=>$count));
  597. }else{
  598. $default_value = '';
  599. }
  600. $form['searchfield'] = array(
  601. '#type' => 'textfield',
  602. '#default_value' => $is_explore ? $keys : $default_value, // TODO: set the search page path global or a variable in settings
  603. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  604. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  605. '#size' => 30,
  606. '#maxlength' => 1024,
  607. '#attributes' => array("default"=>$default_value),
  608. );
  609. if(user_access('use materio search api filters')){
  610. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  611. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  612. foreach ($indexed_bundles as $bundle) {
  613. $bundles_options[$bundle] = $bundle;
  614. $default_bundles[] = $bundle;
  615. }
  616. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  617. $form['bundles_filter'] = array(
  618. '#type'=>'checkboxes',
  619. '#options' => $bundles_options,
  620. '#default_value' => $user_bundles_filter,
  621. // '#attributes' => array('class'=>array('btn-group')),
  622. );
  623. }
  624. $form['create'] = array(
  625. '#type' => 'image_button',
  626. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  627. '#value' => t('Search'),
  628. '#prefix' => '<div class="search-btn-wrapper">',
  629. '#suffix'=> '</div>'
  630. );
  631. // add a back to search/base home bouton if on explore/foo
  632. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  633. // buttons form container
  634. $form['rightcol'] = array(
  635. '#type' => 'container',
  636. );
  637. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  638. 'html'=>true,
  639. 'attributes'=>array('class'=>'back-search-home')
  640. ));
  641. $form['rightcol']['actu'] = array(
  642. '#type' => 'markup',
  643. '#markup' => $link,
  644. //'<a href="'.base_path().'actuality" class="back-search-home"><i class="fi-home"></i></a>'
  645. );
  646. }
  647. return $form;
  648. }
  649. function materio_search_api_search_form_validate($form, &$form_state){
  650. // dsm($form, '$form');
  651. // dsm($form_state, '$form_state');
  652. // dsm(strlen($form_state['values']['searchfield']));
  653. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  654. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  655. }
  656. }
  657. function materio_search_api_search_form_submit($form, &$form_state){
  658. // dsm($form_state, 'form_state');
  659. global $user;
  660. if(user_access('use materio search api filters')){
  661. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  662. if($value)
  663. $bundles[] = $bundle;
  664. # if no filter checked we checked them all by default
  665. if(!isset($bundles))
  666. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  667. $bundles[] = $bundle;
  668. }else{
  669. # if user have no access to filters
  670. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  671. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  672. foreach ($indexed_bundles as $bundle) {
  673. $bundles[] = $bundle;
  674. }
  675. }
  676. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  677. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  678. }
  679. // ___ __ __ _____ __
  680. // / | ____/ / ______ _____ ________ ____/ / / ___/___ ____ ___________/ /_
  681. // / /| |/ __ / | / / __ `/ __ \/ ___/ _ \/ __ / \__ \/ _ \/ __ `/ ___/ ___/ __ \
  682. // / ___ / /_/ /| |/ / /_/ / / / / /__/ __/ /_/ / ___/ / __/ /_/ / / / /__/ / / /
  683. // /_/ |_\__,_/ |___/\__,_/_/ /_/\___/\___/\__,_/ /____/\___/\__,_/_/ \___/_/ /_/
  684. /**
  685. * materio_search_api_advanced_search_form()
  686. */
  687. function materio_search_api_advanced_search_form($form, &$form_state){
  688. // dsm($form_state, 'form_state');
  689. // dsm($form, 'form');
  690. // get vid from settings
  691. $vid = variable_get('msa-advancedsearchvocabulary', null);
  692. if($vid){
  693. $voc = taxonomy_vocabulary_load($vid);
  694. $voc_machinename = $voc->machine_name;
  695. // dsm($voc, "voc");
  696. $hidden_terms = variable_get('msa-hiddentaxoterms', array());
  697. // dsm($hidden_terms, 'hidden_terms');
  698. $values = isset($form_state['values']) ? $form_state['values'] : null;
  699. // dsm($values, "values");
  700. $args = arg();
  701. // dsm($args, 'args');
  702. // get the first elemt of url args
  703. $path = array_shift($args);
  704. if (isset($args[0]) && $args[0] == "filters") {
  705. $path .= '/'.array_shift($args);
  706. }
  707. // get the keywords from args and reconstruct the filter lines with default values
  708. $args_values = array();
  709. if($path == 'explore/filters' && $args[0] !== "ajax"){
  710. // WORKING WITH TIDS instead of term name
  711. $tids = explode("+", $args[0]);
  712. foreach ($tids as $key => $tid) {
  713. $term = taxonomy_term_load($tid);
  714. // dsm($term, $term->tid.' : '.$term->name);
  715. // get parents
  716. if(isset($term->tid)){
  717. $parents = taxonomy_get_parents_all($tid);
  718. // dsm($parents, 'parents');
  719. // build default values
  720. for ($p=count($parents)-1; $p >=0 ; $p--) {
  721. $args_values[$key][] = $parents[$p]->tid;
  722. }
  723. }
  724. }
  725. }
  726. // dsm($args_values, 'args_values');
  727. // TODO: show result count live
  728. // TODO: filter next select form item
  729. // define number of lines,
  730. // 1 by default
  731. // or from url arguments
  732. // or let it as it is if already chanched by ajax
  733. if(!isset($form_state['filterlines'])){
  734. if(count($args_values)){
  735. $form_state['filterlines'] = count($args_values);
  736. }else{
  737. $form_state['filterlines'] = 1;
  738. }
  739. }
  740. // dsm($form_state['filterlines'], "form_state['filterlines']");
  741. // filters form container
  742. $form['filters'] = array(
  743. '#type' => 'container',
  744. '#attributes' => array('class' => array("advancedsearch-filters")),
  745. '#prefix' => '<div id="advancedsearch-filters-wrapper">',
  746. '#suffix' => '</div>',
  747. );
  748. $form['filters']['filters-table'] = array(
  749. '#type' => 'container',
  750. '#attributes' => array('class' => array("advanced-search-form-table"))
  751. );
  752. // form headers
  753. // $form['filters']['filters-table']['headers'] = array(
  754. // '#type' => 'container',
  755. // '#attributes' => array('class' => array("advanced-search-form-headers")),
  756. // 'category' => array(
  757. // '#markup' => t("Category"),
  758. // '#prefix' => '<div>','#suffix' => '</div>',
  759. // ),
  760. // 'main' => array(
  761. // '#markup' => t("Main Filter"),
  762. // '#prefix' => '<div>','#suffix' => '</div>',
  763. // ),
  764. // 'child' => array(
  765. // '#markup' => t("Child Filter"),
  766. // '#prefix' => '<div>','#suffix' => '</div>',
  767. // )
  768. // );
  769. $trigger = FALSE;
  770. if (!empty($form_state['triggering_element']['#name'])) {
  771. $trigger = $form_state['triggering_element'];
  772. }
  773. $rmline = false;
  774. if ($trigger) {
  775. // dsm($form_state['triggering_element'], $form_state['triggering_element']['#name']);
  776. // dsm($values, 'form state values');
  777. // add a line if needed
  778. if($trigger['#name'] == 'addfilters'){
  779. $form_state['filterlines'] = $form_state['filterlines']+1;
  780. }
  781. // remove a line if needed
  782. if(strpos($trigger['#name'], 'rm-filter-') === 0){
  783. // $form_state['filterlines'] = $form_state['filterlines']+1;
  784. $rmline = $trigger['#return_value'];
  785. // dsm($rmline, "rmline");
  786. }
  787. }
  788. // build first level taxonomy tree and select options
  789. $taxotree = taxonomy_get_tree($vid, 0, 1, true);
  790. // $taxotree = msa_get_taxo_tree($vid, 0, 1);
  791. // dsm($taxotree, "taxotree");
  792. // return $form;
  793. foreach ($taxotree as $index => $term) {
  794. // TODO: get translated tag name
  795. if(!in_array($term->tid, $hidden_terms)){
  796. $level_0_ops[$term->tid] = $term->name;
  797. }
  798. }
  799. // build filters lines (ajax enabled)
  800. for ($l=0; $l < $form_state['filterlines']; $l++) {
  801. // if rmline this line,
  802. // continue the loop without creating the line
  803. if($l === $rmline){
  804. continue;
  805. }
  806. // if we are after the removed line,
  807. // create a second counter ($ll) to keep a continuous order for form items
  808. // and keep the current counter ($l) to retrieve de current values
  809. // if before removed line or no removed line at all, $l = $ll
  810. if($rmline !== false && $l >= $rmline){
  811. $ll = $l-1;
  812. }else{
  813. $ll = $l;
  814. }
  815. // dsm("l : ".$l." | ll : ".$ll);
  816. $form['filters']['filters-table']['filterline-'.$ll] = array(
  817. '#type' => 'container',
  818. '#attributes' => array('class' => array("filter-line"))
  819. );
  820. // get the default value
  821. // from from_state values if ajax
  822. // or from url args
  823. // or 0
  824. $default0 = null;
  825. if(isset($values['filter-'.$l.'-0']) && $values['filter-'.$l.'-0']){
  826. $default0 = $values['filter-'.$l.'-0'];
  827. }elseif(isset($args_values[$l][0])){
  828. $default0 = $args_values[$l][0];
  829. }
  830. // dsm($default0, "default 0");
  831. // form select element for first level
  832. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-0'] = array(
  833. '#type' => 'select',
  834. '#options' => $level_0_ops,
  835. '#name' => 'filter-'.$ll.'-0',
  836. '#empty_option' => t("Choose a category"),
  837. // '#empty_values' => 0,
  838. '#default_value' => $default0,
  839. '#ajax' => array(
  840. 'callback' => 'materio_search_api_advanced_search_select_callback',
  841. 'wrapper' => 'advancedsearch-filters-wrapper',
  842. ),
  843. );
  844. // without this line, form api will mess with default values
  845. $form_state["input"]['filter-'.$ll.'-0'] = $default0;
  846. // add level 1 filter if level 0 chosen
  847. if(is_null($default0)){
  848. // $form['filters']['filterline-'.$ll]['filter-'.$ll.'-1']
  849. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  850. '#markup' => t("Main Filter"),
  851. '#prefix' => '<div class="filter-markup"><span>',
  852. '#suffix' => '</span></div>',
  853. );
  854. // the definition of this markup appears twice, i may avoid that ...
  855. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  856. '#markup' => t("Child Filter"),
  857. '#prefix' => '<div class="filter-markup"><span>',
  858. '#suffix' => '</span></div>',
  859. );
  860. }else{
  861. // build second level taxonomy tree and select options
  862. $taxotree1 = taxonomy_get_tree($vid, $default0, 1, true);
  863. // dsm($taxotree1, 'taxotree1');
  864. $level_1_ops = array();
  865. foreach ($taxotree1 as $index => $term) {
  866. // TODO: get translated tag name
  867. // $term = taxonomy_term_load($tid);
  868. $level_1_ops[$term->tid] = $term->name;
  869. }
  870. // get the default value
  871. // from from_state values if ajax
  872. // or from url args
  873. // or 0
  874. $default1 = null;
  875. if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
  876. $default1 = $values['filter-'.$l.'-1'];
  877. }elseif(isset($args_values[$l][1])){
  878. $default1 = $args_values[$l][1];
  879. }
  880. // dsm($default1, "default 1");
  881. // form select element for second level
  882. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  883. '#type' => 'select',
  884. '#options' => $level_1_ops,
  885. '#name' => 'filter-'.$ll.'-1',
  886. '#empty_option' => t("Choose a main filter"),
  887. // '#empty_values' => 0,
  888. '#default_value' => $default1,
  889. '#ajax' => array(
  890. 'callback' => 'materio_search_api_advanced_search_select_callback',
  891. 'wrapper' => 'advancedsearch-filters-wrapper',
  892. ),
  893. );
  894. // without this line, form api will mess with default values
  895. $form_state["input"]['filter-'.$ll.'-1'] = $default1;
  896. // add level 2 filter if level 1 chosen
  897. if(is_null($default1)){
  898. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  899. '#markup' => t("Child Filter"),
  900. '#prefix' => '<div class="filter-markup"><span>',
  901. '#suffix' => '</span></div>',
  902. );
  903. }else{
  904. // build second level taxonomy tree and select options
  905. $taxotree2 = taxonomy_get_tree($vid, $default1, 1, true);
  906. if(!count($taxotree2)){
  907. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  908. '#markup' => t("No Child"),
  909. '#prefix' => '<div class="filter-markup"><span>',
  910. '#suffix' => '</span></div>',
  911. );
  912. }else{
  913. // $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
  914. $default2 = null;
  915. if(isset($values['filter-'.$l.'-2']) && $values['filter-'.$l.'-2']){
  916. $default2 = $values['filter-'.$l.'-2'];
  917. }elseif(isset($args_values[$l][2])){
  918. $default2 = $args_values[$l][2];
  919. }
  920. // dsm($default2, "default 2");
  921. $level_2_ops = array();
  922. foreach ($taxotree2 as $index => $term) {
  923. // TODO: get translated tag name
  924. $level_2_ops[$term->tid] = t($term->name);
  925. }
  926. // form select element for third level
  927. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  928. '#type' => 'select',
  929. '#options' => $level_2_ops,
  930. '#name' => 'filter-'.$ll.'-2',
  931. '#empty_option' => t("Refine with a child filter"),
  932. // '#empty_values' => 0,
  933. '#default_value' => $default2,
  934. );
  935. // without this line, form api will mess with default values
  936. $form_state["input"]['filter-'.$ll.'-2'] = $default2;
  937. }
  938. }
  939. }
  940. // add remove the line button
  941. $form['filters']['filters-table']['filterline-'.$ll]['rm-filter-'.$ll] = array(
  942. '#type' => 'image_button',
  943. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/rmfilter.png',
  944. '#title' => t('Remove this filter line'),
  945. '#name' => 'rm-filter-'.$ll,
  946. '#return_value' => $ll,
  947. '#executes_submit_callback' => false,
  948. '#ajax' => array(
  949. 'callback' => 'materio_search_api_advanced_search_rmline_callback',
  950. 'wrapper' => 'advancedsearch-filters-wrapper',
  951. // 'trigger_as' => array("name"=>"rmfilters".$l),
  952. ),
  953. "#attributes" => array('class'=>array('rm-btn')),
  954. );
  955. } // end of lines loop
  956. if($rmline !== false){
  957. $form_state['filterlines'] = $form_state['filterlines']-1;
  958. }
  959. // button to add a new line of criterias
  960. $form['filters']['addfilters'] = array(
  961. '#type' => 'image_button',
  962. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/addfilter.png',
  963. '#title' => t('Add a filter line'),
  964. '#name' => "addfilters",
  965. '#return_value' => "add",
  966. '#ajax' => array(
  967. 'callback' => 'materio_search_api_advanced_search_addfilter_callback',
  968. 'wrapper' => 'advancedsearch-filters-wrapper',
  969. ),
  970. '#executes_submit_callback' => false,
  971. '#attributes' => array('class'=>array('add-filter'))
  972. );
  973. // buttons form container
  974. $form['rightcol'] = array(
  975. '#type' => 'container',
  976. "#attributes" => array('class'=>array('right-col')),
  977. );
  978. // add a back to search/base home bouton if on explore/foo
  979. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  980. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  981. 'html'=>true,
  982. 'attributes'=>array('class'=>'back-search-home')
  983. ));
  984. $form['rightcol']['actu'] = array(
  985. '#type' => 'markup',
  986. '#markup' => $link,
  987. );
  988. }
  989. // submit the search
  990. $form['rightcol']['search'] = array(
  991. '#type' => 'image_button',
  992. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  993. '#title' => t('Search'),
  994. '#name' => "search",
  995. '#return_value' => "search",
  996. '#prefix' => '<div class="search-btn-wrapper">',
  997. '#suffix'=> '</div>'
  998. );
  999. }else{
  1000. $form['novoc'] = array(
  1001. '#markup' => "Please choose a vocabulary to search from in materio search api settings"
  1002. );
  1003. }
  1004. // dsm($form, 'form');
  1005. return $form;
  1006. }
  1007. function msa_get_taxo_tree($vid, $parent, $depth){
  1008. $query = new EntityFieldQuery();
  1009. $query->entityCondition('entity_type', 'taxonomy_term')
  1010. ->propertyCondition('vid', $vid);
  1011. // ->propertyCondition('parent', $parent);
  1012. // ->propertyCondition('status', 1);
  1013. // ->entityCondition('bundle', $voc_machinename)
  1014. $result = $query->execute();
  1015. if (isset($result['taxonomy_term'])) {
  1016. $terms = $result['taxonomy_term'];
  1017. // At first we need to get field's id. If you already know field id, you can ommit this step
  1018. // Get all fields attached to a given node type
  1019. $fields = field_info_instances('taxonomy_term');
  1020. // Get id of body field
  1021. // $field_id = $fields['name']['field_id'];
  1022. // Attach a field of selected id only to get value for it
  1023. field_attach_load('taxonomy_term', $terms, FIELD_LOAD_CURRENT, array('field_id' => 'name'));
  1024. // Get values of our node field
  1025. // $output = field_get_items('node', $stories, 'field_story_image');
  1026. }
  1027. return $terms;
  1028. }
  1029. function materio_search_api_advanced_search_select_callback($form, &$form_state) {
  1030. return $form['filters'];
  1031. }
  1032. function materio_search_api_advanced_search_addfilter_callback($form, &$form_state) {
  1033. return $form['filters'];
  1034. }
  1035. function materio_search_api_advanced_search_rmline_callback($form, &$form_state) {
  1036. return $form['filters'];
  1037. }
  1038. function materio_search_api_advanced_search_form_validate($form, &$form_state){
  1039. if($form_state['triggering_element']['#name'] == 'search'){
  1040. // dsm($form, '$form');
  1041. dsm($form_state, 'form_state');
  1042. if (!isset($form_state['values']['filter-0-1']) || $form_state['values']['filter-0-1'] == '') {
  1043. form_set_error('filters', 'Please choose at least 1 keyword.');
  1044. }
  1045. }
  1046. }
  1047. function materio_search_api_advanced_search_form_submit($form, &$form_state){
  1048. // dsm($form_state, 'form_state');
  1049. $values = $form_state['values'];
  1050. $filterlines = isset($form_state['filterlines']) ? $form_state['filterlines'] : 0;
  1051. // $keywords = array();
  1052. // extract last tag of each lines
  1053. for ($l=0; $l < $filterlines ; $l++) {
  1054. for ($i=2; $i > 0 ; $i--) {
  1055. if(isset($values['filter-'.$l.'-'.$i]) && $values['filter-'.$l.'-'.$i]){
  1056. // $term = taxonomy_term_load($values['filter-'.$l.'-'.$i]);
  1057. // $keywords[$term->tid] = '"'.$term->name.'"';
  1058. // $tids[] = '"'.$values['filter-'.$l.'-'.$i].'"';
  1059. $tids[] = $values['filter-'.$l.'-'.$i];
  1060. continue 2;
  1061. }else{
  1062. continue;
  1063. }
  1064. }
  1065. }
  1066. // dsm($keywords, 'keywordstid');
  1067. $keywords_str = implode("+", $tids);
  1068. // dsm($keywords_str);
  1069. $form_state['redirect'] = 'explore/filters/'.$keywords_str; //rawurlencode($keywords_str);
  1070. }
  1071. /**
  1072. * viewmode
  1073. */
  1074. function _materio_search_api_change_viewmode($vm){
  1075. // dsm($vm, '_materio_search_api_change_viewmode');
  1076. global $user;
  1077. // dsm($user, 'user');
  1078. $entity_infos = entity_get_info();
  1079. // dsm($entity_infos, 'entity_infos');
  1080. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  1081. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  1082. $rep = array('statut'=>'saved');
  1083. }else{
  1084. $rep = array('statut'=>'viewmode not allowed');
  1085. }
  1086. return $rep;
  1087. }
  1088. /**
  1089. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  1090. */
  1091. /**
  1092. * Implements hook_theme().
  1093. */
  1094. function materio_search_api_theme($existing, $type, $theme, $path) {
  1095. return array(
  1096. 'materio_search_api_search_block' => array(
  1097. 'arguments' => array(),
  1098. 'template' => 'materio-search-api-search-block',
  1099. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1100. ),
  1101. 'materio_search_api_advanced_search_block' => array(
  1102. 'arguments' => array(),
  1103. 'template' => 'materio-search-api-advanced-search-block',
  1104. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1105. ),
  1106. 'materio_search_api_double_search_block' => array(
  1107. 'arguments' => array(),
  1108. 'template' => 'materio-search-api-double-search-block',
  1109. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1110. ),
  1111. 'materio_search_api_select_viewmode_block' => array(
  1112. 'arguments' => array(),
  1113. 'template' => 'materio-search-api-select-viewmode-block',
  1114. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1115. ),
  1116. // 'materio_search_api_filters_block' => array(
  1117. // 'arguments' => array(),
  1118. // 'template' => 'materio-search-api-filters-block',
  1119. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1120. // ),
  1121. 'materio_search_api_results' => array(
  1122. 'arguments' => array(),
  1123. 'template' => 'materio-search-api-results',
  1124. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1125. 'variables' => array(
  1126. 'index' => NULL,
  1127. 'results' => array('result count' => 0),
  1128. 'items' => array(),
  1129. 'view_mode' => 'teaser',
  1130. 'keys' => '',
  1131. // 'page_machine_name' => NULL,
  1132. 'spellcheck' => NULL,
  1133. 'pager' => NULL,
  1134. ),
  1135. ),
  1136. // 'materio_search_api_performance' => array(
  1137. // 'render element' => 'element',
  1138. // ),
  1139. 'materio_search_api_actuality' => array(
  1140. 'template' => 'materio-search-api-actuality',
  1141. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1142. 'arguments' => array(
  1143. 'items' => array(),
  1144. 'view_mode' => "teaser",
  1145. 'pager' => NULL,
  1146. 'count' => 0,
  1147. )
  1148. ),
  1149. 'materio_search_api_form_element' => array(
  1150. 'render element' => 'element',
  1151. ),
  1152. );
  1153. }
  1154. /**
  1155. * Implements theme for rendering search-performance
  1156. */
  1157. // function theme_materio_search_api_performance($variables) {
  1158. // $element = array_shift($variables);
  1159. // return $element['#markup'];
  1160. // }
  1161. /**
  1162. * template_preprocess_materiobase_advanced_search_block();
  1163. */
  1164. function template_preprocess_materio_search_api_search_block(&$vars){
  1165. // dsm($vars, '$vars');
  1166. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1167. }
  1168. /**
  1169. * template_preprocess_materiobase_advanced_search_block();
  1170. */
  1171. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  1172. // dsm($vars, '$vars');
  1173. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1174. }
  1175. /**
  1176. * template_preprocess_materiobase_search_block();
  1177. */
  1178. function template_preprocess_materio_search_api_double_search_block(&$vars){
  1179. // dsm($vars, '$vars');
  1180. $vars['search_title'] = t("Text search");
  1181. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1182. $vars['advanced_search_title'] = t("Filtered search");
  1183. $vars['advanced_searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1184. }
  1185. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  1186. global $user;
  1187. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  1188. $availableviewmodes = variable_get('availableviewmodes', -1);
  1189. // dsm($availableviewmodes);
  1190. $entity_infos = entity_get_info();
  1191. // dsm($entity_infos, 'entity_infos');
  1192. $content = '<div class="btn-group btn-group-vertical">';
  1193. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  1194. if(in_array($viewmode, $availableviewmodes)){
  1195. $link = l(
  1196. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  1197. 'materiosearchapi/viewmode/change/'.$viewmode,
  1198. array(
  1199. 'query' => drupal_get_destination(),
  1200. 'html' => true,
  1201. 'attributes' => array(
  1202. 'class' => array(
  1203. 'viewmode-link',
  1204. 'viewmode-'.$viewmode,
  1205. $active == $viewmode ? " active" : ''
  1206. ),
  1207. 'rel' => $viewmode
  1208. )
  1209. )
  1210. );
  1211. $content .= $link;
  1212. }
  1213. }
  1214. $content .= '</div>';
  1215. $vars['content'] = $content;
  1216. }
  1217. // function template_preprocess_materio_search_api_filters_block(&$vars){
  1218. // $index_machine_name = variable_get('mainsearchindex', -1);
  1219. // $index = search_api_index_load($index_machine_name);
  1220. // dsm($index, 'index');
  1221. // // $entity_infos = entity_get_info($index->item_type);
  1222. // // dsm($entity_infos, 'entity_infos');
  1223. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  1224. // dsm($indexed_bundles, 'indexed_bundles');
  1225. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  1226. // }
  1227. // function materio_search_api_filters_form($form, $form_state, $bundles){
  1228. // $form = array();
  1229. // dsm($bundles, 'bundles');
  1230. // foreach ($bundles as $bundle) {
  1231. // $form[$bundle . '_filter'] = array(
  1232. // '#type'=>'checkbox',
  1233. // '#default_value' => -1,
  1234. // '#title' => $bundle,
  1235. // );
  1236. // }
  1237. // return $form;
  1238. // }
  1239. /**
  1240. * Function for preprocessing the variables for the search_api_page_results
  1241. * template.
  1242. *
  1243. * @param array $variables
  1244. * An associative array containing:
  1245. * - $index: The index this search was executed on.
  1246. * - $results: An array of search results, as returned by
  1247. * SearchApiQueryInterface::execute().
  1248. * - $keys: The keywords of the executed search.
  1249. *
  1250. * @see materio-search-api-results.tpl.php
  1251. */
  1252. function template_preprocess_materio_search_api_results(array &$vars) {
  1253. // dsm($vars, 'vars');
  1254. $results = $vars['results'];
  1255. if(isset($results['result count'])){
  1256. $vars['result_count'] = $results['result count'];
  1257. $vars['sec'] = round($results['performance']['complete'], 3);
  1258. $vars['keywords'] = $vars['keys'];
  1259. if(isset($results['breves count'])){
  1260. // for free users
  1261. // dsm("free users");
  1262. $vars['perfascard'] = true;
  1263. $perfoutput = format_plural(
  1264. $results['breves count'],
  1265. 'The search found 1 news ',
  1266. 'The search found @count news '
  1267. );
  1268. $perfoutput .= format_plural(
  1269. $vars['result_count'] - $results['breves count'],
  1270. 'with 1 associated matter.',
  1271. 'with @count associated matters.'
  1272. );
  1273. $perfoutput .= '<br />';
  1274. $perfoutput .= format_plural(
  1275. $results['could results']['result count'],
  1276. ' You could have found 1 result with a ',
  1277. ' You could have found <strong>@count results</strong> with a '
  1278. );
  1279. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1280. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1281. $vars['search_performance'] = $perfoutput;
  1282. }else{
  1283. // for members
  1284. $vars['perfascard'] = false;
  1285. // $vars['search_performance'] = format_plural(
  1286. // // $results['result count'],
  1287. // $vars['result_count'],
  1288. // 'The search found 1 result ',
  1289. // 'The search found @count results '
  1290. // )
  1291. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  1292. "@count"=>$vars['result_count'],
  1293. "@keys"=>$vars['keywords'])
  1294. );
  1295. // TODO indicate how many materials and how many news
  1296. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  1297. // "@count"=>$vars['result_count'],
  1298. // "@keys"=>$vars['keywords'],
  1299. // "@mat"=>4,
  1300. // "@new"=>7)
  1301. // );
  1302. }
  1303. }else{
  1304. //for anonymous
  1305. $vars['perfascard'] = true;
  1306. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  1307. $perfoutput .= format_plural(
  1308. $results['could results']['result count'],
  1309. ' You could have found 1 result with a ',
  1310. ' You could have found <strong>@count results</strong> with a '
  1311. );
  1312. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1313. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1314. $vars['search_performance'] = $perfoutput;
  1315. }
  1316. //dsm($vars, '$vars');
  1317. }
  1318. function template_preprocess_materio_search_api_actuality(&$vars){
  1319. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  1320. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  1321. $vars['actualities_infos'] = t('');
  1322. }
  1323. function theme_materio_search_api_form_element($variables) {
  1324. // dsm($variables, 'variables');
  1325. $element = &$variables['element'];
  1326. // This function is invoked as theme wrapper, but the rendered form element
  1327. // may not necessarily have been processed by form_builder().
  1328. $element += array(
  1329. '#title_display' => 'before',
  1330. );
  1331. // Add element #id for #type 'item'.
  1332. if (isset($element['#markup']) && !empty($element['#id'])) {
  1333. $attributes['id'] = $element['#id'];
  1334. }
  1335. // Add element's #type and #name as class to aid with JS/CSS selectors.
  1336. $attributes['class'] = array('form-item');
  1337. if (!empty($element['#type'])) {
  1338. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  1339. }
  1340. if (!empty($element['#name'])) {
  1341. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  1342. }
  1343. // Add a class for disabled elements to facilitate cross-browser styling.
  1344. if (!empty($element['#attributes']['disabled'])) {
  1345. $attributes['class'][] = 'form-disabled';
  1346. }
  1347. # adding element class to wrapper
  1348. foreach ($element['#attributes']['class'] as $value) {
  1349. $attributes['class'][] = $value;
  1350. }
  1351. foreach ($element['#attributes'] as $attr => $value) {
  1352. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  1353. $attributes[$attr] = $value;
  1354. }
  1355. }
  1356. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  1357. // If #title is not set, we don't display any label or required marker.
  1358. if (!isset($element['#title'])) {
  1359. $element['#title_display'] = 'none';
  1360. }
  1361. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  1362. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  1363. switch ($element['#title_display']) {
  1364. case 'before':
  1365. case 'invisible':
  1366. $output .= ' ' . theme('form_element_label', $variables);
  1367. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1368. break;
  1369. case 'after':
  1370. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  1371. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  1372. break;
  1373. case 'none':
  1374. case 'attribute':
  1375. // Output no label and no required marker, only the children.
  1376. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1377. break;
  1378. }
  1379. if (!empty($element['#description'])) {
  1380. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  1381. }
  1382. $output .= "</div>\n";
  1383. return $output;
  1384. }
  1385. /**
  1386. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  1387. */
  1388. /**
  1389. * Implements hook_block_view_alter().
  1390. */
  1391. function materio_search_api_block_view_alter(&$data, $block) {
  1392. // this alter prepopulate the search form provide by search_api_page
  1393. if ($block->module == 'search_api_page') {
  1394. $page = search_api_page_load($block->delta);
  1395. $item = menu_get_item();
  1396. if (isset($page->path) && $page->path == $item['path']) {
  1397. $keys = arg(count(arg(NULL, $page->path)));
  1398. if ($keys) {
  1399. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  1400. $data['content']['keys_' . $page->id]['#value'] = $keys;
  1401. }
  1402. }
  1403. }
  1404. }