Skip to content

Instantly share code, notes, and snippets.

@darscan
Created August 2, 2010 15:42
Show Gist options
  • Save darscan/504823 to your computer and use it in GitHub Desktop.
Save darscan/504823 to your computer and use it in GitHub Desktop.
The Worst Code I've Ever Written
<?php
class Module_model extends Model
{
private $log = array();
private $module_row_cache = array();
private $module_field_cache = array();
/**
* Get Module Data and Resolve Dependencies
*
* @param String $module_name
* @param Array $where_array
* @param Array $orderby_array
* @param Integer $limit
* @param Integer $offset
* @return Object $results
*/
function getData( $module_name = '', $where_array = '', $orderby_array = '', $offset = '', $limit = '', $language_id = 0 )
{
$time_start = microtime( true );
$this->log[] = "getRows started: $module_name";
$result = new stdClass();
$result->success = FALSE;
$result->error = '';
$result->json_size = 0;
$result->execution_time = 0;
$result->num_rows = 0;
// $result->fields = array();
$result->rows = array();
$language_id = intval( $language_id );
// Validate Parameters
if ( $module_name == '' )
{
$result->error = 'You must specify a Module Name.';
}
else if ( $where_array != '' && is_array( $where_array ) == FALSE )
{
$result->error = 'If used, the where_array parameter must be an Associative Array.';
}
else if ( $orderby_array != '' && is_array( $orderby_array ) == FALSE )
{
$result->error = 'If used, the order_array parameter must be an Associative Array.';
}
if ( $result->error == '' )
{
$fields = $this->getFields( $module_name );
if ( $fields == FALSE )
{
$result->error = "The $module_name module (or it's field definitions) could not be found.";
}
else
{
// $result->fields = $fields;
// Build Query
$this->db->select()->from( $module_name );
if ( $language_id > 0 ) $this->db->where( 'language_id', $language_id );
if ( is_array( $where_array ) ) $this->db->where( $where_array );
if ( is_array( $orderby_array ) )
{
foreach ( $orderby_array as $key=>$val )
{
$this->db->order_by( $key, $val );
}
}
if ( is_int( $limit ) ) $this->db->limit( $limit );
if ( is_int( $offset ) ) $this->db->offset( $offset );
// Run Query
$query = $this->db->get();
$result->num_rows = $query->num_rows();
$this->log[] = 'db query: '.str_replace( "\n", ' ', $this->db->last_query() );
// Parse Query Results
$rows = array();
$ids_to_fetch = array();
$foreign_id_keys = array();
$rows_to_populate = array();
$db_rows = $query->result();
foreach ( $db_rows as $db_row )
{
// $db_row->table_name = $module_name;
$rows[ $db_row->id ] = $db_row;
$fields = $this->module_field_cache[ $module_name ];
$this->module_row_cache[ $module_name ][ $db_row->id ] = $db_row;
foreach( $fields as $field_name=>$field )
{
$foreign_id = $db_row->$field_name;
$foreign_table = $field->foreign_table_name;
$foreign_id_key = 'id';
switch ( $field->field_type )
{
case 'assetid':
$foreign_table = 'sys_assets';
$foreign_id_key = 'asset_id';
break;
}
if ( $foreign_table != '' )
{
$foreign_row = @$this->module_row_cache[ $foreign_table ][ $foreign_id ];
if ( $foreign_row != null )
{
$db_row->$field_name = $foreign_row;
}
else
{
$ids_to_fetch[ $foreign_table ][ $foreign_id ] = $foreign_id;
$rows_to_populate[ $foreign_table ][ $foreign_id ][ $module_name ][ $db_row->id ][] = $field_name;
$foreign_id_keys[ $foreign_table ] = $foreign_id_key;
}
}
}
}
$result->rows = $rows;
$query->free_result();
// Resolve Foreign Rows
foreach( $ids_to_fetch as $table_name=>$row_ids )
{
$foreign_id_key = $foreign_id_keys[ $table_name ];
$this->db->select()
->from( $table_name )
->where_in( $foreign_id_key, $row_ids );
$query = $this->db->get();
$this->log[] = 'db query: '.str_replace( "\n", ' ', $this->db->last_query() );
$rows = $query->result();
foreach( $rows as $row )
{
$foreign_id = $row->$foreign_id_key;
$interested_tables = $rows_to_populate[ $table_name ][ $foreign_id ];
foreach( $interested_tables as $interested_table_name=>$interested_rows )
{
$this->module_row_cache[ $table_name ][ $foreign_id ] = $row;
foreach( $interested_rows as $interested_id=>$field_names )
{
foreach ( $field_names as $field_name )
{
$cell = $this->module_row_cache[ $interested_table_name ][ $interested_id ];
$cell->$field_name = $row;
}
}
}
}
}
}
}
if ( $result->error != '' ) $this->log[] = 'error: '.$result->error;
$result->success = ( $result->error == '' );
$result->json_size = strlen( json_encode( $result ) );
$result->execution_time = round( microtime( true ) - $time_start, 5 );
// For Profiling
// unset($result->rows);
// unset($result->fields);
$this->log[] = 'getRows completed, execution time: '.$result->execution_time;
return $result;
}
/**
* Get an array of Module/Table Fields and cache the results
*
* @param String $module_name
* @return Array $fields OR FALSE
*/
function getFields( $module_name )
{
if ( isset( $this->module_field_cache[ $module_name ] ) )
{
return $this->module_field_cache[ $module_name ];
}
$this->db->select()
->from( 'sys_fields' )
->where( 'table_name', $module_name );
$query = $this->db->get();
$this->log[] = 'db query: ' . str_replace( "\n", ' ', $this->db->last_query() );
if ( $query->num_rows() > 0 )
{
$field_rows = $query->result();
foreach ( $field_rows as $field_row )
{
$table_name = $field_row->table_name;
$field_name = $field_row->field_name;
$this->module_field_cache[ $table_name ][ $field_name ] = $field_row;
}
$query->free_result();
return $this->module_field_cache[ $module_name ];
}
else
{
return FALSE;
}
}
/**
* Return Profiling Stats
*
* @return stdClass $stats
*/
function getProfileStats()
{
$stats = new stdClass();
$stats->log = $this->log;
// $stats->module_field_cache = $this->module_field_cache;
$stats->module_row_cache = $this->module_row_cache;
return $stats;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment