This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 
<?php
/**
*
* @package Utilities
*/
defined('C5_EXECUTE') or die("Access Denied.");

class Concrete5_Library_DatabaseItemList extends ItemList {

    protected $query = '';
    protected $userQuery = '';
    protected $debug = false;
    protected $filters = array();
    protected $sortByString = '';
    protected $groupByString = '';  
    protected $havingString = '';  
    protected $autoSortColumns = array();
    protected $userPostQuery = '';
    
    public function getTotal() {
        if ($this->total == -1) {
            $db = Loader::db();
            $arr = $this->executeBase(); // returns an associated array of query/placeholder values             
            $r = $db->Execute($arr);
            $this->total = $r->NumRows();
        }       
        return $this->total;
    }
    
    public function debug($dbg = true) {
        $this->debug = $dbg;
    }
    
    protected function setQuery($query) {
        $this->query = $query . ' ';
    }
    
    protected function getQuery() {
        return $this->query;
    }
    
    public function addToQuery($query) {
        $this->userQuery .= $query . ' ';
    }

    protected function setupAutoSort() {
        if (count($this->autoSortColumns) > 0) {
            $req = $this->getSearchRequest();
            if (in_array($req[$this->queryStringSortVariable], $this->autoSortColumns)) {
                $this->sortBy($req[$this->queryStringSortVariable], $req[$this->queryStringSortDirectionVariable]);
            }
        }
    }
    
    protected function executeBase() {
        $db = Loader::db();
        $q = $this->query . $this->userQuery . ' where 1=1 ';
        foreach($this->filters as $f) {
            $column = $f[0];
            $comp = $f[2];
            $value = $f[1];
            // if there is NO column, then we have a free text filter that we just add on
            if ($column == false || $column == '') {
                $q .= 'and ' . $f[1] . ' ';
            } else {
                if (is_array($value)) {
                    if (count($value) > 0) {
                        switch($comp) {
                            case '=':
                                $comp = 'in';
                                break;
                            case '!=':
                                $comp = 'not in';
                                break;
                        }
                        $q .= 'and ' . $column . ' ' . $comp . ' (';
                        for ($i = 0; $i < count($value); $i++) {
                            if ($i > 0) {
                                $q .= ',';
                            }
                            $q .= $db->quote($value[$i]);
                        }
                        $q .= ') ';
                    } else {
                        $q .= 'and 1 = 2 ';
                    }
                } else { 
                    $comp = (is_null($value) && stripos($comp, 'is') === false) ? (($comp == '!=' || $comp == '<>') ? 'IS NOT' : 'IS') : $comp;
                    $q .= 'and ' . $column . ' ' . $comp . ' ' . $db->quote($value) . ' ';
                }
            }
        }
        
        if ($this->userPostQuery != '') {
            $q .= ' ' . $this->userPostQuery . ' ';
        }
        
        if ($this->groupByString != '') {
            $q .= 'group by ' . $this->groupByString . ' ';
        }       

        if ($this->havingString != '') {
            $q .= 'having ' . $this->havingString . ' ';
        }       
        
        return $q;
    }
    
    protected function setupSortByString() {
        if ($this->sortByString == '' && $this->sortBy != '') {
            $this->sortByString = $this->sortBy . ' ' . $this->sortByDirection;
        }
    }
    
    protected function setupAttributeSort() {
        if (is_callable(array($this->attributeClass, 'getList'))) {
            $l = call_user_func(array($this->attributeClass, 'getList'));
            foreach($l as $ak) {
                $this->autoSortColumns[] = 'ak_' . $ak->getAttributeKeyHandle();
            }
            if ($this->sortBy != '' && in_array('ak_' . $this->sortBy, $this->autoSortColumns)) {
                $this->sortBy = 'ak_' . $this->sortBy;
            }
        }
    }
    
    /** 
     * Returns an array of whatever objects extends this class (e.g. PageList returns a list of pages).
     */
    public function get($itemsToGet = 0, $offset = 0) {
        $q = $this->executeBase();
        // handle order by
        $this->setupAttributeSort();
        $this->setupAutoSort();
        $this->setupSortByString();
        
        if ($this->sortByString != '') {
            $q .= 'order by ' . $this->sortByString . ' ';
        }   
        if ($this->itemsPerPage > 0 && (intval($itemsToGet) || intval($offset)) ) {
            $q .= 'limit ' . $offset . ',' . $itemsToGet . ' ';
        }
        
        $db = Loader::db();
        if ($this->debug) { 
            Database::setDebug(true);
        }
        //echo $q.'<br>'; 
        $resp = $db->GetAll($q);
        if ($this->debug) { 
            Database::setDebug(false);
        }
        
        $this->start = $offset;
        return $resp;
    }
    
    /** 
     * Adds a filter to this item list
     */
    public function filter($column, $value, $comparison = '=') {
        $this->filters[] = array($column, $value, $comparison);
    }
    
    public function getSearchResultsClass($field) {
        if ($field instanceof AttributeKey) {
            $field = 'ak_' . $field->getAttributeKeyHandle();
        }
        return parent::getSearchResultsClass($field);
    }

    public function sortBy($key, $dir = 'asc') {
        if ($key instanceof AttributeKey) {
            $key = 'ak_' . $key->getAttributeKeyHandle();
        }
        parent::sortBy($key, $dir);
    }
    
    public function groupBy($key) {
        if ($key instanceof AttributeKey) {
            $key = 'ak_' . $key->getAttributeKeyHandle();
        }
        $this->groupByString = $key;
    }   

    public function having($column, $value, $comparison = '=') {
        if ($column == false) {
            $this->havingString = $value;
        } else {
            $this->havingString = $column . ' ' . $comparison . ' ' . $value;
        }
    }
    
    public function getSortByURL($column, $dir = 'asc', $baseURL = false, $additionalVars = array()) {
        if ($column instanceof AttributeKey) {
            $column = 'ak_' . $column->getAttributeKeyHandle();
        }
        return parent::getSortByURL($column, $dir, $baseURL, $additionalVars);
    }
    
    protected function setupAttributeFilters($join) {       
        $i = 1;
        $this->addToQuery($join);
        foreach($this->attributeFilters as $caf) {
            $this->filter($caf[0], $caf[1], $caf[2]);
        }
    }

    public function filterByAttribute($column, $value, $comparison = '=') {
        if (is_array($column)) {
            $column = $column[key($column)] . '_' . key($column);
        }
        $this->attributeFilters[] = array('ak_' . $column, $value, $comparison);
    }
    

}

class Concrete5_Library_DatabaseItemListColumn {

    public function getColumnValue($obj) {
        if (is_array($this->callback)) {
            return call_user_func($this->callback, $obj);
        } else {
            return call_user_func(array($obj, $this->callback));
        }
    }
    
    public function getColumnKey() {return $this->columnKey;}
    public function getColumnName() {return $this->columnName;}
    public function getColumnDefaultSortDirection() {return $this->defaultSortDirection;}
    public function isColumnSortable() {return $this->isSortable;}
    public function getColumnCallback() {return $this->callback;}
    public function setColumnDefaultSortDirection($dir) {$this->defaultSortDirection = $dir;}
    public function __construct($key, $name, $callback, $isSortable = true, $defaultSort = 'asc') {
        $this->columnKey = $key;
        $this->columnName = $name;
        $this->isSortable = $isSortable;
        $this->callback = $callback;
        $this->defaultSortDirection = $defaultSort;
    }
}

class Concrete5_Library_DatabaseItemListAttributeKeyColumn extends Concrete5_Library_DatabaseItemListColumn {

    protected $attributeKey = false;
    
    public function getAttributeKey() {
        return $this->attributeKey;
    }

    public function __construct($attributeKey, $isSortable = true, $defaultSort = 'asc') {
        $this->attributeKey = $attributeKey;
        parent::__construct('ak_' . $attributeKey->getAttributeKeyHandle(), $attributeKey->getAttributeKeyDisplayName('text'), false, $isSortable, $defaultSort);
    }
    
    public function getColumnValue($obj) {
        if (is_object($this->attributeKey)) {
            $vo = $obj->getAttributeValueObject($this->attributeKey);
            if (is_object($vo)) {
                return $vo->getValue('display');
            }
        }
    }
}

class Concrete5_Library_DatabaseItemListColumnSet {
    
    protected $columns = array();
    protected $defaultSortColumn;
    
    public function addColumn($col) {
        $this->columns[] = $col;
    }
    
    public function __wakeup() {
        $i = 0;
        foreach($this->columns as $col) {
            if ($col instanceof DatabaseItemListAttributeKeyColumn) {
                $ak = call_user_func(array($this->attributeClass, 'getByHandle'), substr($col->getColumnKey(), 3));
                if (!is_object($ak)) {
                    unset($this->columns[$i]);
                }
            }
            $i++;
        }       
    }
    
    public function getSortableColumns() {
        $tmp = array();
        $columns = $this->getColumns();
        foreach($columns as $col) {
            if ($col->isColumnSortable()) {
                $tmp[] = $col;
            }
        }
        return $tmp;
    }
    public function setDefaultSortColumn(DatabaseItemListColumn $col, $direction = false) {
        if ($direction != false) {
            $col->setColumnDefaultSortDirection($direction);
        }
        $this->defaultSortColumn = $col;
    }
    
    public function getDefaultSortColumn() {
        return $this->defaultSortColumn;
    }
    public function getColumnByKey($key) {
        if (substr($key, 0, 3) == 'ak_') {
            $ak = call_user_func(array($this->attributeClass, 'getByHandle'), substr($key, 3));
            $col = new DatabaseItemListAttributeKeyColumn($ak);
            return $col;
        } else {
            foreach($this->columns as $col) {
                if ($col->getColumnKey() == $key) {
                    return $col;            
                }
            }
        }
    }
    public function getColumns() {return $this->columns;}
    public function contains($col) {
        foreach($this->columns as $_col) {
            if ($col instanceof DatabaseItemListColumn) {
                if ($_col->getColumnKey() == $col->getColumnKey()) {
                    return true;
                }
            } else if (is_a($col, 'AttributeKey')) {
                if ($_col->getColumnKey() == 'ak_' . $col->getAttributeKeyHandle()) {
                    return true;
                }
            }
        }
        return false;
    }
}