From 00a516b0d3732ee6fb797bacd5b8e05e91fc70ab Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 9 Feb 2015 01:05:33 -0800 Subject: [PATCH] Support for admin-ui event triggers. Plugin-builder example added. --- e107_admin/plugin.php | 15 ++++++++++- e107_admin/wmessage.php | 2 ++ e107_handlers/admin_ui.php | 53 +++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/e107_admin/plugin.php b/e107_admin/plugin.php index 201204fcd..257de6710 100644 --- a/e107_admin/plugin.php +++ b/e107_admin/plugin.php @@ -2164,7 +2164,7 @@ class pluginBuilder function __construct() { $this->special['checkboxes'] = array('title'=> '','type' => null, 'data' => null, 'width'=>'5%', 'thclass' =>'center', 'forced'=> TRUE, 'class'=>'center', 'toggle' => 'e-multiselect', 'fieldpref'=>true); - $this->special['options'] = array( 'title'=> LAN_OPTIONS, 'type' => null, 'data' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center last', 'forced'=>TRUE, 'fieldpref'=>true); + $this->special['options'] = array( 'title'=> 'LAN_OPTIONS', 'type' => null, 'data' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center last', 'forced'=>TRUE, 'fieldpref'=>true); if(vartrue($_GET['newplugin'])) { @@ -3372,12 +3372,24 @@ $text .= " { continue; } + + + foreach($vars['fields'] as $key=>$val) + { + if($val['type'] == 'image' && empty($val['readParms'])) + { + $vars['fields'][$key]['readParms'] = 'thumb=80x80'; // provide a thumbnail preview by default. + } + } + + $FIELDS = str_replace($srch,$repl,var_export($vars['fields'],true)); $FIELDS = preg_replace("#('([A-Z0-9_]*?LAN[_A-Z0-9]*)')#","$2",$FIELDS); // remove quotations from LANs. $FIELDPREF = array(); foreach($vars['fields'] as $k=>$v) { + if(isset($v['fieldpref']) && $k != 'checkboxes' && $k !='options') { $FIELDPREF[] = "'".$k."'"; @@ -3392,6 +3404,7 @@ class ".$table." extends e_admin_ui protected \$pluginTitle = '".$pluginTitle."'; protected \$pluginName = '".$vars['pluginName']."'; + // protected \$eventName = '".$vars['pluginName']."-".$vars['table']."'; // remove comment to enable event triggers in admin. protected \$table = '".$vars['table']."'; protected \$pid = '".$vars['pid']."'; protected \$perPage = 10; diff --git a/e107_admin/wmessage.php b/e107_admin/wmessage.php index 89b225685..34d121bbd 100644 --- a/e107_admin/wmessage.php +++ b/e107_admin/wmessage.php @@ -59,11 +59,13 @@ class generic_ui extends e_admin_ui protected $pluginTitle = WMLAN_00; protected $pluginName = 'core'; + protected $eventName = 'wmessage'; protected $table = 'generic'; protected $pid = 'gen_id'; protected $perPage = 10; protected $batchDelete = true; protected $batchCopy = true; + // protected $sortField = 'somefield_order'; // protected $orderStep = 10; // protected $tabs = array('Tabl 1','Tab 2'); // Use 'tab'=>0 OR 'tab'=>1 in the $fields below to enable. diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index cd17b9c29..b77266bba 100644 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -2324,6 +2324,13 @@ class e_admin_controller_ui extends e_admin_controller * @var string plugin name */ protected $pluginName; + + + /** + * @var string event name + * base event trigger name to be used. Leave blank for no trigger. + */ + protected $eventName; /** * @var string @@ -2476,6 +2483,15 @@ class e_admin_controller_ui extends e_admin_controller return $this->batchFeaturebox; } + + /** + * @return string + */ + public function getEventName() + { + return $this->eventName; + } + /** * @return string @@ -2493,6 +2509,8 @@ class e_admin_controller_ui extends e_admin_controller return deftrue($this->pluginTitle, $this->pluginTitle); } + + /** * Get Tab data * @return array @@ -3924,6 +3942,8 @@ class e_admin_controller_ui extends e_admin_controller // Scenario I - use request owned POST data - toForm already executed $model->setPostedData($_posted, null, false, false) ->save(true); + + // Scenario II - inner model sanitize //$this->getModel()->setPosted($this->convertToData($_POST, null, false, true); @@ -3931,9 +3951,19 @@ class e_admin_controller_ui extends e_admin_controller if(!$this->getModel()->hasError()) { // callback (if any) + $new_data = $model->getData(); + $id = $model->getId(); + + // Trigger Admin-ui event. + if($triggerName = $this->getEventTriggerName($_posted['etrigger_submit'])) + { + e107::getMessage()->addDebug('Admin-ui Trigger: '.$triggerName); + e107::getEvent()->trigger($triggerName, array('newData'=>$new_data,'oldData'=>$old_data,'id'=> $id)); + } + if($callbackAfter && method_exists($this, $callbackAfter)) { - $this->$callbackAfter($model->getData(), $old_data, $model->getId()); + $this->$callbackAfter($new_data, $old_data, $id); } $model->setMessages(true); //FIX - move messages (and session messages) to the default stack $this->doAfterSubmit($model->getId(), $noredirectAction); @@ -3954,6 +3984,19 @@ class e_admin_controller_ui extends e_admin_controller $model->setMessages(); return false; } + + /** Return a custom event trigger name + */ + public function getEventTriggerName($type=null) + { + if(!$plug = $this->getEventName() || empty($type)) + { + return false; + } + + return 'admin-'.strtolower($plug).'-'.strtolower($type); + + } } class e_admin_ui extends e_admin_controller_ui @@ -4594,6 +4637,14 @@ class e_admin_ui extends e_admin_controller_ui if($this->beforeDelete($data, $id)) { $check = $this->getTreeModel()->delete($id); + + if($triggerName = $this->getEventTriggerName('delete')) + { + e107::getMessage()->addDebug('Admin-ui Trigger: '.$triggerName); + e107::getEvent()->trigger($triggerName, array('newData'=>$data,'oldData'=>$data,'id'=> $id)); + } + + if($this->afterDelete($data, $id, $check)) { $this->getTreeModel()->setMessages();