Source of file AbstractController.php

Size: 10,181 Bytes - Last Modified: 2016-05-18T03:08:27+02:00

buildproject/core/module_system/system/AbstractController.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
<?php
/*"******************************************************************************************************
*   (c) 2013-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$	                                            *
********************************************************************************************************/

namespace Kajona\System\System;


/**
 * A common base class for AdminController and PortalController.
 * Use one of both to create admin-/portal-views.
 * Do NOT extend this class directly.
 *
 * @package module_system
 * @author sidler@mulchprod.de
 * @since 4.4
 */
abstract class AbstractController
{

    const STR_MODULE_ANNOTATION = "@module";
    const STR_MODULEID_ANNOTATION = "@moduleId";

    /**
     * Object containing config-data
     *
     * @inject system_config
     * @var Config
     */
    protected $objConfig;

    /**
     * Object containing the session-management
     *
     * @inject system_session
     * @var Session
     */
    protected $objSession;

    /**
     * Object to handle templates
     *
     * @inject system_template
     * @var Template
     */
    protected $objTemplate;

    /**
     * Object managing the lang-files
     *
     * @inject system_lang
     * @var Lang
     */
    protected $objLang;

    /**
     * Instance of the current modules' definition
     *
     * @var SystemModule
     */
    private $objModule;

    /**
     * The current module to load lang-files from
     * String containing the current module to be used to load texts
     *
     * @var string
     */
    private $strLangBase = "";

    /**
     * Current action-name, used for the controller
     * current action to perform (GET/POST)
     *
     * @var string
     */
    private $strAction = "";

    /**
     * The current systemid as passed by the constructor / params
     *
     * @var string
     */
    private $strSystemid = "";

    /**
     * Array containing information about the current module
     *
     * @var array
     * @deprecated direct access is no longer allowed
     */
    protected $arrModule = array();

    /**
     * String containing the output generated by an internal action
     *
     * @var string
     */
    protected $strOutput = "";

    /**
     * @param string $strSystemid
     */
    public function __construct($strSystemid = "")
    {
        // compatibility workaround. If the dependencies were not injected yet the class was created without the object
        // builder in this case we manually inject them
        if ($this->objConfig === null) {
            $objBuilder = new ObjectBuilder(Carrier::getInstance()->getContainer());
            $objBuilder->resolveDependencies($this);
        }

        //Setting SystemID
        if ($strSystemid == "") {
            $this->setSystemid(Carrier::getInstance()->getParam("systemid"));
        }
        else {
            $this->setSystemid($strSystemid);
        }


        //And keep the action
        $this->setAction($this->getParam("action"));
        //in most cases, the list is the default action if no other action was passed
        if ($this->getAction() == "") {
            $this->setAction("list");
        }

        //try to load the current module-name and the moduleId by reflection
        $objReflection = new Reflection($this);
        if (!isset($this->arrModule["modul"])) {
            $arrAnnotationValues = $objReflection->getAnnotationValuesFromClass(self::STR_MODULE_ANNOTATION);
            if (count($arrAnnotationValues) > 0) {
                $this->setArrModuleEntry("modul", trim($arrAnnotationValues[0]));
            }
            $this->setArrModuleEntry("module", trim($arrAnnotationValues[0]));
        }

        if (!isset($this->arrModule["moduleId"])) {
            $arrAnnotationValues = $objReflection->getAnnotationValuesFromClass(self::STR_MODULEID_ANNOTATION);
            if (count($arrAnnotationValues) > 0) {
                $this->setArrModuleEntry("moduleId", constant(trim($arrAnnotationValues[0])));
            }
        }

        $this->strLangBase = $this->getArrModule("modul");
    }


    // --- Common Methods -----------------------------------------------------------------------------------


    /**
     * Writes a value to the params-array
     *
     * @param string $strKey Key
     * @param mixed $mixedValue Value
     *
     * @return void
     */
    public function setParam($strKey, $mixedValue)
    {
        Carrier::getInstance()->setParam($strKey, $mixedValue);
    }

    /**
     * Returns a value from the params-Array
     *
     * @param string $strKey
     *
     * @return string|string[] else ""
     */
    public function getParam($strKey)
    {
        return Carrier::getInstance()->getParam($strKey);
    }

    /**
     * Returns the complete Params-Array
     *
     * @return mixed
     * @final
     */
    public final function getAllParams()
    {
        return Carrier::getAllParams();
    }

    /**
     * returns the action used for the current request
     *
     * @return string
     * @final
     */
    public final function getAction()
    {
        return (string)$this->strAction;
    }

    /**
     * Overwrites the current action
     *
     * @param string $strAction
     *
     * @return void
     */
    public final function setAction($strAction)
    {
        $this->strAction = htmlspecialchars(trim($strAction), ENT_QUOTES, "UTF-8", false);
    }



    // --- SystemID & System-Table Methods ------------------------------------------------------------------

    /**
     * Sets the current SystemID
     *
     * @param string $strID
     *
     * @return bool
     * @final
     */
    public final function setSystemid($strID)
    {
        if (validateSystemid($strID)) {
            $this->strSystemid = $strID;
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Returns the current SystemID
     *
     * @return string
     * @final
     */
    public final function getSystemid()
    {
        return $this->strSystemid;
    }

    /**
     * Resets the internal system id
     *
     * @final
     */
    public final function unsetSystemid()
    {
        $this->strSystemid = "";
    }


    /**
     * Returns the current Text-Object Instance
     *
     * @return Lang
     */
    protected function getObjLang()
    {
        return $this->objLang;
    }


    /**
     * Returns the current instance of SystemModule, based on the current subclass.
     * Lazy-loading, so loaded on first access.
     *
     * @return SystemModule|null
     */
    protected function getObjModule()
    {
        if ($this->objModule == null) {
            $this->objModule = SystemModule::getModuleByName($this->getArrModule("modul"));
        }

        return $this->objModule;
    }


    /**
     * Generates a sorted array of systemids, reaching from the passed systemid up
     * until the assigned module-id
     *
     * @param string $strSystemid
     * @param string $strStopSystemid
     *
     * @return mixed
     * @deprecated should be handled by the model-classes instead
     */
    public function getPathArray($strSystemid = "", $strStopSystemid = "")
    {
        if ($strSystemid == "") {
            $strSystemid = $this->getSystemid();
        }
        if ($strStopSystemid == "") {
            $strStopSystemid = $this->getObjModule()->getSystemid();
        }

        $objSystemCommon = new SystemCommon();
        return $objSystemCommon->getPathArray($strSystemid, $strStopSystemid);
    }

    /**
     * Returns a value from the $arrModule array.
     * If the requested key not exists, returns ""
     *
     * @param string $strKey
     *
     * @return string
     */
    public function getArrModule($strKey)
    {
        if (isset($this->arrModule[$strKey])) {
            return $this->arrModule[$strKey];
        }
        else {
            return "";
        }
    }

    /**
     * Writes a key-value-pair to the arrModule
     *
     * @param string $strKey
     * @param mixed $strValue
     *
     * @return void
     */
    public function setArrModuleEntry($strKey, $strValue)
    {
        $this->arrModule[$strKey] = $strValue;
    }


    // --- TextMethods --------------------------------------------------------------------------------------

    /**
     * Used to load a property.
     * If you want to provide a list of parameters but no module (automatic loading), pass
     * the parameters array as the second argument (an array). In this case the module is resolved
     * internally.
     *
     * @param string $strName
     * @param string|array $strModule Either the module name (if required) or an array of parameters
     * @param array $arrParameters
     *
     * @return string
     */
    public function getLang($strName, $strModule = "", $arrParameters = array())
    {
        if (is_array($strModule)) {
            $arrParameters = $strModule;
        }

        if ($strModule == "" || is_array($strModule)) {
            $strModule = $this->strLangBase;
        }

        //Now we have to ask the Text-Object to return the text
        return $this->getObjLang()->getLang($strName, $strModule, $arrParameters);
    }

    /**
     * Sets the textbase, so the module used to load texts
     *
     * @param string $strLangbase
     *
     * @return void
     */
    final protected function setStrLangBase($strLangbase)
    {
        $this->strLangBase = $strLangbase;
    }



    // --- PageCache Features -------------------------------------------------------------------------------

    /**
     * Deletes the complete Pages-Cache
     */
    public function flushCompletePagesCache()
    {
        /** @var CacheManager $objCache */
        $objCache = Carrier::getInstance()->getContainer()->offsetGet(ServiceProvider::STR_CACHE_MANAGER);
        $objCache->flushCache();
    }


}