materio_search_api.module 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. <?php
  2. /**
  3. * @file
  4. * This is the file description for Materiobasemod module.
  5. *
  6. * In this more verbose, multi-line description, you can specify what this
  7. * file does exactly. Make sure to wrap your documentation in column 78 so
  8. * that the file can be displayed nicely in default-sized consoles.
  9. */
  10. // define(MATERIO_SEARCH_API_RESULTS_PATH, 'explore');
  11. /**
  12. * Implements hook_permission().
  13. */
  14. function materio_search_api_permission() {
  15. return array(
  16. 'use materio search api' => array(
  17. 'title' => t('Use materio search api'),
  18. 'description' => t('Use materio search api.'),
  19. ),
  20. 'use materio search api for breves' => array(
  21. 'title' => t('Use materio search api for breves'),
  22. 'description' => t('Use materio search api for breves.'),
  23. ),
  24. 'use materio search api autocomplete' => array(
  25. 'title' => t('Use materio search api autocomplete'),
  26. 'description' => t('Use materio search api autocomplete.'),
  27. ),
  28. 'use materio search api viewmode selection' => array(
  29. 'title' => t('Use materio search api viewmode selection'),
  30. 'description' => t('Use materio search api viewmode selection.'),
  31. ),
  32. 'administer materio_search_api' => array(
  33. 'title' => t('administer Materio search api'),
  34. 'description' => t('Administer materio search api.'),
  35. ),
  36. 'use materio search api filters' => array(
  37. 'title' => t('use Materio search api filters'),
  38. 'description' => t('Use materio search api filters.'),
  39. ),
  40. );
  41. }
  42. /**
  43. * Implements hook_menu().
  44. */
  45. function materio_search_api_menu() {
  46. $items = array();
  47. $base = array(
  48. 'type' => MENU_CALLBACK,
  49. 'file' => 'materio_search_api.pages.inc',
  50. );
  51. $items['admin/config/search/search_api/materiosearchapi'] = array(
  52. 'title' => 'Materio Search Api',
  53. 'page callback' => 'drupal_get_form',
  54. 'page arguments' => array('materio_search_api_settings'),
  55. 'access arguments' => array('administer materio_search_api'),
  56. 'file' => 'materio_search_api.admin.inc',
  57. 'type' => MENU_LOCAL_TASK,
  58. );
  59. $items['materiosearchapi/autocomplete/dbselect'] = $base+array(
  60. 'access arguments' => array('use materio search api autocomplete'),
  61. 'page callback' => 'materio_search_api_autocomplete_dbselect',
  62. );
  63. $items['materiosearchapi/autocomplete/searchapi'] = $base+array(
  64. 'access arguments' => array('use materio search api autocomplete'),
  65. 'page callback' => 'materio_search_api_autocomplete_searchapi',
  66. );
  67. $items['explore'] = $base+array(
  68. // 'access arguments' => array('use materio search api'),
  69. 'access callback' => 'materio_search_api_access_search',
  70. 'page callback' => 'materio_search_api_results_search',
  71. 'title' => t('Explore'),
  72. //'page argument' => array(1,2,3),
  73. );
  74. $items['actuality'] = $base+array(
  75. // 'access arguments' => array(),
  76. 'page callback' => 'materio_search_api_actuality',
  77. // 'page argument' => array(),
  78. 'access callback' => TRUE,
  79. );
  80. $items['materiosearchapi/viewmode/change'] = $base+array(
  81. 'access arguments' => array('use materio search api viewmode selection'),
  82. 'page callback' => 'materio_search_api_viewmode_change',
  83. 'page argument' => array(3),
  84. );
  85. return $items;
  86. }
  87. function materio_search_api_access_search(){
  88. return true;
  89. // return user_access('use materio search api for breves') || user_access('use materio search api');
  90. }
  91. /**
  92. * - - - - - - - - - - - - SOLR - - - - - - - - - - - -
  93. */
  94. /**
  95. * Implements hook_search_api_data_type_info().
  96. *
  97. * Declare our new type to Search API so it can be selected for a field.
  98. */
  99. function materio_search_api_search_api_data_type_info() {
  100. return array(
  101. 'edge_n2_kw_text' => array(
  102. 'name' => t('Fulltext (partial matching)'),
  103. 'fallback' => 'text',
  104. 'prefix' => 'tem',
  105. 'always multiValued' => TRUE,
  106. ),
  107. 'edge_n2_kw_mapped_text' => array(
  108. 'name' => t('Fulltext without accents (partial matching)'),
  109. 'fallback' => 'text',
  110. 'prefix' => 'temmap',
  111. 'always multiValued' => TRUE,
  112. ),
  113. // 'term_tid' => array(
  114. // 'name' => t('Taxonomy term tid'),
  115. // 'fallback' => 'integer',
  116. // 'prefix' => 'im',
  117. // 'always multiValued' => TRUE,
  118. // ),
  119. );
  120. }
  121. /**
  122. * Implements hook_search_api_solr_dynamic_field_info().
  123. *
  124. * Tell Search API Solr how to index our new data type.
  125. */
  126. function materio_search_api_search_api_solr_dynamic_field_info() {
  127. return array(
  128. 'edge_n2_kw_text' => array(
  129. 'prefix' => 'tem',
  130. 'always multiValued' => TRUE,
  131. ),
  132. 'edge_n2_kw_mapped_text' => array(
  133. 'prefix' => 'temmap',
  134. 'always multiValued' => TRUE,
  135. ),
  136. // 'term_tid' => array(
  137. // 'prefix' => 'im',
  138. // 'always multiValued' => TRUE,
  139. // ),
  140. );
  141. }
  142. /**
  143. * hook_entity_property_info_alter().
  144. *
  145. * define own fields
  146. * - main taxonomy terms, the purpose is to be able to attribute differente boost on serach api depending on term order
  147. * - companies infos (address) : retrieve for materiau content type the companie info from the tode node
  148. *
  149. */
  150. function materio_search_api_entity_property_info_alter(&$info){
  151. // dsm($info, 'hook_entity_property_info_alter | info');
  152. // watchdog('materio solr', 'materio_search_api_entity_property_info_alter', array());
  153. $node_props = &$info['node']['properties'];
  154. for ($i=1; $i <= 5 ; $i++) {
  155. $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array(
  156. 'type'=>'text',
  157. 'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  158. 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
  159. );
  160. $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array(
  161. 'type'=>'text',
  162. 'label'=> t('Main onthologie term '.$i. ' as tid'),
  163. 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid',
  164. );
  165. }
  166. $node_props['materio_search_api_onthologie_term_others_text'] = array(
  167. 'type'=>'list<text>',
  168. 'label'=> t('Others onthologie terms as text (+ synonyms)'),
  169. 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
  170. );
  171. $node_props['materio_search_api_onthologie_term_others_tid'] = array(
  172. 'type'=>'list<text>',
  173. 'label'=> t('Others onthologie terms as tid'),
  174. 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid',
  175. );
  176. $node_props['materio_search_api_taglibres_text'] = array(
  177. 'type'=>'list<text>',
  178. 'label'=> t('Tag libres terms as text (+ synonyms)'),
  179. 'getter callback'=>'materio_search_api_get_taglibres_terms_text',
  180. );
  181. $node_materiau_props = &$info['node']['bundles']['materiau']['properties'];
  182. $node_materiau_props['materio_search_api_node_propertie_companie'] = array(
  183. 'type'=>'list<text>',
  184. 'label'=> t('Manufacturers and distributors companies localised (dont addresse)'),
  185. 'getter callback'=>'materio_search_api_get_node_propertie_companie',
  186. );
  187. $term_props = &$info['taxonomy_term']['properties'];
  188. $term_props['materio_search_api_term_property_dup_name'] = array(
  189. 'label' => t("Term name field dup"),
  190. 'description' => t("Duplicate the term name field to apply different filter in solr (remove accents for instance)"),
  191. 'type' => 'text',
  192. 'getter callback' => 'materio_search_api_term_property_dup_name',
  193. );
  194. }
  195. /**
  196. * taxonomy propoerties as text + synonymes
  197. */
  198. function materio_search_api_get_onthologie_term_1_text($item){
  199. // dsm($item, 'item');
  200. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 0);
  201. }
  202. function materio_search_api_get_onthologie_term_2_text($item){
  203. // dsm($item, 'item');
  204. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 1);
  205. }
  206. function materio_search_api_get_onthologie_term_3_text($item){
  207. // dsm($item, 'item');
  208. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 2);
  209. }
  210. function materio_search_api_get_onthologie_term_4_text($item){
  211. // dsm($item, 'item');
  212. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 3);
  213. }
  214. function materio_search_api_get_onthologie_term_5_text($item){
  215. // dsm($item, 'item');
  216. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 4);
  217. }
  218. function materio_search_api_get_taxonomy_terms_others_text($item){
  219. // dsm($item, 'item');
  220. $delta = 5;
  221. $terms = array();
  222. while( isset($item->field_onthologie['und'][$delta]) ){
  223. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_onthologie", $delta);
  224. $delta++;
  225. }
  226. return $terms;
  227. }
  228. function materio_search_api_get_taglibres_terms_text($item){
  229. // dsm($item, 'item');
  230. $delta = 0;
  231. $terms = array();
  232. while( isset($item->field_tags_libres['und'][$delta]) ){
  233. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_tags_libres", $delta);
  234. $delta++;
  235. }
  236. return $terms;
  237. }
  238. function materio_search_api_get_taxo_term_field_text($item, $field_name, $delta){
  239. // dsm($item, 'item');
  240. // dsm($delta, 'delta');
  241. if(isset($item->{$field_name}['und'][$delta])){
  242. // print '** item **'."\n";
  243. // print_r($item);
  244. $term = taxonomy_term_load($item->{$field_name}['und'][$delta]['tid']);
  245. // use entity metadata wrappers as they are SMART
  246. $wrapper = entity_metadata_wrapper('taxonomy_term', $term);
  247. // print '** wrapper **'."\n";
  248. // print_r($wrapper);
  249. // print "\n";
  250. $keywords[] = $wrapper->language($item->language)->name_field->raw();
  251. // print '** term_name **'."\n";
  252. // print_r($term->name);
  253. // print "\n";
  254. // print_r($term_name);
  255. // print "\n";
  256. $synonymes = array();
  257. foreach ($wrapper->language($item->language)->synonyms_synonym->value() as $index => $synonym) {
  258. // print '** synonym **'."\n";
  259. // print_r($synonym);
  260. // print "\n";
  261. $keywords[] = $synonym;
  262. }
  263. return implode(" ", $keywords);
  264. }
  265. return null;
  266. }
  267. /**
  268. * taxonomy properties as tid
  269. */
  270. function materio_search_api_get_onthologie_term_1_tid($item){
  271. // dsm($item, 'item');
  272. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 0);
  273. }
  274. function materio_search_api_get_onthologie_term_2_tid($item){
  275. // dsm($item, 'item');
  276. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 1);
  277. }
  278. function materio_search_api_get_onthologie_term_3_tid($item){
  279. // dsm($item, 'item');
  280. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 2);
  281. }
  282. function materio_search_api_get_onthologie_term_4_tid($item){
  283. // dsm($item, 'item');
  284. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 3);
  285. }
  286. function materio_search_api_get_onthologie_term_5_tid($item){
  287. // dsm($item, 'item');
  288. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 4);
  289. }
  290. function materio_search_api_get_taxonomy_terms_others_tid($item){
  291. // dsm($item, 'item');
  292. $delta = 5;
  293. $terms = array();
  294. while( isset($item->field_onthologie['und'][$delta]) ){
  295. $terms[] = materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", $delta);
  296. $delta++;
  297. }
  298. return $terms;
  299. }
  300. function materio_search_api_get_taxo_term_field_tid($item, $field_name, $delta){
  301. // dsm($item, 'item');
  302. // dsm($delta, 'delta');
  303. $tid = null;
  304. if(isset($item->{$field_name}['und'][$delta]))
  305. $tid = $item->{$field_name}['und'][$delta]['tid'];
  306. // print 'tid = '.$tid."\n";
  307. return $tid;
  308. }
  309. /**
  310. *
  311. */
  312. function materio_search_api_get_node_propertie_companie($item){
  313. // dsm($item, '$item');
  314. $lang = "fr";//$item->language;
  315. $languages = language_list();
  316. global $language;
  317. $language = $languages[$lang];
  318. $cies = array();
  319. $cie_fields = array("field_company_fab", "field_company_distrib");
  320. foreach ($cie_fields as $cie_field_name) {
  321. $delta = 0;
  322. while( isset($item->{$cie_field_name}['und'][$delta]) ){
  323. $tid = $item->{$cie_field_name}['und'][$delta]['tid'];
  324. $cie_term = taxonomy_term_load($tid);
  325. if($cie_node = company_get_tode_node($cie_term)){
  326. $cie_node->language = $lang;
  327. $cie_view = node_view($cie_node, "full", $lang);
  328. $cie_rendered = rip_tags(drupal_render($cie_view));
  329. // dsm($cie_rendered, 'cie_rendered');
  330. $cies[] = $cie_rendered;
  331. }
  332. $delta++;
  333. }
  334. }
  335. return $cies;
  336. }
  337. function company_get_tode_node($term){
  338. if(module_exists('tode'))
  339. if( $entitys = tode_get_nids_from_term($term))
  340. if(isset($entitys['node']))
  341. foreach ($entitys['node'] as $nid => $n)
  342. return node_load($nid);
  343. return false;
  344. }
  345. function rip_tags($string) {
  346. // ----- remove HTML TAGs -----
  347. $string = preg_replace ('/<[^>]*>/', ' ', $string);
  348. // ----- remove control characters -----
  349. $string = str_replace("\r", '', $string); // --- replace with empty space
  350. $string = str_replace("\n", ' ', $string); // --- replace with space
  351. $string = str_replace("\t", ' ', $string); // --- replace with space
  352. // $string = str_replace("&nbsp;", ' ', $string); // --- replace with space
  353. // $string = str_replace("&#039;", '\'', $string); // --- replace with space
  354. // ----- remove multiple spaces -----
  355. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  356. // ----- remove html entities
  357. preg_match_all('/&[^;]+;/', $string, $entities);
  358. foreach ($entities[0] as $entity) {
  359. $string = str_replace($entity, mb_convert_encoding($entity, 'UTF-8', 'HTML-ENTITIES'), $string);
  360. }
  361. return $string;
  362. }
  363. function materio_search_api_term_property_dup_name($term){
  364. $lang = $term->language;
  365. return $term->name_field[$lang][0]['value'];
  366. }
  367. /**
  368. * - - -- - - - - - - blocks - - - - - - - - - - -
  369. *
  370. */
  371. /**
  372. * Implements hook_block_info().
  373. */
  374. function materio_search_api_block_info() {
  375. $blocks['materio_search_api_search'] = array(
  376. 'info' => t('Materio search api search'),
  377. 'cache' => DRUPAL_NO_CACHE
  378. );
  379. # NOT USED
  380. $blocks['materio_search_api_viewmode'] = array(
  381. 'info' => t('Materio search api view mode selection'),
  382. 'cache' => DRUPAL_NO_CACHE
  383. );
  384. // $blocks['materio_search_api_filters'] = array(
  385. // 'info' => t('Materio search api filters'),
  386. // 'cache' => DRUPAL_NO_CACHE
  387. // );
  388. return $blocks;
  389. }
  390. /**
  391. * Implements hook_block_view().
  392. */
  393. function materio_search_api_block_view($delta = '') {
  394. // This example comes from node.module. Note that you can also return a
  395. // renderable array rather than rendered HTML for 'content'.
  396. $block = array();
  397. switch ($delta) {
  398. case 'materio_search_api_search':
  399. if (user_access('use materio search api') || user_access('use materio search api for breves')) {
  400. $block['subject'] = t('Search');
  401. $block['content'] = theme('materio_search_api_search_block', array());
  402. }
  403. break;
  404. case 'materio_search_api_viewmode':
  405. // restrict display on node page for materiau and breve
  406. $block_is_visible = true;
  407. $q = parse_url($_GET['q']);
  408. $parts = explode('/', $q['path']);
  409. if($parts[0] == "node"){
  410. $node = node_load($parts[1]);
  411. $block_is_visible = in_array($node->type, array('materiau','breve')) ? true : false;
  412. }
  413. if (user_access('use materio search api viewmode selection') && $block_is_visible) {
  414. $block['subject'] = t('View mode');
  415. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  416. }
  417. break;
  418. // case 'materio_search_api_filters':
  419. // if (user_access('use materio search api filters')) {
  420. // $block['subject'] = t('Filters');
  421. // $block['content'] = theme('materio_search_api_filters_block', array());
  422. // }
  423. // break;
  424. }
  425. return $block;
  426. }
  427. /**
  428. * Implements hook_entity_info_alter().
  429. */
  430. function materio_search_api_entity_info_alter(&$entity_info) {
  431. $entity_info['node']['view modes']['cardsmall'] = array(
  432. 'label' => t('Small cards'),
  433. 'custom settings' => TRUE,
  434. );
  435. $entity_info['node']['view modes']['cardmedium'] = array(
  436. 'label' => t('Medium cards'),
  437. 'custom settings' => TRUE,
  438. );
  439. $entity_info['node']['view modes']['cardbig'] = array(
  440. 'label' => t('Big cards'),
  441. 'custom settings' => TRUE,
  442. );
  443. $entity_info['node']['view modes']['cardfull'] = array(
  444. 'label' => t('Full cards'),
  445. 'custom settings' => TRUE,
  446. );
  447. }
  448. /**
  449. * Implements hook_node_view_alter().
  450. */
  451. function materio_search_api_node_view_alter(&$build) {
  452. $node = $build['#node'];
  453. if (arg(0) == 'node' && arg(1) == $node->nid) {
  454. // dsm($build, 'build');
  455. global $user;
  456. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  457. // dsm($viewmode, 'viewmode');
  458. $node = $build['#node'];
  459. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  460. $build = node_view($node, $viewmode);
  461. }
  462. }
  463. }
  464. /**
  465. * Implements hook_node_view().
  466. */
  467. function materio_search_api_node_view($node, $view_mode, $langcode) {
  468. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  469. # PRINT 7.1
  470. print_node_view($node, 'full');
  471. print_pdf_node_view($node, 'full');
  472. # PRINT 7.2-beta
  473. //print_ui_node_view($node, 'full');
  474. // dsm($node, 'node');
  475. }
  476. }
  477. /**
  478. * materiobase_search_form()
  479. */
  480. function materio_search_api_search_form($form, &$form_state){
  481. // dsm($form_state, 'form_state');
  482. // dsm($form, 'form');
  483. global $user;
  484. $form = array();
  485. $args = arg();
  486. $path = array_shift($args);
  487. // dsm($args, 'args');
  488. if($args[0] == "advanced"){
  489. $advanced = true;
  490. array_shift($args);
  491. // dsm($args, 'shifted arsg');
  492. // foreach ($args as $arg) {
  493. // $typed[] = $arg;//(integer)$arg;
  494. // }
  495. $keys = implode(' +', $args);
  496. }else{
  497. $keys = implode('/', $args);
  498. }
  499. if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters
  500. $query = new EntityFieldQuery();
  501. $query->entityCondition('entity_type', 'node')
  502. ->entityCondition('bundle', 'materiau')
  503. ->propertyCondition('status', 1);
  504. $count = $query->count()->execute();
  505. $default_value = t("search among our !fiches cards", array("!fiches"=>$count));
  506. }else{
  507. $default_value = '';
  508. }
  509. $form['searchfield'] = array(
  510. '#type' => 'textfield',
  511. '#default_value' => $path == 'explore' ? $keys : $default_value, // TODO: set the search page path global or a variable in settings
  512. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  513. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  514. '#size' => 30,
  515. '#maxlength' => 1024,
  516. '#attributes' => array("default"=>$default_value),
  517. );
  518. if(user_access('use materio search api filters')){
  519. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  520. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  521. foreach ($indexed_bundles as $bundle) {
  522. $bundles_options[$bundle] = $bundle;
  523. $default_bundles[] = $bundle;
  524. }
  525. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  526. $form['bundles_filter'] = array(
  527. '#type'=>'checkboxes',
  528. '#options' => $bundles_options,
  529. '#default_value' => $user_bundles_filter,
  530. // '#attributes' => array('class'=>array('btn-group')),
  531. );
  532. }
  533. $form['create'] = array(
  534. '#type' => 'image_button',
  535. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  536. '#value' => t('Search'),
  537. );
  538. return $form;
  539. }
  540. function materio_search_api_search_form_validate($form, &$form_state){
  541. // dsm($form, '$form');
  542. // dsm($form_state, '$form_state');
  543. // dsm(strlen($form_state['values']['searchfield']));
  544. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  545. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  546. }
  547. }
  548. function materio_search_api_search_form_submit($form, &$form_state){
  549. // dsm($form_state, 'form_state');
  550. global $user;
  551. if(user_access('use materio search api filters')){
  552. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  553. if($value)
  554. $bundles[] = $bundle;
  555. # if no filter checked we checked them all by default
  556. if(!isset($bundles))
  557. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  558. $bundles[] = $bundle;
  559. }else{
  560. # if user have no access to filters
  561. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  562. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  563. foreach ($indexed_bundles as $bundle) {
  564. $bundles[] = $bundle;
  565. }
  566. }
  567. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  568. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  569. }
  570. /**
  571. * viewmode
  572. */
  573. function _materio_search_api_change_viewmode($vm){
  574. // dsm($vm, '_materio_search_api_change_viewmode');
  575. global $user;
  576. // dsm($user, 'user');
  577. $entity_infos = entity_get_info();
  578. // dsm($entity_infos, 'entity_infos');
  579. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  580. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  581. $rep = array('statut'=>'saved');
  582. }else{
  583. $rep = array('statut'=>'viewmode not allowed');
  584. }
  585. return $rep;
  586. }
  587. /**
  588. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  589. */
  590. /**
  591. * Implements hook_theme().
  592. */
  593. function materio_search_api_theme($existing, $type, $theme, $path) {
  594. return array(
  595. 'materio_search_api_search_block' => array(
  596. 'arguments' => array(),
  597. 'template' => 'materio-search-api-search-block',
  598. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  599. ),
  600. 'materio_search_api_select_viewmode_block' => array(
  601. 'arguments' => array(),
  602. 'template' => 'materio-search-api-select-viewmode-block',
  603. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  604. ),
  605. // 'materio_search_api_filters_block' => array(
  606. // 'arguments' => array(),
  607. // 'template' => 'materio-search-api-filters-block',
  608. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  609. // ),
  610. 'materio_search_api_results' => array(
  611. 'arguments' => array(),
  612. 'template' => 'materio-search-api-results',
  613. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  614. 'variables' => array(
  615. 'index' => NULL,
  616. 'results' => array('result count' => 0),
  617. 'items' => array(),
  618. 'view_mode' => 'teaser',
  619. 'keys' => '',
  620. // 'page_machine_name' => NULL,
  621. 'spellcheck' => NULL,
  622. 'pager' => NULL,
  623. ),
  624. ),
  625. // 'materio_search_api_performance' => array(
  626. // 'render element' => 'element',
  627. // ),
  628. 'materio_search_api_actuality' => array(
  629. 'template' => 'materio-search-api-actuality',
  630. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  631. 'arguments' => array(
  632. 'items' => array(),
  633. 'view_mode' => "teaser",
  634. 'pager' => NULL,
  635. 'count' => 0,
  636. )
  637. ),
  638. 'materio_search_api_advanced_search_block' => array(
  639. 'template' => 'materio-search-api-advanced-search-block',
  640. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  641. 'arguments' => array(),
  642. ),
  643. 'materio_search_api_form_element' => array(
  644. 'render element' => 'element',
  645. ),
  646. );
  647. }
  648. /**
  649. * Implements theme for rendering search-performance
  650. */
  651. // function theme_materio_search_api_performance($variables) {
  652. // $element = array_shift($variables);
  653. // return $element['#markup'];
  654. // }
  655. /**
  656. * template_preprocess_materiobase_search_block();
  657. */
  658. function template_preprocess_materio_search_api_search_block(&$vars){
  659. // dsm($vars, '$vars');
  660. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  661. }
  662. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  663. global $user;
  664. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  665. $availableviewmodes = variable_get('availableviewmodes', -1);
  666. // dsm($availableviewmodes);
  667. $entity_infos = entity_get_info();
  668. // dsm($entity_infos, 'entity_infos');
  669. $content = '<div class="btn-group btn-group-vertical">';
  670. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  671. if(in_array($viewmode, $availableviewmodes)){
  672. $link = l(
  673. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  674. 'materiosearchapi/viewmode/change/'.$viewmode,
  675. array(
  676. 'query' => drupal_get_destination(),
  677. 'html' => true,
  678. 'attributes' => array(
  679. 'class' => array(
  680. 'viewmode-link',
  681. 'viewmode-'.$viewmode,
  682. $active == $viewmode ? " active" : ''
  683. ),
  684. 'rel' => $viewmode
  685. )
  686. )
  687. );
  688. $content .= $link;
  689. }
  690. }
  691. $content .= '</div>';
  692. $vars['content'] = $content;
  693. }
  694. // function template_preprocess_materio_search_api_filters_block(&$vars){
  695. // $index_machine_name = variable_get('mainsearchindex', -1);
  696. // $index = search_api_index_load($index_machine_name);
  697. // dsm($index, 'index');
  698. // // $entity_infos = entity_get_info($index->item_type);
  699. // // dsm($entity_infos, 'entity_infos');
  700. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  701. // dsm($indexed_bundles, 'indexed_bundles');
  702. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  703. // }
  704. // function materio_search_api_filters_form($form, $form_state, $bundles){
  705. // $form = array();
  706. // dsm($bundles, 'bundles');
  707. // foreach ($bundles as $bundle) {
  708. // $form[$bundle . '_filter'] = array(
  709. // '#type'=>'checkbox',
  710. // '#default_value' => -1,
  711. // '#title' => $bundle,
  712. // );
  713. // }
  714. // return $form;
  715. // }
  716. /**
  717. * Function for preprocessing the variables for the search_api_page_results
  718. * template.
  719. *
  720. * @param array $variables
  721. * An associative array containing:
  722. * - $index: The index this search was executed on.
  723. * - $results: An array of search results, as returned by
  724. * SearchApiQueryInterface::execute().
  725. * - $keys: The keywords of the executed search.
  726. *
  727. * @see materio-search-api-results.tpl.php
  728. */
  729. function template_preprocess_materio_search_api_results(array &$vars) {
  730. // dsm($vars, 'vars');
  731. $results = $vars['results'];
  732. if(isset($results['result count'])){
  733. $vars['result_count'] = $results['result count'];
  734. $vars['sec'] = round($results['performance']['complete'], 3);
  735. $vars['keywords'] = $vars['keys'];
  736. if(isset($results['breves count'])){
  737. // for free users
  738. // dsm("free users");
  739. $vars['perfascard'] = true;
  740. $perfoutput = format_plural(
  741. $results['breves count'],
  742. 'The search found 1 news ',
  743. 'The search found @count news '
  744. );
  745. $perfoutput .= format_plural(
  746. $vars['result_count'] - $results['breves count'],
  747. 'with 1 associated matter.',
  748. 'with @count associated matters.'
  749. );
  750. $perfoutput .= '<br />';
  751. $perfoutput .= format_plural(
  752. $results['could results']['result count'],
  753. ' You could have found 1 result with a ',
  754. ' You could have found <strong>@count results</strong> with a '
  755. );
  756. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  757. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  758. $vars['search_performance'] = $perfoutput;
  759. }else{
  760. // for members
  761. $vars['perfascard'] = false;
  762. // $vars['search_performance'] = format_plural(
  763. // // $results['result count'],
  764. // $vars['result_count'],
  765. // 'The search found 1 result ',
  766. // 'The search found @count results '
  767. // )
  768. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  769. "@count"=>$vars['result_count'],
  770. "@keys"=>$vars['keywords'])
  771. );
  772. // TODO indicate how many materials and how many news
  773. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  774. // "@count"=>$vars['result_count'],
  775. // "@keys"=>$vars['keywords'],
  776. // "@mat"=>4,
  777. // "@new"=>7)
  778. // );
  779. }
  780. }else{
  781. //for anonymous
  782. $vars['perfascard'] = true;
  783. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  784. $perfoutput .= format_plural(
  785. $results['could results']['result count'],
  786. ' You could have found 1 result with a ',
  787. ' You could have found <strong>@count results</strong> with a '
  788. );
  789. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  790. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  791. $vars['search_performance'] = $perfoutput;
  792. }
  793. //dsm($vars, '$vars');
  794. }
  795. function template_preprocess_materio_search_api_actuality(&$vars){
  796. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  797. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  798. $vars['actualities_infos'] = t('');
  799. }
  800. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  801. // dsm($vars, "vars");
  802. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  803. }
  804. function theme_materio_search_api_form_element($variables) {
  805. // dsm($variables, 'variables');
  806. $element = &$variables['element'];
  807. // This function is invoked as theme wrapper, but the rendered form element
  808. // may not necessarily have been processed by form_builder().
  809. $element += array(
  810. '#title_display' => 'before',
  811. );
  812. // Add element #id for #type 'item'.
  813. if (isset($element['#markup']) && !empty($element['#id'])) {
  814. $attributes['id'] = $element['#id'];
  815. }
  816. // Add element's #type and #name as class to aid with JS/CSS selectors.
  817. $attributes['class'] = array('form-item');
  818. if (!empty($element['#type'])) {
  819. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  820. }
  821. if (!empty($element['#name'])) {
  822. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  823. }
  824. // Add a class for disabled elements to facilitate cross-browser styling.
  825. if (!empty($element['#attributes']['disabled'])) {
  826. $attributes['class'][] = 'form-disabled';
  827. }
  828. # adding element class to wrapper
  829. foreach ($element['#attributes']['class'] as $value) {
  830. $attributes['class'][] = $value;
  831. }
  832. foreach ($element['#attributes'] as $attr => $value) {
  833. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  834. $attributes[$attr] = $value;
  835. }
  836. }
  837. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  838. // If #title is not set, we don't display any label or required marker.
  839. if (!isset($element['#title'])) {
  840. $element['#title_display'] = 'none';
  841. }
  842. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  843. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  844. switch ($element['#title_display']) {
  845. case 'before':
  846. case 'invisible':
  847. $output .= ' ' . theme('form_element_label', $variables);
  848. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  849. break;
  850. case 'after':
  851. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  852. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  853. break;
  854. case 'none':
  855. case 'attribute':
  856. // Output no label and no required marker, only the children.
  857. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  858. break;
  859. }
  860. if (!empty($element['#description'])) {
  861. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  862. }
  863. $output .= "</div>\n";
  864. return $output;
  865. }
  866. /**
  867. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  868. */
  869. /**
  870. * Implements hook_block_view_alter().
  871. */
  872. function materio_search_api_block_view_alter(&$data, $block) {
  873. // this alter prepopulate the search form provide by search_api_page
  874. if ($block->module == 'search_api_page') {
  875. $page = search_api_page_load($block->delta);
  876. $item = menu_get_item();
  877. if (isset($page->path) && $page->path == $item['path']) {
  878. $keys = arg(count(arg(NULL, $page->path)));
  879. if ($keys) {
  880. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  881. $data['content']['keys_' . $page->id]['#value'] = $keys;
  882. }
  883. }
  884. }
  885. }