ajout control title

This commit is contained in:
Jephté Clain 2024-06-18 12:41:10 +04:00
parent 49e312b397
commit 111a87bc3b
6 changed files with 109 additions and 24 deletions

View File

@ -67,7 +67,7 @@ Application::run(new class extends Application {
$channel = new $channelClass; $channel = new $channelClass;
} elseif ($this->tableName !== null) { } elseif ($this->tableName !== null) {
$channel = new class($this->tableName) extends CapacitorChannel { $channel = new class($this->tableName) extends CapacitorChannel {
public function __construct(?string $name=null) { function __construct(?string $name=null) {
parent::__construct($name); parent::__construct($name);
$this->tableName = $name; $this->tableName = $name;
} }

View File

@ -63,7 +63,7 @@ Application::run(new class extends Application {
$channel = new $channelClass; $channel = new $channelClass;
} elseif ($this->tableName !== null) { } elseif ($this->tableName !== null) {
$channel = new class($this->tableName) extends CapacitorChannel { $channel = new class($this->tableName) extends CapacitorChannel {
public function __construct(?string $name=null) { function __construct(?string $name=null) {
parent::__construct($name); parent::__construct($name);
$this->tableName = $name; $this->tableName = $name;
} }

View File

@ -0,0 +1,26 @@
<?php
namespace nur\v\bs3\fo;
use nur\A;
use nur\b\params\Tparametrable;
class ControlTitle extends ControlVisual {
use Tparametrable;
const PARAMETRABLE_PARAMS_SCHEMA = [
"title" => ["?content", null, "texte du titre"],
];
/** @var array|string */
protected $ppTitle;
function getLayout(array $control): array {
return $this->getFgsLayout($control, true);
}
function getControl(): array {
$control = A::with(q($this->ppTitle));
if ($this->ppNaked) return $control;
else return $this->getLayout($control);
}
}

View File

@ -743,6 +743,24 @@ class Form extends ComponentPrintable implements IParametrable, ArrayAccess, Cou
function endGroup(): void { function endGroup(): void {
} }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# title
function title($title, ?array $params=null): ControlTitle {
A::set_nz($params, "title", $title);
$this->fixControlParams($params);
return new ControlTitle($this, $params);
}
function addTitle($title, ?array $params=null): self {
$this->addControl($this->title($title, $params));
return $this;
}
function printTitle($title, ?array $params=null): void {
vo::write($this->title($title, $params));
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# hidden # hidden

View File

@ -16,6 +16,8 @@ class CapacitorChannel {
const PRIMARY_KEYS = null; const PRIMARY_KEYS = null;
const MANAGE_TRANSACTIONS = true;
const EACH_COMMIT_THRESHOLD = 100; const EACH_COMMIT_THRESHOLD = 100;
static function verifix_name(?string $name): string { static function verifix_name(?string $name): string {
@ -23,10 +25,17 @@ class CapacitorChannel {
return strtolower($name); return strtolower($name);
} }
function __construct(?string $name=null, ?int $eachCommitThreshold=null) { protected static function verifix_eachCommitThreshold(?int $eachCommitThreshold): ?int {
$eachCommitThreshold ??= static::EACH_COMMIT_THRESHOLD;
if ($eachCommitThreshold < 0) $eachCommitThreshold = null;
return $eachCommitThreshold;
}
function __construct(?string $name=null, ?int $eachCommitThreshold=null, ?bool $manageTransactions=null) {
$this->name = self::verifix_name($name ?? static::NAME); $this->name = self::verifix_name($name ?? static::NAME);
$this->tableName = static::TABLE_NAME ?? ($this->name."_channel"); $this->tableName = static::TABLE_NAME ?? ($this->name."_channel");
$this->eachCommitThreshold = $eachCommitThreshold ?? static::EACH_COMMIT_THRESHOLD; $this->manageTransactions = $manageTransactions ?? static::MANAGE_TRANSACTIONS;
$this->eachCommitThreshold = self::verifix_eachCommitThreshold($eachCommitThreshold);
$this->setup = false; $this->setup = false;
$this->created = false; $this->created = false;
$columnDefinitions = cl::withn(static::COLUMN_DEFINITIONS); $columnDefinitions = cl::withn(static::COLUMN_DEFINITIONS);
@ -50,37 +59,59 @@ class CapacitorChannel {
$this->primaryKeys = $primaryKeys; $this->primaryKeys = $primaryKeys;
} }
/** @var string */ protected string $name;
protected $name;
function getName(): string { function getName(): string {
return $this->name; return $this->name;
} }
protected $tableName; protected string $tableName;
function getTableName(): string { function getTableName(): string {
return $this->tableName; return $this->tableName;
} }
/**
* @var bool indiquer si les modifications de each doivent être gérées dans
* une transaction. si false, l'utilisateur doit lui même gérer la
* transaction.
*/
protected bool $manageTransactions;
function isManageTransactions(): bool {
return $this->manageTransactions;
}
function setManageTransactions(bool $manageTransactions=true): self {
$this->manageTransactions = $manageTransactions;
return $this;
}
/** /**
* @var ?int nombre maximum de modifications dans une transaction avant un * @var ?int nombre maximum de modifications dans une transaction avant un
* commit automatique dans {@link Capacitor::each()}. Utiliser null pour * commit automatique dans {@link Capacitor::each()}. Utiliser null pour
* désactiver la fonctionnalité. * désactiver la fonctionnalité.
*
* ce paramètre n'a d'effet que si $manageTransactions==true
*/ */
protected $eachCommitThreshold; protected ?int $eachCommitThreshold;
function getEachCommitThreshold(): ?int { function getEachCommitThreshold(): ?int {
return $this->eachCommitThreshold; return $this->eachCommitThreshold;
} }
function setEachCommitThreshold(?int $eachCommitThreshold=null): self {
$this->eachCommitThreshold = self::verifix_eachCommitThreshold($eachCommitThreshold);
return $this;
}
/** /**
* initialiser ce channel avant sa première utilisation. * initialiser ce channel avant sa première utilisation.
*/ */
protected function setup(): void { protected function setup(): void {
} }
protected $setup; protected bool $setup;
function ensureSetup() { function ensureSetup() {
if (!$this->setup) { if (!$this->setup) {
@ -89,7 +120,7 @@ class CapacitorChannel {
} }
} }
protected $created; protected bool $created;
function isCreated(): bool { function isCreated(): bool {
return $this->created; return $this->created;

View File

@ -362,10 +362,13 @@ EOT;
$onEach = func::_prepare($func); $onEach = func::_prepare($func);
$db = $this->db(); $db = $this->db();
$tableName = $channel->getTableName(); $tableName = $channel->getTableName();
$commited = false; $manageTransactions = $channel->isManageTransactions();
$count = 0; $count = 0;
$db->beginTransaction(); if ($manageTransactions) {
$commitThreshold = $channel->getEachCommitThreshold(); $commited = false;
$db->beginTransaction();
$commitThreshold = $channel->getEachCommitThreshold();
}
try { try {
$args ??= []; $args ??= [];
foreach ($this->_all($channel, $filter) as $rowValues) { foreach ($this->_all($channel, $filter) as $rowValues) {
@ -381,7 +384,7 @@ EOT;
"values" => $this->serialize($channel, $updates), "values" => $this->serialize($channel, $updates),
"where" => $rowIds, "where" => $rowIds,
]); ]);
if ($commitThreshold !== null) { if ($manageTransactions && $commitThreshold !== null) {
$commitThreshold--; $commitThreshold--;
if ($commitThreshold == 0) { if ($commitThreshold == 0) {
$db->commit(); $db->commit();
@ -392,11 +395,13 @@ EOT;
} }
$count++; $count++;
} }
$db->commit(); if ($manageTransactions) {
$commited = true; $db->commit();
$commited = true;
}
return $count; return $count;
} finally { } finally {
if (!$commited) $db->rollback(); if ($manageTransactions && !$commited) $db->rollback();
} }
} }
@ -422,10 +427,13 @@ EOT;
$onEach = func::_prepare($func); $onEach = func::_prepare($func);
$db = $this->db(); $db = $this->db();
$tableName = $channel->getTableName(); $tableName = $channel->getTableName();
$commited = false; $manageTransactions = $channel->isManageTransactions();
$count = 0; $count = 0;
$db->beginTransaction(); if ($manageTransactions) {
$commitThreshold = $channel->getEachCommitThreshold(); $commited = false;
$db->beginTransaction();
$commitThreshold = $channel->getEachCommitThreshold();
}
try { try {
$args ??= []; $args ??= [];
foreach ($this->_all($channel, $filter) as $rowValues) { foreach ($this->_all($channel, $filter) as $rowValues) {
@ -437,7 +445,7 @@ EOT;
"from" => $tableName, "from" => $tableName,
"where" => $rowIds, "where" => $rowIds,
]); ]);
if ($commitThreshold !== null) { if ($manageTransactions && $commitThreshold !== null) {
$commitThreshold--; $commitThreshold--;
if ($commitThreshold == 0) { if ($commitThreshold == 0) {
$db->commit(); $db->commit();
@ -448,11 +456,13 @@ EOT;
} }
$count++; $count++;
} }
$db->commit(); if ($manageTransactions) {
$commited = true; $db->commit();
$commited = true;
}
return $count; return $count;
} finally { } finally {
if (!$commited) $db->rollback(); if ($manageTransactions && !$commited) $db->rollback();
} }
} }