public function addObject(IcingaObject $object) { $this->content .= $object->toConfigString(); $this->checksum = null; if ($object->hasProperty('object_type')) { $type = $object->object_type; switch ($type) { case 'object': $this->cntObject++; break; case 'template': $this->cntTemplate++; break; } } return $this; }
protected function getDirectorObjects($type, $single, $plural, $map) { $attrs = array_merge(array_keys($map), array('package', 'templates', 'active')); $objects = array(); $result = $this->getObjects($single, $plural, $attrs, 'director'); foreach ($result as $name => $row) { $attrs = $row->attrs; $properties = array('object_name' => $name, 'object_type' => 'external_object'); foreach ($map as $key => $prop) { if (property_exists($attrs, $key)) { $properties[$prop] = $attrs->{$key}; } } $objects[$name] = IcingaObject::createByType($type, $properties, $this->db); if (property_exists($attrs, 'templates') && count($attrs->templates) > 1 && $objects[$name]->supportsImports()) { $imports = $attrs->templates; array_shift($imports); // TODO (prefetch?): $objects[$name]->imports = $imports; } } return $objects; }
public function replaceWith(IcingaObject $object) { $this->setProperties((array) $object->toPlainObject()); return $this; }
protected function createObject($type, $props) { $props = json_decode($props); return IcingaObject::createByType($type, (array) $props, $this->db()); }
public function storeToDb(IcingaObject $object) { $db = $object->getDb(); $table = $object->getVarsTableName(); $foreignColumn = $object->getVarsIdColumn(); $foreignId = $object->id; foreach ($this->vars as $var) { if ($var->isNew()) { $db->insert($table, array($foreignColumn => $foreignId, 'varname' => $var->getKey(), 'varvalue' => $var->getDbValue(), 'format' => $var->getDbFormat())); continue; } $where = $db->quoteInto(sprintf('%s = ?', $foreignColumn), (int) $foreignId) . $db->quoteInto(' AND varname = ?', $var->getKey()); if ($var->hasBeenDeleted()) { $db->delete($table, $where); } elseif ($var->hasBeenModified()) { $db->update($table, array('varvalue' => $var->getDbValue(), 'format' => $var->getDbFormat()), $where); } } $this->setUnmodified(); }
public function setObject(IcingaObject $object) { $this->object = $object; $this->setConnection($object->getConnection()); return $this; }
/** * Evaluates a SyncRule and returns a list of modified objects * * TODO: This needs to be splitted into smaller methods * * @param SyncRule $rule The synchronization rule that should be used * * @return array List of modified IcingaObjects */ protected function prepareSyncForRule(SyncRule $rule) { $db = $rule->getConnection(); $properties = $rule->fetchSyncProperties(); $sources = $this->perpareImportSources($properties, $db); $imported = $this->fetchImportedData($sources, $properties, $rule, $db); // TODO: Filter auf object, nicht template $objects = IcingaObject::loadAllByType($rule->object_type, $db); if ($rule->object_type === 'datalistEntry') { $no = array(); foreach ($objects as $o) { // if ($o->list_id !== $source-> } } $objectKey = $rule->object_type === 'datalistEntry' ? 'entry_name' : 'object_name'; foreach ($sources as $source) { $sourceId = $source->id; foreach ($imported[$sourceId] as $key => $row) { $newProps = array(); $newVars = array(); $imports = array(); foreach ($properties as $p) { if ($p->source_id !== $sourceId) { continue; } $prop = $p->destination_field; $val = $this->fillVariables($p->source_expression, $row); if (substr($prop, 0, 5) === 'vars.') { $varName = substr($prop, 5); if (substr($varName, -2) === '[]') { $varName = substr($varName, 0, -2); $val = $this->wantArray($val); } $newVars[$varName] = $val; } else { if ($prop === 'import') { $imports[] = $val; } else { $newProps[$prop] = $val; } } } if (array_key_exists($key, $objects)) { switch ($rule->update_policy) { case 'override': $object = IcingaObject::createByType($rule->object_type, $newProps, $db); foreach ($newVars as $prop => $var) { $object->vars()->{$prop} = $var; } if (!empty($imports)) { $object->imports()->set($imports); } $objects[$key]->replaceWith($object); break; case 'merge': $object = $objects[$key]; foreach ($newProps as $prop => $value) { // TODO: data type? $object->set($prop, $value); } foreach ($newVars as $prop => $var) { // TODO: property merge policy $object->vars()->{$prop} = $var; } if (!empty($imports)) { // TODO: merge imports ?! $objects[$key]->imports()->set($imports); } break; default: // policy 'ignore', no action } } else { // New object if ($rule->object_type !== 'datalistEntry') { if (!array_key_exists('object_type', $newProps) || $newProps['object_type'] === null) { $newProps['object_type'] = 'object'; } if (!array_key_exists('object_name', $newProps) || $newProps['object_name'] === null) { $newProps['object_name'] = $key; } } $objects[$key] = IcingaObject::createByType($rule->object_type, $newProps, $db); foreach ($newVars as $prop => $var) { $objects[$key]->vars()->{$prop} = $var; } if (!empty($imports)) { $objects[$key]->imports()->set($imports); } } } } $ignore = array(); foreach ($objects as $key => $object) { if ($object->hasBeenLoadedFromDb() && $rule->purge_existing === 'y') { $found = false; foreach ($sources as $source) { if (array_key_exists($object->{$objectKey}, $imported[$source->id])) { $found = true; break; } } if (!$found) { $object->markForRemoval(); } } // TODO: This should be noticed or removed: if (!$object->{$objectKey}) { $ignore[] = $key; } } foreach ($ignore as $key) { unset($objects[$key]); } return $objects; }
protected function dummyObject() { if ($this->dummyObject === null) { $this->dummyObject = IcingaObject::createByType($this->rule->object_type, array(), $this->db); } return $this->dummyObject; }
protected function handleApiRequest() { $request = $this->getRequest(); $db = $this->db(); switch ($request->getMethod()) { case 'DELETE': $name = $this->object->object_name; $obj = $this->object->toPlainObject(false, true); $form = $this->loadForm('icingaDeleteObject')->setObject($this->object)->setRequest($request)->onSuccess(); return $this->sendJson($obj); case 'POST': case 'PUT': $type = $this->getType(); $data = (array) json_decode($request->getRawBody()); if ($object = $this->object) { if ($request->getMethod() === 'POST') { $object->setProperties($data); } else { $object->replaceWith(IcingaObject::createByType($type, $data, $db)); } } else { $object = IcingaObject::createByType($type, $data, $db); } $response = $this->getResponse(); if ($object->hasBeenModified()) { $object->store(); if ($object->hasBeenLoadedFromDb()) { $response->setHttpResponseCode(200); } else { $response->setHttpResponseCode(201); } } else { $response->setHttpResponseCode(304); } return $this->sendJson($object->toPlainObject(false, true)); case 'GET': return $this->sendJson($this->object->toPlainObject($this->params->shift('resolved'), !$this->params->shift('withNull'), $this->params->shift('properties'))); default: throw new Exception('Unsupported method ' . $request->getMethod()); } }
protected function loadObject() { if ($this->object === null && ($name = $this->params->get('name'))) { $this->object = IcingaObject::loadByType($this->getType(), $name, $this->db()); } return $this->object; }