/** * Handles DELETE action * * @return \DreamFactory\Core\Utility\ServiceResponse * @throws BadRequestException * @throws \Exception */ protected function handleDELETE() { $this->triggerActionEvent($this->response); $modelClass = $this->getModel(); if (!empty($this->resource)) { $result = $modelClass::deleteById($this->resource, $this->request->getParameters()); } elseif (!empty($ids = $this->request->getParameter(ApiOptions::IDS))) { $result = $modelClass::deleteByIds($ids, $this->request->getParameters()); } elseif ($records = ResourcesWrapper::unwrapResources($this->getPayloadData())) { if (isset($records[0]) && is_array($records[0])) { $result = $modelClass::bulkDelete($records, $this->request->getParameters()); } else { // this may be a list of ids $result = $modelClass::deleteByIds($records, $this->request->getParameters()); } } else { throw new BadRequestException('No record(s) detected in request.'); } $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST); $id = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier()); $result = ResourcesWrapper::cleanResources($result, $asList, $id, ApiOptions::FIELDS_ALL); return $result; }
/** * Handles DELETE action * * @return \DreamFactory\Core\Utility\ServiceResponse * @throws BadRequestException * @throws \Exception */ protected function handleDELETE() { $deleteStorage = $this->request->getParameterAsBool('delete_storage'); $fields = $this->request->getParameter('fields'); if ($deleteStorage) { if (empty($fields)) { $fields = 'id,storage_service_id,storage_container'; } else { if ($fields !== '*') { $fields = explode(',', $fields); if (!in_array('id', $fields)) { $fields[] = 'id'; } if (!in_array('storage_service_id', $fields)) { $fields[] = 'storage_service_id'; } if (!in_array('storage_container', $fields)) { $fields[] = 'storage_container'; } $fields = implode(',', $fields); } } $this->request->setParameter('fields', $fields); } $result = parent::handleDELETE(); if ($deleteStorage) { $temp = $result; $wrapper = ResourcesWrapper::getWrapper(); if (isset($result[$wrapper])) { $temp = ResourcesWrapper::unwrapResources($temp); } if (ArrayUtils::isArrayNumeric($temp)) { foreach ($temp as $app) { static::deleteHostedAppStorage($app['id'], $app['storage_service_id'], $app['storage_container']); } } else { static::deleteHostedAppStorage($temp['id'], $temp['storage_service_id'], $temp['storage_container']); } } return $result; }
/** * Handles DELETE actions. * * @return \DreamFactory\Core\Utility\ServiceResponse * @throws BadRequestException */ protected function handleDELETE() { $force = $this->request->getParameterAsBool('force', false); if (empty($this->folderPath)) { // delete just folders and files from the container if (!empty($content = ResourcesWrapper::unwrapResources($this->request->getPayloadData()))) { $result = $this->deleteFolderContent($content, '', $force); } else { throw new BadRequestException('No resources given for delete.'); } } else { if (empty($this->filePath)) { // delete directory of files and the directory itself // multi-file or folder delete via post data if (!empty($content = ResourcesWrapper::unwrapResources($this->request->getPayloadData()))) { $result = $this->deleteFolderContent($content, $this->folderPath, $force); } else { $this->driver->deleteFolder($this->container, $this->folderPath, $force); $result = ['name' => basename($this->folderPath), 'path' => $this->folderPath]; } } else { // delete file from permanent storage $this->driver->deleteFile($this->container, $this->filePath); $result = ['name' => basename($this->filePath), 'path' => $this->filePath]; } } return ResourcesWrapper::cleanResources($result); }
/** * Handles GET action * * @return array * @throws NotFoundException */ protected function handleGET() { $data = null; $related = $this->request->getParameter(ApiOptions::RELATED); if (!empty($related)) { $related = explode(',', $related); } else { $related = []; } $meta = []; if (!empty($this->resource)) { // Single resource by ID $data = $this->retrieveById($this->resource, $related); } else { if (!empty($ids = $this->request->getParameter(ApiOptions::IDS))) { $data = $this->retrieveByIds($ids, $related); } else { if (!empty($records = ResourcesWrapper::unwrapResources($this->getPayloadData()))) { if (isset($records[0]) && is_array($records[0])) { $data = $this->retrieveByRecords($records, $related); } else { // this may be a list of ids $data = $this->retrieveByIds($ids, $related); } } else { $data = $this->retrieveByRequest($related); if ($this->request->getParameterAsBool(ApiOptions::INCLUDE_COUNT)) { $meta['count'] = count($data); } } } } if ($this->request->getParameterAsBool(ApiOptions::INCLUDE_SCHEMA)) { /** @var BaseSystemModel $model */ $model = $this->getModel(); $meta['schema'] = $model->getTableSchema()->toArray(); } $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST); $id = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier()); $data = ResourcesWrapper::cleanResources($data, $asList, $id, ApiOptions::FIELDS_ALL, !empty($meta)); if (!empty($meta)) { $data['meta'] = $meta; } return $data; }
/** * @return array * @throws \DreamFactory\Core\Exceptions\BadRequestException * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException * @throws \DreamFactory\Core\Exceptions\NotFoundException * @throws \DreamFactory\Core\Exceptions\RestException */ protected function handleDelete() { if (empty($this->resource)) { // not currently supported, maybe batch opportunity? return false; } $this->triggerActionEvent($this->response); if (false === ($tableName = $this->doesTableExist($this->resource, true))) { throw new NotFoundException('Table "' . $this->resource . '" does not exist in the database.'); } if (!empty($this->resourceId)) { return $this->deleteRecordById($tableName, $this->resourceId, $this->options); } $ids = ArrayUtils::get($this->options, ApiOptions::IDS); if (!empty($ids)) { $result = $this->deleteRecordsByIds($tableName, $ids, $this->options); } else { $records = ResourcesWrapper::unwrapResources($this->payload); if (!empty($records)) { $result = $this->deleteRecords($tableName, $records, $this->options); } else { $filter = ArrayUtils::get($this->options, ApiOptions::FILTER); if (!empty($filter)) { $params = ArrayUtils::get($this->options, ApiOptions::PARAMS, []); $result = $this->deleteRecordsByFilter($tableName, $filter, $params, $this->options); } else { if (!ArrayUtils::getBool($this->options, ApiOptions::FORCE)) { throw new BadRequestException('No filter or records given for delete request.'); } return $this->truncateTable($tableName, $this->options); } } } $meta = ArrayUtils::get($result, 'meta'); unset($result['meta']); $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST); $idField = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier()); $result = ResourcesWrapper::cleanResources($result, $asList, $idField, ApiOptions::FIELDS_ALL, !empty($meta)); if (!empty($meta)) { $result['meta'] = $meta; } return $result; }
/** * @return array|bool * @throws \DreamFactory\Core\Exceptions\BadRequestException * @throws \DreamFactory\Core\Exceptions\NotFoundException */ protected function handleDELETE() { $payload = $this->request->getPayloadData(); $fields = $this->request->getParameter(ApiOptions::FIELDS); if (empty($this->resource)) { $tables = $this->request->getParameter(ApiOptions::IDS); if (empty($tables)) { $tables = ResourcesWrapper::unwrapResources($payload); } if (empty($tables)) { throw new BadRequestException('No data in schema delete request.'); } $result = $this->deleteTables($tables); $asList = $this->request->getParameterAsBool(ApiOptions::AS_LIST); $idField = $this->request->getParameter(ApiOptions::ID_FIELD, $this->getResourceIdentifier()); $result = ResourcesWrapper::cleanResources($result, $asList, $idField, $fields); } else { if (false === ($tableName = $this->doesTableExist($this->resource, true))) { throw new NotFoundException('Table "' . $this->resource . '" does not exist in the database.'); } if (empty($this->resourceId)) { $this->deleteTable($tableName); $result = ['success' => true]; } else { $this->deleteField($tableName, $this->resourceId); $result = ['success' => true]; } } return $result; }