Browse Source

+ UI::tryCatchView

Piotr Labudda 5 năm trước cách đây
mục cha
commit
707af89e35
1 tập tin đã thay đổi với 30 bổ sung0 xóa
  1. 30 0
      SE/se-lib/UI.php

+ 30 - 0
SE/se-lib/UI.php

@@ -830,6 +830,7 @@ class UI {
 	}
 
 	/**
+	 * Execute callback in try/catch block and view output as page layout (content inside html > body)
 	 * @param array $params
 	 * @param bool $params['showMenu']
 	 * @param bool $params['showContainer']
@@ -862,6 +863,35 @@ class UI {
 		if ($params['showContainer']) UI::endContainer();
 		UI::dol(); // UI::dol must include Theme::footer();
 	}
+	/**
+	 * Execute callback inside try/catch block.
+	 * @param array $params
+	 * @param bool $params['showContainer']
+	 * @param string $params['containerClass'] : [ 'fluid' ], default ''
+	 */
+	static function tryCatchView($callback, $params = []) {
+		$params['showContainer'] = V::get('showContainer', false, $params, 'bool');
+		$params['containerClass'] = V::get('containerClass', '', $params);
+		$args = V::get('args', [], $params, 'array');
+		if ($params['showContainer']) UI::startContainer( $params['containerClass'] ? [ 'class' => $params['containerClass'] ] : [] );
+		try {
+			// call_user_func($callback);
+			$callback($args);
+		} catch (AlertSuccessException $e) {
+			DBG::log($e);
+			UI::alert('success', $e->getMessage());
+		} catch (AlertWarningException $e) {
+			DBG::log($e);
+			UI::alert('warning', $e->getMessage());
+		} catch (AlertInfoException $e) {
+			DBG::log($e);
+			UI::alert('info', $e->getMessage());
+		} catch (Exception $e) {
+			DBG::log($e);
+			UI::alert('danger', $e->getMessage());
+		}
+		if ($params['showContainer']) UI::endContainer();
+	}
 
 	public static function startDetails($opts, $summaryChildrens) {
 		$attrs = array_reduce(array_keys($opts), function ($ret, $optionKey) use ($opts) {