Ejemplo n.º 1
0
 /**
  * Output the array
  *
  * @access private
  * @param array $headers
  * @param array $values
  */
 private function output_array($headers, $array)
 {
     header('Content-Type: text/csv; charset=utf-8');
     header('Content-Disposition: attachment; filename=' . $this->pager->get_classname() . '.csv');
     foreach ($headers as $header) {
         echo $header . ';';
     }
     echo "\n";
     foreach ($array as $values) {
         foreach ($values as $value) {
             echo $value . ';';
         }
         echo "\n";
     }
     exit;
     $result = $this->pager->page();
     foreach ($fields as $field) {
         echo $field . ';';
     }
     echo "\n";
     foreach ($this->pager->items as $item) {
         foreach ($fields as $field) {
             echo \Skeleton\Pager\Util::object_get_attribute($item, $field) . ';';
         }
         echo "\n";
     }
     exit;
 }
Ejemplo n.º 2
0
 /**
  * Get paged
  *
  * @access public
  * @param string $sort
  * @param string $direction
  * @param int $page
  * @param int $all
  * @param array $extra_conditions
  * @param array $extra_joins
  */
 public static function get_paged($sort = null, $direction = 'asc', $page = 1, $extra_conditions = [], $all = false, $extra_joins = [])
 {
     $db = self::trait_get_database();
     $table = self::trait_get_database_table();
     $where = self::trait_get_search_where($extra_conditions, $extra_joins);
     $joins = self::trait_get_link_tables();
     if ($sort === null) {
         $sort = $table . '.' . self::trait_get_table_field_id();
     }
     // $sort is sometimes passed as null, which looks better, but we want 1
     // by default
     if ($sort == null) {
         $sort = 1;
     }
     $object = new \ReflectionClass(get_class());
     if (is_callable($sort)) {
         $sorter = 'object';
     } elseif ($object->hasMethod($sort)) {
         $sorter = 'object';
     } else {
         $sorter = 'db';
     }
     if (!$all) {
         $limit = Config::$items_per_page;
     } else {
         $limit = 1000;
     }
     if ($page < 1) {
         $page = 1;
     }
     if (strtolower($direction) != 'asc') {
         $direction = 'desc';
     }
     $sql = 'SELECT DISTINCT ' . $table . '.' . self::trait_get_table_field_id() . ' as id ' . "\n";
     $sql .= 'FROM `' . $table . '`' . "\n";
     /**
      * Automatic join: Join if a condition or a sort is set
      *
      * 1) Find the tables where a condition is set
      * 2) Find the table where a sort is set on
      * 3) Remove all joins that are not in 1 and 2
      */
     $table_joins = self::trait_get_joins();
     $table_joins = array_merge($table_joins, $extra_joins);
     $condition_joins = [];
     foreach ($extra_conditions as $field => $condition) {
         if ($field != '%search%') {
             $condition_table = substr($field, 0, strpos($field, '.'));
             $condition_joins[$condition_table] = $condition_table;
         }
         if ($field == '%search%') {
             if (count($condition[1]) == 0) {
                 $condition_joins['*'] = '*';
             } else {
                 foreach ($condition[1] as $search_condition_field) {
                     $condition_table = substr($search_condition_field, 0, strpos($search_condition_field, '.'));
                     $condition_joins[$condition_table] = $condition_table;
                 }
             }
         }
     }
     $sort_condition_table = substr($sort, 0, strpos($sort, '.'));
     $condition_joins[$sort_condition_table] = $sort_condition_table;
     foreach ($table_joins as $key => $table_join) {
         if (isset($condition_joins['*'])) {
             continue;
         }
         if (!isset($condition_joins[$table_join->get_remote_table()])) {
             unset($table_joins[$key]);
         }
     }
     foreach ($table_joins as $table_join) {
         $sql .= $table_join;
     }
     /**
      * End of automatic join
      */
     $sql .= 'WHERE 1 ' . $where . "\n";
     if ($sorter == 'db') {
         if (strpos($sort, '.') === false and $sort != 1) {
             $sort = $table . '.' . $sort;
         }
         $sql .= 'ORDER BY ' . $sort . ' ' . $direction;
     }
     if ($all !== true and $sorter == 'db') {
         $sql .= ' LIMIT ' . ($page - 1) * $limit . ', ' . $limit;
     }
     $ids = $db->get_column($sql);
     $objects = [];
     foreach ($ids as $id) {
         $objects[] = self::get_by_id($id);
     }
     foreach ($extra_conditions as $key => $value) {
         foreach ($objects as $o_key => $object) {
             if (!method_exists($object, $key) or !is_callable([$object, $key])) {
                 continue;
             }
             try {
                 if (call_user_func_array([$object, $key], $value)) {
                     continue;
                 }
             } catch (Exception $e) {
                 continue;
             }
             unset($objects[$o_key]);
         }
     }
     if ($sorter == 'object') {
         $objects = Util::object_sort($objects, $sort, $direction);
         $objects = array_slice($objects, ($page - 1) * $limit, $limit);
     }
     return $objects;
 }