예제 #1
0
 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;
 }
예제 #2
0
 /**
  * Fetch related, foreign rows for one record's ManyToMany relationships.
  * @param  string $table_name
  * @param  string $foreign_table
  * @param  string $junction_table
  * @param  string $junction_key_left
  * @param  string $junction_key_right
  * @param  string $column_equals
  * @return array                      Foreign rowset
  */
 public function loadManyToManyRelationships($table_name, $foreign_table, $junction_table, $junction_key_left, $junction_key_right, $column_equals)
 {
     $foreign_table_pk = TableSchema::getTablePrimaryKey($foreign_table);
     $foreign_join_column = "{$foreign_table}.{$foreign_table_pk}";
     $junction_join_column = "{$junction_table}.{$junction_key_right}";
     $junction_comparison_column = "{$junction_table}.{$junction_key_left}";
     $junction_table_pk = TableSchema::getTablePrimaryKey($junction_table);
     $junction_id_column = "{$junction_table}." . $junction_table_pk;
     // Less likely name collision:
     $junction_id_column_alias = "directus_junction_id_column_518d31856e131";
     $junction_sort_column_alias = "directus_junction_sort_column_518d318e3f0f5";
     $junctionSelectColumns = array($junction_id_column_alias => $junction_table_pk);
     $sql = new Sql($this->adapter);
     $select = $sql->select();
     // If the Junction Table has a Sort column, do eet.
     // @todo is this the most efficient way?
     // @hint TableSchema#getUniqueColumnName
     $junctionColumns = TableSchema::getAllNonAliasTableColumnNames($junction_table);
     if (in_array('sort', $junctionColumns)) {
         $junctionSelectColumns[$junction_sort_column_alias] = "sort";
         $select->order($junction_sort_column_alias);
     }
     $select->from($foreign_table)->join($junction_table, "{$foreign_join_column} = {$junction_join_column}", $junctionSelectColumns)->where(array($junction_comparison_column => $column_equals))->order("{$junction_id_column} ASC");
     // Only select the fields not on the currently authenticated user group's read field blacklist
     $columns = TableSchema::getAllNonAliasTableColumnNames($foreign_table);
     $select->columns($columns);
     $ForeignTable = new RelationalTableGateway($this->acl, $foreign_table, $this->adapter);
     $results = $ForeignTable->selectWith($select);
     $results = $results->toArray();
     $foreign_data = array();
     $columns = TableSchema::getAllNonAliasTableColumns($foreign_table);
     foreach ($results as $row) {
         $row = $this->parseRecordValuesByMysqlType($row, $columns);
         $junction_table_id = (int) $row[$junction_id_column_alias];
         unset($row[$junction_id_column_alias]);
         $entry = array($junction_table_pk => $junction_table_id);
         if (in_array('sort', $junctionColumns)) {
             // @TODO: check why is this a string instead of an integer.
             $entry['sort'] = (int) $row[$junction_sort_column_alias];
             unset($row[$junction_sort_column_alias]);
         }
         $entry['data'] = $row;
         $foreign_data[] = $entry;
     }
     return array('rows' => $foreign_data);
 }
 /**
  * Fetch related, foreign rows for one record's ManyToMany relationships.
  *
  * @param  string $table_name
  * @param  string $foreign_table
  * @param  string $junction_table
  * @param  string $junction_key_left
  * @param  string $junction_key_right
  * @param  string $column_equals
  * @param  string $parentField
  * @param  int    $level
  *
  * @return array                      Foreign rowset
  */
 public function loadManyToManyRelationships($table_name, $foreign_table, $junction_table, $junction_key_left, $junction_key_right, $column_equals, $parentField = null, $level = 0)
 {
     $foreign_table_pk = TableSchema::getTablePrimaryKey($foreign_table);
     $foreign_join_column = $foreign_table . '.' . $foreign_table_pk;
     $junction_join_column = $junction_table . '.' . $junction_key_right;
     $junction_comparison_column = $junction_table . '.' . $junction_key_left;
     // =============================================================================
     // HOTFIX: prevent infinite circle loop
     // =============================================================================
     if ($parentField && $this->hasToManyCallStack($parentField, $foreign_table)) {
         return $column_equals;
     }
     if ($parentField !== null) {
         $this->addToManyCallStack($level, $parentField, $foreign_table);
     }
     $junction_table_pk = TableSchema::getTablePrimaryKey($junction_table);
     $junction_id_column = $junction_table . '.' . $junction_table_pk;
     // Less likely name collision:
     $junction_id_column_alias = 'directus_junction_id_column_518d31856e131';
     $junction_sort_column_alias = 'directus_junction_sort_column_518d318e3f0f5';
     $junctionSelectColumns = [$junction_id_column_alias => $junction_table_pk];
     $sql = new Sql($this->adapter);
     $select = $sql->select();
     // If the Junction Table has a Sort column, do eet.
     // @todo is this the most efficient way?
     // @hint TableSchema#getUniqueColumnName
     $junctionColumns = TableSchema::getAllNonAliasTableColumnNames($junction_table);
     if (in_array('sort', $junctionColumns)) {
         $junctionSelectColumns[$junction_sort_column_alias] = 'sort';
         $select->order($junction_sort_column_alias);
     }
     $select->from($foreign_table)->join($junction_table, $foreign_join_column . '=' . $junction_join_column, $junctionSelectColumns)->where([$junction_comparison_column => $column_equals])->order($junction_id_column . ' ASC');
     // Only select the fields not on the currently authenticated user group's read field blacklist
     $columns = TableSchema::getAllNonAliasTableColumnNames($foreign_table);
     $select->columns($columns);
     $ForeignTable = new RelationalTableGateway($this->acl, $foreign_table, $this->adapter);
     $results = $ForeignTable->selectWith($select);
     $results = $results->toArray();
     $foreign_data = [];
     $columns = TableSchema::getAllNonAliasTableColumns($foreign_table);
     foreach ($results as $row) {
         $row = $recordData = SchemaManager::parseRecordValuesByType($row, $columns);
         $junction_table_id = (int) $row[$junction_id_column_alias];
         unset($row[$junction_id_column_alias]);
         $entry = [$junction_table_pk => $junction_table_id];
         if (in_array('sort', $junctionColumns)) {
             // @TODO: check why is this a string instead of an integer.
             $entry['sort'] = (int) $row[$junction_sort_column_alias];
             unset($row[$junction_sort_column_alias]);
         }
         $schemaArray = TableSchema::getSchemaArray($foreign_table);
         $alias_fields = $this->filterSchemaAliasFields($schemaArray);
         // (fmrly $alias_schema)
         $row = $this->loadToManyRelationships($row, $alias_fields, $parentField, $level + 1);
         $entry['data'] = $row;
         $foreign_data[] = $entry;
     }
     return ['rows' => $foreign_data];
 }
예제 #4
0
 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;
 }
예제 #5
0
 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;
 }
예제 #6
0
 public function addOrUpdateRecordByArray(array $recordData, $tableName = null)
 {
     $tableName = is_null($tableName) ? $this->table : $tableName;
     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 `' . $tableName . '`.`' . $columnName . '.');
         }
     }
     $columns = TableSchema::getAllNonAliasTableColumns($tableName);
     $recordData = SchemaManager::parseRecordValuesByType($recordData, $columns);
     $TableGateway = $this->makeTable($tableName);
     $rowExists = isset($recordData[$TableGateway->primaryKeyFieldName]);
     if ($rowExists) {
         $Update = new Update($tableName);
         $Update->set($recordData);
         $Update->where([$TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]]);
         $TableGateway->updateWith($Update);
         $this->runHook('postUpdate', [$TableGateway, $recordData, $this->adapter, null]);
     } else {
         $d = $this->applyHook('table.insert:before', [$tableName, $recordData]);
         $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 = [];
             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([$TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]]);
                 $TableGateway->updateWith($Update);
             }
         }
         $this->runHook('postInsert', [$TableGateway, $recordData, $this->adapter, null]);
     }
     $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;
 }