2015-07-23 16:40:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2015-12-04 10:59:25 +01:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2015-07-23 16:40:32 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
|
|
|
|
|
|
|
class SyncRule extends DbObject
|
|
|
|
{
|
|
|
|
protected $table = 'sync_rule';
|
|
|
|
|
|
|
|
protected $keyName = 'id';
|
|
|
|
|
|
|
|
protected $autoincKeyName = 'id';
|
|
|
|
|
|
|
|
protected $defaultProperties = array(
|
2016-02-26 11:58:37 +01:00
|
|
|
'id' => null,
|
|
|
|
'rule_name' => null,
|
|
|
|
'object_type' => null,
|
2015-07-24 10:53:55 +02:00
|
|
|
'update_policy' => null,
|
2016-02-26 11:58:37 +01:00
|
|
|
'purge_existing' => null,
|
|
|
|
'filter_expression' => null,
|
2015-07-23 16:40:32 +02:00
|
|
|
);
|
|
|
|
|
2015-12-04 10:59:25 +01:00
|
|
|
private $filter;
|
|
|
|
|
2015-11-25 12:54:21 +01:00
|
|
|
public function listInvolvedSourceIds()
|
|
|
|
{
|
|
|
|
if (! $this->hasBeenLoadedFromDb()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
return array_map('intval', array_unique(
|
|
|
|
$db->fetchCol(
|
|
|
|
$db->select()
|
|
|
|
->from(array('p' => 'sync_property'), 'p.source_id')
|
|
|
|
->join(array('s' => 'import_source'), 's.id = p.source_id', array())
|
|
|
|
->where('rule_id = ?', $this->id)
|
|
|
|
->order('s.source_name')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:54:00 +01:00
|
|
|
public function getPriorityForNextProperty()
|
|
|
|
{
|
|
|
|
if (! $this->hasBeenLoadedFromDb()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
return $db->fetchOne(
|
|
|
|
$db->select()
|
|
|
|
->from(
|
2015-12-02 15:27:25 +01:00
|
|
|
array('p' => 'sync_property'),
|
|
|
|
array('priority' => '(CASE WHEN MAX(p.priority) IS NULL THEN 1 ELSE MAX(p.priority) + 1 END)')
|
|
|
|
)->where('p.rule_id = ?', $this->id)
|
2015-11-25 12:54:00 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-04 10:59:25 +01:00
|
|
|
public function matches($row)
|
|
|
|
{
|
|
|
|
if ($this->filter_expression === null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->filter()->matches($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function filter()
|
|
|
|
{
|
|
|
|
if ($this->filter === null) {
|
|
|
|
$this->filter = Filter::fromQueryString($this->filter_expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->filter;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:54:11 +02:00
|
|
|
public function fetchSyncProperties()
|
|
|
|
{
|
|
|
|
$db = $this->getDb();
|
|
|
|
return SyncProperty::loadAll(
|
|
|
|
$this->getConnection(),
|
2015-12-08 15:01:33 +01:00
|
|
|
$db->select()
|
|
|
|
->from('sync_property')
|
|
|
|
->where('rule_id = ?', $this->id)
|
|
|
|
->order('priority DESC')
|
2015-07-24 10:54:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return $this->syncProperties;
|
|
|
|
}
|
2015-07-23 16:40:32 +02:00
|
|
|
}
|