From 111a87bc3b3db226923ecc1bf32f746b0cb60d57 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 18 Jun 2024 12:41:10 +0400 Subject: [PATCH] ajout control title --- nur_bin/mysql-storage.php | 2 +- nur_bin/sqlite-storage.php | 2 +- nur_src/v/bs3/fo/ControlTitle.php | 26 +++++++++++++++++ nur_src/v/bs3/fo/Form.php | 18 ++++++++++++ src/db/CapacitorChannel.php | 47 +++++++++++++++++++++++++------ src/db/CapacitorStorage.php | 38 ++++++++++++++++--------- 6 files changed, 109 insertions(+), 24 deletions(-) create mode 100644 nur_src/v/bs3/fo/ControlTitle.php diff --git a/nur_bin/mysql-storage.php b/nur_bin/mysql-storage.php index 08cde38..9d1087b 100755 --- a/nur_bin/mysql-storage.php +++ b/nur_bin/mysql-storage.php @@ -67,7 +67,7 @@ Application::run(new class extends Application { $channel = new $channelClass; } elseif ($this->tableName !== null) { $channel = new class($this->tableName) extends CapacitorChannel { - public function __construct(?string $name=null) { + function __construct(?string $name=null) { parent::__construct($name); $this->tableName = $name; } diff --git a/nur_bin/sqlite-storage.php b/nur_bin/sqlite-storage.php index 5ff50ea..f73c5ce 100755 --- a/nur_bin/sqlite-storage.php +++ b/nur_bin/sqlite-storage.php @@ -63,7 +63,7 @@ Application::run(new class extends Application { $channel = new $channelClass; } elseif ($this->tableName !== null) { $channel = new class($this->tableName) extends CapacitorChannel { - public function __construct(?string $name=null) { + function __construct(?string $name=null) { parent::__construct($name); $this->tableName = $name; } diff --git a/nur_src/v/bs3/fo/ControlTitle.php b/nur_src/v/bs3/fo/ControlTitle.php new file mode 100644 index 0000000..192e00c --- /dev/null +++ b/nur_src/v/bs3/fo/ControlTitle.php @@ -0,0 +1,26 @@ + ["?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); + } +} diff --git a/nur_src/v/bs3/fo/Form.php b/nur_src/v/bs3/fo/Form.php index 5f6b6f9..f9ebcda 100644 --- a/nur_src/v/bs3/fo/Form.php +++ b/nur_src/v/bs3/fo/Form.php @@ -743,6 +743,24 @@ class Form extends ComponentPrintable implements IParametrable, ArrayAccess, Cou 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 diff --git a/src/db/CapacitorChannel.php b/src/db/CapacitorChannel.php index 293edbf..696f03a 100644 --- a/src/db/CapacitorChannel.php +++ b/src/db/CapacitorChannel.php @@ -16,6 +16,8 @@ class CapacitorChannel { const PRIMARY_KEYS = null; + const MANAGE_TRANSACTIONS = true; + const EACH_COMMIT_THRESHOLD = 100; static function verifix_name(?string $name): string { @@ -23,10 +25,17 @@ class CapacitorChannel { 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->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->created = false; $columnDefinitions = cl::withn(static::COLUMN_DEFINITIONS); @@ -50,37 +59,59 @@ class CapacitorChannel { $this->primaryKeys = $primaryKeys; } - /** @var string */ - protected $name; + protected string $name; function getName(): string { return $this->name; } - protected $tableName; + protected string $tableName; function getTableName(): string { 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 * commit automatique dans {@link Capacitor::each()}. Utiliser null pour * désactiver la fonctionnalité. + * + * ce paramètre n'a d'effet que si $manageTransactions==true */ - protected $eachCommitThreshold; + protected ?int $eachCommitThreshold; function getEachCommitThreshold(): ?int { 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. */ protected function setup(): void { } - protected $setup; + protected bool $setup; function ensureSetup() { if (!$this->setup) { @@ -89,7 +120,7 @@ class CapacitorChannel { } } - protected $created; + protected bool $created; function isCreated(): bool { return $this->created; diff --git a/src/db/CapacitorStorage.php b/src/db/CapacitorStorage.php index ceceb9c..871f177 100644 --- a/src/db/CapacitorStorage.php +++ b/src/db/CapacitorStorage.php @@ -362,10 +362,13 @@ EOT; $onEach = func::_prepare($func); $db = $this->db(); $tableName = $channel->getTableName(); - $commited = false; + $manageTransactions = $channel->isManageTransactions(); $count = 0; - $db->beginTransaction(); - $commitThreshold = $channel->getEachCommitThreshold(); + if ($manageTransactions) { + $commited = false; + $db->beginTransaction(); + $commitThreshold = $channel->getEachCommitThreshold(); + } try { $args ??= []; foreach ($this->_all($channel, $filter) as $rowValues) { @@ -381,7 +384,7 @@ EOT; "values" => $this->serialize($channel, $updates), "where" => $rowIds, ]); - if ($commitThreshold !== null) { + if ($manageTransactions && $commitThreshold !== null) { $commitThreshold--; if ($commitThreshold == 0) { $db->commit(); @@ -392,11 +395,13 @@ EOT; } $count++; } - $db->commit(); - $commited = true; + if ($manageTransactions) { + $db->commit(); + $commited = true; + } return $count; } finally { - if (!$commited) $db->rollback(); + if ($manageTransactions && !$commited) $db->rollback(); } } @@ -422,10 +427,13 @@ EOT; $onEach = func::_prepare($func); $db = $this->db(); $tableName = $channel->getTableName(); - $commited = false; + $manageTransactions = $channel->isManageTransactions(); $count = 0; - $db->beginTransaction(); - $commitThreshold = $channel->getEachCommitThreshold(); + if ($manageTransactions) { + $commited = false; + $db->beginTransaction(); + $commitThreshold = $channel->getEachCommitThreshold(); + } try { $args ??= []; foreach ($this->_all($channel, $filter) as $rowValues) { @@ -437,7 +445,7 @@ EOT; "from" => $tableName, "where" => $rowIds, ]); - if ($commitThreshold !== null) { + if ($manageTransactions && $commitThreshold !== null) { $commitThreshold--; if ($commitThreshold == 0) { $db->commit(); @@ -448,11 +456,13 @@ EOT; } $count++; } - $db->commit(); - $commited = true; + if ($manageTransactions) { + $db->commit(); + $commited = true; + } return $count; } finally { - if (!$commited) $db->rollback(); + if ($manageTransactions && !$commited) $db->rollback(); } }