materio_search_api.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * @file
  4. * This is the file description for Materiobasemod module.
  5. *
  6. * In this more verbose, multi-line description, you can specify what this
  7. * file does exactly. Make sure to wrap your documentation in column 78 so
  8. * that the file can be displayed nicely in default-sized consoles.
  9. */
  10. // define(MATERIO_SEARCH_API_RESULTS_PATH, 'explore');
  11. /**
  12. * Implements hook_permission().
  13. */
  14. function materio_search_api_permission() {
  15. return array(
  16. 'use materio search api' => array(
  17. 'title' => t('Use materio search api'),
  18. 'description' => t('Use materio search api.'),
  19. ),
  20. 'use materio search api autocomplete' => array(
  21. 'title' => t('Use materio search api autocomplete'),
  22. 'description' => t('Use materio search api autocomplete.'),
  23. ),
  24. 'use materio search api viewmode selection' => array(
  25. 'title' => t('Use materio search api viewmode selection'),
  26. 'description' => t('Use materio search api viewmode selection.'),
  27. ),
  28. 'administer materio_search_api' => array(
  29. 'title' => t('administer Materio search api'),
  30. 'description' => t('Administer materio search api.'),
  31. ),
  32. 'use materio search api filters' => array(
  33. 'title' => t('use Materio search api filters'),
  34. 'description' => t('Use materio search api filters.'),
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_menu().
  40. */
  41. function materio_search_api_menu() {
  42. $items = array();
  43. $base = array(
  44. 'type' => MENU_CALLBACK,
  45. 'file' => 'materio_search_api.pages.inc',
  46. );
  47. $items['admin/config/search/search_api/materiosearchapi'] = array(
  48. 'title' => 'Materio Search Api',
  49. 'page callback' => 'drupal_get_form',
  50. 'page arguments' => array('materio_search_api_settings'),
  51. 'access arguments' => array('administer materio_search_api'),
  52. 'file' => 'materio_search_api.admin.inc',
  53. 'type' => MENU_LOCAL_TASK,
  54. );
  55. $items['materiosearchapi/autocomplete/dbselect'] = $base+array(
  56. 'access arguments' => array('use materio search api autocomplete'),
  57. 'page callback' => 'materio_search_api_autocomplete_dbselect',
  58. );
  59. $items['materiosearchapi/autocomplete/searchapi'] = $base+array(
  60. 'access arguments' => array('use materio search api autocomplete'),
  61. 'page callback' => 'materio_search_api_autocomplete_searchapi',
  62. );
  63. $items['explore'] = $base+array(
  64. 'access arguments' => array('use materio search api'),
  65. 'page callback' => 'materio_search_api_results_search',
  66. 'title' => t('Explore'),
  67. //'page argument' => array(1,2,3),
  68. );
  69. $items['actuality'] = $base+array(
  70. // 'access arguments' => array(),
  71. 'page callback' => 'materio_search_api_actuality',
  72. // 'page argument' => array(),
  73. 'access callback' => TRUE,
  74. );
  75. $items['materiosearchapi/viewmode/change'] = $base+array(
  76. 'access arguments' => array('use materio search api'),
  77. 'page callback' => 'materio_search_api_viewmode_change',
  78. 'page argument' => array(3),
  79. );
  80. return $items;
  81. }
  82. /**
  83. * hook_entity_property_info_alter().
  84. */
  85. function materio_search_api_entity_property_info_alter(&$info){
  86. // dsm($info, 'hook_entity_property_info_alter | info');
  87. $properties = &$info['node']['properties'];
  88. for ($i=1; $i <= 5 ; $i++) {
  89. $properties['materio_search_api_taxonomy_term_'.$i] = array(
  90. 'type'=>'taxonomy_term',
  91. 'label'=> t('Main taxonomy term '.$i),
  92. 'query callback'=>'entity_metadata_table_query',
  93. 'getter callback'=>'materio_search_api_get_main_taxonomy_term_'.$i,
  94. );
  95. }
  96. return $properties;
  97. }
  98. function materio_search_api_get_main_taxonomy_term_1($item){
  99. // dsm($item, 'item');
  100. return materio_search_api_get_onto_term($item, 0);
  101. }
  102. function materio_search_api_get_main_taxonomy_term_2($item){
  103. // dsm($item, 'item');
  104. return materio_search_api_get_onto_term($item, 1);
  105. }
  106. function materio_search_api_get_main_taxonomy_term_3($item){
  107. // dsm($item, 'item');
  108. return materio_search_api_get_onto_term($item, 2);
  109. }
  110. function materio_search_api_get_main_taxonomy_term_4($item){
  111. // dsm($item, 'item');
  112. return materio_search_api_get_onto_term($item, 3);
  113. }
  114. function materio_search_api_get_main_taxonomy_term_5($item){
  115. // dsm($item, 'item');
  116. return materio_search_api_get_onto_term($item, 4);
  117. }
  118. function materio_search_api_get_onto_term($item, $delta){
  119. // dsm($item, 'item');
  120. // dsm($delta, 'delta');
  121. if(isset($item->field_onthologie['und'][$delta]))
  122. return taxonomy_term_load($item->field_onthologie['und'][$delta]['tid']);
  123. return null;
  124. }
  125. /**
  126. * Implements hook_block_info().
  127. */
  128. function materio_search_api_block_info() {
  129. // This example comes from node.module.
  130. $blocks['materio_search_api_search'] = array(
  131. 'info' => t('Materio search api search'),
  132. 'cache' => DRUPAL_NO_CACHE
  133. );
  134. $blocks['materio_search_api_viewmode'] = array(
  135. 'info' => t('Materio search api view mode selection'),
  136. 'cache' => DRUPAL_NO_CACHE
  137. );
  138. // $blocks['materio_search_api_filters'] = array(
  139. // 'info' => t('Materio search api filters'),
  140. // 'cache' => DRUPAL_NO_CACHE
  141. // );
  142. return $blocks;
  143. }
  144. /**
  145. * Implements hook_block_view().
  146. */
  147. function materio_search_api_block_view($delta = '') {
  148. // This example comes from node.module. Note that you can also return a
  149. // renderable array rather than rendered HTML for 'content'.
  150. $block = array();
  151. switch ($delta) {
  152. case 'materio_search_api_search':
  153. if (user_access('use materio search api')) {
  154. $block['subject'] = t('Search');
  155. $block['content'] = theme('materio_search_api_search_block', array());
  156. }
  157. break;
  158. case 'materio_search_api_viewmode':
  159. if (user_access('use materio search api viewmode selection')) {
  160. $block['subject'] = t('View mode');
  161. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  162. }
  163. break;
  164. // case 'materio_search_api_filters':
  165. // if (user_access('use materio search api filters')) {
  166. // $block['subject'] = t('Filters');
  167. // $block['content'] = theme('materio_search_api_filters_block', array());
  168. // }
  169. // break;
  170. }
  171. return $block;
  172. }
  173. /**
  174. * Implements hook_entity_info_alter().
  175. */
  176. function materio_search_api_entity_info_alter(&$entity_info) {
  177. $entity_info['node']['view modes']['cardsmall'] = array(
  178. 'label' => t('Small cards'),
  179. 'custom settings' => TRUE,
  180. );
  181. $entity_info['node']['view modes']['cardmedium'] = array(
  182. 'label' => t('Medium cards'),
  183. 'custom settings' => TRUE,
  184. );
  185. $entity_info['node']['view modes']['cardbig'] = array(
  186. 'label' => t('Big cards'),
  187. 'custom settings' => TRUE,
  188. );
  189. $entity_info['node']['view modes']['cardfull'] = array(
  190. 'label' => t('Full cards'),
  191. 'custom settings' => TRUE,
  192. );
  193. }
  194. /**
  195. * Implements hook_node_view().
  196. */
  197. function materio_search_api_node_view($node, $view_mode, $langcode) {
  198. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  199. # PRINT 7.1
  200. print_node_view($node, 'full');
  201. print_pdf_node_view($node, 'full');
  202. # PRINT 7.2-beta
  203. //print_ui_node_view($node, 'full');
  204. // dsm($node, 'node');
  205. }
  206. }
  207. /**
  208. * materiobase_search_form()
  209. */
  210. function materio_search_api_search_form($form, &$form_state){
  211. // dsm($form_state, 'form_state');
  212. // dsm($form, 'form');
  213. global $user;
  214. $form = array();
  215. $args = arg();
  216. $path = array_shift($args);
  217. $keys = implode('/', $args);
  218. $form['searchfield'] = array(
  219. '#type' => 'textfield',
  220. '#default_value' => $path == 'explore' ? $keys : "", // TODO: set the search page path global or a variable in settings
  221. // '#value' => $keys,
  222. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  223. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  224. '#size' => 30,
  225. '#maxlength' => 1024,
  226. );
  227. if(user_access('use materio search api filters')){
  228. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  229. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  230. foreach ($indexed_bundles as $bundle) {
  231. $bundles_options[$bundle] = $bundle;
  232. $default_bundles[] = $bundle;
  233. }
  234. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  235. $form['bundles_filter'] = array(
  236. '#type'=>'checkboxes',
  237. '#options' => $bundles_options,
  238. '#default_value' => $user_bundles_filter,
  239. // '#attributes' => array('class'=>array('btn-group')),
  240. );
  241. }
  242. $form['create'] = array(
  243. '#type' => 'image_button',
  244. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  245. '#value' => t('Search'),
  246. );
  247. return $form;
  248. }
  249. function materio_search_api_search_form_validate($form, &$form_state){
  250. // dsm($form, '$form');
  251. // dsm($form_state, '$form_state');
  252. // dsm(strlen($form_state['values']['searchfield']));
  253. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  254. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  255. }
  256. }
  257. function materio_search_api_search_form_submit($form, &$form_state){
  258. // dsm($form_state, 'form_state');
  259. global $user;
  260. if(user_access('use materio search api filters')){
  261. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  262. if($value)
  263. $bundles[] = $bundle;
  264. # if no filter checked we checked them all by default
  265. if(!isset($bundles))
  266. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  267. $bundles[] = $bundle;
  268. }else{
  269. # if user have no access to filters
  270. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  271. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  272. foreach ($indexed_bundles as $bundle) {
  273. $bundles[] = $bundle;
  274. }
  275. }
  276. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  277. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  278. }
  279. /**
  280. * viewmode
  281. */
  282. function _materio_search_api_change_viewmode($vm){
  283. // dsm($vm, '_materio_search_api_change_viewmode');
  284. global $user;
  285. // dsm($user, 'user');
  286. $entity_infos = entity_get_info();
  287. // dsm($entity_infos, 'entity_infos');
  288. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  289. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  290. $rep = array('statut'=>'saved');
  291. }else{
  292. $rep = array('statut'=>'viewmode not allowed');
  293. }
  294. return $rep;
  295. }
  296. /**
  297. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  298. */
  299. /**
  300. * Implements hook_theme().
  301. */
  302. function materio_search_api_theme($existing, $type, $theme, $path) {
  303. return array(
  304. 'materio_search_api_search_block' => array(
  305. 'arguments' => array(),
  306. 'template' => 'materio-search-api-search-block',
  307. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  308. ),
  309. 'materio_search_api_select_viewmode_block' => array(
  310. 'arguments' => array(),
  311. 'template' => 'materio-search-api-select-viewmode-block',
  312. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  313. ),
  314. // 'materio_search_api_filters_block' => array(
  315. // 'arguments' => array(),
  316. // 'template' => 'materio-search-api-filters-block',
  317. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  318. // ),
  319. 'materio_search_api_results' => array(
  320. 'arguments' => array(),
  321. 'template' => 'materio-search-api-results',
  322. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  323. 'variables' => array(
  324. 'index' => NULL,
  325. 'results' => array('result count' => 0),
  326. 'items' => array(),
  327. 'view_mode' => 'teaser',
  328. 'keys' => '',
  329. // 'page_machine_name' => NULL,
  330. 'spellcheck' => NULL,
  331. 'pager' => NULL,
  332. ),
  333. ),
  334. // 'materio_search_api_performance' => array(
  335. // 'render element' => 'element',
  336. // ),
  337. 'materio_search_api_actuality' => array(
  338. 'template' => 'materio-search-api-actuality',
  339. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  340. 'arguments' => array(
  341. 'items' => array(),
  342. 'view_mode' => "teaser",
  343. 'pager' => NULL,
  344. 'count' => 0,
  345. )
  346. )
  347. );
  348. }
  349. /**
  350. * Implements theme for rendering search-performance
  351. */
  352. // function theme_materio_search_api_performance($variables) {
  353. // $element = array_shift($variables);
  354. // return $element['#markup'];
  355. // }
  356. /**
  357. * template_preprocess_materiobase_search_block();
  358. */
  359. function template_preprocess_materio_search_api_search_block(&$vars){
  360. // dsm($vars, '$vars');
  361. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  362. }
  363. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  364. global $user;
  365. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  366. $availableviewmodes = variable_get('availableviewmodes', -1);
  367. // dsm($availableviewmodes);
  368. $entity_infos = entity_get_info();
  369. // dsm($entity_infos, 'entity_infos');
  370. $content = '<div class="btn-group btn-group-vertical">';
  371. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  372. if(in_array($viewmode, $availableviewmodes)){
  373. $link = l(
  374. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  375. 'materiosearchapi/viewmode/change/'.$viewmode,
  376. array(
  377. 'query' => drupal_get_destination(),
  378. 'html' => true,
  379. 'attributes' => array(
  380. 'class' => array(
  381. 'viewmode-link',
  382. 'viewmode-'.$viewmode,
  383. $active == $viewmode ? " active" : ''
  384. ),
  385. 'rel' => $viewmode
  386. )
  387. )
  388. );
  389. $content .= $link;
  390. }
  391. }
  392. $content .= '</div>';
  393. $vars['content'] = $content;
  394. }
  395. // function template_preprocess_materio_search_api_filters_block(&$vars){
  396. // $index_machine_name = variable_get('mainsearchindex', -1);
  397. // $index = search_api_index_load($index_machine_name);
  398. // dsm($index, 'index');
  399. // // $entity_infos = entity_get_info($index->item_type);
  400. // // dsm($entity_infos, 'entity_infos');
  401. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  402. // dsm($indexed_bundles, 'indexed_bundles');
  403. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  404. // }
  405. // function materio_search_api_filters_form($form, $form_state, $bundles){
  406. // $form = array();
  407. // dsm($bundles, 'bundles');
  408. // foreach ($bundles as $bundle) {
  409. // $form[$bundle . '_filter'] = array(
  410. // '#type'=>'checkbox',
  411. // '#default_value' => -1,
  412. // '#title' => $bundle,
  413. // );
  414. // }
  415. // return $form;
  416. // }
  417. /**
  418. * Function for preprocessing the variables for the search_api_page_results
  419. * template.
  420. *
  421. * @param array $variables
  422. * An associative array containing:
  423. * - $index: The index this search was executed on.
  424. * - $results: An array of search results, as returned by
  425. * SearchApiQueryInterface::execute().
  426. * - $keys: The keywords of the executed search.
  427. *
  428. * @see materio-search-api-results.tpl.php
  429. */
  430. function template_preprocess_materio_search_api_results(array &$variables) {
  431. // dsm($variables, '$variables');
  432. $results = $variables['results'];
  433. $keys = $variables['keys'];
  434. $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  435. $variables['result_count'] = $results['result count'];
  436. $variables['sec'] = round($results['performance']['complete'], 3);
  437. $variables['search_performance'] = format_plural(
  438. $results['result count'],
  439. 'The search found 1 result.', // in @sec seconds
  440. 'The search found @count results.', // in @sec seconds
  441. array('@sec' => $variables['sec'])
  442. );
  443. //dsm($variables, 'variables');
  444. }
  445. function template_preprocess_materio_search_api_actuality(&$vars){
  446. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  447. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  448. $vars['actualities_infos'] = t('');
  449. }
  450. /**
  451. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  452. */
  453. /**
  454. * Implements hook_block_view_alter().
  455. */
  456. function materio_search_api_block_view_alter(&$data, $block) {
  457. // this alter prepopulate the search form provide by search_api_page
  458. if ($block->module == 'search_api_page') {
  459. $page = search_api_page_load($block->delta);
  460. $item = menu_get_item();
  461. if (isset($page->path) && $page->path == $item['path']) {
  462. $keys = arg(count(arg(NULL, $page->path)));
  463. if ($keys) {
  464. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  465. $data['content']['keys_' . $page->id]['#value'] = $keys;
  466. }
  467. }
  468. }
  469. }