2

In a custom component I need the user in the edit view of an item to be able to load a field in the editor field through modal and ajax.

The case is the user can fill an editor field from a text templates in order to avoid writing or copy/paste a bunch of html.

The problem is when I set the contents of the editor with javascript, the editor field doesn't change it's value. I have commented as //Not working in the code.

So when I select the value from the modal I am calling a javascript function and saving the contents of the field to the database.

The new problem is that I now have to refresh the page or the field to get the new data from the database to the form.

Searching I found a piece of code that suppose to do that using the set->redirect but it does not work. I also tried to call override and call the save function parrent::save() with the new data but it didn't work also. I cannot figure how to make it work. So I am open to any suggestions or any other ways I should try.

Here Is my code :

The ajax calls in edit template : views/contract/tmpl/edit.php

<script type = text/javascript>
  function loadTemplate(tId, title) {
    jQuery.ajax({
      method: "POST",
      cache: false,
      url: 'index.php?option=com_contract&task=template.getTemplate&id=' + tId,
      data: {id: tId}
    }).success(function (response) {
      var data = JSON.parse(response);
      var tmplText = data.template_text;
      jQuery("#templText").val(""); //Not working
      jQuery("#templText").val(tmplText); //Not working
      saveTemplate();
    }).error(function(error) {
      console.log('Error : ');
    });
  }

  function saveTemplate() {
    jQuery.ajax({
      method: "POST",
      cache: false,
      url: 'index.php?option=com_contract&task=contract.saveContractText&id=<?php echo $this->item->id?>',
      data: jQuery("#tmplForm input").serialize()
    }).success(function (response) {

    }).error(function(error) {
      alert(error);
    });
  }
</script>

The template controller : controllers/template.php

class ContractControllerTemplate extends JControllerForm {

  public function getTemplate() {
    $id = $_POST['id'];

    try {
      $db = JFactory::getDbo();
      $query = $db->getQuery(true);
      $query->select('id, template_text')
              ->from('#__contract_templates')
              ->where('id = ' . $id);
      $db->setQuery($query);

      $results = $db->loadObjectList();
    } catch (Exception $e) {
      $msg = $e->getMessage();
      JLog::add('Error msg : ' . $msg, JLog::DEBUG, 'getTemplate_controller');
      JFactory::getApplication()->enqueueMessage($msg, 'error');
      echo $msg;
    }

    echo json_encode($results[0]);
    die();
  }
}

And finally the Contract controller with the save function and the redirect. controllers/contract.php

class ContractControllerContract extends JControllerForm {

  public function saveContractText() {
    JSession::checkToken() or die('Invalid Token');

    $app = JFactory::getApplication();
    $lang = JFactory::getLanguage();
    $model = $this->getModel();
    $table = $model->getTable();
    $data = $this->input->post->get('jform', array(), 'array');
    $checkin = property_exists($table, 'checked_out');
    $context = "$this->option.edit.$this->context";
    $task = $this->getTask();

    if (empty($key)) {
      $key = $table->getKeyName();
    }

    if (empty($urlVar)) {
      $urlVar = $key;
    }
    $recordId = $this->input->getInt($urlVar);
    // Populate the row id from the session.
    $data[$key] = $recordId;

    $data['id'] = $_POST['itemId'];
    $data['contract_text'] = $_POST['templText'];

    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $field = $db->quoteName('contract_text') . ' = ' . $db->quote($data['contract_text']);
    $condition = $db->quoteName('id') . ' = ' . $data['id'];
    $query->update($db->quoteName('#__contract_contracts'))->set($field)->where($condition);
    $db->setQuery($query);
    $result = $db->execute();

    $app->setUserState($context . '.data', $data);
    $this->setRedirect(
            JRoute::_(
                    'index.php?option=' . $this->option . '&view=' . $this->view_item
                    . $this->getRedirectToItemAppend($recordId, $urlVar), false
            )
    );

    return false;
  }

}

Thank you

1 Answer 1

3

When you save the template with the JavaScript call saveTemplate(), you are sending an Ajax call to the server. This Ajax call is the one being redirected with a HTTP redirect. You are ignoring the response from the server. If you checked it out, it would be the content of the page that you're trying to redirect to.

To issue a redirect after the Ajax call you have to do it in the JavaScript function with window.location.href='[URL]'.

In saveTemplate():

}).success(function (response) {

  window.location.href='[URL]';

}).error(function(error) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.