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


/**
 * Class holding common methods for extended and simplified admin-guis.
 *
 * @package module_system
 * @since 4.0
 */
abstract class class_admin_simple extends class_admin_controller {

    private  $strPeAddon = "";

    /**
     * @param string $strSystemid
     */
    public function __construct($strSystemid = "") {
        parent::__construct($strSystemid);

        if($this->getParam("pe") == "1")
            $this->strPeAddon = "&pe=1";

        if($this->getParam("unlockid") != "") {
            $objLockmanager = new class_lockmanager($this->getParam("unlockid"));
            $objLockmanager->unlockRecord(true);
        }
    }

    /**
     * Overwritten in order to inject a toolbar per record. may be useful for certain actions
     * @param array &$arrContent
     * @return void
     */
    protected function onRenderOutput(&$arrContent) {
        $arrContent["actiontoolbar"] = $this->objToolkit->getContentActionToolbar($this->getContentActionToolbar());
    }

    /**
     * Default-implementation to render an action toolbar
     *
     * @return string
     */
    protected function getContentActionToolbar() {
        if(uniStrpos($this->getAction(), "list") !== false || uniStrpos($this->getAction(), "new") !== false || uniStrpos($this->getAction(), "save") !== false)
            return "";

        if(validateSystemid($this->getSystemid())) {
            $objRecord = class_objectfactory::getInstance()->getObject($this->getSystemid());

            if($objRecord instanceof interface_admin_listable)
                return $this->getActionIcons($objRecord);
        }

        return "";
    }


    /**
     * Renders the form to create a new entry
     * @abstract
     * @return string
     * @permissions edit
     */
    protected abstract function actionNew();

    /**
     * Renders the form to edit an existing entry
     * @abstract
     * @return string
     * @permissions edit
     */
    protected abstract function actionEdit();

    /**
     * Renders the general list of records
     * @abstract
     * @return string
     * @permissions view
     */
    protected abstract function actionList();


    /**
     * A general action to delete a record.
     * This method may be overwritten by subclasses.
     *
     * @permissions delete
     * @throws class_exception
     * @return void
     */
    protected function actionDelete() {
        $objRecord = class_objectfactory::getInstance()->getObject($this->getSystemid());
        if($objRecord != null && $objRecord->rightDelete()) {
            if(!$objRecord->deleteObject())
                throw new class_exception("error deleting object ".strip_tags($objRecord->getStrDisplayName()), class_exception::$level_ERROR);

            $strTargetUrl = urldecode($this->getParam("reloadUrl"));

            if($strTargetUrl == "" || uniStrpos($strTargetUrl, $this->getSystemid()) !== false) {

                $strTargetUrl = "admin=1&module=".$this->getArrModule("modul");

                $intI = 1;
                while($this->getHistory($intI) !== null) {
                    $strTargetUrl = $this->getHistory($intI++);

                    if(uniStrpos($strTargetUrl, $this->getSystemid()) === false) {
                        if(uniStrpos($strTargetUrl, "admin=1") === false)
                            $strTargetUrl = "admin=1&module=".$this->getArrModule("modul");

                        break;
                    }
                }

            }

            $this->adminReload(_indexpath_."?".$strTargetUrl.($this->getParam("pe") != "" ? "&peClose=1&blockAction=1" : ""));
        }
        else
            throw new class_exception("error loading object ".$this->getSystemid(), class_exception::$level_ERROR);
    }

    /**
     * A general action to copy a record.
     * This method may be overwritten by subclasses.
     *
     * @permissions edit
     * @throws class_exception
     * @return void
     */
    protected function actionCopyObject() {
        $objRecord = class_objectfactory::getInstance()->getObject($this->getSystemid());
        if($objRecord != null && $objRecord->rightEdit()) {
            if(!$objRecord->copyObject())
                throw new class_exception("error creating a copy of object ".strip_tags($objRecord->getStrDisplayName()), class_exception::$level_ERROR);

            $this->adminReload(class_link::getLinkAdminHref($this->getArrModule("modul"), $this->getActionNameForClass("list", $objRecord), "&systemid=".$objRecord->getPrevId()));
        }
        else
            throw new class_exception("error loading object ".$this->getSystemid(), class_exception::$level_ERROR);
    }


    /**
     * Returns the action name for a given class name.
     *
     * @param string $strAction
     * @param interface_model $objInstance
     * @return string specific action name
     */
    protected function getActionNameForClass($strAction, $objInstance) {
        return $strAction;
    }


    /**
     * Renders a list of items in a floatable "thumbnail" view, so a grid.
     * Please be aware, that the combination of paging and grids may result in unpredictable ordering.
     * As soon as the list is sortable, the page-size should be at least the same as the number of elements
     *
     * @param class_array_section_iterator $objArraySectionIterator
     * @param string $strListIdentifier an internal identifier to check the current parent-list
     * @param string $strPagerAddon
     * @param bool $bitSortable
     *
     * @throws class_exception
     * @return string
     */
    protected function renderFloatingGrid(class_array_section_iterator $objArraySectionIterator, $strListIdentifier = "", $strPagerAddon = "", $bitSortable = true) {
        $strReturn = "";

        $strListActions = "";
        if($this->renderLevelUpAction($strListIdentifier) != "") {
            $strListActions .= $this->objToolkit->listButton($this->renderLevelUpAction($strListIdentifier));
        }

        $strListActions .= $this->mergeNewEntryActions($this->getNewEntryAction($strListIdentifier));

        if($strListActions != "") {
            $strReturn .= $this->objToolkit->listHeader();
            $strReturn .= $this->objToolkit->genericAdminList("", "", "", $strListActions, 0);
            $strReturn .= $this->objToolkit->listFooter();
        }


        if(!$objArraySectionIterator->valid())
            $strReturn .= $this->objToolkit->getTextRow($this->getLang("commons_list_empty"));


        if($objArraySectionIterator->valid()) {

            $strReturn .= $this->objToolkit->gridHeader($bitSortable, $objArraySectionIterator->getIntElementsPerPage(), $objArraySectionIterator->getPageNumber());

            /** @var $objOneIterable class_model|interface_model|interface_admin_gridable */
            foreach($objArraySectionIterator as $objOneIterable) {

                if(!$objOneIterable->rightView() || !$objOneIterable instanceof interface_admin_gridable)
                    continue;

                $strActions = $this->getActionIcons($objOneIterable, $strListIdentifier);
                $strReturn .= $this->objToolkit->gridEntry($objOneIterable, $strActions, $this->renderGridEntryClickAction($objOneIterable, $strListIdentifier));
            }

            $strReturn .= $this->objToolkit->gridFooter();
        }

        $strReturn .= $this->objToolkit->getPageview($objArraySectionIterator, $this->getArrModule("modul"), $this->getAction(), "&systemid=".$this->getSystemid().$this->strPeAddon.$strPagerAddon);

        return $strReturn;
    }


    /**
     * Renders a list of items, target is the common admin-list.
     * Please be aware, that the combination of paging and sortable-lists may result in unpredictable ordering.
     * As soon as the list is sortable, the page-size should be at least the same as the number of elements
     *
     * @param class_array_section_iterator $objArraySectionIterator
     * @param bool $bitSortable
     * @param string $strListIdentifier an internal identifier to check the current parent-list
     * @param bool $bitAllowTreeDrop
     * @param string $strPagerAddon
     *
     * @throws class_exception
     * @return string
     */
    protected function renderList(class_array_section_iterator $objArraySectionIterator, $bitSortable = false, $strListIdentifier = "", $bitAllowTreeDrop = false, $strPagerAddon = "") {
        $strReturn = "";
        $intI = 0;

        $strListId = generateSystemid();

        if(!$objArraySectionIterator->valid())
            $strReturn .= $this->objToolkit->getTextRow($this->getLang("commons_list_empty"));

        if($bitSortable)
            $strReturn .= $this->objToolkit->dragableListHeader($strListId, false, $bitAllowTreeDrop, $objArraySectionIterator->getIntElementsPerPage(), $objArraySectionIterator->getPageNumber());
        else
            $strReturn .= $this->objToolkit->listHeader();

        if($this->renderLevelUpAction($strListIdentifier) != "") {
            $strReturn .= $this->objToolkit->genericAdminList("", "", "", $this->objToolkit->listButton($this->renderLevelUpAction($strListIdentifier)), $intI++);
        }

        $arrMassActions = $this->getBatchActionHandlers($strListIdentifier);

        /** @var $objOneIterable class_model|interface_model|interface_admin_listable */
        foreach($objArraySectionIterator as $objOneIterable) {

            // we check the right only for models with an system id so that manual created objects still get in the list
            if($objOneIterable->getStrSystemid() != '' && !$objOneIterable->rightView())
                continue;

            $strActions = $this->getActionIcons($objOneIterable, $strListIdentifier);
            $strReturn .= $this->objToolkit->simpleAdminList($objOneIterable, $strActions, $intI++, count($arrMassActions) > 0);
        }

        $strNewActions = $this->mergeNewEntryActions($this->getNewEntryAction($strListIdentifier));
        $strBatchActions = "";

        if(count($arrMassActions) > 0)
            $strBatchActions .= $this->objToolkit->renderBatchActionHandlers($arrMassActions);

        if($strNewActions != "" || $strBatchActions != "")
            $strReturn .= $this->objToolkit->genericAdminList("batchActionSwitch", $strBatchActions, "", $strNewActions, $intI, "", "", $strBatchActions != "");

        if($bitSortable)
            $strReturn .= $this->objToolkit->dragableListFooter($strListId);
        else
            $strReturn .= $this->objToolkit->listFooter();


        $strReturn .= $this->objToolkit->getPageview($objArraySectionIterator, $this->getArrModule("modul"), $this->getAction(), "&systemid=" . $this->getSystemid() . $this->strPeAddon . $strPagerAddon);

        return $strReturn;
    }

    /**<commit_msg>UPDATE: module_system | class_admin_simple > use Closure as optional check to preserve behaviour<commit_after><?php
/*"******************************************************************************************************
*   (c) 2007-2015 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$	                                            *
********************************************************************************************************/


/**
 * Class holding common methods for extended and simplified admin-guis.
 *
 * @package module_system
 * @since 4.0
 */
abstract class class_admin_simple extends class_admin_controller {

    private  $strPeAddon = "";

    /**
     * @param string $strSystemid
     */
    public function __construct($strSystemid = "") {
        parent::__construct($strSystemid);

        if($this->getParam("pe") == "1")
            $this->strPeAddon = "&pe=1";

        if($this->getParam("unlockid") != "") {
            $objLockmanager = new class_lockmanager($this->getParam("unlockid"));
            $objLockmanager->unlockRecord(true);
        }
    }

    /**
     * Overwritten in order to inject a toolbar per record. may be useful for certain actions
     * @param array &$arrContent
     * @return void
     */
    protected function onRenderOutput(&$arrContent) {
        $arrContent["actiontoolbar"] = $this->objToolkit->getContentActionToolbar($this->getContentActionToolbar());
    }

    /**
     * Default-implementation to render an action toolbar
     *
     * @return string
     */
    protected function getContentActionToolbar() {
        if(uniStrpos($this->getAction(), "list") !== false || uniStrpos($this->getAction(), "new") !== false || uniStrpos($this->getAction(), "save") !== false)
            return "";

        if(validateSystemid($this->getSystemid())) {
            $objRecord = class_objectfactory::getInstance()->getObject($this->getSystemid());

            if($objRecord instanceof interface_admin_listable)
                return $this->getActionIcons($objRecord);
        }

        return "";
    }


    /**
     * Renders the form to create a new entry
     * @abstract
     * @return string
     * @permissions edit
     */
    protected abstract function actionNew();

    /**
     * Renders the form to edit an existing entry
     * @abstract
     * @return string
     * @permissions edit
     */
    protected abstract function actionEdit();

    /**
     * Renders the general list of records
     * @abstract
     * @return string
     * @permissions view
     */
    protected abstract function actionList();


    /**
     * A general action to delete a record.
     * This method may be overwritten by subclasses.
     *
     * @permissions delete
     * @throws class_exception
     * @return void
     */
    protected function actionDelete() {
        $objRecord = class_objectfactory::getInstance()->getObject($this->getSystemid());
        if($objRecord != null && $objRecord->rightDelete()) {
            if(!$objRecord->deleteObject())
                throw new class_exception("error deleting object ".strip_tags($objRecord->getStrDisplayName()), class_exception::$level_ERROR);

            $strTargetUrl = urldecode($this->getParam("reloadUrl"));

            if($strTargetUrl == "" || uniStrpos($strTargetUrl, $this->getSystemid()) !== false) {

                $strTargetUrl = "admin=1&module=".$this->getArrModule("modul");

                $intI = 1;
                while($this->getHistory($intI) !== null) {
                    $strTargetUrl = $this->getHistory($intI++);

                    if(uniStrpos($strTargetUrl, $this->getSystemid()) === false) {
                        if(uniStrpos($strTargetUrl, "admin=1") === false)
                            $strTargetUrl = "admin=1&module=".$this->getArrModule("modul");

                        break;
                    }
                }

            }

            $this->adminReload(_indexpath_."?".$strTargetUrl.($this->getParam("pe") != "" ? "&peClose=1&blockAction=1" : ""));
        }
        else
            throw new class_exception("error loading object ".$this->getSystemid(), class_exception::$level_ERROR);
    }

    /**
     * A general action to copy a record.
     * This method may be overwritten by subclasses.
     *
     * @permissions edit
     * @throws class_exception
     * @return void
     */
    protected function actionCopyObject() {
        $objRecord = class_objectfactory::getInstance()->getObject($this->getSystemid());
        if($objRecord != null && $objRecord->rightEdit()) {
            if(!$objRecord->copyObject())
                throw new class_exception("error creating a copy of object ".strip_tags($objRecord->getStrDisplayName()), class_exception::$level_ERROR);

            $this->adminReload(class_link::getLinkAdminHref($this->getArrModule("modul"), $this->getActionNameForClass("list", $objRecord), "&systemid=".$objRecord->getPrevId()));
        }
        else
            throw new class_exception("error loading object ".$this->getSystemid(), class_exception::$level_ERROR);
    }


    /**
     * Returns the action name for a given class name.
     *
     * @param string $strAction
     * @param interface_model $objInstance
     * @return string specific action name
     */
    protected function getActionNameForClass($strAction, $objInstance) {
        return $strAction;
    }


    /**
     * Renders a list of items in a floatable "thumbnail" view, so a grid.
     * Please be aware, that the combination of paging and grids may result in unpredictable ordering.
     * As soon as the list is sortable, the page-size should be at least the same as the number of elements
     *
     * @param class_array_section_iterator $objArraySectionIterator
     * @param string $strListIdentifier an internal identifier to check the current parent-list
     * @param string $strPagerAddon
     * @param bool $bitSortable
     *
     * @throws class_exception
     * @return string
     */
    protected function renderFloatingGrid(class_array_section_iterator $objArraySectionIterator, $strListIdentifier = "", $strPagerAddon = "", $bitSortable = true) {
        $strReturn = "";

        $strListActions = "";
        if($this->renderLevelUpAction($strListIdentifier) != "") {
            $strListActions .= $this->objToolkit->listButton($this->renderLevelUpAction($strListIdentifier));
        }

        $strListActions .= $this->mergeNewEntryActions($this->getNewEntryAction($strListIdentifier));

        if($strListActions != "") {
            $strReturn .= $this->objToolkit->listHeader();
            $strReturn .= $this->objToolkit->genericAdminList("", "", "", $strListActions, 0);
            $strReturn .= $this->objToolkit->listFooter();
        }


        if(!$objArraySectionIterator->valid())
            $strReturn .= $this->objToolkit->getTextRow($this->getLang("commons_list_empty"));


        if($objArraySectionIterator->valid()) {

            $strReturn .= $this->objToolkit->gridHeader($bitSortable, $objArraySectionIterator->getIntElementsPerPage(), $objArraySectionIterator->getPageNumber());

            /** @var $objOneIterable class_model|interface_model|interface_admin_gridable */
            foreach($objArraySectionIterator as $objOneIterable) {

                if(!$objOneIterable->rightView() || !$objOneIterable instanceof interface_admin_gridable)
                    continue;

                $strActions = $this->getActionIcons($objOneIterable, $strListIdentifier);
                $strReturn .= $this->objToolkit->gridEntry($objOneIterable, $strActions, $this->renderGridEntryClickAction($objOneIterable, $strListIdentifier));
            }

            $strReturn .= $this->objToolkit->gridFooter();
        }

        $strReturn .= $this->objToolkit->getPageview($objArraySectionIterator, $this->getArrModule("modul"), $this->getAction(), "&systemid=".$this->getSystemid().$this->strPeAddon.$strPagerAddon);

        return $strReturn;
    }


    /**
     * Renders a list of items, target is the common admin-list.
     * Please be aware, that the combination of paging and sortable-lists may result in unpredictable ordering.
     * As soon as the list is sortable, the page-size should be at least the same as the number of elements
     *
     * @param class_array_section_iterator $objArraySectionIterator
     * @param bool $bitSortable
     * @param string $strListIdentifier an internal identifier to check the current parent-list
     * @param bool $bitAllowTreeDrop
     * @param string $strPagerAddon
     *
     * @throws class_exception
     * @return string
     */
    protected function renderList(class_array_section_iterator $objArraySectionIterator, $bitSortable = false, $strListIdentifier = "", $bitAllowTreeDrop = false, $strPagerAddon = "", Closure $objFilter = null) {
        $strReturn = "";
        $intI = 0;

        $strListId = generateSystemid();

        if(!$objArraySectionIterator->valid())
            $strReturn .= $this->objToolkit->getTextRow($this->getLang("commons_list_empty"));

        if($bitSortable)
            $strReturn .= $this->objToolkit->dragableListHeader($strListId, false, $bitAllowTreeDrop, $objArraySectionIterator->getIntElementsPerPage(), $objArraySectionIterator->getPageNumber());
        else
            $strReturn .= $this->objToolkit->listHeader();

        if($this->renderLevelUpAction($strListIdentifier) != "") {
            $strReturn .= $this->objToolkit->genericAdminList("", "", "", $this->objToolkit->listButton($this->renderLevelUpAction($strListIdentifier)), $intI++);
        }

        $arrMassActions = $this->getBatchActionHandlers($strListIdentifier);

        /** @var $objOneIterable class_model|interface_model|interface_admin_listable */
        foreach($objArraySectionIterator as $objOneIterable) {

            // if we have a filter Closure call it else use the standard rightView method
            if($objFilter !== null) {
                if($objFilter($objOneIterable) === false) {
                    continue;
                }
            }
            else if(!$objOneIterable->rightView()) {
                continue;
            }

            $strActions = $this->getActionIcons($objOneIterable, $strListIdentifier);
            $strReturn .= $this->objToolkit->simpleAdminList($objOneIterable, $strActions, $intI++, count($arrMassActions) > 0);
        }

        $strNewActions = $this->mergeNewEntryActions($this->getNewEntryAction($strListIdentifier));
        $strBatchActions = "";

        if(count($arrMassActions) > 0)
            $strBatchActions .= $this->objToolkit->renderBatchActionHandlers($arrMassActions);

        if($strNewActions != "" || $strBatchActions != "")
            $strReturn .= $this->objToolkit->genericAdminList("batchActionSwitch", $strBatchActions, "", $strNewActions, $intI, "", "", $strBatchActions != "");

        if($bitSortable)
            $strReturn .= $this->objToolkit->dragableListFooter($strListId);
        else
            $strReturn .= $this->objToolkit->listFooter();


        $strReturn .= $this->objToolkit->getPageview($objArraySectionIterator, $this->getArrModule("modul"), $this->getAction(), "&systemid=" . $this->getSystemid() . $this->strPeAddon . $strPagerAddon);

        return $strReturn;
    }

    /**
{'fraction_non_alphanumeric': 0.1566894509991728, 'fraction_numerical': 0.0020897731725368976, 'mean_word_length': 2.703046912784137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 231, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2261664', 'n_tokens_mistral': 6187, 'n_tokens_neox': 5968, 'n_words': 1231}