141 lines
4.0 KiB
PHP
141 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Rule;
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
/**
|
|
* RuleDef Model
|
|
*
|
|
* Rule definitions that can be linked to multiple tests via testrule mapping table.
|
|
*/
|
|
class RuleDefModel extends BaseModel
|
|
{
|
|
protected $table = 'ruledef';
|
|
protected $primaryKey = 'RuleID';
|
|
protected $allowedFields = [
|
|
'RuleCode',
|
|
'RuleName',
|
|
'Description',
|
|
'EventCode',
|
|
'ConditionExpr',
|
|
'ConditionExprCompiled',
|
|
'CreateDate',
|
|
'StartDate',
|
|
'EndDate',
|
|
];
|
|
|
|
protected $useTimestamps = true;
|
|
protected $createdField = 'CreateDate';
|
|
protected $updatedField = 'StartDate';
|
|
|
|
protected $useSoftDeletes = true;
|
|
protected $deletedField = 'EndDate';
|
|
|
|
/**
|
|
* Fetch active rules for an event scoped by TestSiteID.
|
|
*
|
|
* Rules are standalone and only apply when explicitly linked to a test
|
|
* via the testrule mapping table.
|
|
*
|
|
* @param string $eventCode The event code to filter by
|
|
* @param int|null $testSiteID The test site ID to filter by
|
|
* @return array Array of matching rules
|
|
*/
|
|
public function getActiveByEvent(string $eventCode, ?int $testSiteID = null): array
|
|
{
|
|
if ($testSiteID === null) {
|
|
return [];
|
|
}
|
|
|
|
return $this->select('ruledef.*')
|
|
->join('testrule', 'testrule.RuleID = ruledef.RuleID', 'inner')
|
|
->where('ruledef.EventCode', $eventCode)
|
|
->where('ruledef.EndDate IS NULL')
|
|
->where('testrule.TestSiteID', $testSiteID)
|
|
->where('testrule.EndDate IS NULL')
|
|
->orderBy('ruledef.RuleID', 'ASC')
|
|
->findAll();
|
|
}
|
|
|
|
/**
|
|
* Get all tests linked to a rule
|
|
*
|
|
* @param int $ruleID The rule ID
|
|
* @return array Array of test site IDs
|
|
*/
|
|
public function getLinkedTests(int $ruleID): array
|
|
{
|
|
$db = \Config\Database::connect();
|
|
$result = $db->table('testrule')
|
|
->where('RuleID', $ruleID)
|
|
->where('EndDate IS NULL')
|
|
->select('TestSiteID')
|
|
->get()
|
|
->getResultArray();
|
|
|
|
return array_column($result, 'TestSiteID');
|
|
}
|
|
|
|
/**
|
|
* Link a rule to a test
|
|
*
|
|
* @param int $ruleID The rule ID
|
|
* @param int $testSiteID The test site ID
|
|
* @return bool Success status
|
|
*/
|
|
public function linkTest(int $ruleID, int $testSiteID): bool
|
|
{
|
|
$db = \Config\Database::connect();
|
|
|
|
// Check if already linked (and not soft deleted)
|
|
$existing = $db->table('testrule')
|
|
->where('RuleID', $ruleID)
|
|
->where('TestSiteID', $testSiteID)
|
|
->where('EndDate IS NULL')
|
|
->first();
|
|
|
|
if ($existing) {
|
|
return true; // Already linked
|
|
}
|
|
|
|
// Check if soft deleted - restore it
|
|
$softDeleted = $db->table('testrule')
|
|
->where('RuleID', $ruleID)
|
|
->where('TestSiteID', $testSiteID)
|
|
->where('EndDate IS NOT NULL')
|
|
->first();
|
|
|
|
if ($softDeleted) {
|
|
return $db->table('testrule')
|
|
->where('TestRuleID', $softDeleted['TestRuleID'])
|
|
->update(['EndDate' => null]);
|
|
}
|
|
|
|
// Create new link
|
|
return $db->table('testrule')->insert([
|
|
'RuleID' => $ruleID,
|
|
'TestSiteID' => $testSiteID,
|
|
'CreateDate' => date('Y-m-d H:i:s'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Unlink a rule from a test (soft delete)
|
|
*
|
|
* @param int $ruleID The rule ID
|
|
* @param int $testSiteID The test site ID
|
|
* @return bool Success status
|
|
*/
|
|
public function unlinkTest(int $ruleID, int $testSiteID): bool
|
|
{
|
|
$db = \Config\Database::connect();
|
|
|
|
return $db->table('testrule')
|
|
->where('RuleID', $ruleID)
|
|
->where('TestSiteID', $testSiteID)
|
|
->where('EndDate IS NULL')
|
|
->update(['EndDate' => date('Y-m-d H:i:s')]);
|
|
}
|
|
}
|