materio_search_api.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. * materiobase_search_form()
  196. */
  197. function materio_search_api_search_form($form, &$form_state){
  198. // dsm($form_state, 'form_state');
  199. // dsm($form, 'form');
  200. global $user;
  201. $form = array();
  202. $args = arg();
  203. $path = array_shift($args);
  204. $keys = implode('/', $args);
  205. $form['searchfield'] = array(
  206. '#type' => 'textfield',
  207. '#default_value' => $path == 'explore' ? $keys : "", // TODO: set the search page path global or a variable in settings
  208. // '#value' => $keys,
  209. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  210. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  211. '#size' => 30,
  212. '#maxlength' => 1024,
  213. );
  214. if(user_access('use materio search api filters')){
  215. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  216. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  217. foreach ($indexed_bundles as $bundle) {
  218. $bundles_options[$bundle] = $bundle;
  219. $default_bundles[] = $bundle;
  220. }
  221. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  222. $form['bundles_filter'] = array(
  223. '#type'=>'checkboxes',
  224. '#options' => $bundles_options,
  225. '#default_value' => $user_bundles_filter,
  226. // '#attributes' => array('class'=>array('btn-group')),
  227. );
  228. }
  229. $form['create'] = array(
  230. '#type' => 'image_button',
  231. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  232. '#value' => t('Search'),
  233. );
  234. return $form;
  235. }
  236. function materio_search_api_search_form_validate($form, &$form_state){
  237. // dsm($form, '$form');
  238. // dsm($form_state, '$form_state');
  239. // dsm(strlen($form_state['values']['searchfield']));
  240. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  241. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  242. }
  243. }
  244. function materio_search_api_search_form_submit($form, &$form_state){
  245. // dsm($form_state, 'form_state');
  246. global $user;
  247. if(user_access('use materio search api filters')){
  248. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  249. if($value)
  250. $bundles[] = $bundle;
  251. # if no filter checked we checked them all by default
  252. if(!isset($bundles))
  253. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  254. $bundles[] = $bundle;
  255. }else{
  256. # if user have no access to filters
  257. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  258. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  259. foreach ($indexed_bundles as $bundle) {
  260. $bundles[] = $bundle;
  261. }
  262. }
  263. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  264. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  265. }
  266. /**
  267. * viewmode
  268. */
  269. function _materio_search_api_change_viewmode($vm){
  270. // dsm($vm, '_materio_search_api_change_viewmode');
  271. global $user;
  272. // dsm($user, 'user');
  273. $entity_infos = entity_get_info();
  274. // dsm($entity_infos, 'entity_infos');
  275. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  276. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  277. $rep = array('statut'=>'saved');
  278. }else{
  279. $rep = array('statut'=>'viewmode not allowed');
  280. }
  281. return $rep;
  282. }
  283. /**
  284. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  285. */
  286. /**
  287. * Implements hook_theme().
  288. */
  289. function materio_search_api_theme($existing, $type, $theme, $path) {
  290. return array(
  291. 'materio_search_api_search_block' => array(
  292. 'arguments' => array(),
  293. 'template' => 'materio-search-api-search-block',
  294. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  295. ),
  296. 'materio_search_api_select_viewmode_block' => array(
  297. 'arguments' => array(),
  298. 'template' => 'materio-search-api-select-viewmode-block',
  299. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  300. ),
  301. // 'materio_search_api_filters_block' => array(
  302. // 'arguments' => array(),
  303. // 'template' => 'materio-search-api-filters-block',
  304. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  305. // ),
  306. 'materio_search_api_results' => array(
  307. 'arguments' => array(),
  308. 'template' => 'materio-search-api-results',
  309. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  310. 'variables' => array(
  311. 'index' => NULL,
  312. 'results' => array('result count' => 0),
  313. 'items' => array(),
  314. 'view_mode' => 'teaser',
  315. 'keys' => '',
  316. // 'page_machine_name' => NULL,
  317. 'spellcheck' => NULL,
  318. 'pager' => NULL,
  319. ),
  320. ),
  321. // 'materio_search_api_performance' => array(
  322. // 'render element' => 'element',
  323. // ),
  324. 'materio_search_api_actuality' => array(
  325. 'template' => 'materio-search-api-actuality',
  326. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  327. 'arguments' => array(
  328. 'items' => array(),
  329. 'view_mode' => "teaser",
  330. 'pager' => NULL,
  331. 'count' => 0,
  332. )
  333. )
  334. );
  335. }
  336. /**
  337. * Implements theme for rendering search-performance
  338. */
  339. // function theme_materio_search_api_performance($variables) {
  340. // $element = array_shift($variables);
  341. // return $element['#markup'];
  342. // }
  343. /**
  344. * template_preprocess_materiobase_search_block();
  345. */
  346. function template_preprocess_materio_search_api_search_block(&$vars){
  347. // dsm($vars, '$vars');
  348. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  349. }
  350. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  351. global $user;
  352. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  353. $availableviewmodes = variable_get('availableviewmodes', -1);
  354. // dsm($availableviewmodes);
  355. $entity_infos = entity_get_info();
  356. // dsm($entity_infos, 'entity_infos');
  357. // TODO: refaire les boutons view modes pour qu'il fonctionne sans javascript
  358. // faire un lien vers l'enregistrement du mode puis un reload de la page courante
  359. $content = '<div class="btn-group btn-group-vertical">';
  360. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  361. if(in_array($viewmode, $availableviewmodes)){
  362. $link = l(
  363. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  364. 'materiosearchapi/viewmode/change/'.$viewmode,
  365. array(
  366. 'query' => drupal_get_destination(),
  367. 'html' => true,
  368. 'attributes' => array(
  369. 'class' => array(
  370. 'viewmode-link',
  371. 'viewmode-'.$viewmode,
  372. $active == $viewmode ? " active" : ''
  373. ),
  374. 'rel' => $viewmode
  375. )
  376. )
  377. );
  378. $content .= $link;
  379. }
  380. }
  381. $content .= '</div>';
  382. $vars['content'] = $content;
  383. }
  384. // function template_preprocess_materio_search_api_filters_block(&$vars){
  385. // $index_machine_name = variable_get('mainsearchindex', -1);
  386. // $index = search_api_index_load($index_machine_name);
  387. // dsm($index, 'index');
  388. // // $entity_infos = entity_get_info($index->item_type);
  389. // // dsm($entity_infos, 'entity_infos');
  390. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  391. // dsm($indexed_bundles, 'indexed_bundles');
  392. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  393. // }
  394. // function materio_search_api_filters_form($form, $form_state, $bundles){
  395. // $form = array();
  396. // dsm($bundles, 'bundles');
  397. // foreach ($bundles as $bundle) {
  398. // $form[$bundle . '_filter'] = array(
  399. // '#type'=>'checkbox',
  400. // '#default_value' => -1,
  401. // '#title' => $bundle,
  402. // );
  403. // }
  404. // return $form;
  405. // }
  406. /**
  407. * Function for preprocessing the variables for the search_api_page_results
  408. * template.
  409. *
  410. * @param array $variables
  411. * An associative array containing:
  412. * - $index: The index this search was executed on.
  413. * - $results: An array of search results, as returned by
  414. * SearchApiQueryInterface::execute().
  415. * - $keys: The keywords of the executed search.
  416. *
  417. * @see materio-search-api-results.tpl.php
  418. */
  419. function template_preprocess_materio_search_api_results(array &$variables) {
  420. // dsm($variables, '$variables');
  421. $results = $variables['results'];
  422. $keys = $variables['keys'];
  423. $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  424. $variables['result_count'] = $results['result count'];
  425. $variables['sec'] = round($results['performance']['complete'], 3);
  426. $variables['search_performance'] = format_plural(
  427. $results['result count'],
  428. 'The search found 1 result.', // in @sec seconds
  429. 'The search found @count results.', // in @sec seconds
  430. array('@sec' => $variables['sec'])
  431. );
  432. // $variables['search_results'] = array(
  433. // '#theme' => 'search_results_list',
  434. // 'results' => $variables['results']['results'],
  435. // 'items' => $variables['items'],
  436. // 'index' => $variables['index'],
  437. // 'type' => 'ul',
  438. // );
  439. //dsm($variables, 'variables');
  440. }
  441. function template_preprocess_materio_search_api_actuality(&$vars){
  442. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  443. // $items = array();
  444. // foreach ($vars['items'] as $nid => $item) {
  445. // $items[] = node_load($nid);
  446. // }
  447. // $vars['items'] = $items;
  448. $vars['actualities_infos'] = t('Actualities by materiO\'');
  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. }