Factory pattern in Zend data grid

In the abstract class Bvb_Grid, there is static method named factory() which acts as a factory to create specific grid deploy class.

    public static function factory($defaultClass = 'Table', $options = array(), $id = '', $classCallbacks = array(), $requestParams = false)
    {
        self::initDeployClass();//initialize Zend_Loader_PluginLoader and add the prefix path
        try {
            $defaultClass = self::loadDeployClass($defaultClass);// load the deploy class 
        } catch (Zend_Loader_PluginLoader_Exception $e) {            
            if (!class_exists($defaultClass)) {
                throw $e;
            }
        }
        if (false === $requestParams) {
            // use request parameters
            $requestParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        }
        if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        }
        // use this as request parameters
        if (!isset($options['grid'])) {
            $options['grid'] = array('requestParams' => $requestParams);
        } else {
            $options['grid']['requestParams'] = $requestParams;
        }

        // handle _exportTo parameter compatible with calling with grid id and without
        if (isset($requestParams['_exportTo' . $id])) {
            $exportTo = $requestParams['_exportTo' . $id];
        } elseif (isset($requestParams['_exportTo'])) {
            $exportTo = $requestParams['_exportTo'];
        } else {
            $exportTo = false;
        }
        if (false === $exportTo) {
            // return instance of the main Bvb object, because this is not and export request
            $grid = new $defaultClass($options);
            $lClass = $defaultClass;
        } else {
            $lClass = strtolower($exportTo);
            // support translating of parameters specifig for the export initiator class
            if (isset($requestParams['_exportFrom'])) {
                // TODO support translating of parameters specifig for the export initiator class
                $requestParams = $requestParams;
            }
            // now we need to find and load the right Bvb deploy class
            // TODO support user defined classes
            $className = self::loadDeployClass($exportTo);
            if (Zend_Loader_Autoloader::autoload($className)) {
                $grid = new $className($options);
            } else {
                $grid = new $defaultClass($options);
                $lClass = $defaultClass;
            }
        }
        // add the powerfull configuration callback function
        if (isset($classCallbacks[$lClass])) {
            $grid->_configCallbacks = $classCallbacks[$lClass]; // register callbacks
        }
        if (is_string($id)) {
            $grid->setGridId($id);
        }
        return $grid;
    }
If we want to use one specific grid deploy class, we can also use it directly.

<?php

class Bvb_Grid_Deploy_Table extends Bvb_Grid implements Bvb_Grid_Deploy_DeployInterface {

public function __construct (array $options = array())
    {
        $this->_setRemoveHiddenFields(true);

        parent::__construct($options);

        if (isset($this->_options['grid']['id'])) {
            $this->setGridId($this->_options['grid']['id']);
        }

        $this->_gridSession = new Zend_Session_Namespace('Bvb_Grid_' . $this->getGridId());
        $this->addTemplateDir('Bvb/Grid/Template', 'Bvb_Grid_Template', 'table');

        if ($this->getParam('add') || $this->getParam('edit')) {

            if ($this->getParam('add')) {
                $this->_willShow[] = 'form';
                $this->_willShow[] = 'formAdd';
            }
            if ($this->getParam('edit')) {
                $this->_willShow[] = 'form';
                $this->_willShow[] = 'formEdit';
            }
        } else {
            if ($this->getParam('detail') ||  $this->getParam('delete') ){
                $this->_willShow[] = 'detail';
            } else {
                $this->_willShow[] = 'listing';
            }
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值