Beispiel #1
0
 private function handleRequest($request)
 {
     $code = 1;
     $sequence = 0;
     $success = true;
     $message = null;
     $errorCode = null;
     $call = '';
     try {
         // Check we've got a conforming request
         $request = new BtlRequestObject($request);
         $call = $request->getCall();
         if (substr_count($call, '.') !== 1) {
             throw new BtlException(BtlException::INVALID_CALL_FORMAT, $request);
         }
         list($className, $method) = explode('.', $call);
         $this->checkRequestAPI($className, $method);
         if (!$this->controllerNamespace) {
             throw new BtlException(BtlException::MUST_SET_NAMESPACE);
         }
         $sequence = $request->getSequence();
         // Include the file
         $filename = APP_HOME . _DS_ . 'controller' . _DS_ . $this->controllerNamespace . _DS_ . ucfirst($className) . '.php';
         if (!file_exists($filename)) {
             throw new BtlException(BtlException::FILE_DOESNT_EXIST, $filename);
         }
         require_once $filename;
         // Check the class exists
         $class = 'application\\controller\\' . $this->controllerNamespace . '\\' . ucfirst($className);
         if (!class_exists($class)) {
             throw new BtlException(BtlException::CLASS_DOESNT_EXIST, $class);
         }
         // Create a new instance of the class
         $object = new $class($this->plugin->Mvc);
         // Check the method exists
         if (!method_exists($class, $method)) {
             throw new BtlException(BtlException::CLASS_METHOD_DOESNT_EXIST, $class, $method);
         }
         // Pass the request through to the method on the class
         $result = $object->{$method}($request);
     } catch (NutshellException $exception) {
         $exception->log();
         $code = $exception->getCode();
         $success = false;
         $message = 'request failed';
         $result = array();
         $nutshell = Nutshell::getInstance();
         if ($nutshell->config->application->mode == 'development') {
             // header('HTTP/1.1 500 Application Error');
             $message = $exception->getDescription('array');
         }
     }
     $response = array('sequence' => $sequence, 'timestamp' => time(), 'call' => $call, 'code' => $code, 'success' => $success, 'message' => $message, 'data' => $result);
     return $response;
 }
 protected function setIndividualRecord($data, $request)
 {
     // Trigger a set hook
     $this->onBeforeSet($data);
     // Some sanity checking
     $tableName = $this->getTableName();
     $this->performTypeCheck($data, $tableName);
     // Create the Query Object
     $model = $this->getModel();
     $queryObject = new MvcQueryObject($data);
     $queryObject->setModel($model);
     $queryObject->setWhere($data);
     // Insert or Update
     if (isset($data->id) && $data->id) {
         $queryObject->setType('update');
     } else {
         $queryObject->setType('insert');
     }
     // Set It
     $result = $this->plugin->MvcQuery->query($queryObject);
     // Trigger a set hook
     $this->onAfterSet($data);
     $newRequestData = new MvcQueryObject();
     $newRequestData->_type = $tableName;
     if ($queryObject->getType() == 'insert') {
         $newRequestData->id = $result;
     } else {
         $newRequestData->id = $data->id;
     }
     // return .get()
     $request = new BtlRequestObject();
     $request->setQuery($newRequestData);
     $response = $this->get($request);
     if ($response && is_array($response) && sizeof($response)) {
         $response = $response[0];
     }
     return $response;
 }