Всем доброго времени суток! Есть партнёрская программа на Opencart, и есть отчёт – где выводятся все партнёры по уровням. Подскажите, как реализовать следующее: При нажатии к примеру на партнёра первого уровня – показывать всех партнёров второго уровня, которых он привёл лично. При нажатии на одного из показанных партнёров второго уровня показывать партнёров третьего уровня, которых этот второй уровень привёл лично. То есть по сути при загрузке страницы изначально должен показываться список партнёров первого уровня, а дальше при клике на одного из них, страница будет обновляться, и будут показываться его партнёры и тд. Уровней всего три, и расширение больше не планируется.
Вот код модели данного отчёта:
function affiliateCountUser($ parent_id = 0) { $ affiliates = []; $ firstLevelPartners = "SELECT affiliate_id, CONCAT(firstname, ' ', lastname) AS name FROM `" . DB_PREFIX . "affiliate` where parent=$ parent_id "; $ query = $ this->db->query($ firstLevelPartners); if ($ query->num_rows) { $ results = $ query->rows; foreach ($ results as $ result) { $ affiliates[$ result['affiliate_id']]['affiliate_id'] = $ result['affiliate_id']; $ affiliates[$ result['affiliate_id']]['name'] = $ result['name']; $ affiliates[$ result['affiliate_id']]['parents'] = $ this->affiliateCountUser($ result['affiliate_id']); $ affiliates[$ result['affiliate_id']]['counts'] = count($ affiliates[$ result['affiliate_id']]['parents']); } } return $ affiliates; } function affiliateFirstLevelInfo() { $ results = $ this->affiliateCountUser(); foreach ($ results as $ result) { $ affiliates_ids = implode($ this->array_column_recursive($ result, 'affiliate_id'), ','); $ query = "SELECT count(total) as count_shopping, SUM(total) as sum_shopping FROM `" . DB_PREFIX . "order` WHERE affiliate_id IN ({$ affiliates_ids})"; $ ShoppingQuery = $ this->db->query($ query); if ($ ShoppingQuery->num_rows) { $ Shopping = $ ShoppingQuery->rows; } $ resultCount = $ this->array_column_recursive($ result['parents'], 'counts'); $ responseResult[] = [ 'name' => $ result['name'], 'count_partners_second_level' => $ result['counts'], 'count_partners_third_level' => array_sum($ resultCount), 'count_shopping' => $ Shopping[0]['count_shopping'], 'sum_shopping' => $ this->currency->format($ Shopping[0]['sum_shopping'], $ this->config->get('config_currency')), ]; } return $ responseResult; } function affiliateSecondLevelInfo() { $ secondLevelPartnersParent = "SELECT affiliate_id, CONCAT(firstname, ' ', lastname) AS name FROM `" . DB_PREFIX . "affiliate` where parent = 0"; $ query = $ this->db->query($ secondLevelPartnersParent); if ($ query->num_rows) { $ results = $ query->rows; } $ responseSecondLevelResult = []; $ secondLevelIds = []; foreach ($ results as $ result) { $ sumFirstLevel1 = "SELECT affiliate_id FROM `" . DB_PREFIX . "affiliate` WHERE parent = {$ result['affiliate_id']}"; $ query1 = $ this->db->query($ sumFirstLevel1); if ($ query1->num_rows) { $ results1 = $ query1->rows; foreach ($ results1 as $ item) { $ secondLevelIds[] = $ item['affiliate_id']; } } } foreach ($ secondLevelIds as $ secondLevelId) { $ sumSecondLevel1 = "SELECT `" . DB_PREFIX . "customer`.customer_id, SUM(`" . DB_PREFIX . "order`.total) as sum_shopping, count(`" . DB_PREFIX . "order`.total) as count_shopping ,`" . DB_PREFIX . "customer`.date_added, count(`" . DB_PREFIX . "customer`.customer_id) as count_partners, `" . DB_PREFIX . "customer`.affiliate_paid, CONCAT(`" . DB_PREFIX . "customer`.firstname, ' ', `" . DB_PREFIX . "customer`.lastname) AS name, `" . DB_PREFIX . "customer`.affiliate_paid, `" . DB_PREFIX . "customer`.date_added FROM `" . DB_PREFIX . "customer` INNER JOIN `" . DB_PREFIX . "order` ON `" . DB_PREFIX . "order`.customer_id = `" . DB_PREFIX . "customer`.customer_id WHERE `" . DB_PREFIX . "customer`.affiliate_paid = {$ secondLevelId}"; $ namesDateSecondLevel = "SELECT affiliate_id, parent, CONCAT(firstname, ' ', lastname) AS name , date_added FROM `" . DB_PREFIX . "affiliate` where affiliate_id={$ secondLevelId}"; $ countThirdPartners = "SELECT count(affiliate_id) as count_partners FROM `" . DB_PREFIX . "affiliate` where parent={$ secondLevelId}"; $ sumThirdLevel = "SELECT count(total) as count_shopping, SUM(total) as sum_shopping FROM `" . DB_PREFIX . "order` where affiliate_id={$ secondLevelId}"; $ namesDateSecondLevelQuery = $ this->db->query($ namesDateSecondLevel); $ sumSecondLevel1Query = $ this->db->query($ sumSecondLevel1); $ countThirdPartnersQuery = $ this->db->query($ countThirdPartners); $ sumThirdLevelQuery = $ this->db->query($ sumThirdLevel); if ($ namesDateSecondLevelQuery->num_rows) { $ namesDateSecondLevelResult = $ namesDateSecondLevelQuery->rows; } if ($ sumSecondLevel1Query->num_rows) { $ sumSecondLevel1Result = $ sumSecondLevel1Query->rows; } if ($ countThirdPartnersQuery->num_rows) { $ countThirdPartnersResult = $ countThirdPartnersQuery->rows; } if ($ sumThirdLevelQuery->num_rows) { $ sumThirdLevelResult = $ sumThirdLevelQuery->rows; } $ affiliateSecondThirdShoppingResultSum[0]['sum_shopping'] = $ sumSecondLevel1Result[0]['sum_shopping'] + $ sumThirdLevelResult[0]['sum_shopping']; $ affiliateSecondThirdShoppingResultCount[0]['count_shopping'] = $ sumSecondLevel1Result[0]['count_shopping'] + $ sumThirdLevelResult[0]['count_shopping']; foreach ($ namesDateSecondLevelResult as $ nameResult) { $ secondLevelPartnersParentName = "SELECT CONCAT(firstname, ' ', lastname) AS name FROM `" . DB_PREFIX . "affiliate` where affiliate_id= {$ nameResult['parent']}"; $ secondLevelPartnersParentNameQuery = $ this->db->query($ secondLevelPartnersParentName); if ($ secondLevelPartnersParentNameQuery->num_rows) { $ secondLevelPartnersParentNameResult = $ secondLevelPartnersParentNameQuery->rows; } } $ responseSecondLevelResult[] = [ 'name' => $ namesDateSecondLevelResult[0]['name'], 'count_partners' => $ countThirdPartnersResult[0]['count_partners'], 'count_shopping' => $ affiliateSecondThirdShoppingResultCount[0]['count_shopping'], 'date_linked' => date($ this->language->get('date_format_short'), strtotime($ namesDateSecondLevelResult[0]['date_added'])), 'sum_shopping' => $ this->currency->format($ affiliateSecondThirdShoppingResultSum[0]['sum_shopping'], $ this->config->get('config_currency')), 'parent_partner' => $ secondLevelPartnersParentNameResult[0]['name'] ]; } $ inClause = implode(", ", $ secondLevelIds); $ thirdLevelPartnersParent = "SELECT affiliate_id, date_added, parent, CONCAT(firstname, ' ', lastname) AS name FROM `" . DB_PREFIX . "affiliate` where parent in ($ inClause)"; $ query3 = $ this->db->query($ thirdLevelPartnersParent); $ responseThirdLevelResult = []; if ($ query3->num_rows) { $ result3 = $ query3->rows; foreach ($ result3 as $ item) { $ ThirdLevelPartnersParentName = "SELECT CONCAT(firstname, ' ', lastname) AS name FROM `" . DB_PREFIX . "affiliate` where affiliate_id = {$ namesDateSecondLevelResult[0]['affiliate_id']}"; $ ThirdLevelPartnersParentNameQuery = $ this->db->query($ ThirdLevelPartnersParentName); if ($ ThirdLevelPartnersParentNameQuery->num_rows) { $ ThirdLevelPartnersParentNameResult = $ ThirdLevelPartnersParentNameQuery->rows; } $ sumThirdLevel = "SELECT SUM(`" . DB_PREFIX . "order`.total) as sum_shopping, count(`" . DB_PREFIX . "order`.total) as count_shopping, `" . DB_PREFIX . "customer`.affiliate_paid, CONCAT(`" . DB_PREFIX . "customer`.firstname, ' ', `" . DB_PREFIX . "customer`.lastname) AS name, `" . DB_PREFIX . "customer`.date_added FROM `" . DB_PREFIX . "customer` INNER JOIN `" . DB_PREFIX . "order` ON `" . DB_PREFIX . "order`.customer_id = `" . DB_PREFIX . "customer`.customer_id WHERE `" . DB_PREFIX . "customer`.affiliate_paid = {$ item['affiliate_id']}"; $ query4 = $ this->db->query($ sumThirdLevel); if ($ query4->num_rows) { $ result4 = $ query4->rows; $ responseThirdLevelResult[] = [ 'name' => $ result3[0]['name'], 'date_linked' => date($ this->language->get('date_format_short'), strtotime($ result3[0]['date_added'])), 'sum_shopping' => $ this->currency->format($ result4[0]['sum_shopping'], $ this->config->get('config_currency')), 'parent_partner' => $ ThirdLevelPartnersParentNameResult[0]['name'] ]; } } } return [$ responseSecondLevelResult, $ responseThirdLevelResult]; }
Код tpl :
<?php echo $ header; ?><?php echo $ column_left; ?> <div id="content"> <div class="page-header"> <div class="container-fluid"> <h1><?php echo $ heading_title; ?></h1> <ul class="breadcrumb"> <?php foreach ($ breadcrumbs as $ breadcrumb) { ?> <li><a href="<?php echo $ breadcrumb['href']; ?>"><?php echo $ breadcrumb['text']; ?></a></li> <?php } ?> </ul> </div> </div> <div class="container-fluid"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"><i class="fa fa-bar-chart"></i> <?php echo $ text_list; ?></h3> </div> <div class="panel-body"> <span>Партнеры первого уровня</span> <div class="table-responsive"> <table class="table table-striped table-bordered table-hover"> <thead> <tr align="center"> <td>Партнер</td> <td>Количество партнеров второго уровня</td> <td>Количество партнеров третьего уровня</td> <td>Общее количество выполненных заказов по всем партнерам</td> <td>Оборот по выполненным заказам всех партнеров</td> </tr> </thead> <tbody> <?php if (isset($ data['first_level_info'])) { ?> <?php foreach ($ data['first_level_info'] as $ affiliate_info) { ?> <tr align="center"> <td><a href=""><?php echo($ affiliate_info['name']); ?></a></td> <td><?php echo($ affiliate_info['count_partners_second_level']); ?></td> <td><?php echo($ affiliate_info['count_partners_third_level']); ?></td> <td><?php echo($ affiliate_info['count_shopping']); ?></td> <td><?php echo($ affiliate_info['sum_shopping']); ?></td> </tr> </tr> <?php } ?> <?php } else { ?> <tr> <td class="center" colspan="7"><?php echo $ text_no_results; ?></td> </tr> <?php } ?> </tbody> </table> </div> <span>Партнеры второго уровня</span> <div class="table-responsive"> <table class="table table-striped table-bordered table-hover"> <thead> <tr align="center"> <td>Партнер</td> <td>Количество партнеров третьего уровня</td> <td>Общее количество выполненных заказов</td> <td>Оборот по выполненным заказам всех партнеров</td> <td>Дата привязки</td> <td>Пригласивший партнёр</td> </tr> </thead> <tbody> <?php if (isset($ data['second_level_info'])) { ?> <?php foreach ($ data['second_level_info'] as $ affiliate_info_second) { ?> <tr align="center"> <td><a href=""><?php echo($ affiliate_info_second['name']); ?></a></td> <td><?php echo($ affiliate_info_second['count_partners']); ?></td> <td><?php echo($ affiliate_info_second['count_shopping']); ?></td> <td><?php echo($ affiliate_info_second['sum_shopping']); ?></td> <td><?php echo($ affiliate_info_second['date_linked']); ?></td> <td><?php echo($ affiliate_info_second['parent_partner']); ?></td> </tr> <?php } ?> <?php } else { ?> <tr> <td class="center" colspan="7"><?php echo $ text_no_results; ?></td> </tr> <?php } ?> </tbody> </table> </div> <span>Партнеры третьего уровня</span> <div class="table-responsive"> <table class="table table-striped table-bordered table-hover"> <thead> <tr align="center"> <td>Партнер</td> <td>Оборот по выполненным заказам</td> <td>Дата привязки</td> <td>Пригласивший партнёр</td> </tr> </thead> <tbody> <?php if (isset($ data['third_level_info'])) { ?> <?php foreach ($ data['third_level_info'] as $ affiliate_info_third) { ?> <tr align="center"> <td><?php echo($ affiliate_info_third['name']); ?></td> <td><?php echo($ affiliate_info_third['sum_shopping']); ?></td> <td><?php echo($ affiliate_info_third['date_linked']); ?></td> <td><?php echo($ affiliate_info_third['parent_partner']); ?></td> </tr> <?php } ?> <?php } else { ?> <tr> <td class="center" colspan="7"><?php echo $ text_no_results; ?></td> </tr> <?php } ?> </tbody> </table> </div> <?php echo $ footer; ?>