public function __construct(Application $app, Repository $repository, $config)
 {
     $this->repository = $repository;
     $this->adapter = $app->getStorageAdapter($config, $repository->getName());
 }
 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 sortRecords($list, $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);
     }
     $dbh = $this->repository->getDatabaseConnection();
     $tempTableName = $tableName . '_' . substr(md5(uniqid(microtime(), true)), 0, 8);
     $timeshiftTimestamp = $this->repository->getTimeshiftTimestamp();
     $sql = 'CREATE TEMPORARY TABLE ' . $tempTableName . ' SELECT * FROM ' . $tableName . ' WHERE workspace = ? AND language = ? AND validfrom_timestamp <= ? AND validuntil_timestamp > ? AND deleted=0';
     $params = array();
     $params[] = $workspace;
     $params[] = $language;
     $params[] = $timeshiftTimestamp;
     $params[] = $timeshiftTimestamp;
     $stmt = $dbh->prepare($sql);
     $stmt->execute($params);
     $sql = 'UPDATE ' . $tempTableName . ' SET parent_id = null, position=null, position_left = null, position_right = null, position_level = null';
     $params = array();
     $stmt = $dbh->prepare($sql);
     $stmt->execute($params);
     $transform = new AdjacentList2NestedSet($list);
     $transform->traverse(0);
     $nestedSet = $transform->getNestedSet();
     $pos = 0;
     $ids = array();
     foreach ($nestedSet as $id => $item) {
         $ids[] = $id;
         $pos++;
         $sql = 'UPDATE ' . $tempTableName . ' SET parent_id = ?, position=?, position_left = ?, position_right = ?, position_level = ? WHERE id = ?';
         $params = array();
         $params[] = $item['parent_id'];
         $params[] = $pos;
         $params[] = $item['left'];
         $params[] = $item['right'];
         $params[] = $item['level'];
         $stmt = $dbh->prepare($sql);
         $params[] = $id;
         $stmt->execute($params);
     }
     // end validity of all current records
     $sql = 'UPDATE ' . $tableName . ' SET validuntil_timestamp = ? WHERE workspace = ? AND language = ? AND validfrom_timestamp <= ? AND validuntil_timestamp > ? AND deleted=0';
     $timeshiftTimestamp = $this->repository->getTimeshiftTimestamp();
     $params = array();
     $params[] = $timeshiftTimestamp;
     $params[] = $workspace;
     $params[] = $language;
     $params[] = $timeshiftTimestamp;
     $params[] = $timeshiftTimestamp;
     $stmt = $dbh->prepare($sql);
     $stmt->execute($params);
     // set validity of all new records
     $sql = 'UPDATE ' . $tempTableName . ' SET revision=revision+1, validfrom_timestamp = ?';
     $params = array();
     $params[] = $timeshiftTimestamp;
     $stmt = $dbh->prepare($sql);
     $stmt->execute($params);
     // Merge back
     $stmt = $dbh->prepare('INSERT INTO ' . $tableName . ' SELECT * FROM ' . $tempTableName);
     $stmt->execute();
     $stmt = $dbh->prepare('DROP TABLE ' . $tempTableName);
     $stmt->execute();
     //self::_updateContentInfo($content_type, $workspace);
     return true;
 }