public function saveConfig($configTypeDefinition, $properties, $workspace = 'default', $language = 'default')
 {
     $repositoryName = $this->repository->getName();
     $configTypeName = $configTypeDefinition->getName();
     $tableName = $repositoryName . '$$config';
     if ($tableName != Util::generateValidIdentifier($repositoryName) . '$$config') {
         throw new RepositoryException('Invalid repository and/or config type name(s).');
     }
     $possibleProperties = $configTypeDefinition->getProperties();
     $notallowed = array_diff(array_keys($properties), $possibleProperties);
     if (count($notallowed) != 0) {
         throw new RepositoryException('Trying to store undefined properties: ' . join(',', $notallowed) . '.', RepositoryException::REPOSITORY_INVALID_PROPERTIES);
     }
     $mandatoryProperties = $configTypeDefinition->getMandatoryProperties('default');
     $missing = array();
     foreach ($mandatoryProperties as $property) {
         if (array_key_exists($property, $properties)) {
             if ($properties[$property] == '') {
                 $missing[] = $property;
             }
         } else {
             $missing[] = $property;
         }
     }
     if (count($missing) != 0) {
         throw new RepositoryException('Trying to store config, but missing mandatory properties: ' . join(',', $missing) . '.', RepositoryException::REPOSITORY_MISSING_MANDATORY_PROPERTIES);
     }
     $dbh = $this->repository->getDatabaseConnection();
     $timestamp = time();
     $timeshiftTimestamp = $this->repository->getTimeshiftTimestamp();
     // get current revision
     $revision = 0;
     try {
         $record = $this->getConfig($configTypeName, $workspace, $language);
         $revision = $record['record']['info']['revision'];
     } catch (RepositoryException $e) {
         // never mind we don't need an existing record
     }
     // invalidate current revision
     $sql = 'UPDATE ' . $tableName . ' SET validuntil_timestamp = ? WHERE id = ? AND workspace = ? AND language = ? AND validfrom_timestamp <=? AND validuntil_timestamp >?';
     $params = array();
     $params[] = $timeshiftTimestamp;
     $params[] = $configTypeName;
     $params[] = $workspace;
     $params[] = $language;
     $params[] = $timeshiftTimestamp;
     $params[] = $timeshiftTimestamp;
     $stmt = $dbh->prepare($sql);
     $stmt->execute($params);
     $values = array();
     $values['id'] = $configTypeName;
     $values['hash'] = md5(serialize($properties));
     $values['workspace'] = $workspace;
     $values['language'] = $language;
     $values['revision'] = $revision + 1;
     $values['properties'] = json_encode($properties);
     $values['lastchange_timestamp'] = $timestamp;
     $values['lastchange_apiuser'] = $this->repository->getAPIUser();
     $values['lastchange_clientip'] = $this->repository->getClientIp();
     $values['lastchange_username'] = $this->repository->getCurrentUserName();
     $values['lastchange_firstname'] = $this->repository->getCurrentUserFirstname();
     $values['lastchange_lastname'] = $this->repository->getCurrentUserLastname();
     $values['validfrom_timestamp'] = $timeshiftTimestamp;
     $values['validuntil_timestamp'] = $this->repository->getMaxTimestamp();
     $sql = 'INSERT INTO ' . $tableName;
     $sql .= ' (' . join(',', array_keys($values)) . ')';
     $sql .= ' VALUES ( ?';
     $sql .= str_repeat(' , ?', count($values) - 1);
     $sql .= ')';
     $stmt = $dbh->prepare($sql);
     $stmt->execute(array_values($values));
     return true;
 }
 public function deleteRecord($id, $workspace = 'default', $language = 'default')
 {
     $repositoryName = $this->repository->getName();
     $contentTypeName = $this->contentTypeDefinition->getName();
     $tableName = $repositoryName . '$' . $contentTypeName;
     if ($tableName != Util::generateValidIdentifier($repositoryName) . '$' . Util::generateValidIdentifier($contentTypeName)) {
         throw new \Exception('Invalid repository and/or content type name(s).', self::INVALID_NAMES);
     }
     // get row of current revision
     try {
         $row = $this->getRecordTableRow($id, $workspace, $language);
     } catch (RepositoryException $e) {
         return false;
     }
     $dbh = $this->repository->getDatabaseConnection();
     // invalidate current revision
     $timeshiftTimestamp = $this->repository->getTimeshiftTimestamp();
     $sql = 'UPDATE ' . $tableName . ' SET validuntil_timestamp = ? WHERE id = ? AND workspace = ? AND language = ? AND deleted = 0 AND validfrom_timestamp <=? AND validuntil_timestamp >?';
     $params = array();
     $params[] = $timeshiftTimestamp;
     $params[] = $id;
     $params[] = $workspace;
     $params[] = $language;
     $params[] = $timeshiftTimestamp;
     $params[] = $timeshiftTimestamp;
     $stmt = $dbh->prepare($sql);
     $stmt->execute($params);
     // copy last revision row and mark record as deleted
     $row['revision'] = $row['revision'] + 1;
     $row['deleted'] = 1;
     $row['lastchange_timestamp'] = $timeshiftTimestamp;
     $row['lastchange_apiuser'] = $this->repository->getAPIUser();
     $row['lastchange_clientip'] = $this->repository->getClientIp();
     $row['lastchange_username'] = $this->repository->getCurrentUserName();
     $row['lastchange_firstname'] = $this->repository->getCurrentUserFirstname();
     $row['lastchange_lastname'] = $this->repository->getCurrentUserLastname();
     $row['validfrom_timestamp'] = $timeshiftTimestamp;
     $row['validuntil_timestamp'] = $this->repository->getMaxTimestamp();
     $void = null;
     $event = new ContentRecordEvent($this->contentTypeDefinition, $void, $row);
     $this->app['dispatcher']->dispatch('content.record.before.delete', $event);
     $sql = 'INSERT INTO ' . $tableName;
     $sql .= ' (' . join(',', array_keys($row)) . ')';
     $sql .= ' VALUES ( ?';
     $sql .= str_repeat(' , ?', count($row) - 1);
     $sql .= ')';
     $stmt = $dbh->prepare($sql);
     $stmt->execute(array_values($row));
     $this->app['dispatcher']->dispatch('content.record.after.delete', $event);
     return true;
     /*
     // mark new revision as deleted
     
     $sql = 'UPDATE ' . $tableName . ' SET deleted=1 WHERE workspace = ? AND language = ?  AND id = ? AND validfrom_timestamp <= ? AND validuntil_timestamp > ?';
     
     $timestamp = $this->repository->getTimeshiftTimestamp();
     
     $stmt     = $dbh->prepare($sql);
     $params   = array();
     $params[] = $workspace;
     $params[] = $language;
     $params[] = $id;
     $params[] = $timestamp;
     $params[] = $timestamp;
     
     $stmt->execute($params);
     
     if ($stmt->rowCount() == 1)
     {
     
         return true;
     }
     
     return false;
     */
 }