M1n1 Shira0ka File Manager

Current Path : /home/bullgymbny/app2/app/Http/Controllers/
Upload File :
Current File : /home/bullgymbny/app2/app/Http/Controllers/MemberController.php

<?php

namespace App\Http\Controllers;

use App\Notification;
use App\Point;
use Illuminate\Support\Facades\DB;
use App\Subscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use App\Member;
use Illuminate\Support\Facades\Redirect;

define("MINUTE", "60");
define("HEURE", "3600");
define("JOUR", "86400");
define("SEMAINE", "604800");
define("MOIS", "2629800");
define("ANNEE", "31557600");
define('FIRST_YEAR', "2017");



function correctImageOrientation($filename) {
    if (function_exists('exif_read_data')) {
        $exif = exif_read_data($filename);
        if($exif && isset($exif['Orientation'])) {
            $orientation = $exif['Orientation'];
            if($orientation != 1){
                $img = imagecreatefromjpeg($filename);
                $deg = 0;
                switch ($orientation) {
                    case 3:
                        $deg = 180;
                        break;
                    case 6:
                        $deg = 270;
                        break;
                    case 8:
                        $deg = 90;
                        break;
                }
                if ($deg) {
                    $img = imagerotate($img, $deg, 0);
                }
                // then rewrite the rotated image back to the disk as $filename
                imagejpeg($img, $filename, 95);
            } // if there is some rotation necessary
        } // if have the exif orientation info
    } // if function exists
}

class MemberController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        // libérer les codes des membres qui n'on pas souscrit d'abonnement depuis 1 an
        $sql = "
                UPDATE app2_members SET code = '0000' WHERE code <> '0000' AND id NOT IN (SELECT DISTINCT member_id FROM app2_subscriptions WHERE date_end <> '--' AND date_end <> '0000-00-00' AND date_end > DATE_SUB(NOW(), INTERVAL 1 YEAR) ORDER BY member_id) AND auto_inscription IS FALSE;
            ";
        DB::update(DB::raw($sql));
        $members = Member::orderBy('id', 'desc')->limit(20)->get();
        $start = new \DateTime();
        $start->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
        //dd($start->format('Y-m-d'));
        //$member_abo = Subscription::select('member_id')->distinct()->where('date_start', '<', $start->format('Y-m-d'))->where('date_end', '>', $start->format('Y-m-d'))->get();
        $sql = "
                SELECT count(*) as count FROM (
                    SELECT
                           m.id,
                           lastname,
                           firstname,
                           code,
                           email,
                           tel,
                           gym,
                           crossfit,
                           1 as subscription_state,
                           max(s.date_end) as last_date_subscription_end,
                           max(s.date_start) as last_date_subscription_start,
                           max(s.cost) as last_date_subscription_cost,
                           max(s.credit) as last_date_subscription_credit
                    FROM app2_members as m INNER JOIN app2_subscriptions s on m.id = s.member_id AND s.date_end > CURDATE() and date_start < CURDATE()
                    GROUP BY m.id, lastname, firstname, code, gym, crossfit, 1) as m2
            ";
        $result = DB::select(DB::raw($sql));
        $nb_member_with_abo = $result[0]->count;
        //$nb_member_with_abo = count($member_abo);
        return view('members',compact('members', 'nb_member_with_abo'));
    }

    public function create(){
        return view('members_create');
    }

    public function create_subscription($member){
        $member = Member::findOrFail($member);
        return view('subscriptions_create', compact('member'));
    }

    public function list_subscription($member){
        $member = Member::findOrFail($member);
        $subscribes = Subscription::where('member_id', $member->id)->orderBy('date_end', 'DESC')->get();
        return view('subscriptions_list', compact('subscribes','member'));
    }

    public function edit($id){
        $member =  Member::findOrFail($id);
        return view('members_edit', compact('member'));
    }

    public function update($id, Request $request){

        $member = Member::findOrFail($id);
        $data = $request->all();
        if ($request->file('photo')) {
            $path = $request->file('photo')->store('files');
            correctImageOrientation(storage_path('app/') . $path);
            $data['photo'] = $path;
        }
        if (!isset($data['gym'])){
            $data['gym'] = '0';
        }
        if (!isset($data['crossfit'])){
            $data['crossfit'] = '0';
        }
        $data['lastname'] = mb_strtoupper($data['lastname']);
        $data['firstname'] = ucwords($data['firstname']);

        $member->update($data);
        Session::flash('mess.ok', "Membre mis à jour");
        return redirect(route('member.index'));
    }

    public function store( Request $request){
        $code = "0000";
        $data = $request->all();
        //dd($data);
        if (!isset($data['gym'])){
            $data['gym'] = '0';
        }
        if (!isset($data['crossfit'])){
            $data['crossfit'] = '0';
        }
        if ($request->file('photo')) {
            $path = $request->file('photo')->store('files');
            correctImageOrientation(storage_path('app/') . $path);
            $data['photo'] = $path;
        }
        $data['lastname'] = mb_strtoupper($data['lastname']);
        $data['firstname'] = ucwords($data['firstname']);
        $data['country'] = mb_strtoupper($data['country']);
        $data['code'] = $code;
        $member = Member::create($data);
        Session::flash('mess.ok', "Membre ajouté");
        return redirect(route('add_subscription', ['member' => $member->id]));
    }

    public function destroy($id)
    {
        $member = Member::findOrFail($id);
        Point::where('member_id', $id)->delete();
        Subscription::where('member_id', $id)->delete();
        Member::where('id', $id)->delete();
        Session::flash('mess.ok', "Membre supprimé");
        return redirect(route('member.index'));
    }

    public function destroy_subscription($id)
    {
        $sub = Subscription::findOrFail($id);
        $member = Member::findOrFail($sub->member_id);

        Subscription::where('id', $id)->delete();
        Session::flash('mess.ok', "Abonnement supprimé");
        return redirect(route('list_subscription', $member));
    }

    public function edit_subscription($id)
    {
        $sub = Subscription::findOrFail($id);
        $member = Member::findOrFail($sub->member_id);


        return view('subscriptions_edit', compact('member', 'sub'));
    }

    public function edit_store_subscription($id, Request $request){
        $data = $request->all();
        $sub = Subscription::findOrFail($id);
        $member = Member::findOrFail($sub->member_id);
        $data['date_start'] = substr($data['date_start'], -4) . '-' . substr($data['date_start'], 3, 2) . '-' . substr($data['date_start'], 0,2);
        $data['date_end'] = substr($data['date_end'], -4) . '-' . substr($data['date_end'], 3, 2) . '-' . substr($data['date_end'], 0,2);

        $sub->update($data);
        Session::flash('mess.ok', "Abonnement Modifié");
        return redirect(route('list_subscription', $member));
    }

    public function store_subscription( Request $request){

        $data = $request->all();
        $dateFRStart = $data['date_start'];
        $dateFREnd = $data['date_end'];
        $data['date_start'] = substr($data['date_start'], -4) . '-' . substr($data['date_start'], 3, 2) . '-' . substr($data['date_start'], 0,2);
        $data['date_end'] = substr($data['date_end'], -4) . '-' . substr($data['date_end'], 3, 2) . '-' . substr($data['date_end'], 0,2);
        $member = Member::findOrFail($data['member_id']);
        $member->update(['auto_inscription' => 0]);
        if ($member->code == "0000") {
            $member->update(['code' => Member::generate_code()]);
        }
        Subscription::create($data);
        Session::flash('mess.ok', "Abonnement du " . $dateFRStart . " au " . $dateFREnd . " ajouté pour " . $member->firstname . " " . $member->lastname . " - code : <span style='font-size: 2em; font-family: Courier; font-weight: bold'>[ " . $member->code . " ]</span>");
        return redirect(route('member.index'));
    }

    public function avatar($id)
    {
        $user = Member::findOrFail($id);

        $path = storage_path('app/') . $user->photo;
        //echo $user;
        if(!File::exists($path))
            return "";

        $file = File::get($path);

        $type = File::mimeType($path);

        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);

        return $response;
    }


    public function get_notif()
    {
        $notifications = Notification::where('read', false)->orderBy('datelog', 'DESC')->get();
        $return = [];
        foreach($notifications as $notif){
            $notif->update(['read' => true]);
            $photo = "";
            $member = "";
            if(!empty($notif->member) and !empty($notif->member->photo)){
                $photo = route('avatar', $notif->member);
                $member = '<strong>' .$notif->member->lastname . ' ' . $notif->member->firstname . '</strong><br/>';
            }
            $date = new \DateTime($notif->datelog);
            $date->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
            $return[] = [
                'datelog' => $member . ' à ' . $date->format('H:i'),
                'log' => $notif->log,
                'gravity' => $notif->gravity,
                'member_photo' => $photo
            ];
        }
        return json_encode($return);
    }

    public function search(Request $request) {
        $data = $request->all();
        $credit = "";
        if ($data['credit'] == "1") {
            $credit = " AND last_date_subscription_credit > 0";
        }
        if ($data['isAbo'] == "2") {
            $sql = "
                SELECT * FROM (
                    SELECT
                           m.id,
                           lastname,
                           firstname,
                           code,
                           email,
                           country,
                           tel,
                           gym,
                           crossfit,
                           1 as subscription_state,
                           max(STR_TO_DATE(s.date_end, '%Y-%m-%d')) as last_date_subscription_end,
                           max(STR_TO_DATE(s.date_start, '%Y-%m-%d')) as last_date_subscription_start,
                           max(s.cost) as last_date_subscription_cost,
                           max(s.credit) as last_date_subscription_credit
                    FROM app2_members as m INNER JOIN app2_subscriptions s on m.id = s.member_id AND STR_TO_DATE(s.date_end, '%Y-%m-%d') >= CURDATE() and STR_TO_DATE(date_start, '%Y-%m-%d') <= CURDATE()
                    GROUP BY m.id, lastname, firstname, code, gym, crossfit, 1
                    UNION

                    SELECT * FROM (
                        SELECT
                            m.id,
                            lastname,
                            firstname,
                            code,
                            email,
                            country,
                            tel,
                            gym,
                            crossfit,
                            0 as subscription_state,
                            max(STR_TO_DATE(s.date_end, '%Y-%m-%d')) as last_date_subscription_end,
                            max(STR_TO_DATE(s.date_start, '%Y-%m-%d')) as last_date_subscription_start,
                            max(s.cost) as last_date_subscription_cost,
                            max(s.credit) as last_date_subscription_credit
                        FROM app2_members as m LEFT JOIN app2_subscriptions s on m.id = s.member_id AND STR_TO_DATE(s.date_end, '%Y-%m-%d') <= CURDATE()
                        GROUP BY m.id, lastname, firstname, code, gym, crossfit, 1) as m2
                    WHERE m2.id not in (SELECT DISTINCT m.id
                        FROM app2_members as m INNER JOIN app2_subscriptions s on m.id = s.member_id AND STR_TO_DATE(s.date_end, '%Y-%m-%d') >= CURDATE() and STR_TO_DATE(date_start, '%Y-%m-%d') <= CURDATE())) as m3
                WHERE (lastname like '%". $data['name'] ."%' OR firstname like '%". $data['name'] ."%' OR email like '%". $data['name'] ."%' OR code like '%". $data['name'] ."%' OR tel like '%". $data['name'] ."%' )
            " . $credit . " ORDER BY lastname, firstname";
        }
        if ($data['isAbo'] == "1") {
            $sql = "
                SELECT * FROM (
                    SELECT
                           m.id,
                           lastname,
                           firstname,
                           code,
                           email,
                           country,
                           tel,
                           gym,
                           crossfit,
                           1 as subscription_state,
                           max(STR_TO_DATE(s.date_end, '%Y-%m-%d')) as last_date_subscription_end,
                           max(STR_TO_DATE(s.date_start, '%Y-%m-%d')) as last_date_subscription_start,
                           max(s.cost) as last_date_subscription_cost,
                           max(s.credit) as last_date_subscription_credit
                    FROM app2_members as m INNER JOIN app2_subscriptions s on m.id = s.member_id AND STR_TO_DATE(s.date_end, '%Y-%m-%d') >= CURDATE() and STR_TO_DATE(date_start, '%Y-%m-%d') <= CURDATE()
                    GROUP BY m.id, lastname, firstname, code, gym, crossfit, 1) as m2
                WHERE (lastname like '%". $data['name'] ."%' OR firstname like '%". $data['name'] ."%' OR email like '%". $data['name'] ."%' OR code like '%". $data['name'] ."%' OR tel like '%". $data['name'] ."%')
            " . $credit . " ORDER BY lastname, firstname";

        }
        if ($data['isAbo'] == "0") {
            $sql = "
                SELECT * FROM (
                    SELECT
                        m.id,
                        lastname,
                        firstname,
                        code,
                        email,
                        country,
                        tel,
                        gym,
                        crossfit,
                        0 as subscription_state,
                        max(STR_TO_DATE(s.date_end, '%Y-%m-%d')) as last_date_subscription_end,
                        max(STR_TO_DATE(s.date_start, '%Y-%m-%d')) as last_date_subscription_start,
                        max(s.cost) as last_date_subscription_cost,
                        max(s.credit) as last_date_subscription_credit
                    FROM app2_members as m LEFT JOIN app2_subscriptions s on m.id = s.member_id AND STR_TO_DATE(s.date_end, '%Y-%m-%d') <= CURDATE()
                    GROUP BY m.id, lastname, firstname, code, gym, crossfit, 1) as m2
                WHERE m2.id not in (SELECT DISTINCT m.id
                    FROM app2_members as m INNER JOIN app2_subscriptions s on m.id = s.member_id AND STR_TO_DATE(s.date_end, '%Y-%m-%d') >= CURDATE() and STR_TO_DATE(s.date_start, '%Y-%m-%d') <= CURDATE())
                    AND (lastname like '%". $data['name'] ."%' OR firstname like '%". $data['name'] ."%' OR email like '%". $data['name'] ."%' OR code like '%". $data['name'] ."%' OR tel like '%". $data['name'] ."%')
            " . $credit . " ORDER BY lastname, firstname";
        }

        $result = DB::select(DB::raw($sql));
        return json_encode($result);
    }
    public function get_last_notif()
    {
        $notifications = Notification::orderBy('datelog', 'DESC')->limit(200)->get();
        return view('notifications', compact('notifications'));
    }

    public function set_interface() {
        $cookie = cookie()->forever('interface', 'jfdhfdqlmlmqokdhfozqojofdogfdoijoqofd');
        return Redirect::to('home')->withCookie($cookie);
        //return \response('Interface ajouté')->cookie($cookie);
    }

    public function get_number_member() {
        $time_moins_2 = date('Y-m-d H:i:s',strtotime('-2 hour',time()));
        $points = Point::where('time', '>', $time_moins_2)->get();
        return count($points);
    }

    public function list_event(Request $request, $member_id){
        $user = Member::findOrFail($member_id);
        // rechercher les pointages pour member_id et start / end
        $dates = $request->all();
        $start = date('Y-m-d', strtotime($dates['start']));
        //dd($dates['start']);
        $start = new \DateTime($start);
        $start->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
        //dd($start);
        $end = date('Y-m-d', strtotime($dates['end']));
        $end = new \DateTime($end);
        $end->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
        $points = Point::where('member_id', $member_id)->where('time', '>=', $start->format('Y-m-d').' 00:00:00')->where('time', '<', $end->format('Y-m-d').' 00:00:00')->orderBy('time')->get();
        $start2 = $start->format('Y-m-d');
        $end2 = $end->format('Y-m-d');
        $abos = DB::select( DB::raw("
            SELECT * FROM app2_subscriptions WHERE member_id = $member_id AND (
                (date_start >= '$start2' AND date_end <= '$end2') OR
                (date_start < '$start2' AND date_end > '$start2') OR
                (date_start < '$end2' AND date_end > '$end2') OR
                (date_start < '$start2' AND date_end > '$end2')
            )
        "));
        $response = [];
        foreach($abos as $abo) {
            $start = new \DateTime($abo->date_start);
            //$start->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
            $end = new \DateTime($abo->date_end);
            //$end->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
            $response[] = [
                'title' => 'Abonnement',
                'start' => $start->format('c'),
                'end' => $end->format('c'),

            ];
        }


        foreach($points as $point){
            $date = new \DateTime($point->time);
            $date->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
            $response[] = [
                'title' => '',
                'start' => $date->format('c'),
            ];
        }
        return $response;
    }
    public function freq(){
        return view('freq');
    }
    public function list_event_all(Request $request){

        // rechercher les pointages pour member_id et start / end
        $dates = $request->all();
        $start = date('Y-m-d', strtotime($dates['start']));
        //dd($dates['start']);
        $start = new \DateTime($start);
        $start = $start->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
        //dd($start);
        $end = date('Y-m-d', strtotime($dates['end']));
        $end = new \DateTime($end);
        $end = $end->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
        $points = Point::where('time', '>=', $start->format('Y-m-d').' 00:00:00')->where('time', '<', $end->format('Y-m-d').' 00:00:00')->orderBy('time')->get();
        $response = [];
        $nb_point_of_day = 0;
        if (count($points) > 0){
            $date_count = new \DateTime($points[0]->time);
            $date_count->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
            $day = $date_count->format('Y-m-d');
        }
        $k = 1;
        foreach($points as $point){
            $k++;
            $date = new \DateTime($point->time);
            $date->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
            //dd($date->format('Y-m-d'));
            if ($day == $date->format('Y-m-d')) {
                $nb_point_of_day++;
            }else{
                $date_count = new \DateTime($day);
                $response[] = [
                    'title' => $nb_point_of_day. ' pointages',
                    'start' => $date_count->format('c'),
                    'allDay' => true
                ];
                $day = $date->format('Y-m-d');
                $nb_point_of_day = 1;
            }
            $member = Member::findOrFail($point->member_id);
            $title = '';
            if ($member) {
                $title = $member->firstname. ' ' . $member->lastname;
            }
            $response[] = [
                'title' => $title,
                'start' => $date->format('c'),
            ];
        }
        if (count($points) > 0) {
            $response[] = [
                'title' => $nb_point_of_day. ' pointages',
                'start' => $date->format('c'),
                'allDay' => true
            ];
        }
        return $response;
    }

}

GO Party By You