public function addOrUpdateRecordByArray(array $recordData, $tableName = null) { foreach ($recordData as $columnName => $columnValue) { if (is_array($columnValue)) { $table = is_null($tableName) ? $this->table : $tableName; throw new SuppliedArrayAsColumnValue("Attempting to write an array as the value for column `{$table}`.`{$columnName}`."); } } $tableName = is_null($tableName) ? $this->table : $tableName; $TableGateway = new self($this->acl, $tableName, $this->adapter); $rowExists = isset($recordData[$TableGateway->primaryKeyFieldName]); if ($rowExists) { $Update = new Update($tableName); $Update->set($recordData); $Update->where(array($TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName])); $TableGateway->updateWith($Update); Hooks::runHook('postUpdate', array($TableGateway, $recordData, $this->adapter, $this->acl)); } else { $d = $recordData; unset($d['data']); $TableGateway->insert($d); $recordData[$TableGateway->primaryKeyFieldName] = $TableGateway->getLastInsertValue(); if ($tableName == "directus_files") { $Files = new \Directus\Files\Files(); $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION); $thumbnailPath = 'thumbs/THUMB_' . $recordData['name']; if ($Files->exists($thumbnailPath)) { $Files->rename($thumbnailPath, 'thumbs/' . $recordData[$this->primaryKeyFieldName] . '.' . $ext); } $updateArray = array(); if ($Files->getSettings('file_naming') == 'file_id') { $Files->rename($recordData['name'], str_pad($recordData[$this->primaryKeyFieldName], 11, "0", STR_PAD_LEFT) . '.' . $ext); $updateArray['name'] = str_pad($recordData[$this->primaryKeyFieldName], 11, "0", STR_PAD_LEFT) . '.' . $ext; $recordData['name'] = $updateArray['name']; } if (!empty($updateArray)) { $Update = new Update($tableName); $Update->set($updateArray); $Update->where(array($TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName])); $TableGateway->updateWith($Update); } } Hooks::runHook('postInsert', array($TableGateway, $recordData, $this->adapter, $this->acl)); } $columns = TableSchema::getAllNonAliasTableColumnNames($tableName); $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns, $TableGateway) { $select->columns($columns)->limit(1); $select->where->equalTo($TableGateway->primaryKeyFieldName, $recordData[$TableGateway->primaryKeyFieldName]); })->current(); return $recordData; }
public function addOrUpdateRecordByArray(array $recordData, $tableName = null) { foreach ($recordData as $columnName => $columnValue) { if (is_array($columnValue)) { $table = is_null($tableName) ? $this->table : $tableName; throw new SuppliedArrayAsColumnValue("Attempting to write an array as the value for column `{$table}`.`{$columnName}`."); } } $tableName = is_null($tableName) ? $this->table : $tableName; $rowExists = isset($recordData['id']); $TableGateway = new self($this->acl, $tableName, $this->adapter); if ($rowExists) { $Update = new Update($tableName); $Update->set($recordData); $Update->where(array('id' => $recordData['id'])); $TableGateway->updateWith($Update); Hooks::runHook('postUpdate', array($TableGateway, $recordData, $this->adapter, $this->acl)); } else { //If we are adding a new directus_files Item, We need to do that logic if ($tableName == "directus_files") { $Storage = new \Directus\Files\Storage\Storage(); //If trying to save to temp, force to default if (!isset($recordData['storage_adapter']) || $recordData['storage_adapter'] == '' || $Storage->storageAdaptersByRole['TEMP']['id'] == $recordData['storage_adapter']) { $recordData['storage_adapter'] = $Storage->storageAdaptersByRole['DEFAULT']['id']; } //Save Temp Thumbnail name for use after files record save $info = pathinfo($recordData['name']); if (in_array($info['extension'], $this->imagickExtensions)) { $thumbnailName = "THUMB_" . $info['filename'] . '.jpg'; } else { $thumbnailName = "THUMB_" . $recordData['name']; } //If we are using files ID, Dont save until after insert if ($Storage->getFilesSettings()['file_file_naming'] != "file_id") { //Save the file in TEMP Storage Adapter to Designated StorageAdapter $recordData['name'] = $Storage->saveFile($recordData['name'], $recordData['storage_adapter']); } } $TableGateway->insert($recordData); $recordData['id'] = $TableGateway->getLastInsertValue(); if ($tableName == "directus_files") { $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION); $updateArray = array(); //If using file_id saving, then update record and set name to id if ($Storage->getFilesSettings()['file_file_naming'] == "file_id") { $newName = $Storage->saveFile($recordData['name'], $recordData['storage_adapter'], str_pad($recordData['id'], 11, "0", STR_PAD_LEFT) . '.' . $ext); $updateArray['name'] = str_pad($recordData['id'], 11, "0", STR_PAD_LEFT) . '.' . $ext; $recordData['name'] = $updateArray['name']; } //If we are using file_id titles, then set title to id if ($Storage->getFilesSettings()['file_title_naming'] == "file_id") { $updateArray['title'] = str_pad($recordData['id'], 11, "0", STR_PAD_LEFT); $recordData['title'] = $updateArray['title']; } if (!empty($updateArray)) { $Update = new Update($tableName); $Update->set($updateArray); $Update->where(array('id' => $recordData['id'])); $TableGateway->updateWith($Update); } //Save Temp Thumbnail to Thumbnail SA using file id: $params['id'] $tempLocation = $Storage->storageAdaptersByRole['TEMP']['destination']; if (file_exists($tempLocation . $thumbnailName)) { $thumbnailDestination = $Storage->storageAdaptersByRole['THUMBNAIL']['destination']; if (in_array($ext, $this->imagickExtensions)) { $ext = 'jpg'; } $Storage->ThumbnailStorage->acceptFile($tempLocation . $thumbnailName, $recordData['id'] . "." . $ext, $thumbnailDestination); unlink($tempLocation . $thumbnailName); } } Hooks::runHook('postInsert', array($TableGateway, $recordData, $this->adapter, $this->acl)); } $columns = TableSchema::getAllNonAliasTableColumnNames($tableName); $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns) { $select->columns($columns)->limit(1); $select->where->equalTo('id', $recordData['id']); })->current(); return $recordData; }
public function addOrUpdateRecordByArray(array $recordData, $tableName = null) { foreach ($recordData as $columnName => $columnValue) { if (is_array($columnValue)) { $table = is_null($tableName) ? $this->table : $tableName; throw new SuppliedArrayAsColumnValue("Attempting to write an array as the value for column `{$table}`.`{$columnName}`."); } } $tableName = is_null($tableName) ? $this->table : $tableName; $rowExists = isset($recordData['id']); $TableGateway = new self($this->acl, $tableName, $this->adapter); if ($rowExists) { $Update = new Update($tableName); $Update->set($recordData); $Update->where(array('id' => $recordData['id'])); $TableGateway->updateWith($Update); Hooks::runHook('postUpdate', array($TableGateway, $recordData, $this->adapter, $this->acl)); } else { //If we are adding a new directus_files Item, We need to do that logic // @todo: clean up/refactor saving file process if ($tableName == "directus_files") { $Storage = new \Directus\Files\Storage\Storage(); //If trying to save to temp, force to default if (!isset($recordData['storage_adapter']) || $recordData['storage_adapter'] == '' || $Storage->storageAdaptersByRole['TEMP']['id'] == $recordData['storage_adapter']) { $recordData['storage_adapter'] = $Storage->storageAdaptersByRole['DEFAULT']['id']; } $StorageAdapters = new DirectusStorageAdaptersTableGateway($this->acl, $this->adapter); //If desired Storage Adapter Exists... $filesAdapter = $StorageAdapters->fetchOneById($recordData['storage_adapter']); //Save Temp Thumbnail name for use after files record save // @todo: make file name format sanatize by default // same as uniqueName by the adapter // replacing space with underscore $originalFile = $recordData['file_name']; // we do not need it part of our records Data unset($recordData['file_name']); $recordData['name'] = str_replace(' ', '_', $recordData['name']); $info = pathinfo($recordData['name']); if (in_array($info['extension'], $this->imagickExtensions)) { $thumbnailName = "THUMB_" . $info['filename'] . '.jpg'; } else { $thumbnailName = "THUMB_" . $recordData['name']; } //If we are using files ID, Dont save until after insert if ($Storage->getFilesSettings()['file_naming'] == "file_name") { //Save the file in TEMP Storage Adapter to Designated StorageAdapter $recordData['name'] = $Storage->saveFile($recordData['name'], $recordData['storage_adapter']); // $fileData = $Storage->saveData($recordData['data'], $recordData['name'], $filesAdapter['destination']); // $recordData['name'] = $fileData['name']; } else { if ($Storage->getFilesSettings()['file_naming'] == "file_hash") { //Save the file in TEMP Storage Adapter to Designated StorageAdapter $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION); $fileHashName = md5(microtime() . $recordData['name']); $newName = $Storage->saveFile($recordData['name'], $recordData['storage_adapter'], $fileHashName . '.' . $ext); $updateArray['name'] = $fileHashName . '.' . $ext; $recordData['name'] = $updateArray['name']; } } } $d = $recordData; unset($d['data']); $TableGateway->insert($d); $recordData['id'] = $TableGateway->getLastInsertValue(); if ($tableName == "directus_files") { $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION); $updateArray = array(); //If using file_id saving, then update record and set name to id if ($Storage->getFilesSettings()['file_naming'] == "file_id") { $newName = $Storage->saveFile($recordData['name'], $recordData['storage_adapter'], str_pad($recordData['id'], 11, "0", STR_PAD_LEFT) . '.' . $ext); $updateArray['name'] = str_pad($recordData['id'], 11, "0", STR_PAD_LEFT) . '.' . $ext; $recordData['name'] = $updateArray['name']; } // @todo: do not make this file create twice. // file should be copied to temp and then work from there. // but is copied on "media" also on "temp" then copied back to "media" if (file_exists($filesAdapter['destination'] . $originalFile)) { unlink($filesAdapter['destination'] . $originalFile); } if (!empty($updateArray)) { $Update = new Update($tableName); $Update->set($updateArray); $Update->where(array('id' => $recordData['id'])); $TableGateway->updateWith($Update); } //Save Temp Thumbnail to Thumbnail SA using file id: $params['id'] $tempLocation = $Storage->storageAdaptersByRole['TEMP']['destination']; if (file_exists($tempLocation . $thumbnailName)) { $thumbnailDestination = $Storage->storageAdaptersByRole['THUMBNAIL']['destination']; if (in_array($ext, $this->imagickExtensions)) { $ext = 'jpg'; } $Storage->ThumbnailStorage->acceptFile($tempLocation . $thumbnailName, $recordData['id'] . "." . $ext, $thumbnailDestination); unlink($tempLocation . $thumbnailName); } } Hooks::runHook('postInsert', array($TableGateway, $recordData, $this->adapter, $this->acl)); } $columns = TableSchema::getAllNonAliasTableColumnNames($tableName); $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns) { $select->columns($columns)->limit(1); $select->where->equalTo('id', $recordData['id']); })->current(); return $recordData; }