system/classes/listfactory.class.php
branchHEAD
changeset 7391 0bee991937cc
parent 7389 abd5bf00c26b
child 7392 3872f0c040e4
     1.1 --- a/system/classes/listfactory.class.php	Sun Oct 18 02:25:07 2009 +0100
     1.2 +++ b/system/classes/listfactory.class.php	Sun Oct 25 22:01:38 2009 +0000
     1.3 @@ -206,7 +206,13 @@
     1.4          if ($name === ROW_NUMBER) {
     1.5              $sort = false;
     1.6          }
     1.7 -        $this->_fields[] = array('title' => $title, 'name' => $name, 'display' => $display, 'sort' => $sort, 'format' => $format);
     1.8 +        $this->_fields[] = array(
     1.9 +            'title' => $title,
    1.10 +            'name' => $name,
    1.11 +            'display' => $display,
    1.12 +            'sort' => $sort,
    1.13 +            'format' => $format
    1.14 +        );
    1.15      }
    1.16  
    1.17      /**
    1.18 @@ -221,7 +227,26 @@
    1.19      */
    1.20      function setQuery( $title, $name, $sql, $rank )
    1.21      {
    1.22 -        $this->_query_arr[] = array('title' => $title, 'name' => $name, 'sql' => $sql, 'rank' => $rank);
    1.23 +        $this->_query_arr[] = array(
    1.24 +            'type' => 'sql',
    1.25 +            'title' => $title,
    1.26 +            'name' => $name,
    1.27 +            'sql' => $sql,
    1.28 +            'rank' => $rank
    1.29 +        );
    1.30 +        $this->_total_rank += $rank;
    1.31 +    }
    1.32 +
    1.33 +    function setCallback( $title, $name, $function, $rank, $total )
    1.34 +    {
    1.35 +        $this->_query_arr[] = array(
    1.36 +            'type' => 'callback',
    1.37 +            'title' => $title,
    1.38 +            'name' => $name,
    1.39 +            'func' => $function,
    1.40 +            'rank' => $rank,
    1.41 +            'total' => $total
    1.42 +        );
    1.43          $this->_total_rank += $rank;
    1.44      }
    1.45  
    1.46 @@ -282,21 +307,25 @@
    1.47      * @return int Total number of rows
    1.48      *
    1.49      */
    1.50 -    function _numRows( $sql )
    1.51 +    function _getTotal( $param )
    1.52      {
    1.53 -        if (is_array($sql))
    1.54 -        {
    1.55 +        if ($param['type'] == 'callback') {
    1.56 +            return $param['total'];
    1.57 +        }
    1.58 +        else {
    1.59 +            $sql = $param['sql'];
    1.60 +        }
    1.61 +
    1.62 +        if (is_array($sql)) {
    1.63              $sql['mysql'] = preg_replace('/SELECT.*FROM/is', 'SELECT COUNT(*) FROM', $sql['mysql']);
    1.64              $sql['mssql'] = preg_replace('/SELECT.*FROM/is', 'SELECT COUNT(*) FROM', $sql['mssql']);
    1.65          }
    1.66 -        else
    1.67 -        {
    1.68 +        else {
    1.69              $sql = preg_replace('/SELECT.*FROM/is', 'SELECT COUNT(*) FROM', $sql);
    1.70          }
    1.71          $result = DB_query($sql);
    1.72          $num_rows = DB_numRows($result);
    1.73 -        if ($num_rows <= 1)
    1.74 -        {
    1.75 +        if ($num_rows <= 1) {
    1.76              $B = DB_fetchArray($result, true);
    1.77              $num_rows = $B[0];
    1.78          }
    1.79 @@ -397,7 +426,7 @@
    1.80          $limits = array();
    1.81          for ($i = 0; $i < count($this->_query_arr); $i++)
    1.82          {
    1.83 -            $limits[$i]['total'] = $this->_numRows($this->_query_arr[$i]['sql']);
    1.84 +            $limits[$i]['total'] = $this->_getTotal($this->_query_arr[$i]);
    1.85              $limits[$i]['pp'] = round(($this->_query_arr[$i]['rank'] / $this->_total_rank) * $num_query_results);
    1.86              $this->_total_found += $limits[$i]['total'];
    1.87              $pp_total += $limits[$i]['pp'];
    1.88 @@ -415,6 +444,44 @@
    1.89              if ($limits[$i]['limit'] <= 0) {
    1.90                  continue;
    1.91              }
    1.92 +
    1.93 +            // This is a callback function
    1.94 +            if ($this->_query_arr[$i]['type'] == 'callback')
    1.95 +            {
    1.96 +                if (is_callable($this->_query_arr[$i]['func']))
    1.97 +                {
    1.98 +                    $callback_rows = call_user_func_array($this->_query_arr[$i]['func'], array($limits[$i]['offset'], $limits[$i]['limit']));
    1.99 +
   1.100 +                    foreach ($callback_rows as $row)
   1.101 +                    {
   1.102 +                        $col = array();
   1.103 +                        $col[SQL_TITLE] = $this->_query_arr[$i]['title'];
   1.104 +                        $col[SQL_NAME] = $this->_query_arr[$i]['name'];
   1.105 +
   1.106 +                        foreach ($this->_fields as $field)
   1.107 +                        {
   1.108 +                            if (!is_numeric($field['name']) && $field['name'][0] != '_') {
   1.109 +                                if (empty($row[ $field['name'] ])) {
   1.110 +                                    $col[ $field['name'] ] = 'LF_NULL';
   1.111 +                                } else {
   1.112 +                                    $col[ $field['name'] ] = $row[ $field['name'] ];
   1.113 +                                }
   1.114 +                            }
   1.115 +                        }
   1.116 +
   1.117 +                        // Need to call the format function before and after
   1.118 +                        // sorting the results.
   1.119 +                        if (is_callable($this->_function)) {
   1.120 +                            $col = call_user_func_array($this->_function, array(true, $col));
   1.121 +                        }
   1.122 +
   1.123 +                        $rows_arr[] = $col;
   1.124 +                    }
   1.125 +                }
   1.126 +                continue;
   1.127 +            }
   1.128 +
   1.129 +            // This is an SQL query, so execute it and format the results
   1.130              $limit_sql = " LIMIT {$limits[$i]['offset']},{$limits[$i]['limit']}";
   1.131  
   1.132              if (is_array($this->_query_arr[$i]['sql']))