diff --git a/e107_admin/notify.php b/e107_admin/notify.php index 35a11ab08..050e4fdf4 100644 --- a/e107_admin/notify.php +++ b/e107_admin/notify.php @@ -17,26 +17,24 @@ if (!getperms('O')) } e107::coreLan('notify', true); +e107::library("load", "animate.css"); +e107::js('footer-inline'," -$js = " + $('select').on('change', function() + { + var valueSelected = this.value; + valueSelected = valueSelected.replace('::','_'); + var id = $(this).attr('id'); + var targetid = '#' + id + '-' + valueSelected; + var disp = '.' + id + '-disp'; + $(disp).hide(); + $(targetid).show(); + + + }); - function mail_field(val,id) - { - if(val == 'email') - { - document.getElementById(id).style.display =''; - } - else - { - document.getElementById(id).style.display ='none'; - } - } - -"; - - -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,23 +155,29 @@ 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'); - - $config_category = str_replace("_menu","",ucfirst($val))." ".LAN_NOTIFY_01; - - foreach($data as $v) + if($data = e107::callMethod($val."_notify", 'config')) { - $func = $v['function']; - $config_events[$func] = $v['name']; + + $config_category = str_replace("_menu","",ucfirst($val))." ".LAN_NOTIFY_01; + + foreach($data as $v) + { + $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 = " - - + "; + $text .= deftrue('e_DEBUG') ? "" : ''; + $text .= ""; foreach($cat as $c=>$ev) @@ -305,7 +303,10 @@ class plugin_notify_admin_ui extends e_admin_ui $text .= "
\n"; - $tab[] = array('caption'=> $config_category, 'text'=> $text); + 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.' »'; $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 .= ""; + $inputs .= e107::callMethod($plg.'_notify',$route['field'],"event[".$id."][".$k."]",varset($this->notify_prefs['event'][$id]['recipient'])); + $inputs .= " "; + $uc->fixed_classes[$k] = $route['label'].' »'; + $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 .= "".$id.""; } $text .= " - ".$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 .= "\n"; + $text .= "\n"; + + $text .= $inputs; + $text .= $frm->hidden("event[".$id."][include]", $include); $text .= $frm->hidden("event[".$id."][legacy]", $legacy); // function or method @@ -383,89 +411,75 @@ class plugin_notify_admin_ui extends e_admin_ui } - $text .= ""; - - - - - $text .= " + $text .= " "; + 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; - } - else - { - return false; + return null; } + return e107::getConfig('notify')->updatePref($this->notify_prefs)->save(true,true,true); } - function update_event($id) + private function update_event($id) { - $changed = FALSE; + $ret = []; - if ($this -> notify_prefs['event'][$id]['class'] != $_POST['event'][$id]['class']) + $classVal = null; + + if(isset($_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; + $classVal = $_POST['event'][$id]['class']; + $ret['class'] = $_POST['event'][$id]['class']; } - $this -> notify_prefs['event'][$id]['include'] = $_POST['event'][$id]['include']; - $this -> notify_prefs['event'][$id]['legacy'] = $_POST['event'][$id]['legacy']; + 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]; + } - unset($this -> notify_prefs['event'][$id]['plugin']); - unset($this -> notify_prefs['event'][$id]['type']); + $ret['include'] = $_POST['event'][$id]['include']; + $ret['legacy'] = $_POST['event'][$id]['legacy']; - if ($changed) + if($this->notify_prefs['event'][$id] !== $ret) { - $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; + return $ret; } + + return false; + } } diff --git a/e107_handlers/notify_class.php b/e107_handlers/notify_class.php index 49d6a2ed9..30f859a93 100644 --- a/e107_handlers/notify_class.php +++ b/e107_handlers/notify_class.php @@ -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)) { diff --git a/e107_handlers/userclass_class.php b/e107_handlers/userclass_class.php index 229d95f72..5638d6656 100644 --- a/e107_handlers/userclass_class.php +++ b/e107_handlers/userclass_class.php @@ -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\n"; + + if ($text) $text = "\n\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 diff --git a/e107_plugins/_blank/e_notify.php b/e107_plugins/_blank/e_notify.php index d50d1be63..36746c705 100644 --- a/e107_plugins/_blank/e_notify.php +++ b/e107_plugins/_blank/e_notify.php @@ -42,7 +42,48 @@ class _blank_notify extends notify $this->send('customNotify', $subject, $message); } - + + // 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. + } } diff --git a/e107_tests/tests/unit/notifyTest.php b/e107_tests/tests/unit/notifyTest.php new file mode 100644 index 000000000..12d4292a9 --- /dev/null +++ b/e107_tests/tests/unit/notifyTest.php @@ -0,0 +1,102 @@ +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() + { + + }*/ + + +}