2018-12-07 05:19:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
use App\Classes\Frame as FrameClass;
|
|
|
|
use App\Classes\Frame\Ansi as AnsiFrame;
|
|
|
|
use App\Classes\Frame\Videotex as VideotexFrame;
|
|
|
|
|
|
|
|
use App\Classes\Server;
|
|
|
|
use App\Classes\Server\Ansi as AnsiServer;
|
|
|
|
use App\Classes\Server\Videotex as VideotexServer;
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
class Mode extends Model
|
|
|
|
{
|
2018-12-08 22:43:18 +00:00
|
|
|
public function frames()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Frame::class);
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:42:01 +00:00
|
|
|
public function frameId(int $id)
|
|
|
|
{
|
|
|
|
return $this->frameLoad(Frame::findOrFail($id));
|
|
|
|
}
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
/**
|
|
|
|
* Return a frame class for the Model
|
|
|
|
*
|
|
|
|
* @param Model $o
|
|
|
|
* @return FrameClass
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2019-07-12 03:42:01 +00:00
|
|
|
private function frameLoad(Model $o): FrameClass
|
2018-12-08 22:43:18 +00:00
|
|
|
{
|
|
|
|
switch (strtolower($this->name)) {
|
|
|
|
case 'ansi':
|
|
|
|
return new AnsiFrame($o);
|
|
|
|
case 'videotex':
|
|
|
|
return new VideotexFrame($o);
|
|
|
|
default:
|
2019-07-12 03:42:01 +00:00
|
|
|
throw new \Exception('Unknown Frame type: '.$this->name);
|
2018-12-08 22:43:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:42:01 +00:00
|
|
|
public function frameNew(Server $so,int $frame,string $index='a'): FrameClass
|
|
|
|
{
|
|
|
|
$o = new Frame;
|
|
|
|
$o->frame = $frame;
|
|
|
|
$o->index = $index;
|
|
|
|
$o->mode_id = $this->id;
|
|
|
|
|
|
|
|
// Make sure parent frame exists
|
|
|
|
// @todo make sure not trying to edit test frames
|
|
|
|
if ($o->index != 'a' AND ! FrameModel::where('frame',$fo->frame())->where('index',$fo->index_prev())->exists())
|
|
|
|
{
|
|
|
|
$so->sendBaseline($so->co,ERR_ROUTE);
|
|
|
|
return new Frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->frameLoad($o);
|
|
|
|
}
|
|
|
|
|
2018-12-08 22:43:18 +00:00
|
|
|
/**
|
|
|
|
* Fetch a specific frame from the DB
|
|
|
|
*
|
|
|
|
* @param int $frame
|
|
|
|
* @param string $index
|
2018-12-13 13:02:42 +00:00
|
|
|
* @param Server $so
|
2018-12-08 22:43:18 +00:00
|
|
|
* @return FrameClass
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2019-07-12 03:42:01 +00:00
|
|
|
public function framePage(int $frame,string $index='a',Server $so): FrameClass
|
2018-12-08 22:43:18 +00:00
|
|
|
{
|
2019-07-12 03:42:01 +00:00
|
|
|
return ($frame == '999' and $index == 'a')
|
2018-12-08 22:43:18 +00:00
|
|
|
// Return our internal test frame.
|
2019-07-12 03:42:01 +00:00
|
|
|
? $this->frameTest($so)
|
|
|
|
: $this->frameLoad($this->frames()
|
|
|
|
->where('frame','=',$frame)
|
|
|
|
->where('index','=',$index)
|
|
|
|
->firstOrFail()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function frameTest(Server $so): FrameClass
|
|
|
|
{
|
|
|
|
switch (strtolower($this->name)) {
|
|
|
|
case 'ansi':
|
|
|
|
return AnsiFrame::testFrame($so);
|
|
|
|
case 'videotex':
|
|
|
|
return VideotexFrame::testFrame($so);
|
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown Frame type: '.$this->name);
|
|
|
|
}
|
2018-12-08 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 05:19:51 +00:00
|
|
|
/**
|
|
|
|
* Return our server instance
|
|
|
|
*/
|
|
|
|
public function server()
|
|
|
|
{
|
2018-12-08 22:43:18 +00:00
|
|
|
switch (strtolower($this->name)) {
|
|
|
|
case 'ansi': return new AnsiServer($this);
|
|
|
|
case 'videotex': return new VideotexServer($this);
|
2018-12-07 05:19:51 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown server type: '.$this->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|