13use Ajax\semantic\html\collections\HtmlMessage;
14use Ajax\common\html\HtmlContentOnly;
16use Ajax\semantic\widgets\datatable\DataTable;
32 parent::__construct ();
33 DAO::$transformerOp =
'toView';
44 $objects = \array_map (
function ($o) {
47 return \json_encode(\array_values ( $objects ));
51 if ($this->style ===
'inverted') {
52 $elm->setInverted (
true );
53 if ($elm instanceof DataTable) {
54 $elm->setActiveRowSelector (
'black' );
66 $objects = $this->
getInstances ( $totalCount ,$this->activePage);
67 $modal = ($this->
_getModelViewer ()->isModal ( $objects, $this->model )) ?
"modal" :
"no";
68 $dt = $this->
_getModelViewer ()->getModelDataTable ( $objects, $this->model, $totalCount ,$this->activePage);
69 $this->jquery->getOnClick (
'#btAddNew', $this->
_getBaseRoute () .
'/newModel/' . $modal,
'#frm-add-update', [
'hasLoader' =>
'internal' ] );
70 $this->
_getEvents ()->onDisplayElements ( $dt, $objects,
false );
71 $this->
crudLoadView ( $this->
_getFiles ()->getViewIndex (), [
'classname' => $this->model,
'messages' => $this->jquery->semantic ()->matchHtmlComponents ( function ($compo) {
72 return $compo instanceof HtmlMessage;
83 #[\Ubiquity\attributes\items\router\Post]
85 $instance = $_SESSION [
'instance'] ??
null;
86 if (isset ( $instance )) {
87 $this->
_getEvents ()->onBeforeUpdateRequest ( $_POST,
false );
89 $this->
_getEvents ()->onBeforeUpdate ( $inst,
false );
92 if ($callback ===
false) {
93 $dt = $this->
_getModelViewer ()->getModelDataTable ( [ $instance ], $this->model, 1 );
94 $dt->setGroupByFields(
null);
96 echo
new HtmlContentOnly ( $dt->getFieldValue ( $member ) );
98 if (\method_exists ( $this, $callback )) {
99 $this->$callback ( $member, $instance );
101 throw new \Exception (
"The method `" . $callback .
"` does not exists in " . get_class () );
105 UResponse::setResponseCode ( 404 );
108 throw new \Exception (
'$_SESSION["instance"] is null' );
117 if (isset ( $_POST [
's'] )) {
120 $page = URequest::post (
'p', 1 );
121 $instances = $this->
getInstances ( $totalCount, $page );
123 if (! isset ( $totalCount )) {
128 if (isset ( $recordsPerPage )) {
129 if (! is_array ( $grpByFields )) {
130 UResponse::asJSON ();
136 $this->jquery->execAtLast (
'$("#search-query-content").html("' . $_POST [
's'] .
'");$("#search-query").show();$("#table-details").html("");' );
147 public function edit($modal =
"no", $ids =
"") {
148 if (URequest::isAjax ()) {
150 $instance->_new =
false;
151 $this->
_edit ( $instance, $modal );
153 $this->jquery->execAtLast (
"$('._edit[data-ajax={$ids}]').trigger('click');" );
164 if (URequest::isAjax ()) {
166 $instance =
new $model ();
167 $this->
_getEvents()->onNewInstance($instance);
168 $instance->_new =
true;
169 $this->
_edit ( $instance, $modal );
171 $this->jquery->execAtLast (
"$('.ui.button._new').trigger('click');" );
182 #[\Ubiquity\attributes\items\router\Post]
184 $ids = URequest::post (
"id" );
185 $td = URequest::post (
"td" );
186 $part = URequest::post (
"part" );
188 $_SESSION [
"instance"] = $instance;
189 $instance->_new =
false;
190 $form = $this->
_getModelViewer ()->getMemberForm (
"frm-member-" . $member, $instance, $member, $td, $part,
'updateMember' );
191 $form->setLibraryId (
"_compo_" );
192 $this->jquery->renderView (
"@framework/main/component.html" );
201 public function display($modal =
"no", $ids =
"") {
202 if (URequest::isAjax ()) {
204 $key = OrmUtils::getFirstKeyValue ( $instance );
205 $this->jquery->execOn (
"click",
"._close",
'$("#table-details").html("");$("#dataTable").show();' );
206 $this->jquery->getOnClick (
"._edit", $this->
_getBaseRoute () .
"/edit/" . $modal .
"/" . $key,
"#frm-add-update" );
207 $this->jquery->getOnClick (
"._delete", $this->
_getBaseRoute () .
"/delete/" . $key,
"#table-messages" );
209 $this->
_getModelViewer ()->getModelDataElement ( $instance, $this->model, $modal );
210 $this->jquery->renderView ( $this->
_getFiles ()->getViewDisplay (), [
"classname" => $this->model,
"instance" => $instance,
"pk" => $key ] );
212 $this->jquery->execAtLast (
"$('._display[data-ajax={$ids}]').trigger('click');" );
223 #[\Ubiquity\attributes\items\router\Route(methods: ['get','post'])]
224 public function delete($ids) {
225 if (URequest::isAjax ()) {
228 if (\count ( $_POST ) > 0) {
230 if (DAO::remove ( $instance )) {
231 $message =
new CRUDMessage (
"Deletion of `<b>" . $instanceString .
"</b>`",
"Deletion",
"info",
"info circle", 4000 );
232 $message = $this->
_getEvents ()->onSuccessDeleteMessage ( $message, $instance );
233 $this->jquery->exec (
"$('._element[data-ajax={$ids}]').remove();",
true );
235 $message =
new CRUDMessage (
"Can not delete `" . $instanceString .
"`",
"Deletion",
"warning",
"warning circle" );
236 $message = $this->
_getEvents ()->onErrorDeleteMessage ( $message, $instance );
238 }
catch ( \Exception $e ) {
239 $message =
new CRUDMessage (
"Exception : can not delete `" . $instanceString .
"`",
"Exception",
"warning",
"warning" );
240 $message = $this->
_getEvents ()->onErrorDeleteMessage ( $message, $instance );
244 $message =
new CRUDMessage (
"Do you confirm the deletion of `<b>" . $instanceString .
"</b>`?",
"Remove confirmation",
"error",
"question circle" );
245 $message = $this->
_getEvents ()->onConfDeleteMessage ( $message, $instance );
249 echo $this->jquery->compile ( $this->view );
251 $this->jquery->execAtLast (
"$('._delete[data-ajax={$ids}]').trigger('click');" );
258 $this->jquery->execAtLast (
'$("#table-details").html("");' );
259 $compo->setLibraryId (
"_compo_" );
260 $this->jquery->renderView (
"@framework/main/component.html" );
270 #[\Ubiquity\attributes\items\router\Post]
272 $message =
new CRUDMessage (
"Modifications were successfully saved",
"Updating" );
273 $instance = $_SESSION [
"instance"] ??
null;
274 if (isset ( $instance )) {
275 $isNew = $instance->_new;
277 $this->
_getEvents ()->onBeforeUpdateRequest ( $_POST, $isNew );
278 $updated =
CRUDHelper::update ( $instance, $_POST,
true,
true,
function ($inst, $isNew) {
279 $this->
_getEvents ()->onBeforeUpdate ( $inst, $isNew );
282 $message->setType (
"success" )->setIcon (
"check circle outline" );
283 $message = $this->
_getEvents ()->onSuccessUpdateMessage ( $message, $instance );
286 $message->setMessage (
"An error has occurred. Can not save changes." )->setType (
"error" )->setIcon (
"warning circle" );
287 $message = $this->
_getEvents ()->onErrorUpdateMessage ( $message, $instance );
289 }
catch ( \Exception $e ) {
291 $message =
new CRUDMessage (
"Exception : can not update `" . $instanceString .
"`",
"Exception",
"warning",
"warning" );
292 $message = $this->
_getEvents ()->onErrorUpdateMessage ( $message, $instance );
295 echo $this->jquery->compile ( $this->view );
298 throw new \Exception (
'$_SESSION["instance"] is null' );
308 if (URequest::isAjax ()) {
311 $hasElements =
false;
314 $semantic = $this->jquery->semantic ();
315 $grid = $semantic->htmlGrid (
'detail' );
316 if (($nb = \count ( $fkInstances )) > 0) {
317 $wide = intval ( 16 / $nb );
321 foreach ( $fkInstances as $member => $fkInstanceArray ) {
322 $element = $viewer->getFkMemberElementDetails ( $member, $fkInstanceArray [
'objectFK'], $fkInstanceArray [
'fkClass'], $fkInstanceArray [
'fkTable'] );
323 if (isset ( $element )) {
324 $grid->addCol ( $wide )->setContent ( $element );
330 $url = $this->
_getFiles ()->getDetailClickURL ( $this->model );
331 if (UString::isNotNull ( $url )) {
335 echo $this->jquery->compile ( $this->view );
338 $this->jquery->execAtLast (
"$('tr[data-ajax={$ids}]').trigger('click');" );
343 public function detailClick($url,$responseElement=
'#divTable',$attributes=[
"attr" =>
"data-ajax"]) {
344 $this->jquery->postOnClick (
".showTable", $this->
_getBaseRoute () .
"/" . $url,
"{}", $responseElement, $attributes );
Default controller used in a new project.
Ubiquity\controllers\crud$CRUDController This class is part of Ubiquity.
__construct()
Constructor initialize $view variable.
index()
Default page : list all objects Uses modelViewer.isModal, modelViewer.getModelDataTable Uses CRUDFile...
refreshAsJson($instances)
Return a JSON representation of $instances for the JsonDataTable component.
detailClick($url, $responseElement='#divTable', $attributes=["attr"=> "data-ajax"])
edit($modal="no", $ids="")
Edits an instance.
refresh_()
Refreshes the area corresponding to the DataTable.
showDetail($ids)
Shows associated members with foreign keys.
updateModel()
Updates an instance from the data posted in a form.
updateMember($member, $callback=false)
newModel($modal="no")
Adds a new instance and edits it.
display($modal="no", $ids="")
Displays an instance.
getInstanceToString($instance)
getModelInstance($ids, $transform=true, $included=true)
_edit($instance, $modal="no")
_renderDataTableForRefresh($instances, $model, $totalCount)
showConfMessage_(CRUDMessage $message, $url, $responseElement, $data, $attributes=null)
crudLoadView($viewName, $vars=[])
showSimpleMessage_(CRUDMessage $message, $staticName=null)
getInstances(&$totalCount, $page=1, $id=null)
refreshInstance($instance, $isNew)
static getFKIntances($instance, $model, $included=false)
static update($instance, $values, $setValues=true, $updateMany=true, $eventCallback=null)
Gateway class between database and object model.
Object/relational mapping utilities.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.