Skip to content

Instantly share code, notes, and snippets.

@abbadon1334
Created December 5, 2019 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abbadon1334/c3e5c33b2147335e38ca3dadc9287de1 to your computer and use it in GitHub Desktop.
Save abbadon1334/c3e5c33b2147335e38ca3dadc9287de1 to your computer and use it in GitHub Desktop.
User Grid Criteria, an object to store filters and columns visibility
<?php
class UserGridCriteria extends \atk4\data\Model
{
public $table = "user_grid_criteria";
public $title_field = "name";
/**
* @var string
*/
private $grid_identifier;
public function __construct($persistence = null, $defaults = [])
{
parent::__construct($persistence, $defaults);
if (null === $this->grid_identifier) {
$exc = new \atk4\ui\Exception([
'grid_identifier not defined in $defaults'
]);
$exc->addSolution('this model must be called like : new UserGridCriteria($db,["grid_identifier" => "grid_xyz"])');
throw $exc;
}
}
public function init()
{
parent::init();
/**
* UID for the grid ( can't be the FQCN class, because grid can be reused in multiple part of the app)
*/
$this->addField('grid_identifier', [
'type' => 'string',
'required' => true
]);
/**
* The linked User Model
*/
$this->hasOne('user', [
User::class,
'default' => $this->getLoggedUserID()
]);
/**
* Name defined by the user ( current = last criteria used )
*/
$this->addField('name', ['type' => 'string', 'default' => 'current']);
/**
* Filters criteria
* [
* ['Date', '>' , '01-01-2019']
* ['Status', 'IN' , [5,8,9,12]]
* ]
*/
$this->addField('plugins', ['type' => 'array', 'serialize' => 'json']);
/**
* Filters criteria
* [
* ['Date', '>' , '01-01-2019']
* ['Status', 'IN' , [5,8,9,12]]
* ]
*/
$this->addField('filters', ['type' => 'array', 'serialize' => 'json']);
/**
* Columns show/hide
* [
* 'columnA' => true
* 'columnB' => false
* ]
*/
$this->addField('columns', ['type' => 'array', 'serialize' => 'json']);
/**
* Lock the model to the logged in user
*/
$this->addCondition('user', $this->getLoggedUserID());
/**
* Lock the model to the grid_identifier
*/
$this->addCondition('grid_identifier', $this->grid_identifier);
}
/**
* Load a saved criteria as the current
*
* @return $this
* @throws \atk4\data\Exception
*/
public function setAsCurrent(): self
{
$this->duplicate()->save(['name' => 'current']);
}
/**
* Save current as new criteria with a name
*
* @param string $name
* @return $this
* @throws \atk4\data\Exception
*/
public function saveCurrentAs(string $name): self
{
$this->duplicate()->save(['name' => 'current']);
}
/**
* Return the current/last used criteria
*
* @throws \atk4\data\Exception
*/
public function getCurrentCriteria(): self
{
/** @var self $MCriteria - need to do this, if not i get error on static analysis */
$MCriteria = $this->newInstance()->addCondition('name', 'current');
if ($MCriteria->tryLoadAny()->loaded()) {
return $MCriteria;
}
/** @var self $model - need to do this, if not i get error on static analysis */
$model = $this->newInstance();
return $model;
}
/**
* Return Logged in User ID ( to override non standard auth )
*
* @return int
* @throws \atk4\ui\Exception
* @todo this probably may be removed when we normalize auth system.
*
*/
protected function getLoggedUserID(): int
{
try {
return $this->app->auth->user->id;
} catch (\atk4\core\Exception $e) {
$exc = new \atk4\ui\Exception(['Error on get current user : ' . $e->getMessage()]);
$exc->addSolution('If you use another Auth than the default one ( atk4/login) , you have to override ' . __METHOD__);
throw $exc;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment