Internal rework pending editframe

This commit is contained in:
Deon George
2019-07-12 10:42:01 +07:00
parent 4f79a1a997
commit 264747e2f3
14 changed files with 611 additions and 428 deletions

View File

@@ -2,11 +2,12 @@
namespace App\Classes;
use App\Models\Mode;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use App\User;
use App\Models\CUG;
use App\Models\{CUG,Mode};
use App\Models\Frame as FrameModel;
/**
* Handles all aspects of frame
@@ -34,12 +35,14 @@ use App\Models\CUG;
*/
abstract class Frame
{
protected $frame = NULL;
protected $output = '';
protected $startline = 1;
// This holds the parser object for this frame.
protected $po = NULL;
// This holds the frame object as retrieved from the DB
protected $fo = NULL;
// All this vars should be overridden in the child class
/*
// All this vars should be overridden in the child class
protected $frame_length = 22;
protected $frame_width = 40;
@@ -54,9 +57,8 @@ abstract class Frame
const FRAMETYPE_LOGIN = 'l';
const FRAMETYPE_TERMINATE = 't';
public $fields = NULL; // The fields in this frame.
// Fields that are editable
// @todo This needs rework.
private $fieldoptions = [
'p'=>['edit'=>TRUE,'mask'=>'*'], // Password
't'=>['edit'=>TRUE], // Text
@@ -65,38 +67,44 @@ abstract class Frame
// @todo Move this to the database
private $header = RED.'T'.BLUE.'E'.GREEN.'S'.YELLOW.'T'.MAGENTA.'!';
public function __construct(\App\Models\Frame $o)
public function __construct(FrameModel $o)
{
$this->frame = $o;
$this->fo = $o;
$startline = 1;
$this->output = $this->frame->cls ? CLS : HOME;
if ($this->fo->exists) {
if (! $this->hasFlag('ip') AND (! $this->isCUG(0) OR $this->type() !== self::FRAMETYPE_LOGIN)) {
$startline = 2;
if (! $this->hasFlag('ip') AND (! $this->isCUG(0) OR $this->type() !== self::FRAMETYPE_LOGIN)) {
// Set the page header: CUG/Site Name | Page # | Cost
$this->output .= $this->render_header($this->header).
$this->render_page($this->frame->frame,$this->frame->index).
$this->render_cost($this->frame->cost);
$this->startline = 2;
} elseif ($this->isCUG(0) AND $this->type() === self::FRAMETYPE_LOGIN) {
$this->startline = 2;
$this->output .= str_repeat(DOWN,$this->startline-1);
} elseif ($this->isCUG(0) AND $this->type() === self::FRAMETYPE_LOGIN) {
$startline = 2;
}
}
// Calculate fields and render output.
$this->fields = collect(); // Fields in this frame.
$this->fields($this->startline);
// Our parser object
$this->po = $this->parser($startline);
}
/**
* Render the frame
*
* @return null|string
* @throws \Exception
*/
public function __toString()
{
return $this->output;
$output = $this->fo->cls ? CLS : HOME;
if (! $this->hasFlag('ip') AND (! $this->isCUG(0) OR $this->type() !== self::FRAMETYPE_LOGIN)) {
$output .= $this->render_header($this->header).
$this->render_page($this->fo->frame,$this->fo->index).
$this->render_cost($this->fo->cost);
} elseif ($this->isCUG(0) AND $this->type() === self::FRAMETYPE_LOGIN) {
$output .= str_repeat(DOWN,1);
}
return $output.(string)$this->po;
}
/**
@@ -106,9 +114,9 @@ abstract class Frame
*/
public function alts(Mode $o)
{
return \App\Models\Frame::where('frame',$this->frame())
return FrameModel::where('frame',$this->fo->frame)
->where('index',$this->index())
->where('id','<>',$this->frame->id)
->where('id','<>',$this->fo->id)
->where('mode_id',$o->id)
->where('access',1)
->limit(9);
@@ -119,23 +127,33 @@ abstract class Frame
*/
public function created()
{
return $this->frame->created_at;
return $this->fo->created_at;
}
/**
* Convert the frame from Binary to Output
* Look for fields within the frame.
*
* @param int $startline
* Return fields within the frame.
*/
abstract public function fields($startline=0);
public function fields()
{
return $this->po->fields;
}
/**
* Returns the current frame.
*/
public function frame()
{
return $this->frame->frame;
return $this->fo->frame;
}
public function frame_length()
{
return static::$frame_length;
}
public function frame_width()
{
return static::$frame_width;
}
/**
@@ -149,7 +167,7 @@ abstract class Frame
public function getCUG()
{
$co = NULL;
$frame = $this->frame->frame;
$frame = $this->fo->frame;
while (! $co)
{
@@ -171,7 +189,7 @@ abstract class Frame
*/
public function getField(int $id)
{
return $this->fields->get($id);
return $this->fields()->get($id);
}
/**
@@ -183,7 +201,7 @@ abstract class Frame
*/
public function getFieldId($type='edit',$after=0)
{
return $this->fields
return $this->fields()
->search(function($item,$key) use ($type,$after) {
return $key >= $after AND $this->isFieldEditable($item->type);
});
@@ -204,7 +222,7 @@ abstract class Frame
*/
public function hasFlag($flag)
{
return $this->frame->hasFlag($flag);
return $this->fo->hasFlag($flag);
}
/**
@@ -214,7 +232,7 @@ abstract class Frame
*/
public function id()
{
return $this->frame->id;
return $this->fo->id;
}
/**
@@ -224,7 +242,7 @@ abstract class Frame
*/
public function index()
{
return $this->frame->index;
return $this->fo->index;
}
/**
@@ -232,16 +250,25 @@ abstract class Frame
*/
public function index_next()
{
return chr(ord($this->frame->index)+1);
return chr(ord($this->fo->index)+1);
}
/**
* Return the previous index
*/
public function index_prev()
{
return $this->fo->index == 'a' ? 'a' : chr(ord($this->fo->index)-1);
}
public function isAccessible():bool
{
return $this->frame->access ? TRUE : FALSE;
return $this->fo->access ? TRUE : FALSE;
}
/**
* Determine if the frame is a particular CUG
*
* @param int $cug
* @return bool
*/
@@ -273,10 +300,10 @@ abstract class Frame
*/
public function isFramePublic(): bool
{
return $this->frame->public ? TRUE : FALSE;
return $this->fo->public ? TRUE : FALSE;
}
// @todo To implement
// @todo To implement
public function isOwner(User $o):bool
{
return FALSE;
@@ -287,7 +314,7 @@ abstract class Frame
*/
public function page(bool $as_array=FALSE)
{
return $as_array ? ['frame'=>$this->frame->frame,'index'=>$this->frame->index] : $this->frame->page;
return $as_array ? ['frame'=>$this->fo->frame,'index'=>$this->fo->index] : $this->fo->page;
}
/**
@@ -296,11 +323,19 @@ abstract class Frame
* @param bool $as_array
* @return mixed
*/
public function pagenext(bool $as_array=FALSE)
public function page_next(bool $as_array=FALSE)
{
return $as_array ? ['frame'=>$this->frame->frame,'index'=>$this->index_next()] : $this->frame->frame.$this->index_next();
return $as_array ? ['frame'=>$this->fo->frame,'index'=>$this->index_next()] : $this->fo->frame.$this->index_next();
}
/**
* Load the parser
*
* @param int $startline
* @return Parser
*/
abstract protected function parser(int $startline): Parser;
/**
* Render the cost of the frame
*
@@ -359,6 +394,8 @@ abstract class Frame
* Get the route for the key press
*
* @param string $read
* @return string
* @throws \Exception
*/
public function route(string $read)
{
@@ -366,11 +403,11 @@ abstract class Frame
throw new \Exception('Routes are single digit');
// If we dont have a route record...
if (! $this->frame->route)
if (! $this->fo->route)
return '*';
$key = 'r'.$read;
return $this->frame->route->{$key};
return $this->fo->route->{$key};
}
/**
@@ -381,12 +418,12 @@ abstract class Frame
* @param $text
* @return int
*/
abstract function strlenv($text):int;
abstract public static function strlenv($text):int;
public static function testFrame(Server $so)
public static function testFrame()
{
// Simulate a DB load
$o = new \App\Models\Frame;
$o = new FrameModel;
$content = '';
$o->flags = ['ip'];
@@ -399,7 +436,7 @@ abstract class Frame
// Header
$sid = R_RED.'T'.R_BLUE.'E'.R_GREEN.'S'.R_YELLOW.'T';
$content .= substr($sid.'-'.str_repeat('12345678901234567890',4),0,static::$header_length+(strlen($sid)-$so->strlenv($sid))).
$content .= substr($sid.'-'.str_repeat('12345678901234567890',4),0,static::$header_length+(strlen($sid)-static::strlenv($sid))).
R_WHITE.str_repeat('9',static::$pagenum_length).'a'.R_RED.sprintf('%07.0f',999).'u';
$content .= R_WHITE.str_repeat('+-',static::$frame_width/2-3).' '.R_RED.'01';
@@ -410,7 +447,7 @@ abstract class Frame
$o->content = $content;
return $o;
return new static($o);
}
/**
@@ -418,6 +455,6 @@ abstract class Frame
*/
public function type()
{
return $this->frame->type();
return $this->fo->type();
}
}