Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 81 |
| iaTableIt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
2352.00 | |
0.00% |
0 / 81 |
| tableIt_css | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| tableIt | |
0.00% |
0 / 1 |
306.00 | |
0.00% |
0 / 33 |
|||
| valueClass | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 6 |
|||
| headerDeduce | |
0.00% |
0 / 1 |
462.00 | |
0.00% |
0 / 33 |
|||
| labelIt | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 6 |
|||
| isNumber | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| <?php | |
| namespace ia\Lib; | |
| use ia\Lib\iaPalabra; | |
| /** | |
| * iaTableIt | |
| * | |
| * Simple echo Array to table, keys are column headers | |
| * | |
| * @copyright 2014 | |
| * @version 1.0.1 | |
| */ | |
| class iaTableIt { | |
| /** @var bool $useIaPalabra true: supone headers en español intenta arreglar ortografía, false no lo intenta */ | |
| public static $useIaPalabra = true; | |
| /** | |
| * Put css for tablit inside style tags | |
| * | |
| * @return void | |
| */ | |
| public static function tableIt_css() { | |
| ?> | |
| .tableitdiv{overflow-x:auto;border:4px blue double;} | |
| .tableit, .tableit TH, .tableit TD {border: 1px solid silver} | |
| .tableit {border-collapse:collapse;border-spacing:0;margin:auto;overflow-x:hidden;empty-cells:show} | |
| .tableit CAPTION {font-weight:bold;font-size:larger;} | |
| .tableit TH {padding:4px;background-color: whitesmoke} | |
| .tableit TD {padding:4px;vertical-align:top} | |
| .tableit TR:hover {background-color: #f5f5f5} | |
| .tableit TR:nth-child(even) {background-color: #f2f2f2} | |
| .tableit .lft {text-align:left} | |
| .tableit .cen {text-align:center} | |
| .tableit .rgt {text-align:right} | |
| .tableit .date {text-align:center;white-space:pre-line} | |
| .tableit .red {color:red} | |
| .tableit .green{color:darkgreen} | |
| .tableit .blue{color:blue} | |
| .tableit .remark{background-color:yellow} | |
| <?php | |
| } | |
| /** | |
| * Simple echo Array to table, keys are column headers | |
| * | |
| * @param array $arr Data for table [ ['key1'=>1,'key2'=>'name'], ...] | |
| * @param string $caption | |
| * @param array $header [ 'key'=>['label'=>'', 'class'=>'', 'dec'=>4, 'prefix'=>'$', 'suffix'=>'MN', 'valueClass'=>['Si'=>'red', 'SI'=>'red'] ], ] | |
| * @param boolean $niceHeaders true label($key), false use keys. default true | |
| * @param string $idPrefix sets id for html tags: table id='tableit$idPrefix', tr id='tr$idPrefix$id' default tblIt | |
| * @param string $tableClass sets contining div to class='$tableClassdiv' and table's class='$tableClass'. Default tableit | |
| * @return void | |
| */ | |
| public static function tableIt($arr,$caption='',$header=array(), $niceHeaders=true,$idPrefix='tblIt',$tableClass='tableit') { | |
| echo "<div class='$tableClass"."div'><table id='tableit$idPrefix' class='$tableClass tablesorter'>"; | |
| if(empty($arr)) { | |
| echo "</table></div>"; | |
| return; | |
| } | |
| if(!empty($caption)) { | |
| echo "<caption>$caption</caption>"; | |
| } | |
| echo "<thead><tr>"; | |
| $header = self::headerDeduce($arr, $header, $niceHeaders); | |
| foreach($header as $key => $h) { | |
| echo "<th>".(isset($h['label']) ? $h['label'] : htmlentities($key,ENT_NOQUOTES,'UTF-8')); | |
| } | |
| echo "</thead>"; | |
| echo "<tbody>"; | |
| foreach($arr as $id => $row) { | |
| echo "<tr id='tr$idPrefix$id'>"; | |
| foreach($header as $key => $h) { | |
| if(!isset($row[$key])) | |
| echo "<td class='$h[class]'>"; | |
| else { | |
| $color = self::valueClass($row[$key], $h); | |
| if(isset($h['dec']) && self::isNumber($row[$key]) ) { | |
| echo "<td class='$h[class]".($row[$key] < 0 ? ' red' : '')." $color'>"; | |
| if(!empty($h['prefix'])) | |
| echo $h['prefix']; | |
| echo is_numeric($row[$key]) ? number_format($row[$key], $h['dec'], $h['dec'] ? '.' : '' , ',') : htmlentities($row[$key]); | |
| } else { | |
| echo "<td class='$h[class] $color'>"; | |
| if(!empty($h['prefix'])) | |
| echo $h['prefix']; | |
| if(!empty($h['html'])) | |
| echo $row[$key]; | |
| else | |
| echo htmlentities($row[$key],ENT_NOQUOTES,'UTF-8'); | |
| } | |
| if(!empty($h['suffix'])) { | |
| echo $h['suffix']; | |
| } | |
| } | |
| } | |
| } | |
| echo "</tbody></table></div>"; | |
| } | |
| /** | |
| * Add class if value matches $columnHeader['valueClass'] | |
| * | |
| * @param mixed $value value for the cell | |
| * @param array $columnHeader for column | |
| * @return string class to add according to $columnHeader['valueClass'] or '' | |
| */ | |
| protected static function valueClass($value, $columnHeader) { | |
| if(empty($columnHeader['valueClass'])) { | |
| return ''; | |
| } | |
| foreach($columnHeader['valueClass'] as $find => $class) { | |
| if($value == $find) { | |
| return $class; | |
| } | |
| } | |
| return ''; | |
| } | |
| /** | |
| * Deduce column definition respecting defaults in $headerIn | |
| * | |
| * @param array $arr Data for table | |
| * @param array $headerIn default column definition | |
| * @param bool $niceHeaders true = process key for nice header (accent,...). flase=just use keys for headers | |
| * @return array complete and deduce headers | |
| */ | |
| protected static function headerDeduce($arr, $headerIn, $niceHeaders=true) { | |
| $header = []; | |
| $row1 = reset($arr); | |
| foreach($row1 as $key => $ignore) { | |
| $header[$key] = empty($headerIn[$key]) ? [] : $headerIn[$key]; | |
| } | |
| $irow=0; | |
| foreach($arr as $row) { | |
| if($irow++ > 10) { | |
| break; | |
| } | |
| if($irow===1) { | |
| foreach($row as $key=>$data) { | |
| if(isset($header[$key])) { | |
| } | |
| if(!array_key_exists('label', $header[$key])) { | |
| $header[$key]['label'] = self::labelIt($key, $niceHeaders); | |
| } | |
| $class = null; | |
| if(self::isNumber($data) ) { | |
| $class = 'rgt'; | |
| if(!isset($header[$key]['dec'])) { | |
| $header[$key]['dec'] = strpos($data,'.') === false ? 0 : 2; | |
| } | |
| } elseif(($len=strlen($data))<=4) { | |
| $class = 'cen'; | |
| } elseif($len === 10) { | |
| $class = 'date'; | |
| } else { | |
| $class = 'lft'; | |
| } | |
| if($class !== null && empty($header[$key]['class'])) { | |
| $header[$key]['class'] = $class; | |
| } | |
| } // foreach($row as $key=>$Data) | |
| } else { | |
| foreach($row as $key=>$data) { | |
| $head = &$header[$key]; | |
| if(self::isNumber($data)) { | |
| if(empty($head['class'])) { | |
| $head['class'] = 'rgt'; | |
| } | |
| if(empty($head['dec']) && strpos($data,'.')) { | |
| $head['dec'] = 2; | |
| } | |
| } | |
| } // foreach($row as $key=>$Data) | |
| } | |
| } // foreach($arr as $row) | |
| return $header; | |
| } | |
| /** | |
| * On $niceHeaders=true make nice words for header from s | |
| * | |
| * @param string $s | |
| * @param boolean $niceHeaders | |
| * @return string | |
| */ | |
| protected static function labelIt($s, $niceHeaders) { | |
| if(!$niceHeaders || !self::$useIaPalabra) { | |
| return ucwords($s); | |
| } | |
| $words = explode(' ',str_replace('_', ' ', $s)); | |
| foreach($words as &$w) { | |
| $w = iaPalabra::acentua( $w ); | |
| } | |
| return iaPalabra::ucwords(implode(' ',$words)); | |
| } | |
| /** | |
| * Determine if string $s is a number | |
| * | |
| * @param mixed $s item to check if it is a number | |
| * @return bool true $s is a number | |
| */ | |
| protected static function isNumber($s) { | |
| return is_numeric(str_replace([',','$',' '], '', $s)); | |
| } | |
| } |