jaguar /lib/LP/RecordCollection.php

Language PHP Lines 337
MD5 Hash 29700878496e9173d12ff6ed15c2d910
Repository https://github.com/radicaldesigns/jaguar.git View Raw File
  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
<?php
require_once('Record.php');
require_once('RecordSearch.php');

class RecordCollection extends Data {

    var $sort = array();
    var $limit;
    var $offset;
    var $_search_class = 'RecordSearch';
    var $_search_exact_values = array();
    var $_search_fulltext = array( );
    var $_id_field_lookups;
    
    var $source;

    //Data Management Functions

    function readData() {
        $sql = $this->_assembleSQL();
        AMP::debug_sql( $sql, get_class($this));
        if ($this->source = $this->dbcon->Execute($sql)) {
            return true;
        }

        trigger_error ( sprintf( "SQL failed for %s %s Error: %s SQL: %s", get_class( $this ), 'read', $this->dbcon->ErrorMsg(), $sql ));
        return false;
        
    }

    function getData() {
        if (!$this->hasData()) return false;
        return $this->source->FetchRow();
    }

    function isReady() {
        return $this->makeReady();
    }

    function refreshData() {
        $this->clearCache( );
        $this->readData();
    }

    function clearCache( ){
        $cached_sql = $this->_assembleSQL( );
        $this->dbcon->CacheFlush( $cached_sql );
        AMP::debug_sql( $cached_sql, get_class($this). ' cleared cache');
    }

    function makeReady() {
        if (!$this->hasData()) return false;
        $this->source->MoveFirst();
        return true;
    }

    function setSort( $expression_set, $hold_priority = true ) {
        if (!(is_array($expression_set) || is_string($expression_set)) ) return false;
        $this->sort = array();

        if (is_string($expression_set)) return $this->addSort( $expression_set, false );
				/*
        if ($hold_priority) {
						$expression_set = array_reverse( $expression_set, true );
				}
				*/

        foreach ($expression_set as $exp) {
            $this->addSort ( $exp, false);
        }
    }

    function addSort ( $exp, $primary = true ) {
        if (!is_string($exp)) return false;
        if (array_search( $exp, $this->sort )!==FALSE) return true;

        if ($primary) return array_unshift( $this->sort, $exp );

        return ( $this->sort[] = $exp);
        return true;
    }

    function getSort() {
        if (empty($this->sort)) return false;
        reset ($this->sort);
        return current( $this->sort );
    }


    function _assembleSQL( $criteria = null ) {
        $sql  = $this->_makeSelect();
        $sql .= $this->_makeSource();
        $sql .= isset( $criteria ) ? 
                    ' WHERE ' . $criteria 
                    : $this->_makeCriteria();
        $sql .= $this->_makeSort();
        $sql .= $this->_makeLimit();
        return $sql;
    }

    function deleteData($criteria) {
        if (!$criteria) return false;
        $sql = "DELETE" . $this->_makeSource(). " where " . $criteria;
        if($this->dbcon->Execute($sql)) {
            AMP::debug_sql( $sql, get_class($this));

            $cached_sql = $this->_assembleSql( $criteria ) ;
            $this->dbcon->CacheFlush( $cached_sql );
            AMP::debug_sql( $sql, get_class($this). ' cleared cache');
            return $this->dbcon->Affected_Rows();
        }
        trigger_error ( sprintf( AMP_TEXT_ERROR_DATABASE_SQL_FAILED, get_class( $this ), 'delete', $this->dbcon->ErrorMsg(), $sql ));

        return false;
        
    }

    function updateData( $update_actions, $criteria = "1" ) {
        if (!is_array( $update_actions )) return false;
        $sql = "UPDATE " . $this->datatable . " SET " . join( ", ", $update_actions ) .
               " where " . $criteria;
        if ($this->dbcon->Execute($sql)) {
            AMP::debug_sql( $sql, get_class($this) . ' update');
            return $this->dbcon->Affected_Rows();
        }
        trigger_error ( sprintf( AMP_TEXT_ERROR_DATABASE_SQL_FAILED, get_class( $this ), 'update', $this->dbcon->ErrorMsg(), $sql ));
        return false;
    }

    function insertData( $values ){
        $source = new Record( $this->dbcon );
        $source->setSource( $this->datatable );
        $source->set( $values );
        $sql = $source->debug_insertSQL( );
        if ($this->dbcon->Execute($sql)) {
            AMP::debug_sql( $sql, get_class($this) . ' insert');
            return $this->dbcon->Affected_Rows();
        }

        trigger_error ( sprintf( AMP_TEXT_ERROR_DATABASE_SQL_FAILED, get_class( $this ), 'insert', $this->dbcon->ErrorMsg(), $sql ));
        return false;
         
    }


    function _makeSort() {
        if (empty($this->sort)) return false;
        return " ORDER BY ". join(", ", $this->sort);
    }

    function _makeLimit() {
        if (!( isset($this->limit) && $this->limit )) return false;
        return " LIMIT " .$this->_buildLimit();
    }

    function _buildLimit() {
        if (!isset($this->offset)) return $this->limit;
        return $this->offset . ', ' . $this->limit;
    }

    function setLimit( $qty ) {
        $this->limit = $qty;
    }

    function setOffset( $offset ) {
        $this->offset = $offset;
    }

    function getGroupedIndex($column) {
        $sql = "SELECT $column, count(" . $this->id_field . ") as qty FROM "
            . $this->datatable . $this->_makeCriteria() . " GROUP BY $column";
        AMP::debug_sql( $sql, get_class($this) . ' index');
        $result = $this->dbcon->CacheGetAssoc($sql);
        if ( !$result && $db_error = $this->dbcon->ErrorMsg( )) {
            trigger_error( sprintf( AMP_TEXT_ERROR_LOOKUP_SQL_FAILED, get_class($this) . __FUNCTION__, $db_error ) . $sql );
        }
        return $result;
    }

    function RecordCount() {
        if (!is_object($this->source)) return false;
        return $this->source->RecordCount();
    }

    function NoLimitRecordCount() {
        $sql = "SELECT count(" . $this->id_field . ") as qty from "
            . $this->datatable . $this->_makeCriteria();
        $set = $this->dbcon->Execute( $sql );


        if ( !$set && $db_error = $this->dbcon->ErrorMsg( )) {
            trigger_error( sprintf( AMP_TEXT_ERROR_LOOKUP_SQL_FAILED, get_class($this) . __FUNCTION__, $db_error ) . $sql );
			return 0;
        }
        AMP::debug_sql( $sql, get_class($this) . ' count');

        return $set->Fields( 'qty' );
    }

    function getIdFieldLookups( ) {
        if ( isset( $this->_id_field_lookups )) return $this->_id_field_lookups;
        return $this->id_field;
    }

    function setIdFieldLookups( $field ) {
        $this->_id_field_lookups = $field; 
    }

	function getLookup($field, $use_sort = false ) {
        $set = array();
		if( !$this->makeReady() ) {
			$sql = "SELECT " . $this->getIdFieldLookups( ). ", $field " . $this->_makeSource()
            . $this->_makeCriteria();
            if ( $use_sort ) {
                $sql .= $this->_makeSort( );
            }
			$set = $this->dbcon->CacheGetAssoc( $sql );
            AMP::debug_sql( $sql, get_class($this) . ' lookup');
            if ( !$set && $db_error = $this->dbcon->ErrorMsg( )) {
                trigger_error( sprintf( AMP_TEXT_ERROR_LOOKUP_SQL_FAILED, get_class($this) . __FUNCTION__, $db_error ) . $sql );
            }
		} else {
			while($record = $this->getData()) {
				$set[$record['id']] = $record[$field];
			}
		}

		return $set;
	}

    function filter( $fieldname, $value, $max_qty=null ) {
        if (!$this->makeReady()) return false;
        $result = array();
        while( $data = $this->getData() ) {
            if (isset($max_qty) && count($result)==$max_qty) break;
            if ($data[ $fieldname ] != $value) continue;
            $result[ $data[$this->id_field] ] = $data;
        }

        if (empty($result)) return false;
        return $result;
    }

    function getArray() {
        if (!$this->makeReady()) return false;
        return $this->source->GetArray();
    }

    function instantiateItems($rows, $class) {
            if(empty($rows) or !$rows or !$class or !(class_exists($class))) return false;

            $items = array();
            foreach ($rows as $row) {
                    $object = new $class();
                    $object->set_data_from_db($row);
                    $items[$object->id] = $object;
            }

            return $items;
    }


    function applySearch( $search_values, $run_query=true ) {
        $search = &$this->getSearch( );
        $search->applyValues( $search_values );
        if ( $run_query ) $this->readData( );
    }

    function &getSearch() {
        if ( !isset( $this->_search )) {
            $this->_search = new $this->_search_class( $this );
        }
        return $this->_search;
    }

    /**
     * Returns an array of fields which should be searched via exact, rather than %contains% methods 
     * 
     * @access public
     * @return array 
     */
    function &getLiteralCriteria( ){
        return $this->_search_exact_values;
    }

    /**
     * Search all defined text fields for relevance to a search string 
     * 
     * @param mixed $search_string 
     * @access public
     * @return void
     */
    function addCriteriaFulltext( $search_string, $sortby_relevance = false ) {
        $search = &$this->getSearch( );
        $fulltext_criteria = $search->getCriteriaFulltext( $search_string );
        $this->addCriteria( $fulltext_criteria );
        if ( $sortby_relevance ) $this->addSort( $fulltext_criteria . ' DESC' );
    }

    function getFullTextFields( ){
        return $this->_search_fulltext;
    }

    function makeCriteriaTag( $tag_value ) {
        if ( !isset( $this->_item_type )) {
            trigger_error( sprintf( AMP_TEXT_ERROR_NOT_DEFINED, get_class( $this ), '_item_type' ));
            trigger_error( sprintf( AMP_TEXT_ERROR_CREATE_FAILED, get_class( $this ), 'tag criteria ' . $tag_value ));
            return false;
        }
        $lookup_type =  AMP_pluralize(  $this->_item_type ) . 'ByTag' ;
        $id_set = AMPSystem_Lookup::instance( $lookup_type, $tag_value );
        if ( !$id_set || empty( $id_set )) return 'FALSE';

        return $this->id_field . ' IN ( '. join( ',', array_keys( $id_set ) ) . ')';
        
    }

    function setSortAndLimit( $options ) {
        $order_by = isset( $options['sort']) && $options['sort'] ? $options['sort'] : false;
        $source_limit = isset( $options['limit']) && $options['limit'] ? $options['limit'] : false;
        $source_offset = isset( $options['offset']) && $options['offset'] ? $options['offset'] : false;

        if ( $order_by || $source_limit || $source_offset ) {
            if( $order_by ) {
                $this->setSort( $order_by );
            }
            if ( $source_limit ) {
                $this->setLimit( $source_limit );
            }
            if ( $source_offset ) {
                $this->setOffset( $source_offset );
            }
        }
    }
    
}
Back to Top