From dbf987298db4f7e9db1a90a80322451eb8151b8a Mon Sep 17 00:00:00 2001
From: Cameron <cameron@e107coders.org>
Date: Tue, 18 Jun 2013 20:21:10 -0700
Subject: [PATCH] Updated e_notify.php to v2.x spec.

---
 e107_admin/notify.php                         | 86 +++++++++++++++----
 e107_admin/update_routines.php                |  1 +
 e107_handlers/event_class.php                 | 17 ++++
 e107_handlers/notify_class.php                | 24 +++++-
 e107_plugins/chatbox_menu/e_notify.php        | 46 +++++++---
 .../languages/English/English_global.php      |  1 +
 6 files changed, 145 insertions(+), 30 deletions(-)

diff --git a/e107_admin/notify.php b/e107_admin/notify.php
index 848958dee..1b9ccdb29 100644
--- a/e107_admin/notify.php
+++ b/e107_admin/notify.php
@@ -30,7 +30,9 @@ require_once('auth.php');
 require_once(e_HANDLER.'userclass_class.php');
 
 $frm = e107::getForm();
+
 $nc = new notify_config;
+
 $uc = new user_class;
 $mes = e107::getMessage();
 
@@ -55,6 +57,7 @@ if (isset($_POST['update']))
 
  //	$ns -> tablerender($message,"<div style='text-align:center'>".$message."</div>");
 }
+
 $nc -> config();
 
 
@@ -62,8 +65,9 @@ class notify_config
 {
 	var $notify_prefs;
 	var $changeList = array();
+	var $pluginConfig = array();
 
-	function notify_config() 
+	function __construct() 
 	{
 		global $sysprefs, $eArrayStorage;
 		$ns = e107::getRender();
@@ -79,33 +83,62 @@ class notify_config
 		// load every e_notify.php file.
 		if($pref['e_notify_list'])
 		{
-	        foreach($pref['e_notify_list'] as $val)
+	        foreach($pref['e_notify_list'] as $val) // List of available e_notify.php files. 
 			{
-					if (!isset($this -> notify_prefs['plugins'][$val]))
+				//	if (!isset($this->notify_prefs['plugins'][$val]))
 					{
+
 						$this -> notify_prefs['plugins'][$val] = TRUE;
+						
 						if (is_readable(e_PLUGIN.$val."/e_notify.php"))
 						{
 							require_once(e_PLUGIN.$val.'/e_notify.php');
-							foreach ($config_events as $event_id => $event_text)
-					   		{
-								$this -> notify_prefs['event'][$event_id] = array('class' => '255', 'email' => '');
+							
+							if(class_exists($val."_notify")) // new v2.x 
+							{
+								$legacy = 0; // Newe. 
+								$config_events = array();
+								
+								$data = e107::callMethod($val."_notify", 'config');
+								
+								$config_category = str_replace("_menu","",ucfirst($val))." Events";
+								
+								foreach($data as $v)
+								{
+									$func = $v['function'];
+									$config_events[$func] = $v['name'];	
+								}
+								
 							}
+							else
+							{
+								$legacy = 1;	// Legacy Mode. 
+							}
+							
+					//		foreach ($config_events as $event_id => $event_text)
+					//   		{
+							//	$this -> notify_prefs['event'][$event_id] = array('class' => '255', 'email' => '', 'plugin'=> $val);
+								
+					//		}
+							$this->pluginConfig[$val] = array('category' => $config_category, 'events' => $config_events, 'legacy'=> $legacy);
 							$recalibrate = true;
 						}
 					}
 			}
 		}
-
-
+		
+	//	print_a($this->pluginConfig);
+		
 		if ($recalibrate) 
 		{
 			$s_prefs = $tp -> toDB($this -> notify_prefs);
 			$s_prefs = $eArrayStorage -> WriteArray($s_prefs);
-			$sql -> db_Update("core", "e107_value='".$s_prefs."' WHERE e107_name='notify_prefs'");
+		//	$sql -> db_Update("core", "e107_value='".$s_prefs."' WHERE e107_name='notify_prefs'");
 		}
 	}
 
+
+
 	function config()
 	{
 		//global $ns, $rs, $frm, $emessage;
@@ -212,14 +245,15 @@ class notify_config
 		</fieldset>
 		</div>";
 
-
-
-		foreach ($this -> notify_prefs['plugins'] as $plugin_id => $plugin_settings)
+		foreach ($this->notify_prefs['plugins'] as $plugin_id => $plugin_settings)
 		{
             if(is_readable(e_PLUGIN.$plugin_id.'/e_notify.php'))
 			{
-				require(e_PLUGIN.$plugin_id.'/e_notify.php');
-				//$text .= "</fieldset>
+				$config_category = $this->pluginConfig[$plugin_id]['category'];
+				$legacy = $this->pluginConfig[$plugin_id]['legacy'];
+				
+			//	require(e_PLUGIN.$plugin_id.'/e_notify.php');
+
 				$text .= "<div class='tab-pane' id='notify-".$plugin_id."'>
 				<fieldset id='core-notify-".str_replace(" ","_",$config_category)."'>
 		        <legend>".$config_category."</legend>
@@ -228,10 +262,13 @@ class notify_config
 		        		<col class='col-label' />
 		        		<col class='col-control' />
 		        	</colgroup>";
-				foreach ($config_events as $event_id => $event_text)
+				;
+
+				foreach ($this->pluginConfig[$plugin_id]['events'] as $event_id => $event_text)
 				{
-					$text .= $this -> render_event($event_id, $event_text);
+					$text .= $this->render_event($event_id, $event_text, $plugin_id, $legacy);
 				}
+				
 				$text .= "</table>
 				</div>";
 			}
@@ -251,16 +288,17 @@ class notify_config
 	}
 
 
-	function render_event($id, $description) 
+	function render_event($id, $description, $include='', $legacy = 0) 
 	{
 		global $uc; // $rs
 		$tp = e107::getParser();
+		$frm = e107::getForm();
 
 		$text = "
 			<tr>
 				<td >".$description.":	</td>
 				<td  class='nowrap'>
-				".$uc->uc_dropdown('event['.$id.'][class]', $this -> notify_prefs['event'][$id]['class'],"nobody,main,admin,member,classes,email","onchange=\"mail_field(this.value,'event_".$id."');\" ");
+				".$uc->uc_dropdown('event['.$id.'][class]', varset($this->notify_prefs['event'][$id]['class'],255), "nobody,main,admin,member,classes,email","onchange=\"mail_field(this.value,'event_".$id."');\" ");
 
 			if($this -> notify_prefs['event'][$id]['class'] == 'email')
 			{
@@ -275,6 +313,9 @@ class notify_config
 
 			$text .= "<input type='text' style='width:180px;$disp' class='tbox' id='event_".$id."' name='event[".$id."][email]' value=\"".$value."\" />\n";
 
+		$text .= $frm->hidden("event[".$id."][include]", $include);
+		$text .= $frm->hidden("event[".$id."][legacy]", $legacy); // function or method 
+
 		$text .= "</td>
 		</tr>";
 		return $text;
@@ -301,6 +342,8 @@ class notify_config
 		 	$pref['notify'] = FALSE;
 		}
 	  	save_prefs();
+		
+	//	print_a($this->notify_prefs);
 		/*
 		$s_prefs = $tp -> toDB($this -> notify_prefs);
 		$s_prefs = $eArrayStorage -> WriteArray($s_prefs);
@@ -333,6 +376,13 @@ class notify_config
 			$this -> notify_prefs['event'][$id]['email'] = $_POST['event'][$id]['email'];
 			$changed = TRUE;
 		}
+		
+		$this -> notify_prefs['event'][$id]['include'] 	= $_POST['event'][$id]['include'];
+		$this -> notify_prefs['event'][$id]['legacy'] 	= $_POST['event'][$id]['legacy'];
+		
+		unset($this -> notify_prefs['event'][$id]['plugin']);
+		unset($this -> notify_prefs['event'][$id]['type']);
+		
 		if ($changed)
 		{
 			$this->changeList[$id] = $this->notify_prefs['event'][$id]['class'].', '.$this->notify_prefs['event'][$id]['email'];
diff --git a/e107_admin/update_routines.php b/e107_admin/update_routines.php
index 857d00069..dfba12ade 100644
--- a/e107_admin/update_routines.php
+++ b/e107_admin/update_routines.php
@@ -1263,6 +1263,7 @@ function update_706_to_800($type='')
 						$notify_prefs['event'][$e]['class'] = e_UC_NOBODY;		// Just disable if we don't know what else to do
 				}
 				$nt_changed++;
+				$notify_prefs['event'][$e]['legacy'] = 1;
 				unset($notify_prefs['event'][$e]['type']);
 			}
 		}
diff --git a/e107_handlers/event_class.php b/e107_handlers/event_class.php
index 842b653b1..80a942425 100644
--- a/e107_handlers/event_class.php
+++ b/e107_handlers/event_class.php
@@ -39,6 +39,17 @@ class e107_event
 		}
 	}
 
+
+
+	function debug()
+	{
+		
+		print_a($this->functions);
+		print_a($this->includes);	
+		
+	}
+
+
 	/**
 	 * Trigger event
 	 * TODO - admin log for failed callback attempts?
@@ -109,6 +120,11 @@ class e107_event
 		return (isset($ret) ? $ret : false);
 	}
 
+
+
+
+
+
 	function triggerAdminEvent($type, $parms=array())
 	{
 		global $pref;
@@ -164,6 +180,7 @@ class e107_event
 	{
 		$text = ''; 
 		$e_event_list = e107::getPref('e_event_list');
+		
 		if(is_array($e_event_list))
 		{
 			foreach($e_event_list as $hook)
diff --git a/e107_handlers/notify_class.php b/e107_handlers/notify_class.php
index 9177a30c1..56e269506 100644
--- a/e107_handlers/notify_class.php
+++ b/e107_handlers/notify_class.php
@@ -33,7 +33,27 @@ class notify
 			{
 				if ($status['class'] != 255)
 				{
-					$e_event->register($id, 'notify_'.$id);
+					if($status['include']) // Plugin 
+					{
+						$include 	= e_PLUGIN.$status['include']."/e_notify.php";
+						
+						if($status['legacy'] != 1)
+						{
+							$class 		= $status['include']."_notify";
+							$method 	= $id;
+							$e_event->register($id, array($class, $method), $include);	
+						}
+						else
+						{
+							$e_event->register($id, 'notify_'.$id, $include);			
+						}
+					}
+					else // core   
+					{
+						$e_event->register($id, 'notify_'.$id);			
+					}
+					
+				
 				}
 			}
 		}
@@ -253,7 +273,7 @@ function notify_fileupload($data)
 }
 
 
-if (isset($nt->notify_prefs['plugins']))
+if (isset($nt->notify_prefs['plugins']) && e_PAGE != 'notify.php')
 {
 	foreach ($nt->notify_prefs['plugins'] as $plugin_id => $plugin_settings)
 	{
diff --git a/e107_plugins/chatbox_menu/e_notify.php b/e107_plugins/chatbox_menu/e_notify.php
index c88e05d45..206639f88 100644
--- a/e107_plugins/chatbox_menu/e_notify.php
+++ b/e107_plugins/chatbox_menu/e_notify.php
@@ -2,25 +2,19 @@
 /*
  * e107 website system
  *
- * Copyright (C) 2008-2009 e107 Inc (e107.org)
+ * Copyright (C) 2008-2013 e107 Inc (e107.org)
  * Released under the terms and conditions of the
  * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  *
- *
- *
- * $Source: /cvs_backup/e107_0.8/e107_plugins/chatbox_menu/e_notify.php,v $
- * $Revision$
- * $Date$
- * $Author$
  */
 
 if (!defined('e107_INIT')) { exit; }
-
+/*
 if(defined('ADMIN_PAGE') && ADMIN_PAGE === true)
 {
 	include_lan(e_PLUGIN."chatbox_menu/languages/".e_LANGUAGE."/".e_LANGUAGE.".php");
-	$config_category = NT_LAN_CB_1;
-	$config_events = array('cboxpost' => NT_LAN_CB_2);
+//	$config_category = NT_LAN_CB_1;
+//	$config_events = array('cboxpost' => NT_LAN_CB_2);
 }
 
 
@@ -33,4 +27,36 @@ if (!function_exists('notify_cboxpost')) {
 		$nt -> send('cboxpost', NT_LAN_CB_6, $message);
 	}
 }
+*/
 
+
+
+// v2.x Standard 
+class chatbox_menu_notify extends notify // plugin-folder + '_notify' 
+{		
+	function config()
+	{
+	//	include_lan(e_PLUGIN."chatbox_menu/languages/".e_LANGUAGE."/".e_LANGUAGE.".php"); Use English_global.php instead. 
+		
+		$config = array();
+	
+		$config[] = array(
+			'name'			=> NT_LAN_CB_2, //  "Message posted"
+			'function'		=> "cboxpost",
+			'category'		=> ''
+		);	
+		
+		return $config;
+	}
+	
+	function cboxpost($data) 
+	{
+		include_lan(e_PLUGIN."chatbox_menu/languages/".e_LANGUAGE."/".e_LANGUAGE.".php"); // Use English_global.php instead. 
+	
+		$message = NT_LAN_CB_3.': '.USERNAME.' ('.NT_LAN_CB_4.': '.e107::getIPHandler()->ipDecode($data['ip']).' )<br />';
+		$message .= NT_LAN_CB_5.':<br />'.$data['cmessage'].'<br /><br />';
+		
+		$this->send('cboxpost', NT_LAN_CB_6, $message);
+	}
+	
+}
\ No newline at end of file
diff --git a/e107_plugins/chatbox_menu/languages/English/English_global.php b/e107_plugins/chatbox_menu/languages/English/English_global.php
index 76a7701be..9b49390ce 100644
--- a/e107_plugins/chatbox_menu/languages/English/English_global.php
+++ b/e107_plugins/chatbox_menu/languages/English/English_global.php
@@ -5,6 +5,7 @@ define("LAN_PLUGIN_CHATBOX_MENU_NAME",		 	"Chatbox");
 define("LAN_PLUGIN_CHATBOX_MENU_DESCRIPTION", 	"Chatbox Menu");
 
 // Admin Log 
+//FIXME - Global LANS must begin with LAN_PLUGIN_{FOLDER_NAME}_ 
 define("LAN_AL_CHBLAN_01","Chatbox settings updated");
 define("LAN_AL_CHBLAN_02","Chatbox pruned");
 define("LAN_AL_CHBLAN_03","Chatbox posts recalculated");