materio_search_api.module 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. );
  629. // add a back to search/base home bouton if on explore/foo
  630. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  631. // buttons form container
  632. $form['rightcol'] = array(
  633. '#type' => 'container',
  634. );
  635. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  636. 'html'=>true,
  637. 'attributes'=>array('class'=>'back-search-home')
  638. ));
  639. $form['rightcol']['actu'] = array(
  640. '#type' => 'markup',
  641. '#markup' => $link,
  642. //'<a href="'.base_path().'actuality" class="back-search-home"><i class="fi-home"></i></a>'
  643. );
  644. }
  645. return $form;
  646. }
  647. function materio_search_api_search_form_validate($form, &$form_state){
  648. // dsm($form, '$form');
  649. // dsm($form_state, '$form_state');
  650. // dsm(strlen($form_state['values']['searchfield']));
  651. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  652. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  653. }
  654. }
  655. function materio_search_api_search_form_submit($form, &$form_state){
  656. // dsm($form_state, 'form_state');
  657. global $user;
  658. if(user_access('use materio search api filters')){
  659. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  660. if($value)
  661. $bundles[] = $bundle;
  662. # if no filter checked we checked them all by default
  663. if(!isset($bundles))
  664. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  665. $bundles[] = $bundle;
  666. }else{
  667. # if user have no access to filters
  668. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  669. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  670. foreach ($indexed_bundles as $bundle) {
  671. $bundles[] = $bundle;
  672. }
  673. }
  674. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  675. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  676. }
  677. // ___ __ __ _____ __
  678. // / | ____/ / ______ _____ ________ ____/ / / ___/___ ____ ___________/ /_
  679. // / /| |/ __ / | / / __ `/ __ \/ ___/ _ \/ __ / \__ \/ _ \/ __ `/ ___/ ___/ __ \
  680. // / ___ / /_/ /| |/ / /_/ / / / / /__/ __/ /_/ / ___/ / __/ /_/ / / / /__/ / / /
  681. // /_/ |_\__,_/ |___/\__,_/_/ /_/\___/\___/\__,_/ /____/\___/\__,_/_/ \___/_/ /_/
  682. /**
  683. * materio_search_api_advanced_search_form()
  684. */
  685. function materio_search_api_advanced_search_form($form, &$form_state){
  686. // dsm($form_state, 'form_state');
  687. // dsm($form, 'form');
  688. // global $user;
  689. // $form = array();
  690. // get vid from settings
  691. $vid = variable_get('msa-advancedsearchvocabulary', null);
  692. if($vid){
  693. // global $language;
  694. // dsm($language, "language");
  695. $voc = taxonomy_vocabulary_load($vid);
  696. $voc_machinename = $voc->machine_name;
  697. // dsm($voc, "voc");
  698. $hidden_terms = variable_get('msa-hiddentaxoterms', array());
  699. // dsm($hidden_terms, 'hidden_terms');
  700. $values = isset($form_state['values']) ? $form_state['values'] : null;
  701. // dsm($values, "values");
  702. $args = arg();
  703. // dsm($args, 'args');
  704. // get the first elemt of url args
  705. $path = array_shift($args);
  706. if (isset($args[0]) && $args[0] == "filters") {
  707. $path .= '/'.array_shift($args);
  708. }
  709. // get the keywords from args and reconstruct the filter lines with default values
  710. $args_values = array();
  711. if($path == 'explore/filters' && $args[0] !== "ajax"){
  712. // WORKING WITH TERM NAMES instead of tids
  713. // $keywords = explode("+", $args[0]);
  714. // // dsm($keywords, 'keywords');
  715. // foreach ($keywords as $key => $value) {
  716. // $name = str_replace('"', '', $value);
  717. // //Get the term
  718. // $terms = taxonomy_get_term_by_name($name, $voc_machinename);
  719. // $term = array_shift($terms);
  720. // // dsm($term, $term->tid.' : '.$term->name);
  721. // // get parents
  722. // if(isset($term->tid)){
  723. // $parents = taxonomy_get_parents_all($term->tid);
  724. // // dsm($parents, 'parents');
  725. // // build default values
  726. // for ($p=count($parents)-1; $p >=0 ; $p--) {
  727. // $args_values[$key][] = $parents[$p]->tid;
  728. // }
  729. // }
  730. // }
  731. // WORKING WITH TIDS instead of term name
  732. $tids = explode("+", $args[0]);
  733. foreach ($tids as $key => $tid) {
  734. $term = taxonomy_term_load($tid);
  735. // dsm($term, $term->tid.' : '.$term->name);
  736. // get parents
  737. if(isset($term->tid)){
  738. $parents = taxonomy_get_parents_all($tid);
  739. // dsm($parents, 'parents');
  740. // build default values
  741. for ($p=count($parents)-1; $p >=0 ; $p--) {
  742. $args_values[$key][] = $parents[$p]->tid;
  743. }
  744. }
  745. }
  746. }
  747. // dsm($args_values, 'args_values');
  748. // TODO: show result count live
  749. // TODO: filter next select form item
  750. // define number of lines,
  751. // 1 by default
  752. // or from url arguments
  753. // or let it as it is if already chanched by ajax
  754. if(!isset($form_state['filterlines'])){
  755. if(count($args_values)){
  756. $form_state['filterlines'] = count($args_values);
  757. }else{
  758. $form_state['filterlines'] = 1;
  759. }
  760. }
  761. // dsm($form_state['filterlines'], "form_state['filterlines']");
  762. // filters form container
  763. $form['filters'] = array(
  764. '#type' => 'container',
  765. '#prefix' => '<div id="advancedsearch-filters">',
  766. '#suffix' => '</div>',
  767. );
  768. $trigger = FALSE;
  769. if (!empty($form_state['triggering_element']['#name'])) {
  770. $trigger = $form_state['triggering_element'];
  771. }
  772. $rmline = false;
  773. if ($trigger) {
  774. // dsm($form_state['triggering_element'], $form_state['triggering_element']['#name']);
  775. // dsm($values, 'form state values');
  776. // add a line if needed
  777. if($trigger['#name'] == 'addfilters'){
  778. $form_state['filterlines'] = $form_state['filterlines']+1;
  779. }
  780. // remove a line if needed
  781. if(strpos($trigger['#name'], 'rm-filter-') === 0){
  782. // $form_state['filterlines'] = $form_state['filterlines']+1;
  783. $rmline = $trigger['#return_value'];
  784. // dsm($rmline, "rmline");
  785. }
  786. }
  787. // build first level taxonomy tree and select options
  788. $taxotree = taxonomy_get_tree($vid, 0, 1, true);
  789. // $taxotree = msa_get_taxo_tree($vid, 0, 1);
  790. // dsm($taxotree, "taxotree");
  791. // return $form;
  792. foreach ($taxotree as $index => $term) {
  793. // TODO: get translated tag name
  794. if(!in_array($term->tid, $hidden_terms)){
  795. $level_0_ops[$term->tid] = $term->name;
  796. }
  797. }
  798. // build filters lines (ajax enabled)
  799. for ($l=0; $l < $form_state['filterlines']; $l++) {
  800. // if rmline this line,
  801. // continue the loop without creating the line
  802. if($l === $rmline){
  803. continue;
  804. }
  805. // if we are after the removed line,
  806. // create a second counter ($ll) to keep a continuous order for form items
  807. // and keep the current counter ($l) to retrieve de current values
  808. // if before removed line or no removed line at all, $l = $ll
  809. if($rmline !== false && $l >= $rmline){
  810. $ll = $l-1;
  811. }else{
  812. $ll = $l;
  813. }
  814. // dsm("l : ".$l." | ll : ".$ll);
  815. $form['filters']['filterline-'.$ll] = array(
  816. '#type' => 'container',
  817. '#attributes' => array(
  818. 'class' => array("filter-line")
  819. )
  820. );
  821. // get the default value
  822. // from from_state values if ajax
  823. // or from url args
  824. // or 0
  825. $default0 = null;
  826. if(isset($values['filter-'.$l.'-0']) && $values['filter-'.$l.'-0']){
  827. $default0 = $values['filter-'.$l.'-0'];
  828. }elseif(isset($args_values[$l][0])){
  829. $default0 = $args_values[$l][0];
  830. }
  831. // dsm($default0, "default 0");
  832. // form select element for first level
  833. $form['filters']['filterline-'.$ll]['filter-'.$ll.'-0'] = array(
  834. '#type' => 'select',
  835. '#options' => $level_0_ops,
  836. '#name' => 'filter-'.$ll.'-0',
  837. '#empty_option' => t("Choose a filter"),
  838. // '#empty_values' => 0,
  839. '#default_value' => $default0,
  840. '#ajax' => array(
  841. 'callback' => 'materio_search_api_advanced_search_select_callback',
  842. 'wrapper' => 'advancedsearch-filters',
  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($default0){
  849. // build second level taxonomy tree and select options
  850. $taxotree1 = taxonomy_get_tree($vid, $default0, 1, true);
  851. // dsm($taxotree1, 'taxotree1');
  852. $level_1_ops = array();
  853. foreach ($taxotree1 as $index => $term) {
  854. // TODO: get translated tag name
  855. // $term = taxonomy_term_load($tid);
  856. $level_1_ops[$term->tid] = $term->name;
  857. }
  858. // get the default value
  859. // from from_state values if ajax
  860. // or from url args
  861. // or 0
  862. $default1 = null;
  863. if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
  864. $default1 = $values['filter-'.$l.'-1'];
  865. }elseif(isset($args_values[$l][1])){
  866. $default1 = $args_values[$l][1];
  867. }
  868. // dsm($default1, "default 1");
  869. // form select element for second level
  870. $form['filters']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  871. '#type' => 'select',
  872. '#options' => $level_1_ops,
  873. '#name' => 'filter-'.$ll.'-1',
  874. '#empty_option' => t("Choose a filter"),
  875. // '#empty_values' => 0,
  876. '#default_value' => $default1,
  877. '#ajax' => array(
  878. 'callback' => 'materio_search_api_advanced_search_select_callback',
  879. 'wrapper' => 'advancedsearch-filters',
  880. ),
  881. );
  882. // without this line, form api will mess with default values
  883. $form_state["input"]['filter-'.$ll.'-1'] = $default1;
  884. // add level 2 filter if level 1 chosen
  885. if($default1){
  886. // build second level taxonomy tree and select options
  887. $taxotree2 = taxonomy_get_tree($vid, $default1, 1, true);
  888. if(count($taxotree2)){
  889. // $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
  890. $default2 = null;
  891. if(isset($values['filter-'.$l.'-2']) && $values['filter-'.$l.'-2']){
  892. $default2 = $values['filter-'.$l.'-2'];
  893. }elseif(isset($args_values[$l][2])){
  894. $default2 = $args_values[$l][2];
  895. }
  896. // dsm($default2, "default 2");
  897. $level_2_ops = array();
  898. foreach ($taxotree2 as $index => $term) {
  899. // TODO: get translated tag name
  900. $level_2_ops[$term->tid] = t($term->name);
  901. }
  902. // form select element for third level
  903. $form['filters']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  904. '#type' => 'select',
  905. '#options' => $level_2_ops,
  906. '#name' => 'filter-'.$ll.'-2',
  907. '#empty_option' => t("Choose a filter"),
  908. // '#empty_values' => 0,
  909. '#default_value' => $default2,
  910. );
  911. // without this line, form api will mess with default values
  912. $form_state["input"]['filter-'.$ll.'-2'] = $default2;
  913. }
  914. }
  915. }
  916. // add remove the line button
  917. $form['filters']['filterline-'.$ll]['rm-filter-'.$ll] = array(
  918. '#type' => 'image_button',
  919. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/rmfilter.png',
  920. '#title' => t('Remove this filter line'),
  921. '#name' => 'rm-filter-'.$ll,
  922. '#return_value' => $ll,
  923. '#executes_submit_callback' => false,
  924. '#ajax' => array(
  925. 'callback' => 'materio_search_api_advanced_search_rmline_callback',
  926. 'wrapper' => 'advancedsearch-filters',
  927. // 'trigger_as' => array("name"=>"rmfilters".$l),
  928. ),
  929. "#attributes" => array('class'=>array('rm-btn')),
  930. );
  931. } // end of lines loop
  932. if($rmline !== false){
  933. $form_state['filterlines'] = $form_state['filterlines']-1;
  934. }
  935. // button to add a new line of criterias
  936. $form['filters']['addfilters'] = array(
  937. '#type' => 'image_button',
  938. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/addfilter.png',
  939. '#title' => t('Add a filter line'),
  940. '#name' => "addfilters",
  941. '#return_value' => "add",
  942. '#ajax' => array(
  943. 'callback' => 'materio_search_api_advanced_search_addfilter_callback',
  944. 'wrapper' => 'advancedsearch-filters',
  945. ),
  946. '#executes_submit_callback' => false,
  947. '#attributes' => array(
  948. 'class'=>array('add-filter')
  949. ),
  950. );
  951. // buttons form container
  952. $form['rightcol'] = array(
  953. '#type' => 'container',
  954. );
  955. // add a back to search/base home bouton if on explore/foo
  956. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  957. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  958. 'html'=>true,
  959. 'attributes'=>array('class'=>'back-search-home')
  960. ));
  961. $form['rightcol']['actu'] = array(
  962. '#type' => 'markup',
  963. '#markup' => $link,
  964. );
  965. }
  966. // submit the search
  967. $form['rightcol']['search'] = array(
  968. '#type' => 'image_button',
  969. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  970. '#title' => t('Search'),
  971. '#name' => "search",
  972. '#return_value' => "search",
  973. );
  974. }else{
  975. $form['novoc'] = array(
  976. '#markup' => "Please choose a vocabulary to search from in materio search api settings"
  977. );
  978. }
  979. // dsm($form, 'form');
  980. return $form;
  981. }
  982. function msa_get_taxo_tree($vid, $parent, $depth){
  983. $query = new EntityFieldQuery();
  984. $query->entityCondition('entity_type', 'taxonomy_term')
  985. ->propertyCondition('vid', $vid);
  986. // ->propertyCondition('parent', $parent);
  987. // ->propertyCondition('status', 1);
  988. // ->entityCondition('bundle', $voc_machinename)
  989. $result = $query->execute();
  990. if (isset($result['taxonomy_term'])) {
  991. $terms = $result['taxonomy_term'];
  992. // At first we need to get field's id. If you already know field id, you can ommit this step
  993. // Get all fields attached to a given node type
  994. $fields = field_info_instances('taxonomy_term');
  995. // Get id of body field
  996. // $field_id = $fields['name']['field_id'];
  997. // Attach a field of selected id only to get value for it
  998. field_attach_load('taxonomy_term', $terms, FIELD_LOAD_CURRENT, array('field_id' => 'name'));
  999. // Get values of our node field
  1000. // $output = field_get_items('node', $stories, 'field_story_image');
  1001. }
  1002. return $terms;
  1003. }
  1004. function materio_search_api_advanced_search_select_callback($form, &$form_state) {
  1005. return $form['filters'];
  1006. }
  1007. function materio_search_api_advanced_search_addfilter_callback($form, &$form_state) {
  1008. return $form['filters'];
  1009. }
  1010. function materio_search_api_advanced_search_rmline_callback($form, &$form_state) {
  1011. return $form['filters'];
  1012. }
  1013. function materio_search_api_advanced_search_form_validate($form, &$form_state){
  1014. if($form_state['triggering_element']['#name'] == 'search'){
  1015. // dsm($form, '$form');
  1016. // dsm($form_state, 'form_state');
  1017. if (!isset($form_state['values']['filter-0-1']) || $form_state['values']['filter-0-1'] == '') {
  1018. form_set_error('filters', 'Please choose at least 1 keyword.');
  1019. }
  1020. }
  1021. }
  1022. function materio_search_api_advanced_search_form_submit($form, &$form_state){
  1023. // dsm($form_state, 'form_state');
  1024. $values = $form_state['values'];
  1025. $filterlines = isset($form_state['filterlines']) ? $form_state['filterlines'] : 0;
  1026. // $keywords = array();
  1027. // extract last tag of each lines
  1028. for ($l=0; $l < $filterlines ; $l++) {
  1029. for ($i=2; $i > 0 ; $i--) {
  1030. if(isset($values['filter-'.$l.'-'.$i]) && $values['filter-'.$l.'-'.$i]){
  1031. // $term = taxonomy_term_load($values['filter-'.$l.'-'.$i]);
  1032. // $keywords[$term->tid] = '"'.$term->name.'"';
  1033. // $tids[] = '"'.$values['filter-'.$l.'-'.$i].'"';
  1034. $tids[] = $values['filter-'.$l.'-'.$i];
  1035. continue 2;
  1036. }else{
  1037. continue;
  1038. }
  1039. }
  1040. }
  1041. // dsm($keywords, 'keywordstid');
  1042. $keywords_str = implode("+", $tids);
  1043. // dsm($keywords_str);
  1044. $form_state['redirect'] = 'explore/filters/'.$keywords_str; //rawurlencode($keywords_str);
  1045. }
  1046. /**
  1047. * viewmode
  1048. */
  1049. function _materio_search_api_change_viewmode($vm){
  1050. // dsm($vm, '_materio_search_api_change_viewmode');
  1051. global $user;
  1052. // dsm($user, 'user');
  1053. $entity_infos = entity_get_info();
  1054. // dsm($entity_infos, 'entity_infos');
  1055. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  1056. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  1057. $rep = array('statut'=>'saved');
  1058. }else{
  1059. $rep = array('statut'=>'viewmode not allowed');
  1060. }
  1061. return $rep;
  1062. }
  1063. /**
  1064. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  1065. */
  1066. /**
  1067. * Implements hook_theme().
  1068. */
  1069. function materio_search_api_theme($existing, $type, $theme, $path) {
  1070. return array(
  1071. 'materio_search_api_search_block' => array(
  1072. 'arguments' => array(),
  1073. 'template' => 'materio-search-api-search-block',
  1074. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1075. ),
  1076. 'materio_search_api_advanced_search_block' => array(
  1077. 'arguments' => array(),
  1078. 'template' => 'materio-search-api-advanced-search-block',
  1079. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1080. ),
  1081. 'materio_search_api_double_search_block' => array(
  1082. 'arguments' => array(),
  1083. 'template' => 'materio-search-api-double-search-block',
  1084. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1085. ),
  1086. 'materio_search_api_select_viewmode_block' => array(
  1087. 'arguments' => array(),
  1088. 'template' => 'materio-search-api-select-viewmode-block',
  1089. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1090. ),
  1091. // 'materio_search_api_filters_block' => array(
  1092. // 'arguments' => array(),
  1093. // 'template' => 'materio-search-api-filters-block',
  1094. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1095. // ),
  1096. 'materio_search_api_results' => array(
  1097. 'arguments' => array(),
  1098. 'template' => 'materio-search-api-results',
  1099. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1100. 'variables' => array(
  1101. 'index' => NULL,
  1102. 'results' => array('result count' => 0),
  1103. 'items' => array(),
  1104. 'view_mode' => 'teaser',
  1105. 'keys' => '',
  1106. // 'page_machine_name' => NULL,
  1107. 'spellcheck' => NULL,
  1108. 'pager' => NULL,
  1109. ),
  1110. ),
  1111. // 'materio_search_api_performance' => array(
  1112. // 'render element' => 'element',
  1113. // ),
  1114. 'materio_search_api_actuality' => array(
  1115. 'template' => 'materio-search-api-actuality',
  1116. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1117. 'arguments' => array(
  1118. 'items' => array(),
  1119. 'view_mode' => "teaser",
  1120. 'pager' => NULL,
  1121. 'count' => 0,
  1122. )
  1123. ),
  1124. 'materio_search_api_form_element' => array(
  1125. 'render element' => 'element',
  1126. ),
  1127. );
  1128. }
  1129. /**
  1130. * Implements theme for rendering search-performance
  1131. */
  1132. // function theme_materio_search_api_performance($variables) {
  1133. // $element = array_shift($variables);
  1134. // return $element['#markup'];
  1135. // }
  1136. /**
  1137. * template_preprocess_materiobase_advanced_search_block();
  1138. */
  1139. function template_preprocess_materio_search_api_search_block(&$vars){
  1140. // dsm($vars, '$vars');
  1141. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1142. }
  1143. /**
  1144. * template_preprocess_materiobase_advanced_search_block();
  1145. */
  1146. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  1147. // dsm($vars, '$vars');
  1148. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1149. }
  1150. /**
  1151. * template_preprocess_materiobase_search_block();
  1152. */
  1153. function template_preprocess_materio_search_api_double_search_block(&$vars){
  1154. // dsm($vars, '$vars');
  1155. $vars['search_title'] = t("Text search");
  1156. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1157. $vars['advanced_search_title'] = t("Filtered search");
  1158. $vars['advanced_searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1159. }
  1160. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  1161. global $user;
  1162. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  1163. $availableviewmodes = variable_get('availableviewmodes', -1);
  1164. // dsm($availableviewmodes);
  1165. $entity_infos = entity_get_info();
  1166. // dsm($entity_infos, 'entity_infos');
  1167. $content = '<div class="btn-group btn-group-vertical">';
  1168. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  1169. if(in_array($viewmode, $availableviewmodes)){
  1170. $link = l(
  1171. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  1172. 'materiosearchapi/viewmode/change/'.$viewmode,
  1173. array(
  1174. 'query' => drupal_get_destination(),
  1175. 'html' => true,
  1176. 'attributes' => array(
  1177. 'class' => array(
  1178. 'viewmode-link',
  1179. 'viewmode-'.$viewmode,
  1180. $active == $viewmode ? " active" : ''
  1181. ),
  1182. 'rel' => $viewmode
  1183. )
  1184. )
  1185. );
  1186. $content .= $link;
  1187. }
  1188. }
  1189. $content .= '</div>';
  1190. $vars['content'] = $content;
  1191. }
  1192. // function template_preprocess_materio_search_api_filters_block(&$vars){
  1193. // $index_machine_name = variable_get('mainsearchindex', -1);
  1194. // $index = search_api_index_load($index_machine_name);
  1195. // dsm($index, 'index');
  1196. // // $entity_infos = entity_get_info($index->item_type);
  1197. // // dsm($entity_infos, 'entity_infos');
  1198. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  1199. // dsm($indexed_bundles, 'indexed_bundles');
  1200. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  1201. // }
  1202. // function materio_search_api_filters_form($form, $form_state, $bundles){
  1203. // $form = array();
  1204. // dsm($bundles, 'bundles');
  1205. // foreach ($bundles as $bundle) {
  1206. // $form[$bundle . '_filter'] = array(
  1207. // '#type'=>'checkbox',
  1208. // '#default_value' => -1,
  1209. // '#title' => $bundle,
  1210. // );
  1211. // }
  1212. // return $form;
  1213. // }
  1214. /**
  1215. * Function for preprocessing the variables for the search_api_page_results
  1216. * template.
  1217. *
  1218. * @param array $variables
  1219. * An associative array containing:
  1220. * - $index: The index this search was executed on.
  1221. * - $results: An array of search results, as returned by
  1222. * SearchApiQueryInterface::execute().
  1223. * - $keys: The keywords of the executed search.
  1224. *
  1225. * @see materio-search-api-results.tpl.php
  1226. */
  1227. function template_preprocess_materio_search_api_results(array &$vars) {
  1228. // dsm($vars, 'vars');
  1229. $results = $vars['results'];
  1230. if(isset($results['result count'])){
  1231. $vars['result_count'] = $results['result count'];
  1232. $vars['sec'] = round($results['performance']['complete'], 3);
  1233. $vars['keywords'] = $vars['keys'];
  1234. if(isset($results['breves count'])){
  1235. // for free users
  1236. // dsm("free users");
  1237. $vars['perfascard'] = true;
  1238. $perfoutput = format_plural(
  1239. $results['breves count'],
  1240. 'The search found 1 news ',
  1241. 'The search found @count news '
  1242. );
  1243. $perfoutput .= format_plural(
  1244. $vars['result_count'] - $results['breves count'],
  1245. 'with 1 associated matter.',
  1246. 'with @count associated matters.'
  1247. );
  1248. $perfoutput .= '<br />';
  1249. $perfoutput .= format_plural(
  1250. $results['could results']['result count'],
  1251. ' You could have found 1 result with a ',
  1252. ' You could have found <strong>@count results</strong> with a '
  1253. );
  1254. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1255. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1256. $vars['search_performance'] = $perfoutput;
  1257. }else{
  1258. // for members
  1259. $vars['perfascard'] = false;
  1260. // $vars['search_performance'] = format_plural(
  1261. // // $results['result count'],
  1262. // $vars['result_count'],
  1263. // 'The search found 1 result ',
  1264. // 'The search found @count results '
  1265. // )
  1266. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  1267. "@count"=>$vars['result_count'],
  1268. "@keys"=>$vars['keywords'])
  1269. );
  1270. // TODO indicate how many materials and how many news
  1271. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  1272. // "@count"=>$vars['result_count'],
  1273. // "@keys"=>$vars['keywords'],
  1274. // "@mat"=>4,
  1275. // "@new"=>7)
  1276. // );
  1277. }
  1278. }else{
  1279. //for anonymous
  1280. $vars['perfascard'] = true;
  1281. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  1282. $perfoutput .= format_plural(
  1283. $results['could results']['result count'],
  1284. ' You could have found 1 result with a ',
  1285. ' You could have found <strong>@count results</strong> with a '
  1286. );
  1287. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1288. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1289. $vars['search_performance'] = $perfoutput;
  1290. }
  1291. //dsm($vars, '$vars');
  1292. }
  1293. function template_preprocess_materio_search_api_actuality(&$vars){
  1294. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  1295. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  1296. $vars['actualities_infos'] = t('');
  1297. }
  1298. function theme_materio_search_api_form_element($variables) {
  1299. // dsm($variables, 'variables');
  1300. $element = &$variables['element'];
  1301. // This function is invoked as theme wrapper, but the rendered form element
  1302. // may not necessarily have been processed by form_builder().
  1303. $element += array(
  1304. '#title_display' => 'before',
  1305. );
  1306. // Add element #id for #type 'item'.
  1307. if (isset($element['#markup']) && !empty($element['#id'])) {
  1308. $attributes['id'] = $element['#id'];
  1309. }
  1310. // Add element's #type and #name as class to aid with JS/CSS selectors.
  1311. $attributes['class'] = array('form-item');
  1312. if (!empty($element['#type'])) {
  1313. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  1314. }
  1315. if (!empty($element['#name'])) {
  1316. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  1317. }
  1318. // Add a class for disabled elements to facilitate cross-browser styling.
  1319. if (!empty($element['#attributes']['disabled'])) {
  1320. $attributes['class'][] = 'form-disabled';
  1321. }
  1322. # adding element class to wrapper
  1323. foreach ($element['#attributes']['class'] as $value) {
  1324. $attributes['class'][] = $value;
  1325. }
  1326. foreach ($element['#attributes'] as $attr => $value) {
  1327. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  1328. $attributes[$attr] = $value;
  1329. }
  1330. }
  1331. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  1332. // If #title is not set, we don't display any label or required marker.
  1333. if (!isset($element['#title'])) {
  1334. $element['#title_display'] = 'none';
  1335. }
  1336. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  1337. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  1338. switch ($element['#title_display']) {
  1339. case 'before':
  1340. case 'invisible':
  1341. $output .= ' ' . theme('form_element_label', $variables);
  1342. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1343. break;
  1344. case 'after':
  1345. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  1346. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  1347. break;
  1348. case 'none':
  1349. case 'attribute':
  1350. // Output no label and no required marker, only the children.
  1351. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1352. break;
  1353. }
  1354. if (!empty($element['#description'])) {
  1355. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  1356. }
  1357. $output .= "</div>\n";
  1358. return $output;
  1359. }
  1360. /**
  1361. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  1362. */
  1363. /**
  1364. * Implements hook_block_view_alter().
  1365. */
  1366. function materio_search_api_block_view_alter(&$data, $block) {
  1367. // this alter prepopulate the search form provide by search_api_page
  1368. if ($block->module == 'search_api_page') {
  1369. $page = search_api_page_load($block->delta);
  1370. $item = menu_get_item();
  1371. if (isset($page->path) && $page->path == $item['path']) {
  1372. $keys = arg(count(arg(NULL, $page->path)));
  1373. if ($keys) {
  1374. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  1375. $data['content']['keys_' . $page->id]['#value'] = $keys;
  1376. }
  1377. }
  1378. }
  1379. }