Some snippets from legacy code I have to deal with at work .

============================

This code does something

=============================

$values = explode ( "," , $data [ $i ] [ 'values' ] ) ;

for ( $x = 0 ; $x < count ( $fields ) ; $x ++ ) {

if ( $fields [ $x ] != '' ) {

$data [ $i ] [ $fields [ $x ] ] = $values [ $x ] ;

if ( $i > 0 ) {

if ( $data [ $i ] [ $fields [ $x ] ] > $data [ $i - 1 ] [ $fields [ $x ] ] )

$changes = '+' . ( $data [ $i ] [ $fields [ $x ] ] - $data [ $i - 1 ] [ $fields [ $x ] ] ) . '' ;

elseif ( $data [ $i ] [ $fields [ $x ] ] < $data [ $i - 1 ] [ $fields [ $x ] ] )

$changes = '-' . ( $data [ $i - 1 ] [ $fields [ $x ] ] - $data [ $i - 1 ] [ $fields [ $x ] ] ) . '' ;

else

$changes = '' ;

} else

$changes = '' ;

$percent = '(' . intval ( ( $values [ $x ] / $data [ $i ] [ 'total_record' ] ) * 100 ) . '%)' ;

$display [ $submenu_nav ] [ $i ] [ $fields [ $x ] ] = $values [ $x ] . ' ' . $percent . ' ' . $changes . '' ;

}

}

========================

$db_type used everywhere

========================

// check if user logged in otherwise check all the links from fields..

if ( $GLOBALS [ 'identity' ] -> access_level == 0 ) {

$GLOBALS [ 'readonly_fields' ] [ $db_type ] = array_merge ( $GLOBALS [ $db_type . '_field' ] , $GLOBALS [ $db_type . '_submenu_field' ] , $GLOBALS [ $db_type . '_field_text' ] , $GLOBALS [ $db_type . '_submenu_field_text' ] , $GLOBALS [ $db_type . '_field_one' ] , $GLOBALS [ $db_type . '_submenu_field_one' ] , $GLOBALS [ $db_type . '_field_many' ] , $GLOBALS [ $db_type . '_submenu_field_many' ] , array ( 'logo_pic_name' , 'website' , 'username' ) ) ;

if ( isset ( $_GET [ 'sub_type' ] ) && isset ( $GLOBALS [ $db_type . '_submenu' ] [ $_GET [ 'sub_type' ] ] [ 'submenu_fields' ] ) ) {

$GLOBALS [ 'readonly_fields' ] [ $db_type ] = array_merge ( $GLOBALS [ 'readonly_fields' ] [ $db_type ] , $GLOBALS [ $db_type . '_submenu' ] [ $_GET [ 'sub_type' ] ] [ 'submenu_fields' ] ) ;

}

}

======================

At least the view code is simple

=====================

//////////////////////////////////////////////////////////////////////////////////////////////////////

$count = '' ;

if ( isset ( $_SESSION [ $db_type . '_submenu' ] [ 'photos' ] [ 'submenu_count' ] ) && $_SESSION [ $db_type . '_submenu' ] [ 'photos' ] [ 'submenu_count' ] > 0 )

$count = $_SESSION [ $db_type . '_submenu' ] [ 'photos' ] [ 'submenu_count' ] ;

echo '<a rel="nofollow" id="products_photos" href="' . $GLOBALS [ 'bor_url' ] . '?script=submenu&nav=photos&' . $GLOBALS [ 'url_parameters' ] [ $db_type ] [ 'url_id' ] . '=' . $display [ 'product_id' ] . '#products_photos" class="submenu_' ;

if ( $submenu_nav != "photos" ) { echo 'un' ; }

echo 'selected">Photos' ;

if ( $submenu_nav != "photos" && $count != '' ) echo '<span class="tab_notification">' . $count . '</span>' ;

echo '</a>' ;

/////////////////////////////////////////////////////////////

$count = '' ;

if ( isset ( $_SESSION [ $db_type . '_submenu' ] [ 'products_care' ] [ 'submenu_count' ] ) && $_SESSION [ $db_type . '_submenu' ] [ 'products_care' ] [ 'submenu_count' ] > 0 )

$count = $_SESSION [ $db_type . '_submenu' ] [ 'products_care' ] [ 'submenu_count' ] ;

echo '<a rel="nofollow" id="products_care" href="' . $GLOBALS [ 'bor_url' ] . '?script=submenu&nav=product_care&' . $GLOBALS [ 'url_parameters' ] [ $db_type ] [ 'url_id' ] . '=' . $display [ 'products_id' ] . '#products_care" class="submenu_' ;

if ( $submenu_nav != "product_care" ) { echo 'un' ; }

echo 'selected">product care' ;

if ( $submenu_nav != "product_care" && $count != '' ) echo '<span class="tab_notification">' . $count . '</span>' ;

echo '</a>' ;

/////////////////////////////////////////////////////////////

$count = '' ;

if ( isset ( $_SESSION [ $db_type . '_submenu' ] [ 'goeswell' ] [ 'submenu_count' ] ) && $_SESSION [ $db_type . '_submenu' ] [ 'goeswell' ] [ 'submenu_count' ] > 0 )

$count = $_SESSION [ $db_type . '_submenu' ] [ 'goeswell' ] [ 'submenu_count' ] ;

echo '<a rel="nofollow" id="products_goeswell" href="' . $GLOBALS [ 'bor_url' ] . '?script=submenu&nav=goeswell&' . $GLOBALS [ 'url_parameters' ] [ $db_type ] [ 'url_id' ] . '=' . $display [ 'product_id' ] . '#products_goeswell" class="submenu_' ;

if ( $submenu_nav != "goeswell" ) { echo 'un' ; }

echo 'selected">Goes well with' ;

if ( $submenu_nav != "goeswell" && $count != '' ) echo '<span class="tab_notification">' . $count . '</span>' ;

echo '</a>' ;

/////////////////////////////////////////////////////////////

$count = '' ;

if ( isset ( $_SESSION [ $db_type . '_submenu' ] [ 'alternatives' ] [ 'submenu_count' ] ) && $_SESSION [ $db_type . '_submenu' ] [ 'alternatives' ] [ 'submenu_count' ] > 0 )

$count = $_SESSION [ $db_type . '_submenu' ] [ 'alternatives' ] [ 'submenu_count' ] ;

echo '<a rel="nofollow" id="products_alternatives" href="' . $GLOBALS [ 'bor_url' ] . '?script=submenu&nav=alternatives&' . $GLOBALS [ 'url_parameters' ] [ $db_type ] [ 'url_id' ] . '=' . $display [ 'product_id' ] . '#products_alternatives" class="submenu_' ;

if ( $submenu_nav != "alternatives" ) { echo 'un' ; }

echo 'selected">Alternatives' ;

if ( $submenu_nav != "alternatives" && $count != '' ) echo '<span class="tab_notification">' . $count . '</span>' ;

echo '</a>' ;

//////////////////////////////////////////////////////////////

$count = '' ;

if ( isset ( $_SESSION [ $db_type . '_submenu' ] [ 'affected' ] [ 'submenu_count' ] ) && $_SESSION [ $db_type . '_submenu' ] [ 'affected' ] [ 'submenu_count' ] > 0 )

$count = $_SESSION [ $db_type . '_submenu' ] [ 'affected' ] [ 'submenu_count' ] ;

echo '<a rel="nofollow" id="pests_and_diseases_affected" href="' . $GLOBALS [ 'bor_url' ] . '?script=submenu&nav=affected&' . $GLOBALS [ 'url_parameters' ] [ $db_type ] [ 'url_id' ] . '=' . $display [ 'product_id' ] . '#pests_and_diseases_affected" class="submenu_' ;

if ( $submenu_nav != "affected" ) { echo 'un' ; }

echo 'selected">Pests & diseases' ;

if ( $submenu_nav != "affected" && $count != '' ) echo '<span class="tab_notification">' . $count . '</span>' ;

echo '</a>' ;

==========================

Intellisearch

===========================

///////////////////////////////////////////////

public function intelliSearch (

$parameters , // o2/m2m conditions

$search_fields , // search fields for text search

$search_term , // text search

$fields , // show the fields

$order , // field order

$limit , // limit records

$filters // preset searches to include with the original search.. check function searchFilters for required params for each f..

)

{

$result = array ( ) ;

$db = $this -> search ;

$db_key = $GLOBALS [ 'db_key' ] [ $db ] ;

$basicFields = array ( $db_key , 'main_' . $GLOBALS [ 'url_parameters' ] [ $db ] [ 'url_id' ] . '_pic' ) ; //,'edit_user',admin_user_id,'edit_flag','edit_date');

if ( is_array ( $fields ) ) {

$result_fields = array_merge ( $fields , $basicFields ) ;

} else {

$result_fields = $basicFields ;

}

// custom_search_fields

$custom_search = array ( 'period' , 'season' ) ;

if ( $db == 'users' ) {

$search_sql = $GLOBALS [ 'dbRead' ] -> select ( )

-> from ( array ( $db => 'users' ) , $result_fields )

-> where ( $db . '.admin_user_id=' . $GLOBALS [ 'identity' ] -> user_id ) ;

} else {

// <additionalclan.dirty>

$additionalClans = '' ;

if ( isset ( $filters [ 'includeClan' ] ) && $filters [ 'includeClan' ] [ 'clan' ] <> '' ) {

$additionalClans = ' OR ' . $db . '2c.clan_' . $db_key . ' IN (' . $filters [ 'includeClan' ] [ 'clan' ] . ')' ;

}

// </additionalclan.dirty>

// user restriction

if ( $GLOBALS [ 'identity' ] -> access_level < $GLOBALS [ 'super_admin' ] ) {

/*

$clan_sql = $GLOBALS['dbRead']->select()

->from(array('u2c'=>'users2clan'),'')

->join(array($db.'2u'=>$db.'2users'),'('.$db.'2u.user_id=u2c.clan_user_id)','')

->join(array($db.'2c'=>$db.'2clan'),$db.'2c.clan_'.$db_key.'='.$db.'2u.'.$db_key.$additionalClans,array($db_key));

if (0 == $GLOBALS['identity']->user_id){

$clan_sql ->where('u2c.user_id =?',0);

} else {

$clan_sql ->where('u2c.user_id IN (0,'.$GLOBALS['identity']->user_id.')');

}

*/

if ( 0 == $GLOBALS [ 'identity' ] -> user_id ) {

$clan_sql = $GLOBALS [ 'dbRead' ] -> select ( )

-> from ( array ( 'u2c' => 'users2clan' ) , '' )

-> join ( array ( $db . '2u' => $db . '2users' ) , '(' . $db . '2u.user_id=u2c.clan_user_id)' , '' )

-> join ( array ( $db . '2c' => $db . '2clan' ) , $db . '2c.clan_' . $db_key . '=' . $db . '2u.' . $db_key . $additionalClans , array ( $db_key ) )

-> where ( 'u2c.user_id =?' , 0 ) ;

} else {

$clan_sql = $GLOBALS [ 'dbRead' ] -> select ( )

-> from ( array ( 'u' => 'users' ) , '' )

-> joinleft ( array ( 'u2c' => 'users2clan' ) , 'u.user_id=u2c.user_id' , '' )

-> join ( array ( $db . '2u' => $db . '2users' ) , '(' . $db . '2u.user_id=u.user_id OR ' . $db . '2u.user_id=u2c.clan_user_id)' , '' )

-> join ( array ( $db . '2c' => $db . '2clan' ) , $db . '2c.clan_' . $db_key . '=' . $db . '2u.' . $db_key . $additionalClans , array ( $db_key ) )

-> where ( 'u.user_id IN (0,' . $GLOBALS [ 'identity' ] -> user_id . ')' ) ;

}

$search_sql = $GLOBALS [ 'dbRead' ] -> select ( )

-> from ( array ( $db ) , $result_fields )

-> where ( $db . '.admin_user_id=' . $GLOBALS [ 'identity' ] -> user_id . ' OR ' . $db . '.clan=0 OR ' . $db . '.' . $db_key . ' IN (' . $clan_sql . ')' )

-> order ( $order ) ;

if ( $GLOBALS [ 'identity' ] -> access_level < $GLOBALS [ 'editor_access_level' ] ) {

$search_sql -> where ( $db . '.clan <> ' . $db . '.' . $GLOBALS [ 'db_key' ] [ $db ] ) ;

}

}

}

if ( $GLOBALS [ 'identity' ] -> access_level >= $GLOBALS [ 'super_admin' ] ) {

//// Big Brother is logged in..

$search_sql = $GLOBALS [ 'dbRead' ] -> select ( )

-> from ( array ( $db => $db ) , $result_fields )

-> order ( $order ) ;

}

//ACTUAL SEARCH////////

// single, o2m and m2m

if ( $parameters ) {

foreach ( $parameters as $key => $val ) {

if ( in_array ( $key , $custom_search ) && $val <> 'all' && $val <> '' ) {

$search_sql = $this -> customSearch ( $search_sql , $key , $val ) ;

} elseif ( isset ( $GLOBALS [ 'field_relations' ] [ $this -> search ] [ $key ] ) && $val <> 'all' && $val <> '' ) {

$search_sql = $this -> searchSQL ( $search_sql , $key , $val , $GLOBALS [ 'field_relations' ] [ $this -> search ] [ $key ] ) ;

$search_sql -> group ( $GLOBALS [ 'db_key' ] [ $this -> search ] ) ;

} elseif ( in_array ( $key , $GLOBALS [ $this -> search . '_field' ] ) && $val <> 'all' && $val <> '' ) {

$search_sql -> where ( $key . ' LIKE "%' . $val . '%"' ) ;

} elseif ( $val == '' || $val == 'all' ) {

unset ( $parameters [ $key ] ) ;

if ( isset ( $_SESSION [ $this -> search ] [ 'search_values' ] [ $key ] ) ) {

unset ( $_SESSION [ $this -> search ] [ 'search_values' ] [ $key ] ) ;

}

}

}

if ( is_array ( $parameters ) && count ( $parameters ) > 0 ) {

//includeFiltersFrom

$filters = $this -> presetFilters ( $filters , $parameters ) ;

}

}

// Search Input //

if ( $search_term <> '' ) {

$search_sql = $this -> searchSpecificText ( $search_sql , $search_fields , $search_term ) ;

}

//ORDER

if ( is_array ( $order ) && in_array ( 'genus_name' , $order ) ) {

$search_sql -> reset ( 'order' ) ;

$search_sql -> order ( 'concat_ws("",' . implode ( ',' , $order ) . ')' ) ;

}

//LIMIT

if ( intval ( $limit ) > 0 ) {

$search_sql -> limit ( intval ( $limit ) ) ;

}

//FILTERS

//executeFilters

$search_sql = $this -> addFilters ( $search_sql , $filters ) ;

/////////////////////////////

//echo '<pre>';

//print_r($filters);

// print_r($parameters);

//echo '</pre>';

//if($GLOBALS['identity']->user_id == 34){

/////////////////////////////

// //

// //

// echo $search_sql; //

// //

// //

/////////////////////////////

//}

//die;

return $search_sql ;