TemKa_SD Posted February 24, 2019 Share Posted February 24, 2019 Здравствуйте. У меня есть две функции: Основная (лишний код удалил) : public function getGamelist($ActiveGamesWidgetType, $ActiveGamesWidgetBots, $ActiveGamesWidgetSortBy, $ActiveGamesWidgetPagination, $ActiveGamesWidgetPerPage, $uniqueKey) { $where = array(); $botIds = array(); $ids = array(); // Фильтр по botid if ( isset( \IPS\Request::i()->botId ) ) { $ids = explode( ',', \IPS\Request::i()->botId ); } else if ( isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) ) { $ids = explode( ',', \IPS\Request::i()->cookie['runningGames_botIds'] ); } if ( isset( $ActiveGamesWidgetBots ) AND (!isset( \IPS\Request::i()->botId ) AND !isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) ) ) { $ids = explode( ',', $ActiveGamesWidgetBots ); } if ( \count( $ids ) ) { foreach( $ids as $id ) { $botIds = array_filter( $ids, 'intval' ); } if ( \count( $botIds ) ) { $where[] = array( \IPS\Db::i()->in( 'botid', array_filter( $botIds ) ) ); } } } Это передается из настроек виджета: public function getGamelist($ActiveGamesWidgetType, $ActiveGamesWidgetBots, $ActiveGamesWidgetSortBy, $ActiveGamesWidgetPagination, $ActiveGamesWidgetPerPage, $uniqueKey) ВТОРАЯ ФУНКЦИЯ СО СТАТИСТИКОЙ: public function getStats() { $where = array(); $botIds = array(); $ids = array(); // Фильтр по botid if ( isset( \IPS\Request::i()->botId ) ) { $ids = explode( ',', \IPS\Request::i()->botId ); } else if ( isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) ) { $ids = explode( ',', \IPS\Request::i()->cookie['runningGames_botIds'] ); } if ( \count( $ids ) ) { foreach( $ids as $id ) { $botIds = array_filter( $ids, 'intval' ); } if ( \count( $botIds ) ) { $where[] = array( \IPS\Db::i()->in( 'botid', array_filter( $botIds ) ) ); } } $cg = new \IPS\Patterns\ActiveRecordIterator(\IPS\Db::i('wc3')->select('*', static::$databaseTable, $where, 'lobby DESC'), '\IPS\wc3\ActiveGames\ActiveGames'); $lobbies = $games = $players = 0; foreach($cg as $g) { if($g->getLobby()) { $lobbies++; } else { $games++; } $players += $g->getSlotstaken(); foreach( static::getUsers($g->getUsernames()) as $realm ) { isset( $realms[ $realm['realm'] ] ) ? $realms[ $realm['realm'] ]++ : $realms[ $realm['realm'] ] = 1; } } return [ 'lobbies' => $lobbies, 'games' => $games, 'players' => $players, 'realms' => isset( $realms ) ? $realms : '' ]; } Этот код в статистике дублируется: $botIds = array(); $ids = array(); // Фильтр по botid if ( isset( \IPS\Request::i()->botId ) ) { $ids = explode( ',', \IPS\Request::i()->botId ); } else if ( isset( \IPS\Request::i()->cookie['runningGames_botIds'] ) ) { $ids = explode( ',', \IPS\Request::i()->cookie['runningGames_botIds'] ); } if ( \count( $ids ) ) { foreach( $ids as $id ) { $botIds = array_filter( $ids, 'intval' ); } if ( \count( $botIds ) ) { $where[] = array( \IPS\Db::i()->in( 'botid', array_filter( $botIds ) ) ); } } Как правильно передать значения из первой функции во вторую, чтобы не дублировать код? Link to comment Share on other sites More sharing options...
siv1987 Posted February 24, 2019 Share Posted February 24, 2019 Я думаю тут уместо написать отдельную функцию которая вернет массив where. Link to comment Share on other sites More sharing options...
TemKa_SD Posted February 25, 2019 Author Share Posted February 25, 2019 Потом вызывать в этих функциях с помощью $this? Link to comment Share on other sites More sharing options...
siv1987 Posted February 25, 2019 Share Posted February 25, 2019 Абсолютли. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now