Created
May 27, 2020 18:59
-
-
Save hadamlenz/b0aacdc2d8f53293b156eaae86f44612 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Given an array of members looks for $width_param and tries to figure out the columns widths for all members | |
* will return equal parts if all members do not have $width_param set | |
* will fill in the blank if one is set and another is not | |
* returns false if $members is not an array or is empty | |
* | |
* @param [array] $members | |
* @param [string] $breakpoint | |
* @param [string] $width_param | |
* @return [array] $members | |
*/ | |
function column_width_classes( $members, $breakpoint, $width_param = 'column_width' ){ | |
if( !is_array( $members ) || empty( $members ) ){ | |
return false; | |
} | |
$widths_array = array(); | |
$count = count( $members ); | |
$class_prefix = 'col-' . $breakpoint . '-'; | |
//get all the $width_params into $widths_array | |
for( $i = 0; $i < $count; $i++ ){ | |
$widths_array[$i] = $members[$i][$width_param]; | |
} | |
//get the sum of all widths | |
$sum = array_sum( $widths_array ); | |
//see if both values are set | |
$set_values = array(); | |
$all_set = true; | |
//determin if all are set | |
for( $i = 0; $i < $count; $i++ ){ | |
if( "" === $members[$i][$width_param] ){ | |
$set_values[$i] = 'false'; | |
$all_set = false; | |
} else { | |
$set_values[$i] = 'true'; | |
} | |
} | |
//error_log( 'sum=' . $sum ); | |
//determin what the classes will be | |
if( 0 === $sum ){ //no widths have been set | |
$common_width = floor( 12/$count ); | |
$common_class = $class_prefix . $common_width; | |
for( $i = 0; $i < $count; $i++ ){ | |
$members[$i][$width_param] = $common_class; | |
} | |
} elseif( 0 < $sum && 12 > $sum ){ //sum is not 12 | |
if( $all_set ){ //both are set | |
for( $i = 0; $i < $count; $i++ ){ | |
$members[$i][$width_param] = $class_prefix . $members[$i][$width_param]; | |
} | |
} else { //not all of them are set, set the blank ones equally | |
$freqency = array_count_values( $set_values ); | |
$common_width = ( floor( 12/$freqency['false'] ) ) - $sum; | |
for( $i = 0; $i < $count; $i++ ){ | |
if( 'false' === $set_values[$i] ){ | |
$members[$i][$width_param] = $class_prefix . $common_width; | |
} elseif( 'true' === $set_values[$i] ){ | |
$members[$i][$width_param] = $class_prefix . $members[$i][$width_param]; | |
} | |
} | |
} | |
} elseif( 12 == $sum ){ //the whole grid has been used | |
//just add the classes | |
for( $i = 0; $i < $count; $i++ ){ | |
$members[$i][$width_param] = $class_prefix . $members[$i][$width_param]; | |
} | |
} else { //failed all above | |
//set the classes to what the user set it as so they can see that it broke | |
for( $i = 0; $i < $count; $i++ ){ | |
$members[$i][$width_param] = $class_prefix . $members[$i][$width_param]; | |
} | |
} | |
//error_log( var_export( $members, true )); | |
return $members; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment