materio_search_api.module 33 KB

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