<commit_before><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\TeamRequest;
use App\Http\Requests\TeamMemberRequest;
use App\TeamMember;
use App\Team;
use App\User;
use Log;
use DB;

use Auth;
use Session;

class CampaignController extends Controller
{
  public function teammember($id)
  {
      Log::info('CampaignController.teammember: ');

      $teamMember = TeamMember::findOrFail($id);
      $team_id = $teamMember->team_id;

      $user = Auth::user();
      $data['link_show']='';
      $data['button_show']= 'true';

      if ($user) {
          $joinedMemberIfExist = DB::table('team_members')
              ->select('team_members.id')
              ->where('team_members.team_id', '=' ,$team_id)
              ->where('team_members.user_id', '=', $user->id)
              ->get();

          if($joinedMemberIfExist){
              $data['button_show'] = 'false';
          } else {
              $data['button_show'] = 'true';
          }

          if($user->id == $teamMember->user_id)
          {
              $data['link_show'] = 'show';
          } else {
              $data['link_show'] = 'hide';
          }

      }

      $teammemberDonation = DB::table('donations')
          ->select(DB::raw('sum(donations.amount) as donation_amount'))
          ->join('team_members', 'donations.team_member_id', '=', 'team_members.id')
          ->where('team_members.id', '=' ,$id)
          ->get();

      $teamMembers= DB::table('team_members')
          ->select('users.name', 'team_members.goal', 'team_members.id', DB::raw(
              'SUM(donations.amount) as amount,(SUM(donations.amount)/team_members.goal) * 100 as per_raised'))
          ->leftJoin('donations', 'team_members.id', '=', 'donations.team_member_id')
          ->join('users', 'team_members.user_id', '=', 'users.id')
          ->where('team_members.team_id', '=', $id)
          ->groupBy('users.name', 'team_members.goal', 'team_members.id')
          ->orderBy('per_raised')
          ->get();

      return view('campaign.teammember', compact('teamMember','teamMembers','teammemberDonation','data'));
      
  }


  public function joinTeam($teamId)
  {
      Log::info('CampaignController.joinTeam: ');
      $data['teamId'] = $teamId;
      $data['action'] = 'join';
      $data['heading'] = 'Join a Campaign Team';

      $teamInfo = DB::table('teams')
                  ->leftJoin('organizations', 'teams.organization_id', '=', 'organizations.id')
                  ->leftJoin('campaigns', 'teams.campaign_id', '=', 'campaigns.id')
                  ->select('teams.name as teamName', 'organizations.name as orgName', 'campaigns.name as campName')
                  ->where('teams.id', '=', $data['teamId'])
                  ->first();

      $data['teamInfo'] = $teamInfo;

      return view('campaign.jointeam', $data);
      
  }

  public function createTeam($campaignId)
  {
      Log::info('CampaignController.createTeam: ');
      $data['campaignId'] = $campaignId;
      $data['action'] = 'create';
      $data['heading'] = 'Create a Campaign Team';


      $campaignInfo = DB::table('campaigns')
                      ->select('campaigns.name as campName')
                      ->where('campaigns.id', '=', $data['campaignId'])
                      ->first();

      $data['campaignInfo'] = $campaignInfo;

      $organizationList = DB::table('organizations')
                          ->whereNull('organizations.deleted_at')
                          ->lists('name', 'id');

      $data['organizationList'] = $organizationList;

      return view('campaign.jointeam', $data);
      
  }

    public function team($id)
    {
        Log::info('CampaignController.team: ');
        $team = Team::findOrFail($id);

        $user = Auth::user();
        $data['link_show']='';
        $data['button_show']= 'true';

        if ($user) {
            $joinedMemberIfExist = DB::table('team_members')
                ->select('team_members.id')
                ->where('team_members.team_id', '=' ,$id)
                ->where('team_members.user_id', '=', $user->id)
                ->get();

            if($joinedMemberIfExist){
                $data['button_show'] = 'false';
            } else {
                $data['button_show'] = 'true';
            }

            if($user->id == $team->created_by)
            {
                $data['link_show'] = 'show';
            } else {
                $data['link_show'] = 'hide';
            }

        }

        $teamDonation = DB::table('donations')
            ->select(DB::raw('sum(donations.amount) as donation_amount'))
            ->join('teams', 'donations.team_id', '=', 'teams.id')
            ->where('teams.id', '=' ,$id)
            ->get();

        $teamMembers= DB::table('team_members')
            ->select('users.name', 'team_members.goal', 'team_members.id', DB::raw(
                'SUM(donations.amount) as amount,(SUM(donations.amount)/team_members.goal) * 100 as per_raised'))
            ->leftJoin('donations', 'team_members.id', '=', 'donations.team_member_id')
            ->join('users', 'team_members.user_id', '=', 'users.id')
            ->where('team_members.team_id', '=', $id)
            ->groupBy('users.name', 'team_members.goal', 'team_members.id')
            ->orderBy('per_raised')
            ->get();


        return view('campaign.team', compact('teamMembers','team','teamDonation','data'));

    }

  public function joinTeamStore(TeamMemberRequest $request)
  {
      Log::info('CampaignController.joinTeamStore - Start: ');<commit_msg>Worked on controller logic to include token on team and team member page<commit_after><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\TeamRequest;
use App\Http\Requests\TeamMemberRequest;
use App\TeamMember;
use App\Team;
use App\User;
use Log;
use DB;

use Auth;
use Session;

class CampaignController extends Controller
{
  public function teammember($id)
  {
      Log::info('CampaignController.teammember: ');

      //$teamMember = TeamMember::findOrFail($id);
      $teamMember = TeamMember::where('token','=',$id);
      $team_id = $teamMember->team_id;

      $user = Auth::user();
      $data['link_show']='';
      $data['button_show']= 'true';

      if ($user) {
          $joinedMemberIfExist = DB::table('team_members')
              ->select('team_members.id')
              ->where('team_members.team_id', '=' ,$team_id)
              ->where('team_members.created_by', '=', $user->id)
              ->get();

          if($joinedMemberIfExist){
              $data['button_show'] = 'false';
          } else {
              $data['button_show'] = 'true';
          }

          if($user->id == $teamMember->user_id)
          {
              $data['link_show'] = 'show';
          } else {
              $data['link_show'] = 'hide';
          }

      }

      $teammemberDonation = DB::table('donations')
          ->select(DB::raw('sum(donations.amount) as donation_amount'))
          ->join('team_members', 'donations.team_member_id', '=', 'team_members.id')
          ->where('team_members.id', '=' ,$teamMember->id)
          ->get();

      $teamMembers= DB::table('team_members')
          ->select('users.name', 'team_members.goal', 'team_members.id', DB::raw(
              'SUM(donations.amount) as amount,(SUM(donations.amount)/team_members.goal) * 100 as per_raised'))
          ->leftJoin('donations', 'team_members.id', '=', 'donations.team_member_id')
          ->join('users', 'team_members.user_id', '=', 'users.id')
          ->where('team_members.team_id', '=', $team_id)
          ->groupBy('users.name', 'team_members.goal', 'team_members.id')
          ->orderBy('per_raised')
          ->get();

      return view('campaign.teammember', compact('teamMember','teamMembers','teammemberDonation','data'));
      
  }


  public function joinTeam($teamId)
  {
      Log::info('CampaignController.joinTeam: ');
      $data['teamId'] = $teamId;
      $data['action'] = 'join';
      $data['heading'] = 'Join a Campaign Team';

      $teamInfo = DB::table('teams')
                  ->leftJoin('organizations', 'teams.organization_id', '=', 'organizations.id')
                  ->leftJoin('campaigns', 'teams.campaign_id', '=', 'campaigns.id')
                  ->select('teams.name as teamName', 'organizations.name as orgName', 'campaigns.name as campName')
                  ->where('teams.id', '=', $data['teamId'])
                  ->first();

      $data['teamInfo'] = $teamInfo;

      return view('campaign.jointeam', $data);
      
  }

  public function createTeam($campaignId)
  {
      Log::info('CampaignController.createTeam: ');
      $data['campaignId'] = $campaignId;
      $data['action'] = 'create';
      $data['heading'] = 'Create a Campaign Team';


      $campaignInfo = DB::table('campaigns')
                      ->select('campaigns.name as campName')
                      ->where('campaigns.id', '=', $data['campaignId'])
                      ->first();

      $data['campaignInfo'] = $campaignInfo;

      $organizationList = DB::table('organizations')
                          ->whereNull('organizations.deleted_at')
                          ->lists('name', 'id');

      $data['organizationList'] = $organizationList;

      return view('campaign.jointeam', $data);
      
  }

    public function team($id)
    {
        Log::info('CampaignController.team: ');
       // $team = Team::findOrFail($id);
        $team = Team::where('token','=',$id);

        $user = Auth::user();
        $data['link_show']='';
        $data['button_show']= 'true';

        if ($user) {
            $joinedMemberIfExist = DB::table('team_members')
                ->select('team_members.id')
                ->where('team_members.team_id', '=' ,$team->id)
                ->where('team_members.created_by', '=', $user->id)
                ->get();

            if($joinedMemberIfExist){
                $data['button_show'] = 'false';
            } else {
                $data['button_show'] = 'true';
            }

            if($user->id == $team->created_by)
            {
                $data['link_show'] = 'show';
            } else {
                $data['link_show'] = 'hide';
            }

        }

        $teamDonation = DB::table('donations')
            ->select(DB::raw('sum(donations.amount) as donation_amount'))
            ->join('teams', 'donations.team_id', '=', 'teams.id')
            ->where('teams.id', '=' ,$team->id)
            ->get();

        $teamMembers= DB::table('team_members')
            ->select('users.name', 'team_members.goal', 'team_members.id', DB::raw(
                'SUM(donations.amount) as amount,(SUM(donations.amount)/team_members.goal) * 100 as per_raised'))
            ->leftJoin('donations', 'team_members.id', '=', 'donations.team_member_id')
            ->join('users', 'team_members.created_by', '=', 'users.id')
            ->where('team_members.team_id', '=', $team->id)
            ->groupBy('users.name', 'team_members.goal', 'team_members.id')
            ->orderBy('per_raised')
            ->get();


        return view('campaign.team', compact('teamMembers','team','teamDonation','data'));

    }

  public function joinTeamStore(TeamMemberRequest $request)
  {
      Log::info('CampaignController.joinTeamStore - Start: ');
{'fraction_non_alphanumeric': 0.18178637039629078, 'fraction_numerical': 0.0010497769224039892, 'mean_word_length': 2.318432510885341, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1550008', 'n_tokens_mistral': 3605, 'n_tokens_neox': 3436, 'n_words': 524}