41 abstract protected function showSimpleMessage($content, $type, $title=null,$icon=
"info", $timeout=NULL, $staticName=null):HtmlMessage;
48 $message=\ob_get_clean();
50 if ($refresh ===
true)
52 echo $this->jquery->compile($this->view);
59 if (\
sizeof($restRoutes) > 0) {
62 $result=$this->
showSimpleMessage(
"No resource Rest found. You can add a new resource.",
"",
"Rest",
"warning circle", null,
"tabsRest");
65 $result.=$this->
showSimpleMessage(\nl2br($e->getMessage()),
"error",
"Rest error",
"warning circle", null,
"tabsRest");
74 $path=@$_POST[
"path"];
75 $resource=@$_POST[
"resource"];
76 $controller=@$_POST[
"controller"];
77 $controller=\urldecode($controller);
78 $action=@$_POST[
"action"];
80 $frm=$this->jquery->semantic()->htmlForm(
"frmTester-" . $path);
81 $pathId=JString::cleanIdentifier($path);
82 $containerId=
"div-tester-" . $pathId;
83 $input=$frm->addInput(
"path", null,
"text", $path);
84 $pathField=$input->getDataField()->setIdentifier(
"path-" . $path)->addClass(
"_path");
86 $methodField=$dd->setIdentifier(
"dd-method-" . $path)->getDataField()->setProperty(
"name",
"method");
87 $methodField->setIdentifier(
"method-" . $path)->addClass(
"_method");
88 $input->addAction(
"Headers...",
"right",
"barcode")->addClass(
"basic _requestWithHeaders")->setTagName(
"div");
89 $input->addAction(
"Parameters...",
"right",
"settings")->addClass(
"basic _requestWithParams")->setTagName(
"div");
90 $btGo=$input->addAction(
"Send")->setColor(
"blue");
91 $btGo->addIcon(
"send");
92 $btGo->setIdentifier(
"btGo-" . $path);
94 $frmHeaders=
new HtmlForm(
"frm-headers-" . $path);
95 $frmParameters=
new HtmlForm(
"frm-parameters-" . $path);
97 $this->jquery->postOnClick(
"#" . $btGo->getIdentifier(), $this->
_getAdminFiles()->getAdminBaseRoute() .
"/_runRestMethod",
"{pathId: '" . $path .
"',path: $('#" . $pathField->getIdentifier() .
"').val(),method: $('#" . $methodField->getIdentifier() .
"').val(),headers:$('#" . $frmHeaders->getIdentifier() .
"').serialize(),params:$('#" . $frmParameters->getIdentifier() .
"').serialize()}",
"#" . $containerId .
" ._runRestMethod", [ ]);
98 $this->jquery->postOnClick(
"#" . $containerId .
" ._requestWithParams", $this->
_getAdminFiles()->getAdminBaseRoute() .
"/_runPostWithParams/_/parameter/rest",
"{actualParams:$('#" . $frmParameters->getIdentifier() .
"').serialize(),model: '" . $resource .
"',toUpdate:'" . $frmParameters->getIdentifier() .
"',method:$('#" . $containerId .
" ._method').val(),url:$('#" . $containerId .
" ._path').val()}",
"#modal", [
"attr" =>
"",
"hasLoader" => false ]);
99 $this->jquery->postOnClick(
"#" . $containerId .
" ._requestWithHeaders", $this->
_getAdminFiles()->getAdminBaseRoute() .
"/_runPostWithParams/_/header/rest",
"{actualParams: $('#" . $frmHeaders->getIdentifier() .
"').serialize(),model: '" . $resource .
"',toUpdate:'" . $frmHeaders->getIdentifier() .
"',method:$('#" . $containerId .
" ._method').val(),url:$('#" . $containerId .
" ._path').val()}",
"#modal", [
"attr" =>
"",
"hasLoader" => false ]);
100 if (!$msgHelp->_empty) {
101 $this->jquery->exec(
'$("#' . JString::cleanIdentifier(
"help-" . $action . $controller) .
'").transition("show");',
true);
103 $this->jquery->compile($this->view);
104 $this->loadView($this->
_getAdminFiles()->getViewRestFormTester(), [
"frmHeaders" => $frmHeaders,
"frmParameters" => $frmParameters,
"frmTester" => $frm,
"pathId" => $pathId,
"msgHelp" => $msgHelp ]);
109 $msg=$this->
showSimpleMessage($docParser->getDescriptionAsHtml(),
"",
"",
"help circle blue", null,
"msg-help-" . $action . $controller);
110 $msg->addHeader(
"Method " . $action);
111 $msg->addList($docParser->getMethodParamsReturnAsHtml());
112 $msg->addClass(
"hidden transition");
113 $msg->_empty=$docParser->isEmpty();
119 $frm=$this->jquery->semantic()->htmlForm(
"frmNewResource");
120 $frm->addMessage(
"msg",
"Creating a new REST controller...",
"New resource", HtmlIconGroups::corner(
"heartbeat",
"plus"));
121 $input=$frm->addInput(
"ctrlName",
"Controller name")->addRule(
"empty");
123 $fields=$frm->addFields();
125 $resources=\array_combine($resources, $resources);
126 $fields->addDropdown(
"resource", $resources,
"Resource", end($resources))->addRule([
"exactCount[1]" ]);
127 $fields->addInput(
"route",
"Main route path",
"text",
"/rest/")->addRule(
"empty");
128 $frm->addCheckbox(
"re-init",
"Re-init Rest cache (recommanded)",
"reInit")->setChecked(
true);
131 $fields=$frm->addFields();
132 $bt=$fields->addButton(
"bt-create-new-resource",
"Create new controller",
"teal");
133 $bt->addIcon(
"plus");
134 $fields->addButton(
"bt-cancel-new-resource",
"Cancel",
"",
"$('#frmNewResource').hide();$('#divRest').show();");
135 $frm->setValidationParams([
"on" =>
"blur",
"inline" =>
false ]);
136 $frm->addErrorMessage();
137 $frm->setSubmitParams($this->
_getAdminFiles()->getAdminBaseRoute() .
"/_createNewResource",
"#divRest", [
"dataType" =>
"html" ]);
138 $this->jquery->exec(
"$('#divRest').hide();$('#div-new-resource').show();",
true);
139 echo $frm->compile($this->jquery, $this->view);
140 echo $this->jquery->compile($this->view);
145 if (isset($_POST[
"ctrlName"]) && $_POST[
"ctrlName"] !==
"") {
147 $resource=$_POST[
"resource"];
148 $route=$_POST[
"route"];
150 $restControllersDir=ROOT . DS . str_replace(
"\\", DS, $restControllerNS);
152 $controllerName=\ucfirst($_POST[
"ctrlName"]);
153 $filename=$restControllersDir . DS . $controllerName .
".php";
154 if (!\file_exists($filename)) {
156 if ($restControllerNS !==
"")
157 $namespace=
"namespace " . $restControllerNS .
";";
158 UFileSystem::openReplaceWriteFromTemplateFile($frameworkDir .
"/admin/templates/restController.tpl", $filename, [
"%resource%" => $resource,
"%route%" => $route,
"%controllerName%" => $controllerName,
"%namespace%" => $namespace ]);
159 echo $this->
showSimpleMessage(
"The <b>" . $controllerName .
"</b> Rest controller has been created in <b>" .
UFileSystem::cleanPathname($filename) .
"</b>.",
"success",
"Rest creation" ,
"checkmark circle", 30000,
"msgGlobal");
160 if (isset($_POST[
"re-init"])) {
164 echo $this->
showSimpleMessage(
"The file <b>" . $filename .
"</b> already exists.<br>Can not create the <b>" . $controllerName .
"</b> Rest controller!",
"warning",
"Rest error",
"warning circle", 30000,
"msgGlobal");
168 $this->jquery->exec(
"$('#div-new-resource').hide();$('#divRest').show();",
true);
169 echo $this->jquery->compile($this->view);
174 $this->jquery->click(
"._toTest",
"if(!$(this).hasClass('active')){ 175 \$(this).closest('tr').after('<tr class=\"active\"><td id=\"sub-td'+$(this).closest('tr').attr('id')+'\" colspan=\"'+$(this).closest('tr').children('td').length+'\">test</td></tr>'); 176 $(this).addClass('active').removeClass('visibleover');}else{ 177 $(this).removeClass('active').addClass('visibleover'); 178 $(this).closest('tr').find('.ui.icon.help').transition('hide'); 179 $('#sub-td'+$(this).closest('tr').attr('id')).remove(); 180 }",
false,
false,
true);
181 $this->jquery->click(
"._showMsgHelp",
'$("#"+$(this).attr("data-show")).transition();');
182 $this->jquery->postOnClick(
"._toTest", $this->
_getAdminFiles()->getAdminBaseRoute() .
"/_displayRestFormTester",
"{resource:$(this).attr('data-resource'),controller:$(this).attr('data-controller'),action:$(this).attr('data-action'),path:$(this).closest('tr').attr('data-ajax')}",
"'#sub-td'+$(self).closest('tr').attr('id')", [
"ajaxTransition" =>
"random",
"stopPropagation" =>
true,
"jsCondition" =>
"!$(self).hasClass('active')" ]);
183 $this->jquery->exec(
"addToken=function(jqXHR){ 184 if(jqXHR.getResponseHeader('authorization')!=null && jqXHR.getResponseHeader('authorization').trim().startsWith('Bearer')){ 185 var bearer=jqXHR.getResponseHeader('authorization').trim().slice(7); 186 $('#access-token').val(bearer); 187 $('#access-token').trigger('change'); 194 $method=$_POST[
"method"];
195 $path=$_POST[
"path"];
196 $formId=
"sub-tddtRest-tr-" . JString::cleanIdentifier($_POST[
"pathId"]);
197 $this->jquery->ajax($method, $path,
"#" . $formId .
" ._restResponse", [
"jsCallback" =>
"$('#" . $formId .
" ._restResponse').html(JSON.stringify(data,null,2))",
"complete" =>
"var status = { 200 : 'green', 401 : 'orange', 403 : 'brown', 404 : 'black', 500 : 'red' }; 198 var headers=jqXHR.getAllResponseHeaders(); 199 headers=headers.split(/\\r\\n/); 201 $.each(headers,function(index,header){ 202 var vp=header.split(':'); 204 bHeaders.push('\"'+vp[0]+'\":\"'+vp[1]+'\"'); 206 headers=$.parseJSON('{'+bHeaders.join(',')+'}'); 207 $('#" . $formId .
" ._responseHeaders').html(JSON.stringify(headers,null,2)); 208 if(jqXHR.responseText==null){ 209 $('#" . $formId .
" ._restResponse').html('The response is empty'); 210 }else if(jqXHR.status!=200){ 211 $('#" . $formId .
" ._restResponse').html(jqXHR.responseText); 213 $('#" . $formId .
" ._statusText').html(jqXHR.statusText); 214 $('#" . $formId .
" ._status').html(jqXHR.status); 215 $('#" . $formId .
" ._status').removeClass('red black brown orange green').addClass(status[jqXHR.status]); 216 addToken(jqXHR);",
"dataType" =>
"json",
"headers" => $headers,
"params" => $this->
getRestRequestParams() ]);
217 echo
'<div><h5 class="ui top block attached header">Response headers</h5><div class="ui attached segment"><pre style="font-size: 10px;" class="_responseHeaders"></pre></div></div>';
218 echo $this->jquery->compile($this->view);
222 $result=[
'"Authorization": "Bearer "+$("#access-token").val()' ];
223 if (isset($_POST[
"headers"])) {
224 $headers=urldecode($_POST[
"headers"]);
225 \parse_str($headers, $output);
228 return "{" . \implode(
",", $result) .
"}";
233 if (isset($_POST[
"params"])) {
234 $headers=urldecode($_POST[
"params"]);
235 \parse_str($headers, $output);
238 return "{" . \implode(
",", $result) .
"}";
242 if (isset($params[
"name"])) {
243 $names=$params[
"name"];
244 $values=$params[
"value"];
245 $count=\sizeof($names);
246 for($i=0; $i < $count; $i++) {
249 if (isset($values[$i]))
250 $result[]=
'"' . $name .
'": "' . \addslashes($values[$i]) .
'"';
257 if (isset($_POST[
"_token"]))
258 $_SESSION[
"_token"]=$_POST[
"_token"];
262 $keys=$_POST[
"name"];
263 $values=$_POST[
"value"];
264 $toUpdate=$_POST[
"toUpdate"];
265 $frm=$this->jquery->semantic()->htmlForm($toUpdate);
266 $frm->setSize(
"mini");
267 $count=\sizeof($values);
268 for($i=0; $i < $count; $i++) {
269 if (JString::isNull($keys[$i])) {
274 $keys=\array_values($keys);
275 $values=\array_values($values);
276 $count=\sizeof($values);
278 $fields=$frm->addFields();
279 $fields->addElement(
"",
"Name",
"",
"div",
"ui label mini black pointing below");
280 $fields->addElement(
"",
"Value",
"",
"div",
"ui label mini black pointing below");
281 for($i=0; $i < $count; $i++) {
282 $fields=$frm->addFields();
283 $fields->addInput(
"name[]",
"",
"text", $keys[$i])->setIdentifier(
"name-" . $i);
284 $input=$fields->addInput(
"value[]",
"",
"text", $values[$i])->setIdentifier(
"value-" . $i);
285 $input->addAction(
"",
true,
"remove")->addClass(
"icon basic mini _deleteParameter");
288 $frm->addItem(
new HtmlLabel(
"",
"No " . $type .
"s"));
290 $this->jquery->click(
"._deleteParameter",
" 291 $(this).parents('.fields').remove(); 292 if($('#" . $toUpdate .
"').find('.fields').length==1){ 293 $('#" . $toUpdate .
"').children('.fields').remove(); 294 $('#" . $toUpdate .
"').append('<div class=\"ui label\">No " . $type .
"s</div>'); 296 ",
true,
true,
true);
298 echo $this->jquery->compile($this->view);
static isPost()
Returns true if the request is sent by the POST method.
initRestCache($refresh=true)
_displayActionDoc($controller, $action)
static docMethodParser($classname, $method)
static getModels(&$config, $silent=false)
_refreshRest($refresh=false)
_saveRequestParams($type="parameter")
static cleanPathname($path)
static openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues)
_addRestDataTableBehavior()
showSimpleMessage($content, $type, $title=null, $icon="info", $timeout=NULL, $staticName=null)
static getRestNamespace()
static initCache(&$config, $type="all", $silent=false)
_getParamsForJSON(&$result, $params)