phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
JsUtils.php
Go to the documentation of this file.
1<?php
3
5use Ubiquity\controllers\Startup;
6use Ubiquity\utils\http\URequest;
7use Ubiquity\security\csp\ContentSecurityManager;
8use Ubiquity\utils\http\UResponse;
9
10class JsUtils extends \Ajax\JsUtils {
11
18 protected function _open_script($src = '') {
19 $str = '<script ';
20 if (($this->params['csp'] ?? false) === 'nonce' && ContentSecurityManager::isStarted()) {
21 $nonce = ContentSecurityManager::getNonce('jsUtils');
22 $str .= ' nonce="' . $nonce . '" ';
23 }
24 $str .= ($src == '') ? '>' : ' src="' . $src . '">';
25 return $str;
26 }
27
28 public function inline($script, $cdata = true) {
29 if (($this->params['csp'] ?? false) === 'hash' && ContentSecurityManager::isStarted()) {
30 $script = ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
31 ContentSecurityManager::getHash('jsUtils', $script);
32 }
33 return $this->_open_script() . $script . $this->_close_script();
34 }
35
36 public function getUrl($url) {
37 return URequest::getUrl($url);
38 }
39
40 public function addViewElement($identifier, $content, &$view) {
41 $controls = $view->getVar("q");
42 if (isset($controls) === false) {
43 $controls = array();
44 }
45 $controls[$identifier] = $content;
46 $view->setVar("q", $controls);
47 }
48
49 public function createScriptVariable(&$view, $view_var, $output) {
50 $view->setVar($view_var, $output);
51 }
52
53 public function forward($initialController, $controller, $action, $params = array()) {
54 return $initialController->forward($controller, $action, $params, true, true, true);
55 }
56
57 public function renderContent($initialControllerInstance, $viewName, $params = NULL) {
58 return $initialControllerInstance->loadView($viewName, $params, true);
59 }
60
61 public function fromDispatcher($dispatcher) {
62 return Startup::$urlParts;
63 }
64
75 public function renderView($viewName, $parameters = [], $asString = false) {
76 if (isset($this->injected)) {
77 $view = $this->injected->getView();
78 $this->compile($view);
79 return $this->injected->loadView($viewName, $parameters, $asString);
80 }
81 throw new \Exception(get_class() . " instance is not properly instancied : you omitted the second parameter \$controller!");
82 }
83
93 public function renderDefaultView($parameters = [], $asString = false) {
94 return $this->renderView($this->injected->getDefaultViewName(), $parameters, $asString);
95 }
96
106 public function execJSFromFile($jsFile, $parameters = [], $immediatly = true) {
107 if (isset($this->injected)) {
108 $view = $this->injected->getView();
109 if (isset($parameters))
110 $view->setVars($parameters);
111 $js = $view->render($jsFile . '.js', true);
112 return $this->exec($js, $immediatly);
113 }
114 throw new \Exception(get_class() . " instance is not properly instancied : you omitted the second parameter \$controller!");
115 }
116
124 public static function diSemantic($controller, $options = [
125 'defer' => true,
126 'gc' => true
127 ]) {
128 $jquery = new JsUtils($options, $controller);
129 $jquery->semantic(new \Ajax\Semantic());
130 $jquery->setAjaxLoader("<div class=\"ui active centered inline text loader\">Loading</div>");
131 return $jquery;
132 }
133
141 public static function diBootstrap($controller, $options = [
142 'defer' => true,
143 'gc' => true
144 ]) {
145 $jquery = new JsUtils($options, $controller);
146 $jquery->bootstrap(new \Ajax\Bootstrap());
147 $jquery->setAjaxLoader("<div class=\"d-flex justify-content-center\"><div class=\"spinner-border\" role=\"status\"><span class=\"sr-only\">Loading...</span></div></div>");
148 return $jquery;
149 }
150
160 public function ajaxValidationRule(string $ruleName,string $checkUrl,string $params='{_value:value}',string $method='post'): void {
161 $this->exec(Rule::ajax($this, $ruleName, $checkUrl, $params, 'result=data.result;', $method), true);
162 }
163
169 public function checkValidationRule($callback): void {
170 if (URequest::isPost()) {
171 $result = [];
172 UResponse::asJSON();
173 $value = $_POST['_value'];
174 $result['result'] = $callback($value);
175 echo \json_encode($result);
176 }
177 }
178}
JQuery PHP library.
Definition JsUtils.php:23
compile(&$view=NULL, $view_var='script_foot', $script_tags=TRUE)
gather together all script needing to be output
Definition JsUtils.php:318
exec($js, $immediatly=false)
Executes the code $js.
ajax($method, $url, $responseElement='', $parameters=[])
Performs an ajax request.
_close_script($extra="\n")
Outputs an closing </script>
renderView($viewName, $parameters=[], $asString=false)
Performs jQuery compilation and displays a view.
Definition JsUtils.php:75
fromDispatcher($dispatcher)
Collects url parts from the request dispatcher : controllerName, actionName, parameters Used internal...
Definition JsUtils.php:61
getUrl($url)
Generates an URL Used internally by phpMv.
Definition JsUtils.php:36
checkValidationRule($callback)
Checks a validation rule declared with @ajaxValidationRule.
Definition JsUtils.php:169
renderContent($initialControllerInstance, $viewName, $params=NULL)
render the content of an existing view : $viewName and set the response to the modal content Used int...
Definition JsUtils.php:57
forward($initialController, $controller, $action, $params=array())
Forwards to.
Definition JsUtils.php:53
createScriptVariable(&$view, $view_var, $output)
Creates the script variable script_foot Used internally by phpMv.
Definition JsUtils.php:49
static diBootstrap($controller, $options=['defer'=> true, 'gc'=> true])
Returns an instance of JsUtils initialized with Bootstrap (for di injection)
Definition JsUtils.php:141
renderDefaultView($parameters=[], $asString=false)
Performs jQuery compilation and displays the default view.
Definition JsUtils.php:93
_open_script($src='')
Outputs an opening <script>
Definition JsUtils.php:18
execJSFromFile($jsFile, $parameters=[], $immediatly=true)
Loads and eventually executes a jsFile with php parameters, using the default template engine.
Definition JsUtils.php:106
static diSemantic($controller, $options=['defer'=> true, 'gc'=> true])
Returns an instance of JsUtils initialized with Semantic (for di injection)
Definition JsUtils.php:124
addViewElement($identifier, $content, &$view)
Adds the array of controls q in the $view element Used internally by phpMv.
Definition JsUtils.php:40
ajaxValidationRule(string $ruleName, string $checkUrl, string $params='{_value:value}', string $method='post')
Adds a new semantic validation ajax rule.
Definition JsUtils.php:160