materio_search_api.pages.inc 14 KB

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