Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ModelViewer.php
Go to the documentation of this file.
1<?php
2
4
5use Ajax\semantic\html\elements\HtmlButton;
6use Ajax\semantic\html\elements\HtmlHeader;
7use Ajax\semantic\html\elements\HtmlLabel;
8use Ajax\semantic\widgets\datatable\DataTable;
9use Ajax\semantic\widgets\datatable\PositionInTable;
10use Ajax\service\JString;
16
30 private $jquery;
31 private $style;
32
37 protected $controller;
38
40 $this->jquery = $controller->jquery;
41 $this->controller = $controller;
42 $this->style = $style;
43 }
44
45 public function setStyle($elm) {
46 if ($this->style === 'inverted') {
47 $elm->setInverted ( true );
48 if ($elm instanceof DataTable) {
49 $elm->setActiveRowSelector ( 'black' );
50 }
51 }
52 }
53
64 public function getModelDataElement($instance, $model, $modal): \Ajax\semantic\widgets\dataelement\DataElement {
65 $semantic = $this->jquery->semantic ();
66 $fields = $this->controller->_getAdminData ()->getElementFieldNames ( $model );
67
68 $dataElement = $semantic->dataElement ( "de", $instance );
69 $pk = OrmUtils::getFirstKeyValue ( $instance );
70 $dataElement->getInstanceViewer ()->setIdentifierFunction ( function () use ($pk) {
71 return $pk;
72 } );
73 $dataElement->setFields ( $fields );
74 $dataElement->setCaptions ( $this->getElementCaptions ( $fields, $model, $instance ) );
75
76 $fkInstances = CRUDHelper::getFKIntances ( $instance, $model );
77 foreach ( $fkInstances as $member => $fkInstanceArray ) {
78 if (array_search ( $member, $fields ) !== false) {
79 $dataElement->setValueFunction ( $member, function () use ($fkInstanceArray, $member) {
80 return $this->getFkMemberElement ( $member, $fkInstanceArray ["objectFK"], $fkInstanceArray ["fkClass"], $fkInstanceArray ["fkTable"] );
81 } );
82 }
83 }
84 $this->addEditMemberFonctionality ( "dataElement" );
85 return $dataElement->addClass ( $this->style );
86 }
87
95 public function getElementCaptions($captions, $className, $instance): array {
96 return \array_map ( 'ucfirst', $captions );
97 }
98
106 public function getModelDataTable($instances, $model, $totalCount, $page = 1): DataTable {
107 $adminRoute = $this->controller->_getBaseRoute ();
108 $files = $this->controller->_getFiles ();
109 $dataTable = $this->getDataTableInstance ( $instances, $model, $totalCount, $page );
110 $attributes = $this->controller->_getAdminData ()->getFieldNames ( $model );
111 $this->setDataTableAttributes ( $dataTable, $attributes, $model, $instances );
112 $dataTable->setCaptions ( $this->getCaptions ( $attributes, $model ) );
113 $dataTable->setFocusable(true);
114 $dataTable->addClass ( 'small very compact' );
115 $lbl = new HtmlLabel ( 'search-query', "<span id='search-query-content'></span>" );
116 $icon = $lbl->addIcon ( 'delete', false );
117 $lbl->wrap ( "<span>", "</span>" );
118 $lbl->setProperty ( "style", "display: none;" );
119 $icon->getOnClick ( $adminRoute . $files->getRouteRefreshTable (), '#' . $this->getDataTableId (), [ "jqueryDone" => "replaceWith","hasLoader" => "internal" ] );
120
121 $dataTable->addItemInToolbar ( $lbl );
122 $dataTable->addSearchInToolbar ();
123 $dataTable->setToolbarPosition ( PositionInTable::FOOTER );
124 $dataTable->getToolbar ()->setSecondary ();
125 $this->setStyle ( $dataTable );
126 return $dataTable;
127 }
128
129 public function setDataTableAttributes(DataTable $dataTable, $attributes, $model, $instances, $selector = null) {
130 $modal = ($this->isModal ( $instances, $model ) ? 'modal' : 'no');
131
132 $adminRoute = $this->controller->_getBaseRoute ();
133 $files = $this->controller->_getFiles ();
134 $dataTable->setButtons ( $this->getDataTableRowButtons () );
135 $dataTable->setFields ( $attributes );
136 if (\array_search ( 'password', $attributes ) !== false) {
137 $dataTable->setValueFunction ( 'password', function ($v) {
138 return UString::mask ( $v );
139 } );
140 }
141 $dataTable->setIdentifierFunction ( CRUDHelper::getIdentifierFunction ( $model ) );
142
143 if (! isset ( $selector )) {
144 if (\count ( $instances ) > 0 && $this->showDetailsOnDataTableClick ()) {
145 $dataTable->getOnRow ( 'click', $adminRoute . $files->getRouteDetails (), "#table-details", [ "selector" => $selector,"attr" => "data-ajax","hasLoader" => false,"jsCallback" => "return false;" ] );
146 $dataTable->setActiveRowSelector ( 'active' );
147 }
148
149 $dataTable->setUrls ( [ 'refresh' => $adminRoute . $files->getRouteRefresh (),'delete' => $adminRoute . $files->getRouteDelete (),'edit' => $adminRoute . $files->getRouteEdit () . "/" . $modal,'display' => $adminRoute . $files->getRouteDisplay () . "/" . $modal ] );
150 $dataTable->setTargetSelector ( [ 'delete' => '#table-messages','edit' => '#frm-add-update','display' => '#table-details' ] );
151 $this->addEditMemberFonctionality ( 'dataTable' );
152 }
153 $this->addAllButtons ( $dataTable, $attributes );
154 }
155
156 public function addEditMemberFonctionality($part) {
157 if (($editMemberParams = $this->getEditMemberParams ()) !== false) {
158 if (isset ( $editMemberParams [$part] )) {
159 $params = $editMemberParams [$part];
160 $params->compile ( $this->controller->_getBaseRoute (), $this->jquery, $part );
161 }
162 }
163 }
164
171 public function recordsPerPage($model, $totalCount = 0) {
172 if ($totalCount > 6) {
173 return 6;
174 }
175 return;
176 }
177
181 public function getGroupByFields() {
182 return;
183 }
184
194 protected function getDataTableInstance($instances, $model, $totalCount, $page = 1): DataTable {
195 $dtId = $this->getDataTableId ();
196 $semantic = $this->jquery->semantic ();
197 $recordsPerPage = $this->recordsPerPage ( $model, $totalCount );
198 if (\is_numeric ( $recordsPerPage )) {
199 $grpByFields = $this->getGroupByFields ();
200 if (\is_array ( $grpByFields )) {
201 $dataTable = $semantic->dataTable ( $dtId, $model, $instances );
202 $dataTable->setGroupByFields ( $grpByFields );
203 } else {
204 $dataTable = $semantic->jsonDataTable ( $dtId, $model, $instances );
205 }
206 $dataTable->paginate ( $page, $totalCount, $recordsPerPage, 5 );
207 $dataTable->onSearchTerminate ( '$("#search-query-content").html(data);$("#search-query").show();$("#table-details").html("");' );
208 } else {
209 $dataTable = $semantic->dataTable ( $dtId, $model, $instances );
210 }
211 $dataTable->setLibraryId ( 'lv' );
212 return $dataTable;
213 }
214
220 protected function getDataTableRowButtons(): array {
221 return [ 'edit','delete' ];
222 }
223
224 public function addAllButtons(DataTable $dataTable, $attributes) {
225 $transition = $this->getTransition ();
226 $dataTable->onPreCompile ( function () use (&$dataTable) {
227 $dataTable->getHtmlComponent ()->colRightFromRight ( 0 );
228 $tb = $dataTable->getPaginationToolbar ();
229 if (isset ( $tb )) {
230 $tb->addClass ( $this->style );
231 }
232 } );
233 $dataTable->addAllButtons ( false, [ 'ajaxTransition' => $transition,'hasLoader' => 'internal' ], function ($bt) {
234 $bt->addClass ( 'circular ' . $this->style );
235 $this->onDataTableRowButton ( $bt ,'display');
236 }, function ($bt) {
237 $bt->addClass ( 'circular ' . $this->style );
238 $this->onDataTableRowButton ( $bt ,'edit');
239 }, function ($bt) {
240 $bt->addClass ( 'circular ' . $this->style );
241 $this->onDataTableRowButton ( $bt ,'delete');
242 } );
243 $buttons=\array_diff($this->getDataTableRowButtons(),['edit','display','delete']);
244 foreach ($buttons as $bt){
245 $attr=JString::cleanIdentifier($bt);
246 $dataTable->insertDefaultButtonIn($dataTable->getButtonsColumn(),$bt,"_$attr circular basic",false,function($b) use($attr){
247 $b->addClass ( $this->style );
248 $this->onDataTableRowButton ( $b,$attr );
249 },$bt);
250 }
251 $dataTable->setDisplayBehavior ( [ 'jsCallback' => '$("#dataTable").hide();','ajaxTransition' => $transition,'hasLoader' => 'internal' ] );
252 }
253
259 public function getTransition(): string {
260 return 'fade';
261 }
262
263 public function getDataTableId(): string {
264 return 'lv';
265 }
266
273 public function onDataTableRowButton(HtmlButton $bt,?string $name=null) {
274 }
275
282 public function onConfirmButtons(HtmlButton $confirmBtn, HtmlButton $cancelBtn) {
283 }
284
291 public function getCaptions($captions, $className):array {
292 return \array_map ( 'ucfirst', $captions );
293 }
294
303 public function getFkHeaderElementDetails($member, $className, $object) {
304 $res = new HtmlHeader ( '', 4, $member, 'content' );
305 return $res->addClass ( $this->style );
306 }
307
316 public function getFkHeaderListDetails($member, $className, $list) {
317 $res = new HtmlHeader ( '', 4, $member . ' (' . \count ( $list ) . ')', 'content' );
318 return $res->addClass ( $this->style );
319 }
320
329 public function getFkElementDetails($member, $className, $object) {
330 $res = $this->jquery->semantic ()->htmlLabel ( 'element-' . $className . '.' . $member, $object . '' );
331 return $res->addClass ( $this->style );
332 }
333
342 public function getFkListDetails($member, $className, $list) {
343 $element = $this->jquery->semantic ()->htmlList ( 'list-' . $className . '.' . $member );
344 $element->setMaxVisible ( 15 );
345
346 return $element->addClass ( 'animated divided celled ' . $this->style );
347 }
348
358 public function getFkMemberElementDetails($memberFK, $objectFK, $fkClass, $fkTable) {
359 $_fkClass = str_replace ( "\\", ".", $fkClass );
360 $header = new HtmlHeader ( "", 4, $memberFK, "content" );
361 if (is_array ( $objectFK ) || $objectFK instanceof \Traversable) {
362 $header = $this->getFkHeaderListDetails ( $memberFK, $fkClass, $objectFK );
363 $element = $this->getFkListDetails ( $memberFK, $fkClass, $objectFK );
364 foreach ( $objectFK as $oItem ) {
365 if (method_exists ( $oItem, "__toString" )) {
366 $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $oItem );
367 $item = $element->addItem ( $oItem . "" );
368 $item->setProperty ( "data-ajax", $_fkClass . "||" . $id );
369 $item->addClass ( "showTable" );
370 $this->onDisplayFkElementListDetails ( $item, $memberFK, $fkClass, $oItem );
371 }
372 }
373 } else {
374 if (method_exists ( $objectFK, "__toString" )) {
375 $header = $this->getFkHeaderElementDetails ( $memberFK, $fkClass, $objectFK );
376 $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $objectFK );
377 $element = $this->getFkElementDetails ( $memberFK, $fkClass, $objectFK );
378 $element->setProperty ( "data-ajax", $_fkClass . "||" . $id )->addClass ( "showTable" );
379 }
380 }
381 if (isset ( $element )) {
382 return [ $header,$element ];
383 }
384 return null;
385 }
386
395 public function onDisplayFkElementListDetails($element, $member, $className, $object) {
396 }
397
407 public function getFkMemberElement($memberFK, $objectFK, $fkClass, $fkTable) {
408 $element = "";
409 $_fkClass = str_replace ( "\\", ".", $fkClass );
410 if (is_array ( $objectFK ) || $objectFK instanceof \Traversable) {
411 $element = $this->getFkList ( $memberFK, $objectFK );
412 foreach ( $objectFK as $oItem ) {
413 if (\method_exists ( $oItem, '__toString' )) {
414 $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $oItem );
415 $item = $element->addItem ( $oItem . '' );
416 $item->setProperty ( 'data-ajax', $_fkClass . '||' . $id );
417 $item->addClass ( 'showTable' );
418 $this->displayFkElementList ( $item, $memberFK, $fkClass, $oItem );
419 }
420 }
421 } else {
422 if (method_exists ( $objectFK, '__toString' )) {
423 $id = (CRUDHelper::getIdentifierFunction ( $fkClass )) ( 0, $objectFK );
424 $element = $this->getFkElement ( $memberFK, $fkClass, $objectFK );
425 $element->setProperty ( 'data-ajax', $_fkClass . '||' . $id )->addClass ( 'showTable' );
426 }
427 }
428 return $element;
429 }
430
439 public function getFkList($member, $list) {
440 $element = $this->jquery->semantic ()->htmlList ( 'list-' . $member );
441 $element->setMaxVisible ( 10 );
442 return $element->addClass ( 'animated' );
443 }
444
453 public function displayFkElementList($element, $member, $className, $object) {
454 }
455
464 public function getFkElement($member, $className, $object) {
465 $res = $this->jquery->semantic ()->htmlLabel ( 'element-' . $className . '.' . $member, $object . '' );
466 return $res->addClass ( $this->style );
467 }
468
474 public function showDetailsOnDataTableClick(): bool {
475 return true;
476 }
477}
478
Ubiquity\controllers\crud$CRUDHelper This class is part of Ubiquity.
static getFKIntances($instance, $model, $included=false)
Associated with a CRUDController class Responsible of the display.
getFkMemberElement($memberFK, $objectFK, $fkClass, $fkTable)
Returns a component for displaying a foreign object (from DataElement)
addAllButtons(DataTable $dataTable, $attributes)
getFkListDetails($member, $className, $list)
Returns a list component for displaying a collection of foreign objects (*ToMany relations),...
getFkHeaderElementDetails($member, $className, $object)
Returns the header for a single foreign object (element is an instance, issue from ManyToOne),...
getDataTableInstance($instances, $model, $totalCount, $page=1)
Returns the dataTable instance for dispaying a list of object.
onDisplayFkElementListDetails($element, $member, $className, $object)
To modify for displaying an element in a list component of foreign objects (from DataTable)
setDataTableAttributes(DataTable $dataTable, $attributes, $model, $instances, $selector=null)
getGroupByFields()
Returns the fields on which a grouping is performed.
getFkList($member, $list)
Returns a list component for displaying a collection of foreign objects (*ToMany relations),...
getDataTableRowButtons()
Returns an array of buttons ["display","edit","delete"] to display for each row in dataTable.
__construct(HasModelViewerInterface $controller, $style=null)
showDetailsOnDataTableClick()
To override to make sure that the detail of a clicked object is displayed or not.
getModelDataTable($instances, $model, $totalCount, $page=1)
Returns the dataTable responsible for displaying instances of the model.
displayFkElementList($element, $member, $className, $object)
To modify for displaying an element in a list component of foreign objects, (from DataElement)
onDataTableRowButton(HtmlButton $bt,?string $name=null)
To override for modifying the dataTable row buttons.
getElementCaptions($captions, $className, $instance)
Returns the captions for DataElement fields in display route.
getCaptions($captions, $className)
Returns the captions for list fields in showTable action.
onConfirmButtons(HtmlButton $confirmBtn, HtmlButton $cancelBtn)
To override for modifying the showConfMessage dialog buttons.
getFkHeaderListDetails($member, $className, $list)
Returns the header for a list of foreign objects (issue from oneToMany or ManyToMany),...
getFkElement($member, $className, $object)
Returns a component for displaying a single foreign object (manyToOne relation), (from DataElement)
getTransition()
The default transition for display, edit and delete behaviors.
getFkMemberElementDetails($memberFK, $objectFK, $fkClass, $fkTable)
Returns a component for displaying a foreign object (from DataTable)
getFkElementDetails($member, $className, $object)
Returns a component for displaying a single foreign object (manyToOne relation), (from DataTable)
getModelDataElement($instance, $model, $modal)
Returns a DataElement object for displaying the instance Used in the display method of the CrudContro...
Object/relational mapping utilities.
Definition OrmUtils.php:17
String utilities.
Definition UString.php:15