config = $config; $this->config_text = $config_text; $this->db = $db; $this->language = $language; $this->operator_item = $operator_item; $this->parser = $parser; $this->renderer = $renderer; $this->request = $request; $this->template = $template; $this->user_loader = $user_loader; $this->utils = $utils; $this->categories_table = $categories_table; $this->items_table = $items_table; $this->logs_table = $logs_table; } /** * Handle and display the "Overview" ACP mode. * * @return void * @access public */ public function overview() { $this->language->add_lang(['ass_acp_common', 'ass_common'], 'phpbbstudio/ass'); $action = $this->request->variable('action', '', true); $submit = $this->request->is_set_post('submit'); $notes = $this->config_text->get('ass_admin_notes'); if ($action === 'notes') { if ($submit) { $notes = $this->parser->parse($this->request->variable('notes', '', true)); $this->config_text->set('ass_admin_notes', $notes); } else { $this->template->assign_vars([ 'NOTES_EDIT' => $this->utils->unparse($notes), 'S_NOTES' => true, ]); } } $item_modes = [ 'featured', 'featured_coming', 'sale', 'sale_coming', 'low_stock', 'low_sellers', 'top_sellers', 'recent', ]; foreach ($item_modes as $mode) { $items = $this->get_items($mode); foreach ($items as $item) { $this->template->assign_block_vars($mode, $this->operator_item->get_variables($item)); } } foreach ($this->get_recent() as $row) { $item = $this->operator_item->get_entity()->import($row); $this->template->assign_block_vars('purchases', array_merge( $this->operator_item->get_variables($item), ['PURCHASE_TIME' => (int) $row['log_time']] )); } $buyers = $this->get_users('buyers'); $gifters = $this->get_users('gifters'); $spenders = $this->get_users('spenders'); $this->user_loader->load_users(array_merge(array_keys($buyers), array_keys($gifters), array_keys($spenders))); $users = [ 'buyers' => $buyers, 'gifters' => $gifters, 'spenders' => $spenders, ]; foreach ($users as $user_mode => $users_array) { foreach ($users_array as $user_id => $count) { $this->template->assign_block_vars($user_mode, [ 'NAME' => $this->user_loader->get_username($user_id, 'full'), 'AVATAR' => $this->user_loader->get_avatar($user_id), 'COUNT' => $count, ]); } } $this->template->assign_vars([ 'COUNTS' => $this->get_counts(), 'NOTES' => $notes ? $this->renderer->render(htmlspecialchars_decode($notes, ENT_COMPAT)) : '', 'GIFTING_ENABLED' => (bool) $this->config['ass_gift_enabled'], 'NO_IMAGE_ICON' => (string) $this->config['ass_no_image_icon'], 'SHOP_ACTIVE' => (bool) $this->config['ass_active'], 'SHOP_ENABLED' => (bool) $this->config['ass_enabled'], 'U_ACTION' => $this->u_action, 'U_NOTES' => $this->u_action . '&action=notes', ]); } /** * Get items for a specific mode. * * @param string $mode The item mode (featured|sale|etc..) * @return array Item entities * @access protected */ protected function get_items($mode) { $sql_array = [ 'SELECT' => 'i.*', 'FROM' => [$this->items_table => 'i'], 'WHERE' => $this->get_sql_where($mode), 'ORDER_BY' => $this->get_sql_order($mode), ]; $sql = $this->db->sql_build_query('SELECT', $sql_array); $result = $this->db->sql_query_limit($sql, 5); $rowset = $this->db->sql_fetchrowset($result); $this->db->sql_freeresult($result); return $this->operator_item->get_entities($rowset); } /** * Get recent items. * * @return array Item entities * @access protected */ protected function get_recent() { $sql = 'SELECT i.*, l.log_time FROM ' . $this->logs_table . ' l, ' . $this->items_table . ' i WHERE i.item_id = l.item_id AND l.item_purchase = 1 ORDER BY l.log_time DESC'; $result = $this->db->sql_query_limit($sql, 5); $rowset = $this->db->sql_fetchrowset($result); $this->db->sql_freeresult($result); return (array) $rowset; } /** * Get users for a specific mode. * * @param string $mode The mode (buyers|gifters|spenders) * @return array User rows * @access protected */ protected function get_users($mode) { $users = []; switch ($mode) { case 'buyers': $select = 'COUNT(log_id)'; $where = ' WHERE recipient_id = 0'; break; case 'gifters': $select = 'COUNT(log_id)'; $where = ' WHERE recipient_id <> 0'; break; case 'spenders': default: $select = 'SUM(points_sum)'; $where = ''; break; } $sql = 'SELECT ' . $select . ' as count, user_id FROM ' . $this->logs_table . $where . ' GROUP BY user_id ORDER BY count DESC'; $result = $this->db->sql_query_limit($sql, 5); while ($row = $this->db->sql_fetchrow($result)) { $users[(int) $row['user_id']] = $row['count']; } $this->db->sql_freeresult($result); return (array) $users; } /** * Get counts for various things. * * @return array Array of counts * @access protected */ protected function get_counts() { $counts = [ 'categories' => (int) $this->db->get_row_count($this->categories_table), 'items' => (int) $this->db->get_row_count($this->items_table), ]; $sql = 'SELECT COUNT(i.item_id) as count FROM ' . $this->items_table . ' i WHERE ' . $this->get_sql_where('featured'); $result = $this->db->sql_query_limit($sql , 1); $counts['featured'] = (int) $this->db->sql_fetchfield('count'); $this->db->sql_freeresult($result); $sql = 'SELECT COUNT(i.item_id) as count FROM ' . $this->items_table . ' i WHERE ' . $this->get_sql_where('sale'); $result = $this->db->sql_query_limit($sql , 1); $counts['sale'] = (int) $this->db->sql_fetchfield('count'); $this->db->sql_freeresult($result); $sql = 'SELECT SUM(item_purchases) as count FROM ' . $this->items_table; $result = $this->db->sql_query_limit($sql , 1); $counts['purchases'] = (int) $this->db->sql_fetchfield('count'); $this->db->sql_freeresult($result); $sql = 'SELECT SUM(points_sum) as count FROM ' . $this->logs_table; $result = $this->db->sql_query_limit($sql , 1); $counts['spent'] = (double) $this->db->sql_fetchfield('count'); $this->db->sql_freeresult($result); $sql = 'SELECT COUNT(item_conflict) as count FROM ' . $this->items_table . ' WHERE item_conflict = 1'; $result = $this->db->sql_query_limit($sql , 1); $counts['errors'] = (double) $this->db->sql_fetchfield('count'); $this->db->sql_freeresult($result); return $counts; } /** * Get the SQL WHERE statement for a specific mode * * @param string $mode * @return string * @access protected */ protected function get_sql_where($mode) { switch ($mode) { case 'low_stock': return 'i.item_stock_unlimited <> 1'; case 'featured': return 'i.item_sale_start < ' . time() . ' AND i.item_featured_start <> 0 AND i.item_featured_until <> 0 AND (' . time() . ' BETWEEN i.item_featured_start AND i.item_featured_until)'; case 'featured_coming': return 'i.item_featured_start <> 0 AND i.item_featured_until <> 0 AND i.item_featured_start > ' . time(); case 'sale': return 'i.item_featured_start < ' . time() . ' AND i.item_sale_start <> 0 AND i.item_sale_until <> 0 AND (' . time() . ' BETWEEN i.item_sale_start AND item_sale_until)'; case 'sale_coming': return 'i.item_sale_start <> 0 AND i.item_sale_until <> 0 AND i.item_sale_start > ' . time(); default: return ''; } } /** * Get the SQL ORDER BY statement for a specific mode. * * @param string $mode * @return string * @access protected */ protected function get_sql_order($mode) { switch ($mode) { case 'low_stock': return 'i.item_stock ASC, i.item_title ASC'; case 'low_sellers': return 'i.item_purchases ASC, i.item_title ASC'; case 'top_sellers': return 'i.item_purchases DESC, i.item_title ASC'; case 'recent': return 'i.item_create_time DESC'; case 'featured': return 'i.item_featured_until ASC, i.item_title ASC'; case 'featured_coming': return 'i.item_featured_start ASC, i.item_title ASC'; case 'sale': return 'i.item_sale_until ASC, i.item_title ASC'; case 'sale_coming': return 'i.item_sale_start ASC, i.item_title ASC'; default: return 'i.item_title ASC'; } } /** * Set custom form action. * * @param string $u_action Custom form action * @return self $this This controller for chaining calls * @access public */ public function set_page_url($u_action) { $this->u_action = $u_action; return $this; } }