Source of file SystemtaskBase.php

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

buildproject/core/module_system/admin/systemtasks/SystemtaskBase.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
<?php
/*"******************************************************************************************************
*   (c) 2004-2006 by MulchProductions, www.mulchprod.de                                                 *
*   (c) 2007-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*   $Id$                               *
********************************************************************************************************/

namespace Kajona\System\Admin\Systemtasks;

use Kajona\System\Admin\AdminFormgenerator;
use Kajona\System\Admin\Formentries\FormentryButton;
use Kajona\System\Admin\Formentries\FormentryHidden;
use Kajona\System\Admin\ToolkitAdmin;
use Kajona\System\System\Carrier;
use Kajona\System\System\Classloader;
use Kajona\System\System\Database;
use Kajona\System\System\Lang;
use Kajona\System\System\Link;
use Kajona\System\System\Resourceloader;
use Kajona\System\System\SystemCommon;


/**
 * Base class for all systemtasks. Provides a few methods to be used by the concrete tasks.
 *
 * @package module_system
 * @autor sidler@mulchprod.de
 */
abstract class SystemtaskBase
{

    private $strTextbase = "system";

    /**
     * Instance of Database
     *
     * @var Database
     */
    private $objDB;

    /**
     * Instance of Lang
     *
     * @var Lang
     */
    private $objLang;

    /**
     * Instance of ToolkitAdmin
     *
     * @var ToolkitAdmin
     */
    protected $objToolkit;

    /**
     * URL used to trigger a reload, e.g. during long tasks
     *
     * @var string
     */
    private $strReloadParam = "";

    /**
     * Infos regarding the current process
     *
     * @var string
     */
    private $strProgressInformation = "";

    /**
     * @var SystemCommon
     */
    private $objSystemCommon;

    /**
     * Indicates, wether the form to set up the task is a multipart-form or not (e.g.
     * for fileuploads)
     *
     * @var bool
     */
    private $bitMultipartform = false;

    /**
     * Default constructor
     */
    public function __construct()
    {

        //load the external objects
        $this->objDB = Carrier::getInstance()->getObjDB();
        $this->objLang = Carrier::getInstance()->getObjLang();
        $this->objToolkit = Carrier::getInstance()->getObjToolkit("admin");
        $this->objSystemCommon = new SystemCommon();

    }

    /**
     * A helper to fetch instances of all systemtasks found in the current installation
     *
     * @return SystemtaskBase[]|AdminSystemtaskInterface[]
     */
    public static function getAllSystemtasks()
    {
        $arrFiles = Resourceloader::getInstance()->getFolderContent("/admin/systemtasks", array(".php"), false, null, function (&$strOneFile, $strPath) {

            $objInstance = Classloader::getInstance()->getInstanceFromFilename($strPath, "Kajona\\System\\Admin\\Systemtasks\\SystemtaskBase");

            if ($objInstance instanceof AdminSystemtaskInterface) {
                $strOneFile = $objInstance;
            }
            else {
                $strOneFile = null;
            }

        });

        return array_filter($arrFiles, function ($objTask) {
            return $objTask != null;
        });
    }

    /**
     * Delegate requests for strings to the text-subsystem
     *
     * @param string $strLangKey
     * @param array $arrParameters
     *
     * @return string
     */
    protected function getLang($strLangKey, $arrParameters = array())
    {
        return $this->objLang->getLang($strLangKey, $this->strTextbase, $arrParameters);
    }

    /**
     * Method invoking the hook-methods to generate a form.
     *
     * @param string $strTargetModule
     * @param string $strTargetAction
     * @param string|AdminFormgenerator $objAdminForm
     *
     * @return string
     */
    public final function generateAdminForm($strTargetModule = "system", $strTargetAction = "systemTasks", $objAdminForm = null)
    {
        $strReturn = "";
        $objAdminForm = $objAdminForm == null ? $this->getAdminForm() : $objAdminForm;

        if ($objAdminForm instanceof AdminFormgenerator) {
            $objAdminForm->addField(new FormentryHidden("", "execute"))->setStrValue("true");
            $objAdminForm->addField(new FormentryButton("", "systemtask_run"))->setStrLabel($this->objLang->getLang("systemtask_run", "system"))->setStrValue("submit");

            if ($this->bitMultipartform) {
                $objAdminForm->setStrFormEncoding(AdminFormgenerator::FORM_ENCTYPE_MULTIPART);
            }

            $strLink = Link::getLinkAdminHref($strTargetModule, $strTargetAction, "task=".$this->getStrInternalTaskName());
            $strReturn = $objAdminForm->renderForm($strLink, 0);
        }
        elseif ($objAdminForm != "") {
            if ($this->bitMultipartform) {
                $strReturn .= $this->objToolkit->formHeader(Link::getLinkAdminHref($strTargetModule, $strTargetAction, "task=".$this->getStrInternalTaskName()), "taskParamForm", AdminFormgenerator::FORM_ENCTYPE_MULTIPART);
            }
            else {
                $strReturn .= $this->objToolkit->formHeader(Link::getLinkAdminHref($strTargetModule, $strTargetAction, "task=".$this->getStrInternalTaskName()), "taskParamForm");
            }
            $strReturn .= $objAdminForm;
            $strReturn .= $this->objToolkit->formInputHidden("execute", "true");
            $strReturn .= $this->objToolkit->formInputSubmit($this->objLang->getLang("systemtask_run", "system"));
            $strReturn .= $this->objToolkit->formClose();

        }

        return $strReturn;
    }

    /**
     * Sets the current textbase, so a module.
     * If your textfiles are coming along with a module different than module system, pass the name here
     * to enable a proper text-loading.
     *
     * @param string $strModulename
     *
     * @return void
     */
    protected function setStrTextBase($strModulename)
    {
        $this->strTextbase = $strModulename;
    }

    /**
     * Empty implementation, override in subclass!
     *
     * @return AdminFormgenerator
     */
    public function getAdminForm()
    {
    }

    /**
     * Empty implementation, override in subclass!
     *
     * @return string[]
     */
    public function getSubmitParams()
    {
        return "";
    }

    /**
     * Empty implementation, oveerride in subclass!
     *
     * @return string
     */
    public function getStrInternalTaskName()
    {
    }

    /**
     * @param string $strReloadParam
     */
    public function setStrReloadParam($strReloadParam)
    {
        $this->strReloadParam = $strReloadParam;
    }

    /**
     * @return string
     */
    public function getStrReloadParam()
    {
        return $this->strReloadParam;
    }

    /**
     * @return string
     */
    public function getStrReloadUrl()
    {
        if ($this->strReloadParam != "") {
            return getLinkAdminHref("system", "systemTasks", "&task=".$this->getStrInternalTaskName().$this->strReloadParam);
        }
        else {
            return "";
        }
    }

    /**
     * @param string $strProgressInformation
     *
     * @return void
     */
    public function setStrProgressInformation($strProgressInformation)
    {
        $this->strProgressInformation = $strProgressInformation;
    }

    /**
     * @return string
     */
    public function getStrProgressInformation()
    {
        return $this->strProgressInformation;
    }

    /**
     * Delegate to system-kernel, used to read from params.
     * Provides acces to the GET and POST params
     *
     * @param string $strKey
     *
     * @return mixed
     */
    public function getParam($strKey)
    {
        return $this->objSystemCommon->getParam($strKey);
    }

    /**
     * Delegate to system-kernel, used to write to params
     *
     * @param string $strKey
     * @param mixed $strValue
     *
     * @return void
     */
    public function setParam($strKey, $strValue)
    {
        $this->objSystemCommon->setParam($strKey, $strValue);
    }

    /**
     * Indicates, wether the form to set up the task is a multipart-form or not (e.g.
     * for fileuploads)
     *
     * @param bool $bitMultipartform
     */
    public function setBitMultipartform($bitMultipartform)
    {
        $this->bitMultipartform = $bitMultipartform;
    }

}