]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
flash messages pour indiquer qu'une action s'est bien effectuée ou qu'il y a eu une... 67/head 0.2
authornicosomb <nicolas@loeuillet.org>
Sun, 21 Apr 2013 17:32:19 +0000 (19:32 +0200)
committernicosomb <nicolas@loeuillet.org>
Sun, 21 Apr 2013 17:32:19 +0000 (19:32 +0200)
12 files changed:
css/style.css
img/messages/close.png [new file with mode: 0755]
img/messages/cross.png [new file with mode: 0755]
img/messages/help.png [new file with mode: 0755]
img/messages/tick.png [new file with mode: 0755]
img/messages/warning.png [new file with mode: 0755]
inc/class.messages.php [new file with mode: 0644]
inc/config.php
inc/functions.php
index.php
tpl/home.html
tpl/messages.html [new file with mode: 0644]

index 7fc8f056be0b8750ee17d5e3c7981f9aa147ab26..36ebf85d2191ee4ef554ccaaaa39bba4ee68d780 100644 (file)
@@ -199,4 +199,19 @@ body.article {
     }
 }
 
-
+/*** ***/
+/*** MESSAGES ***/
+
+.messages { width: 100%; -moz-border-radius: 4px; border-radius: 4px; display: block; padding: 10px 0; margin: 10px auto 10px; clear: both; }
+.messages a.closeMessage { margin: -14px -8px 0 0; display:none; width: 16px; height: 16px; float: right; background: url(../img/messages/close.png) no-repeat; }
+/*.messages:hover a.closeMessage { visibility:visible; }*/
+.messages p { margin: 3px 0 3px 10px !important; padding: 0 10px 0 23px !important; font-size: 14px; line-height: 16px; }
+.messages.error { border: 1px solid #C42608; color: #c00 !important; background: #FFF0EF; }
+.messages.error p { background: url(../img/messages/cross.png ) no-repeat 0px 50%; color:#c00 !important; }
+.messages.success {background: #E0FBCC; border: 1px solid #6DC70C; } 
+.messages.success p { background: url(../img/messages/tick.png) no-repeat 0px 50%; color: #2B6301 !important; }
+.messages.warning { background: #FFFCD3; border: 1px solid #EBCD41; color: #000; }
+.messages.warning p { background: url(../img/messages/warning.png ) no-repeat 0px 50%; color: #5F4E01; }
+.messages.information, .messages.info { background: #DFEBFB; border: 1px solid #82AEE7; }
+.messages.information p, .messages.info p { background: url(../img/messages/help.png ) no-repeat 0px 50%; color: #064393; }
+.messages.information a { text-decoration: underline; }
\ No newline at end of file
diff --git a/img/messages/close.png b/img/messages/close.png
new file mode 100755 (executable)
index 0000000..731aa01
Binary files /dev/null and b/img/messages/close.png differ
diff --git a/img/messages/cross.png b/img/messages/cross.png
new file mode 100755 (executable)
index 0000000..1514d51
Binary files /dev/null and b/img/messages/cross.png differ
diff --git a/img/messages/help.png b/img/messages/help.png
new file mode 100755 (executable)
index 0000000..5c87017
Binary files /dev/null and b/img/messages/help.png differ
diff --git a/img/messages/tick.png b/img/messages/tick.png
new file mode 100755 (executable)
index 0000000..a9925a0
Binary files /dev/null and b/img/messages/tick.png differ
diff --git a/img/messages/warning.png b/img/messages/warning.png
new file mode 100755 (executable)
index 0000000..628cf2d
Binary files /dev/null and b/img/messages/warning.png differ
diff --git a/inc/class.messages.php b/inc/class.messages.php
new file mode 100644 (file)
index 0000000..6d515bf
--- /dev/null
@@ -0,0 +1,231 @@
+<?php
+//--------------------------------------------------------------------------------------------------
+// Session-Based Flash Messages v1.0
+// Copyright 2012 Mike Everhart (http://mikeeverhart.net)
+//
+//   Licensed under the Apache License, Version 2.0 (the "License");
+//   you may not use this file except in compliance with the License.
+//   You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+//   Unless required by applicable law or agreed to in writing, software
+//   distributed under the License is distributed on an "AS IS" BASIS,
+//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//   See the License for the specific language governing permissions and
+//      limitations under the License.
+//      
+//------------------------------------------------------------------------------
+// Description:
+//------------------------------------------------------------------------------
+//
+//     Stores messages in Session data to be easily retrieved later on.
+//     This class includes four different types of messages:
+//  - Success
+//  - Error
+//  - Warning
+//  - Information
+// 
+//  See README for basic usage instructions, or see samples/index.php for more advanced samples
+//
+//--------------------------------------------------------------------------------------------------
+// Changelog
+//--------------------------------------------------------------------------------------------------
+// 
+//     2011-05-15 - v1.0 - Initial Version
+//
+//--------------------------------------------------------------------------------------------------
+
+class Messages {
+       
+       //-----------------------------------------------------------------------------------------------
+       // Class Variables
+       //-----------------------------------------------------------------------------------------------       
+       var $msgId;
+       var $msgTypes = array( 'help', 'info', 'warning', 'success', 'error' );
+       var $msgClass = 'messages';
+       var $msgWrapper = "<div class='%s %s'><a href='#' class='closeMessage'></a>\n%s</div>\n";
+       var $msgBefore = '<p>';
+       var $msgAfter = "</p>\n";
+
+       
+       /**
+        * Constructor
+        * @author Mike Everhart
+        */
+       public function __construct() {
+       
+               // Generate a unique ID for this user and session
+               $this->msgId = md5(uniqid());
+               
+               // Create the session array if it doesnt already exist
+               if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array();
+               
+       }
+       
+       /**
+        * Add a message to the queue
+        * 
+        * @author Mike Everhart
+        * 
+        * @param  string   $type               The type of message to add
+        * @param  string   $message            The message
+        * @param  string   $redirect_to        (optional) If set, the user will be redirected to this URL
+        * @return  bool 
+        * 
+        */
+       public function add($type, $message, $redirect_to=null) {
+               
+               if( !isset($_SESSION['flash_messages']) ) return false;
+               
+               if( !isset($type) || !isset($message[0]) ) return false;
+
+               // Replace any shorthand codes with their full version
+               if( strlen(trim($type)) == 1 ) {
+                       $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type );
+               
+               // Backwards compatibility...
+               } elseif( $type == 'information' ) {
+                       $type = 'info'; 
+               }
+               
+               // Make sure it's a valid message type
+               if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' );
+               
+               // If the session array doesn't exist, create it
+               if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array();
+               
+               $_SESSION['flash_messages'][$type][] = $message;
+
+               if( !is_null($redirect_to) ) {
+                       header("Location: $redirect_to");
+                       exit();
+               }
+               
+               return true;
+               
+       }
+       
+       //-----------------------------------------------------------------------------------------------
+       // display()
+       // print queued messages to the screen
+       //-----------------------------------------------------------------------------------------------
+       /**
+        * Display the queued messages
+        * 
+        * @author Mike Everhart
+        * 
+        * @param  string   $type     Which messages to display
+        * @param  bool         $print    True  = print the messages on the screen
+        * @return mixed              
+        * 
+        */
+       public function display($type='all', $print=true) {
+               $messages = '';
+               $data = '';
+               
+               if( !isset($_SESSION['flash_messages']) ) return false;
+               
+               if( $type == 'g' || $type == 'growl' ) {
+                       $this->displayGrowlMessages();
+                       return true;
+               }
+               
+               // Print a certain type of message?
+               if( in_array($type, $this->msgTypes) ) {
+                       foreach( $_SESSION['flash_messages'][$type] as $msg ) {
+                               $messages .= $this->msgBefore . $msg . $this->msgAfter;
+                       }
+
+                       $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
+                       
+                       // Clear the viewed messages
+                       $this->clear($type);
+               
+               // Print ALL queued messages
+               } elseif( $type == 'all' ) {
+                       foreach( $_SESSION['flash_messages'] as $type => $msgArray ) {
+                               $messages = '';
+                               foreach( $msgArray as $msg ) {
+                                       $messages .= $this->msgBefore . $msg . $this->msgAfter; 
+                               }
+                               $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages);
+                       }
+                       
+                       // Clear ALL of the messages
+                       $this->clear();
+               
+               // Invalid Message Type?
+               } else { 
+                       return false;
+               }
+               
+               // Print everything to the screen or return the data
+               if( $print ) { 
+                       echo $data; 
+               } else { 
+                       return $data; 
+               }
+       }
+       
+       
+       /**
+        * Check to  see if there are any queued error messages
+        * 
+        * @author Mike Everhart
+        * 
+        * @return bool  true  = There ARE error messages
+        *               false = There are NOT any error messages
+        * 
+        */
+       public function hasErrors() { 
+               return empty($_SESSION['flash_messages']['error']) ? false : true;      
+       }
+       
+       /**
+        * Check to see if there are any ($type) messages queued
+        * 
+        * @author Mike Everhart
+        * 
+        * @param  string   $type     The type of messages to check for
+        * @return bool                   
+        * 
+        */
+       public function hasMessages($type=null) {
+               if( !is_null($type) ) {
+                       if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type];     
+               } else {
+                       foreach( $this->msgTypes as $type ) {
+                               if( !empty($_SESSION['flash_messages']) ) return true;  
+                       }
+               }
+               return false;
+       }
+       
+       /**
+        * Clear messages from the session data
+        * 
+        * @author Mike Everhart
+        * 
+        * @param  string   $type     The type of messages to clear
+        * @return bool 
+        * 
+        */
+       public function clear($type='all') { 
+               if( $type == 'all' ) {
+                       unset($_SESSION['flash_messages']); 
+               } else {
+                       unset($_SESSION['flash_messages'][$type]);
+               }
+               return true;
+       }
+       
+       public function __toString() { return $this->hasMessages();     }
+
+       public function __destruct() {
+               //$this->clear();
+       }
+
+
+} // end class
+?>
\ No newline at end of file
index cf3529cb1e8de4e198e3c7e258f06a6bfeefd135..9d4b7fae260cb9478650346f35a8a48840d90b3e 100644 (file)
@@ -28,8 +28,12 @@ require_once 'Session.class.php';
 require_once 'store/store.class.php';
 require_once 'store/sqlite.class.php';
 require_once 'store/file.class.php';
+require_once 'class.messages.php';
 
-$store = new $storage_type();
+Session::init();
+
+$store         = new $storage_type();
+$msg   = new Messages();
 
 # initialisation de RainTPL
 raintpl::$tpl_dir   = './tpl/';
@@ -37,4 +41,5 @@ raintpl::$cache_dir = './cache/';
 raintpl::$base_url  = get_poche_url();
 raintpl::configure('path_replace', false);
 raintpl::configure('debug', false);
-$tpl = new raintpl();
\ No newline at end of file
+$tpl = new raintpl();
+$tpl->assign('msg', $msg);
\ No newline at end of file
index ec5b3d6a300678518554382c83302a0ce24d5359..205f3968130dd51da4809679ca5ae2cb758479dc 100644 (file)
@@ -125,6 +125,7 @@ function prepare_url($url)
         }
     }
 
+    $msg->add('e', 'error during url preparation');
     logm('error during url preparation');
     return FALSE;
 }
@@ -236,7 +237,7 @@ function remove_directory($directory)
 
 function display_view($view, $id = 0, $full_head = 'yes')
 {
-    global $tpl, $store;
+    global $tpl, $store, $msg;
 
     switch ($view)
     {
@@ -300,7 +301,7 @@ function display_view($view, $id = 0, $full_head = 'yes')
  */
 function action_to_do($action, $url, $id = 0)
 {
-    global $store;
+    global $store, $msg;
 
     switch ($action)
     {
@@ -315,9 +316,11 @@ function action_to_do($action, $url, $id = 0)
                     if (DOWNLOAD_PICTURES) {
                         $content = filtre_picture($parametres_url['content'], $url, $last_id);
                     }
+                    $msg->add('s', 'the link has been added successfully');
                 }
             }
             else {
+                $msg->add('e', 'the link has been added successfully');
                 logm($url . ' is not a valid url');
             }
 
@@ -326,14 +329,17 @@ function action_to_do($action, $url, $id = 0)
         case 'delete':
             remove_directory(ABS_PATH . $id);
             $store->deleteById($id);
+            $msg->add('s', 'the link has been deleted successfully');
             logm('delete link #' . $id);
             break;
         case 'toggle_fav' :
             $store->favoriteById($id);
+            $msg->add('s', 'the favorite toggle has been done successfully');
             logm('mark as favorite link #' . $id);
             break;
         case 'toggle_archive' :
             $store->archiveById($id);
+            $msg->add('s', 'the archive toggle has been done successfully');
             logm('archive link #' . $id);
             break;
         default:
index f64a04187fd50563e85cecf2b174d23eb1482535..6eefd277e808647b331e42ca4c184cb03a0a38c4 100644 (file)
--- a/index.php
+++ b/index.php
@@ -10,9 +10,8 @@
 
 include dirname(__FILE__).'/inc/config.php';
 
-# initialize session
 myTool::initPhp();
-Session::init();
+
 # XSRF protection with token
 if (!empty($_POST)) {
     if (!Session::isToken($_POST['token'])) {
index 33aec78c5e487a34aa5cc84af2e1d05074dc1106..ad8819977ca863f3498dfe8708e2aca82c672f1d 100644 (file)
@@ -15,4 +15,5 @@
                 <li><img src="img/up.png" onclick="sort_links('{$view}', 'ia');" title="by date asc" /> by date <img src="img/down.png" onclick="sort_links('{$view}', 'id');" title="by date desc" /></li>
                 <li><img src="img/up.png" onclick="sort_links('{$view}', 'ta');" title="by title asc" /> by title <img src="img/down.png" onclick="sort_links('{$view}', 'td');" title="by title desc" /></li>
             </ul>
-            {/if}
\ No newline at end of file
+            {/if}
+            {include="messages"}
\ No newline at end of file
diff --git a/tpl/messages.html b/tpl/messages.html
new file mode 100644 (file)
index 0000000..87af259
--- /dev/null
@@ -0,0 +1 @@
+<div id="messages"><?php echo $msg->display(); ?></div>
\ No newline at end of file