materio_search_api.pages.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. preg_match('/^(\+|-)?(.*)$/', trim(array_pop($adv_search_q[0])), $last);
  64. $tosearch = isset($last[2]) ? $last[2] : $typed;
  65. // build the query
  66. global $language;
  67. $index_machine_name = variable_get('autocompletesearchindex_'.$language->language, -1);
  68. $query = search_api_query($index_machine_name);
  69. // $query_filter = $query->createFilter();
  70. // $query_filter->condition('name', $tosearch);
  71. // $query_filter->condition('type', 'article');
  72. // $query->filter($query_filter);
  73. $query->keys($tosearch);
  74. $tags_return = $query->execute();
  75. // dsm($tags_return, '$tags_return');
  76. if($tags_return['result count']){
  77. $term_matches = array();
  78. $index = search_api_index_load($index_machine_name);
  79. $delta = 0;
  80. foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) {
  81. //dsm($item, '$item');
  82. //$term_matches[$item->tid] = check_plain($item->name);
  83. // $term_matches[check_plain($item->name)] = check_plain($item->name);
  84. // TODO: leave tags with nodes
  85. $term_matches[ trim(implode(' ', $adv_search_q[0]).' '.$last[1].$item->name)] = check_plain($item->name);
  86. $delta++;
  87. if($delta > 7)
  88. break;
  89. }
  90. drupal_json_output($term_matches);
  91. }else{
  92. drupal_json_output(array());
  93. }
  94. }else{
  95. return;
  96. }
  97. // dsm($term_matches, 'term_matches');
  98. // return 'debug mode of materio_search_api_autocomplete_searchapi';
  99. }
  100. /**
  101. * materio_search_api_results_search()
  102. *
  103. *
  104. */
  105. function materio_search_api_results_search(){
  106. global $user;
  107. //dsm("materio_search_api_results_search");
  108. $args = func_get_args();
  109. $typed = implode('/', $args);
  110. // dsm($typed, 'typed');
  111. preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
  112. //dsm($pages, '$pages');
  113. if($pages[0]){
  114. $typed = str_replace($pages[0][0], '', $typed);
  115. // $page = $pages[1][0];
  116. }
  117. // else{
  118. // $page = 0;
  119. // }
  120. preg_match_all('/\s?[^\s]+\s?/', $typed, $words);
  121. // dsm($words, "words");
  122. // $match = array('\\', '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '~', '*', '?', ':', '"', ';', ' ');
  123. // $replace = array('\\\\', '\\+', '\\-', '\\&', '\\|', '\\!', '\\(', '\\)', '\\{', '\\}', '\\[', '\\]', '\\^', '\\~', '\\*', '\\?', '\\:', '\\"', '\\;', '\\ ');
  124. foreach ($words[0] as $word) {
  125. // dsm($word);
  126. $word = trim($word);
  127. // $word = str_replace($match, $replace, $word);
  128. // dsm($word, 'word');
  129. $keys[] = $word;
  130. }
  131. // dsm($keys, 'keys');
  132. global $language;
  133. // if(user_access('use materio search api')){
  134. // }else if(user_access('use materio search api for breves')){
  135. // }
  136. if(user_access('use materio search api')){
  137. $index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
  138. $index = search_api_index_load($index_machine_name);
  139. }else if(user_access('use materio search api for breves')){
  140. $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
  141. // dsm($index_machine_name, '$index_machine_name');
  142. $index = search_api_index_load($index_machine_name);
  143. $could_index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
  144. $could_index = search_api_index_load($index_machine_name);
  145. }
  146. // $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
  147. // dsm($index_machine_name, '$index_machine_name');
  148. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  149. foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; }
  150. $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  151. // dsm($bundles_filter, 'bundles_filter');
  152. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  153. // dsm($viewmode, 'viewmode');
  154. if ($keys) {
  155. // TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
  156. try {
  157. $limit = variable_get($viewmode.'_limite', '10');
  158. $offset = pager_find_page() * $limit; //$page*$limit;//
  159. $query = search_api_query($index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
  160. ->keys(implode(' ', $keys))
  161. ->range($offset, $limit);
  162. $filter = $query->createFilter('OR');
  163. foreach ($bundles_filter as $type) {
  164. $filter->condition('type', $type, '=');
  165. }
  166. // dsm($filter, 'filter');
  167. $query->filter($filter);
  168. // $query->setOption('search_api_bypass_access', true);
  169. $query->setOption('search_api_access_account', $user);
  170. $results = $query->execute();
  171. # in case of utilisateur search, run a real search to indicate how much items you could find
  172. if(isset($could_index)){
  173. $could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
  174. ->keys(implode(' ', $keys))
  175. ->range($offset, $limit);
  176. // ->filter($filter);
  177. $could_results = $could_query->execute();
  178. }
  179. }
  180. catch (SearchApiException $e) {
  181. $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
  182. watchdog('materiobasemod', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  183. }
  184. // dsm($results, 'results');
  185. // $accessible_results = array();
  186. // foreach ($results['results'] as $nid => $entity) {
  187. // if(node_access('view', node_load($nid)))
  188. // $accessible_results[$nid] = $entity;
  189. // }
  190. // $results['results'] = $accessible_results;
  191. // $results['result count'] = count($accessible_results);
  192. if(user_access('use materio search api')){
  193. $items = $index->loadItems(array_keys($results['results']));
  194. // dsm($items, 'items');
  195. // $count = $results['result count'];
  196. }else if(user_access('use materio search api for breves')){
  197. $items = array();
  198. $breves = $index->loadItems(array_keys($results['results']));
  199. foreach ($breves as $nid => $breve) {
  200. if(!node_access('view', $breve))
  201. continue;
  202. $items[] = $breve;
  203. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  204. // dsm($materiaux, 'materiaux');
  205. if($materiaux){
  206. foreach ($materiaux as $value) {
  207. $materiau = node_load($value['target_id']);
  208. if(node_access('view', $materiau))
  209. $items[] = $materiau;
  210. }
  211. }
  212. }
  213. $results['breves count'] = $results['result count'];
  214. $results['result count'] = count($items);
  215. $results['could results'] = $could_results;
  216. }
  217. $ret['results']['#theme'] = 'materio_search_api_results';
  218. $ret['results']['#index'] = $index;//search_api_index_load($index_machine_name);
  219. $ret['results']['#results'] = $results;
  220. $ret['results']['#items'] = $items;
  221. $ret['results']['#view_mode'] = $viewmode;
  222. $ret['results']['#keys'] = $keys;
  223. drupal_set_title('<i class="icon-materio-search"></i>'.check_plain($typed), PASS_THROUGH);
  224. if(isset($results)){
  225. // Load pager.
  226. // if ($results['result count'] > $page->options['per_page']) {
  227. pager_default_initialize($results['result count'], $limit);
  228. $ret['results']['#pager'] = theme('pager');
  229. // }
  230. if (!empty($results['ignored'])) {
  231. drupal_set_message(
  232. t('The following search keys are too short or too common and were therefore ignored: "@list".',
  233. array( '@list' => implode(t('", "'), $results['ignored']) ) ),
  234. 'warning'
  235. );
  236. }
  237. if (!empty($results['warnings'])) {
  238. foreach ($results['warnings'] as $warning) {
  239. drupal_set_message($warning, 'warning');
  240. }
  241. }
  242. }
  243. }
  244. return $ret;
  245. }
  246. /**
  247. * materio_search_api_actuality()
  248. *
  249. */
  250. function materio_search_api_actuality(){
  251. global $user;
  252. if(isset($user->roles[1])){
  253. $date_limit = strtotime('-6 month');
  254. // dsm(date('d m y', $date_limit));
  255. }
  256. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  257. $limit = 10;//variable_get($viewmode.'_limite', '10');
  258. $offset = pager_find_page() * $limit;
  259. // dsm($offset);
  260. $query = new EntityFieldQuery;
  261. $query
  262. ->entityCondition('entity_type', 'node')
  263. ->propertyCondition('status', 1)
  264. ->entityCondition('bundle', array('breve'))
  265. ->propertyOrderBy('created', 'DESC')
  266. ->range($offset,$limit);
  267. if(isset($user->roles[1])){
  268. $query->propertyCondition('created', $date_limit, '>');
  269. }
  270. $result = $query->execute();
  271. // dsm($result, '$result');
  272. $count_query = new EntityFieldQuery;
  273. $count_query
  274. ->entityCondition('entity_type', 'node')
  275. ->propertyCondition('status', 1)
  276. ->entityCondition('bundle', array('breve'));
  277. // dsm($count, 'count');
  278. if(isset($user->roles[1])){
  279. $count_query->propertyCondition('created', $date_limit, '>');
  280. }
  281. $count = $count_query->count()->execute();
  282. pager_default_initialize($count, $limit);
  283. $items = array();
  284. if(isset($result['node'])){
  285. foreach ($result['node'] as $nid => $n) {
  286. $breve = node_load($nid);
  287. if(!node_access('view', $breve))
  288. continue;
  289. $items[] = $breve;
  290. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  291. // dsm($materiaux, 'materiaux');
  292. if($materiaux){
  293. foreach ($materiaux as $value) {
  294. $materiau = node_load($value['target_id']);
  295. if(node_access('view', $materiau))
  296. $items[] = $materiau;
  297. }
  298. }
  299. }
  300. }
  301. // drupal_set_title(t('Actualities'));
  302. drupal_set_title(t(''));
  303. return theme('materio_search_api_actuality', array(
  304. 'items' => $items,
  305. 'view_mode' => $viewmode,
  306. 'count' => $count,
  307. 'pager' => theme('pager'),
  308. ));
  309. }
  310. function materio_search_api_viewmode_change($vm){
  311. //dsm($vm, 'materio_search_api_viewmode_change');
  312. $rep = _materio_search_api_change_viewmode($vm);
  313. //return 'debug mode for materio_search_api_viewmode_change';
  314. // drupal_json_output($rep);
  315. drupal_goto();
  316. }