Просмотр исходного кода

#1559 main.css from all modules are included on all pages

Alex Trofimov 8 лет назад
Родитель
Сommit
bd9c42f035

+ 3 - 2
inc/classes/BxDolModule.php

@@ -68,8 +68,9 @@ class BxDolModule extends BxDol
 
         $sClassName = $sClassPrefix . 'Template';
         require_once($sClassPath . $sClassName . '.php');
-        $this->_oTemplate = new $sClassName($this->_oConfig, $this->_oDb);
-        $this->_oTemplate->init();
+        $oTemplate = new $sClassName($this->_oConfig, $this->_oDb); 
+        $oTemplate->init();
+        $this->_oTemplate = new BxDolModuleProxy('module_template', $oTemplate);
     }
 
     /**

+ 71 - 0
inc/classes/BxDolModuleProxy.php

@@ -0,0 +1,71 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+/**
+ * Proxy class for module classes, now only module template class is proxied.
+ * It's possible to override any module template method call by making a response 
+ * for 'module_template_method_call' alert.
+ */ 
+class BxDolModuleProxy
+{
+    private $_oProxifiedObject;
+    private $_sObjectType;
+
+    function __construct($sObjectType, $oProxifiedObject)
+    {
+        $this->_sObjectType = $sObjectType;
+        $this->_oProxifiedObject = $oProxifiedObject;
+    }
+    
+    public function __call($sMethodName, $aArguments)
+    {
+        if (is_callable(array($this->_oProxifiedObject, $sMethodName))) {
+
+            // check if we need to include css&js files
+            if ('module_template' == $this->_sObjectType && !empty($this->_oProxifiedObject->aMethodsToCallAddJsCss)) {
+                foreach ($this->_oProxifiedObject->aMethodsToCallAddJsCss as $s) {
+                    if (0 === strpos($s, $sMethodName)) {
+                        $this->_oProxifiedObject->addCssJs();
+                        break;
+                    }
+                }
+            }
+
+            // make it possible to override the call or arguments
+            $res = null;
+            bx_alert($this->_sObjectType . '_method_call', $sMethodName, 0, 0, array('args' => &$aArguments, 'override_result' => &$res));
+            if (null !== $res)
+                return $res;
+
+            // call original method
+            return call_user_func_array(array($this->_oProxifiedObject, $sMethodName), $aArguments);
+        }
+        else {
+            $sClass = get_class($this->_oProxifiedObject);
+            trigger_error('Method ' . $sMethodName . ' was not found for the class ' . $sClass, E_USER_ERROR);
+        }
+    }
+
+    /**
+     * Dirty fix for pass by reference to BxTimelineTemplate::getData
+     */
+    public function getData(&$aEvent, $aBrowseParams = array())
+    {
+        return $this->_oProxifiedObject->getData($aEvent, $aBrowseParams);
+    }
+    /**
+     * Dirty fix for pass by reference to BxNtfsTemplate::getPost
+     */
+    public function getPost(&$aEvent, $aBrowseParams = array())
+    {
+        return $this->_oProxifiedObject->getPost($aEvent, $aBrowseParams);
+    }
+}
+
+/** @} */

+ 4 - 0
inc/classes/BxDolModuleTemplate.php

@@ -151,6 +151,10 @@ class BxDolModuleTemplate extends BxDolTemplate
             return $sRet;
         }
     }
+
+    public function addCssJs()
+    {
+    }
 }
 
 /** @} */

+ 8 - 2
modules/base/general/classes/BxBaseModGeneralTemplate.php

@@ -17,10 +17,11 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
     protected $MODULE;
     protected $_oModule;
 
+    public $aMethodsToCallAddJsCss = array('entry', 'unit');
+
     function __construct(&$oConfig, &$oDb)
     {
-        parent::__construct($oConfig, $oDb);        
-        $this->addCss ('main.css');
+        parent::__construct($oConfig, $oDb);
     }
 
     public function getModule()
@@ -274,6 +275,11 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
 
         return $aAttachmnts;
     }
+
+    public function addCssJs()
+    {
+        $this->addCss('main.css');
+    }
 }
 
 /** @} */