Exemplo n.º 1
0
 public function do_search($params = array())
 {
     global $wpdb;
     $params = array_merge(array('return_type' => ARRAY_A, 'posts_per_page' => 10, 'debug' => false, 'orderby_options' => array('latest' => array('sql' => 'p.post_date DESC'))), $params);
     if ($this->request->empty_GET(TF_SEEK_HELPER::get_search_parameter('page'))) {
         $curr_page = 1;
     } else {
         $curr_page = (int) $this->request->GET(TF_SEEK_HELPER::get_search_parameter('page'));
         if ($curr_page < 1) {
             $curr_page = 1;
         }
     }
     $forms_options = $this->get->ext_options($this->_the_class_name, 'forms');
     $form_id = $this->request->GET($this->get_search_parameter('form_id'));
     if (!isset($forms_options[$form_id])) {
         $form_id = '';
         $built_where = array('sql' => '');
         $form_id = NULL;
     } else {
         $built_where = $this->build_form_search_where_sql($form_id);
     }
     $where_sql = trim($built_where['sql']);
     $where = $where_sql ? " AND " . $where_sql : "";
     // Build ORDER BY
     $order_by_sql = '';
     //
     $order_by_key = TF_SEEK_HELPER::get_input_value(TF_SEEK_HELPER::get_search_parameter('orderby'), '');
     if (isset($params['orderby_options'][$order_by_key])) {
         $order_by_sql = $params['orderby_options'][$order_by_key]['sql'];
     } elseif (sizeof($params['orderby_options'])) {
         $first_value = reset($params['orderby_options']);
         $order_by_sql = $first_value['sql'];
     }
     if ($order_by_sql) {
         $order_by_sql = ' ORDER BY ' . $order_by_sql . ' ';
     }
     // ^end Buld ORDER BY
     //ini_set("mysql.trace_mode", "0"); // for FOUND_ROWS() to work
     $sql = 'SELECT
         SQL_CALC_FOUND_ROWS *
             ' . $this->get_search_sql() . '
             ' . $where . '
         GROUP BY p.ID
         ' . $order_by_sql . '
         LIMIT ' . ($curr_page - 1) * $params['posts_per_page'] . ',' . $params['posts_per_page'];
     $sql = apply_filters('tf_filter_seek_search_sql', $sql, $params, $form_id);
     if ($params['debug']) {
         tf_print($sql);
     }
     $rows = $wpdb->get_results($sql, $params['return_type']);
     $tmp = $wpdb->get_row('SELECT FOUND_ROWS() as total_rows', ARRAY_A);
     $total = reset($tmp);
     $max_pages = intval($total / $params['posts_per_page']);
     if ($total % $params['posts_per_page']) {
         $max_pages++;
     }
     if ($curr_page > 1 && !($rows_on_page = count($rows))) {
         $curr_page = $max_pages;
     }
     $ret = array('rows' => $rows, 'total' => $total, 'curr_page' => $curr_page, 'max_pages' => $max_pages);
     do_action('tf_ext_seek_do_search');
     return $ret;
 }
Exemplo n.º 2
0
 /**
  * Use this instead creating manual sql for simple deletes
  * First argument: array returned by ::map()
  * Second argument: array with conditions
  */
 public static function delete($map, $options, $debug = false)
 {
     /** @var wpdb $wpdb */
     global $wpdb;
     // Options array structure
     // ! Be sure that in $options are safe sql strings (sql injections... use $wpdb->prepare(...))
     array('_group' => 'my_group_name', 'where' => array('mappedColumnsAliasKey1' => "=7", 'mappedColumnsAliasKey2' => "='foo'", 'mappedColumnsAliasKey3' => "LIKE 'bar%'"), 'suffix' => '...');
     // Verify $options structure
     if (!isset($options['_group'])) {
         trigger_error(__('Required option "_group" not found in $options', 'tfuse'), E_USER_ERROR);
     }
     // Generate update sql
     $sql = "DELETE FROM " . self::getTableName();
     $sql .= "\n  WHERE _group";
     if (is_array($options['_group'])) {
         foreach ($options['_group'] as $key => $_group) {
             $options['_group'][$key] = $wpdb->prepare('%s', $_group);
         }
         $sql .= " IN (" . implode(',', $options['_group']) . ")";
     } else {
         $sql .= " = " . $wpdb->prepare('%s', $options['_group']);
     }
     $sql .= " ";
     if (isset($options['where'])) {
         if (is_array($options['where'])) {
             $where = array();
             foreach ($options['where'] as $alias => $sqlWhere) {
                 if (in_array($alias, self::$reservedColumns)) {
                     $field = $alias;
                 } elseif (isset($map[$alias])) {
                     $field = $map[$alias];
                 } else {
                     trigger_error(sprintf(__('Invalid argument "%s" given in $options["where"] (not found alias in $map)', 'tfuse'), $alias), E_USER_ERROR);
                 }
                 $where[] = $field . " " . trim($sqlWhere);
             }
             $sql .= "\n    AND " . implode(' AND ', $where);
         } else {
             $sql .= "\n    AND " . $options['where'];
         }
     }
     if (isset($options['suffix']) && trim($options['suffix'])) {
         $sql .= "\n  " . $options['suffix'];
     }
     if ($debug) {
         tf_print($sql);
     }
     return $wpdb->query($sql);
 }