Alpha Framework 3.0.0 API Documentation
  • Namespace
  • Class

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
      • Service
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • Alpha\Controller\ActiveRecordController
  • Alpha\Controller\ArticleController
  • Alpha\Controller\AttachmentController
  • Alpha\Controller\CacheController
  • Alpha\Controller\Controller
  • Alpha\Controller\DEnumController
  • Alpha\Controller\ExcelController
  • Alpha\Controller\FeedController
  • Alpha\Controller\Front\FrontController
  • Alpha\Controller\GenSecureQueryStringController
  • Alpha\Controller\ImageController
  • Alpha\Controller\IndexController
  • Alpha\Controller\InstallController
  • Alpha\Controller\ListActiveRecordsController
  • Alpha\Controller\LogController
  • Alpha\Controller\LoginController
  • Alpha\Controller\LogoutController
  • Alpha\Controller\MetricController
  • Alpha\Controller\PhpinfoController
  • Alpha\Controller\RecordSelectorController
  • Alpha\Controller\SearchController
  • Alpha\Controller\SequenceController
  • Alpha\Controller\TagController
  • Alpha\Model\ActionLog
  • Alpha\Model\ActiveRecord
  • Alpha\Model\ActiveRecordProviderMySQL
  • Alpha\Model\ActiveRecordProviderSQLite
  • Alpha\Model\Article
  • Alpha\Model\ArticleComment
  • Alpha\Model\ArticleVote
  • Alpha\Model\BadRequest
  • Alpha\Model\BlacklistedClient
  • Alpha\Model\BlacklistedIP
  • Alpha\Model\Person
  • Alpha\Model\Rights
  • Alpha\Model\Tag
  • Alpha\Model\Type\Boolean
  • Alpha\Model\Type\Date
  • Alpha\Model\Type\DEnum
  • Alpha\Model\Type\DEnumItem
  • Alpha\Model\Type\Double
  • Alpha\Model\Type\Enum
  • Alpha\Model\Type\Integer
  • Alpha\Model\Type\Relation
  • Alpha\Model\Type\RelationLookup
  • Alpha\Model\Type\Sequence
  • Alpha\Model\Type\SmallText
  • Alpha\Model\Type\Text
  • Alpha\Model\Type\Timestamp
  • Alpha\Model\Type\Type
  • Alpha\Task\BackupTask
  • Alpha\Task\CronManager
  • Alpha\Util\Backup\BackupUtils
  • Alpha\Util\Cache\CacheProviderAPC
  • Alpha\Util\Cache\CacheProviderArray
  • Alpha\Util\Cache\CacheProviderFile
  • Alpha\Util\Cache\CacheProviderMemcache
  • Alpha\Util\Cache\CacheProviderRedis
  • Alpha\Util\Code\Highlight\HighlightProviderGeshi
  • Alpha\Util\Code\Highlight\HighlightProviderLuminous
  • Alpha\Util\Code\Metric\Inspector
  • Alpha\Util\Config\ConfigProvider
  • Alpha\Util\Convertor\ActiveRecord2Excel
  • Alpha\Util\Email\EmailProviderPHP
  • Alpha\Util\ErrorHandlers
  • Alpha\Util\Extension\Markdown
  • Alpha\Util\Extension\MarkdownFacade
  • Alpha\Util\Extension\TCPDF
  • Alpha\Util\Extension\TCPDFFacade
  • Alpha\Util\Feed\Atom
  • Alpha\Util\Feed\Feed
  • Alpha\Util\Feed\RSS
  • Alpha\Util\Feed\RSS2
  • Alpha\Util\File\FileUtils
  • Alpha\Util\Graph\GraphNode
  • Alpha\Util\Graph\TreeGraph
  • Alpha\Util\Helper\Validator
  • Alpha\Util\Http\AgentUtils
  • Alpha\Util\Http\Filter\ClientBlacklistFilter
  • Alpha\Util\Http\Filter\ClientTempBlacklistFilter
  • Alpha\Util\Http\Filter\IPBlacklistFilter
  • Alpha\Util\Http\PHPServerUtils
  • Alpha\Util\Http\Request
  • Alpha\Util\Http\Response
  • Alpha\Util\Http\Session\SessionProviderArray
  • Alpha\Util\Http\Session\SessionProviderPHP
  • Alpha\Util\Image\ImageUtils
  • Alpha\Util\InputFilter
  • Alpha\Util\Logging\KPI
  • Alpha\Util\Logging\Logger
  • Alpha\Util\Logging\LogProviderFile
  • Alpha\Util\Search\SearchProviderTags
  • Alpha\Util\Security\SecurityUtils
  • Alpha\Util\Service\ServiceFactory
  • Alpha\View\ArticleCommentView
  • Alpha\View\ArticleView
  • Alpha\View\DEnumView
  • Alpha\View\PersonView
  • Alpha\View\Renderer\Html\RendererProviderHTML
  • Alpha\View\Renderer\Json\RendererProviderJSON
  • Alpha\View\SequenceView
  • Alpha\View\View
  • Alpha\View\ViewState
  • Alpha\View\Widget\Button
  • Alpha\View\Widget\DateBox
  • Alpha\View\Widget\Image
  • Alpha\View\Widget\RecordSelector
  • Alpha\View\Widget\SmallTextBox
  • Alpha\View\Widget\TagCloud
  • Alpha\View\Widget\TextBox

Interfaces

  • Alpha\Controller\ControllerInterface
  • Alpha\Model\ActiveRecordProviderInterface
  • Alpha\Model\Type\TypeInterface
  • Alpha\Task\TaskInterface
  • Alpha\Util\Cache\CacheProviderInterface
  • Alpha\Util\Code\Highlight\HighlightProviderInterface
  • Alpha\Util\Email\EmailProviderInterface
  • Alpha\Util\Http\Filter\FilterInterface
  • Alpha\Util\Http\Session\SessionProviderInterface
  • Alpha\Util\Logging\LogProviderInterface
  • Alpha\Util\Search\SearchProviderInterface
  • Alpha\View\Renderer\RendererProviderInterface

Exceptions

  • Alpha\Exception\AlphaException
  • Alpha\Exception\BadTableNameException
  • Alpha\Exception\CustomQueryException
  • Alpha\Exception\FailedDeleteException
  • Alpha\Exception\FailedIndexCreateException
  • Alpha\Exception\FailedLookupCreateException
  • Alpha\Exception\FailedSaveException
  • Alpha\Exception\FailedUnitCommitException
  • Alpha\Exception\FileNotFoundException
  • Alpha\Exception\IllegalArguementException
  • Alpha\Exception\LockingException
  • Alpha\Exception\MailNotSentException
  • Alpha\Exception\NotImplementedException
  • Alpha\Exception\PHPException
  • Alpha\Exception\RecordNotFoundException
  • Alpha\Exception\ResourceNotAllowedException
  • Alpha\Exception\ResourceNotFoundException
  • Alpha\Exception\SearchIndexWriteException
  • Alpha\Exception\SecurityException
  • Alpha\Exception\ValidationException
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 
<?php

namespace Alpha\View;

use Alpha\Controller\Front\FrontController;
use Alpha\Util\Logging\Logger;
use Alpha\Util\Config\ConfigProvider;
use Alpha\Model\ActiveRecord;
use Alpha\Model\Type\DEnum;
use Alpha\Exception\IllegalArguementException;
use Alpha\Util\Service\ServiceFactory;
use Alpha\View\Renderer\RendererProviderInterface;
use ReflectionClass;

/**
 * The master rendering view class for the Alpha Framework.
 *
 * @since 1.0
 *
 * @author John Collins <dev@alphaframework.org>
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
 * All rights reserved.
 *
 * <pre>
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the
 * following conditions are met:
 *
 * * Redistributions of source code must retain the above
 *   copyright notice, this list of conditions and the
 *   following disclaimer.
 * * Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the
 *   following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * * Neither the name of the Alpha Framework nor the names
 *   of its contributors may be used to endorse or promote
 *   products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * </pre>
 */
class View
{
    /**
     * The business object that will be rendered.
     *
     * @var \Alpha\Model\ActiveRecord
     *
     * @since 1.0
     */
    protected $record;

    /**
     * The rendering provider that will be used to render the active record.
     *
     * @var \Alpha\View\Renderer\RendererProviderInterface
     *
     * @since 1.2
     */
    private static $provider;

    /**
     * Trace logger.
     *
     * @var Logger
     *
     * @since 1.0
     */
    private static $logger = null;

    /**
     * Constructor for the View.  As this is protected, use the View::getInstance method from a public scope.
     *
     * @param ActiveRecord $record           The main business object that this view is going to render
     * @param string       $acceptHeader Optionally pass the HTTP Accept header to select the correct renderer provider.
     *
     * @throws \Alpha\Exception\IllegalArguementException
     *
     * @since 1.0
     */
    protected function __construct($record, $acceptHeader = null)
    {
        self::$logger = new Logger('View');
        self::$logger->debug('>>__construct(Record=['.var_export($record, true).'], acceptHeader=['.$acceptHeader.'])');

        $config = ConfigProvider::getInstance();

        if ($record instanceof ActiveRecord) {
            $this->record = $record;
        } else {
            throw new IllegalArguementException('The record type provided ['.get_class($record).'] is not defined anywhere!');
        }

        self::setProvider($config->get('app.renderer.provider.name'), $acceptHeader);
        self::$provider->setRecord($this->record);

        self::$logger->debug('<<__construct');
    }

    /**
     * Static method which returns a View object or a custom child view for the Record specified
     * if one exists.
     *
     * @param ActiveRecord $record           The main business object that this view is going to render
     * @param bool         $returnParent Flag to enforce the return of this object instead of a child (defaults to false)
     * @param string       $acceptHeader Optionally pass the HTTP Accept header to select the correct renderer provider.
     *
     * @return View Returns a View object, or a child view object if one exists for this record
     *
     * @since 1.0
     */
    public static function getInstance($record, $returnParent = false, $acceptHeader = null)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>getInstance(Record=['.var_export($record, true).'], returnParent=['.$returnParent.'], acceptHeader=['.$acceptHeader.'])');

        $class = new ReflectionClass($record);
        $childView = $class->getShortname();
        $childView = $childView.'View';

        // Check to see if a custom view exists for this record, and if it does return that view instead
        if (!$returnParent) {
            $className = '\Alpha\View\\'.$childView;

            if (class_exists($className)) {
                self::$logger->debug('<<getInstance [new '.$className.'('.get_class($record).')]');

                $instance = new $className($record, $acceptHeader);

                return $instance;
            }

            $className = '\View\\'.$childView;

            if (class_exists('\View\\'.$childView)) {
                self::$logger->debug('<<getInstance [new '.$className.'('.get_class($record).')]');

                $instance = new $className($record, $acceptHeader);

                return $instance;
            }

            self::$logger->debug('<<getInstance [new View('.get_class($record).', '.$acceptHeader.')]');

            return new self($record, $acceptHeader);
        } else {
            self::$logger->debug('<<getInstance [new View('.get_class($record).', '.$acceptHeader.')]');

            return new self($record, $acceptHeader);
        }
    }

    /**
     * Simple setter for the view business object.
     *
     * @param \Alpha\Model\ActiveRecord $record
     *
     * @throws \Alpha\Exception\IllegalArguementException
     *
     * @since 1.0
     */
    public function setRecord($record)
    {
        self::$logger->debug('>>setRecord(Record=['.var_export($record, true).'])');

        if ($record instanceof \Alpha\Model\ActiveRecord) {
            $this->record = $record;
        } else {
            throw new IllegalArguementException('The Record provided ['.get_class($record).'] is not defined anywhere!');
        }

        self::$logger->debug('<<setRecord');
    }

    /**
     * Gets the Record attached to this view (if any).
     *
     * @return \Alpha\Model\ActiveRecord
     *
     * @since 1.0
     */
    public function getRecord()
    {
        return $this->record;
    }

    /**
     * Renders the default create view.
     *
     * @param array $fields Hash array of fields to pass to the template
     *
     * @return string
     *
     * @since 1.0
     */
    public function createView($fields = array())
    {
        self::$logger->debug('>>createView(fields=['.var_export($fields, true).'])');

        if (method_exists($this, 'before_createView_callback')) {
            $this->{'before_createView_callback'}();
        }

        $body = self::$provider->createView($fields);

        if (method_exists($this, 'after_createView_callback')) {
            $this->{'after_createView_callback'}();
        }

        self::$logger->debug('<<createView');

        return $body;
    }

    /**
     * Renders a form to enable object editing.
     *
     * @param array $fields Hash array of fields to pass to the template
     *
     * @return string
     *
     * @since 1.0
     */
    public function editView($fields = array())
    {
        self::$logger->debug('>>editView(fields=['.var_export($fields, true).'])');

        if (method_exists($this, 'before_editView_callback')) {
            $this->{'before_editView_callback'}();
        }

        $body = self::$provider->editView($fields);

        if (method_exists($this, 'after_editView_callback')) {
            $this->{'after_editView_callback'}();
        }

        self::$logger->debug('<<editView');

        return $body;
    }

    /**
     * Renders the list view.
     *
     * @param array $fields Hash array of fields to pass to the template
     *
     * @return string
     *
     * @since 1.0
     */
    public function listView($fields = array())
    {
        self::$logger->debug('>>listView(fields=['.var_export($fields, true).'])');

        if (method_exists($this, 'before_listView_callback')) {
            $this->{'before_listView_callback'}();
        }

        $body = self::$provider->listView($fields);

        if (method_exists($this, 'after_listView_callback')) {
            $this->{'after_listView_callback'}();
        }

        self::$logger->debug('<<listView');

        return $body;
    }

    /**
     * Renders a detailed view of the object (read-only).
     *
     * @param array $fields Hash array of fields to pass to the template
     *
     * @return string
     *
     * @since 1.0
     */
    public function detailedView($fields = array())
    {
        self::$logger->debug('>>detailedView(fields=['.var_export($fields, true).'])');

        if (method_exists($this, 'before_detailedView_callback')) {
            $this->{'before_detailedView_callback'}();
        }

        $body = self::$provider->detailedView($fields);

        if (method_exists($this, 'after_detailedView_callback')) {
            $this->{'after_detailedView_callback'}();
        }

        self::$logger->debug('<<detailedView');

        return $body;
    }

    /**
     * Renders the admin view for the business object screen.
     *
     * @param array $fields Hash array of fields to pass to the template
     *
     * @return string
     *
     * @since 1.0
     */
    public function adminView($fields = array())
    {
        self::$logger->debug('>>adminView(fields=['.var_export($fields, true).'])');

        if (method_exists($this, 'before_adminView_callback')) {
            $this->{'before_adminView_callback'}();
        }

        $body = self::$provider->adminView($fields);

        if (method_exists($this, 'after_adminView_callback')) {
            $this->{'after_adminView_callback'}();
        }

        self::$logger->debug('<<adminView');

        return $body;
    }

    /**
     * Method to render the page header content.
     *
     * @param \Alpha\Controller\Controller $controller
     *
     * @return string
     *
     * @throws \Alpha\Exception\IllegalArguementException
     *
     * @since 1.0
     */
    public static function displayPageHead($controller)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>displayPageHead(controller=['.var_export($controller, true).'])');

        if (method_exists($controller, 'before_displayPageHead_callback')) {
            $controller->{'before_displayPageHead_callback'}();
        }

        $config = ConfigProvider::getInstance();

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $header = $provider::displayPageHead($controller);

        if (method_exists($controller, 'after_displayPageHead_callback')) {
            $header .= $controller->{'after_displayPageHead_callback'}();
        }

        self::$logger->debug('<<displayPageHead ['.$header.']');

        return $header;
    }

    /**
     * Method to render the page footer content.
     *
     * @param \Alpha\Controller\Controller $controller
     *
     * @return string
     *
     * @since 1.0
     */
    public static function displayPageFoot($controller)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }

        self::$logger->debug('>>displayPageFoot(controller=['.get_class($controller).'])');

        $config = ConfigProvider::getInstance();

        $footer = '';

        if (method_exists($controller, 'before_displayPageFoot_callback')) {
            $footer .= $controller->before_displayPageFoot_callback();
        }

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $footer .= $provider::displayPageFoot($controller);

        if (method_exists($controller, 'after_displayPageFoot_callback')) {
            $footer .= $controller->{'after_displayPageFoot_callback'}();
        }

        self::$logger->debug('<<displayPageFoot ['.$footer.']');

        return $footer;
    }

    /**
     * Method for rendering the pagination links.
     *
     * @param \Alpha\Controller\Controller $controller
     *
     * @return string
     *
     * @since 3.0
     */
    public static function displayPageLinks($controller)
    {
        $config = ConfigProvider::getInstance();

        $html = '';
        $recordCount = $controller->getRecordCount();
        $start = $controller->getStart();
        $limit = $controller->getLimit();

        // the index of the last record displayed on this page
        $last = $start+$config->get('app.list.page.amount');

        // ensure that the last index never overruns the total record count
        if ($last > $recordCount) {
            $last = $recordCount ;
        }

        // render a message for an empty list
        if ($recordCount  > 0) {
            $html .= '<ul class="pagination">';
        } else {
            $html .= '<p align="center">The list is empty.&nbsp;&nbsp;</p>';

            return $html;
        }

        // render "Previous" link
        if ($start  > 0) {
            // handle secure URLs
            if ($controller->getRequest()->getParam('token', null) != null) {
                $url = FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.$controller->getRequest()->getParam('ActiveRecordType').'&start='.($start-$controller->getLimit()).'&limit='.$limit);
            } else {
                $url = '/records/'.urlencode($controller->getRequest()->getParam('ActiveRecordType')).'/'.($start-$limit).'/'.$limit;
            }
            $html .= '<li><a href="'.$url.'">&lt;&lt;-Previous</a></li>';
        } elseif ($recordCount  > $limit) {
            $html .= '<li class="disabled"><a href="#">&lt;&lt;-Previous</a></li>';
        }

        // render the page index links
        if ($recordCount  > $limit) {
            $page = 1;

            for ($i = 0; $i < $recordCount ; $i += $limit) {
                if ($i != $start) {
                    // handle secure URLs
                    if ($controller->getRequest()->getParam('token', null) != null) {
                        $url = FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.$controller->getRequest()->getParam('ActiveRecordType').'&start='.$i.'&limit='.$limit);
                    } else {
                        $url = '/records/'.urlencode($controller->getRequest()->getParam('ActiveRecordType')).'/'.$i.'/'.$limit;
                    }
                    $html .= '<li><a href="'.$url.'">'.$page.'</a></li>';
                } elseif ($recordCount  > $limit) { // render an anchor for the current page
                    $html .= '<li class="active"><a href="#">'.$page.'</a></li>';
                }

                ++$page;
            }
        }

        // render "Next" link
        if ($recordCount  > $last) {
            // handle secure URLs
            if ($controller->getRequest()->getParam('token', null) != null) {
                $url = FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.$controller->getRequest()->getParam('ActiveRecordType').'&start='.($start+$limit).'&limit='.$limit);
            } else {
                $url = '/records/'.urlencode($controller->getRequest()->getParam('ActiveRecordType')).'/'.($start+$limit.'/'.$limit);
            }
            $html .= '<li><a href="'.$url.'">Next-&gt;&gt;</a></li>';
        } elseif ($recordCount  > $limit) {
            $html .= '<li class="disabled"><a href="#">Next-&gt;&gt;</a></li>';
        }

        $html .= '</ul>';

        return $html;
    }

    /**
     * Renders the content for an update (e.g. successful save) message.
     *
     * @param string $message
     *
     * @return string
     *
     * @since 1.0
     */
    public static function displayUpdateMessage($message)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>displayUpdateMessage(message=['.$message.'])');

        $config = ConfigProvider::getInstance();

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $message = $provider::displayUpdateMessage($message);

        self::$logger->debug('<<displayUpdateMessage ['.$message.']');

        return $message;
    }

    /**
     * Renders the content for an error (e.g. save failed) message.
     *
     * @param string $message
     *
     * @return string
     *
     * @since 1.0
     */
    public static function displayErrorMessage($message)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>displayErrorMessage(message=['.$message.'])');

        $config = ConfigProvider::getInstance();

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $message = $provider::displayErrorMessage($message);

        self::$logger->debug('<<displayErrorMessage ['.$message.']');

        return $message;
    }

    /**
     * Renders an error page with the supplied error code (typlically a HTTP code) and a message.
     *
     * @param string $code
     * @param string $message
     *
     * @return string
     *
     * @since 1.0
     */
    public static function renderErrorPage($code, $message)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>renderErrorPage(code=['.$code.'],message=['.$message.'])');

        $config = ConfigProvider::getInstance();

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $message = $provider::renderErrorPage($code, $message);

        self::$logger->debug('<<renderErrorPage ['.$message.']');

        return $message;
    }

    /**
     * Method to render a hidden HTML form for posting the ID of an object to be deleted.
     *
     * @param string $URI The URI that the form will point to
     *
     * @return string
     *
     * @since 1.0
     */
    public static function renderDeleteForm($URI)
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>renderDeleteForm()');

        $config = ConfigProvider::getInstance();

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $html = $provider::renderDeleteForm($URI);

        self::$logger->debug('<<renderDeleteForm ['.$html.']');

        return $html;
    }

    /**
     * Method to render a HTML form with two hidden, hashed (MD5) form fields to be used as
     * a check to ensure that a post to the controller is being sent from the same server
     * as hosting it.
     *
     * @return string
     *
     * @since 1.0
     */
    public static function renderSecurityFields()
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>renderSecurityFields()');

        $config = ConfigProvider::getInstance();

        if (!self::$provider instanceof RendererProviderInterface) {
            self::setProvider($config->get('app.renderer.provider.name'));
        }

        $provider = self::$provider;
        $html = $provider::renderSecurityFields();

        self::$logger->debug('<<renderSecurityFields ['.$html.']');

        return $html;
    }

    /**
     * Method to render the default Integer HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderIntegerField($name, $label, $mode, $value = '')
    {
        self::$logger->debug('>>renderIntegerField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.']');

        $html = self::$provider->renderIntegerField($name, $label, $mode, $value);

        self::$logger->debug('<<renderIntegerField ['.$html.']');

        return $html;
    }

    /**
     * Method to render the default Double HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderDoubleField($name, $label, $mode, $value = '')
    {
        self::$logger->debug('>>renderDoubleField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'])');

        $html = self::$provider->renderDoubleField($name, $label, $mode, $value);

        self::$logger->debug('<<renderDoubleField ['.$html.']');

        return $html;
    }

    /**
     * Method to render the default Boolean HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderBooleanField($name, $label, $mode, $value = '')
    {
        self::$logger->debug('>>renderBooleanField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'])');

        $html = self::$provider->renderBooleanField($name, $label, $mode, $value);

        self::$logger->debug('<<renderBooleanField ['.$html.']');

        return $html;
    }

    /**
     * Method to render the default Enum HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param array  $options   The Enum options
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderEnumField($name, $label, $mode, $options, $value = '')
    {
        self::$logger->debug('>>renderEnumField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'])');

        $html = self::$provider->renderEnumField($name, $label, $mode, $options, $value);

        self::$logger->debug('<<renderEnumField ['.$html.']');

        return $html;
    }

    /**
     * Method to render the default DEnum HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param array  $options   The DEnum options
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderDEnumField($name, $label, $mode, $options, $value = '')
    {
        self::$logger->debug('>>renderDEnumField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'])');

        $html = self::$provider->renderDEnumField($name, $label, $mode, $options, $value);

        self::$logger->debug('<<renderDEnumField ['.$html.']');

        return $html;
    }

    /**
     * Method to render the default field HTML when type is not known.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderDefaultField($name, $label, $mode, $value = '')
    {
        self::$logger->debug('>>renderDefaultField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'])');

        $html = self::$provider->renderDefaultField($name, $label, $mode, $value);

        self::$logger->debug('<<renderDefaultField ['.$html.']');

        return $html;
    }

    /**
     * render the default Text HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param string $value     The field value (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderTextField($name, $label, $mode, $value = '')
    {
        self::$logger->debug('>>renderTextField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'])');

        $html = self::$provider->renderTextField($name, $label, $mode, $value);

        self::$logger->debug('<<renderTextField ['.$html.']');

        return $html;
    }

    /**
     * render the default Relation HTML.
     *
     * @param string $name      The field name
     * @param string $label     The label to apply to the field
     * @param string $mode      The field mode (create/edit/view)
     * @param string $value     The field value (optional)
     * @param bool   $expanded  Render the related fields in expanded format or not (optional)
     * @param bool   $buttons   Render buttons for expanding/contacting the related fields (optional)
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderRelationField($name, $label, $mode, $value = '', $expanded = false, $buttons = true)
    {
        self::$logger->debug('>>renderRelationField(name=['.$name.'], label=['.$label.'], mode=['.$mode.'], value=['.$value.'], expanded=['.$expanded.'], buttons=['.$buttons.'])');

        $html = self::$provider->renderRelationField($name, $label, $mode, $value, $expanded, $buttons);

        self::$logger->debug('<<renderRelationField ['.$html.']');

        return $html;
    }

    /**
     * Renders all fields for the current Record in edit/create/view mode.
     *
     * @param string $mode           (view|edit|create)
     * @param array  $filterFields   Optional list of field names to exclude from rendering
     * @param array  $readOnlyFields Optional list of fields to render in a readonly fashion when rendering in create or edit mode
     *
     * @return string
     *
     * @since 1.0
     */
    public function renderAllFields($mode, $filterFields = array(), $readOnlyFields = array())
    {
        self::$logger->debug('>>renderAllFields(mode=['.$mode.'], filterFields=['.var_export($filterFields, true).'], readOnlyFields=['.var_export($readOnlyFields, true).'])');

        $html = self::$provider->renderAllFields($mode, $filterFields, $readOnlyFields);

        self::$logger->debug('<<renderAllFields ['.$html.']');

        return $html;
    }

    /**
     * Loads a template for the Record specified if one exists.  Lower level custom templates
     * take precedence.
     *
     * @param \Alpha\Model\ActiveRecord $record
     * @param string                   $mode
     * @param array                    $fields
     *
     * @return string
     *
     * @since 1.0
     *
     * @throws \Alpha\Exception\IllegalArguementException
     */
    public static function loadTemplate($record, $mode, $fields = array())
    {
        self::$logger->debug('>>loadTemplate(Record=['.var_export($record, true).'], mode=['.$mode.'], fields=['.var_export($fields, true).'])');

        $config = ConfigProvider::getInstance();

        // for each Record property, create a local variable holding its value
        $reflection = new ReflectionClass(get_class($record));
        $properties = $reflection->getProperties();

        foreach ($properties as $propObj) {
            $propName = $propObj->name;

            if ($propName != 'logger' && !$propObj->isPrivate()) {
                $prop = $record->getPropObject($propName);
                if ($prop instanceof DEnum) {
                    ${$propName} = $prop->getDisplayValue();
                } else {
                    ${$propName} = $record->get($propName);
                }
            }
        }

        // loop over the $fields array and create a local variable for each key value
        foreach (array_keys($fields) as $fieldName) {
            ${$fieldName} = $fields[$fieldName];
        }

        $filename = $mode.'.phtml';
        $class = new ReflectionClass($record);
        $className = $class->getShortname();

        $customPath = $config->get('app.root').'src/View/Html/Templates/'.$className.'/'.$filename;
        $defaultPath1 = $config->get('app.root').'vendor/alphadevx/alpha/Alpha/View/Renderer/Html/Templates/'.$className.'/'.$filename;
        $defaultPath2 = $config->get('app.root').'vendor/alphadevx/alpha/Alpha/View/Renderer/Html/Templates/'.$filename;
        $defaultPath3 = $config->get('app.root').'Alpha/View/Renderer/Html/Templates/'.$className.'/'.$filename;
        $defaultPath4 = $config->get('app.root').'Alpha/View/Renderer/Html/Templates/'.$filename;

        // Check to see if a custom template exists for this record, and if it does load that
        if (file_exists($customPath)) {
            self::$logger->debug('Loading template ['.$customPath.']');
            ob_start();
            require $customPath;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplate');
            return $html;
        } elseif (file_exists($defaultPath1)) {
            self::$logger->debug('Loading template ['.$defaultPath1.']');
            ob_start();
            require $defaultPath1;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplate');
            return $html;
        } elseif (file_exists($defaultPath2)) {
            self::$logger->debug('Loading template ['.$defaultPath2.']');
            ob_start();
            require $defaultPath2;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplate');
            return $html;
        } elseif (file_exists($defaultPath3)) {
            self::$logger->debug('Loading template ['.$defaultPath3.']');
            ob_start();
            require $defaultPath3;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplate');
            return $html;
        } elseif (file_exists($defaultPath4)) {
            self::$logger->debug('Loading template ['.$defaultPath4.']');
            ob_start();
            require $defaultPath4;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplate');
            return $html;
        } else {
            self::$logger->debug('<<loadTemplate');
            throw new IllegalArguementException('No ['.$mode.'] HTML template found for class ['.$className.']');
        }
    }

    /**
     * Loads a template fragment from the Renderer/[type]/Fragments/[filename.ext] location.
     *
     * @param string $type     Currently only html supported, later json and xml.
     * @param string $fileName The name of the fragment file
     * @param array  $fields   A hash array of field values to pass to the template fragment.
     *
     * @return string
     *
     * @since 1.2
     *
     * @throws \Alpha\Exception\IllegalArguementException
     */
    public static function loadTemplateFragment($type, $fileName, $fields = array())
    {
        if (self::$logger == null) {
            self::$logger = new Logger('View');
        }
        self::$logger->debug('>>loadTemplateFragment(type=['.$type.'], fileName=['.$fileName.'], fields=['.var_export($fields, true).'])');

        $config = ConfigProvider::getInstance();

        // loop over the $fields array and create a local variable for each key value
        foreach (array_keys($fields) as $fieldName) {
            ${$fieldName} = $fields[$fieldName];
        }

        $customPath = $config->get('app.root').'src/View/'.ucfirst($type).'/Fragments/'.$fileName;
        $defaultPath1 = $config->get('app.root').'vendor/alphadevx/alpha/Alpha/View/Renderer/'.ucfirst($type).'/Fragments/'.$fileName;
        $defaultPath2 = $config->get('app.root').'Alpha/View/Renderer/'.ucfirst($type).'/Fragments/'.$fileName;

        // Check to see if a custom template exists for this record, and if it does load that
        if (file_exists($customPath)) {
            self::$logger->debug('Loading template ['.$customPath.']');
            ob_start();
            require $customPath;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplateFragment');
            return $html;
        } elseif (file_exists($defaultPath1)) {
            self::$logger->debug('Loading template ['.$defaultPath1.']');
            ob_start();
            require $defaultPath1;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplateFragment');
            return $html;
        } elseif (file_exists($defaultPath2)) {
            self::$logger->debug('Loading template ['.$defaultPath2.']');
            ob_start();
            require $defaultPath2;
            $html = ob_get_clean();

            self::$logger->debug('<<loadTemplateFragment');
            return $html;
        } else {
            self::$logger->debug('<<loadTemplateFragment');
            throw new IllegalArguementException('Template fragment not found in ['.$customPath.'] or ['.$defaultPath1.'] or ['.$defaultPath2.']!');
        }
    }

    /**
     * Enables you to set an explicit type of RendererProviderInterface implementation to use for rendering the records
     * attached to this view.
     *
     * @param string $ProviderClassName The name of the RendererProviderInterface implementation to use in this view object
     * @param string $acceptHeader      Optional pass the HTTP Accept header to select the correct renderer provider.
     *
     * @since 1.2
     *
     * @throws \Alpha\Exception\IllegalArguementException
     */
    public static function setProvider($ProviderClassName, $acceptHeader = null)
    {
        if ($ProviderClassName == 'auto') {
            $ProviderClassName = 'Alpha\View\Renderer\Html\RendererProviderHTML';

            if ($acceptHeader == 'application/json') {
                $ProviderClassName = 'Alpha\View\Renderer\Json\RendererProviderJSON';
            }

            self::$provider = ServiceFactory::getInstance($ProviderClassName, 'Alpha\View\Renderer\RendererProviderInterface');
        } else {
            if (class_exists($ProviderClassName)) {
                $provider = new $ProviderClassName();

                if ($provider instanceof RendererProviderInterface) {
                    self::$provider = ServiceFactory::getInstance($ProviderClassName, 'Alpha\View\Renderer\RendererProviderInterface');
                } else {
                    throw new IllegalArguementException('The provider class ['.$ProviderClassName.'] does not implement the RendererProviderInterface interface!');
                }
            } else {
                throw new IllegalArguementException('The provider class ['.$ProviderClassName.'] does not exist!');
            }
        }
    }

    /**
     * Get the current view renderer provider.
     *
     * @return \Alpha\View\Renderer\RendererProviderInterface
     *
     * @since 2.0
     */
    public static function getProvider()
    {
        if (self::$provider instanceof RendererProviderInterface) {
            return self::$provider;
        } else {
            $config = ConfigProvider::getInstance();

            self::$provider = ServiceFactory::getInstance($config->get('app.renderer.provider.name'), 'Alpha\View\Renderer\RendererProviderInterface');

            return self::$provider;
        }
    }
}
Alpha Framework 3.0.0 API Documentation API documentation generated by ApiGen