Index: trunk/core/kernel/db/dblist.php =================================================================== diff -u -r2581 -r2600 --- trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 2581) +++ trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 2600) @@ -5,19 +5,19 @@ * */ define('FLT_SYSTEM', 1); - + /** * User Having/Where filter * */ define('FLT_NORMAL', 2); - + /** * User "Search" Having/Where filter * */ define('FLT_SEARCH', 3); - + /** * User "View Menu" Having/Where filter * @@ -31,82 +31,82 @@ * @package kernel4 */ class kDBList extends kDBBase { - + /** * Description * * @var array * @access public */ var $OrderFields; - + /** * Holds counted total number of records in the query - without pagination (system+user filters) * * @var int * @access public */ var $RecordsCount; - - + + /** * Records count with system filters only applied * * @var int * @access private */ var $NoFilterCount = 0; - + /** * Record count selected to be * showed on current page * * @var int */ var $SelectedCount=0; - + /** * Array of records selected * * @var Array * @access private */ var $Records; - + var $CurrentIndex = 0; - + /** * List items per-page * * @var int * @access public */ var $PerPage; - + /** * Pages count in list based on PerPage & RecordsCount attributes * * @var int * @access public */ var $TotalPages; - + /** * Description * * @var int * @access public */ - - var $Direction; + + var $Direction; /** * Holds current page number - used when forming LIMIT clause of SELECT statement * * @var int * @access public */ var $Page; - + /** * Holds offset for LIMIT clause, calculated in {@link kDBList::PerPage()} * @@ -122,28 +122,28 @@ * @access private */ var $hasCounted = false; - + /** * Holds list WHERE filter object * * @var kMultipleFilter * @access private */ var $WhereFilter = Array(FLT_SYSTEM => null, FLT_NORMAL => null, FLT_SEARCH => null, FLT_VIEW => null); - + /** * Holds list HAVING filter object * * @var kMultipleFilter * @access private */ var $HavingFilter = Array(FLT_SYSTEM => null, FLT_NORMAL => null, FLT_SEARCH => null, FLT_VIEW => null); - + var $GroupByFields = Array(); - + var $Queried = false; var $Counted = false; - + /** * Creates kDBList * @@ -152,26 +152,26 @@ function kDBList() { parent::kDBBase(); $this->OrderFields = Array(); - + $this->WhereFilter[FLT_SYSTEM] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); $this->WhereFilter[FLT_NORMAL] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); - + $this->WhereFilter[FLT_SEARCH] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); $this->WhereFilter[FLT_SEARCH]->setType(FLT_TYPE_OR); - + $this->WhereFilter[FLT_VIEW] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); - + $this->HavingFilter[FLT_SYSTEM] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); $this->HavingFilter[FLT_NORMAL] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); - + $this->HavingFilter[FLT_SEARCH] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); $this->HavingFilter[FLT_SEARCH]->setType(FLT_TYPE_OR); - + $this->HavingFilter[FLT_VIEW] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); - + $this->PerPage = -1; } - + /** * Adds new or replaces old filter with same name * @@ -184,12 +184,12 @@ function addFilter($name, $clause, $filter_type = WHERE_FILTER, $filter_scope = FLT_SYSTEM) { $filter_name = ($filter_type == WHERE_FILTER) ? 'WhereFilter' : 'HavingFilter'; - + $filter =& $this->$filter_name; $filter =& $filter[$filter_scope]; $filter->addFilter($name,$clause); } - + /** * Removes specified filter from filters list * @@ -201,12 +201,12 @@ function removeFilter($name, $filter_type = WHERE_FILTER, $filter_scope = FLT_SYSTEM) { $filter_name = ($filter_type == WHERE_FILTER) ? 'WhereFilter' : 'HavingFilter'; - + $filter =& $this->$filter_name; $filter =& $filter[$filter_scope]; $filter->removeFilter($name); } - + /** * Clear list filters * @@ -236,7 +236,7 @@ $this->HavingFilter[FLT_VIEW]->clearFilters(); } } - + /** * Counts the total number of records base on the query resulted from {@link kDBList::GetSelectSQL()} * @@ -252,7 +252,7 @@ { $all_sql = $this->GetSelectSQL(true,false); $sql = $this->getCountSQL($all_sql); - + if( $this->GetGroupClause() ) { $this->RecordsCount = count( $this->Conn->GetCol($sql) ); @@ -261,7 +261,7 @@ { $this->RecordsCount = (int)$this->Conn->GetOne($sql); } - + $system_sql = $this->GetSelectSQL(true,true); if ($system_sql == $all_sql) { //no need to query the same again $this->NoFilterCount = $this->RecordsCount; @@ -271,7 +271,7 @@ $this->NoFilterCount = (int)$this->Conn->GetOne($sql); $this->Counted = true; } - + function getCountSQL($sql) { if ( preg_match("/DISTINCT(.*?)FROM(?!_)/is",$sql,$regs ) ) @@ -283,7 +283,7 @@ return preg_replace("/^.*SELECT(.*?)FROM(?!_)/is", "SELECT COUNT(*) AS count FROM ", $sql); } } - + /** * Queries the database with SQL resulted from {@link kDBList::GetSelectSQL()} and stores result in {@link kDBList::SelectRS} * @@ -301,25 +301,25 @@ echo get_class($this)." Query SQL: $q LIMIT ".$this->PerPage." OFFSET ".$this->Offset." Page: ".$this->Page."
"; } //$rs = $this->Conn->SelectLimit($q, $this->PerPage, $this->Offset); - + //in case we have not counted records try to select one more item to find out if we have something more than perpage - $limit = $this->Counted ? $this->PerPage : $this->PerPage+1; - + $limit = $this->Counted ? $this->PerPage : $this->PerPage+1; + $sql = $q.' '.$this->Conn->getLimitClause($this->Offset,$limit); - + $this->Records = $this->Conn->Query($sql); $this->SelectedCount = count($this->Records); if (!$this->Counted) $this->RecordsCount = $this->SelectedCount; if (!$this->Counted && $this->SelectedCount > $this->PerPage && $this->PerPage != -1) $this->SelectedCount--; - + if ($this->Records === false) { //handle errors here return false; } $this->Queried = true; - return true; + return true; } - + /** * Builds full select query except for LIMIT clause * @@ -330,20 +330,20 @@ { $q = parent::GetSelectSQL($this->SelectClause); if(!$for_counting) $q = $this->addCalculatedFields($q); - + $where = $this->GetWhereClause($for_counting,$system_filters_only); $having = $this->GetHavingClause($for_counting,$system_filters_only); $order = $this->GetOrderClause(); $group = $this->GetGroupClause(); - - if (!empty($where)) $q .= ' WHERE ' . $where; + + if (!empty($where)) $q .= ' WHERE ' . $where; if (!empty($group)) $q .= ' GROUP BY ' . $group; if (!empty($having)) $q .= ' HAVING ' . $having; if ( !$for_counting && !empty($order) ) $q .= ' ORDER BY ' . $order; - + return str_replace('%1$s',$this->TableName,$q); } - + function extractCalculatedFields($clause) { if ( is_array($this->CalculatedFields) ) { @@ -354,7 +354,7 @@ } return $clause; } - + /** * Returns WHERE clause of the query * @@ -365,9 +365,9 @@ function GetWhereClause($for_counting=false,$system_filters_only=false) { $where =& $this->Application->makeClass('kMultipleFilter'); - + $where->addFilter('system_where', $this->WhereFilter[FLT_SYSTEM] ); - + if (!$system_filters_only) { $where->addFilter('view_where', $this->WhereFilter[FLT_VIEW] ); $search_w = $this->WhereFilter[FLT_SEARCH]->getSQL(); @@ -378,16 +378,16 @@ $where->addFilter('search_where', $search_w ); } } - + if( $for_counting ) // add system_having and view_having to where { $where->addFilter('system_having', $this->extractCalculatedFields( $this->HavingFilter[FLT_SYSTEM]->getSQL() ) ); if (!$system_filters_only) $where->addFilter('view_having', $this->extractCalculatedFields( $this->HavingFilter[FLT_VIEW]->getSQL() ) ); } - + return $where->getSQL(); } - + /** * Depricated method * @@ -399,11 +399,11 @@ if( $this->Application->isDebugMode() ) { global $debugger; - $debugger->appendTrace(); + $debugger->appendTrace(); } trigger_error('Depricated method kDBList->SetWhereClause. Use kDBList->addFilter instead.', E_USER_ERROR); } - + /** * Returns HAVING clause of the query * @@ -414,9 +414,9 @@ function GetHavingClause($for_counting=false, $system_filters_only=false) { if( $for_counting ) return ''; - + $having =& $this->Application->makeClass('kMultipleFilter'); - + $having->addFilter('system_having', $this->HavingFilter[FLT_SYSTEM] ); if (!$system_filters_only) { $having->addFilter('view_having', $this->HavingFilter[FLT_VIEW] ); @@ -425,10 +425,10 @@ $having->addFilter('search_having', $this->HavingFilter[FLT_SEARCH] ); } } - + return $having->getSQL(); } - + /** * Returns GROUP BY clause of the query * @@ -444,7 +444,7 @@ { $this->GroupByFields[$field] = $field; } - + function RemoveGroupByField($field) { unset($this->GroupByFields[$field]); @@ -465,10 +465,10 @@ $lang = $this->Application->GetVar('m_lang'); $field = 'l'.$lang.'_'.$field; } - + $this->OrderFields[] = Array($field, $direction); } - + /** * Removes all order fields * @@ -479,7 +479,7 @@ { $this->OrderFields = Array(); } - + /** * Returns ORDER BY Clause of the query * @@ -492,35 +492,35 @@ { $ret = ''; foreach ($this->OrderFields as $field) { - + $name = $field[0]; $ret .= isset($this->Fields[$name]) && !isset($this->VirtualFields[$name]) ? '`'.$this->TableName.'`.' : ''; if ($field[0] == 'RAND()') { $ret .= $field[0].' '.$field[1].','; } else { - $ret .= '`'.$field[0] . '` ' . $field[1] . ','; + $ret .= '`'.$field[0] . '` ' . $field[1] . ','; } } $ret = rtrim($ret, ','); return $ret; } - - function GetOrderField($pos=NULL) + + function GetOrderField($pos=NULL) { if(!(isset($this->OrderFields[$pos]) && $this->OrderFields[$pos]) ) { $pos = 0; } return isset($this->OrderFields[$pos][0]) ? $this->OrderFields[$pos][0] : ''; } - - function GetOrderDirection($pos=NULL) + + function GetOrderDirection($pos=NULL) { if( !getArrayValue($this->OrderFields, $pos) ) $pos = 0; return getArrayValue($this->OrderFields, $pos, 1); } - + /** * Return unformatted field value * @@ -533,23 +533,23 @@ $row =& $this->getCurrentRecord(); return $row[$name]; } - + function HasField($name) { $row =& $this->getCurrentRecord(); return isset($row[$name]); } - + function GetFieldValues() { return $this->getCurrentRecord(); } - + function &getCurrentRecord() { return $this->Records[$this->CurrentIndex]; } - + /** * Description * @@ -561,7 +561,7 @@ { $this->CurrentIndex = 0; } - + /** * Description * @@ -595,7 +595,7 @@ { return ($this->CurrentIndex >= $this->SelectedCount); } - + /** * Description * @@ -610,8 +610,8 @@ $this->TotalPages = (($this->RecordsCount - ($this->RecordsCount % $this->PerPage)) / $this->PerPage) // integer part of division + (($this->RecordsCount % $this->PerPage) != 0); // adds 1 if there is a reminder return $this->TotalPages; - } - + } + /** * Sets number of records to query per page * @@ -623,12 +623,12 @@ { $this->PerPage = $per_page; } - + function GetPerPage() { return $this->PerPage == -1 ? $this->RecordsCount : $this->PerPage; } - + /** * Description * @@ -652,7 +652,7 @@ } //$this->GoFirst(); } - + /** * Sets current item field value * (doesn't apply formatting) @@ -666,7 +666,7 @@ { $this->Records[$this->CurrentIndex][$name] = $value; } - + /** * Apply where clause, that links this object to it's parent item * @@ -680,10 +680,10 @@ { $parent_table_key = $this->Application->getUnitOption($this->Prefix, 'ParentTableKey'); $foreign_key_field = $this->Application->getUnitOption($this->Prefix, 'ForeignKey'); - + $parent_object =& $this->Application->recallObject($parent_prefix.'.'.$special); $parent_id = $parent_object->GetDBField($parent_table_key); - + $this->addFilter('parent_filter', '`'.$this->TableName.'`.`'.$foreign_key_field.'` = '.$parent_id); // only for list in this case } }