PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/LP/RecordSearch.php

https://github.com/radicaldesigns/jaguar
PHP | 103 lines | 73 code | 21 blank | 9 comment | 11 complexity | eecf09f23dce13cb597a62de89cece04 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. class RecordSearch {
  3. var $_source;
  4. var $_dbcon;
  5. var $_exact_value_fields = array( );
  6. function __construct( &$source ) {
  7. $this->_source = &$source;
  8. $this->_dbcon = &$source->dbcon;
  9. }
  10. function setExactValues() {
  11. $this->_exact_value_fields = $this->_source->getLiteralCriteria();
  12. $this->_exact_value_fields[] = $this->_source->id_field ;
  13. }
  14. function &getSource( ){
  15. return $this->_source;
  16. }
  17. function applyValues( $data ) {
  18. if ( !( isset( $data ) && is_array( $data ))) return false;
  19. foreach ($data as $key => $value) {
  20. $crit_method = '_addCriteria' . ucfirst( $key );
  21. if (method_exists( $this, $crit_method )) {
  22. $this->$crit_method( $value );
  23. continue;
  24. }
  25. $crit_method = substr( $crit_method, 1 );
  26. if ( method_exists( $this->_source, $crit_method )) {
  27. $this->_source->$crit_method( $value );
  28. continue;
  29. }
  30. if ( $crit_method = $this->_getCriteriaMethod( $key )){
  31. $this->$crit_method( $key, $value );
  32. }
  33. }
  34. }
  35. function _getCriteriaMethod( $fieldname ) {
  36. if ( !$this->_source->isColumn( $fieldname )) return false;
  37. if (array_search( $fieldname, $this->_exact_value_fields ) !==FALSE) return '_addCriteriaEquals';
  38. return '_addCriteriaContains';
  39. }
  40. function _addCriteriaContains( $key, $value ) {
  41. $sql_criterion = $key . ' LIKE ' . $this->_dbcon->qstr( '%' . $value . '%' );
  42. $this->_source->addCriteria( $sql_criterion );
  43. }
  44. function _addCriteriaEquals( $key, $value ) {
  45. $sql_criterion = $key . ' = ' . $this->_dbcon->qstr( $value );
  46. $this->_source->addCriteria( $sql_criterion );
  47. }
  48. function _addCriteriaAMPSearch( $value ) {
  49. }
  50. function getRelatedSetCriteria( &$set, $external_key, $id_field = null ) {
  51. if ( !isset( $id_field )) $id_field = $this->_source->id_field;
  52. $relatedSetCriteria = 'FALSE';
  53. $allowed_ids = &$set->getLookup( $external_key );
  54. if ( !empty( $allowed_ids )) $relatedSetCriteria = $id_field . ' in ( '. join( ", ", $allowed_ids ). ')';
  55. return $relatedSetCriteria;
  56. }
  57. /**
  58. * extract quoted phrases
  59. *
  60. * method kudos to insipience.com
  61. *
  62. * @param string $search_string A value to be parsed for quoted phrases
  63. * @access public
  64. * @return array a set of phrases to be searched
  65. */
  66. function separateSearchPhrases( $search_string ) {
  67. if ( !( substr_count( $search_string, '"') >= 2) ) return AMP_removeBlankElements( split(' ', $search_string ));
  68. preg_match_all("/\"([\w\s]+)(\"|$)/", $search_string, $result_phrases, PREG_PATTERN_ORDER);
  69. $quoted_phrases = $result_phrases[1];
  70. $single_terms = explode(" ", preg_replace("/\"[\w\s]*(\"|$)/", "", $search_string));
  71. return AMP_removeBlankElements( array_merge( $single_terms , $quoted_phrases ));
  72. }
  73. function getCriteriaFulltext( $search_string ) {
  74. $fulltext_fields = $this->_source->getFullTextFields( );
  75. if ( empty ( $fulltext_fields )) {
  76. trigger_error( 'No fulltext search fields have been defined for ' . get_class( $this->_source ));
  77. return false;
  78. }
  79. return "MATCH ( " . join( ",", $fulltext_fields ) . " ) AGAINST ( ". $this->_dbcon->qstr( $search_string ) ." )";
  80. }
  81. }
  82. ?>