Moved registration processing into Frame/Action

This commit is contained in:
Deon George
2019-07-14 22:43:31 +10:00
parent 264747e2f3
commit aa6b2f3244
9 changed files with 316 additions and 217 deletions

View File

@@ -2,8 +2,7 @@
namespace App\Classes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Arr;
use App\User;
use App\Models\{CUG,Mode};
@@ -41,6 +40,12 @@ abstract class Frame
// This holds the frame object as retrieved from the DB
protected $fo = NULL;
// Current field being edited
private $field_active = FALSE;
// Current input data for fields
private $field_data = [];
/*
// All this vars should be overridden in the child class
protected $frame_length = 22;
@@ -65,7 +70,7 @@ abstract class Frame
];
// @todo Move this to the database
private $header = RED.'T'.BLUE.'E'.GREEN.'S'.YELLOW.'T'.MAGENTA.'!';
private $header = RED.SPACE.'T'.BLUE.SPACE.'E'.GREEN.SPACE.'S'.YELLOW.SPACE.'T'.MAGENTA.SPACE.'!';
public function __construct(FrameModel $o)
{
@@ -83,6 +88,9 @@ abstract class Frame
// Our parser object
$this->po = $this->parser($startline);
// Set our first editable field
$this->resetCurrentField();
}
/**
@@ -186,10 +194,44 @@ abstract class Frame
/**
* Return the current field configuration
* Will return false if no id specified and the current field is not active
*/
public function getField(int $id)
public function getField(int $id=NULL)
{
return $this->fields()->get($id);
if (is_null($id) AND $this->field_active === FALSE)
return FALSE;
return $this->fields()->get(is_null($id) ? $this->field_active : $id);
}
/**
* Get the input for the current field
*
* @return mixed
*/
public function getFieldCurrentInput()
{
return Arr::get($this->field_data,$this->field_active);
}
public function getFieldData()
{
return $this->field_data;
}
public function getFieldDataId(int $id)
{
return Arr::get($this->field_data,$id);
}
/**
* Return the current field ID
*
* @return bool
*/
public function getFieldId()
{
return $this->field_active;
}
/**
@@ -199,7 +241,7 @@ abstract class Frame
* @param int $after
* @return mixed
*/
public function getFieldId($type='edit',$after=0)
public function getFieldNextId($type='edit',$after=0)
{
return $this->fields()
->search(function($item,$key) use ($type,$after) {
@@ -207,9 +249,16 @@ abstract class Frame
});
}
public function getFieldOptions(int $id)
public function getFieldPrevId($type='edit',$before=FALSE)
{
return array_get($this->fieldoptions,$this->getField($id)->type);
if (! $before)
$before = $this->fields()->count();
return $this->fields()
->reverse()
->search(function($item,$key) use ($type,$before) {
return $key < $before AND $this->isFieldEditable($item->type);
});
}
/**
@@ -278,19 +327,28 @@ abstract class Frame
}
/**
* Determine if a field is editable
* Determine if the current field should be masked during input
*
* @param string $field
* @return mixed
*/
public function isFieldEditable(string $field)
public function isFieldCurrentMask(int $id=NULL): string
{
return array_get(array_get($this->fieldoptions,$field),'edit',FALSE);
if (! $x=$this->getField($id))
return FALSE;
return array_get(array_get($this->fieldoptions,$x->type),'mask','');
}
public function isFieldMasked(string $field)
/**
* Determine if a field is editable
*
* @param string $field
* @return boolean
*/
public function isFieldEditable(string $field): bool
{
return array_get(array_get($this->fieldoptions,$field),'mask',FALSE);
return array_get(array_get($this->fieldoptions,$field),'edit',FALSE);
}
/**
@@ -349,11 +407,11 @@ abstract class Frame
throw new \Exception('Price too high');
if ($cost > 100)
$color = RED;
$color = RED.SPACE;
elseif ($cost > 0)
$color = YELLOW;
$color = YELLOW.SPACE;
else
$color = GREEN;
$color = GREEN.SPACE;
return sprintf($color.'% '.static::$cost_length.'.0f%s',$cost,static::$cost_unit);
}
@@ -387,7 +445,26 @@ abstract class Frame
if (strlen($frame) !== 1)
throw new \Exception('Frame invalid',500);
return WHITE.$num.$frame.(str_repeat(' ',static::$pagenum_length-strlen($num)));
return WHITE.SPACE.$num.$frame.(str_repeat(' ',static::$pagenum_length-strlen($num)));
}
/**
* Reset the current active field to the first one
*/
public function resetCurrentField()
{
$this->field_active = $this->getFieldNextId('edit',0);
$this->resetCurrentFieldData();
}
/**
* Clear the current field
*/
public function resetCurrentFieldData()
{
if ($this->field_active !== FALSE AND isset($this->field_data[$this->field_active]))
unset($this->field_data[$this->field_active]);
}
/**
@@ -410,6 +487,61 @@ abstract class Frame
return $this->fo->route->{$key};
}
/**
* Set the field that is current in focus for input
*
* @param int $num
*/
public function setFieldCurrent(int $num)
{
$this->po->field_active = $num;
}
public function setFieldCurrentInput(string $read)
{
if ($this->field_active === FALSE)
throw new \Exception('No field active?',500);
if (! array_key_exists($this->field_active,$this->field_data))
$this->field_data[$this->field_active] = '';
$this->field_data[$this->field_active] .= $read;
}
/**
* Delete the last character of the current Input field
*
* @return bool
*/
public function setFieldCurrentInputDelete(): bool
{
if (! $this->field_data[$this->field_active])
return FALSE;
$this->field_data[$this->field_active] = substr($this->field_data[$this->field_active],0,-1);
return TRUE;
}
/**
* Set the next active field
*/
public function setFieldNext()
{
$this->field_active = $this->getFieldNextId('edit',$this->field_active+1);
$this->resetCurrentFieldData();
}
/**
* Set the previous active field
*/
public function setFieldPrev()
{
$this->field_active = $this->getFieldPrevId('edit',$this->field_active);
$this->resetCurrentFieldData();
}
/**
* Calculate the length of text
*