materio_search_api.pages.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /**
  3. * materiobase_search_autocomplete_dbselect()
  4. *
  5. * inspired by taxonomy_autocomplete()
  6. *
  7. * OBSOLETE : this fonction use a direct dbselect request to provide results forautocomplete
  8. *
  9. */
  10. function materio_search_api_autocomplete_dbselect($typed = ''){
  11. // If the request has a '/' in the search text, then the menu system will have
  12. // split it into multiple arguments, recover the intended $tags_typed.
  13. $args = func_get_args();
  14. $typed = implode('/', $args);
  15. /*
  16. TODO riche serach engine + \\ etc gmail like
  17. */
  18. if ($typed != '') {
  19. // Part of the criteria for the query come from the field's own settings.
  20. $vids = array();
  21. $vocabularies = taxonomy_vocabulary_get_names();
  22. foreach ($vocabularies as $voc) {
  23. $vids[] = $voc->vid;
  24. }
  25. $query = db_select('taxonomy_term_data', 't');
  26. $query->addTag('translatable');
  27. $query->addTag('term_access');
  28. // Select rows that match by term name.
  29. $tags_return = $query
  30. ->fields('t', array('tid', 'name'))
  31. ->condition('t.vid', $vids)
  32. ->condition('t.name', '%' . db_like($typed) . '%', 'LIKE')
  33. ->range(0, 10)
  34. ->execute()
  35. ->fetchAllKeyed();
  36. $term_matches = array();
  37. foreach ($tags_return as $tid => $name) {
  38. $n = $name;
  39. // Term names containing commas or quotes must be wrapped in quotes.
  40. // if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  41. // $n = '"' . str_replace('"', '""', $name) . '"';
  42. // }
  43. $term_matches[$n] = check_plain($name);
  44. }
  45. }
  46. drupal_json_output($term_matches);
  47. }
  48. /**
  49. * materio_search_api_autocomplete_searchapi($typed = '')
  50. *
  51. * GOOD one using searchapi (SOLR)
  52. */
  53. function materio_search_api_autocomplete_searchapi($typed = ''){
  54. // If the request has a '/' in the search text, then the menu system will have
  55. // split it into multiple arguments, recover the intended $tags_typed.
  56. $args = func_get_args();
  57. $typed = implode('/', $args);
  58. // dsm($typed, 'typed');
  59. if ($typed != '') {
  60. // search for patterns like key -another key +lastkey
  61. // and provide auto completion for the last key
  62. preg_match_all('/\s?[\+|-]?[^\s]+/', $typed, $adv_search_q);
  63. // dsm($adv_search_q, "adv_search_q");
  64. preg_match('/^(\+|-)?(.*)$/', trim(array_pop($adv_search_q[0])), $last);
  65. // dsm($last, "last");
  66. $tosearch = isset($last[2]) ? $last[2] : $typed;
  67. // build the query
  68. global $language;
  69. $index_machine_name = variable_get('autocompletesearchindex_'.$language->language, -1);
  70. $query = search_api_query($index_machine_name);
  71. // $query_filter = $query->createFilter();
  72. // $query_filter->condition('name', $tosearch);
  73. // $query_filter->condition('type', 'article');
  74. // $query->filter($query_filter);
  75. $query->keys($tosearch);
  76. $tags_return = $query->execute();
  77. // dsm($tags_return, '$tags_return');
  78. if($tags_return['result count']){
  79. $term_matches = array();
  80. $index = search_api_index_load($index_machine_name);
  81. $delta = 0;
  82. foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) {
  83. //dsm($item, '$item');
  84. //$term_matches[$item->tid] = check_plain($item->name);
  85. // $term_matches[check_plain($item->name)] = check_plain($item->name);
  86. // TODO: leave tags with nodes
  87. $itemname = $item->name;
  88. $term_matches[trim(implode(' ', $adv_search_q[0]).' "'.$last[1].$itemname).'"'] = check_plain($itemname);
  89. $delta++;
  90. if($delta > 15)
  91. break;
  92. }
  93. drupal_json_output($term_matches);
  94. }else{
  95. drupal_json_output(array());
  96. }
  97. }else{
  98. return;
  99. }
  100. // dsm($term_matches, 'term_matches');
  101. // return 'debug mode of materio_search_api_autocomplete_searchapi';
  102. }
  103. /**
  104. * materio_search_api_results_search()
  105. *
  106. *
  107. */
  108. function materio_search_api_results_search(){
  109. //dsm("materio_search_api_results_search");
  110. // retreive typed words separated by slashes as a sentence
  111. $args = func_get_args();
  112. // dsm($args, 'args');
  113. $keys = $args;
  114. // $keys = explode(' ',implode(' ',$args));
  115. // dsm($keys, 'keys');
  116. $typed = implode(' ', $keys);
  117. // dsm($typed, 'typed');
  118. # with parse mode terms we use a single string of words seperated by spaces wich will be OR or AND regarding the conjunction query option
  119. # had to add q.op = OR on solr requesthandler on solrconfig.xml
  120. // remove query page params
  121. preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
  122. //dsm($pages, '$pages');
  123. if($pages[0]){
  124. $typed = str_replace($pages[0][0], '', $typed);
  125. }
  126. // dsm($typed, 'typed');
  127. if ($typed) {
  128. global $language;
  129. global $user;
  130. # retrieve viewmode and then use it to define the query range
  131. $viewmode = isset($user->data['materiosearchapi_viewmode'])
  132. ? $user->data['materiosearchapi_viewmode']
  133. : variable_get('defaultviewmode', 'full');
  134. $limit = variable_get($viewmode.'_limite', '10');
  135. // dsm($limit, "limit");
  136. $offset = pager_find_page() * $limit; //$page*$limit;//
  137. // dsm($offset, "offset");
  138. # Normal search
  139. if(user_access('use materio search api')){
  140. // dsm('normal search');
  141. $results = msa_solrquery_materiauxbreves($typed, $language, $user, $offset, $limit);
  142. }
  143. # only breves search (+ related materials)
  144. # not used anymore as free users not exists anymore
  145. else if(user_access('use materio search api for breves')){
  146. // dsm('limited search');
  147. $results = msa_solrquery_breves($typed, $language, $user, $offset, $limit);
  148. }
  149. // for all case
  150. $ret['results']['#results'] = $results;
  151. $ret['results']['#items'] = $results['items'];
  152. // $ret['results']['#index'] = $results['index'];
  153. $ret['results']['#theme'] = 'materio_search_api_results';
  154. $ret['results']['#keys'] = $typed;
  155. $ret['results']['#view_mode'] = $viewmode;
  156. // page title
  157. $page_title = $typed;
  158. drupal_set_title('<i class="icon-materio-search"></i>'.check_plain($page_title), PASS_THROUGH);
  159. // render results
  160. if(isset($results)){
  161. // Load pager.
  162. // if ($results['result count'] > $page->options['per_page']) {
  163. pager_default_initialize($results['result count'], $limit);
  164. $ret['results']['#pager'] = theme('pager');
  165. // }
  166. if (!empty($results['ignored'])) {
  167. drupal_set_message(
  168. t('The following search keys are too short or too common and were therefore ignored: "@list".',
  169. array( '@list' => implode(t('", "'), $results['ignored']) ) ),
  170. 'warning'
  171. );
  172. }
  173. if (!empty($results['warnings'])) {
  174. foreach ($results['warnings'] as $warning) {
  175. drupal_set_message($warning, 'warning');
  176. }
  177. }
  178. }
  179. }
  180. // dsm($ret, 'ret');
  181. return $ret;
  182. }
  183. function msa_solrquery_materiauxbreves($keys, $language, $user, $offset, $limit){
  184. // dsm($offset, 'offset');
  185. // dsm($limit, 'limit');
  186. // -- communs --//
  187. # define default bundle option (materiaux, breves)
  188. $default_bundles = array();
  189. if(isset($taxo_index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'])){
  190. $indexed_bundles = $taxo_index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  191. foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; }
  192. }
  193. # choose solr query bundle option
  194. $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter'])
  195. ? $user->data['materiosearchapi_bundlesfilter']
  196. : $default_bundles;
  197. // -- -- taxo search AND -- -- //
  198. # define search api solr index
  199. $taxo_index_machine_name = variable_get('taxonomysearchindex_'.$language->language, -1);
  200. $taxo_index = search_api_index_load($taxo_index_machine_name);
  201. # choose solr query options
  202. $query_options = array('conjunction'=>'AND', 'parse mode'=>'terms');
  203. #create the solr query for taxonomy search
  204. $taxo_query = search_api_query($taxo_index_machine_name, $query_options)
  205. ->keys($keys);
  206. # apply bundle options to solr query if usefull
  207. if(count($bundles_filter)){
  208. $filter = $taxo_query->createFilter('OR');
  209. foreach ($bundles_filter as $type)
  210. $filter->condition('type', $type, '=');
  211. $taxo_query->filter($filter);
  212. }
  213. // $query->setOption('search_api_bypass_access', true);
  214. # add user access solr query option
  215. $taxo_query->setOption('search_api_access_account', $user);
  216. // $taxo_query->setOption('q.op', 'OR');
  217. #execute first time to get the all items, to be able to filter the full text research
  218. $taxo_total_results = $taxo_query->execute();
  219. // dsm($taxo_total_results, "taxo total results");
  220. $return = array(
  221. "results" => $taxo_total_results['results']
  222. );
  223. // -- -- full text search -- -- //
  224. #define search api solr index
  225. $fulltxt_index_machine_name = variable_get('fulltextsearchindex_'.$language->language, -1);
  226. $fulltxt_index = search_api_index_load($fulltxt_index_machine_name);
  227. # choose solr query options
  228. $query_options = array('conjunction'=>'OR', 'parse mode'=>'terms');
  229. #create the solr query for taxonomy search
  230. $fulltxt_query = search_api_query($fulltxt_index_machine_name, $query_options)
  231. ->keys($keys);
  232. # apply bundle options to solr query if usefull
  233. if(count($bundles_filter)){
  234. $filter = $fulltxt_query->createFilter('OR');
  235. foreach ($bundles_filter as $type)
  236. $filter->condition('type', $type, '=');
  237. $fulltxt_query->filter($filter);
  238. }
  239. # filter to remove precedent search query
  240. $nid_filter = $fulltxt_query->createFilter('AND');
  241. foreach ($results['results'] as $nid => $item)
  242. $nid_filter->condition('nid', $nid, '<>');
  243. $fulltxt_query->filter($nid_filter);
  244. # add user access solr query option
  245. $fulltxt_query->setOption('search_api_access_account', $user);
  246. # execute solr query and record results
  247. $fulltxt_results = $fulltxt_query->execute();
  248. // dsm($fulltxt_results, "fulltxt_results");
  249. # add the fulltext search results to the global results
  250. $return['results'] += $fulltxt_results['results'];
  251. # count global results
  252. $return['result count'] = count($return['results']);
  253. # create items array with the good range
  254. $return['items'] = array();
  255. $o = -1;
  256. foreach ($return['results'] as $nid => $value) {
  257. $o++;
  258. if( $o < $offset ) continue;
  259. if ($o >= $offset+$limit) break;
  260. $return['items'][$nid] = node_load($nid);
  261. }
  262. # TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
  263. // dsm($return, 'return');
  264. return $return;
  265. }
  266. # not used anymore as free users not exists anymore
  267. function msa_solrquery_breves($typed, $language, $user, $offset, $limit){
  268. $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
  269. // dsm($index_machine_name, '$index_machine_name');
  270. $index = search_api_index_load($index_machine_name);
  271. # choose solr query options
  272. $query_options = array('conjunction'=>'OR', 'parse mode'=>'direct');
  273. #create the solr query
  274. $query = search_api_query($index_machine_name, $query_options)
  275. ->keys($typed)
  276. ->range($offset, $limit);
  277. // $query->setOption('search_api_bypass_access', true);
  278. # add user access solr query option
  279. $query->setOption('search_api_access_account', $user);
  280. # execute solr query and record results
  281. $results = $query->execute();
  282. // dsm($results, 'results');
  283. $results['index'] = $index;//search_api_index_load($index_machine_name);
  284. #could
  285. $could_index_machine_name = variable_get('fulltextsearchindex_'.$language->language, -1);
  286. $could_index = search_api_index_load($could_index_machine_name);
  287. # in case of free user search, run a real search to indicate how much items you could find
  288. $could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
  289. ->keys($typed);
  290. $could_results = $could_query->execute();
  291. // dsm($could_results, 'could_results');
  292. $results['could results'] = $could_results;
  293. # add items : breve + materials wich are in the could result
  294. $items = array();
  295. $breves = $index->loadItems(array_keys($results['results']));
  296. foreach ($breves as $nid => $breve) {
  297. if(!node_access('view', $breve))
  298. continue;
  299. $items[] = $breve;
  300. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  301. // dsm($materiaux, 'materiaux');
  302. if($materiaux){
  303. foreach ($materiaux as $value) {
  304. if(!isset($could_results['results'][$value['target_id']]))
  305. continue;
  306. $materiau = node_load($value['target_id']);
  307. if(node_access('view', $materiau))
  308. $items[] = $materiau;
  309. }
  310. }
  311. }
  312. $results['items'] = $items;
  313. $results['breves count'] = $results['result count'];
  314. $results['result count'] = count($items);
  315. // dsm($results, "results");
  316. # TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
  317. return $results;
  318. }
  319. /**
  320. * materio_search_api_actuality()
  321. *
  322. */
  323. # only breves search (+ related materials)
  324. # NOT USED ANYMORE as free users not exists anymore
  325. function materio_search_api_actuality(){
  326. global $user;
  327. if(isset($user->roles[1])){
  328. $date_limit = strtotime('-6 month');
  329. // dsm(date('d m y', $date_limit));
  330. }
  331. # retrieve viewmode and then use it to define the query range
  332. $viewmode = user_access('access to materio database')
  333. ? isset($user->data['materiosearchapi_viewmode'])
  334. ? $user->data['materiosearchapi_viewmode']
  335. : variable_get('defaultviewmode', 'cardmedium')
  336. : 'cardbig';
  337. $limit = 10;//variable_get($viewmode.'_limite', '10');
  338. $offset = pager_find_page() * $limit;
  339. // dsm($offset);
  340. $query = new EntityFieldQuery;
  341. $query
  342. ->entityCondition('entity_type', 'node')
  343. ->propertyCondition('status', 1)
  344. ->entityCondition('bundle', array('breve'))
  345. ->propertyOrderBy('created', 'DESC')
  346. ->range($offset,$limit);
  347. if(isset($user->roles[1])){
  348. $query->propertyCondition('created', $date_limit, '>');
  349. }
  350. $result = $query->execute();
  351. // dsm($result, '$result');
  352. $count_query = new EntityFieldQuery;
  353. $count_query
  354. ->entityCondition('entity_type', 'node')
  355. ->propertyCondition('status', 1)
  356. ->entityCondition('bundle', array('breve'));
  357. // dsm($count, 'count');
  358. if(isset($user->roles[1])){
  359. $count_query->propertyCondition('created', $date_limit, '>');
  360. }
  361. $count = $count_query->count()->execute();
  362. pager_default_initialize($count, $limit);
  363. $items = array();
  364. if(isset($result['node'])){
  365. foreach ($result['node'] as $nid => $n) {
  366. $breve = node_load($nid);
  367. if(!node_access('view', $breve))
  368. continue;
  369. $items[] = $breve;
  370. // if(user_access('access to materio database')){
  371. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  372. // dsm($materiaux, 'materiaux');
  373. if($materiaux){
  374. foreach ($materiaux as $value) {
  375. $materiau = node_load($value['target_id']);
  376. if(node_access('view', $materiau))
  377. $items[] = $materiau;
  378. }
  379. }
  380. // }
  381. }
  382. }
  383. // drupal_set_title(t('Actualities'));
  384. drupal_set_title(t(''));
  385. return theme('materio_search_api_actuality', array(
  386. 'items' => $items,
  387. 'view_mode' => $viewmode,
  388. 'count' => $count,
  389. 'pager' => theme('pager'),
  390. ));
  391. }
  392. function materio_search_api_viewmode_change($vm){
  393. //dsm($vm, 'materio_search_api_viewmode_change');
  394. $rep = _materio_search_api_change_viewmode($vm);
  395. //return 'debug mode for materio_search_api_viewmode_change';
  396. // drupal_json_output($rep);
  397. drupal_goto();
  398. }