materio_search_api.module 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. // 'progress'=>array('message'=>''),
  843. ),
  844. );
  845. // without this line, form api will mess with default values
  846. $form_state["input"]['filter-'.$ll.'-0'] = $default0;
  847. // add level 1 filter if level 0 chosen
  848. if(is_null($default0)){
  849. // $form['filters']['filterline-'.$ll]['filter-'.$ll.'-1']
  850. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  851. '#markup' => t("Main Filter"),
  852. '#prefix' => '<div class="filter-markup"><span>',
  853. '#suffix' => '</span></div>',
  854. );
  855. // the definition of this markup appears twice, i may avoid that ...
  856. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  857. '#markup' => t("Child Filter"),
  858. '#prefix' => '<div class="filter-markup"><span>',
  859. '#suffix' => '</span></div>',
  860. );
  861. }else{
  862. // build second level taxonomy tree and select options
  863. $taxotree1 = taxonomy_get_tree($vid, $default0, 1, true);
  864. // dsm($taxotree1, 'taxotree1');
  865. $level_1_ops = array();
  866. foreach ($taxotree1 as $index => $term) {
  867. // TODO: get translated tag name
  868. // $term = taxonomy_term_load($tid);
  869. $level_1_ops[$term->tid] = $term->name;
  870. }
  871. // get the default value
  872. // from from_state values if ajax
  873. // or from url args
  874. // or 0
  875. $default1 = null;
  876. if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
  877. $default1 = $values['filter-'.$l.'-1'];
  878. }elseif(isset($args_values[$l][1])){
  879. $default1 = $args_values[$l][1];
  880. }
  881. // dsm($default1, "default 1");
  882. // form select element for second level
  883. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  884. '#type' => 'select',
  885. '#options' => $level_1_ops,
  886. '#name' => 'filter-'.$ll.'-1',
  887. '#empty_option' => t("Choose a main filter"),
  888. // '#empty_values' => 0,
  889. '#default_value' => $default1,
  890. '#ajax' => array(
  891. 'callback' => 'materio_search_api_advanced_search_select_callback',
  892. 'wrapper' => 'advancedsearch-filters-wrapper',
  893. ),
  894. );
  895. // without this line, form api will mess with default values
  896. $form_state["input"]['filter-'.$ll.'-1'] = $default1;
  897. // add level 2 filter if level 1 chosen
  898. if(is_null($default1)){
  899. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  900. '#markup' => t("Child Filter"),
  901. '#prefix' => '<div class="filter-markup"><span>',
  902. '#suffix' => '</span></div>',
  903. );
  904. }else{
  905. // build second level taxonomy tree and select options
  906. $taxotree2 = taxonomy_get_tree($vid, $default1, 1, true);
  907. if(!count($taxotree2)){
  908. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  909. '#markup' => t("No Child"),
  910. '#prefix' => '<div class="filter-markup"><span>',
  911. '#suffix' => '</span></div>',
  912. );
  913. }else{
  914. // $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
  915. $default2 = null;
  916. if(isset($values['filter-'.$l.'-2']) && $values['filter-'.$l.'-2']){
  917. $default2 = $values['filter-'.$l.'-2'];
  918. }elseif(isset($args_values[$l][2])){
  919. $default2 = $args_values[$l][2];
  920. }
  921. // dsm($default2, "default 2");
  922. $level_2_ops = array();
  923. foreach ($taxotree2 as $index => $term) {
  924. // TODO: get translated tag name
  925. $level_2_ops[$term->tid] = t($term->name);
  926. }
  927. // form select element for third level
  928. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  929. '#type' => 'select',
  930. '#options' => $level_2_ops,
  931. '#name' => 'filter-'.$ll.'-2',
  932. '#empty_option' => t("Refine with a child filter"),
  933. // '#empty_values' => 0,
  934. '#default_value' => $default2,
  935. );
  936. // without this line, form api will mess with default values
  937. $form_state["input"]['filter-'.$ll.'-2'] = $default2;
  938. }
  939. }
  940. }
  941. // add remove the line button
  942. $form['filters']['filters-table']['filterline-'.$ll]['rm-filter-'.$ll] = array(
  943. '#type' => 'image_button',
  944. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/rmfilter.png',
  945. '#title' => t('Remove this filter line'),
  946. '#name' => 'rm-filter-'.$ll,
  947. '#return_value' => $ll,
  948. '#executes_submit_callback' => false,
  949. '#ajax' => array(
  950. 'callback' => 'materio_search_api_advanced_search_rmline_callback',
  951. 'wrapper' => 'advancedsearch-filters-wrapper',
  952. // 'trigger_as' => array("name"=>"rmfilters".$l),
  953. ),
  954. // "#attributes" => array('class'=>array('rm-btn')),
  955. '#prefix'=> '<div class="rm-btn">', '#suffix'=> '</div>',
  956. );
  957. } // end of lines loop
  958. if($rmline !== false){
  959. $form_state['filterlines'] = $form_state['filterlines']-1;
  960. }
  961. // button to add a new line of criterias
  962. $form['filters']['addfilters'] = array(
  963. '#type' => 'image_button',
  964. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/addfilter.png',
  965. '#title' => t('Add a filter line'),
  966. '#name' => "addfilters",
  967. '#return_value' => "add",
  968. '#ajax' => array(
  969. 'callback' => 'materio_search_api_advanced_search_addfilter_callback',
  970. 'wrapper' => 'advancedsearch-filters-wrapper',
  971. ),
  972. '#executes_submit_callback' => false,
  973. '#attributes' => array('class'=>array('add-filter'))
  974. );
  975. // buttons form container
  976. $form['rightcol'] = array(
  977. '#type' => 'container',
  978. "#attributes" => array('class'=>array('right-col')),
  979. );
  980. // add a back to search/base home bouton if on explore/foo
  981. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  982. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  983. 'html'=>true,
  984. 'attributes'=>array('class'=>'back-search-home')
  985. ));
  986. $form['rightcol']['actu'] = array(
  987. '#type' => 'markup',
  988. '#markup' => $link,
  989. );
  990. }
  991. // submit the search
  992. $form['rightcol']['search'] = array(
  993. '#type' => 'image_button',
  994. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  995. '#title' => t('Search'),
  996. '#name' => "search",
  997. '#return_value' => "search",
  998. '#prefix' => '<div class="search-btn-wrapper">',
  999. '#suffix'=> '</div>'
  1000. );
  1001. }else{
  1002. $form['novoc'] = array(
  1003. '#markup' => "Please choose a vocabulary to search from in materio search api settings"
  1004. );
  1005. }
  1006. // dsm($form, 'form');
  1007. return $form;
  1008. }
  1009. function msa_get_taxo_tree($vid, $parent, $depth){
  1010. $query = new EntityFieldQuery();
  1011. $query->entityCondition('entity_type', 'taxonomy_term')
  1012. ->propertyCondition('vid', $vid);
  1013. // ->propertyCondition('parent', $parent);
  1014. // ->propertyCondition('status', 1);
  1015. // ->entityCondition('bundle', $voc_machinename)
  1016. $result = $query->execute();
  1017. if (isset($result['taxonomy_term'])) {
  1018. $terms = $result['taxonomy_term'];
  1019. // At first we need to get field's id. If you already know field id, you can ommit this step
  1020. // Get all fields attached to a given node type
  1021. $fields = field_info_instances('taxonomy_term');
  1022. // Get id of body field
  1023. // $field_id = $fields['name']['field_id'];
  1024. // Attach a field of selected id only to get value for it
  1025. field_attach_load('taxonomy_term', $terms, FIELD_LOAD_CURRENT, array('field_id' => 'name'));
  1026. // Get values of our node field
  1027. // $output = field_get_items('node', $stories, 'field_story_image');
  1028. }
  1029. return $terms;
  1030. }
  1031. function materio_search_api_advanced_search_select_callback($form, &$form_state) {
  1032. return $form['filters'];
  1033. }
  1034. function materio_search_api_advanced_search_addfilter_callback($form, &$form_state) {
  1035. $commands = array();
  1036. $commands[] = ajax_command_replace('#advancedsearch-filters-wrapper', render($form['filters']));
  1037. $commands[] = array('command'=>'ajaxAfterAddedFilterLine');
  1038. return array("#type"=>'ajax', "#commands"=>$commands);
  1039. // return $form['filters'];
  1040. }
  1041. function materio_search_api_advanced_search_rmline_callback($form, &$form_state) {
  1042. $commands = array();
  1043. $commands[] = ajax_command_replace('#advancedsearch-filters-wrapper', render($form['filters']));
  1044. $commands[] = array('command'=>'ajaxAfterRemovedFilterLine');
  1045. return array("#type"=>'ajax', "#commands"=>$commands);
  1046. // return $form['filters'];
  1047. }
  1048. function materio_search_api_advanced_search_form_validate($form, &$form_state){
  1049. if($form_state['triggering_element']['#name'] == 'search'){
  1050. // dsm($form, '$form');
  1051. dsm($form_state, 'form_state');
  1052. if (!isset($form_state['values']['filter-0-1']) || $form_state['values']['filter-0-1'] == '') {
  1053. form_set_error('filters', 'Please choose at least 1 keyword.');
  1054. }
  1055. }
  1056. }
  1057. function materio_search_api_advanced_search_form_submit($form, &$form_state){
  1058. // dsm($form_state, 'form_state');
  1059. $values = $form_state['values'];
  1060. $filterlines = isset($form_state['filterlines']) ? $form_state['filterlines'] : 0;
  1061. // $keywords = array();
  1062. // extract last tag of each lines
  1063. for ($l=0; $l < $filterlines ; $l++) {
  1064. for ($i=2; $i > 0 ; $i--) {
  1065. if(isset($values['filter-'.$l.'-'.$i]) && $values['filter-'.$l.'-'.$i]){
  1066. // $term = taxonomy_term_load($values['filter-'.$l.'-'.$i]);
  1067. // $keywords[$term->tid] = '"'.$term->name.'"';
  1068. // $tids[] = '"'.$values['filter-'.$l.'-'.$i].'"';
  1069. $tids[] = $values['filter-'.$l.'-'.$i];
  1070. continue 2;
  1071. }else{
  1072. continue;
  1073. }
  1074. }
  1075. }
  1076. // dsm($keywords, 'keywordstid');
  1077. $keywords_str = implode("+", $tids);
  1078. // dsm($keywords_str);
  1079. $form_state['redirect'] = 'explore/filters/'.$keywords_str; //rawurlencode($keywords_str);
  1080. }
  1081. /**
  1082. * viewmode
  1083. */
  1084. function _materio_search_api_change_viewmode($vm){
  1085. // dsm($vm, '_materio_search_api_change_viewmode');
  1086. global $user;
  1087. // dsm($user, 'user');
  1088. $entity_infos = entity_get_info();
  1089. // dsm($entity_infos, 'entity_infos');
  1090. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  1091. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  1092. $rep = array('statut'=>'saved');
  1093. }else{
  1094. $rep = array('statut'=>'viewmode not allowed');
  1095. }
  1096. return $rep;
  1097. }
  1098. /**
  1099. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  1100. */
  1101. /**
  1102. * Implements hook_theme().
  1103. */
  1104. function materio_search_api_theme($existing, $type, $theme, $path) {
  1105. return array(
  1106. 'materio_search_api_search_block' => array(
  1107. 'arguments' => array(),
  1108. 'template' => 'materio-search-api-search-block',
  1109. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1110. ),
  1111. 'materio_search_api_advanced_search_block' => array(
  1112. 'arguments' => array(),
  1113. 'template' => 'materio-search-api-advanced-search-block',
  1114. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1115. ),
  1116. 'materio_search_api_double_search_block' => array(
  1117. 'arguments' => array(),
  1118. 'template' => 'materio-search-api-double-search-block',
  1119. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1120. ),
  1121. 'materio_search_api_select_viewmode_block' => array(
  1122. 'arguments' => array(),
  1123. 'template' => 'materio-search-api-select-viewmode-block',
  1124. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1125. ),
  1126. // 'materio_search_api_filters_block' => array(
  1127. // 'arguments' => array(),
  1128. // 'template' => 'materio-search-api-filters-block',
  1129. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1130. // ),
  1131. 'materio_search_api_results' => array(
  1132. 'arguments' => array(),
  1133. 'template' => 'materio-search-api-results',
  1134. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1135. 'variables' => array(
  1136. 'index' => NULL,
  1137. 'results' => array('result count' => 0),
  1138. 'items' => array(),
  1139. 'view_mode' => 'teaser',
  1140. 'keys' => '',
  1141. // 'page_machine_name' => NULL,
  1142. 'spellcheck' => NULL,
  1143. 'pager' => NULL,
  1144. ),
  1145. ),
  1146. // 'materio_search_api_performance' => array(
  1147. // 'render element' => 'element',
  1148. // ),
  1149. 'materio_search_api_actuality' => array(
  1150. 'template' => 'materio-search-api-actuality',
  1151. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1152. 'arguments' => array(
  1153. 'items' => array(),
  1154. 'view_mode' => "teaser",
  1155. 'pager' => NULL,
  1156. 'count' => 0,
  1157. )
  1158. ),
  1159. 'materio_search_api_form_element' => array(
  1160. 'render element' => 'element',
  1161. ),
  1162. );
  1163. }
  1164. /**
  1165. * Implements theme for rendering search-performance
  1166. */
  1167. // function theme_materio_search_api_performance($variables) {
  1168. // $element = array_shift($variables);
  1169. // return $element['#markup'];
  1170. // }
  1171. /**
  1172. * template_preprocess_materiobase_advanced_search_block();
  1173. */
  1174. function template_preprocess_materio_search_api_search_block(&$vars){
  1175. // dsm($vars, '$vars');
  1176. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1177. }
  1178. /**
  1179. * template_preprocess_materiobase_advanced_search_block();
  1180. */
  1181. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  1182. // dsm($vars, '$vars');
  1183. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1184. }
  1185. /**
  1186. * template_preprocess_materiobase_search_block();
  1187. */
  1188. function template_preprocess_materio_search_api_double_search_block(&$vars){
  1189. // dsm($vars, '$vars');
  1190. $vars['search_title'] = t("Text search");
  1191. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1192. $vars['advanced_search_title'] = t("Filtered search");
  1193. $vars['advanced_searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1194. }
  1195. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  1196. global $user;
  1197. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  1198. $availableviewmodes = variable_get('availableviewmodes', -1);
  1199. // dsm($availableviewmodes);
  1200. $entity_infos = entity_get_info();
  1201. // dsm($entity_infos, 'entity_infos');
  1202. $content = '<div class="btn-group btn-group-vertical">';
  1203. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  1204. if(in_array($viewmode, $availableviewmodes)){
  1205. $link = l(
  1206. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  1207. 'materiosearchapi/viewmode/change/'.$viewmode,
  1208. array(
  1209. 'query' => drupal_get_destination(),
  1210. 'html' => true,
  1211. 'attributes' => array(
  1212. 'class' => array(
  1213. 'viewmode-link',
  1214. 'viewmode-'.$viewmode,
  1215. $active == $viewmode ? " active" : ''
  1216. ),
  1217. 'rel' => $viewmode
  1218. )
  1219. )
  1220. );
  1221. $content .= $link;
  1222. }
  1223. }
  1224. $content .= '</div>';
  1225. $vars['content'] = $content;
  1226. }
  1227. // function template_preprocess_materio_search_api_filters_block(&$vars){
  1228. // $index_machine_name = variable_get('mainsearchindex', -1);
  1229. // $index = search_api_index_load($index_machine_name);
  1230. // dsm($index, 'index');
  1231. // // $entity_infos = entity_get_info($index->item_type);
  1232. // // dsm($entity_infos, 'entity_infos');
  1233. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  1234. // dsm($indexed_bundles, 'indexed_bundles');
  1235. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  1236. // }
  1237. // function materio_search_api_filters_form($form, $form_state, $bundles){
  1238. // $form = array();
  1239. // dsm($bundles, 'bundles');
  1240. // foreach ($bundles as $bundle) {
  1241. // $form[$bundle . '_filter'] = array(
  1242. // '#type'=>'checkbox',
  1243. // '#default_value' => -1,
  1244. // '#title' => $bundle,
  1245. // );
  1246. // }
  1247. // return $form;
  1248. // }
  1249. /**
  1250. * Function for preprocessing the variables for the search_api_page_results
  1251. * template.
  1252. *
  1253. * @param array $variables
  1254. * An associative array containing:
  1255. * - $index: The index this search was executed on.
  1256. * - $results: An array of search results, as returned by
  1257. * SearchApiQueryInterface::execute().
  1258. * - $keys: The keywords of the executed search.
  1259. *
  1260. * @see materio-search-api-results.tpl.php
  1261. */
  1262. function template_preprocess_materio_search_api_results(array &$vars) {
  1263. // dsm($vars, 'vars');
  1264. $results = $vars['results'];
  1265. if(isset($results['result count'])){
  1266. $vars['result_count'] = $results['result count'];
  1267. $vars['sec'] = round($results['performance']['complete'], 3);
  1268. $vars['keywords'] = $vars['keys'];
  1269. if(isset($results['breves count'])){
  1270. // for free users
  1271. // dsm("free users");
  1272. $vars['perfascard'] = true;
  1273. $perfoutput = format_plural(
  1274. $results['breves count'],
  1275. 'The search found 1 news ',
  1276. 'The search found @count news '
  1277. );
  1278. $perfoutput .= format_plural(
  1279. $vars['result_count'] - $results['breves count'],
  1280. 'with 1 associated matter.',
  1281. 'with @count associated matters.'
  1282. );
  1283. $perfoutput .= '<br />';
  1284. $perfoutput .= format_plural(
  1285. $results['could results']['result count'],
  1286. ' You could have found 1 result with a ',
  1287. ' You could have found <strong>@count results</strong> with a '
  1288. );
  1289. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1290. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1291. $vars['search_performance'] = $perfoutput;
  1292. }else{
  1293. // for members
  1294. $vars['perfascard'] = false;
  1295. // $vars['search_performance'] = format_plural(
  1296. // // $results['result count'],
  1297. // $vars['result_count'],
  1298. // 'The search found 1 result ',
  1299. // 'The search found @count results '
  1300. // )
  1301. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  1302. "@count"=>$vars['result_count'],
  1303. "@keys"=>$vars['keywords'])
  1304. );
  1305. // TODO indicate how many materials and how many news
  1306. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  1307. // "@count"=>$vars['result_count'],
  1308. // "@keys"=>$vars['keywords'],
  1309. // "@mat"=>4,
  1310. // "@new"=>7)
  1311. // );
  1312. }
  1313. }else{
  1314. //for anonymous
  1315. $vars['perfascard'] = true;
  1316. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  1317. $perfoutput .= format_plural(
  1318. $results['could results']['result count'],
  1319. ' You could have found 1 result with a ',
  1320. ' You could have found <strong>@count results</strong> with a '
  1321. );
  1322. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1323. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1324. $vars['search_performance'] = $perfoutput;
  1325. }
  1326. //dsm($vars, '$vars');
  1327. }
  1328. function template_preprocess_materio_search_api_actuality(&$vars){
  1329. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  1330. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  1331. $vars['actualities_infos'] = t('');
  1332. }
  1333. function theme_materio_search_api_form_element($variables) {
  1334. // dsm($variables, 'variables');
  1335. $element = &$variables['element'];
  1336. // This function is invoked as theme wrapper, but the rendered form element
  1337. // may not necessarily have been processed by form_builder().
  1338. $element += array(
  1339. '#title_display' => 'before',
  1340. );
  1341. // Add element #id for #type 'item'.
  1342. if (isset($element['#markup']) && !empty($element['#id'])) {
  1343. $attributes['id'] = $element['#id'];
  1344. }
  1345. // Add element's #type and #name as class to aid with JS/CSS selectors.
  1346. $attributes['class'] = array('form-item');
  1347. if (!empty($element['#type'])) {
  1348. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  1349. }
  1350. if (!empty($element['#name'])) {
  1351. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  1352. }
  1353. // Add a class for disabled elements to facilitate cross-browser styling.
  1354. if (!empty($element['#attributes']['disabled'])) {
  1355. $attributes['class'][] = 'form-disabled';
  1356. }
  1357. # adding element class to wrapper
  1358. foreach ($element['#attributes']['class'] as $value) {
  1359. $attributes['class'][] = $value;
  1360. }
  1361. foreach ($element['#attributes'] as $attr => $value) {
  1362. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  1363. $attributes[$attr] = $value;
  1364. }
  1365. }
  1366. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  1367. // If #title is not set, we don't display any label or required marker.
  1368. if (!isset($element['#title'])) {
  1369. $element['#title_display'] = 'none';
  1370. }
  1371. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  1372. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  1373. switch ($element['#title_display']) {
  1374. case 'before':
  1375. case 'invisible':
  1376. $output .= ' ' . theme('form_element_label', $variables);
  1377. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1378. break;
  1379. case 'after':
  1380. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  1381. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  1382. break;
  1383. case 'none':
  1384. case 'attribute':
  1385. // Output no label and no required marker, only the children.
  1386. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1387. break;
  1388. }
  1389. if (!empty($element['#description'])) {
  1390. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  1391. }
  1392. $output .= "</div>\n";
  1393. return $output;
  1394. }
  1395. /**
  1396. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  1397. */
  1398. /**
  1399. * Implements hook_block_view_alter().
  1400. */
  1401. function materio_search_api_block_view_alter(&$data, $block) {
  1402. // this alter prepopulate the search form provide by search_api_page
  1403. if ($block->module == 'search_api_page') {
  1404. $page = search_api_page_load($block->delta);
  1405. $item = menu_get_item();
  1406. if (isset($page->path) && $page->path == $item['path']) {
  1407. $keys = arg(count(arg(NULL, $page->path)));
  1408. if ($keys) {
  1409. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  1410. $data['content']['keys_' . $page->id]['#value'] = $keys;
  1411. }
  1412. }
  1413. }
  1414. }