From c2a3c1912476779da59aeaf6dfd4cab0ca82a812 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Wed, 24 Jan 2018 19:02:35 +0100 Subject: [PATCH] W.I.P restructuring of Notifications actions --- calendar/inc/class.calendar_boupdate.inc.php | 22 ++--------- calendar/inc/class.calendar_hooks.inc.php | 31 ++++++++++++++++ calendar/setup/setup.inc.php | 1 + notifications/inc/class.notifications.inc.php | 37 +++++++------------ .../inc/class.notifications_ajax.inc.php | 12 +++++- .../inc/class.notifications_email.inc.php | 5 ++- .../inc/class.notifications_iface.inc.php | 3 +- .../inc/class.notifications_jpopup.inc.php | 5 ++- .../inc/class.notifications_popup.inc.php | 11 ++++-- .../inc/class.notifications_winpopup.inc.php | 5 ++- notifications/js/notificationajaxpopup.js | 9 ++--- notifications/setup/setup.inc.php | 2 +- notifications/setup/tables_current.inc.php | 3 +- notifications/setup/tables_update.inc.php | 11 ++++++ 14 files changed, 96 insertions(+), 61 deletions(-) diff --git a/calendar/inc/class.calendar_boupdate.inc.php b/calendar/inc/class.calendar_boupdate.inc.php index 708e29faff..2c7647a55d 100644 --- a/calendar/inc/class.calendar_boupdate.inc.php +++ b/calendar/inc/class.calendar_boupdate.inc.php @@ -1050,25 +1050,9 @@ class calendar_boupdate extends calendar_bo if ($method =='REQUEST') { // Add ACCEPT|REHECT|TENTATIVE actions - $notification->set_popupactions(array( - array( - 'id' => 'A', - 'caption' => lang('Accept'), - 'icon' => 'accepted', - 'onExecute' => 'egw().json("calendar.calendar_uiforms.ajax_status",['.$event['id'].','.$userid.','.'"A"'.']).sendRequest(true);' - ), - array( - 'id' => 'R', - 'caption' => lang('Reject'), - 'icon' => 'rejected', - 'onExecute' => 'egw().json("calendar.calendar_uiforms.ajax_status",['.$event['id'].','.$userid.','.'"R"'.']).sendRequest(true);' - ), - array( - 'id' => 'T', - 'caption' => lang('Tentative'), - 'icon' => 'tentative', - 'onExecute' => 'egw().json("calendar.calendar_uiforms.ajax_status",['.$event['id'].','.$userid.','.'"T"'.']).sendRequest(true);' - ) + $notification->set_popupdata('calendar', array( + 'event_id' => $event['id'], + 'user_id' => $userid )); } $notification->set_popupmessage($subject."\n\n".$notify_body."\n\n".$details['description']."\n\n".$details_body."\n\n"); diff --git a/calendar/inc/class.calendar_hooks.inc.php b/calendar/inc/class.calendar_hooks.inc.php index 71bf9b4722..f239b774d7 100644 --- a/calendar/inc/class.calendar_hooks.inc.php +++ b/calendar/inc/class.calendar_hooks.inc.php @@ -864,6 +864,37 @@ END:VALARM'; 'popup' => Link::get_registry('calendar', 'edit_popup') ); } + + /** + * Method to construct notifications actions + * + * @param type $params + * @return type + */ + public static function notifications_actions ($params) + { + Api\Translation::add_app('calendar'); + return array( + array( + 'id' => 'A', + 'caption' => lang('Accept'), + 'icon' => 'accepted', + 'onExecute' => 'egw().json("calendar.calendar_uiforms.ajax_status",['.$params['data']['event_id'].','.$params['data']['user_id'].','.'"A"'.']).sendRequest(true);' + ), + array( + 'id' => 'R', + 'caption' => lang('Reject'), + 'icon' => 'rejected', + 'onExecute' => 'egw().json("calendar.calendar_uiforms.ajax_status",['.$params['data']['event_id'].','.$params['data']['user_id'].','.'"R"'.']).sendRequest(true);' + ), + array( + 'id' => 'T', + 'caption' => lang('Tentative'), + 'icon' => 'tentative', + 'onExecute' => 'egw().json("calendar.calendar_uiforms.ajax_status",['.$params['data']['event_id'].','.$params['data']['user_id'].','.'"T"'.']).sendRequest(true);' + ) + ); + } } // Not part of the class, since config hooks are still using the old style diff --git a/calendar/setup/setup.inc.php b/calendar/setup/setup.inc.php index e5f4dfad3a..7ab41ec552 100755 --- a/calendar/setup/setup.inc.php +++ b/calendar/setup/setup.inc.php @@ -46,6 +46,7 @@ $setup_info['calendar']['hooks']['export_limit'] = 'calendar_hooks::getAppExport $setup_info['calendar']['hooks']['acl_rights'] = 'calendar_hooks::acl_rights'; $setup_info['calendar']['hooks']['categories'] = 'calendar_hooks::categories'; $setup_info['calendar']['hooks']['mail_import'] = 'calendar_hooks::mail_import'; +$setup_info['calendar']['hooks']['notifications_actions'] = 'calendar_hooks::notifications_actions'; /* Dependencies for this app to work */ $setup_info['calendar']['depends'][] = array( diff --git a/notifications/inc/class.notifications.inc.php b/notifications/inc/class.notifications.inc.php index ec24cd9c0d..c96de02177 100644 --- a/notifications/inc/class.notifications.inc.php +++ b/notifications/inc/class.notifications.inc.php @@ -155,10 +155,10 @@ class notifications { protected $popup_links = array(); /** - * array with objects of actions + * array with objects of data * @var array */ - protected $popup_actions = array(); + protected $popup_data = array(); /** * array with objects of attachments @@ -327,7 +327,6 @@ class notifications { public function set_popupmessage($_message) { //popup requires html if(strlen($_message) == strlen(strip_tags($_message))) $_message = self::plain2html($_message); - if ($this->popup_actions) $_message .= '
'; $this->message_popup = $_message; return true; } @@ -550,8 +549,9 @@ class notifications { { if (!empty($this->popupsubject)) $lsubject = $this->popupsubject; if ($this->popup_links) $llinks = $this->popup_links; + if (is_array($this->popup_data)) $popup_data = $this->popup_data; } - $obj->send($this->prepend_message($messages, $prepend_message), $lsubject, $llinks, $this->attachments); + $obj->send($this->prepend_message($messages, $prepend_message), $lsubject, $llinks, $this->attachments, $popup_data); } catch (Exception $exception) { $backend_errors[] = $notification_backend.' failed: '.$exception->getMessage(); @@ -786,31 +786,20 @@ class notifications { } } - /** - * Add action button to popup message - * @param array $_action - * - * @return boolean - */ - public function add_popupaction($_action) { - if(!is_array($_action)) { return false; } - $this->popup_actions[] = (object)$_action; - return true; - } /** - * Set popup actions + * Set popup data * - * @param array $_actions + * @param string $_appname + * @param array $_data * @return boolean */ - public function set_popupactions($_actions) { - $this->popup_actions = array(); - foreach($_actions as $action) { - if(is_array($action)) { - $this->add_popupaction($action); - } - } + public function set_popupdata($_appname, $_data) { + $this->popup_data = array( + 'appname' => $_appname, + 'data' => $_data + ); + return true; } } diff --git a/notifications/inc/class.notifications_ajax.inc.php b/notifications/inc/class.notifications_ajax.inc.php index db69b40e5e..52ed7c11ac 100644 --- a/notifications/inc/class.notifications_ajax.inc.php +++ b/notifications/inc/class.notifications_ajax.inc.php @@ -185,6 +185,15 @@ class notifications_ajax { if ($rs->NumRows() > 0) { foreach ($rs as $notification) { $message = null; + $data = json_decode($notification['notify_data'], true); + if ($data['appname']) + { + $_actions = Api\Hooks::process (array( + 'location' => 'notifications_actions', + 'data' => $data['data'] + ), $data['appname'], true); + $actions = $_actions[$data['appname']]; + } if($browserNotify) { $message = $notification['notify_message']; @@ -204,7 +213,8 @@ class notifications_ajax { $message3, $notification['notify_status'], $notification['notify_created'], - new DateTime()) + new DateTime(), + is_array($actions)?$actions:NULL) ); } diff --git a/notifications/inc/class.notifications_email.inc.php b/notifications/inc/class.notifications_email.inc.php index 80684582e5..e8532ea327 100644 --- a/notifications/inc/class.notifications_email.inc.php +++ b/notifications/inc/class.notifications_email.inc.php @@ -86,9 +86,12 @@ class notifications_email implements notifications_iface { * @param string $_subject * @param array $_links * @param array $_attachments + * @param array $_data */ - public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false) + public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false) { + unset ($_data); + $body_plain = $_messages['plain'].$this->render_links($_links, false, $this->preferences->external_mailclient); $body_html = "\n".$_messages['html'].$this->render_links($_links, true, $this->preferences->external_mailclient)."\n\n"; diff --git a/notifications/inc/class.notifications_iface.inc.php b/notifications/inc/class.notifications_iface.inc.php index 240cbdde1e..2a7f4f0a52 100644 --- a/notifications/inc/class.notifications_iface.inc.php +++ b/notifications/inc/class.notifications_iface.inc.php @@ -33,6 +33,7 @@ interface notifications_iface { * @param string $_subject * @param array $_links * @param array $_attachments + * @param array $_data */ - public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false); + public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false); } diff --git a/notifications/inc/class.notifications_jpopup.inc.php b/notifications/inc/class.notifications_jpopup.inc.php index d6d1ae6697..ee53bd5416 100755 --- a/notifications/inc/class.notifications_jpopup.inc.php +++ b/notifications/inc/class.notifications_jpopup.inc.php @@ -91,10 +91,11 @@ class notifications_jpopup implements notifications_iface * @param string $_subject * @param array $_links * @param array $_attachments + * @param array $_data */ - public function send(array $_messages, $_subject=false, $_links=false, $_attachments=false) + public function send(array $_messages, $_subject=false, $_links=false, $_attachments=false, $_data = false) { - unset($_attachments); // not used + unset($_attachments, $_data); // not used $jmessage = array(); diff --git a/notifications/inc/class.notifications_popup.inc.php b/notifications/inc/class.notifications_popup.inc.php index c6e263ffc6..6d45dd76ab 100644 --- a/notifications/inc/class.notifications_popup.inc.php +++ b/notifications/inc/class.notifications_popup.inc.php @@ -99,8 +99,9 @@ class notifications_popup implements notifications_iface { * @param string $_subject * @param array $_links * @param array $_attachments + * @param array $_data */ - public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false) + public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false) { unset($_attachments); // not used @@ -109,7 +110,7 @@ class notifications_popup implements notifications_iface { .(isset($_messages['popup'])&&!empty($_messages['popup'])?$_messages['popup']:$_messages['html']) .$this->render_links($_links); - $this->save( $message ); + $this->save($message, $_data); } /** @@ -117,12 +118,14 @@ class notifications_popup implements notifications_iface { * * @param string $_message * @param array $_user_sessions + * @param array $_data */ - private function save( $_message ) { + private function save($_message, $_data) { $result = $this->db->insert( self::_notification_table, array( 'account_id' => $this->recipient->account_id, 'notify_message' => $_message, - 'notify_type' => self::_type + 'notify_type' => self::_type, + 'notify_data' => is_array($_data) ? json_encode($_data) : NULL ), false,__LINE__,__FILE__,self::_appname); if ($result === false) throw new Exception("Can't save notification into SQL table"); } diff --git a/notifications/inc/class.notifications_winpopup.inc.php b/notifications/inc/class.notifications_winpopup.inc.php index 6ce4e7ec32..fb7cb63e6a 100644 --- a/notifications/inc/class.notifications_winpopup.inc.php +++ b/notifications/inc/class.notifications_winpopup.inc.php @@ -101,10 +101,11 @@ class notifications_winpopup implements notifications_iface { * @param string $_subject * @param array $_links * @param array $_attachments + * @param array $_data */ - public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false) + public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false) { - unset($_links, $_attachments); // not used + unset($_links, $_attachments, $_data); // not used $user_sessions = array(); foreach (Api\Session::session_list(0, 'asc', 'session_dla', true) as $session) { diff --git a/notifications/js/notificationajaxpopup.js b/notifications/js/notificationajaxpopup.js index 2bf24c3151..655b498977 100644 --- a/notifications/js/notificationajaxpopup.js +++ b/notifications/js/notificationajaxpopup.js @@ -243,7 +243,7 @@ } } ).addClass('et2_link'); - if (notifymessages[id]['data'] && notifymessages[id]['data']['actions']) + if (notifymessages[id]['data'] && notifymessages[id]['data']['actions'] && notifymessages[id]['data']['actions'].length > 0) { var $actions_container = jQuery(document.createElement('div')).addClass('egwpopup_actions_container'); for(var action in notifymessages[id].data.actions) @@ -322,7 +322,7 @@ */ notifications.prototype.clickOnMessage = function (_node, _event){ // Do not run the click handler if it's been already expanded - if (_node[0].hasClass('egwpopup_expanded')) return; + if (_node[0].hasClass('egwpopup_expanded') || jQuery(_event.target).hasClass('et2_button')) return; this.message_seen(_node, _event); var $node = jQuery(_node[0][0].cloneNode()); if ($node) @@ -432,7 +432,7 @@ * @return undefined */ notifications.prototype.append = function(_id, _message, _browser_notify, - _status, _created, _current) { + _status, _created, _current, _actions) { var data = this.getData(_message); // Prevent the same thing popping up multiple times notifymessages[_id] = { @@ -442,6 +442,7 @@ created: _created, current: _current }; + if (_actions && _actions.length > 0) notifymessages[_id]['data']['actions'] = _actions; // Notification API if(_browser_notify && !_status) { @@ -488,14 +489,12 @@ notifications.prototype.getData = function (_message) { var dom = jQuery(document.createElement('div')).html(_message);; var link = dom.find('div[data-id],div[data-url]'); - var actions = dom.find('div[data-actions]'); var data = { message: dom.text(), title: link.text(), icon: link.find('img').attr('src') }; jQuery.extend(data,link.data()); - if (actions.data()) jQuery.extend(data,actions.data()); return typeof data == 'object'? data: {}; }; diff --git a/notifications/setup/setup.inc.php b/notifications/setup/setup.inc.php index d0d6aee9ec..7aa3e17f9b 100644 --- a/notifications/setup/setup.inc.php +++ b/notifications/setup/setup.inc.php @@ -14,7 +14,7 @@ if (!defined('NOTIFICATION_APP')) } $setup_info[NOTIFICATION_APP]['name'] = NOTIFICATION_APP; -$setup_info[NOTIFICATION_APP]['version'] = '17.1'; +$setup_info[NOTIFICATION_APP]['version'] = '17.1.001'; $setup_info[NOTIFICATION_APP]['app_order'] = 1; $setup_info[NOTIFICATION_APP]['tables'] = array('egw_notificationpopup'); $setup_info[NOTIFICATION_APP]['enable'] = 2; diff --git a/notifications/setup/tables_current.inc.php b/notifications/setup/tables_current.inc.php index c02ceff0b7..0a93225dae 100644 --- a/notifications/setup/tables_current.inc.php +++ b/notifications/setup/tables_current.inc.php @@ -17,7 +17,8 @@ $phpgw_baseline = array( 'notify_message' => array('type' => 'varchar','precision' => '16384','comment' => 'notification message'), 'notify_created' => array('type' => 'timestamp','meta' => 'timestamp','default' => 'current_timestamp','comment' => 'creation time of notification'), 'notify_type' => array('type' => 'ascii','precision' => '32','comment' => 'notification type'), - 'notify_status' => array('type' => 'varchar','precision' => '32','comment' => 'notification status') + 'notify_status' => array('type' => 'varchar','precision' => '32','comment' => 'notification status'), + 'notify_data' => array('type' => 'varchar','precision' => '4096','comment' => 'notification data') ), 'pk' => array('notify_id'), 'fk' => array(), diff --git a/notifications/setup/tables_update.inc.php b/notifications/setup/tables_update.inc.php index 70d52d2c5c..c10bb7bc38 100644 --- a/notifications/setup/tables_update.inc.php +++ b/notifications/setup/tables_update.inc.php @@ -137,3 +137,14 @@ function notifications_upgrade16_1() return $GLOBALS['setup_info']['notifications']['currentver'] = '17.1'; } + +function notifications_upgrade17_1() +{ + $GLOBALS['egw_setup']->oProc->AddColumn('egw_notificationpopup','notify_data',array( + 'type' => 'varchar', + 'precision' => '4096', + 'comment' => 'notification actions' + )); + + return $GLOBALS['setup_info']['notifications']['currentver'] = '17.1.001'; +} \ No newline at end of file