Skip to content

Instantly share code, notes, and snippets.

@jaspertandy
Created October 26, 2012 07:57
Show Gist options
  • Select an option

  • Save jaspertandy/3957525 to your computer and use it in GitHub Desktop.

Select an option

Save jaspertandy/3957525 to your computer and use it in GitHub Desktop.
<?php
class Hex_Rows_Responsive_Breaks {
/**
* key: modulus for row breaks
* value = array of classes where key is "break" for the break and "pad" for the pad, for example:
*
array(
7 => array(
'break' => 'mq-mid mq-small',
'pad' => 'mq-mid-i-block mq-small-i-block'
),
9 => array(
'break' => 'mq-wide',
'pad' => 'mq-wide-i-block'
),
)
*/
public $break_at = array();
public $low_start = false; // true if we need to use the smaller half to lead off
public $view = 'work/clients/item.php';
public $view_empty = 'work/clients/empty.php';
public function __construct(){}
public function render( $images ){
$views = array();
$c = 0;
foreach ( $images as $image ) {
if ( $c > 0 ) {
foreach ( $this->break_at as $mod => $classes ) {
$low = floor( $mod / 2 );
$is_low = $this->low_start && $c == $low;
if ( $is_low || $c % $mod === 0 ) {
if ( $is_low ) {
$this->low_start = false;
$c = 0;
}
$views[] = '<br class="'. $classes['break'] .'">';
}
}
}
if ( is_object( $image ) && get_class( $image ) === 'View' ) {
$views[] = $image;
} else {
$view = View::f( $this->view );
foreach ( $image as $var => $value ) {
$view->$var = $value;
}
$views[] = $view;
}
$c++;
}
$count = count( $images );
foreach ( $this->break_at as $mod => $classes ) {
$views = array_merge( $views , $this->overhanging( $count , $mod , $classes['pad'] ) );
}
return implode( "" , $views );
}
private function overhanging( $displayed , $mod , $class ){
$views = array();
$pad = $this->calculate_overhang_required( $displayed , $mod );
if ( $pad > 0 ) {
foreach ( range( 1,$pad ) as $i ) {
$views[] = View::f( $this->view_empty )
->set( 'class' , $class );
}
}
return $views;
}
public function calculate_overhang_required( $displayed , $mod ){
$overhang = $displayed % $mod;
if ( $overhang == 0 ) return 0;
$complete_full_loop = $mod - $overhang;
$half = ceil( $mod / 2 );
if ( $mod - $half == $complete_full_loop ) return 0;
return $complete_full_loop > ( $half - 1 )
? $half - $overhang
: $mod - $overhang;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment