From 3b49416788e5f7d84559086e3557e1e51c7b8c77 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 22 Aug 2014 14:29:18 +0000 Subject: [PATCH] class to send push notifications to a logged in user --- phpgwapi/inc/class.egw_json.inc.php | 429 ++++++++++++----------- phpgwapi/inc/class.egw_json_push.inc.php | 105 ++++++ 2 files changed, 328 insertions(+), 206 deletions(-) create mode 100644 phpgwapi/inc/class.egw_json_push.inc.php diff --git a/phpgwapi/inc/class.egw_json.inc.php b/phpgwapi/inc/class.egw_json.inc.php index 2feedebe04..d4bf3ff906 100644 --- a/phpgwapi/inc/class.egw_json.inc.php +++ b/phpgwapi/inc/class.egw_json.inc.php @@ -15,7 +15,6 @@ */ class egw_json_request { - private static $_hadJSONRequest = false; /** @@ -28,7 +27,7 @@ class egw_json_request * Neccessary if json.php is used to send arbitrary JSON data eg. nodes for foldertree! * egw_json_request::isJSONRequest(false) * - * @param boolean $set=null + * @param boolean $set =null * @return boolean */ public static function isJSONRequest($set=null) @@ -165,10 +164,225 @@ class egw_json_request } } +/** + * Abstract class implementing different type of JSON messages understood by client-side + */ +abstract class egw_json_msg +{ + /** + * Adds an "alert" to the response which can be handeled on the client side. + * + * The default implementation simply displays the text supplied here with the JavaScript function "alert". + * + * @param string $message contains the actual message being sent to the client. + * @param string $details (optional) can be used to inform the user on the client side about additional details about the error. This might be information how the error can be resolved/why it was raised or simply some debug data. + */ + public function alert($message, $details = '') + { + if (is_string($message) && is_string($details)) + { + $this->addGeneric('alert', array( + "message" => $message, + "details" => $details)); + } + else + { + throw new Exception("Invalid parameters supplied."); + } + } + + /** + * Allows to add a generic java script to the response which will be executed upon the request gets received. + * + * @deprecated + * @param string $script the script code which should be executed upon receiving + */ + public function script($script) + { + if (is_string($script)) + { + $this->addGeneric('script', $script); + } + else + { + throw new Exception("Invalid parameters supplied."); + } + } + + /** + * Allows to call a global javascript function with given parameters: window[$func].apply(window, $parameters) + * + * @param string $function name of the global (window) javascript function to call + * @param array $parameters =array() + */ + public function apply($function,array $parameters=array()) + { + if (is_string($function)) + { + $this->addGeneric('apply', array( + 'func' => $function, + 'parms' => $parameters, + )); + } + else + { + throw new Exception("Invalid parameters supplied."); + } + } + + /** + * Allows to call a global javascript function with given parameters: window[$func].call(window[, $param1[, ...]]) + * + * @param string $func name of the global (window) javascript function to call + * @param mixed $parameters variable number of parameters + */ + public function call($function) + { + $parameters = func_get_args(); + array_shift($parameters); // shift off $function + + if (is_string($function)) + { + $this->addGeneric('apply', array( + 'func' => $function, + 'parms' => $parameters, + )); + } + else + { + throw new Exception("Invalid parameters supplied."); + } + } + + /** + * Allows to call a jquery function on a selector with given parameters: $j($selector).$func($parmeters) + * + * @param string $selector jquery selector + * @param string $method name of the jquery to call + * @param array $parameters =array() + */ + public function jquery($selector,$method,array $parameters=array()) + { + if (is_string($selector) && is_string($method)) + { + $this->addGeneric('jquery', array( + 'select' => $selector, + 'func' => $method, + 'parms' => $parameters, + )); + } + else + { + throw new Exception("Invalid parameters supplied."); + } + } + + public function generic($type, array $parameters = array()) + { + if (is_string($type)) + { + $this->addGeneric($type, $parameters); + } + else + { + throw new Exception("Invalid parameters supplied."); + } + } + + /** + * Adds an html assign to the response, which is excecuted upon the request is received. + * + * @param string $id id of dom element to modify + * @param string $key attribute name of dom element which should be modified + * @param string $value the value which should be assigned to the given attribute + */ + public function assign($id, $key, $value) + { + if (is_string($id) && is_string($key) && (is_string($value) || is_numeric($value) || is_null($value))) + { + $this->addGeneric('assign', array( + 'id' => $id, + 'key' => $key, + 'value' => $value, + )); + } + else + { + throw new Exception("Invalid parameters supplied"); + } + } + + /** + * Redirect to given url + * + * @param string $url + * @param boolean $global specifies whether to redirect the whole framework + * @param string $app =null default current app from flags + * or only the current application + */ + public function redirect($url, $global = false, $app=null) + { + if (is_string($url) && is_bool($global)) + { + //self::script("location.href = '$url';"); + $this->addGeneric('redirect', array( + 'url' => $url, + 'global' => $global, + 'app' => $app ? $app : $GLOBALS['egw_info']['flags']['currentapp'], + )); + } + } + + /** + * Displays an error message on the client + */ + public function error($msg) + { + if (is_string($msg)) + { + $this->addGeneric('error', $msg); + } + } + + /** + * Includes the given CSS file. Every url can only be included once. + * + * @param string $url specifies the url to the css file to include + */ + public function includeCSS($url) + { + if (is_string($url)) + { + $this->addGeneric('css', $url); + } + } + + /** + * Includes the given JS file. Every url can only be included once. + * + * @param string $url specifies the url to the css file to include + */ + public function includeScript($url) + { + if (is_string($url)) + { + $this->addGeneric('js', $url); + } + } + + /** + * Adds any type of data to the message + * + * @param string $key + * @param mixed $data + */ + abstract protected function addGeneric($key, $data); +} + /** * Class used to send ajax responses */ -class egw_json_response +class egw_json_response extends egw_json_msg { /** * A response can only contain one generic data part. @@ -201,7 +415,7 @@ class egw_json_response /** * Force use of singleton: $response = egw_json_response::get(); */ - private function __construct() + protected function __construct() { } @@ -312,7 +526,10 @@ class egw_json_response } /** - * Adds any type of data to the response array + * Adds any type of data to the message + * + * @param string $key + * @param mixed $data */ protected function addGeneric($key, $data) { @@ -336,6 +553,7 @@ class egw_json_response return $return; } + /** * Adds a "data" response to the json response. * @@ -358,207 +576,6 @@ class egw_json_response } } - /** - * Adds an "alert" to the response which can be handeled on the client side. - * - * The default implementation simply displays the text supplied here with the JavaScript function "alert". - * - * @param string $message contains the actual message being sent to the client. - * @param string $details (optional) can be used to inform the user on the client side about additional details about the error. This might be information how the error can be resolved/why it was raised or simply some debug data. - */ - public function alert($message, $details = '') - { - if (is_string($message) && is_string($details)) - { - $this->addGeneric('alert', array( - "message" => $message, - "details" => $details)); - } - else - { - throw new Exception("Invalid parameters supplied."); - } - } - - /** - * Allows to add a generic java script to the response which will be executed upon the request gets received. - * - * @deprecated - * @param string $script the script code which should be executed upon receiving - */ - public function script($script) - { - if (is_string($script)) - { - $this->addGeneric('script', $script); - } - else - { - throw new Exception("Invalid parameters supplied."); - } - } - - /** - * Allows to call a global javascript function with given parameters: window[$func].apply(window, $parameters) - * - * @param string $func name of the global (window) javascript function to call - * @param array $parameters=array() - */ - public function apply($function,array $parameters=array()) - { - if (is_string($function)) - { - $this->addGeneric('apply', array( - 'func' => $function, - 'parms' => $parameters, - )); - } - else - { - throw new Exception("Invalid parameters supplied."); - } - } - - /** - * Allows to call a global javascript function with given parameters: window[$func].call(window[, $param1[, ...]]) - * - * @param string $func name of the global (window) javascript function to call - * @param mixed $parameters variable number of parameters - */ - public function call($function) - { - $parameters = func_get_args(); - array_shift($parameters); // shift off $function - - if (is_string($function)) - { - $this->addGeneric('apply', array( - 'func' => $function, - 'parms' => $parameters, - )); - } - else - { - throw new Exception("Invalid parameters supplied."); - } - } - - /** - * Allows to call a jquery function on a selector with given parameters: $j($selector).$func($parmeters) - * - * @param string $selector jquery selector - * @param string $method name of the jquery to call - * @param array $parameters=array() - */ - public function jquery($selector,$method,array $parameters=array()) - { - if (is_string($selector) && is_string($method)) - { - $this->addGeneric('jquery', array( - 'select' => $selector, - 'func' => $method, - 'parms' => $parameters, - )); - } - else - { - throw new Exception("Invalid parameters supplied."); - } - } - - public function generic($type, array $parameters = array()) - { - if (is_string($type)) - { - $this->addGeneric($type, $parameters); - } - else - { - throw new Exception("Invalid parameters supplied."); - } - } - - /** - * Adds an html assign to the response, which is excecuted upon the request is received. - * - * @param string $id id of dom element to modify - * @param string $key attribute name of dom element which should be modified - * @param string $value the value which should be assigned to the given attribute - */ - public function assign($id, $key, $value) - { - if (is_string($id) && is_string($key) && (is_string($value) || is_numeric($value) || is_null($value))) - { - $this->addGeneric('assign', array( - 'id' => $id, - 'key' => $key, - 'value' => $value, - )); - } - else - { - throw new Exception("Invalid parameters supplied"); - } - } - - /** - * Redirect to given url - * - * @param string $url - * @param boolean $global specifies whether to redirect the whole framework - * @param string $app=null default current app from flags - * or only the current application - */ - public function redirect($url, $global = false, $app=null) - { - if (is_string($url) && is_bool($global)) - { - //self::script("location.href = '$url';"); - $this->addGeneric('redirect', array( - 'url' => $url, - 'global' => $global, - 'app' => $app ? $app : $GLOBALS['egw_info']['flags']['currentapp'], - )); - } - } - - /** - * Displays an error message on the client - */ - public function error($msg) - { - if (is_string($msg)) - { - $this->addGeneric('error', $msg); - } - } - - /** - * Includes the given CSS file. Every url can only be included once. - * - * @param string $url specifies the url to the css file to include - */ - public function includeCSS($url) - { - if (is_string($url)) - { - $this->addGeneric('css', $url); - } - } - - /** - * Includes the given JS file. Every url can only be included once. - * - * @param string $url specifies the url to the css file to include - */ - public function includeScript($url) - { - if (is_string($url)) - { - self::get()->addGeneric('js', $url); - } - } - /** * Returns the actual JSON code generated by calling the above "add" function. * diff --git a/phpgwapi/inc/class.egw_json_push.inc.php b/phpgwapi/inc/class.egw_json_push.inc.php new file mode 100644 index 0000000000..cf4db64113 --- /dev/null +++ b/phpgwapi/inc/class.egw_json_push.inc.php @@ -0,0 +1,105 @@ + + * @version $Id$ + */ + +/** + * Class to push JSON commands to client + */ +class egw_json_push extends egw_json_msg +{ + /** + * Available backends to try + * + * @var array + */ + protected static $backends = array( + 'notifications_push', + ); + /** + * Backend to use + * + * @var egw_json_push_backend + */ + protected static $backend; + + /** + * account_id we are pushing too + * + * @var int + */ + protected $account_id; + + /** + * + * @param int $account_id account_id of user to push to + */ + public function __construct($account_id) + { + $this->account_id = $account_id; + } + + /** + * Adds any type of data to the message + * + * @param string $key + * @param mixed $data + * @throws egw_json_push_exception_not_online if $account_id is not online + */ + protected function addGeneric($key, $data) + { + if (!isset(self::$backend)) + { + foreach(self::$backends as $class) + { + if (class_exists($class)) + { + try { + self::$backend = new $class; + break; + } + catch (Exception $e) { + // ignore exception + unset($e, self::$backend); + } + } + } + if (!isset(self::$backend)) + { + throw new egw_json_push_exception_not_online('No valid push-backend found!'); + } + } + self::$backend->addGeneric($this->account_id, $key, $data); + } +} + +/** + * Interface for push backends + */ +interface egw_json_push_backend +{ + /** + * Adds any type of data to the message + * + * @param int $account_id account_id to push message too + * @param string $key + * @param mixed $data + * @throws egw_json_push_exception_not_online if $account_id is not online + */ + public function addGeneric($account_id, $key, $data); +} + +/** + * Exception thrown, if message can not be pushed + */ +class egw_json_push_exception_not_online extends egw_exception_not_found +{ + +} \ No newline at end of file