Location: PHPKode > scripts > Meta Builder > includes/builder/Grid.php
<?php

/*
 * BuilderGrid
 *
 * Copyright(c) 2010, Thomas Shone
 * Licensed under the Creative Commons Attribution 3.0 Unported License.
 * http://www.shone.co.za
 *
 * Constructs grids from meta data
 */

class BuilderGrid extends BuilderBase
{
    protected $iColCount;
    protected $iColSpan;

    protected function ConstructGridData($aMeta)
    {
        $this->iColCount = is_array($aMeta['columns']) ? count($aMeta['columns']) : 0;
        $this->iColSpan = $this->iColCount;
        $this->sGroup = !empty($aMeta['group']) ? $aMeta['group'] : '';
    }

    protected function ConstructFlags($aMeta)
    {
        $aFlags = array('grid' => true);
        foreach ($aMeta['columns'] as $aColumn)
        {
            if (!empty($aColumn['sort']))
            {
                $aFlags['sortable'] = true;
            }
        }

        if (!empty($aMeta['paging']))
        {
            $aFlags['paging'] = true;
        }

        return array_keys($aFlags);
    }

    protected function ConstructFlagMeta($aMeta)
    {
        $aFlagsMeta = array();
        if (!empty($aMeta['paging']['num-records']))
        {
            $aFlagsMeta[] = $aMeta['paging']['num-records'];
        }

        return $aFlagsMeta;
    }

    protected function ConstructActions($aActions)
    {
        foreach ($aActions as $aAction)
        {
            $this->sResult .= "<input class=\"button " . (!empty($aAction['class']) ? $aAction['class'] : '') . "\" type=\"submit\" name=\"input[action]\" value=\"" . $aAction['name'] . "\" />\n";
        }
    }

    protected function BuildColumns($aColumns)
    {
        $this->sResult .= "<thead class=\"grid-columns\">\n<tr>\n";
        foreach ($aColumns as $aColumn)
        {
            $aClasses = array('grid-column');
            if (!empty($aColumn['sort']))
            {
                if (in_array($aColumn['sort'], array('alpha', 'numeric')))
                {
                    $aClasses[] = 'grid-sort-' . $aColumn['sort'];
                }
                else
                {
                    $aClasses[] = 'grid-sort-alpha';
                }
            }
            $this->sResult .= '<td class="'. implode(' ', $aClasses) .'">'. $aColumn['label'] .'</td>' ."\n";
        }
        $this->sResult .= "</tr>\n</thead>\n";

        $this->BuildData($aColumns, $this->aData);
    }

    protected function BuildData($aHeaders, $aData)
    {
        $this->sResult .= "<tbody class=\"grid-dataset\">\n";
        if (!empty($aData))
        {
            $sGroup = '';
            //$sRowClass = 'row-odd';
            $sRowClass = '';
            $iRowCount = 0;

            foreach ($aData as $sKey => $aRow)
            {
                $iRowCount++;
                // Entry grouping
                if (isset($aRow[$this->sGroup]) && $sGroup != $aRow[$this->sGroup])
                {
                    $this->sResult .= '<tr class="grid-group-header"><td colspan="' . $this->iColSpan . '">' . $aRow[$this->sGroup] . '</td></tr>' ."\n";
                    $this->sResult .= '<tbody class="grid-group" id="grid-group-' . $aRow[$this->sGroup] . '">' ."\n";
                }

                //$sColumnClass = 'column-odd';
                $sColumnClass = '';
                $this->sResult .= "<tr class=\"grid-row\" id=\"grid-row-". $iRowCount . "\">\n";
                foreach ($aHeaders as $aHeader)
                {
                    $sFormat = !empty($aHeader['format']) ? $aHeader['format'] : '';
                    $mValue = $this->ConstructField($aRow, $aHeader['name'], $sFormat);

                    if (!empty($aHeader['type']))
                    {
                        switch ($aHeader['type'])
                        {
                            case 'checkbox':
                                $mValue = '<input type="checkbox" class="grid-checkbox" id="checkbox-'. $aHeader['name'] .'-'. $iRowCount .'" name="input['. $aHeader['name'] .'][]" value="' . $mValue. '" />';
                                break;
                        }
                    }

                    if (!empty($aHeader['link']))
                    {
                        $aParams = array();
                        $sLink = $aHeader['link']['path'];

                        if (count($aHeader['link']['param']))
                        {
                            foreach ($aHeader['link']['param'] as $aParam)
                            {
                                $aParams[] = $aParam['name'] . '=' . $aRow[$aParam['key']];
                            }
                            $sLink .= '?' . implode('&', $aParams);
                        }

                        $mValue = '<a href="' . $sLink . '">' . $mValue . '</a>';
                    }

                    $this->sResult .= '<td class="grid-cell">'. $mValue .'</td>' ."\n";

                    //$sColumnClass = $sColumnClass == 'column-odd' ? 'column-even' : 'column-odd';
                }
                $this->sResult .= "</tr>\n";

                //$sRowClass = $sRowClass == 'row-odd' ? 'row-even' : 'row-odd';

                // Entry grouping
                if (isset($aRow[$this->sGroup]) && $sGroup != $aRow[$this->sGroup])
                {
                    $this->sResult .= '</tbody>';
                    $sGroup = $aRow[$this->sGroup];
                }
            }
        }
        else
        {
            $this->sResult .= "<tr><td colspan=\"". $this->iColSpan ."\"><i>No entries found</i></td></tr>\n";
        }
        $this->sResult .= "</tbody>\n";
        $this->sResult .= "<tfoot>\n";
        $this->sResult .= "<tr><td colspan=\"". $this->iColSpan ."\">\n";

        if (!empty($this->aMeta['action']['footer']))
        {
            $this->ConstructActions($this->aMeta['action']['footer']);
        }

        $this->sResult .= "</td></tr>\n";
        $this->sResult .= "</tfoot>\n";
    }

    protected function BuildIdentity($sIdentity)
    {
        $this->sResult .= "<input type=\"hidden\" name=\"input[identity]\" value=\"" . $sIdentity . "\">\n";
    }

    protected function BuildWorkflow($sWorkflow)
    {
        $this->sResult .= "<input type=\"hidden\" name=\"input[workflow]\" value=\"" . $sWorkflow . "\">\n";
    }

    public function Render($aMeta, $aData = array())
    {
        $this->SetConfig($aMeta, $aData);
        $this->ConstructGridData($aMeta);
        $aFlags = $this->ConstructFlags($aMeta);
        $aRel = $this->ConstructFlagMeta($aMeta);

//        $this->sResult = "<script src=\"html/javascript/lib/grid.js\" type=\"text/javascript\"></script>\n";
        if (!empty($aMeta['identity']) && !empty($aMeta['workflow']))
        {
            $sAction = !empty($aMeta['script']) ? $aMeta['script'] : 'form.php';
            $this->sResult .= "<form method=\"post\" action=\"" . BuildUrl($sAction) ."\" id=\"" . $aMeta['identity'] . "\">\n";
            $this->BuildIdentity($aMeta['identity']);
            $this->BuildWorkflow($aMeta['workflow']);
        }
        $this->sResult .= "<table class=\"" . implode(' ', $aFlags) . "\" rel=\"" . implode(' ', $aRel) . "\" id=\"" . $aMeta['identity'] . "-table\" cellspacing=\"0\">\n<thead>\n";
        !empty($aMeta['header'])    && $this->sResult .= '<tr><td colspan="' . $this->iColSpan . '" class="grid-header">'    . $aMeta['header']    . '</td></tr>' . "\n";
        !empty($aMeta['subheader']) && $this->sResult .= '<tr><td colspan="' . $this->iColSpan . '" class="grid-subheader">' . $aMeta['subheader'] . '</td></tr>' . "\n";
        $this->sResult .= "</thead>\n";
        $this->Build($aMeta);
        $this->sResult .= "</table>\n";

        if (!empty($aMeta['identity']) && !empty($aMeta['workflow']))
        {
            $this->sResult .= "</form>\n";
        }

        return $this->sResult;
    }
}
Return current item: Meta Builder