1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-05 06:07:32 +02:00

Closes #4922 - Option for plugins to extend notification routing.

This commit is contained in:
Cameron
2022-12-13 18:23:48 -08:00
parent f85de1ba5b
commit 05b1b040c8
5 changed files with 296 additions and 116 deletions

View File

@@ -17,26 +17,24 @@ if (!getperms('O'))
}
e107::coreLan('notify', true);
e107::library("load", "animate.css");
e107::js('footer-inline',"
$js = "
function mail_field(val,id)
$('select').on('change', function()
{
if(val == 'email')
{
document.getElementById(id).style.display ='';
}
else
{
document.getElementById(id).style.display ='none';
}
}
";
var valueSelected = this.value;
valueSelected = valueSelected.replace('::','_');
var id = $(this).attr('id');
var targetid = '#' + id + '-' + valueSelected;
var disp = '.' + id + '-disp';
$(disp).hide();
$(targetid).show();
e107::js('inline', $js);
});
");
class plugin_notify_admin extends e_admin_dispatcher
{
@@ -48,7 +46,7 @@ class plugin_notify_admin extends e_admin_dispatcher
);
protected $adminMenu = array(
'main/config' => array('caption'=> "Email", 'perm' => '0'),
'main/config' => array('caption'=> LAN_PREFS, 'perm' => '0'),
// 'main/push' => array('caption'=> "Push (experimental)", 'perm' => '0')
);
@@ -113,36 +111,39 @@ class plugin_notify_admin_ui extends e_admin_ui
'pref_name' => array('title'=> 'name', 'type' => 'text', 'data' => 'string', 'validate' => 'regex', 'rule' => '#^[\w]+$#i', 'help' => 'allowed characters are a-zA-Z and underscore')*/
);
var $notify_prefs;
private $notify_prefs = [];
var $changeList = array();
var $pluginConfig = array();
function init()
{
if(!empty($_POST['update']))
{
if(!$this-> update())
{
e107::getMessage()->addError(LAN_UPDATED_FAILED);
}
}
$pref = e107::getPref();
$this->notify_prefs = e107::getConfig('notify')->getPref();
$this->prefCleanup();
$this->test();
if(!empty($_POST['update']))
{
if($this-> update() === null)
{
e107::getMessage()->addInfo(LAN_NO_CHANGE);
}
}
$recalibrate = FALSE;
// load every e_notify.php file.
if(!empty($pref['e_notify_list']))
{
$config_events = array();
foreach($pref['e_notify_list'] as $val) // List of available e_notify.php files.
{
$config_category = '';
// if (!isset($this->notify_prefs['plugins'][$val]))
{
@@ -154,10 +155,11 @@ class plugin_notify_admin_ui extends e_admin_ui
if(class_exists($val."_notify")) // new v2.x
{
$legacy = 0; // Newe.
$legacy = 0; // New.
$config_events = array();
$data = e107::callMethod($val."_notify", 'config');
if($data = e107::callMethod($val."_notify", 'config'))
{
$config_category = str_replace("_menu","",ucfirst($val))." ".LAN_NOTIFY_01;
@@ -166,11 +168,16 @@ class plugin_notify_admin_ui extends e_admin_ui
$func = $v['function'];
$config_events[$func] = $v['name'];
}
}
$routers = e107::callMethod($val."_notify", 'router');
}
else
{
$legacy = 1; // Legacy Mode.
$routers = [];
// $config_category = 'Other';
// $config_events = array();
}
@@ -180,11 +187,13 @@ class plugin_notify_admin_ui extends e_admin_ui
// $this -> notify_prefs['event'][$event_id] = array('class' => '255', 'email' => '', 'plugin'=> $val);
// }
$this->pluginConfig[$val] = array('category' => $config_category, 'events' => $config_events, 'legacy'=> $legacy);
$this->pluginConfig[$val] = array('category' => $config_category, 'events' => $config_events, 'legacy'=> $legacy, 'routers'=>$routers);
$recalibrate = true;
}
}
}
}
// print_a($this->pluginConfig);
@@ -221,18 +230,6 @@ class plugin_notify_admin_ui extends e_admin_ui
function pushPage()
{
e107::getMessage()->addInfo("Under Construction");
}
function test()
{
if(!empty($_POST['test']))
@@ -263,8 +260,9 @@ class plugin_notify_admin_ui extends e_admin_ui
{
$text = " <table class='table adminform'>
<colgroup>
<col class='col-label' />
<col class='col-control' />
<col class='col-label' />";
$text .= deftrue('e_DEBUG') ? "<col class='col-control' />" : '';
$text .= "<col style='width:50%' />
</colgroup>";
foreach($cat as $c=>$ev)
@@ -305,10 +303,13 @@ class plugin_notify_admin_ui extends e_admin_ui
$text .= "</table>\n";
if(!empty($config_category))
{
$tab[] = array('caption'=> $config_category, 'text'=> $text);
}
}
}
}
@@ -328,13 +329,37 @@ class plugin_notify_admin_ui extends e_admin_ui
}
function render_event($id, $description, $include='', $legacy = 0)
private function render_event($id, $description, $include='', $legacy = 0)
{
$tp = e107::getParser();
$frm = e107::getForm();
$uc = e107::getUserClass();
$uc->fixed_classes['email'] = NM_LAN_3;
$uc->fixed_classes['email'] = LAN_EMAIL.' &raquo;';
$uc->text_class_link['email'] = 'email';
$ucDropList = ['nobody','main','admin','member','classes','email'];
$inputs = '';
foreach($this->pluginConfig as $plg => $cfg)
{
if(!empty($cfg['routers']))
{
foreach($cfg['routers'] as $key => $route)
{
$k = $plg.'::'.$key;
$containerId = 'event-'.$id.'-'.$plg.'_'.$key;
$disp = (varset($this->notify_prefs['event'][$id]['class']) == $k) ? 'display:visible' : 'display:none';
$inputs .= "<span id='$containerId' class='animated fadeIn event-".$id."-disp' style='$disp'>";
$inputs .= e107::callMethod($plg.'_notify',$route['field'],"event[".$id."][".$k."]",varset($this->notify_prefs['event'][$id]['recipient']));
$inputs .= "</span> ";
$uc->fixed_classes[$k] = $route['label'].' &raquo;';
$uc->text_class_link[$k] = $k;
$ucDropList[] = $k;
}
}
}
if(defined($description))
@@ -352,14 +377,14 @@ class plugin_notify_admin_ui extends e_admin_ui
if(e_DEBUG)
if(deftrue('e_DEBUG'))
{
$text .= "<td>".$id."</td>";
}
$text .= "
<td class='form-inline nowrap'>
".$uc->uc_dropdown('event['.$id.'][class]', varset($this->notify_prefs['event'][$id]['class'], e_UC_NOBODY), "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'], e_UC_NOBODY), implode(',',$ucDropList),['id'=>'event-'.$id] /*"onchange=\"mail_field(this.value,'event_".$id."');\" "*/);
if(varset($this->notify_prefs['event'][$id]['class']) == 'email')
{
@@ -372,7 +397,10 @@ class plugin_notify_admin_ui extends e_admin_ui
$value= "";
}
$text .= "<input class='form-control' type='text' style='width:200px;$disp' class='tbox' id='event_".$id."' name='event[".$id."][email]' value=\"".$value."\" placeholder='eg. your@email.com' />\n";
$text .= "<input class='form-control animated fadeIn input-large event-".$id."-disp' type='text' style='$disp' class='tbox' id='event-".$id."-email' name='event[".$id."][email]' value=\"".$value."\" placeholder='eg. your@email.com' />\n";
$text .= $inputs;
$text .= $frm->hidden("event[".$id."][include]", $include);
$text .= $frm->hidden("event[".$id."][legacy]", $legacy); // function or method
@@ -383,90 +411,76 @@ class plugin_notify_admin_ui extends e_admin_ui
}
$text .= "</td>";
$text .= "
$text .= "</td>
</tr>";
return $text;
}
function update()
private function update()
{
$this->changeList = array();
$active = false;
$modified = [];
foreach ($_POST['event'] as $key => $value)
{
if ($this -> update_event($key))
unset($this->notify_prefs['event'][$key]['plugin']); // BC Cleanup
unset($this->notify_prefs['event'][$key]['type']); // BC Cleanup
if ($res = $this->update_event($key))
{
$active = true;
$this->notify_prefs['event'][$key] = $res;
e107::getMessage()->addDebug("Modified:".print_a($res,true));
$modified[] = $res;
}
}
// print_a($this->notify_prefs);
/*
$s_prefs = $tp -> toDB($this -> notify_prefs);
$s_prefs = $eArrayStorage -> WriteArray($s_prefs);
if($sql -> db_Update("core", "e107_value='".$s_prefs."' WHERE e107_name='notify_prefs'")!==FALSE)
*/
e107::getConfig()->set('notify',$active)->save(true,true,false);
e107::getConfig('notify')->updatePref($this->notify_prefs);
if (e107::getConfig('notify')->save(FALSE))
if(empty($modified))
{
// e107::getAdminLog()->logArrayAll('NOTIFY_01',$this->changeList);
return true;
return null;
}
else
return e107::getConfig('notify')->updatePref($this->notify_prefs)->save(true,true,true);
}
private function update_event($id)
{
$ret = [];
$classVal = null;
if(isset($_POST['event'][$id]['class']))
{
$classVal = $_POST['event'][$id]['class'];
$ret['class'] = $_POST['event'][$id]['class'];
}
if(!empty($_POST['event'][$id]['email']) && $classVal === 'email')
{
$ret['email'] = $_POST['event'][$id]['email'];
}
elseif($classVal !== null && !empty($_POST['event'][$id][$classVal]))
{
$ret['recipient'] = $_POST['event'][$id][$classVal];
}
$ret['include'] = $_POST['event'][$id]['include'];
$ret['legacy'] = $_POST['event'][$id]['legacy'];
if($this->notify_prefs['event'][$id] !== $ret)
{
return $ret;
}
return false;
}
}
function update_event($id)
{
$changed = FALSE;
if ($this -> notify_prefs['event'][$id]['class'] != $_POST['event'][$id]['class'])
{
$this -> notify_prefs['event'][$id]['class'] = $_POST['event'][$id]['class'];
$changed = TRUE;
}
if ($this -> notify_prefs['event'][$id]['email'] != $_POST['event'][$id]['email'])
{
$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'];
}
if ($this -> notify_prefs['event'][$id]['class'] != 255)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
class plugin_notify_admin_form_ui extends e_admin_form_ui

View File

@@ -31,7 +31,7 @@ class notify
if(empty($this->notify_prefs))
{
$this->notify_prefs = e107::getConfig('notify')->getPref();
$this->notify_prefs = (array) e107::getConfig('notify')->getPref();
}
}
@@ -58,7 +58,7 @@ class notify
{
$include = null;
if ($status['class'] != e_UC_NOBODY) // 255;
if (varset($status['class']) != e_UC_NOBODY) // 255;
{
if(varset($status['include'])) // Plugin
{
@@ -150,7 +150,25 @@ class notify
$blockOriginator = FALSE; // TODO: set this using a pref
$recipients = array();
if ($notifyTarget == 'email') // Single email address - that can always go immediately
if(strpos($notifyTarget, '::') !== false) // custom router @see e107_plugins/_blank/e_notify.php
{
list($class,$method) = explode('::', $notifyTarget);
if($cls = e107::getAddon($class,'e_notify'))
{
$evData = [
'id' => $id,
'subject' => $subject,
'message' => $message,
'recipient' => varset($this->notify_prefs['event'][$id]['recipient'])
];
return e107::callMethod($cls, $method, $evData);
}
return false;
}
elseif ($notifyTarget == 'email') // Single email address - that can always go immediately
{
if (!$blockOriginator || ($this->notify_prefs['event'][$id]['email'] != USEREMAIL))
{

View File

@@ -456,7 +456,7 @@ class user_class
* @param string $extra_js - can add JS handlers (e.g. 'onclick', 'onchange') if required
*/
public function uc_dropdown($fieldname, $curval = 0, $optlist = '', $extra_js = '')
public function uc_dropdown($fieldname, $curval = 0, $optlist = '', $extra_js = null)
{
$optlist = (string) $optlist;
$show_classes = self::uc_required_class_list($optlist); // Get list of classes which meet criteria
@@ -479,9 +479,9 @@ class user_class
{
$options = $extra_js;
$extra_js = '';
}
$id = $fieldname;
$class = "tbox form-control";
if(!empty($options['class']))
@@ -489,6 +489,11 @@ class user_class
$class .= " ".$options['class'];
}
if(!empty($options['id']))
{
$id = $options['id'];
}
// Inverted Classes
if(strpos($optlist, 'no-excludes') === FALSE)
@@ -515,7 +520,8 @@ class user_class
}
// Only return the select box if we've ended up with some options
if ($text) $text = "\n<select class='".$class."' name='{$fieldname}' id='{$fieldname}' {$extra_js}>\n".$text."</select>\n";
if ($text) $text = "\n<select class='".$class."' name='$fieldname' id='$id' {$extra_js}>\n".$text."</select>\n";
return $text;
}
@@ -1144,7 +1150,6 @@ class user_class
}
/**
* See if a class can be edited (in the sense of the class ID not being fixed)
* @param integer $classID - class number. Must be >= 0

View File

@@ -43,6 +43,47 @@ class _blank_notify extends notify
}
// BELOW IS OPTIONAL - R IF YOU WISH YOUR PLUGIN TO BECOME A ROUTER OF NOTIFICATIONS. eg. sending sms or messages to other platforms.
/**
* REMOVE THIS METHOD WHEN NOT IN USE.
* @return array
*/
function router()
{
$ret = [];
$ret['other_type'] = array( // 'other_type' method will be called when this notification is triggered (see method below)
'label' => "Blank Example", // label used in admin Notify area
'field' => "handle", // rendered in the admin Notify area when this option is selected. see method below.
'category' => ''
);
return $ret;
}
/**
* Custom method used to render a field in Admin Notify area.
* REMOVE THIS METHOD WHEN NOT IN USE.
* @param string $name Field name.
* @param mixed $curVal current value.
* @return string
*/
function handle($name, $curVal)
{
return e107::getForm()->text($name, $curVal, 80, ['size'=>'large','placeholder'=>'eg. account name']);
}
/**
* Custom Method to handle notification.
* REMOVE THIS METHOD WHEN NOT IN USE
* @param array $data
* @return string
*/
function other_type($data)
{
return $data; // Peform some other kind of notification and return true on success / false on error.
}
}

View File

@@ -0,0 +1,102 @@
<?php
class notifyTest extends \Codeception\Test\Unit
{
/** @var notify */
protected $nt;
protected function _before()
{
e107::getPlugin()->install('_blank');
try
{
$this->nt = $this->make('notify');
}
catch(Exception $e)
{
$this->fail($e->getMessage());
}
$this->nt->__construct();
}
function _after()
{
e107::getPlugin()->uninstall('_blank');
}
public function testSendCustom()
{
// Simulate saved pref
$this->nt->notify_prefs['event'] = array (
'custom_event' => array (
'class' => '_blank::other_type', // _blank plugin e_notify.php router()
'recipient' => 'exampleAccount',
'include' => '',
'legacy' => '0',
),
);
$expected = array (
'id' => 'custom_event',
'subject' => 'Test subject',
'message' => 'Test message',
'recipient' => 'exampleAccount',
);
$result = $this->nt->send('custom_event','Test subject','Test message');
$this->assertSame($expected, $result);
}
/*
public function testSendEmail()
{
$this->nt->notify_prefs['event'] = array (
'custom_event' => array (
'class' => 'email',
'email' => 'my@email.com',
'include' => '',
'legacy' => '0',
),
);
}
public function testSend()
{
$this->nt->notify_prefs['event'] = array (
'custom_event' =>
array (
'class' => '254', // Admin class.
'include' => '',
'legacy' => '0',
),
);
}
*/
/*
public function test__construct()
{
}
public function testRegisterEvents()
{
}
public function testGeneric()
{
}*/
}