From f0070a15e4725255dad967bde76155a39d189631 Mon Sep 17 00:00:00 2001 From: nicosomb Date: Sun, 21 Apr 2013 19:32:19 +0200 Subject: [PATCH] =?utf8?q?flash=20messages=20pour=20indiquer=20qu'une=20ac?= =?utf8?q?tion=20s'est=20bien=20effectu=C3=A9e=20ou=20qu'il=20y=20a=20eu?= =?utf8?q?=20une=20erreur?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- css/style.css | 17 ++- img/messages/close.png | Bin 0 -> 662 bytes img/messages/cross.png | Bin 0 -> 655 bytes img/messages/help.png | Bin 0 -> 786 bytes img/messages/tick.png | Bin 0 -> 537 bytes img/messages/warning.png | Bin 0 -> 666 bytes inc/class.messages.php | 231 +++++++++++++++++++++++++++++++++++++++ inc/config.php | 9 +- inc/functions.php | 10 +- index.php | 3 +- tpl/home.html | 3 +- tpl/messages.html | 1 + 12 files changed, 266 insertions(+), 8 deletions(-) create mode 100755 img/messages/close.png create mode 100755 img/messages/cross.png create mode 100755 img/messages/help.png create mode 100755 img/messages/tick.png create mode 100755 img/messages/warning.png create mode 100644 inc/class.messages.php create mode 100644 tpl/messages.html diff --git a/css/style.css b/css/style.css index 7fc8f056..36ebf85d 100644 --- a/css/style.css +++ b/css/style.css @@ -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 index 0000000000000000000000000000000000000000..731aa018f240a80ed89ed48e386f8a364089e99e GIT binary patch literal 662 zcmV;H0%`q;P)z@;j(q!3lK=n!AY({UO#lFTB>(_`g8%^e{{R4h=>PzA zFaQARU;qF*m;eA5Z<1fdMgRZ;3`s;mRCwBA{Qv(y0~)Xa;xHh#0%Ap|*nJ>A2E?m? z_z1FMfB<4dGJqe5n}Im-^XJbzXU?2qxO3+YSVC1*mBGZsgn@^L=L?WKABby#_#;RT zAb=P^1^_W15bpru|MTb1|F5g73o;c2gTz2`P_P54hXEjfU-TN zkO7lFe*E}fOG^tZsiULw|J%23{~tVf@Lxqm<-eGi*#Fa~PlK(jtgHm90jUM)0qF$@ zAdmrP_U+pTw@p}B`2V_f>%bar-@g6-^5x55_Q8V(|I5qE!_|TGfb;?c5XgYHvuDpn z@h1ld$Nw!`w!r-Q|J=EA|M~g(QPj+uH47y579fC_U@?IVczJmlgoK3Pd@e372B6DO z)Bpn;OalZEh8GkR6#ielcoA&#-o1POZ`!m8%zpIf(SI{DGYl^P1Q13b>g((O|MKO_ z|HFq5{}&V#{LjwL{(se~RsX+#|NcKLEDR$M0RjkXG=gG6R8$ns2YTthfq?;>4^2?u zXaoo#l!R1ORfRVZVM|B=0fds2|NZ;-f8M-#Sd%iyRUkQ#8swx55I|Tn6EFuEoyT++I zn$b9r%cFfhHe2K68PkBu*@^<$y+7xQ$wJ~;c5aBx$R=xq*41Wo zhwQus_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9VkA~NYOKEQXsj?6a9_hcwfU$acAhJhB)zb_w@MVUEy@S zX&I>K-R!bhu3?(6bHWIg$HEl7{9g>>&l_qdd+UYb(1~BCo9LptNq&8>!yoJ3Ui(i5 zRJ|XnYBklL!{@$-7=3mJ>P@1c=7Oc79e-V7yf+%lD2!I;Y&nXBZ>=B!5?CB>LvEx6 znI%n)qqi$#X#wKB(U7XP2P=+4{b@j#r%9-K(8UqtSDk>0UKzf*HM9yqMZ1D!$2MdZ zR=`U>0zhOH1XqN?nY@AQqB7)Fp4{v&dKXvb43hZKvnN8;Po;+jY*}~*Z|W9Q0W%{D z^T}Cc<|r(Su=1K=P5>Z4 zg`et&Va}tdzBS-G-ZcO)zCWpJvGQwrHZ`@wpM420ac@bI5~KkTFfGEM3sPWO8co4^fI6lPnA)Y{ef%@{+SnoUk0+dW+*{8WvF8}}l07*qoM6N<$g7cXs A&j0`b literal 0 HcmV?d00001 diff --git a/inc/class.messages.php b/inc/class.messages.php new file mode 100644 index 00000000..6d515bf6 --- /dev/null +++ b/inc/class.messages.php @@ -0,0 +1,231 @@ +\n%s\n"; + var $msgBefore = '

'; + var $msgAfter = "

\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 diff --git a/inc/config.php b/inc/config.php index cf3529cb..9d4b7fae 100644 --- a/inc/config.php +++ b/inc/config.php @@ -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 diff --git a/inc/functions.php b/inc/functions.php index ec5b3d6a..205f3968 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -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: diff --git a/index.php b/index.php index f64a0418..6eefd277 100644 --- 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'])) { diff --git a/tpl/home.html b/tpl/home.html index 33aec78c..ad881997 100644 --- a/tpl/home.html +++ b/tpl/home.html @@ -15,4 +15,5 @@
  • by date
  • by title
  • - {/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 index 00000000..87af259b --- /dev/null +++ b/tpl/messages.html @@ -0,0 +1 @@ +
    display(); ?>
    \ No newline at end of file -- 2.41.0