materio_search_api.module 34 KB

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