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

/**
 * The core eventmanager is used to trigger and fire internal events such as status-changed or record-deleted-events.
 * Therefore the corresponding interface-implementers are called and notified.
 *
 * Since version 4.4, the eventdispatcher is totally generic. Hardcoded dependencies are no longer required.
 *
 * @package module_system
 * @author <EMAIL>
 * @since 4.0
 */
class class_core_eventdispatcher {

    /**
     * @var class_model[]
     */
    private static $arrListeners = array();


    /**
     * Returns all classes (here: instances of the class) implementing a given interface
     * @param string $strInterface
     *
     * @return class_model
     */
    public static function getEventListeners($strInterface) {
        if(!isset(self::$arrListeners[$strInterface]))
            self::$arrListeners[$strInterface] = self::loadInterfaceImplementers($strInterface);

        return self::$arrListeners[$strInterface];
    }


    /**
     * Generic function to notify a set of event-listeners.
     * Therefore the implementing interface, the name of the notify-method and an array of arguments sent to the handler
     * are required.
     *
     * @param string $strInterface
     * @param string $strMethodname
     * @param array $arrArguments
     *
     * @return bool
     */
    public static function notifyListeners($strInterface, $strMethodname,  $arrArguments) {
        if(!isset(self::$arrListeners[$strInterface]))
            self::$arrListeners[$strInterface] = self::loadInterfaceImplementers($strInterface);

        $bitReturn = true;
        foreach(self::$arrListeners[$strInterface] as $objOneListener) {
            $bitReturn = $bitReturn && call_user_func_array(array($objOneListener, $strMethodname), $arrArguments);
        }

        return $bitReturn;
    }


    /**
     * @var interface_recordupdated_listener
     */
    private static $arrRecordUpdatedListener = null;

    /**
     * Triggers all model-classes implementing the interface interface_statuschanged_listener and notifies
     * about a new status set.
     *
     * @param string $strSystemid
     * @param int $intNewStatus
     *
     * @return bool
     * @static
     * @see interface_statuschanged_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyStatusChangedListeners($strSystemid, $intNewStatus) {
        return self::notifyListeners("interface_statuschanged_listener", "handleStatusChangedEvent", array($strSystemid, $intNewStatus));
    }

    /**
     * Triggers all model-classes implementing the interface interface_recorddeleted_listener and notifies them about a
     * deleted record.
     *
     * @param string $strSystemid
     * @param string $strClass
     *
     * @return bool
     *
     * @see interface_recorddeleted_listener
     * @static
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyRecordDeletedListeners($strSystemid, $strClass) {
        return self::notifyListeners("interface_recorddeleted_listener", "handleRecordDeletedEvent", array($strSystemid, $strClass));
    }


    /**
     * Triggers all model-classes implementing the interface interface_previdchanged_listener and notifies them about a
     * deleted record.
     *
     * @param string $strSystemid
     * @param string $strOldPrevid
     * @param string $strNewPrevid
     *
     * @static
     * @return bool
     * @see interface_previdchanged_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyPrevidChangedListeners($strSystemid, $strOldPrevid, $strNewPrevid) {
        return self::notifyListeners("interface_previdchanged_listener", "handlePrevidChangedEvent", array($strSystemid, $strOldPrevid, $strNewPrevid));
    }

    /**
     * Triggers all model-classes implementing the interface interface_recordupdated_listener and notifies them about an updated object
     *
     * @static
     *
     * @param class_model $objRecord
     *
     * @return bool
     * @see interface_recordupdated_listener
     */
    public static function notifyRecordUpdatedListeners(class_model $objRecord) {
        $bitReturn = true;
        $arrListener = self::getRecordUpdatedListeners();
        /** @var interface_recordupdated_listener $objOneListener */
        foreach($arrListener as $objOneListener) {
            class_logger::getInstance(class_logger::EVENTS)->addLogRow("propagating recordUpdated to ".get_class($objOneListener)." sysid: ".$objRecord->getSystemid(), class_logger::$levelInfo);
            $bitReturn = $bitReturn && $objOneListener->handleRecordUpdatedEvent($objRecord);
        }

        return $bitReturn;
    }


    /**
     * Triggers all model-classes implementing the interface interface_recordcopied_listener and notifies them about a
     * copied record.
     *
     * @param string $strOldSystemid
     * @param string $strNewSystemid
     *
     * @static
     * @return bool
     * @see interface_recordcopied_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyRecordCopiedListeners($strOldSystemid, $strNewSystemid) {
        return self::notifyListeners("interface_recordcopied_listener", "handleRecordCopiedEvent", array($strOldSystemid, $strNewSystemid));
    }


    /**
     * Triggers all model-classes implementing the interface interface_userfirstlogin_listener and notifies them about a
     * users first login.
     *
     * @param string $strUserid
     *
     * @static
     * @return bool
     * @see interface_userfirstlogin_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyUserFirstLoginListeners($strUserid) {
        return self::notifyListeners("interface_userfirstlogin_listener", "handleUserFirstLoginEvent", array($strUserid));
    }


    /**
     * Loads all objects registered to ne notified in case of previd-changes
     * @static
     * @return interface_recordupdated_listener[]
     */
    private static function getRecordUpdatedListeners() {
        if(self::$arrRecordUpdatedListener == null) {
            self::$arrRecordUpdatedListener = self::loadInterfaceImplementers("interface_recordupdated_listener");
        }

        return self::$arrRecordUpdatedListener;
    }

    /**
     * Loads all business-objects implementing the passed interface
     *
     * @param string $strTargetInterface
     *
     * @static
     * @return array
     */
    private static function loadInterfaceImplementers($strTargetInterface) {
        //load classes in system-folders
        return class_resourceloader::getInstance()->getFolderContent("/system", array(".php"), false, function(&$strOneFile) use ($strTargetInterface) {<commit_msg>NEW: migrated a internal event-dispatcher to the generic approach<commit_after><?php
/*"******************************************************************************************************
*   (c) 2004-2006 by MulchProductions, www.mulchprod.de                                                 *
*   (c) 2007-2014 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*   $Id$                                         *
********************************************************************************************************/

/**
 * The core eventmanager is used to trigger and fire internal events such as status-changed or record-deleted-events.
 * Therefore the corresponding interface-implementers are called and notified.
 *
 * Since version 4.4, the eventdispatcher is totally generic. Hardcoded dependencies are no longer required.
 *
 * @package module_system
 * @author <EMAIL>
 * @since 4.0
 */
class class_core_eventdispatcher {

    /**
     * @var class_model[]
     */
    private static $arrListeners = array();


    /**
     * Returns all classes (here: instances of the class) implementing a given interface
     * @param string $strInterface
     *
     * @return class_model
     */
    public static function getEventListeners($strInterface) {
        if(!isset(self::$arrListeners[$strInterface]))
            self::$arrListeners[$strInterface] = self::loadInterfaceImplementers($strInterface);

        return self::$arrListeners[$strInterface];
    }


    /**
     * Generic function to notify a set of event-listeners.
     * Therefore the implementing interface, the name of the notify-method and an array of arguments sent to the handler
     * are required.
     *
     * @param string $strInterface
     * @param string $strMethodname
     * @param array $arrArguments
     *
     * @return bool
     */
    public static function notifyListeners($strInterface, $strMethodname,  $arrArguments) {
        if(!isset(self::$arrListeners[$strInterface]))
            self::$arrListeners[$strInterface] = self::loadInterfaceImplementers($strInterface);

        $bitReturn = true;
        foreach(self::$arrListeners[$strInterface] as $objOneListener) {
            $bitReturn = $bitReturn && call_user_func_array(array($objOneListener, $strMethodname), $arrArguments);
        }

        return $bitReturn;
    }


    /**
     * Triggers all model-classes implementing the interface interface_statuschanged_listener and notifies
     * about a new status set.
     *
     * @param string $strSystemid
     * @param int $intNewStatus
     *
     * @return bool
     * @static
     * @see interface_statuschanged_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyStatusChangedListeners($strSystemid, $intNewStatus) {
        return self::notifyListeners("interface_statuschanged_listener", "handleStatusChangedEvent", array($strSystemid, $intNewStatus));
    }

    /**
     * Triggers all model-classes implementing the interface interface_recorddeleted_listener and notifies them about a
     * deleted record.
     *
     * @param string $strSystemid
     * @param string $strClass
     *
     * @return bool
     *
     * @see interface_recorddeleted_listener
     * @static
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyRecordDeletedListeners($strSystemid, $strClass) {
        return self::notifyListeners("interface_recorddeleted_listener", "handleRecordDeletedEvent", array($strSystemid, $strClass));
    }


    /**
     * Triggers all model-classes implementing the interface interface_previdchanged_listener and notifies them about a
     * deleted record.
     *
     * @param string $strSystemid
     * @param string $strOldPrevid
     * @param string $strNewPrevid
     *
     * @static
     * @return bool
     * @see interface_previdchanged_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyPrevidChangedListeners($strSystemid, $strOldPrevid, $strNewPrevid) {
        return self::notifyListeners("interface_previdchanged_listener", "handlePrevidChangedEvent", array($strSystemid, $strOldPrevid, $strNewPrevid));
    }


    /**
     * Triggers all model-classes implementing the interface interface_recordupdated_listener and notifies them about an updated object
     *
     * @param class_model $objRecord
     *
     * @static
     * @return bool
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see interface_recordupdated_listener
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyRecordUpdatedListeners(class_model $objRecord) {
        return self::notifyListeners("interface_recordupdated_listener", "handleRecordUpdatedEvent", array($objRecord));
    }


    /**
     * Triggers all model-classes implementing the interface interface_recordcopied_listener and notifies them about a
     * copied record.
     *
     * @param string $strOldSystemid
     * @param string $strNewSystemid
     *
     * @static
     * @return bool
     * @see interface_recordcopied_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyRecordCopiedListeners($strOldSystemid, $strNewSystemid) {
        return self::notifyListeners("interface_recordcopied_listener", "handleRecordCopiedEvent", array($strOldSystemid, $strNewSystemid));
    }


    /**
     * Triggers all model-classes implementing the interface interface_userfirstlogin_listener and notifies them about a
     * users first login.
     *
     * @param string $strUserid
     *
     * @static
     * @return bool
     * @see interface_userfirstlogin_listener
     * @deprecated use class_core_eventdispatcher::notifyListeners() instead
     * @see class_core_eventdispatcher::notifyListeners()
     */
    public static function notifyUserFirstLoginListeners($strUserid) {
        return self::notifyListeners("interface_userfirstlogin_listener", "handleUserFirstLoginEvent", array($strUserid));
    }


    /**
     * Loads all business-objects implementing the passed interface
     *
     * @param string $strTargetInterface
     *
     * @static
     * @return array
     */
    private static function loadInterfaceImplementers($strTargetInterface) {
        //load classes in system-folders
        return class_resourceloader::getInstance()->getFolderContent("/system", array(".php"), false, function(&$strOneFile) use ($strTargetInterface) {
{'fraction_non_alphanumeric': 0.12907952577594245, 'fraction_numerical': 0.00293059810843213, 'mean_word_length': 3.090166167256878, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5006604', 'n_tokens_mistral': 3756, 'n_tokens_neox': 3781, 'n_words': 908}