Back to PhocacartPosPrint class

Method printLineColumns

public
printLineColumns
(mixed $items, mixed $ignoreLength = 0, mixed $class = '')

Method printLineColumns - Source code

public function printLineColumns($items, $ignoreLength = 0, $class = '')
{
    $startTag = '<div>';
    $endTag = '</div>';
    if ($class != '') {
        $startTag = '<div class="' . $class . '">';
    }
    $item = array();
    $item['output'] = array();
    $item['outputcolumn1'] = '';
    $item['outputcolumn2'] = '';
    $item['length'] = array();
    $item['lengthcolumn1'] = 0;
    $item['lengthcolumn2'] = 0;
    $item['lengthsum'] = 0;
    $item['count'] = count($items);
    $item['lastitem'] = $item['count'] - 1;
    if (!empty($items)) {
        foreach ($items as $k => $v) {
            $item['output'][$k] = $v;
            $item['length'][$k] = StringHelper::strlen($v);
            $item['lengthsum'] += StringHelper::strlen($v);
            // Divide all items into two columns (left and rights
            // First left column are all items except the last one
            // Second right column is only last item
            // If there is only one item (first = last) - don't set it as last
            if ($k == $item['lastitem'] && $k > 0) {
                $item['outputcolumn2'] = $item['outputcolumn2'] != '' ? $item['outputcolumn2'] . ' ' . $v : $v;
                $item['lengthcolumn2'] = StringHelper::strlen($item['outputcolumn2']);
            } else {
                $item['outputcolumn1'] = $item['outputcolumn1'] != '' ? $item['outputcolumn1'] . ' ' . $v : $v;
                $item['lengthcolumn1'] = StringHelper::strlen($item['outputcolumn1']);
            }
        }
    }
    if ($ignoreLength == 1) {
        return $startTag . trim(implode(' ', $item['output'])) . $endTag . $this->lineEnd;
    }
    if ($class == 'pDoubleSize') {
        $sizeRow = $this->lineLengthDoubleSize;
    } else {
        $sizeRow = $this->lineLength;
    }
    $o = '';
    $spaces = 0;
    $newLengthSum = (int) $item['lengthcolumn1'] + (int) $item['lengthcolumn2'];
    // We need to count each column because the became larger through join of spaces
    if ($newLengthSum < (int) $sizeRow || $newLengthSum == (int) $sizeRow) {
        // OK - we can add all items to one row
        $spaces = (int) $sizeRow - $newLengthSum;
        $length = (int) $item['lengthcolumn1'] + $spaces;
        //$item['outputcolumn1'] = str_replace('€', 'E', $item['outputcolumn1']);
        //$item['outputcolumn2'] = str_replace('€', 'E', $item['outputcolumn2']);
        $o .= $startTag . StringHelper::str_pad($item['outputcolumn1'], $length, ' ', STR_PAD_RIGHT) . $item['outputcolumn2'] . $endTag . $this->lineEnd;
    } else {
        $o .= $startTag . $item['outputcolumn1'] . $endTag . $this->lineEnd;
        // Possible TO DO - divide first column into blocks by items
        $o .= $startTag . $item['outputcolumn2'] . $endTag . $this->lineEnd;
    }
    return $o;
}