kenken999/php
0
1<?php2class PDLayout3{4 var $bootstrapTheme = "";5 var $customCssPageName = "";6 var $page;7 var $table;8 var $version = 4;9 var $bootstrapSize;10 var $name="";11 var $style="";12 var $stylePath="";13 /**14 * True when 'this page has custom settings' is checked off15 */16 var $customSettings = false;17 18 function __construct( $table, $page, $theme, $size = "normal", $stylePath = "", $customSettings = false )19 {20 $this->page = $page;21 $this->table = $table;22 $this->bootstrapTheme = $theme;23 $this->bootstrapSize = $size;24 $this->stylePath = $stylePath;25 $this->customSettings = $customSettings;26 }27 28 29 /**30 * Returns list of CSS files required for displaying the layout31 */32 public function getCSSFiles($rtl = false, $mobile = false, $pdf = false)33 {34 $files = array();35 $suffix = "";36 if( $rtl ) {37 $suffix = "RTL";38 }39 40 // add custom theme style for page(Editor tab settings) or project theme style41 if ($this->customSettings && strlen($this->stylePath) != 0) {42 $files[] = $this->stylePath . "/style" . $suffix . ".css";43 } else {44 $files[] = "styles/bootstrap/" . $this->bootstrapTheme . "/" . $this->bootstrapSize . "/style" . $suffix . ".css";45 }46 47 $files[] = "styles/font-awesome/css/font-awesome.min.css";48 49 // add project CustomCSS50 if (!$this->customSettings) {51 if (file_exists(getabspath("styles/custom/custom" . $suffix . ".css"))) {52 $files[] = "styles/custom/custom" . $suffix . ".css";53 }54 }55 56 $files[] = "styles/pages/".$this->table."_".$this->page["id"].$suffix.".css";57 58 $files[] = "fonts/fonts.css";59 60 return $files;61 }62 63 64 /**65 * Hide items and grid cells that should be hidden66 * @param XTempl @xt67 * @param array $itemsToHide68 * @param ProjectSettings ps69 * @return {object} copy of cell map with hidden rows and cols removed70 */71 public function & prepareGrid( $xt, $itemsToHide, $recordItemsToHide, &$cellMap, $location, $pageObject )72 {73 $checkRecordItems = !!$recordItemsToHide;74 $recordsToCheck = array();75 // mark cells that can be removed or hidden76 foreach( $cellMap->cells as $cell => $dummy ) {77 $cMapRef = &$cellMap->cells[$cell];78 if( $cMapRef["fixedAtClient"] )79 continue;80 $empty = false;81 if( !$cMapRef["fixedAtServer"] ) {82 $empty = true;83 foreach( $cMapRef["tags"] as $i => $item ) {84 if( $xt->getVar( $item ) )85 {86 $empty = false;87 break;88 }89 }90 }91 $hidden = true;92 $visibleItems = array();93 foreach( $cMapRef["items"] as $i => $item ) {94 if( !$itemsToHide[ $item ] )95 {96 $hidden = false;97 if( $checkRecordItems ) {98 $visibleItems[ $item ] = true;99 }100 else101 break;102 }103 }104 if( $checkRecordItems && !$hidden && !$empty && $visibleItems ) {105 $cMapRef["hiddenRecords"] = $this->findHiddenRecords( $visibleItems, $recordItemsToHide );106 $recordsToCheck = addToAssocArray($recordsToCheck, $cMapRef["hiddenRecords"] );107 }108 if( $empty )109 $cMapRef["removable"] = true;110 if( $hidden )111 $cMapRef["hidable"] = true;112 }113 114 // mark rows and cols that can be removed115 $removedColsRows = $cellMap->removeRowsColumns("removable");116 117 // mark rows and cols that can be hidden118 $hidingMap = $cellMap->makeClone();119 $hiddenColsRows = $hidingMap->removeRowsColumns("hidable" );120 121 122 123 // PDF JSON needs this124 $visibleWidth = $cellMap->width - count($hiddenColsRows["cols"]);125 $xt->assign( "formwidth_" . $location, $visibleWidth );126 127 // do actual removal and hiding128 129 // hide rows first130 if( !$pageObject->pdfJsonMode() ) {131 foreach( $removedColsRows["rows"] as $row ) {132 $xt->assign( "row_" . $location . "_" . $row, 'data-hidden' );133 }134 foreach( $hiddenColsRows["rows"] as $row ) {135 $xt->assign( "row_" . $location . "_" . $row, 'data-hidden' );136 }137 } else {138 139 for( $row = 0; $row < $cellMap->height; ++$row ) {140 $xt->assign( "row_" . $location . "_" . $row, true );141 }142 foreach( $removedColsRows["rows"] as $row ) {143 $xt->assign( "row_" . $location . "_" . $row, false );144 }145 foreach( $hiddenColsRows["rows"] as $row ) {146 $xt->assign( "row_" . $location . "_" . $row, false );147 }148 // columns149 for( $col = 0; $col < $cellMap->width; ++$col ) {150 $xt->assign( "col_" . $location . "_" . $col, true );151 }152 foreach( $removedColsRows["cols"] as $col ) {153 $xt->assign( "col_" . $location . "_" . $col, false );154 }155 foreach( $hiddenColsRows["cols"] as $col ) {156 $xt->assign( "col_" . $location . "_" . $col, false );157 }158 159 160 }161 162 foreach( $cellMap->cells as $cell => $cMap ) {163 164 if( 0 == count( $cMap["rows"] ) || 0 == count( $cMap["cols"] )) {165 // don't display cell166 continue;167 }168 169 // display cell170 $xt->assign( "cellblock_" . $location . "_" . $cell, true );171 172 // add cell attributes173 $dummyData = null;174 $this->assignCellAttrs( $hidingMap, $cell, $location, $pageObject, $xt, $dummyData );175 176 }177 178 if( $checkRecordItems ) {179 foreach( $recordsToCheck as $recId => $dummy ) {180 $recordHidingMap = $hidingMap->makeClone();181 $recordHidingMap->setRecordId( $recId );182 $hiddenRecordRows = $recordHidingMap->removeRowsColumns("hidable", true );183 $recordData =& $pageObject->findRecordAssigns( $recId );184 185 // hide whole rows186 if( !$pageObject->pdfJsonMode() ) {187 foreach( $hiddenRecordRows["rows"] as $row ) {188 $recordData[ "row_" . $location . "_" . $row] = 'data-hidden';189 }190 } else {191 foreach( $hiddenRecordRows["rows"] as $row ) {192 $recordData[ "row_" . $location . "_" . $row ] = false;193 }194 }195 196 foreach( $cellMap->cells as $cell => $cMap ) {197 198 if( 0 == count( $cMap["rows"] ) || 0 == count( $cMap["cols"] )) {199 // don't display cell200 continue;201 }202 // add cell attributes203 $this->assignCellAttrs( $recordHidingMap, $cell, $location, $pageObject, $xt, $recordData, true );204 }205 206 207 }208 }209 210 return $hidingMap;211 212 }213 214 function assignCellAttrs( &$cellMap, $cell, $location, $pageObject, $xt, &$recordData, $forceCellSpans = false ) {215 $cellAttrs = array();216 $hCell =& $cellMap->cells[$cell];217 218 if( !$pageObject->pdfJsonMode() ) {219 if( 0 == count( $hCell["rows"] ) || 0 == count( $hCell["cols"] )) {220 // display cell hidden221 $cellAttrs[] = 'data-hidden';222 }223 224 if( $forceCellSpans || count( $hCell["cols"] ) > 1 ) {225 $cellAttrs[] = 'colspan="' . count( $hCell["cols"] ) . '"';226 }227 if( $forceCellSpans || count( $hCell["rows"] ) > 1 ) {228 $cellAttrs[] = 'rowspan="' . count( $hCell["rows"] ) . '"';229 }230 // specify which cols and rows are visible231 232 if( count( $cellAttrs ) ) {233 $this->assignPageVar( $recordData, $xt, "cell_".$location."_" . $cell, implode( " ", $cellAttrs ) );234 }235 } else {236 if( 0 == count( $hCell["rows"] ) || 0 == count( $hCell["cols"] )) {237 $this->assignPageVar( $recordData, $xt, "cellblock_" . $location . "_" . $cell, false );238 }239 240 if( $forceCellSpans || count( $hCell["cols"] ) > 1 ) {241 $this->assignPageVar( $recordData, $xt, "colspan_" . $location . "_" . $cell, count( $hCell["cols"] ) );242 }243 if( $forceCellSpans || count( $hCell["rows"] ) > 1 ) {244 $this->assignPageVar( $recordData, $xt, "rowspan_" . $location . "_" . $cell, count( $hCell["rows"] ) );245 }246 }247 }248 249 function assignPageVar( &$recordData, $xt, $name, $value ) {250 if( $recordData === null ) {251 $xt->assign( $name, $value );252 } else {253 $recordData[ $name ] = $value;254 }255 }256 /**257 * @param array $allItems associative array of items258 * @param array $hiddenItems - associative array of items hidden in specific rows259 * $hiddenItems[<itemid>] = array( <rowid>,<rowid>,<rowid> )260 * @return array Array of rowids where all items are hidden261 */262 public function findHiddenRecords( $allItems, $hiddenItems)263 {264 $result = null;265 foreach( $allItems as $item => $dummy ) {266 if( !$hiddenItems[ $item ] ) {267 return array();268 }269 if($result === null ) {270 $result = $hiddenItems[ $item ];271 } else {272 $result = array_intersect( $result, $hiddenItems[ $item ] );273 }274 if( count($result) == 0 )275 break;276 }277 return $result;278 }279 280 public function visibleOnMedia( $media, $visibilty ) {281 if( $media == 0 ) {282 return $visibilty == 0283 || $visibilty == 3284 || $visibilty == 4285 || $visibilty == 5;286 287 } else if( $media == 1 ) {288 return $visibilty == 0289 || $visibilty == 2290 || $visibilty == 4;291 }292}293 294 /**295 * @param {array} $itemToHide - array of items to be hidden. Pairs of [itemId] => true296 */297 public function prepareForms( $xt, $itemsToHide, $recordItemsToHide, $pageObject ) {298 299 /* desktop=0, mobile=1 */300 301 if( $pageObject ) {302 $ps = $pageObject->pSet;303 } else {304 $ps = new ProjectSettings( GetTableByShort($this->table), $this->page["type"], $this->page["id"] );305 }306 307 $helper =& $ps->helperFormItems();308 309 // make array of items hidden by application or media type310 $invisibleItems = $itemsToHide;311 $mediaHidenItems = array();312 $mediaType = $pageObject->pdfJsonMode() ? MEDIA_DESKTOP : getMediaType();313 foreach( $helper["itemVisiblity"] as $itemId => $visibility ) {314 if( !$this->visibleOnMedia( $mediaType, $visibility )) {315 $invisibleItems[ $itemId ] = true;316 $mediaHidenItems[ $itemId ] = true;317 }318 }319 320 $visibleCellsMap = array();321 $cellMaps =& $ps->helperCellMaps();322 foreach( array_keys($cellMaps) as $loc ) {323 $formRecordItemsToHide = array();324 foreach( $recordItemsToHide as $item => $itemRecords ) {325 if($helper["itemForms"][$item] == $loc )326 $formRecordItemsToHide[$item] = array_keys( $itemRecords );327 }328 $hMap =& $this->prepareGrid($xt, $invisibleItems, $formRecordItemsToHide, new CellMapPD( $cellMaps[ $loc ] ), $loc, $pageObject );329 if( $pageObject ) {330 $visibleCellsMap[ $loc ] = & $this->prepareClientCellMap( $cellMaps[ $loc ], $hMap );331 }332 }333 if( $pageObject ) {334 $pageObject->setPageData("cellMaps", $visibleCellsMap );335 }336 337 // hide items338 if( !$pageObject->pdfJsonMode() ) {339 foreach( array_keys($invisibleItems) as $item ) {340 $hideAttrs = array();341 if( $mediaHidenItems[ $item ] ) {342 $hideAttrs[] = "data-media-hidden";343 }344 if( $itemsToHide[ $item ] ) {345 $hideAttrs[] = "data-hidden";346 }347 $xt->assign( "item_" . $item, implode( ' ', $hideAttrs ) );348 }349 } else {350 foreach( array_keys($invisibleItems) as $item ) {351 $xt->assign( "item_hide_" . $item, '1' );352 }353 }354 355 // hide items in records356 if( $pageObject ) {357 foreach( $recordItemsToHide as $item => $itemRecords ) {358 foreach( array_keys( $itemRecords ) as $recordId ) {359 $pageObject->hideRecordItem( $item, $recordId );360 }361 }362 }363 364 $xt->assign("firstAboveGridCell", true);365 366 // hide other cells & forms367 368 $formTags =& $helper["formXtTags"];369 foreach( array_keys($formTags) as $loc ) {370 $present = false;371 foreach( $formTags[$loc] as $tag ) {372 if( $xt->getvar( $tag ) ) {373 $present = true;374 break;375 }376 }377 if( !$present ) {378 // hide the whole form379 $xt->assign( $loc."_block", false );380 $xt->assign( $loc."_outerblock", false );381 }382 }383 384 385 386 $formItems =& $helper["formItems"];387 foreach( array_keys($formItems) as $loc ) {388 $present = false;389 foreach( $formItems[$loc] as $item ) {390 if( !isset( $invisibleItems[$item] ) ) {391 $present = true;392 break;393 }394 }395 if( !$present ) {396 // hide the whole form397 $xt->assign( "form_" . $loc, 'data-hidden' );398 }399 }400 }401 protected function & prepareClientCellMap( &$allCells, &$visibleCells ) {402 foreach( array_keys( $visibleCells->cells ) as $cellId ) {403 $allCells["cells"][$cellId]["visibleCols"] = $visibleCells->cells[$cellId]["cols"];404 }405 return $allCells;406 }407 408}409 410class CellMapPD {411 public $cells;412 public $height;413 public $width;414 415 function __construct( &$map ) {416 $this->cells = &$map["cells"];417 $this->height = $map["height"];418 $this->width = $map["width"];419 }420 421 function makeClone() {422 $newMap = array(423 "cells" => cloneArray( $this->cells ),424 "height" => $this->height,425 "width" => $this->width,426 );427 return new CellMapPD( $newMap );428 }429 430 /**431 * Update "hidable" flag with values from "hiddenRecords"432 */433 function setRecordId( $recId ) {434 foreach( $this->cells as $cell => $dummy ) {435 $cMapRef = &$this->cells[$cell];436 if( !$cMapRef["hiddenRecords"] || $cMapRef["hidable"] ) {437 continue;438 }439 foreach( $cMapRef["hiddenRecords"] as $rec ) {440 if( $rec === $recId ) {441 $cMapRef["hidable"] = true;442 break;443 }444 }445 }446 }447 448 public function getColumnCells( $col ) {449 $ret = array();450 foreach( $this->cells as $cell => $cMap ) {451 if( array_search( $col, $cMap["cols"] ) !== FALSE )452 $ret[] = $cell;453 }454 return $ret;455 }456 457 public function getRowCells( $row ) {458 $ret = array();459 foreach( $this->cells as $cell => $cMap ) {460 if( array_search( $row, $cMap["rows"] ) !== FALSE )461 $ret[] = $cell;462 }463 return $ret;464 }465 466 /**467 * Returns array of row indices that can be removed468 */469 public function removeRowsColumns( $cellRemoveFlag, $rowsOnly = false ) {470 471 $ret = array( "cols" => array(), "rows" => array() );472 // remove unnecessary columns473 if( !$rowsOnly ) {474 for( $col = 0; $col < $this->width; ++$col ) {475 $colCells = $this->getColumnCells( $col );476 $canDeleteCol = true;477 foreach( $colCells as $cell ) {478 if( !$this->cells[$cell][ $cellRemoveFlag ] && count($this->cells[$cell]["cols"]) == 1 ) {479 $canDeleteCol = false;480 break;481 }482 }483 if( !$canDeleteCol )484 continue;485 for( $i = 0; $i < count($colCells); ++$i ) {486 $cell = $colCells[$i];487 $colIdx = array_search( $col, $this->cells[$cell]["cols"] );488 array_splice( $this->cells[$cell]["cols"], $colIdx, 1 );489 }490 $ret["cols"][] = $col;491 }492 }493 494 // remove unnecessary rows495 for( $row = $this->height - 1; $row >= 0; --$row ) {496 $rowCells = $this->getRowCells( $row );497 $canDeleteRow = true;498 foreach( $rowCells as $cell ) {499 if( !$this->cells[$cell][ $cellRemoveFlag ] &&500 ( count( $this->cells[$cell]["rows"]) == 1501 /* can't delete the first row of a rowspanned cell */ || $this->cells[$cell]["rows"][0] === $row ) ) {502 $canDeleteRow = false;503 break;504 }505 }506 if( !$canDeleteRow )507 continue;508 for( $i = 0; $i < count($rowCells); ++$i ) {509 $cell = $rowCells[$i];510 $rowIdx = array_search( $row, $this->cells[$cell]["rows"] );511 512 array_splice( $this->cells[$cell]["rows"], $rowIdx, 1 );513 }514 $ret["rows"][] = $row;515 }516 return $ret;517 }518 519}520?>