Create a new instance of phery
static
phery
factory
([
$config =
null])
-
array
$config: Associative config array
Create a <form> tag with ajax enabled. Must be closed manually with </form>
static
void
form_for
(
string $action,
string $function, [
$attributes =
array()], [
phery $phery =
null])
-
string
$action: where to go, can be empty
-
string
$function: Registered function name
-
array
$attributes:
'confirm' => 'Are you sure?',
'data-type' => 'json',
'submit' => array('all' => true, 'disabled' => true) // 'all' submits all elements on the form, even if empty or not checked, disabled also submit disabled elements
-
phery
$phery: Pass the current instance of phery, so it can check if the functions are defined, and throw exceptions
Generates just one instance. Useful to use in many included files. Chainable
static
phery
instance
([
$config =
null])
-
array
$config: Associative config array
Check if the current call is an ajax call
static
bool
is_ajax
()
Helper function that generates an ajax link, defaults to "A" tag
static
string
link_to
(
string $title,
string $function, [
$attributes =
array()], [
phery $phery =
null])
-
string
$title: The content of the link
-
string
$function: The PHP function assigned name on phery::set()
-
array
$attributes: Extra attributes that can be passed to the link, like class, style, etc
'confirm' => 'Are you sure?' // Display confirmation on click
'tag' => 'a' // The tag for the item, defaults to a
'href' => '/path/to/url' // Define another URI for the AJAX call, this defines the HREF of A
'args' => array(1, "a") // Extra arguments to pass to the AJAX function, will be stored in the args attribute as a JSON notation
'target' => '/default/ajax/controller' // Set the "href" attriute for non-anchor (a) AJAX tags (like buttons or spans). Works for A links too, it won't function without javascript
'data-type' => 'json' // Define the data-type for the communication
-
phery
$phery: Pass the current instance of phery, so it can check if the functions are defined, and throw exceptions
Construct the new phery instance
phery
__construct
([ $config = null])
Return the data associatated with a processed unobstructive POST call
mixed
answer_for
(string $alias, [ $default = NULL])
-
string
$alias: The name of the alias for the process function
-
$default
Set callbacks for pre and post filters. Callbacks are useful for example, if you have 2
or more AJAX functions, and you need to perform the same data manipulation, like removing an 'id' from the $_POST['args'], or to check for potential CSRF or SQL injection attempts on all the functions, clean data or perform START TRANSACTION for database, etc
phery
callback
(
$callbacks)
-
array
$callbacks:
'pre' => array|function // Set a function to be called BEFORE processing the request, if it's an AJAX to be processed request, can be an array of callbacks
'post' => array|function // Set a function to be called AFTER processing the request, if it's an AJAX processed request, can be an array of callbacks
The callback function should be // $additional_args is passed using the callback_data() function, in this case, a pre callback
function pre_callback($ajax_data, $internal_data){
// Do stuff
$_POST['args']['id'] = $additional_args['id'];
return true;
}
// post callback would be to save the data perhaps? Just to keep the code D.R.Y.
function post_callback($ajax_data, $internal_data){
$this->database->save();
return true;
}
Returning false on the callback will make the process() phase to RETURN, but won't exit. You may manually exit on the post callback if desired Any data that should be modified will be inside $_POST['args'] (can be accessed freely on 'pre', will be passed to the AJAX function)
Config the current instance of phery
-
array
$config: Associative array containing the following options
'exit_allowed' => true/false // Defaults to true, stop further script execution
'no_stripslashes' => true/false // Don't apply stripslashes on the args
'exceptions' => true/false // Throw exceptions on errors
'unobstructive' => array('function-alias-1','function-alias-2') // Set the functions that will be called even if is a POST but not an AJAX call
Set any data to pass to the callbacks
phery
data
(
$args,
mixed $args,...)
-
mixed
$args,...: Parameters, can be anything
-
$args
Process the AJAX requests if any
void
process
([bool $last_call = true])
-
bool
$last_call: Set this to false if any other further calls to process() will happen, otherwise it will exit
Sets the functions to respond to the ajax call.
For security reasons, these functions should not be available for direct POST/GET requests. These will be set only for AJAX requests as it will only be called in case of an ajax request, to save resources. The answer/process function, must necessarily have the following structure:
function func($ajax_data, $callback_data){
// Sometimes the $callback_data will have an item called 'submit_id',
// is the ID of the calling DOM element.
// if (isset($callback_data['submit_id'])) { }
$r->jquery('#id')->animate(...);
return $r;
}
-
array
$functions: An array of functions to register to the instance.
void
__set
( $name, $value)