Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,302 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Report\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class ReportColumn implements JsonSerializable
|
||||
{
|
||||
/** @var string ALIGN_LEFT */
|
||||
public const ALIGN_LEFT = 'left';
|
||||
|
||||
/** @var string ALIGN_RIGHT */
|
||||
public const ALIGN_RIGHT = 'right';
|
||||
|
||||
/** @var string ALIGN_CENTER */
|
||||
public const ALIGN_CENTER = 'center';
|
||||
|
||||
/** @var string ALIGN_JUSTIFY */
|
||||
public const ALIGN_JUSTIFY = 'justify';
|
||||
|
||||
/** @var string SORT_NUMERIC */
|
||||
public const SORT_NUMERIC = 'numeric';
|
||||
|
||||
/** @var string SORT_ALPHABETIC */
|
||||
public const SORT_ALPHABETIC = 'alphabetic';
|
||||
|
||||
/** @var string FORMAT_SUM_MONEY_DE */
|
||||
public const FORMAT_SUM_MONEY_DE = 'sum_money_de';
|
||||
|
||||
/** @var string FORMAT_SUM_MONEY_EN */
|
||||
public const FORMAT_SUM_MONEY_EN = 'sum_money_en';
|
||||
|
||||
/** @var string FORMAT_DATE_DMY */
|
||||
public const FORMAT_DATE_DMY = 'date_dmy';
|
||||
|
||||
/** @var string FORMAT_DATE_YMD */
|
||||
public const FORMAT_DATE_YMD = 'date_ymd';
|
||||
|
||||
/** @var string FORMAT_DATE_DMYHIS */
|
||||
public const FORMAT_DATE_DMYHIS = 'date_dmyhis';
|
||||
|
||||
/** @var string FORMAT_DATE_YMDHIS */
|
||||
public const FORMAT_DATE_YMDHIS = 'date_ymdhis';
|
||||
|
||||
/** @var string FORMAT_CUSTOM */
|
||||
public const FORMAT_CUSTOM = 'custom';
|
||||
|
||||
/** @var string $key */
|
||||
private $key;
|
||||
|
||||
/** @var string $title */
|
||||
private $title;
|
||||
|
||||
/** @var string size */
|
||||
private $width;
|
||||
|
||||
/** @var string $alignment */
|
||||
private $alignment;
|
||||
|
||||
/** @var bool $isSumColumn */
|
||||
private $isSumColumn;
|
||||
|
||||
/** @var int $id */
|
||||
private $id;
|
||||
|
||||
/** @var int $sequence */
|
||||
private $sequence;
|
||||
|
||||
/** @var string $sorting */
|
||||
private $sorting;
|
||||
|
||||
/** @var string|null $formatType */
|
||||
private $formatType;
|
||||
|
||||
/** @var string|null $formatStatement */
|
||||
private $formatStatement;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $title
|
||||
* @param string $width
|
||||
* @param string $alignment
|
||||
* @param bool $isSumColumn
|
||||
* @param int $id
|
||||
* @param int $sequence
|
||||
* @param string $sorting
|
||||
* @param string|null $formatType
|
||||
* @param null $formatStatement
|
||||
*/
|
||||
public function __construct(
|
||||
$key,
|
||||
$title,
|
||||
$width = '30',
|
||||
$alignment = self::ALIGN_LEFT,
|
||||
$isSumColumn = false,
|
||||
$id = 0,
|
||||
$sequence = 0,
|
||||
$sorting = 'numeric',
|
||||
$formatType = null,
|
||||
$formatStatement = null
|
||||
) {
|
||||
$this->key = $key;
|
||||
$this->title = $title;
|
||||
$this->width = $width;
|
||||
$this->alignment = $alignment;
|
||||
$this->isSumColumn = $isSumColumn;
|
||||
$this->id = $id;
|
||||
$this->sequence = $sequence;
|
||||
$this->sorting = $sorting;
|
||||
$this->formatType = $formatType;
|
||||
$this->formatStatement = $formatStatement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return ReportColumn|null
|
||||
*/
|
||||
public static function fromDbState($data)
|
||||
{
|
||||
if (!isset($data['key_name'])) {
|
||||
return null;
|
||||
}
|
||||
if (!isset($data['title'])) {
|
||||
return null;
|
||||
}
|
||||
$key = (string)$data['key_name'];
|
||||
$title = (string)$data['title'];
|
||||
$width = '';
|
||||
if (isset($data['width'])) {
|
||||
$width = $data['width'];
|
||||
}
|
||||
$alignment = '';
|
||||
if (isset($data['alignment'])) {
|
||||
$alignment = $data['alignment'];
|
||||
}
|
||||
$sorting = 'numeric';
|
||||
if (isset($data['sorting'])) {
|
||||
$sorting = $data['sorting'];
|
||||
}
|
||||
$sequence = 0;
|
||||
if (isset($data['sequence'])) {
|
||||
$sequence = $data['sequence'];
|
||||
}
|
||||
$format = null;
|
||||
if (array_key_exists('format_type', $data)) {
|
||||
$format = $data['format_type'];
|
||||
}
|
||||
$formatStatement = null;
|
||||
if (array_key_exists('format_statement', $data)) {
|
||||
$formatStatement = $data['format_statement'];
|
||||
}
|
||||
$sum = false;
|
||||
if (isset($data['sum']) && $data['sum'] === 1) {
|
||||
$sum = true;
|
||||
}
|
||||
$id = 0;
|
||||
if (isset($data['id'])) {
|
||||
$id = $data['id'];
|
||||
}
|
||||
|
||||
return new self(
|
||||
$key,
|
||||
$title,
|
||||
$width,
|
||||
$alignment,
|
||||
$sum,
|
||||
$id,
|
||||
$sequence,
|
||||
$sorting,
|
||||
$format,
|
||||
$formatStatement
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'key_name' => $this->getKey(),
|
||||
'title' => $this->getTitle(),
|
||||
'width' => $this->getWidth(),
|
||||
'alignment' => $this->getAlignment(),
|
||||
'sorting' => $this->getSorting(),
|
||||
'sum' => $this->isSumColumn(),
|
||||
'id' => $this->getId(),
|
||||
'sequence' => $this->getSequence(),
|
||||
'format_type' => $this->getFormatType(),
|
||||
'format_statement' => $this->getFormatStatement(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$data = $this->toArray();
|
||||
unset($data['id'], $data['sequence']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getWidth()
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAlignment()
|
||||
{
|
||||
return $this->alignment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSumColumn()
|
||||
{
|
||||
return $this->isSumColumn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSequence()
|
||||
{
|
||||
return $this->sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSorting()
|
||||
{
|
||||
return $this->sorting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes sequence number and returns old sequence number.
|
||||
*
|
||||
* @param int $sequence
|
||||
*
|
||||
* @return int previous sequence number
|
||||
*/
|
||||
public function changeSequence($sequence)
|
||||
{
|
||||
$oldSequence = $this->getSequence();
|
||||
$this->sequence = $sequence;
|
||||
|
||||
return $oldSequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getFormatType(): ?string
|
||||
{
|
||||
return $this->formatType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getFormatStatement(): ?string
|
||||
{
|
||||
return $this->formatStatement;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Report\Data;
|
||||
|
||||
use ArrayIterator;
|
||||
use Iterator;
|
||||
use JsonSerializable;
|
||||
use Xentral\Modules\Report\Exception\FormDataException;
|
||||
|
||||
class ReportColumnCollection implements JsonSerializable, Iterator
|
||||
{
|
||||
/** @var ArrayIterator $columnIterator */
|
||||
private $columnIterator;
|
||||
|
||||
/**
|
||||
* @param ReportColumn[] $columns
|
||||
*/
|
||||
public function __construct($columns = [])
|
||||
{
|
||||
$this->columnIterator = new ArrayIterator($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $formData
|
||||
*
|
||||
* @return ReportColumnCollection
|
||||
*/
|
||||
public static function fromFormData($formData)
|
||||
{
|
||||
$columns = [];
|
||||
foreach ($formData as $item) {
|
||||
if (!is_array($item)) {
|
||||
throw new FormDataException('Columns form is wrong format. Array of arrays expected.');
|
||||
}
|
||||
$col = new ReportColumn(
|
||||
$item['key_name'],
|
||||
$item['title'],
|
||||
$item['width'],
|
||||
$item['alignment'],
|
||||
(int)$item['sum'] === 1,
|
||||
(int)$item['id'],
|
||||
(int)$item['sequence'],
|
||||
$item['sorting']
|
||||
);
|
||||
$columns[] = $col;
|
||||
}
|
||||
|
||||
return new self($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->columnIterator->getArrayCopy();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$array = [];
|
||||
foreach ($this->columnIterator as $column) {
|
||||
$array[] = $column->toArray();
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current element
|
||||
*
|
||||
* @return ReportColumn
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->columnIterator->current();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move forward to next element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
$this->columnIterator->next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key of the current element
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->columnIterator->key();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current position is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->columnIterator->valid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind the Iterator to the first element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->columnIterator->rewind();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Report\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
use Xentral\Modules\Report\Exception\FormDataException;
|
||||
|
||||
class ReportData implements JsonSerializable
|
||||
{
|
||||
/**@var string $name */
|
||||
private $name;
|
||||
|
||||
/** @var string $description */
|
||||
private $description;
|
||||
|
||||
/** @var int $projectId */
|
||||
private $projectId;
|
||||
|
||||
/**@var string $sqlQuery */
|
||||
private $sqlQuery;
|
||||
|
||||
/**@var ReportColumnCollection|null $columns */
|
||||
private $columns;
|
||||
|
||||
/** @var ReportParameterCollection|null $parameters */
|
||||
private $parameters;
|
||||
|
||||
/** @var int $id */
|
||||
private $id;
|
||||
|
||||
/** @var string $remark */
|
||||
private $remark;
|
||||
|
||||
/** @var string $category */
|
||||
private $category;
|
||||
|
||||
/** @var bool $readonly */
|
||||
private $readonly;
|
||||
|
||||
/** @var bool $isFavorite */
|
||||
private $isFavorite;
|
||||
|
||||
/** @var string $csvDelimiter */
|
||||
private $csvDelimiter;
|
||||
|
||||
/** @var string $csvEnclosure */
|
||||
private $csvEnclosure;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $description
|
||||
* @param int $projectId
|
||||
* @param string $sqlQuery
|
||||
* @param ReportColumnCollection $columns
|
||||
* @param ReportParameterCollection $parameters
|
||||
* @param int $id
|
||||
* @param string $remark
|
||||
* @param string $category
|
||||
* @param bool $readonly
|
||||
* @param null $csvDelimiter
|
||||
* @param null $csvEnclosure
|
||||
*/
|
||||
public function __construct(
|
||||
$name,
|
||||
$description = '',
|
||||
$projectId = 0,
|
||||
$sqlQuery = '',
|
||||
$columns = null,
|
||||
$parameters = null,
|
||||
$id = 0,
|
||||
$remark = '',
|
||||
$category = '',
|
||||
$readonly = false,
|
||||
$csvDelimiter = ',',
|
||||
$csvEnclosure = ''
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->description = $description;
|
||||
$this->projectId = $projectId;
|
||||
$this->sqlQuery = $sqlQuery;
|
||||
$this->columns = $columns;
|
||||
$this->parameters = $parameters;
|
||||
$this->id = $id;
|
||||
$this->remark = $remark;
|
||||
$this->category = $category;
|
||||
$this->readonly = $readonly;
|
||||
$this->csvDelimiter = $csvDelimiter;
|
||||
$this->csvEnclosure = $csvEnclosure;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $formData
|
||||
*
|
||||
* @return ReportData
|
||||
*/
|
||||
public static function fromFormData($formData)
|
||||
{
|
||||
if (!isset($formData['name'])) {
|
||||
throw new FormDataException('name is required');
|
||||
}
|
||||
$name = $formData['name'];
|
||||
$category = '';
|
||||
if (isset($formData['category'])) {
|
||||
$category = $formData['category'];
|
||||
}
|
||||
$description = '';
|
||||
if (isset($formData['description'])) {
|
||||
$description = $formData['description'];
|
||||
}
|
||||
$project = 0;
|
||||
if (isset($formData['project'])) {
|
||||
$project = (int)$formData['project'];
|
||||
}
|
||||
$query = '';
|
||||
if (isset($formData['sql_query'])) {
|
||||
$query = $formData['sql_query'];
|
||||
}
|
||||
$id = 0;
|
||||
if (isset($formData['id'])) {
|
||||
$id = $formData['id'];
|
||||
}
|
||||
$remarks = '';
|
||||
if (isset($formData['remark'])) {
|
||||
$remarks = $formData['remark'];
|
||||
}
|
||||
$readonly = false;
|
||||
if (isset($formData['readonly']) && $formData['readonly'] === 1) {
|
||||
$readonly = true;
|
||||
}
|
||||
$delimiter = ',';
|
||||
if (isset($formData['csv_delimiter'])) {
|
||||
$delimiter = $formData['csv_delimiter'];
|
||||
}
|
||||
$quote = '';
|
||||
if (isset($formData['csv_enclosure'])) {
|
||||
$quote = $formData['csv_enclosure'];
|
||||
}
|
||||
$instance = new self(
|
||||
$name,
|
||||
$description,
|
||||
$project,
|
||||
$query,
|
||||
null,
|
||||
null,
|
||||
$id,
|
||||
$remarks,
|
||||
$category,
|
||||
$readonly,
|
||||
$delimiter,
|
||||
$quote
|
||||
);
|
||||
if (isset($formData['is_favorite']) && $formData['is_favorite'] === 1) {
|
||||
$instance->isFavorite = true;
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFormData()
|
||||
{
|
||||
$data = [
|
||||
'name' => $this->getName(),
|
||||
'description' => $this->getDescription(),
|
||||
'project' => $this->getProjectId(),
|
||||
'sql_query' => $this->getSqlQuery(),
|
||||
'id' => $this->getId(),
|
||||
'remark' => $this->getRemark(),
|
||||
'category' => $this->getCategory(),
|
||||
'csv_delimiter' => $this->getCsvDelimiter(),
|
||||
'csv_enclosure' => $this->getCsvEnclosure(),
|
||||
];
|
||||
if ($this->isFavorite === true) {
|
||||
$data['is_favorite'] = 1;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getProjectId()
|
||||
{
|
||||
return $this->projectId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSqlQuery()
|
||||
{
|
||||
return $this->sqlQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ReportColumnCollection|null
|
||||
*/
|
||||
public function getColumns()
|
||||
{
|
||||
return $this->columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ReportParameterCollection|null
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRemark()
|
||||
{
|
||||
return $this->remark;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCategory()
|
||||
{
|
||||
return $this->category;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $readonly
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setReadonly($readonly)
|
||||
{
|
||||
$this->readonly = $readonly;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isReadonly()
|
||||
{
|
||||
return $this->readonly;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFavorite()
|
||||
{
|
||||
return $this->isFavorite;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCsvDelimiter()
|
||||
{
|
||||
return $this->csvDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCsvEnclosure()
|
||||
{
|
||||
return $this->csvEnclosure;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isFavorite
|
||||
*/
|
||||
public function setIsFavorite($isFavorite)
|
||||
{
|
||||
$this->isFavorite = $isFavorite;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReportColumnCollection $columns
|
||||
*
|
||||
* @return ReportData
|
||||
*/
|
||||
public function setColumns($columns)
|
||||
{
|
||||
$this->columns = $columns;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReportParameterCollection $parameters
|
||||
*
|
||||
* @return ReportData
|
||||
*/
|
||||
public function setParameters($parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
$data = [
|
||||
'name' => $this->getName(),
|
||||
'description' => $this->getDescription(),
|
||||
'project' => $this->getProjectId(),
|
||||
'sql_query' => $this->getSqlQuery(),
|
||||
'columns' => $this->getColumns(),
|
||||
'parameters' => $this->getParameters(),
|
||||
'remark' => $this->getRemark(),
|
||||
'category' => $this->getCategory(),
|
||||
'csv_delimiter' => $this->getCsvDelimiter(),
|
||||
'csv_enclosure' => $this->getCsvEnclosure(),
|
||||
];
|
||||
if ($this->isReadonly()) {
|
||||
$data['readonly'] = true;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,305 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Report\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
use Xentral\Modules\Report\Exception\OptionParseException;
|
||||
use Xentral\Modules\Report\Exception\ParameterNameException;
|
||||
|
||||
class ReportParameter implements JsonSerializable
|
||||
{
|
||||
/** @var string $varname */
|
||||
private $varname;
|
||||
|
||||
/** @var string $displayname */
|
||||
private $displayname;
|
||||
|
||||
/** @var string $defaultValue */
|
||||
private $defaultValue;
|
||||
|
||||
/** @var ReportParameterOptionValue[] $options */
|
||||
private $options;
|
||||
|
||||
/** @var string $description */
|
||||
private $description;
|
||||
|
||||
/** @var bool $editable */
|
||||
private $editable;
|
||||
|
||||
/** @var int $id */
|
||||
private $id;
|
||||
|
||||
/** @var mixed|null */
|
||||
private $temporaryValue;
|
||||
|
||||
/** @var string $controlType */
|
||||
private $controlType;
|
||||
|
||||
/**
|
||||
* @param string $varname
|
||||
* @param string $defaultValue
|
||||
* @param string $displayname
|
||||
* @param ReportParameterOptionValue[] $options
|
||||
* @param string $description
|
||||
* @param bool $editable
|
||||
* @param int $id
|
||||
* @param string $controlType
|
||||
*/
|
||||
public function __construct(
|
||||
$varname,
|
||||
$defaultValue,
|
||||
$displayname = '',
|
||||
$options = [],
|
||||
$description = '',
|
||||
$editable = true,
|
||||
$id = 0,
|
||||
$controlType = ''
|
||||
) {
|
||||
$this->setVarName($varname);
|
||||
$this->displayname = $displayname;
|
||||
$this->defaultValue = $defaultValue;
|
||||
$this->options = $options;
|
||||
$this->description = $description;
|
||||
$this->editable = $editable;
|
||||
$this->id = $id;
|
||||
$this->controlType = $controlType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return ReportParameter|null
|
||||
*/
|
||||
public static function fromDbState($data)
|
||||
{
|
||||
if (!isset($data['varname'])) {
|
||||
return null;
|
||||
}
|
||||
if (!isset($data['default_value'])) {
|
||||
return null;
|
||||
}
|
||||
$varname = (string)$data['varname'];
|
||||
$default = (string)$data['default_value'];
|
||||
$displayname = '';
|
||||
if (isset($data['displayname'])) {
|
||||
$displayname = $data['displayname'];
|
||||
}
|
||||
$options = [];
|
||||
if (isset($data['options'])) {
|
||||
if (is_string($data['options'])) {
|
||||
$options = self::parseOptions($data['options']);
|
||||
}
|
||||
if (is_array($data['options'])) {
|
||||
foreach ($data['options'] as $option) {
|
||||
$key = array_keys($option)[0];
|
||||
$options[] = new ReportParameterOptionValue($option[$key], $key);
|
||||
}
|
||||
}
|
||||
}
|
||||
$description = '';
|
||||
if (isset($data['description'])) {
|
||||
$description = $data['description'];
|
||||
}
|
||||
$editable = true;
|
||||
if (isset($data['editable']) && ($data['editable'] === 0 || $data['editable'] === false)) {
|
||||
$editable = false;
|
||||
}
|
||||
$id = 0;
|
||||
if (isset($data['id'])) {
|
||||
$id = $data['id'];
|
||||
}
|
||||
$controlType = '';
|
||||
if (isset($data['control_type'])) {
|
||||
$controlType = $data['control_type'];
|
||||
}
|
||||
|
||||
return new self($varname, $default, $displayname, $options, $description, $editable, $id, $controlType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $optionsLine
|
||||
*
|
||||
* @return ReportParameterOptionValue[]
|
||||
*/
|
||||
public static function parseOptions($optionsLine)
|
||||
{
|
||||
if (empty($optionsLine)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$optionObjects = [];
|
||||
|
||||
$options = explode(',', $optionsLine);
|
||||
foreach ($options as $option) {
|
||||
|
||||
$option = trim($option);
|
||||
if (empty($option)) {
|
||||
throw new OptionParseException('Invalid Options Format. Unable to parse.');
|
||||
}
|
||||
|
||||
$value = explode(':', $option);
|
||||
if (count($value) === 2) {
|
||||
if (empty(trim($value[0])) || empty(trim($value[1]))) {
|
||||
throw new OptionParseException('Invalid Options Format. Unable to parse.');
|
||||
}
|
||||
$optionObjects[] = new ReportParameterOptionValue(trim($value[1]), trim($value[0]));
|
||||
} else {
|
||||
$optionObjects[] = new ReportParameterOptionValue(trim($value[0]));
|
||||
}
|
||||
}
|
||||
|
||||
return $optionObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'varname' => $this->getVarname(),
|
||||
'default_value' => $this->getDefaultValue(),
|
||||
'displayname' => $this->getDisplayname(),
|
||||
'options' => $this->getOptionsAsString(),
|
||||
'description' => $this->getDescription(),
|
||||
'editable' => $this->isEditable(),
|
||||
'id' => $this->getId(),
|
||||
'control_type' => $this->getControlType(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'varname' => $this->varname,
|
||||
'default_value' => $this->defaultValue,
|
||||
'displayname' => $this->displayname,
|
||||
'options' => $this->options,
|
||||
'description' => $this->description,
|
||||
'editable' => $this->editable,
|
||||
'control_type' => $this->controlType,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed|null $value
|
||||
*/
|
||||
public function setTemporaryValue($value)
|
||||
{
|
||||
$this->temporaryValue = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
if ($this->temporaryValue !== null) {
|
||||
return $this->temporaryValue;
|
||||
}
|
||||
|
||||
return $this->getDefaultValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getVarname()
|
||||
{
|
||||
return $this->varname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDisplayname()
|
||||
{
|
||||
return $this->displayname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return $this->defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ReportParameterOptionValue[]
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ReportParameterOptionValue[] $options
|
||||
*/
|
||||
public function setOptions($options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOptionsAsString()
|
||||
{
|
||||
$optionstrings = [];
|
||||
foreach ($this->options as $option) {
|
||||
$optionstrings[] = (string)$option;
|
||||
}
|
||||
|
||||
return implode(',', $optionstrings);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getControlType()
|
||||
{
|
||||
return $this->controlType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEditable()
|
||||
{
|
||||
return $this->editable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $varname
|
||||
*/
|
||||
private function setVarName($varname)
|
||||
{
|
||||
if (!preg_match('/^[a-zA-Z]\w*$/', $varname)) {
|
||||
throw new ParameterNameException(sprintf('Invalid parameter name "%s"', 'name'));
|
||||
}
|
||||
|
||||
$this->varname = $varname;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Report\Data;
|
||||
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use Iterator;
|
||||
use JsonSerializable;
|
||||
use Xentral\Modules\Report\Exception\FormDataException;
|
||||
|
||||
class ReportParameterCollection implements JsonSerializable, Iterator, Countable
|
||||
{
|
||||
/** @var ArrayIterator $paramIterator */
|
||||
private $paramIterator;
|
||||
|
||||
/**
|
||||
* @param ReportParameter[] $parameters
|
||||
*/
|
||||
public function __construct($parameters = [])
|
||||
{
|
||||
$this->paramIterator = new ArrayIterator($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $formData
|
||||
*
|
||||
* @return ReportParameterCollection
|
||||
*/
|
||||
public static function fromFormData($formData)
|
||||
{
|
||||
/** @var ReportParameter[] $parameters */
|
||||
$parameters = [];
|
||||
foreach ($formData as $item) {
|
||||
if (!is_array($item)) {
|
||||
throw new FormDataException('Parameter form is wrong format. Array of arrays expected.');
|
||||
}
|
||||
$param = new ReportParameter(
|
||||
$item['varname'],
|
||||
$item['default_value'],
|
||||
$item['displayname'],
|
||||
ReportParameter::parseOptions($item['options']),
|
||||
$item['description'],
|
||||
($item['editable'] === 1),
|
||||
$item['id'],
|
||||
$item['control_type']
|
||||
);
|
||||
$parameters[] = $param;
|
||||
}
|
||||
|
||||
return new self($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
$array = [];
|
||||
foreach ($this->paramIterator as $param) {
|
||||
$array[] = $param->toArray();
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->paramIterator->getArrayCopy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current element
|
||||
*
|
||||
* @return ReportParameter
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->paramIterator->current();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move forward to next element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
$this->paramIterator->next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key of the current element
|
||||
*
|
||||
* @return mixed scalar on success, or null on failure.
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->paramIterator->key();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current position is valid
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->paramIterator->valid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind the Iterator to the first element
|
||||
*
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->paramIterator->rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* Count elements of an object
|
||||
*
|
||||
* The return value is cast to an integer.
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return $this->paramIterator->count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Report\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class ReportParameterOptionValue implements JsonSerializable
|
||||
{
|
||||
/** @var string $value */
|
||||
private $value;
|
||||
|
||||
/** @var string $description */
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param string $description
|
||||
*/
|
||||
public function __construct($value, $description = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
if (empty($description)) {
|
||||
$this->description = $this->value;
|
||||
} else {
|
||||
$this->description = $description;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return sprintf('%s:%s', $this->getDescription(), $this->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return [$this->getDescription() => $this->getValue()];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user