/** * Returns true if: * * - This is a PUT request * - The "ids" parameter contains at least one integer (can be a csv list) * - The decoded body is not an array of arrays * * Expansion is required when a PUT request is made to apply the same changes to * an array of objects specified by the "ids" parameter. * */ protected function messageBodyRequiresExpansion() { $method = $this->getMethod(); try { $object_ids = \Altumo\Validation\Arrays::sanitizeCsvArrayPostitiveInteger($this->getParameter('ids', '')); } catch (\Exception $e) { $id_field_map = new \sfAltumoPlugin\Api\ApiFieldMap('id', null, 'ID'); $response = \sfContext::getInstance()->getResponse(); $response->addError('The primary key of the object you\'re trying to update was not set or was not a comma-separated list of integers.', null, $id_field_map); throw $e; } $raw_message_body = $this->getMessageBodyData(); if ($method == self::PUT && !empty($object_ids)) { // If the body is not an array of arrays, we require expansion if (!isset($raw_message_body[0]) || !is_array($raw_message_body[0])) { return true; } } return false; }
/** * Optionally builds a string to add to the end of the URL. * eg. * /2,4,5,6,8 * * @param array $ids * @throws \Exception //if $ids is not null or an * array of positive integers. * @return string */ protected static function constructIdsUrlSuffix($ids = null) { $url_suffix = ''; if (!is_null($ids)) { $ids = \Altumo\Validation\Arrays::sanitizeCsvArrayPostitiveInteger($ids); return '/' . implode(',', $ids); } return ''; }
/** * Runs the write operation and sets the response body. * * ApiResponseBody within the ApiResponse. * */ public function run() { $delete_object = $this->getDeleteObject(); //function $query = $this->getQuery(); $request = $this->getRequest(); // Validate IDs try { $delete_object_ids = \Altumo\Validation\Arrays::sanitizeCsvArrayPostitiveInteger($request->getParameter('ids', '')); } catch (Exception $e) { throw new \Exception("One of the ids provided was invalid."); } if (empty($delete_object_ids)) { throw new \Exception("No valid ids were provided for deletion."); } // Get objects based on $query and filtering by ids $query->filterById($delete_object_ids); $objects_to_delete = $query->find(); if ($objects_to_delete->isEmpty()) { throw new \Exception("No valid ids were provided for deletion."); } // Delete objects $deleted_ids = array(); foreach ($objects_to_delete as $object_to_delete) { $deleted_ids[] = $object_to_delete->getId(); $delete_object($object_to_delete); } $api_response_body = $this->getResponse()->getResponseBody(); $api_response_body->setBody(array("deleted" => $deleted_ids)); }
/** * API Action for api users to crud event subscriptions * * @param ApiRequest $request * * @return void */ public function executeSystemEventSubscription(\sfAltumoPlugin\Api\ApiRequest $request) { try { /* @var $response \sfAltumoPlugin\Api\ApiResponse() */ $response = $this->getResponse(); $user = $this->assertAndRetrieveAuthenticatedUser($request); //prepare the query $query = SystemEventSubscriptionQuery::create()->joinSystemEvent(); // if ids set, filter for those $ids_filter_value = $request->getParameter('ids'); if (!empty($ids_filter_value)) { $query->filterById(\Altumo\Validation\Arrays::sanitizeCsvArrayPostitiveInteger($ids_filter_value)); } //do before_save checks $before_save = function (&$model, &$request_object, &$response, $remote_id, $update) { if (!$model->getUser()) { $current_user = sfContext::getInstance()->getUser()->getUser(); $model->setUser($current_user); } }; $plural = 'system_event_subscriptions'; switch ($request->getMethod()) { case sfWebRequest::GET: // select $response->setStatusCode('200'); $api_get_query = new \sfAltumoPlugin\Api\ApiGetQuery($request, $response, $query, $plural, $this->getSystemEventSubscriptionResultModifier()); $api_get_query->runQuery(); break; case sfWebRequest::POST: // insert $response->setStatusCode('200'); $api_write_operation = new \sfAltumoPlugin\Api\ApiWriteOperation($request, $response, $plural); $api_write_operation->setFieldMaps($this->getSystemEventSubscriptionFieldMappings()); $api_write_operation->setUpdate(false); $api_write_operation->setQuery($query); $api_write_operation->setModifyResult($this->getSystemEventSubscriptionResultModifier()); $api_write_operation->setBeforeSave($before_save); $api_write_operation->run(); break; case sfWebRequest::PUT: // update $response->setStatusCode('200'); $api_write_operation = new \sfAltumoPlugin\Api\ApiWriteOperation($request, $response, $plural); $api_write_operation->setFieldMaps($this->getSystemEventSubscriptionFieldMappings()); $api_write_operation->setUpdate(true); $api_write_operation->setQuery($query); $api_write_operation->setBeforeSave($before_save); $api_write_operation->setModifyResult($this->getSystemEventSubscriptionResultModifier()); $api_write_operation->run(); break; case sfWebRequest::DELETE: // delete $response->setStatusCode('200'); $api_delete_operation = new \sfAltumoPlugin\Api\ApiDeleteOperation($request, $response, $query); $api_delete_operation->run(); break; default: // action not supported $response->setStatusCode('405'); break; } } catch (Exception $e) { $response->addException($e); } return $response->respond(); }