materio_search_api.module 48 KB

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