$view = 'search';
- $sql = "SELECT * FROM $this->table";
$year_ranges = new glmObitSupportFunctions($this->wpdb, $this->config);
$birth_death_range = array('birth' => $year_ranges->getObitYearRange(BIRTH_YEAR_START), 'death' => $year_ranges->getObitYearRange(DEATH_YEAR_START));
$logic_dropdown = array('Contains' => 'LIKE' ,'Starts With' => 'LIKE%','Equals' => '=', 'More than' => '>', 'Less than' => '<');
$per_page = isset( $_REQUEST['results-limit']) ? (int)filter_var( $_REQUEST['results-limit'], FILTER_SANITIZE_STRING) : 50;
-// echo "PER PAGE:" . $per_page;
-
- $form_data = $_POST;
-
- // build array of potential search fields , will be used in the paging urls
- $form_fields = array(
- 'last' => filter_var($form_data['last_name'],FILTER_SANITIZE_STRING),
- 'first' => filter_var($form_data['first_mid'],FILTER_SANITIZE_STRING),
- 'b_yr' => filter_var($form_data['b_year'],FILTER_SANITIZE_STRING),
- 'b_range' => filter_var($form_data['b_yr_range'],FILTER_SANITIZE_STRING),
- 'd_yr' => filter_var($form_data['d_year'],FILTER_SANITIZE_STRING),
- 'd_range' => filter_var($form_data['d_yr_range'],FILTER_SANITIZE_STRING),
- 'spouse' => filter_var($form_data['spouse_partner'],FILTER_SANITIZE_STRING),
- 'maiden' => filter_var($form_data['maiden_other'],FILTER_SANITIZE_STRING)
- );
-
- $get_fields = array(
- 'last_name' => filter_var($_GET['last'],FILTER_SANITIZE_STRING),
- 'first_mid' => filter_var($_GET['first'],FILTER_SANITIZE_STRING),
- 'b_year' => filter_var($_GET['b_yr'],FILTER_SANITIZE_STRING),
- 'b_yr_range' => filter_var($_GET['b_range'],FILTER_SANITIZE_STRING),
- 'd_year' => filter_var($_GET['d_yr'],FILTER_SANITIZE_STRING),
- 'd_yr_range' => filter_var($_GET['d_range'],FILTER_SANITIZE_STRING),
- 'spouse_partner' => filter_var($_GET['spouse'],FILTER_SANITIZE_STRING),
- 'maiden_other' => filter_var($_GET['maiden'],FILTER_SANITIZE_STRING)
- );
- // get the alpha key
- if ( isset( $_REQUEST['alpha'] ) ){
- $alpha_key = filter_var( $_REQUEST['alpha'], FILTER_SANITIZE_STRING );
- }
+
- // create the any condition or all conditions query logic
- if( isset( $_REQUEST['conditions'] ) && $_REQUEST['conditions'][0] == 'any'){
- $clause = ' OR ';
- } else if ( isset( $_REQUEST['conditions'] ) && $_REQUEST['conditions'][0] == 'all') {
- $clause = ' AND ';
- }
+ // only execute search if the search button is clicked or there are search params in the url
+ if ( isset( $_REQUEST['searchButton'] ) || !empty( $_GET['alpha'] ) || !empty( $_GET['pageSelect'] ) ){
- // separate the search fields from the logic ('not', 'contains, 'equals') etc.
- foreach($form_data as $key=>$value){
- if (isset($value) && $value !== ''){
- if( $value !== 'Search' && $key !== 'conditions' && $key !== 'results-limit'){
- if(strpos($key,'-logic') === false && strpos($key,'not-') === false ) {
- $value = filter_var($value, FILTER_SANITIZE_STRING);
- $search_fields[$key] = $value;
- } else {
- $search_options[$key] = $value;
- }
+ $form_data = $_POST;
+
+ // build array of potential search fields , will be used in the paging urls
+ $form_fields = array(
+ 'last' => filter_var($form_data['last_name'],FILTER_SANITIZE_STRING),
+ 'first' => filter_var($form_data['first_mid'],FILTER_SANITIZE_STRING),
+ 'b_yr' => filter_var($form_data['b_year'],FILTER_SANITIZE_STRING),
+ 'b_range' => filter_var($form_data['b_yr_range'],FILTER_SANITIZE_STRING),
+ 'd_yr' => filter_var($form_data['d_year'],FILTER_SANITIZE_STRING),
+ 'd_range' => filter_var($form_data['d_yr_range'],FILTER_SANITIZE_STRING),
+ 'spouse' => filter_var($form_data['spouse_partner'],FILTER_SANITIZE_STRING),
+ 'maiden' => filter_var($form_data['maiden_other'],FILTER_SANITIZE_STRING)
+ );
+
+ $get_fields = array(
+ 'last_name' => filter_var($_GET['last'],FILTER_SANITIZE_STRING),
+ 'first_mid' => filter_var($_GET['first'],FILTER_SANITIZE_STRING),
+ 'b_year' => filter_var($_GET['b_yr'],FILTER_SANITIZE_STRING),
+ 'b_yr_range' => filter_var($_GET['b_range'],FILTER_SANITIZE_STRING),
+ 'd_year' => filter_var($_GET['d_yr'],FILTER_SANITIZE_STRING),
+ 'd_yr_range' => filter_var($_GET['d_range'],FILTER_SANITIZE_STRING),
+ 'spouse_partner' => filter_var($_GET['spouse'],FILTER_SANITIZE_STRING),
+ 'maiden_other' => filter_var($_GET['maiden'],FILTER_SANITIZE_STRING)
+ );
+ // get the alpha key
+ if ( isset( $_REQUEST['alpha'] ) ){
+ $alpha_key = filter_var( $_REQUEST['alpha'], FILTER_SANITIZE_STRING );
+ }
+
+ // create the any condition or all conditions query logic
+ if( isset( $_REQUEST['conditions'] ) && $_REQUEST['conditions'][0] == 'any'){
+ $clause = ' OR ';
+ } else if ( isset( $_REQUEST['conditions'] ) && $_REQUEST['conditions'][0] == 'all') {
+ $clause = ' AND ';
+ }
+
+ // separate the search fields from the logic ('not', 'contains, 'equals') etc.
+ foreach($form_data as $key=>$value){
+ if (isset($value) && $value !== ''){
+ if( $value !== 'Search' && $key !== 'conditions' && $key !== 'results-limit'){
+ if(strpos($key,'-logic') === false && strpos($key,'not-') === false ) {
+ $value = filter_var($value, FILTER_SANITIZE_STRING);
+ $search_fields[$key] = $value;
+ } else {
+ $search_options[$key] = $value;
+ }
+ }
+ } else {
+ // populate an array for each empty search value
+ $empty_values[] = 'void';
}
+ }
+
+ // check to see if the GET query params search array is empty
+ foreach($get_fields as $key=>$val){
+ if( $val !== '' && isset($val)){
+ $get_values[$key] = $val;
+ }
+ }
+
+
+ // check to see if there are 8 empty values, if there are, there are no search values
+ if(count($empty_values) === 8){
+ $empty_search = 'true';
} else {
- // populate an array for each empty search value
- $empty_values[] = 'void';
- }
- }
+ $empty_search = 'false';
+ }
+
+ // build the initial sql query based on the search fields and search options selected
+ foreach($search_fields as $field => $f){
+
+ foreach($search_options as $option => $o){
- // check to see if the GET query params search array is empty
- foreach($get_fields as $key=>$val){
- if( $val !== '' && isset($val)){
- $get_values[$key] = $val;
+ if( strpos($option, $field) !== false){
+
+ if(strpos($option, 'not') !== false){
+
+ $o = '<>';
+ $search_fields[$field] = $field . " $o " . "'$f'";
+
+ break;
+
+ } else if(strpos($o, '%') !== false){
+ $o = rtrim($o, "%");
+ $search_fields[$field] = $field . " $o " . "'$f%'";
+ } else if($o === 'LIKE') {
+ $search_fields[$field] = $field . " $o " . "'%$f%'";
+ } else {
+ $search_fields[$field] = $field . " $o " . "'$f'";
+ }
+ }
+ }
}
- }
-
-
- // check to see if there are 8 empty values, if there are, there are no search values
- if(count($empty_values) === 8){
- $empty_search = 'true';
- } else {
- $empty_search = 'false';
- }
+
+ // generate the WHERE clause string from the array of options
+ implode($clause, $search_fields);
+
+ // check the GET variables for sql queries with paging
+ if( empty( $search ) && empty( $alpha_key) && empty($get_values) ){
+
+ $where = "";
+
+ } else if( !empty( $alpha_key ) ) {
+
+ $where = "last_name LIKE '$alpha_key%'";
- // build the initial sql query based on the search fields populated
- foreach($search_fields as $field => $f){
-
- foreach($search_options as $option => $o){
-
- if( strpos($option, $field) !== false){
-
- if(strpos($option, 'not') !== false){
-
- $o = '<>';
- $search_fields[$field] = $field . " $o " . "'$f'";
-
- break;
-
- } else if(strpos($o, '%') !== false){
- $o = rtrim($o, "%");
- $search_fields[$field] = $field . " $o " . "'$f%'";
- } else if($o === 'LIKE') {
- $search_fields[$field] = $field . " $o " . "'%$f%'";
- } else {
- $search_fields[$field] = $field . " $o " . "'$f'";
+ } else if( !empty( $get_values ) ){
+
+ foreach($get_values as $key=>$value){
+
+ if($value !== ''){
+ $where .= "$key LIKE '%$value%' AND ";
}
+
}
+ // remove the last AND or OR statement from the query string
+ $where = substr($where, 0, -5);
+
+ }else {
+
+ $where = $search;
}
- }
- $search = implode($clause, $search_fields);
-
- // check the GET variables for sql queries with paging
- if( empty( $search ) && empty( $alpha_key) && empty($get_values) ){
- $where = "";
- } else if( !empty( $alpha_key ) ) {
- $where = "last_name LIKE '$alpha_key%'";
-
- } else if( !empty( $get_values ) ){
- foreach($get_values as $key=>$value){
- if($value !== ''){
- $where .= "$key LIKE '%$value%' AND ";
+ // begin paging
+ if (isset($_REQUEST['pageSelect'])) {
+
+ // If request is for Next
+ if ($_REQUEST['pageSelect'][0] == 'N') {
+ $newStart = $_REQUEST['nextStart'] - 0;
+
+ // Otherwise it must be Previous
+ } else {
+ $newStart = $_REQUEST['prevStart'] - 0;
+ }
+
+ if ($newStart > 0) {
+ $start = $newStart;
}
}
- $where = substr($where, 0, -5);
- }else {
- $where = $search;
- }
-
- if (isset($_REQUEST['pageSelect'])) {
- // If request is for Next
- if ($_REQUEST['pageSelect'][0] == 'N') {
- $newStart = $_REQUEST['nextStart'] - 0;
+ $obits = $this->getList($where, 'last_name', $fieldVals = true, $idField = 'id', $start, $per_page);
+
+ $returned = $obits['returned'];
+
+ // Get paging results
+ $numbDisplayed = $obits['returned'];
+
+ $lastDisplayed = $obits['last'];
- // Otherwise it must be Previous
+ if ($start == 1) {
+ $prevStart = false;
} else {
- $newStart = $_REQUEST['prevStart'] - 0;
+ $prevStart = $start - $per_page;
+ if ($start < 1) {
+ $start = 1;
+ }
}
-
- if ($newStart > 0) {
- $start = $newStart;
+ if ($obits['returned'] == $per_page) {
+ $nextStart = $start + $per_page;
}
- }
-
- $obits = $this->getList($where, 'last_name', $fieldVals = true, $idField = 'id', $start, $per_page);
-// echo '<pre>', print_r($obits), '</pre>';
- $returned = $obits['returned'];
- // Get paging results
- $numbDisplayed = $obits['returned'];
-
- $lastDisplayed = $obits['last'];
-
- if ($start == 1) {
- $prevStart = false;
- } else {
- $prevStart = $start - $per_page;
- if ($start < 1) {
- $start = 1;
+
+
+ // get the total number of results returned from the query depending on the WHERE clause
+ if( empty( $where ) ){
+ $get_rows = "SELECT COUNT(*) FROM $this->table";
+ } else {
+ $get_rows = "SELECT COUNT(*) FROM $this->table WHERE $where";
}
- }
- if ($obits['returned'] == $per_page) {
- $nextStart = $start + $per_page;
- }
+
+ $rows = $this->wpdb->get_results($get_rows, 'ARRAY_A');
- if( empty( $where ) ){
- $get_rows = "SELECT COUNT(*) FROM $this->table";
- } else {
- $get_rows = "SELECT COUNT(*) FROM $this->table WHERE $where";
- }
-
-
- $rows = $this->wpdb->get_results($get_rows, 'ARRAY_A');
-
- $total_records = $rows[0]['COUNT(*)'];
-
- $alphaList = false;
- $alphaWhere = '';
- if ($this->config['settings']['list_show_search_alpha']) {
+ $total_records = $rows[0]['COUNT(*)'];
- $alphaSelected = false;
+ $alphaList = false;
+
+ if ($this->config['settings']['list_show_search_alpha']) {
- // Check for alpha selected
- if ($actionData['request']['alpha'] && strlen($actionData['request']['alpha']) == 1) {
- $alphaSelected = strtoupper($actionData['request']['alpha']);
- $alphaWhere .= " AND T.member_name LIKE '$alphaSelected%'";
- }
+ $alphaSelected = false;
+
+ // Check for alpha selected
+ if ($actionData['request']['alpha'] && strlen($actionData['request']['alpha']) == 1) {
+ $alphaSelected = strtoupper($actionData['request']['alpha']);
+ }
- // Get full list for all other filters, but not filtered by alpha (that would be silly)
- $alphaList = $this->getAlphaList(' AND '.$where, $alphaSelected);
+ // Get full list for all other filters, but not filtered by alpha (that would be silly)
+ $alphaList = $this->getAlphaList(' AND '.$where, $alphaSelected);
+ }
}
-// print_r($alphaList);
if( isset($_REQUEST['searchButton']) || isset( $_REQUEST['pageSelect'] ) || isset($_REQUEST['alpha'] ) ){
$view = 'searchResults';
}
+
// Compile template data
$templateData = array(