Опубликовано: 24 февраля 20196 г Здравствуйте. У меня есть две функции: Основная (лишний код удалил) : 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 ) ) ); } } Как правильно передать значения из первой функции во вторую, чтобы не дублировать код?
Опубликовано: 24 февраля 20196 г Я думаю тут уместо написать отдельную функцию которая вернет массив where.
Здравствуйте.
У меня есть две функции:
Основная (лишний код удалил) :
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 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 ) ) ); } }Как правильно передать значения из первой функции во вторую, чтобы не дублировать код?