function get_search_str($where_clause = '', $search_str, $form_id = false, $fid = false)
 {
     global $frm_entry_meta;
     $where_item = '';
     $join = ' (';
     if (!is_array($search_str)) {
         $search_str = explode(" ", $search_str);
     }
     foreach ($search_str as $search_param) {
         $search_param = esc_sql(like_escape($search_param));
         if (!is_numeric($fid)) {
             $where_item .= empty($where_item) ? ' (' : ' OR';
             if (in_array($fid, array('created_at', 'user_id', 'updated_at'))) {
                 if ($fid == 'user_id' and !is_numeric($search_param)) {
                     $search_param = FrmProAppHelper::get_user_id_param($search_param);
                 }
                 $where_item .= " it.{$fid} like '%{$search_param}%'";
             } else {
                 $where_item .= " it.name like '%{$search_param}%' OR it.item_key like '%{$search_param}%' OR it.description like '%{$search_param}%' OR it.created_at like '%{$search_param}%'";
             }
         }
         if (empty($fid) or is_numeric($fid)) {
             $where_entries = "(meta_value LIKE '%{$search_param}%'";
             if ($data_fields = FrmProForm::has_field('data', $form_id, false)) {
                 $df_form_ids = array();
                 //search the joined entry too
                 foreach ((array) $data_fields as $df) {
                     $df->field_options = maybe_unserialize($df->field_options);
                     if (is_numeric($df->field_options['form_select'])) {
                         $df_form_ids[] = $df->field_options['form_select'];
                     }
                     unset($df);
                 }
                 unset($data_fields);
                 global $wpdb, $frmdb;
                 $data_form_ids = $wpdb->get_col("SELECT form_id FROM {$frmdb->fields} WHERE id in (" . implode(',', $df_form_ids) . ")");
                 unset($df_form_ids);
                 if ($data_form_ids) {
                     $data_entry_ids = $frm_entry_meta->getEntryIds("fi.form_id in (" . implode(',', $data_form_ids) . ") and meta_value LIKE '%" . $search_param . "%'");
                     if (!empty($data_entry_ids)) {
                         $where_entries .= " OR meta_value in (" . implode(',', $data_entry_ids) . ")";
                     }
                 }
                 unset($data_form_ids);
             }
             $where_entries .= ")";
             if (is_numeric($fid)) {
                 $where_entries .= " AND fi.id={$fid}";
             }
             $meta_ids = $frm_entry_meta->getEntryIds($where_entries);
             if (!empty($meta_ids)) {
                 if (!empty($where_clause)) {
                     $where_clause .= " AND" . $join;
                     if (!empty($join)) {
                         $join = '';
                     }
                 }
                 $where_clause .= " it.id in (" . implode(',', $meta_ids) . ")";
             } else {
                 if (!empty($where_clause)) {
                     $where_clause .= " AND" . $join;
                     if (!empty($join)) {
                         $join = '';
                     }
                 }
                 $where_clause .= " it.id=0";
             }
         }
     }
     if (!empty($where_item)) {
         $where_item .= ')';
         if (!empty($where_clause)) {
             $where_clause .= empty($fid) ? ' OR' : ' AND';
         }
         $where_clause .= $where_item;
         if (empty($join)) {
             $where_clause .= ')';
         }
     } else {
         if (empty($join)) {
             $where_clause .= ')';
         }
     }
     return $where_clause;
 }
Esempio n. 2
0
 function get_search_ids($s, $form_id)
 {
     global $wpdb, $frmdb, $frm_entry_meta;
     if (empty($s)) {
         return false;
     }
     $s = stripslashes($s);
     preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
     $search_terms = array_map('_search_terms_tidy', $matches[0]);
     $n = '%';
     //!empty($q['exact']) ? '' : '%';
     $p_search = $search = '';
     $search_or = '';
     $data_field = FrmProForm::has_field('data', $form_id, false);
     foreach ((array) $search_terms as $term) {
         $term = esc_sql(like_escape($term));
         $p_search .= " AND (({$wpdb->posts}.post_title LIKE '{$n}{$term}{$n}') OR ({$wpdb->posts}.post_content LIKE '{$n}{$term}{$n}'))";
         $search .= "{$search_or}meta_value LIKE '{$n}{$term}{$n}'";
         $search_or = ' OR ';
         if ($data_field) {
             $df_form_ids = array();
             //search the joined entry too
             foreach ((array) $data_field as $df) {
                 $df->field_options = maybe_unserialize($df->field_options);
                 if (is_numeric($df->field_options['form_select'])) {
                     $df_form_ids[] = $df->field_options['form_select'];
                 }
                 unset($df);
             }
             global $wpdb, $frmdb;
             $data_form_ids = $wpdb->get_col("SELECT form_id FROM {$frmdb->fields} WHERE id in (" . implode(',', $df_form_ids) . ")");
             unset($df_form_ids);
             if ($data_form_ids) {
                 $data_entry_ids = $frm_entry_meta->getEntryIds("fi.form_id in (" . implode(',', $data_form_ids) . ") and meta_value LIKE '%" . $term . "%'");
                 if ($data_entry_ids) {
                     $search .= "{$search_or}meta_value in (" . implode(',', $data_entry_ids) . ")";
                 }
             }
             unset($data_form_ids);
         }
     }
     $p_ids = '';
     $matching_posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE 1=1 {$p_search}");
     if ($matching_posts) {
         $p_ids = $wpdb->get_col("SELECT id from {$frmdb->entries} WHERE post_id in (" . implode(',', $matching_posts) . ") AND form_id='{$form_id}'");
         $p_ids = $p_ids ? " OR item_id in (" . implode(',', $p_ids) . ")" : '';
     }
     return $frm_entry_meta->getEntryIds("(({$search}){$p_ids}) and fi.form_id='{$form_id}'");
 }