示例#1
0
 public function testOmitItems()
 {
     $items = ['name' => 'Jim', 'age' => 79, 'sex' => 'M', 'country' => 'N/A'];
     $this->assertEquals(count(ArrayUtils::omit($items, ['country'])), 3);
     $this->assertEquals(count(ArrayUtils::omit($items, ['country', 'city'])), 3);
     $this->assertEquals(ArrayUtils::omit($items, 'name'), ['age' => 79, 'sex' => 'M', 'country' => 'N/A']);
     $this->assertEquals(count(ArrayUtils::filterByKey($items, ['country'], true)), 3);
     $this->assertEquals(count(ArrayUtils::filterByKey($items, ['name', 'age', 'city'], true)), 2);
     $this->assertEquals(ArrayUtils::filterByKey($items, 'name', true), ['age' => 79, 'sex' => 'M', 'country' => 'N/A']);
 }
 /**
  * @param string $table
  * @param array $recordData
  * @return bool
  */
 public function copyFiles($tableName, &$recordData)
 {
     $schemaArray = TableSchema::getSchemaArray($tableName);
     foreach ($schemaArray as $column) {
         $colName = $column['id'];
         // Ignore absent values & non-arrays
         if (!isset($recordData[$colName]) || !is_array($recordData[$colName])) {
             continue;
         }
         $foreignRow = $recordData[$colName];
         $colUiType = $column['ui'];
         // $isManyToOne = (array_key_exists('relationship', $column) &&
         //     $column['relationship']['type'] == 'MANYTOONE'
         // );
         // $isManyToMany = (array_key_exists('relationship', $column) &&
         //     $column['relationship']['type'] == 'MANYTOMANY'
         // );
         $foreignTableName = $column['relationship']['related_table'];
         // @todo: rewrite this
         if ($foreignTableName === 'directus_files') {
             // Update/Add foreign record
             $Files = new \Directus\Files\Files();
             if (count(array_filter($foreignRow, 'is_array')) == count($foreignRow)) {
                 $index = 0;
                 foreach ($foreignRow as $row) {
                     if (!isset($row['data'][$this->primaryKeyFieldName]) && isset($row['data']['data'])) {
                         if (array_key_exists('type', $row['data']) && strpos($row['data']['type'], 'embed/') === 0) {
                             $recordData[$colName][$index]['data'] = $Files->saveEmbedData($row['data']);
                         } else {
                             $recordData[$colName][$index]['data'] = $Files->saveData($row['data']['data'], $row['data']['name']);
                             // @NOTE: this is duplicate code from the upload file endpoint
                             //        to maintain the file title.
                             $recordData[$colName][$index]['data'] = array_merge($recordData[$colName][$index]['data'], ArrayUtils::omit($row['data'], ['data', 'name']));
                         }
                     }
                     unset($recordData[$colName][$index]['data']['data']);
                     $index++;
                 }
             } else {
                 if (!isset($foreignRow[$this->primaryKeyFieldName]) && isset($foreignRow['data'])) {
                     if (array_key_exists('type', $foreignRow) && strpos($foreignRow['type'], 'embed/') === 0) {
                         $recordData[$colName] = $Files->saveEmbedData($foreignRow);
                     } else {
                         $recordData[$colName] = $Files->saveData($foreignRow['data'], $foreignRow['name']);
                         // @NOTE: this is duplicate code from the upload file endpoint
                         //        to maintain the file title.
                         $recordData[$colName] = array_merge($recordData[$colName], ArrayUtils::omit($foreignRow, ['data', 'name']));
                     }
                 }
                 unset($recordData[$colName]['data']);
             }
         }
     }
     return true;
 }
示例#3
0
 $TableGateway = new TableGateway($acl, $table, $ZendDb);
 $activityLoggingEnabled = !(isset($_GET['skip_activity_log']) && 1 == $_GET['skip_activity_log']);
 $activityMode = $activityLoggingEnabled ? TableGateway::ACTIVITY_ENTRY_MODE_PARENT : TableGateway::ACTIVITY_ENTRY_MODE_DISABLED;
 switch ($app->request()->getMethod()) {
     case 'POST':
         $requestPayload['user'] = $currentUser['id'];
         $requestPayload['date_uploaded'] = DateUtils::now();
         // When the file is uploaded there's not a data key
         if (array_key_exists('data', $requestPayload)) {
             $Files = new \Directus\Files\Files();
             if (!array_key_exists('type', $requestPayload) || strpos($requestPayload['type'], 'embed/') === 0) {
                 $recordData = $Files->saveEmbedData($requestPayload);
             } else {
                 $recordData = $Files->saveData($requestPayload['data'], $requestPayload['name']);
             }
             $requestPayload = array_merge($recordData, ArrayUtils::omit($requestPayload, ['data', 'name']));
         }
         $newRecord = $TableGateway->manageRecordUpdate($table, $requestPayload, $activityMode);
         $params['id'] = $newRecord['id'];
         break;
     case 'PATCH':
         $requestPayload['id'] = $id;
     case 'PUT':
         if (!is_null($id)) {
             $TableGateway->manageRecordUpdate($table, $requestPayload, $activityMode);
             break;
         }
 }
 $Files = new TableGateway($acl, $table, $ZendDb);
 $response = $Files->getEntries($params);
 if (!$response) {