/** * 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; }
/** * Loads the XML form and pass the fields to the response. * Usually there would be no need to override this method. * * @return void */ protected function setForm() { // Get the row $row = K2Response::getRow(); // Initialize form object $_form = new stdClass(); // Get the dispatcher. $dispatcher = JDispatcher::getInstance(); // Check if form file exists jimport('joomla.filesystem.file'); if (JFile::exists(JPATH_ADMINISTRATOR . '/components/com_k2/models/' . $this->getName() . '.xml')) { // Import JForm jimport('joomla.form.form'); // Determine form name and path $formName = 'K2' . ucfirst($this->getName()) . 'Form'; $formPath = JPATH_ADMINISTRATOR . '/components/com_k2/models/' . $this->getName() . '.xml'; // Convert JRegistry instances to plain object so JForm can bind them if (property_exists($row, 'metadata')) { $row->metadata = $row->metadata->toObject(); } if (property_exists($row, 'params')) { $row->params = $row->params->toObject(); } if (property_exists($row, 'plugins')) { $row->plugins = $row->plugins->toObject(); } // Get the form instance $form = JForm::getInstance($formName, $formPath); // Bind values $form->bind($row); // Import plugins to extend the form JPluginHelper::importPlugin('content'); // Trigger the form preparation event $results = $dispatcher->trigger('onContentPrepareForm', array($form, $row)); // Pass the JForm to the model before attaching the fields $this->prepareJForm($form, $row); // Attach the JForm fields to the form foreach ($form->getFieldsets() as $fieldset) { $array = array(); foreach ($form->getFieldset($fieldset->name) as $field) { $tmp = new stdClass(); $tmp->label = $field->label; $tmp->input = $field->input; $tmp->type = $field->type; $array[$field->name] = $tmp; } $name = $fieldset->name; $_form->{$name} = $array; } } // Extend the form with K2 plugins $_form->k2Plugins = array(); JPluginHelper::importPlugin('k2'); $dispatcher->trigger('onK2RenderAdminForm', array(&$_form, $row, $this->getName())); $this->setFormFields($_form, $row); K2Response::setForm($_form); }