Пример #1
0
 /**
  * Action for the creation of "hooks" (see {@link ApiHook})
  *
  * @param integer $_id ID of the hook.
  * @param string $_class Subclass of {@link X2Model} to which the hook pertains
  */
 public function actionHooks($_id = null, $_class = null)
 {
     $method = Yii::app()->request->getRequestType();
     if ($method == 'DELETE') {
         $hook = ApiHook::model()->findByPk($_id);
         if (!$hook instanceof ApiHook) {
             $this->send(404, '"Hook not found." -Smee');
         } elseif (!$hook->userId != Yii::app()->getSuId()) {
             $this->send(403, 'You cannot delete other API users\' hooks in X2Engine.');
         }
         $hook->setScenario('delete.remote');
         if ($hook->delete()) {
             $this->sendEmpty("Successfully unsubscribed from hook with " . "return URL {$hook->target_url}.");
         }
     } else {
         // POST (will respond to all other methods with 405)
         if ($_id !== null) {
             $this->send(405, 'Cannot manipulate preexisting hooks with POST.');
         }
         $hook = new ApiHook();
         $hook->attributes = $this->getJpost();
         $hook->userId = Yii::app()->getSuId();
         if (!empty($_class)) {
             $hook->modelName = get_class($this->staticModel);
         }
         if (!$hook->validate('event')) {
             $this->send(429, "The maximum number of hooks ({$maximum}) has " . "been reached for events of this type.");
         }
         if (!$hook->validate()) {
             $this->response['errors'] = $hook->errors;
             $this->send(422);
         }
         if ($hook->save()) {
             $this->response->httpHeader['Location'] = $this->createAbsoluteUrl('/api2/hooks', array('_id' => $hook->id));
             $this->responseBody = $hook;
             $this->send(201);
         } else {
             $this->send(500, 'Could not save hook due to unexpected ' . 'internal server error.');
         }
     }
 }