Files
opensourcepos/app/Models/PluginConfig.php
Ollama 896ed87797 fix: Address CodeRabbit AI review comments
- Move plugin discovery to pre_system in Events.php (allows events to be registered before they fire)
- Add plugin existence check in disablePlugin()
- Add is_subclass_of check before instantiating plugin classes
- Fix str_replace prefix removal in getPluginSettings using str_starts_with + substr
- Add down() migration to drop table on rollback
- Fix saveSettings to JSON-encode arrays/objects
- Update README to use MailchimpPlugin as reference implementation
- Remove CasposPlugin examples from documentation
2026-03-22 19:47:09 +00:00

107 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class PluginConfig extends Model
{
protected $table = 'plugin_config';
protected $primaryKey = 'key';
protected $useAutoIncrement = false;
protected $useSoftDeletes = false;
protected $allowedFields = [
'key',
'value'
];
public function exists(string $key): bool
{
$builder = $this->db->table('plugin_config');
$builder->where('key', $key);
return ($builder->get()->getNumRows() === 1);
}
public function get(string $key): ?string
{
$builder = $this->db->table('plugin_config');
$query = $builder->getWhere(['key' => $key], 1);
if ($query->getNumRows() === 1) {
return $query->getRow()->value;
}
return null;
}
public function set(string $key, string $value): bool
{
$builder = $this->db->table('plugin_config');
if ($this->exists($key)) {
return $builder->update(['value' => $value], ['key' => $key]);
}
return $builder->insert(['key' => $key, 'value' => $value]);
}
public function getPluginSettings(string $pluginId): array
{
$builder = $this->db->table('plugin_config');
$builder->like('key', $pluginId . '_', 'after');
$query = $builder->get();
$settings = [];
$prefix = $pluginId . '_';
foreach ($query->getResult() as $row) {
$key = str_starts_with($row->key, $prefix)
? substr($row->key, strlen($prefix))
: $row->key;
$settings[$key] = $row->value;
}
return $settings;
}
public function deleteKey(string $key): bool
{
$builder = $this->db->table('plugin_config');
return $builder->delete(['key' => $key]);
}
public function deleteAllStartingWith(string $prefix): bool
{
$builder = $this->db->table('plugin_config');
$builder->like('key', $prefix, 'after');
return $builder->delete();
}
public function batchSave(array $data): bool
{
$success = true;
$this->db->transStart();
foreach ($data as $key => $value) {
$success &= $this->set($key, $value);
}
$this->db->transComplete();
return $success && $this->db->transStatus();
}
public function getAll(): array
{
$builder = $this->db->table('plugin_config');
$query = $builder->get();
$configs = [];
foreach ($query->getResult() as $row) {
$configs[$row->key] = $row->value;
}
return $configs;
}
}