materio_search_api.pages.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 > 15)
  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. //dsm("materio_search_api_results_search");
  107. // retreive typed words separated by slashes as a sentence
  108. $keys = func_get_args();
  109. // dsm($args, 'args');
  110. if($keys[0] == "advanced"){
  111. $advanced = true;
  112. array_shift($keys);
  113. // dsm($keys, 'shifted keys');
  114. // foreach ($args as $arg) {
  115. // $typed[] = $arg;//(integer)$arg;
  116. // }
  117. $typed = implode(' +', $keys);
  118. }else{
  119. $typed = implode('/', $keys);
  120. }
  121. // remove query page params
  122. preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
  123. //dsm($pages, '$pages');
  124. if($pages[0]){
  125. $typed = str_replace($pages[0][0], '', $typed);
  126. }
  127. // dsm($typed, 'typed');
  128. global $language;
  129. global $user;
  130. if($advanced && user_access('use materio search api advanced search')){
  131. // dsm('advanced search');
  132. $index_machine_name = variable_get('advancedsearchindex_'.$language->language, -1);
  133. // dsm($index_machine_name, '$index_machine_name');
  134. $index = search_api_index_load($index_machine_name);
  135. }
  136. else if(user_access('use materio search api')){
  137. // dsm('normal search');
  138. # switch index depending on key words type full text or taxonomy term (autocomplete selection)
  139. $searchmode = isset($user->data['materiosearchapi_searchmode']) ? $user->data['materiosearchapi_searchmode'] : "fulltext";
  140. switch($searchmode){
  141. case "fulltext":
  142. default:
  143. $index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
  144. break;
  145. // case "taxonomy":
  146. // $index_machine_name = variable_get('taxonomysearchindex_'.$language->language, -1);
  147. // break;
  148. }
  149. $index = search_api_index_load($index_machine_name);
  150. }
  151. else if(user_access('use materio search api for breves')){
  152. // dsm('limited search');
  153. $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
  154. // dsm($index_machine_name, '$index_machine_name');
  155. $index = search_api_index_load($index_machine_name);
  156. }
  157. if(!user_access('use materio search api advanced search') && !user_access('use materio search api')){
  158. # potential results index for anonymous and free user
  159. $could_index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
  160. $could_index = search_api_index_load($index_machine_name);
  161. }
  162. if ($typed) {
  163. # TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
  164. try {
  165. # retrieve viewmode and then use it to define the query range
  166. $viewmode = isset($user->data['materiosearchapi_viewmode'])
  167. ? $user->data['materiosearchapi_viewmode']
  168. : variable_get('defaultviewmode', 'full');
  169. $limit = variable_get($viewmode.'_limite', '10');
  170. $offset = pager_find_page() * $limit; //$page*$limit;//
  171. if(isset($index)){
  172. # define default bundle option (materiaux, breves)
  173. $default_bundles = array();
  174. if(isset($index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'])){
  175. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  176. foreach ($indexed_bundles as $bundle) { $default_bundles[] = $bundle; }
  177. }
  178. # choose solr query bundle option
  179. $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter'])
  180. ? $user->data['materiosearchapi_bundlesfilter']
  181. : $default_bundles;
  182. # choose solr query options
  183. $query_options = $advanced
  184. ? array('conjunction'=>'AND', 'parse mode'=>'direct')
  185. : array('conjunction'=>'OR', 'parse mode'=>'direct');
  186. #create the solr query
  187. $query = search_api_query($index_machine_name, $query_options)
  188. ->keys($typed)
  189. ->range($offset, $limit);
  190. # apply bundle options to solr query if usefull
  191. if(count($bundles_filter)){
  192. $filter = $query->createFilter('OR');
  193. foreach ($bundles_filter as $type)
  194. $filter->condition('type', $type, '=');
  195. $query->filter($filter);
  196. }
  197. // $query->setOption('search_api_bypass_access', true);
  198. # add user access solr query option
  199. $query->setOption('search_api_access_account', $user);
  200. # execute solr query and record results
  201. $results = $query->execute();
  202. }
  203. # in case of free user search, run a real search to indicate how much items you could find
  204. if(isset($could_index)){
  205. $could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
  206. // ->keys(implode(' ', $keys))
  207. ->keys($typed)
  208. ->range($offset, $limit);
  209. // ->filter($filter);
  210. $could_results = $could_query->execute();
  211. // dsm($could_results, 'could_results');
  212. }
  213. }
  214. catch (SearchApiException $e) {
  215. $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
  216. watchdog('materiobasemod', 'search error: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  217. }
  218. // dsm($results, 'results');
  219. if(user_access('use materio search api for breves')
  220. || user_access('use materio search api')
  221. || user_access('use materio search api advanced search'))
  222. {
  223. if(user_access('use materio search api') || user_access('use materio search api advanced search')){
  224. if(is_array($results['results'])){
  225. $items = $index->loadItems(array_keys($results['results']));
  226. }else{
  227. $items = array();
  228. }
  229. // dsm($items, 'items');
  230. }
  231. else{
  232. $items = array();
  233. $breves = $index->loadItems(array_keys($results['results']));
  234. foreach ($breves as $nid => $breve) {
  235. if(!node_access('view', $breve))
  236. continue;
  237. $items[] = $breve;
  238. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  239. // dsm($materiaux, 'materiaux');
  240. if($materiaux){
  241. foreach ($materiaux as $value) {
  242. if(!isset($could_results['results'][$value['target_id']]))
  243. continue;
  244. $materiau = node_load($value['target_id']);
  245. if(node_access('view', $materiau))
  246. $items[] = $materiau;
  247. }
  248. }
  249. }
  250. $results['breves count'] = $results['result count'];
  251. $results['result count'] = count($items);
  252. $results['could results'] = $could_results;
  253. }
  254. $ret['results']['#index'] = $index;//search_api_index_load($index_machine_name);
  255. $ret['results']['#items'] = $items;
  256. }
  257. else{
  258. // for anonymous
  259. $results['could results'] = $could_results;
  260. }
  261. // dsm($typed, 'typed');
  262. // for all case
  263. $ret['results']['#results'] = $results;
  264. $ret['results']['#theme'] = 'materio_search_api_results';
  265. $ret['results']['#keys'] = $typed;
  266. $ret['results']['#view_mode'] = $viewmode;
  267. // page title
  268. if($advanced){
  269. foreach($keys as $tid){
  270. $t = taxonomy_term_load($tid);
  271. $ptk[] = $t->name;
  272. }
  273. $page_title = implode(' +', $ptk);
  274. }
  275. else{
  276. $page_title = $typed;
  277. }
  278. drupal_set_title('<i class="icon-materio-search"></i>'.check_plain($page_title), PASS_THROUGH);
  279. // render results
  280. if(isset($results)){
  281. // Load pager.
  282. // if ($results['result count'] > $page->options['per_page']) {
  283. pager_default_initialize($results['result count'], $limit);
  284. $ret['results']['#pager'] = theme('pager');
  285. // }
  286. if (!empty($results['ignored'])) {
  287. drupal_set_message(
  288. t('The following search keys are too short or too common and were therefore ignored: "@list".',
  289. array( '@list' => implode(t('", "'), $results['ignored']) ) ),
  290. 'warning'
  291. );
  292. }
  293. if (!empty($results['warnings'])) {
  294. foreach ($results['warnings'] as $warning) {
  295. drupal_set_message($warning, 'warning');
  296. }
  297. }
  298. }
  299. }
  300. return $ret;
  301. }
  302. /**
  303. * materio_search_api_actuality()
  304. *
  305. */
  306. function materio_search_api_actuality(){
  307. global $user;
  308. if(isset($user->roles[1])){
  309. $date_limit = strtotime('-6 month');
  310. // dsm(date('d m y', $date_limit));
  311. }
  312. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  313. $limit = 10;//variable_get($viewmode.'_limite', '10');
  314. $offset = pager_find_page() * $limit;
  315. // dsm($offset);
  316. $query = new EntityFieldQuery;
  317. $query
  318. ->entityCondition('entity_type', 'node')
  319. ->propertyCondition('status', 1)
  320. ->entityCondition('bundle', array('breve'))
  321. ->propertyOrderBy('created', 'DESC')
  322. ->range($offset,$limit);
  323. if(isset($user->roles[1])){
  324. $query->propertyCondition('created', $date_limit, '>');
  325. }
  326. $result = $query->execute();
  327. // dsm($result, '$result');
  328. $count_query = new EntityFieldQuery;
  329. $count_query
  330. ->entityCondition('entity_type', 'node')
  331. ->propertyCondition('status', 1)
  332. ->entityCondition('bundle', array('breve'));
  333. // dsm($count, 'count');
  334. if(isset($user->roles[1])){
  335. $count_query->propertyCondition('created', $date_limit, '>');
  336. }
  337. $count = $count_query->count()->execute();
  338. pager_default_initialize($count, $limit);
  339. $items = array();
  340. if(isset($result['node'])){
  341. foreach ($result['node'] as $nid => $n) {
  342. $breve = node_load($nid);
  343. if(!node_access('view', $breve))
  344. continue;
  345. $items[] = $breve;
  346. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  347. // dsm($materiaux, 'materiaux');
  348. if($materiaux){
  349. foreach ($materiaux as $value) {
  350. $materiau = node_load($value['target_id']);
  351. if(node_access('view', $materiau))
  352. $items[] = $materiau;
  353. }
  354. }
  355. }
  356. }
  357. // drupal_set_title(t('Actualities'));
  358. drupal_set_title(t(''));
  359. return theme('materio_search_api_actuality', array(
  360. 'items' => $items,
  361. 'view_mode' => $viewmode,
  362. 'count' => $count,
  363. 'pager' => theme('pager'),
  364. ));
  365. }
  366. function materio_search_api_viewmode_change($vm){
  367. //dsm($vm, 'materio_search_api_viewmode_change');
  368. $rep = _materio_search_api_change_viewmode($vm);
  369. //return 'debug mode for materio_search_api_viewmode_change';
  370. // drupal_json_output($rep);
  371. drupal_goto();
  372. }