materio_search_api.module 45 KB

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