示例#1
0
 /**
  * Sync function. Entry point for the Backbone.js requests.
  * All Backbone JSON requests are routed here.
  * This function performs inside routing depending on the request.
  *
  * @return JControllerLegacy	A JControllerLegacy object to support chaining.
  */
 public function sync()
 {
     $user = JFactory::getUser();
     if ($this->resourceType != 'comments' && $user->guest) {
         K2Response::throwError(JText::_('K2_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_THIS_OPERATION'), 403);
     }
     // Get the HTTP request method
     $method = $this->input->getMethod();
     // GET requests ( list or form rendering )
     if ($method == 'GET') {
         $id = $this->input->get('id', null, 'int');
         $id === null ? $this->read('collection') : $this->read('row', $id);
         // For GET request we want to render the whole page, so send back the whole response
         $response = K2Response::getResponse();
         $response->method = $method;
         echo json_encode($response);
     } else {
         if ($method == 'POST') {
             // Get BackboneJS method variable
             $this->_method = $this->input->get('_method', '', 'cmd');
             // Execute the task based on the Backbone method
             switch ($this->_method) {
                 case 'POST':
                     $this->create();
                     break;
                 case 'PUT':
                     $this->update();
                     break;
                 case 'PATCH':
                     $this->patch();
                     break;
                 case 'DELETE':
                     $this->delete();
                     break;
             }
             // For actions we only send back the status the row (if any) and the errors
             $response = new stdClass();
             $response->row = K2Response::getRow();
             $response->status = K2Response::getStatus();
             $response->messages = JFactory::getApplication()->getMessageQueue();
             $response->method = $method;
             echo json_encode($response);
         }
     }
     // Return
     return $this;
 }