Jump to content

Problem with ajax / jeditable process module


pwFoo
 Share

Recommended Posts

Hi, 

I play with jeditable as a frontend inline edit module and handle changes with a process module.

All works fine as admin, but if I call the process module as guest the process module returns the login form instead an error message.

I know the redirect at the backend after a logout, but how could I prevent the process module to return the login form?

See returned code here (login form).

<form id="ProcessLoginForm" class="InputfieldForm" method="post" action="./" data-colspacing="0"><ul class="Inputfields">	<li class="Inputfield InputfieldText Inputfield_login_name" id="wrap_login_name">		<label class="InputfieldHeader" for="login_name">Username<i class="toggle-icon fa fa-angle-down" data-to="fa-angle-down fa-angle-right"></i></label>		<div class="InputfieldContent"><input id="login_name" class="ProcessLoginName InputfieldMaxWidth" name="login_name" type="text" maxlength="2048">		</div>	</li>	<li class="Inputfield InputfieldText Inputfield_login_pass" id="wrap_login_pass">		<label class="InputfieldHeader" for="login_pass">Password<i class="toggle-icon fa fa-angle-down" data-to="fa-angle-down fa-angle-right"></i></label>		<div class="InputfieldContent"><input id="login_pass" class="ProcessLoginPass InputfieldMaxWidth" name="login_pass" type="password" maxlength="2048">		</div>	</li>	<li class="Inputfield InputfieldSubmit Inputfield_login_submit" id="wrap_Inputfield_login_submit">		<label class="InputfieldHeader InputfieldHeaderHidden"><span>login_submit<i class="toggle-icon fa fa-angle-down" data-to="fa-angle-down fa-angle-right"></i></span></label>		<div class="InputfieldContent"><button id="Inputfield_login_submit" class="ui-button ui-widget ui-state-default ui-corner-all" name="login_submit" value="Login" type="submit"><span class="ui-button-text">Login</span></button>		</div>	</li></ul><input type="hidden" name="TOKEN1422165275X1417982169" value="1EFKXn9SmQE4bfQzGWMCYh3H21X/C.J81" class="_post_token"></form><p></p><div><a href="/pw/"><i class="fa fa-home"></i> Home</a></div><p></p>

Here is my process module.

 <?php
/**
 * Processwire 'JeditableProcess' module
 * 
 * Needed to handle Jeditable module input at backend
 * 
 * @author pwFoo
 * @since 2014-12-07
 * 
 * ProcessWire 2.x
 * Copyright (C) 2011 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 */
class JeditableProcess extends Process {
    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     * @return array
     */
    public static function getModuleInfo() {
        return array(
            'title' => 'JeditableProcess',
            'summary' => 'Needed to handle Jeditable module input at backend',
            'version' => '0.0.1',
            'permission' => 'page-edit',
        );
    } 
    
    /**
     * Get the input, update and save the page.
     * Each AJAX call to this method will update a single field
     */
    public function ___execute() {
        //print_r($this->input->post);

        //Is AJAX request?
        if(!$this->config->ajax) {
            throw new WireException("Request must be via AJAX");
        }
        
        $pid   = intval($this->sanitizer->selectorValue($this->input->post->pid));
        $field = $this->sanitizer->fieldName($this->input->post->field);
        $val   = $this->sanitizer->text($this->input->post->content);
        
        //echo "PID={$pid}|FIELD={$fieldname}|CONTENT={$val}";

        $refPage = $this->pages->get($pid);

        if(!$refPage->editable($field)) {
            echo "Permission denied!";
        }
        else {
            $refPage->of(false);
            $refPage->set($field, $val);
            $refPage->save($field);

            echo $refPage->$field;
        }
    }
}

As admin all works fine. Field gets changed and saved.

Edited by pwFoo
Link to comment
Share on other sites

I found that problem because $page->editable() or $page->editable("field") doesn't work inside my module...

Method call

$jedit->jeditable('title', $page) // $page == current page, should be instance of Page...?

Inside the module...

// public function jeditable($fieldname, Page $refPage) { // doesn't work. GEt an error message that $refPage is NOT an instance of Page...?!
    public function jeditable($fieldname, $refPage) {
        if (!$refPage->editable($fieldname)) return null;
        
        $fieldname = $this->fields->get($fieldname)->name;
        $pageId = $refPage->id;
        
        return "data-jeditable=1 data-pid={$pageId} data-field={$fieldname}";
    }

1) $refPage (= $page) isn't instance of Page... Why?

2) editable() check doesn't work

Error:  Call to a member function editable() on a non-object
Link to comment
Share on other sites

All PW variables (incl. $page) are not available inside modules, or functions in templates. You need wire('page')->editable() etc, or often in modules, depending on the context you can use $this->page, but wire('page') always works.

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...