Monthly Archives: December 2020

Convert Decimal Hour to Carbon Object

use Carbon\Carbon;

if(!function_exists('convert_decimal_hours')){
	function convert_decimal_time($seconds, $timezone='Asia/Taipei'){
		$today = Carbon::now($timezone)->toDateString();
		$today = Carbon::parse($today, $timezone);
        $today->second($seconds);

		return $today;
	}
}

Click to upload (Hide upload form)

HTML

<button type="button" id="change_id_copy">更新正面</button>
<button type="button" id="change_id_copy2">更新反面</button>


<form id="form_id_front" name="form_id_front" novalidate action="/admin/ajax/member/id_copy/front" method="post" style="height: 0px; overflow: hidden;">
	<input type="file" name="file" id="id_copy">
	<input type="hidden" name="member_id" value="">
</form>

<form id="form_id_back" name="form_id_back" novalidate action="/admin/ajax/member/id_copy/back" method="post" style="height: 0px; overflow: hidden;">
	<input type="file" name="file" id="id_copy">
	<input type="hidden" name="member_id" value="">
</form>

Javascript

var id_copy_app = function(){
    var $c = null;
    var $form_front = null;
    var $form_back = null;
    var member_id = 0;

    function init(){
        $c = $('#md_member');
        $form_front = $c.find('#form_id_front');
        $form_back = $c.find('#form_id_back');

        $form_front.ajaxForm({
            dataType: 'json',
            beforeSubmit: function(arr, $form, options) {
            },
            success: function(data, statusText, xhr, $form){
              //console.log(data);
              if(typeof data.status == 'undefined'){
                    alert(data);
              }else if(data.status == 'fail'){
                    alert(data.message);
              }else{
                    //console.log(data);
                    alert('修改成功');
                    var $link = $c.find('.id_img.id_copy');
                    $link.attr('href', data.data);
                    $link.find('img').attr('src', data.data);
              }
            },
            error: function(data){
                console.log(data);
            }
        });

        $form_back.ajaxForm({
            dataType: 'json',
            beforeSubmit: function(arr, $form, options) {
            },
            success: function(data, statusText, xhr, $form){
              //console.log(data);
              if(typeof data.status == 'undefined'){
                    alert(data);
              }else if(data.status == 'fail'){
                    alert(data.message);
              }else{
                    //console.log(data);
                    alert('修改成功');
                    var $link = $c.find('.id_img.id_copy2');
                    $link.attr('href', data.data);
                    $link.find('img').attr('src', data.data);
              }
            },
            error: function(data){
                console.log(data);
            }
        });

        $c.find('#change_id_copy').on('click', function(){
            $form_front.find('#id_copy').trigger('click');
        });

        $form_front.find('#id_copy').on('change', function(){
            //alert('ok');
            $form_front.submit();
        });

        $c.find('#change_id_copy2').on('click', function(){
            $form_back.find('#id_copy').trigger('click');
        });

        $form_back.find('#id_copy').on('change', function(){
            //alert('ok');
            $form_back.submit();
        });
    }

    function set_id(_id){
        //$form.find('[type="text"]').val('');
        member_id = _id;
        $c.find('form [name="member_id"]').val(member_id);
    }

    function show_response_error(errors){
        var str = '';
        //console.log(errors);

        for(var key in errors){
            var err = errors[key];
            str += err[0] + '\n';
        }
        
        alert(str);
    }

    return {
        init: init, 
        set_id: set_id
    }
}();

Laravel

public function update_id_copy_front(Request $req){
	$this->check_login();

	$member_id = $req->input('member_id');
	if($member_id == false){
		$this->fail_response(0, '錯誤的會員代碼');
	}

	$file = $req->file('file');
	if($file == false){
		$this->fail_response(0, '請選擇上傳檔案');
	}
	//logg($file);
	$path = $file->store('public/' . $member_id . '/id_copy');
	$path = str_replace('public', '', $path);
	//logg($path);

	$member = MemberM::find($member_id);
	$member->id_copy = $path;
	$member->save();

	$this->success_response(img_url($path));
}

FullCalendar

Official: https://fullcalendar.io/

Business Hours

https://fullcalendar.io/docs/business-hours

Modal HTML Example

<div class="modal" tabindex="-1" role="dialog" id="md_calendar">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Open Hours</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div id='calendar'></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Javascript App

var calendar_app = function(){
    var $md = null;
    var $c = null;
    var calendar = null;
    var id = 0;

    function init(id){
        $md = $('#md_calendar');
        $c = $md.find('#calendar');

        $('.open_hours').on('click', function(e){
            e.preventDefault();
            var $this = $(this);
            var id = $this.data('id');
            show(id);
        });
    }

    function show(_id){
        id = _id;
        $md.modal();
        //clear();
        refresh();
    }

    function refresh(){
        calendar = new FullCalendar.Calendar($c[0], {
            initialView: 'timeGridWeek',
            views: {
                timeGrid: {
                    allDaySlot: false,
                    slotDuration: '01:00:00',
                    slotLabelFormat: {
                        hour: '2-digit'
                    },
                    slotMinTime: '09:00:00',
                    slotMaxTime: '22:00:00'
                }
            },
            events: '/open_hours?id=' + id
        });

        calendar.render();
    }

    return {
        init: init, 
        show: show
    }
}();

PHP Example (Laravel)

    public function open_hours(Request $req){
        $bid = $req->input('id');
        $branch = BranchM::find($bid);
        $json = json_decode($branch->hours);
        $hours = $json->selection;
        if($hours == false){
            $data = false;
        }else{
            $data = [];
            foreach ($hours as $key => $item) {
                //logg($item, false);
                foreach($item->hours as $key2 => $hour){
                    $event = [];
                    $event['id'] = $key . '_' . $key2;
                    $event['title'] = 'Open';
                    $event['start'] = '2020-12-01';
                    $event['daysOfWeek'] = $item->days;
                    $event['startTime'] = $hour->from_hour . ':00';
                    $event['endTime'] = $hour->to_hour . ':00';
                    $event['startRecur'] = '2020-12-01';

                    $data[] = $event;
                }
            }
        }

        return response()->json($data);
    }

Quick Links: