82 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			82 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Models;
 | |
| 
 | |
| use Illuminate\Database\Eloquent\Model;
 | |
| 
 | |
| class Room extends Model
 | |
| {
 | |
| 
 | |
|     protected $table = 'rooms';
 | |
|     protected $primaryKey = 'room_id';
 | |
|     public $timestamps = false;
 | |
|     protected $fillable = [
 | |
|         'room_name',
 | |
|         'room_rows',
 | |
|         'room_columns',
 | |
|         'user_id', // who added the room?
 | |
|         'cinema_id',
 | |
|     ];
 | |
| 
 | |
|     protected $hidden = [
 | |
|         'created_at',
 | |
|         'updated_at',
 | |
|     ];
 | |
| 
 | |
|     public static function find(int $room_id)
 | |
|     {
 | |
|         return self::where('room_id', $room_id)->first();
 | |
|     }
 | |
| 
 | |
|     public function showings()
 | |
|     {
 | |
|         return $this->hasMany(Showing::class, 'room_id', 'room_id');
 | |
|     }
 | |
| 
 | |
|     public function seats()
 | |
|     {
 | |
|         return $this->hasMany(Seat::class, 'room_id', 'room_id');
 | |
|     }
 | |
| 
 | |
|     public function cinema()
 | |
|     {
 | |
|         return $this->belongsTo(Cinema::class, 'cinema_id', 'cinema_id');
 | |
|     }
 | |
| 
 | |
|     public function seatMatrix($showing_id = null)
 | |
|     {
 | |
|         $seats = $this->seats;
 | |
|         $matrix = [];
 | |
|         // then, fill it with the seats
 | |
|         foreach ($seats as $seat) {
 | |
|             if (!isset($matrix[$seat->seat_row - 1])) $matrix[$seat->seat_row - 1] = [];
 | |
|             $matrix[$seat->seat_row-1][$seat->seat_column-1] = [
 | |
|                 'seat_id' => $seat->seat_id,
 | |
|                 'seat_row' => $seat->seat_row,
 | |
|                 'seat_column' => $seat->seat_column,
 | |
|                 'seat_type' => $seat->seat_type,
 | |
|                 'seat_linked_id' => $seat->seat_linked_id,
 | |
|                 'reserved' => $showing_id ? $seat->isReserved($showing_id) : false,
 | |
|             ];
 | |
|         }
 | |
|         return fix_matrix($matrix, $this->room_columns-1, $this->room_rows-1);
 | |
|     }
 | |
| 
 | |
| }
 | |
| 
 | |
| function fix_matrix($m, $h, $w) {
 | |
|     $_nm = [];
 | |
|     // write the matrix into _nm and fill the empty spaces with null
 | |
|     for ($i = 0; $i <= $h; $i++) {
 | |
|         $_nm[$i] = [];
 | |
|         for ($j = 0; $j <= $w; $j++) {
 | |
|             if (isset($m[$i][$j])) {
 | |
|                 $_nm[$i][$j] = $m[$i][$j];
 | |
|             } else {
 | |
|                 $_nm[$i][$j] = null;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return $_nm;
 | |
| }
 |