materio_search_api.module 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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. * - - -- - - - - - - blocks - - - - - - - - - - -
  402. *
  403. */
  404. /**
  405. * Implements hook_block_info().
  406. */
  407. function materio_search_api_block_info() {
  408. $blocks['materio_search_api_search'] = array(
  409. 'info' => t('Materio search api search'),
  410. 'cache' => DRUPAL_NO_CACHE
  411. );
  412. $blocks['materio_search_api_advanced_search'] = array(
  413. 'info' => t('Materio search api advanced search'),
  414. 'cache' => DRUPAL_NO_CACHE
  415. );
  416. # NOT USED
  417. $blocks['materio_search_api_viewmode'] = array(
  418. 'info' => t('Materio search api view mode selection'),
  419. 'cache' => DRUPAL_NO_CACHE
  420. );
  421. // $blocks['materio_search_api_filters'] = array(
  422. // 'info' => t('Materio search api filters'),
  423. // 'cache' => DRUPAL_NO_CACHE
  424. // );
  425. return $blocks;
  426. }
  427. /**
  428. * Implements hook_block_view().
  429. */
  430. function materio_search_api_block_view($delta = '') {
  431. // This example comes from node.module. Note that you can also return a
  432. // renderable array rather than rendered HTML for 'content'.
  433. $block = array();
  434. switch ($delta) {
  435. case 'materio_search_api_search':
  436. if (user_access('use materio search api') || user_access('use materio search api for breves')) {
  437. $block['subject'] = t('Search');
  438. $block['content'] = theme('materio_search_api_search_block', array());
  439. }
  440. break;
  441. case 'materio_search_api_advanced_search':
  442. if (user_access('use materio advanced search')) {
  443. $block['subject'] = t('Advanced Search');
  444. $block['content'] = theme('materio_search_api_advanced_search_block', array());
  445. }
  446. break;
  447. case 'materio_search_api_viewmode':
  448. // restrict display on node page for materiau and breve
  449. $block_is_visible = true;
  450. $q = parse_url($_GET['q']);
  451. $parts = explode('/', $q['path']);
  452. if($parts[0] == "node"){
  453. $node = node_load($parts[1]);
  454. $block_is_visible = in_array($node->type, array('materiau','breve')) ? true : false;
  455. }
  456. if (user_access('use materio search api viewmode selection') && $block_is_visible) {
  457. $block['subject'] = t('View mode');
  458. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  459. }
  460. break;
  461. // case 'materio_search_api_filters':
  462. // if (user_access('use materio search api filters')) {
  463. // $block['subject'] = t('Filters');
  464. // $block['content'] = theme('materio_search_api_filters_block', array());
  465. // }
  466. // break;
  467. }
  468. return $block;
  469. }
  470. /**
  471. * Implements hook_entity_info_alter().
  472. */
  473. function materio_search_api_entity_info_alter(&$entity_info) {
  474. $entity_info['node']['view modes']['cardsmall'] = array(
  475. 'label' => t('Small cards'),
  476. 'custom settings' => TRUE,
  477. );
  478. $entity_info['node']['view modes']['cardmedium'] = array(
  479. 'label' => t('Medium cards'),
  480. 'custom settings' => TRUE,
  481. );
  482. $entity_info['node']['view modes']['cardbig'] = array(
  483. 'label' => t('Big cards'),
  484. 'custom settings' => TRUE,
  485. );
  486. $entity_info['node']['view modes']['cardfull'] = array(
  487. 'label' => t('Full cards'),
  488. 'custom settings' => TRUE,
  489. );
  490. }
  491. /**
  492. * Implements hook_node_view_alter().
  493. */
  494. function materio_search_api_node_view_alter(&$build) {
  495. $node = $build['#node'];
  496. if (arg(0) == 'node' && arg(1) == $node->nid) {
  497. // dsm($build, 'build');
  498. global $user;
  499. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  500. // dsm($viewmode, 'viewmode');
  501. $node = $build['#node'];
  502. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  503. $build = node_view($node, $viewmode);
  504. }
  505. }
  506. }
  507. /**
  508. * Implements hook_node_view().
  509. */
  510. function materio_search_api_node_view($node, $view_mode, $langcode) {
  511. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  512. # PRINT 7.1
  513. // print_node_view($node, 'full');
  514. // print_pdf_node_view($node, 'full');
  515. # PRINT 7.2-beta
  516. // call for hook_node_view of print module to insert link
  517. print_ui_node_view($node, 'full');
  518. // dsm($node, 'node');
  519. }
  520. }
  521. /**
  522. * materiobase_search_form()
  523. */
  524. function materio_search_api_search_form($form, &$form_state){
  525. // dsm($form_state, 'form_state');
  526. // dsm($form, 'form');
  527. global $user;
  528. $form = array();
  529. $args = arg();
  530. $path = array_shift($args);
  531. // dsm($args, 'args');
  532. if($args[0] == "advanced"){
  533. $advanced = true;
  534. array_shift($args);
  535. // dsm($args, 'shifted arsg');
  536. // foreach ($args as $arg) {
  537. // $typed[] = $arg;//(integer)$arg;
  538. // }
  539. $keys = implode(' +', $args);
  540. }else{
  541. $keys = implode('/', $args);
  542. }
  543. if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters
  544. $query = new EntityFieldQuery();
  545. $query->entityCondition('entity_type', 'node')
  546. ->entityCondition('bundle', 'materiau')
  547. ->propertyCondition('status', 1);
  548. $count = $query->count()->execute();
  549. $default_value = t("search among our !fiches cards", array("!fiches"=>$count));
  550. }else{
  551. $default_value = '';
  552. }
  553. // add a back to search/base home bouton if on explore/foo
  554. if ($path == 'explore' || $path == 'bookmarks' || $path == 'lists' ) {
  555. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  556. 'html'=>true,
  557. 'attributes'=>array('class'=>'back-search-home')
  558. ));
  559. $form['actu'] = array(
  560. '#type' => 'markup',
  561. '#markup' => $link,
  562. //'<a href="'.base_path().'actuality" class="back-search-home"><i class="fi-home"></i></a>'
  563. );
  564. }
  565. $form['searchfield'] = array(
  566. '#type' => 'textfield',
  567. '#default_value' => $path == 'explore' ? $keys : $default_value, // TODO: set the search page path global or a variable in settings
  568. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  569. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  570. '#size' => 30,
  571. '#maxlength' => 1024,
  572. '#attributes' => array("default"=>$default_value),
  573. );
  574. if(user_access('use materio search api filters')){
  575. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  576. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  577. foreach ($indexed_bundles as $bundle) {
  578. $bundles_options[$bundle] = $bundle;
  579. $default_bundles[] = $bundle;
  580. }
  581. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  582. $form['bundles_filter'] = array(
  583. '#type'=>'checkboxes',
  584. '#options' => $bundles_options,
  585. '#default_value' => $user_bundles_filter,
  586. // '#attributes' => array('class'=>array('btn-group')),
  587. );
  588. }
  589. $form['create'] = array(
  590. '#type' => 'image_button',
  591. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  592. '#value' => t('Search'),
  593. );
  594. return $form;
  595. }
  596. function materio_search_api_search_form_validate($form, &$form_state){
  597. // dsm($form, '$form');
  598. // dsm($form_state, '$form_state');
  599. // dsm(strlen($form_state['values']['searchfield']));
  600. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  601. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  602. }
  603. }
  604. function materio_search_api_search_form_submit($form, &$form_state){
  605. // dsm($form_state, 'form_state');
  606. global $user;
  607. if(user_access('use materio search api filters')){
  608. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  609. if($value)
  610. $bundles[] = $bundle;
  611. # if no filter checked we checked them all by default
  612. if(!isset($bundles))
  613. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  614. $bundles[] = $bundle;
  615. }else{
  616. # if user have no access to filters
  617. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  618. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  619. foreach ($indexed_bundles as $bundle) {
  620. $bundles[] = $bundle;
  621. }
  622. }
  623. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  624. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  625. }
  626. /**
  627. * materiobase_search_form()
  628. */
  629. function materio_search_api_advanced_search_form($form, &$form_state){
  630. // dsm($form_state, 'form_state');
  631. // dsm($form, 'form');
  632. global $user;
  633. $form = array();
  634. $args = arg();
  635. $path = array_shift($args);
  636. // add a back to search/base home bouton if on explore/foo
  637. if ($path == 'explore' || $path == 'bookmarks' || $path == 'lists' ) {
  638. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  639. 'html'=>true,
  640. 'attributes'=>array('class'=>'back-search-home')
  641. ));
  642. $form['actu'] = array(
  643. '#type' => 'markup',
  644. '#markup' => $link,
  645. //'<a href="'.base_path().'actuality" class="back-search-home"><i class="fi-home"></i></a>'
  646. );
  647. }
  648. $form['create'] = array(
  649. '#type' => 'image_button',
  650. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  651. '#value' => t('Search'),
  652. );
  653. return $form;
  654. }
  655. /**
  656. * viewmode
  657. */
  658. function _materio_search_api_change_viewmode($vm){
  659. // dsm($vm, '_materio_search_api_change_viewmode');
  660. global $user;
  661. // dsm($user, 'user');
  662. $entity_infos = entity_get_info();
  663. // dsm($entity_infos, 'entity_infos');
  664. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  665. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  666. $rep = array('statut'=>'saved');
  667. }else{
  668. $rep = array('statut'=>'viewmode not allowed');
  669. }
  670. return $rep;
  671. }
  672. /**
  673. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  674. */
  675. /**
  676. * Implements hook_theme().
  677. */
  678. function materio_search_api_theme($existing, $type, $theme, $path) {
  679. return array(
  680. 'materio_search_api_search_block' => array(
  681. 'arguments' => array(),
  682. 'template' => 'materio-search-api-search-block',
  683. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  684. ),
  685. 'materio_search_api_advanced_search_block' => array(
  686. 'arguments' => array(),
  687. 'template' => 'materio-search-api-advanced-search-block',
  688. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  689. ),
  690. 'materio_search_api_select_viewmode_block' => array(
  691. 'arguments' => array(),
  692. 'template' => 'materio-search-api-select-viewmode-block',
  693. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  694. ),
  695. // 'materio_search_api_filters_block' => array(
  696. // 'arguments' => array(),
  697. // 'template' => 'materio-search-api-filters-block',
  698. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  699. // ),
  700. 'materio_search_api_results' => array(
  701. 'arguments' => array(),
  702. 'template' => 'materio-search-api-results',
  703. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  704. 'variables' => array(
  705. 'index' => NULL,
  706. 'results' => array('result count' => 0),
  707. 'items' => array(),
  708. 'view_mode' => 'teaser',
  709. 'keys' => '',
  710. // 'page_machine_name' => NULL,
  711. 'spellcheck' => NULL,
  712. 'pager' => NULL,
  713. ),
  714. ),
  715. // 'materio_search_api_performance' => array(
  716. // 'render element' => 'element',
  717. // ),
  718. 'materio_search_api_actuality' => array(
  719. 'template' => 'materio-search-api-actuality',
  720. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  721. 'arguments' => array(
  722. 'items' => array(),
  723. 'view_mode' => "teaser",
  724. 'pager' => NULL,
  725. 'count' => 0,
  726. )
  727. ),
  728. 'materio_search_api_form_element' => array(
  729. 'render element' => 'element',
  730. ),
  731. );
  732. }
  733. /**
  734. * Implements theme for rendering search-performance
  735. */
  736. // function theme_materio_search_api_performance($variables) {
  737. // $element = array_shift($variables);
  738. // return $element['#markup'];
  739. // }
  740. /**
  741. * template_preprocess_materiobase_search_block();
  742. */
  743. function template_preprocess_materio_search_api_search_block(&$vars){
  744. // dsm($vars, '$vars');
  745. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  746. }
  747. /**
  748. * template_preprocess_materiobase_advanced_search_block();
  749. */
  750. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  751. // dsm($vars, '$vars');
  752. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  753. }
  754. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  755. global $user;
  756. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  757. $availableviewmodes = variable_get('availableviewmodes', -1);
  758. // dsm($availableviewmodes);
  759. $entity_infos = entity_get_info();
  760. // dsm($entity_infos, 'entity_infos');
  761. $content = '<div class="btn-group btn-group-vertical">';
  762. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  763. if(in_array($viewmode, $availableviewmodes)){
  764. $link = l(
  765. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  766. 'materiosearchapi/viewmode/change/'.$viewmode,
  767. array(
  768. 'query' => drupal_get_destination(),
  769. 'html' => true,
  770. 'attributes' => array(
  771. 'class' => array(
  772. 'viewmode-link',
  773. 'viewmode-'.$viewmode,
  774. $active == $viewmode ? " active" : ''
  775. ),
  776. 'rel' => $viewmode
  777. )
  778. )
  779. );
  780. $content .= $link;
  781. }
  782. }
  783. $content .= '</div>';
  784. $vars['content'] = $content;
  785. }
  786. // function template_preprocess_materio_search_api_filters_block(&$vars){
  787. // $index_machine_name = variable_get('mainsearchindex', -1);
  788. // $index = search_api_index_load($index_machine_name);
  789. // dsm($index, 'index');
  790. // // $entity_infos = entity_get_info($index->item_type);
  791. // // dsm($entity_infos, 'entity_infos');
  792. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  793. // dsm($indexed_bundles, 'indexed_bundles');
  794. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  795. // }
  796. // function materio_search_api_filters_form($form, $form_state, $bundles){
  797. // $form = array();
  798. // dsm($bundles, 'bundles');
  799. // foreach ($bundles as $bundle) {
  800. // $form[$bundle . '_filter'] = array(
  801. // '#type'=>'checkbox',
  802. // '#default_value' => -1,
  803. // '#title' => $bundle,
  804. // );
  805. // }
  806. // return $form;
  807. // }
  808. /**
  809. * Function for preprocessing the variables for the search_api_page_results
  810. * template.
  811. *
  812. * @param array $variables
  813. * An associative array containing:
  814. * - $index: The index this search was executed on.
  815. * - $results: An array of search results, as returned by
  816. * SearchApiQueryInterface::execute().
  817. * - $keys: The keywords of the executed search.
  818. *
  819. * @see materio-search-api-results.tpl.php
  820. */
  821. function template_preprocess_materio_search_api_results(array &$vars) {
  822. // dsm($vars, 'vars');
  823. $results = $vars['results'];
  824. if(isset($results['result count'])){
  825. $vars['result_count'] = $results['result count'];
  826. $vars['sec'] = round($results['performance']['complete'], 3);
  827. $vars['keywords'] = $vars['keys'];
  828. if(isset($results['breves count'])){
  829. // for free users
  830. // dsm("free users");
  831. $vars['perfascard'] = true;
  832. $perfoutput = format_plural(
  833. $results['breves count'],
  834. 'The search found 1 news ',
  835. 'The search found @count news '
  836. );
  837. $perfoutput .= format_plural(
  838. $vars['result_count'] - $results['breves count'],
  839. 'with 1 associated matter.',
  840. 'with @count associated matters.'
  841. );
  842. $perfoutput .= '<br />';
  843. $perfoutput .= format_plural(
  844. $results['could results']['result count'],
  845. ' You could have found 1 result with a ',
  846. ' You could have found <strong>@count results</strong> with a '
  847. );
  848. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  849. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  850. $vars['search_performance'] = $perfoutput;
  851. }else{
  852. // for members
  853. $vars['perfascard'] = false;
  854. // $vars['search_performance'] = format_plural(
  855. // // $results['result count'],
  856. // $vars['result_count'],
  857. // 'The search found 1 result ',
  858. // 'The search found @count results '
  859. // )
  860. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  861. "@count"=>$vars['result_count'],
  862. "@keys"=>$vars['keywords'])
  863. );
  864. // TODO indicate how many materials and how many news
  865. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  866. // "@count"=>$vars['result_count'],
  867. // "@keys"=>$vars['keywords'],
  868. // "@mat"=>4,
  869. // "@new"=>7)
  870. // );
  871. }
  872. }else{
  873. //for anonymous
  874. $vars['perfascard'] = true;
  875. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  876. $perfoutput .= format_plural(
  877. $results['could results']['result count'],
  878. ' You could have found 1 result with a ',
  879. ' You could have found <strong>@count results</strong> with a '
  880. );
  881. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  882. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  883. $vars['search_performance'] = $perfoutput;
  884. }
  885. //dsm($vars, '$vars');
  886. }
  887. function template_preprocess_materio_search_api_actuality(&$vars){
  888. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  889. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  890. $vars['actualities_infos'] = t('');
  891. }
  892. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  893. // dsm($vars, "vars");
  894. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  895. }
  896. function theme_materio_search_api_form_element($variables) {
  897. // dsm($variables, 'variables');
  898. $element = &$variables['element'];
  899. // This function is invoked as theme wrapper, but the rendered form element
  900. // may not necessarily have been processed by form_builder().
  901. $element += array(
  902. '#title_display' => 'before',
  903. );
  904. // Add element #id for #type 'item'.
  905. if (isset($element['#markup']) && !empty($element['#id'])) {
  906. $attributes['id'] = $element['#id'];
  907. }
  908. // Add element's #type and #name as class to aid with JS/CSS selectors.
  909. $attributes['class'] = array('form-item');
  910. if (!empty($element['#type'])) {
  911. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  912. }
  913. if (!empty($element['#name'])) {
  914. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  915. }
  916. // Add a class for disabled elements to facilitate cross-browser styling.
  917. if (!empty($element['#attributes']['disabled'])) {
  918. $attributes['class'][] = 'form-disabled';
  919. }
  920. # adding element class to wrapper
  921. foreach ($element['#attributes']['class'] as $value) {
  922. $attributes['class'][] = $value;
  923. }
  924. foreach ($element['#attributes'] as $attr => $value) {
  925. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  926. $attributes[$attr] = $value;
  927. }
  928. }
  929. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  930. // If #title is not set, we don't display any label or required marker.
  931. if (!isset($element['#title'])) {
  932. $element['#title_display'] = 'none';
  933. }
  934. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  935. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  936. switch ($element['#title_display']) {
  937. case 'before':
  938. case 'invisible':
  939. $output .= ' ' . theme('form_element_label', $variables);
  940. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  941. break;
  942. case 'after':
  943. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  944. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  945. break;
  946. case 'none':
  947. case 'attribute':
  948. // Output no label and no required marker, only the children.
  949. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  950. break;
  951. }
  952. if (!empty($element['#description'])) {
  953. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  954. }
  955. $output .= "</div>\n";
  956. return $output;
  957. }
  958. /**
  959. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  960. */
  961. /**
  962. * Implements hook_block_view_alter().
  963. */
  964. function materio_search_api_block_view_alter(&$data, $block) {
  965. // this alter prepopulate the search form provide by search_api_page
  966. if ($block->module == 'search_api_page') {
  967. $page = search_api_page_load($block->delta);
  968. $item = menu_get_item();
  969. if (isset($page->path) && $page->path == $item['path']) {
  970. $keys = arg(count(arg(NULL, $page->path)));
  971. if ($keys) {
  972. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  973. $data['content']['keys_' . $page->id]['#value'] = $keys;
  974. }
  975. }
  976. }
  977. }