| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871 |
- <?php
- Lib::loadClass('ViewAjax');
- class TreeAjax extends ViewAjax {
- private $_cnf = '';// Column
- private $_htmlID = '';
- private $_dataSource = null;
- private $_rowFunctions = array();
- public function __construct($tblAcl) {
- $this->_tbl = $tblAcl->getName();
- $this->_acl = $tblAcl;
- $this->_zasobID = $tblAcl->getID();
- Lib::loadClass('Data_Tree_Source');
- $this->_dataSource = new Data_Tree_Source($tblAcl->getDB());
- $this->_dataSource->setTable($tblAcl->getName());
- $this->_dataSource->setCols($tblAcl->getRealFieldList());
- $this->_dataSource->setVirtualCols($tblAcl->getVirtualFieldList());
- // TODO: mv to config or Zasoby (ALIAS?)
- switch ($tblAcl->getName()) {
- case 'IN7_MK_BAZA_DYSTRYBUCJI':
- $this->_dataSource->setParentIdField('P_ID');
- $this->_dataSource->setNameField('ID');//M_DIST_DESC
- break;
- case 'CRM_LISTA_ZASOBOW':
- $this->_dataSource->setParentIdField('PARENT_ID');
- $this->_dataSource->setNameField('ID');// 'DESC_PL'
- $this->_dataSource->setSortField('SORT_PRIO');
- break;
- case 'CRM_PROCES':
- $this->_dataSource->setParentIdField('PARENT_ID');
- $this->_dataSource->setNameField('ID');
- $this->_dataSource->setSortField('SORT_PRIO');
- break;
- default:
-
- }
- $hash = V::get('_hash', '', $_GET);
- $this->_htmlID = ($hash)? $hash : $this->_tbl . '-Tree';//uniqid($this->_tbl);
- // TODO: load filters and state: table, edit, add, etc.
- // TODO: save this data in Workspace 1, 2, 3, etc.
- }
- public function renderRootNode() {
- ?>
- <tr data-tt-id="0" data-tt-parent-id="" data-tt-branch="true">
- <td>
- <span class="directory">0</span>
- </td>
- <td coslpan="<?php echo count($fieldList); ?>"></td>
- </tr>
- <?php
- }
- public function renderNode($node) {
- $visibleCols = $this->_acl->getVisibleFieldList();
- $fieldList = array();
- foreach ($visibleCols as $fieldID => $col) {
- if ($col == 'ID') {// || $col == $this->_dataSource->getParentIdField()) {
- continue;
- }
- $fieldList[] = $col;
- }
- ?>
- <tr data-tt-id="<?php echo $node->id; ?>" data-tt-parent-id="<?php echo ($node->parent_id > 0)? $node->parent_id : ''; ?>" data-tt-branch="<?php echo ($node->type == 'folder')? 'true' : 'false'; ?>" class="ui-state-default">
- <td class="TreeAjax-cell-indenter">
- <span class="<?php echo $node->type; ?>">
- <i class="glyphicon glyphicon-folder-open"></i>
- <?php echo $node->name; ?>
- </span>
- </td>
- <?php if ($this->_dataSource->hasSortField()) : ?>
- <td class="TreeAjax-cell-sorter">
- <i class="sort-handler glyphicon glyphicon-resize-vertical"></i>
- </td>
- <?php endif; ?>
- <?php if (true) : ?>
- <td class="TreeAjax-cell-edit">
- <a href="#EDIT/<?php echo $node->id; ?>" class="glyphicon glyphicon-pencil" title="Edytuj rekord"> </a>
- </td>
- <?php endif; ?>
- <?php foreach ($fieldList as $fieldName) : $val = V::get($fieldName, '', $node->data); ?>
- <td class="TreeAjax-cell-data tbl-short-txt"><span title="<?php echo $val; ?>"><?php echo $val ?></span></td>
- <?php endforeach; ?>
- </tr>
- <?php
- }
- public function render() {
- if (!$this->_dataSource->isValidTree()) {
- $msg = "Błąd nie można wyświetlić drzewa dla tej tabeli";
- $msg = '<div class="alert alert-danger">' . $msg . '</div>';
- $msg = '<div class="container">' . $msg . '</div>';
- return $msg;
- }
- $visibleCols = $this->_acl->getVisibleFieldList();
- $fieldList = array();
- foreach ($visibleCols as $fieldID => $col) {
- if ($col == 'ID') {// || $col == $this->_dataSource->getParentIdField()) {
- continue;
- }
- $fieldList[] = $col;
- }
- $nodes = $this->_dataSource->getTreeNodes(0, array('order_by'=>'SORT_PRIO'));
- $pInitList = array();
- $forceFilterInit = array();
- ob_start();
- // class="TreeAjax table table-striped table-hover table-bordered table-condensed"
- ?>
- <link rel="stylesheet" href="stuff/jquery.treetable/jquery.treetable.css" type="text/css" />
- <link rel="stylesheet" href="stuff/jquery.treetable/jquery.treetable.theme.default.css" type="text/css" />
- <style type="text/css">
- .treetable {white-space:nowrap; width:auto;}
- .treetable .TreeAjax-cell-sorter { cursor:pointer; }
- .treetable td,
- .treetable th { border-color:#999; }
- .AjaxTreeCont .breadcrumb em { color:#bbb; }
- .AjaxTreeCont .treetable tr.selected td { background-color:#3875D7; color:#FFF; }
- /* loading */
- .AjaxTreeCont .loading-info {display:none;}
- .AjaxTreeCont .inlineEditBox .loading-info {display:block;}
- .AjaxTree-loading .loading-info,
- .AjaxTreeCont .inlineEditBox .loading-info {display:block; padding:0 0 0 20px; background:url(./icon/loading.gif) no-repeat left top;}
- /* form btn Cofnij: .appendBack */
- .AjaxTreeCont .AjaxTableEdit .show-last-value .button-appendBack .glyphicon,
- .AjaxTreeCont .AjaxFrmHorizontalEdit .show-last-value .button-appendBack .glyphicon {display:none;}
- </style>
- <div class="AjaxTreeCont">
- <ul class="breadcrumb">
- <li><a href="#" onclick="return treeAjaxBackToTree();"><?php echo $this->getLabelHtml(); ?></a></li>
- <?php if (!empty($pInitList)) : ?>
- <div class="btn-group pull-right">
- <a class="btn btn-xs btn-info dropdown-toggle" data-toggle="dropdown" href="#" title="Uruchom filtr procesu powiązanego z tabelą <?php echo $this->getLabel(); ?>">
- Filtr procesu
- <span class="caret"></span>
- </a>
- <ul class="dropdown-menu pull-left">
- <?php foreach ($pInitList as $kId => $vLabel) : ?>
- <?php if ($pInitListSelected && $pInitListSelected == $kId) : ?>
- <li class="disabled">
- <?php else : ?>
- <li>
- <?php endif; ?>
- <a href="index.php?FUNCTION_INIT=MENU_SELECT_PROCES&_action=setPermsByProces&id_proces=<?php echo $kId; ?>&MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=<?php echo $this->_zasobID; ?>" title="<?php echo htmlspecialchars("{{$kId}} {$vLabel}"); ?>"><?php echo "{{$kId}} " . V::strShortUtf8($vLabel, 50); ?></a>
- </li>
- <?php endforeach; ?>
- </ul>
- </div>
- <?php endif; ?>
- <a class="pull-right" style="padding:0 20px 0 0;" href="<?php echo "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$this->_zasobID}"; ?>"><i class="glyphicon glyphicon-list"></i> Tabela</a>
- </ul>
- <table id="<?php echo $this->_htmlID; ?>" class="TreeAjax table table-bordered">
- <thead>
- <tr>
- <th></th>
- <?php if ($this->_dataSource->hasSortField()) : ?>
- <th class="TreeAjax-cell-sorter">
- <i title="Sortuj" class="glyphicon glyphicon-question-sign"></i>
- </th>
- <?php endif; ?>
- <?php if (true) : ?>
- <th class="TreeAjax-cell-edit">
- <i title="Edytuj rekord" class="glyphicon glyphicon-question-sign"></i>
- </th>
- <?php endif; ?>
- <?php foreach ($fieldList as $label) : ?>
- <th><?php echo $label; ?></th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <?php //$this->renderRootNode(); ?>
- <?php foreach ($nodes as $node) : ?>
- <?php //$this->renderNode($node); ?>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- <script>
- /*
- * jQuery treeTable Plugin 3.0.2
- * http://ludo.cubicphuse.nl/jquery-treetable
- *
- * Copyright 2013, Ludo van den Boom
- * Dual licensed under the MIT or GPL Version 2 licenses.
- */
- (function() {
- var $, Node, Tree, methods;
- $ = jQuery;
- Node = (function() {
- function Node(row, tree, settings) {
- var parentId;
- this.row = row;
- this.tree = tree;
- this.settings = settings;
- // TODO Ensure id/parentId is always a string (not int)
- this.id = this.row.data(this.settings.nodeIdAttr);
- // TODO Move this to a setParentId function?
- parentId = this.row.data(this.settings.parentIdAttr);
- if (parentId != null && parentId !== "") {
- this.parentId = parentId;
- }
- this.treeCell = $(this.row.children(this.settings.columnElType)[this.settings.column]);
- this.expander = $(this.settings.expanderTemplate);
- this.indenter = $(this.settings.indenterTemplate);
- this.children = [];
- this.initialized = false;
- this.treeCell.prepend(this.indenter);
- }
- Node.prototype.addChild = function(child) {
- return this.children.push(child);
- };
- Node.prototype.ancestors = function() {
- var ancestors, node;
- node = this;
- ancestors = [];
- while (node = node.parentNode()) {
- ancestors.push(node);
- }
- return ancestors;
- };
- Node.prototype.collapse = function() {
- this._hideChildren();
- this.row.removeClass("expanded").addClass("collapsed");
- this.expander.attr("title", this.settings.stringExpand);
- if (this.initialized && this.settings.onNodeCollapse != null) {
- this.settings.onNodeCollapse.apply(this);
- }
- return this;
- };
- // TODO destroy: remove event handlers, expander, indenter, etc.
- Node.prototype.expand = function() {
- if (this.initialized && this.settings.onNodeExpand != null) {
- this.settings.onNodeExpand.apply(this);
- }
- this.row.removeClass("collapsed").addClass("expanded");
- if ($(this.row).is(":visible")) {
- this._showChildren();
- }
- this.expander.attr("title", this.settings.stringCollapse);
- return this;
- };
- Node.prototype.expanded = function() {
- return this.row.hasClass("expanded");
- };
- Node.prototype.hide = function() {
- this._hideChildren();
- this.row.hide();
- return this;
- };
- Node.prototype.isBranchNode = function() {
- if(this.children.length > 0 || this.row.data(this.settings.branchAttr) === true) {
- return true;
- } else {
- return false;
- }
- };
- Node.prototype.updateBranchLeafClass = function(){
- this.row.removeClass('branch');
- this.row.removeClass('leaf');
- this.row.addClass(this.isBranchNode()?'branch':'leaf');
- };
- Node.prototype.level = function() {
- return this.ancestors().length;
- };
- Node.prototype.parentNode = function() {
- if (this.parentId != null) {
- return this.tree[this.parentId];
- } else {
- return null;
- }
- };
- Node.prototype.removeChild = function(child) {
- var i = $.inArray(child, this.children);
- return this.children.splice(i, 1)
- };
- Node.prototype.render = function() {
- var handler,
- settings = this.settings,
- target;
- if (settings.expandable === true && this.isBranchNode()) {
- handler = function(e) {
- $(this).parents("table").treetable("node", $(this).parents("tr").data(settings.nodeIdAttr)).toggle();
- return e.preventDefault();
- };
- this.indenter.html(this.expander);
- target = settings.clickableNodeNames === true ? this.treeCell : this.expander;
- target.off("click.treetable").on("click.treetable", handler);
- target.off("keydown.treetable").on("keydown.treetable", function(e) {
- if (e.keyCode == 13) {
- handler.apply(this, [e]);
- }
- });
- }
- if (settings.expandable === true && settings.initialState === "collapsed") {
- this.collapse();
- } else {
- this.expand();
- }
- this.indenter[0].style.paddingLeft = "" + (this.level() * settings.indent) + "px";
- return this;
- };
- Node.prototype.reveal = function() {
- if (this.parentId != null) {
- this.parentNode().reveal();
- }
- return this.expand();
- };
- Node.prototype.setParent = function(node) {
- if (this.parentId != null) {
- this.tree[this.parentId].removeChild(this);
- }
- this.parentId = node.id;
- this.row.data(this.settings.parentIdAttr, node.id);
- return node.addChild(this);
- };
- Node.prototype.show = function() {
- if (!this.initialized) {
- this._initialize();
- }
- this.row.show();
- if (this.expanded()) {
- this._showChildren();
- }
- return this;
- };
- Node.prototype.toggle = function() {
- if (this.expanded()) {
- this.collapse();
- } else {
- this.expand();
- }
- return this;
- };
- Node.prototype._hideChildren = function() {
- var child, _i, _len, _ref, _results;
- _ref = this.children;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- child = _ref[_i];
- _results.push(child.hide());
- }
- return _results;
- };
- Node.prototype._initialize = function() {
- this.render();
- if (this.settings.onNodeInitialized != null) {
- this.settings.onNodeInitialized.apply(this);
- }
- return this.initialized = true;
- };
- Node.prototype._showChildren = function() {
- var child, _i, _len, _ref, _results;
- _ref = this.children;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- child = _ref[_i];
- _results.push(child.show());
- }
- return _results;
- };
- return Node;
- })();
- Tree = (function() {
- function Tree(table, settings) {
- this.table = table;
- this.settings = settings;
- this.tree = {};
- // Cache the nodes and roots in simple arrays for quick access/iteration
- this.nodes = [];
- this.roots = [];
- if (typeof this.settings.router == "function") {
- var that = this;
- $(window).bind('hashchange', function() {
- //console.log('hashchange: location.hash: ' + location.hash);
- that.settings.router.apply(this);
- });
- that.settings.router.apply(this);
- }
- }
- Tree.prototype.collapseAll = function() {
- var node, _i, _len, _ref, _results;
- _ref = this.nodes;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- node = _ref[_i];
- _results.push(node.collapse());
- }
- return _results;
- };
- Tree.prototype.expandAll = function() {
- var node, _i, _len, _ref, _results;
- _ref = this.nodes;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- node = _ref[_i];
- _results.push(node.expand());
- }
- return _results;
- };
- Tree.prototype.findLastNode = function (node) {
- if (node.children.length > 0) {
- return this.findLastNode(node.children[node.children.length - 1]);
- } else {
- return node;
- }
- };
- Tree.prototype.loadRows = function(rows) {
- var node, row, i;
- if (rows != null) {
- for (i = 0; i < rows.length; i++) {
- row = $(rows[i]);
- if (row.data(this.settings.nodeIdAttr) != null) {
- node = new Node(row, this.tree, this.settings);
- this.nodes.push(node);
- this.tree[node.id] = node;
- if (node.parentId != null) {
- if (this.tree[node.parentId]) {
- this.tree[node.parentId].addChild(node);
- } else {
- //console.log('loadRows this.tree[' + node.parentId + ']', this.tree[node.parentId])
- }
- } else {
- this.roots.push(node);
- }
- }
- }
- }
- for (i = 0; i < this.nodes.length; i++) {
- node = this.nodes[i].updateBranchLeafClass();
- }
- return this;
- };
- Tree.prototype.move = function(node, destination) {
- // Conditions:
- // 1: +node+ should not be inserted as a child of +node+ itself.
- // 2: +destination+ should not be the same as +node+'s current parent (this
- // prevents +node+ from being moved to the same location where it already
- // is).
- // 3: +node+ should not be inserted in a location in a branch if this would
- // result in +node+ being an ancestor of itself.
- var nodeParent = node.parentNode();
- if (node !== destination && destination.id !== node.parentId && $.inArray(node, destination.ancestors()) === -1) {
- node.setParent(destination);
- this._moveRows(node, destination);
- // Re-render parentNode if this is its first child node, and therefore
- // doesn't have the expander yet.
- if (node.parentNode().children.length === 1) {
- node.parentNode().render();
- }
- }
- if(nodeParent){
- nodeParent.updateBranchLeafClass();
- }
- if(node.parentNode()){
- node.parentNode().updateBranchLeafClass();
- }
- node.updateBranchLeafClass();
- return this;
- };
- Tree.prototype.render = function() {
- var root, _i, _len, _ref;
- _ref = this.roots;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- root = _ref[_i];
- // Naming is confusing (show/render). I do not call render on node from
- // here.
- root.show();
- }
- return this;
- };
- Tree.prototype._moveRows = function(node, destination) {
- var child, _i, _len, _ref, _results;
- node.row.insertAfter(destination.row);
- node.render();
- _ref = node.children;
- _results = [];
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- child = _ref[_i];
- _results.push(this._moveRows(child, node));
- }
- return _results;
- };
- Tree.prototype.unloadBranch = function(node) {
- var child, children, i;
- for (i = 0; i < node.children.length; i++) {
- child = node.children[i];
- // Recursively remove all descendants of +node+
- this.unloadBranch(child);
- // Remove child from DOM (<tr>)
- child.row.remove();
- // Clean up Tree object (so Node objects are GC-ed)
- delete this.tree[child.id];
- this.nodes.splice($.inArray(child, this.nodes), 1);
- }
- // Reset node's collection of children
- node.children = [];
- node.updateBranchLeafClass();
- return this;
- };
- Tree.prototype.updateNodeHtmlData = function(id, newDataHtml) {
- var draggedNode = this.tree[id]
- // draggedNode = this.data("treetable").tree[id];// $("#<?php echo $this->_htmlID; ?>").treetable("node", node.id);
- , cellsOld = draggedNode.row.find('td')
- , cellsNew = $(newDataHtml).find('td')
- , cellsOldCnt = cellsOld.length
- , cellsNewCnt = cellsNew.length;
- if (cellsOldCnt > 1 && cellsOldCnt == cellsNewCnt) {
- for(var i=1; i<cellsOldCnt; i++){
- $(cellsOld[i]).replaceWith(cellsNew[i]);
- }
- }
- return this;
- };
- return Tree;
- })();
- // jQuery Plugin
- methods = {
- init: function(options, force) {
- var settings;
- settings = $.extend({
- branchAttr: "ttBranch",
- clickableNodeNames: false,
- column: 0,
- columnElType: "td", // i.e. 'td', 'th' or 'td,th'
- expandable: false,
- expanderTemplate: "<a href='#'> </a>",
- indent: 19,
- indenterTemplate: "<span class='indenter'></span>",
- initialState: "collapsed",
- nodeIdAttr: "ttId", // maps to data-tt-id
- parentIdAttr: "ttParentId", // maps to data-tt-parent-id
- stringExpand: "Expand",
- stringCollapse: "Collapse",
- router: null, // router
- // Events
- onInitialized: null,
- onNodeCollapse: null,
- onNodeExpand: null,
- onNodeInitialized: null
- }, options);
- return this.each(function() {
- var el = $(this), tree;
- if (force || el.data("treetable") === undefined) {
- tree = new Tree(this, settings);
- tree.loadRows(this.rows).render();
- el.addClass("treetable").data("treetable", tree);
- if (settings.onInitialized != null) {
- settings.onInitialized.apply(tree);
- }
- }
- return el;
- });
- },
- destroy: function() {
- return this.each(function() {
- return $(this).removeData("treetable").removeClass("treetable");
- });
- },
- collapseAll: function() {
- this.data("treetable").collapseAll();
- return this;
- },
- collapseNode: function(id) {
- var node = this.data("treetable").tree[id];
- if (node) {
- node.collapse();
- } else {
- throw new Error("Unknown node '" + id + "'");
- }
- return this;
- },
- expandAll: function() {
- this.data("treetable").expandAll();
- return this;
- },
- expandNode: function(id) {
- var node = this.data("treetable").tree[id];
- if (node) {
- if (!node.initialized) {
- node._initialize();
- }
- node.expand();
- } else {
- throw new Error("Unknown node '" + id + "'");
- }
- return this;
- },
- loadBranch: function(node, rows) {
- var settings = this.data("treetable").settings,
- tree = this.data("treetable").tree;
- // TODO Switch to $.parseHTML
- rows = $(rows);
- if (node == null) { // Inserting new root nodes
- this.append(rows);
- } else {
- var lastNode = this.data("treetable").findLastNode(node);
- rows.insertAfter(lastNode.row);
- }
- this.data("treetable").loadRows(rows);
- // Make sure nodes are properly initialized
- rows.filter("tr").each(function() {
- tree[$(this).data(settings.nodeIdAttr)].show();
- });
- return this;
- },
- move: function(nodeId, destinationId) {
- var destination, node;
- node = this.data("treetable").tree[nodeId];
- destination = this.data("treetable").tree[destinationId];
- this.data("treetable").move(node, destination);
- return this;
- },
- node: function(id) {
- return this.data("treetable").tree[id];
- },
- reveal: function(id) {
- var node = this.data("treetable").tree[id];
- if (node) {
- node.reveal();
- } else {
- throw new Error("Unknown node '" + id + "'");
- }
- return this;
- },
- unloadBranch: function(node) {
- this.data("treetable").unloadBranch(node);
- return this;
- },
- updateNode: function(id, params) {
- var params = params || {};
- if ('html' in params) {
- this.data("treetable").updateNodeHtmlData(id, params.html);
- }
- else {
- var that = this;
- $.ajax({
- data: {},
- type: "GET",
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=NODE&ID=' + id
- })
- .done(function(data, textStatus, jqXHR){
- //console.log('move-info: ', data);
- //$("#<?php echo $this->_htmlID; ?>").treetable("updateNode", id, {html: jqXHR.responseText});
- that.data("treetable").updateNodeHtmlData(id, jqXHR.responseText);
- jQuery.notify('Aktualizacja danych ' + id, 'success');
- })
- .fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
- var txt = jqXHR.responseText || 'Error';
- if (jqXHR.status == 404) {
- jQuery.notify(jqXHR.responseText, 'error');
- } else {
- jQuery.notify(jqXHR.responseText, 'warn');
- }
- });
- }
- }
- };
- $.fn.treetable = function(method) {
- if (methods[method]) {
- return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
- } else if (typeof method === 'object' || !method) {
- return methods.init.apply(this, arguments);
- } else {
- return $.error("Method " + method + " does not exist on jQuery.treetable");
- }
- };
- // Expose classes to world
- this.TreeTable || (this.TreeTable = {});
- this.TreeTable.Node = Node;
- this.TreeTable.Tree = Tree;
- }).call(this);
- </script>
- <script>
- function treeAjaxBackToTree() {
- var cont = jQuery('#<?php echo $this->_htmlID; ?>').parent();
- cont.show();
- var taskCont = jQuery('#<?php echo $this->_htmlID . '_task'; ?>').parent();
- taskCont.remove();
- // TODO: reload data?
- //var con = jQuery('#<?php echo $this->_htmlID; ?>');
- //con.TableAjaxLoadPage(0);// TODO: load current page
- }
- jQuery(document).ready(function(){
- /*
- jQuery('#<?php echo $this->_htmlID; ?>').TableAjax({
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>',
- debug: true, // TODO: DBG
- height: 400 // TODO: ?
- });
- */
- var table = $("#<?php echo $this->_htmlID; ?>");
- function initializeTableRow() {
- var n = $(this);
- // Highlight on press
- n.on("mousedown", function() {
- $("tr.selected").removeClass("selected");
- $(this).addClass("selected");
- });
- // Drag & Drop Example Code
- n.find(".folder").on("mouseenter", function() {
- var el = $(this);
- if(!el.data("dndInit")) {
- el.data("dndInit", true);
- el.draggable({
- helper: "clone",
- opacity: .75,
- refreshPositions: true, // Performance?
- revert: "invalid",
- revertDuration: 300,
- scroll: true
- });
- }
- });
- // add droppable
- n.droppable({
- accept: ".file, .folder",
- drop: function(e, ui) {
- var droppedEl = ui.draggable.parents("tr")
- , node = $("#<?php echo $this->_htmlID; ?>").treetable("node", droppedEl.data("ttId"))
- , this_ttId = $(this).data("ttId");
- $(this).find(".placeholder").remove();
- // Update server-side tree
- $.ajax({
- data: { MOVE_TO_ID: this_ttId },
- type: "POST",
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=MOVE&ID=' + node.id,
- })
- .done(function(data, textStatus, jqXHR){
- //console.log('move-info: ', data);
- $("#<?php echo $this->_htmlID; ?>").treetable("move", node.id, this_ttId);
- $("#<?php echo $this->_htmlID; ?>").treetable("updateNode", node.id, {html: jqXHR.responseText});
- jQuery.notify('Przeniesienie udane', 'success');
- })
- .fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
- var txt = jqXHR.responseText || 'Error';
- if (jqXHR.status == 404) {
- jQuery.notify(jqXHR.responseText, 'error');
- } else {
- jQuery.notify(jqXHR.responseText, 'warn');
- }
- });
- },
- hoverClass: "accept",
- over: function(e, ui) {
- var droppedEl = ui.draggable.parents("tr");
- if(this != droppedEl[0] && !$(this).is(".expanded")) {
- $("#<?php echo $this->_htmlID; ?>").treetable("expandNode", $(this).data("ttId"));
- }
- }
- });
- /*
- .sortable({
- items: "tr:not(.placeholder)",
- sort: function() {
- // gets added unintentionally by droppable interacting with sortable
- // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
- $(this).removeClass("ui-state-default");
- }
- });
- */
- // add tooltips
- if (false) n.find('.TreeAjax-cell-data').each(function(ind, n){
- var title;
- //console.log('TreeAjax-cell-data.each(n:', n, n.firstChild);
- if(n.firstChild) {
- title = jQuery(n.firstChild).data('original-title');
- }
- if (title) {
- jQuery(n).tooltip({
- title: title,
- html: true,
- container: 'body',
- trigger: 'hover',
- placement: 'top',
- delay: {
- show: 500,
- hide: 100
- }
- });
- }
- });
- }
- table.treetable({
- expandable: true,
- onInitialized: function() {
- //console.log('onInitialized...');
- var node;
- // Render loader/spinner while loading
- $.ajax({
- async: false, // Must be false, otherwise loadBranch happens after showChildren?
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=ROOT_NODES',
- })
- .done(function(html) {
- if ($.trim(html) == '') {
- if (node.indenter) {
- $(node.indenter).empty();// no children -> remove indenter
- }
- return false;
- }
- var rows = $(html).filter("tr");
- rows.find(".folder").parents("tr").each(function() {
- initializeTableRow.apply(this);
- })
- table.treetable("loadBranch", node, rows);
- });
- },
- router: function () {
- var routes = {
- EDIT: function (args) {
- var recordID = args;
- if (typeof args == 'object') {
- recordID = args.shift();
- recordID = parseInt(recordID);
- }
- if (typeof recordID !== 'number' || recordID <= 0) {
- // TODO: msg
- return false;
- }
- var cont = jQuery('#<?php echo $this->_htmlID; ?>').parent();
- cont.hide();
- // remove previous task content
- var taskCnt = jQuery('#<?php echo $this->_htmlID; ?>_task');
- taskCnt.parent().remove();
- taskCnt.remove();
- var taskCont = jQuery('<div class="AjaxTreeCont"></div>').insertBefore(cont);
- jQuery('<ul class="breadcrumb">' +
- '<li><a href="#" onclick="return treeAjaxBackToTree();"><?php echo $this->getLabelHtml(); ?></a> <span class="divider">/</span></li>' +
- '<li class="active">Edytuj rekord</li>' +
- '</ul>').appendTo(taskCont);
- taskCnt = jQuery('<div id="<?php echo $this->_htmlID . '_task'; ?>" class="AjaxTableTaskCnt AjaxTable-loading"></div>').appendTo(taskCont);
- jQuery('<span class="loading-info"> loading ...</span>').appendTo(taskCnt);
- jQuery.ajax({
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=EDIT&ID=' + recordID,
- type: 'GET',
- dataType: 'text',
- data: '',
- async: true,
- success: function (data) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request finished L.<?php echo __LINE__; ?>');
- jQuery(data).appendTo(taskCnt);
- taskCnt.find('.se_type-date').datepicker({
- format: "yyyy-mm-dd"
- , language: 'pl'
- , todayBtn: "linked"
- });
- taskCnt.find('.se_type-datetime').parent().datetimepicker({
- language: 'pl'
- , format: 'yyyy-MM-dd hh:mm'
- , weekStart: 1
- });
- },
- error: function (err) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request error: {0}'.f(err));
- }
- });
- //return false;
- },
- HIST: function (args) {
- var recordID = args;
- if (typeof args == 'object') {
- recordID = args.shift();
- recordID = parseInt(recordID);
- }
- if (typeof recordID !== 'number' || recordID <= 0) {
- // TODO: msg
- return false;
- }
- var cont = jQuery('#<?php echo $this->_htmlID; ?>').parent();
- cont.hide();
- // remove previous task content
- var taskCnt = jQuery('#<?php echo $this->_htmlID; ?>_task');
- taskCnt.parent().remove();
- taskCnt.remove();
- var taskCont = jQuery('<div class="AjaxTableCont"></div>').insertBefore(cont);
- jQuery('<ul class="breadcrumb">' +
- '<li><a href="#" onclick="return treeAjaxBackToTree();"><?php echo $this->getLabelHtml(); ?></a> <span class="divider">/</span></li>' +
- '<li class="active">Historia rekordu</li>' +
- '</ul>').appendTo(taskCont);
- var taskCnt = jQuery('<div id="<?php echo $this->_htmlID . '_task'; ?>" class="AjaxTableTaskCnt AjaxTable-loading"></div>').appendTo(taskCont);
- jQuery('<span class="loading-info"> loading ...</span>').appendTo(taskCnt);
- jQuery.ajax({
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=HIST&ID=' + recordID,
- type: 'GET',
- dataType: 'text',
- data: '',
- async: true,
- success: function (data) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request finished L.<?php echo __LINE__; ?>');
- jQuery(data).appendTo(taskCnt);
- },
- error: function (err) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request error: {0}'.f(err));
- }
- });
- //return false;
- },
- FILES: function tableAjaxFiles(args) {
- var recordID = args;
- if (typeof args == 'object') {
- recordID = args.shift();
- recordID = parseInt(recordID);
- }
- if (typeof recordID !== 'number' || recordID <= 0) {
- // TODO: msg
- return false;
- }
- var cont = jQuery('#<?php echo $this->_htmlID; ?>').parent();
- cont.hide();
- // remove previous task content
- var taskCnt = jQuery('#<?php echo $this->_htmlID; ?>_task');
- taskCnt.parent().remove();
- taskCnt.remove();
- var taskCont = jQuery('<div class="AjaxTableCont"></div>').insertBefore(cont);
- jQuery('<ul class="breadcrumb">' +
- '<li><a href="#" onclick="return treeAjaxBackToTree();"><?php echo $this->getLabelHtml(); ?></a> <span class="divider">/</span></li>' +
- '<li class="active">Pliki</li>' +
- '</ul>').appendTo(taskCont);
- var taskCnt = jQuery('<div id="<?php echo $this->_htmlID . '_task'; ?>" class="AjaxTable-loading"></div>').appendTo(taskCont);
- jQuery('<span class="loading-info"> loading ...</span>').appendTo(taskCnt);
- jQuery.ajax({
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=FILES&ID=' + recordID,
- type: 'GET',
- dataType: 'text',
- data: '',
- async: true,
- success: function (data) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request finished L.<?php echo __LINE__; ?>', data);
- jQuery(data).appendTo(taskCnt);
- },
- error: function (jqXHR, textStatus, errorThrown) {
- //console.log('request error:', jqXHR.status, 'txt:', jqXHR.responseText, 'err:', jqXHR);
- taskCnt.removeClass('AjaxTable-loading');
- var txt = jqXHR.responseText || 'Error';
- if (jqXHR.status == 404) {
- jQuery('<div class="container"><div class="alert alert-danger">' + txt + '</div></div>').appendTo(taskCnt);
- } else {
- jQuery('<div class="container"><div class="alert alert-danger">' + txt + '</div></div>').appendTo(taskCnt);
- }
- }
- });
- //return false;
- },
- CREATE: function tableAjaxCreate() {
- var cont = jQuery('#<?php echo $this->_htmlID; ?>').parent();
- cont.hide();
- // remove previous task content
- var taskCnt = jQuery('#<?php echo $this->_htmlID; ?>_task');
- taskCnt.parent().remove();
- taskCnt.remove();
- var taskCont = jQuery('<div class="AjaxTableCont"></div>').insertBefore(cont);
- jQuery('<ul class="breadcrumb">' +
- '<li><a href="#" onclick="return treeAjaxBackToTree();"><?php echo $this->getLabelHtml(); ?></a> <span class="divider">/</span></li>' +
- '<li class="active">Dodaj nowy rekord</li>' +
- '</ul>').appendTo(taskCont);
- taskCnt = jQuery('<div id="<?php echo $this->_htmlID . '_task'; ?>" class="AjaxTableTaskCnt AjaxTable-loading"></div>').appendTo(taskCont);
- jQuery('<span class="loading-info"> loading ...</span>').appendTo(taskCnt);
- var reqData = {};
- var forceFilterInit = <?php echo json_encode($forceFilterInit);// TODO: read from TableAjax ?>;
- if (forceFilterInit) {
- $.map(forceFilterInit, function (fltrProps, fltr) {
- reqData['ff_' + fltr] = fltrProps;
- });
- }
- jQuery.ajax({
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=CREATE',
- type: 'GET',
- dataType: 'text',
- data: reqData,
- async: true,
- success: function (data) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request finished L.<?php echo __LINE__; ?>');
- jQuery(data).appendTo(taskCnt);
- taskCnt.find('.se_type-date').datepicker({
- format: "yyyy-mm-dd"
- , language: 'pl'
- , todayBtn: "linked"
- });
- taskCnt.find('.se_type-datetime').parent().datetimepicker({
- language: 'pl'
- , format: 'yyyy-MM-dd hh:mm'
- , weekStart: 1
- });
- },
- error: function (err) {
- taskCnt.removeClass('AjaxTable-loading');
- //console.log('request error: {0}'.f(err));
- }
- });
- //return false;
- }
- };
- var routePath = location.hash;
- //console.log('location.hash: ' + routePath);
- if (location.hash == '' || location.hash == '#') {
- return false;
- }
- if (routePath.charAt(0) == '#') {
- routePath = routePath.substr(1);
- }
- //console.log('routePath: ' + routePath);
- var parts = routePath.split('/');
- var task = parts.shift();
- //console.log('task(' + task + ') parts:');
- //console.log(parts);
- if (task in routes && typeof routes[task] == 'function') {
- routes[task](parts);
- } else {
- return false;
- }
- },
- onNodeCollapse: function() {
- var node = this;
- //table.treetable("unloadBranch", node);// when unloadBranch childrens are only hidden and unnecessary ajax call on expand
- },
- onNodeExpand: function() {
- var node = this;
- // prevent ajax call if childrens are only hidden
- if (node.children && node.children.length > 0) {
- return;
- }
- // Render loader/spinner while loading
- $.ajax({
- async: false, // Must be false, otherwise loadBranch happens after showChildren?
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=CHILDREN&ID=' + node.id,
- })
- .done(function(html) {
- if ($.trim(html) == '') {
- $(node.indenter).empty();// no children -> remove indenter
- return false;
- }
- var rows = $(html).filter("tr");
- rows.find(".folder").parents("tr").each(function() {
- initializeTableRow.apply(this);
- })
- table.treetable("loadBranch", node, rows);
- });
- }
- });
- jQuery.fx.interval = 100;// performance tip
- var tblBody = $("#<?php echo $this->_htmlID; ?>").find("tbody");
- // treetable("sortBranch", parentId, columnOrFunction)
- tblBody.sortable({
- //connectWith: "#<?php echo $this->_htmlID; ?> tbody",
- handle: ".sort-handler",
- items: "tr:not(.ui-state-disabled)",
- activate: function(e, ui) {// == start
- //tblBody.find('.ui-state-default').addClass('ui-state-disabled');
- },
- start: function(e, ui) {
- //console.log('start: ttId(',ui.item.data('ttId'),') ttParentId(',ui.item.data('ttParentId'),') e:', e, 'ui:', ui);
- // ui.data = { ttId: "118" ttParentId: "499" }
- // TODO: set class ui-state-disabled to all tr which is not under ui.data('ttParentId')
- },
- stop: function(e, ui) {// sort finish
- },
- update: function(e, ui) {// when position change - dom update
- //console.log('update: ttId(',ui.item.data('ttId'),') ttParentId(',ui.item.data('ttParentId'),') e:', e, 'ui:', ui);
- var prev = ui.item.next('tr'),
- next = ui.item.prev('tr'),
- allowMode = false,
- reqData = {};
- //console.log('ui.item next', prev.data('ttId'), 'pId:', prev.data('ttParentId'));
- //console.log('ui.item prev', next.data('ttId'), 'pId:', next.data('ttParentId'));
- if (prev && prev.data('ttParentId') == ui.item.data('ttParentId')) {
- allowMode = true;
- reqData = {after: prev.data('ttId')}
- }
- if (next && next.data('ttParentId') == ui.item.data('ttParentId')) {
- allowMode = true;
- reqData = {before: next.data('ttId')}
- }
- if (allowMode) {// && confirm("Przenieść rekord " + ui.item.data('ttId') + "?")) {
- $.ajax({
- data: reqData,
- type: "POST",
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=MOVE_SORT&ID=' + ui.item.data('ttId'),
- })
- .done(function(data, textStatus, jqXHR){
- //console.log('move-sort-info: ', data);
- jQuery.notify('Przeniesienie udane', 'success');
- })
- .fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
- var txt = jqXHR.responseText || 'Error';
- if (jqXHR.status == 404) {
- jQuery.notify(jqXHR.responseText, 'error');
- } else {
- jQuery.notify(jqXHR.responseText, 'warn');
- }
- });
- } else {
- if (!allowMode) {
- var notAllowMsg = 'Błąd: nie można przenieść rekordu';
- jQuery.notify(notAllowMsg, 'error');
- }
- $(this).sortable("cancel");
- }
- }
- // , helper: function(e, ui) {// Return a helper with preserved width of cells
- // ui.children().each(function() {
- // $(this).width($(this).width());
- // });
- // return ui;
- // }
- }).disableSelection();
- });
- </script>
- <?php
- $out = ob_get_contents();
- ob_end_clean();
- return $out;
- }
- private function sendAjaxChildren($id, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- header("Content-type: text/html");
- $out = '';
- $params = array();
- $params['order_by'] = 'SORT_PRIO';
- $nodes = $this->_dataSource->getTreeNodes($id, $params);
- if (!empty($nodes)) {
- ob_start();
- ?>
- <?php foreach ($nodes as $node) : ?>
- <?php $this->renderNode($node); ?>
- <?php endforeach; ?>
- <?php
- $out = ob_get_contents();
- ob_end_clean();
- }
- echo $out;
- exit;
- }
- private function sendAjaxMove($id, $p_id, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- header("Content-type: text/html");
- $moved = $this->_dataSource->moveTreeNode($id, $p_id);
- if ($moved) {
- $node = $this->_dataSource->getTreeNode($id);
- $this->renderNode($node);
- } else {
- header("HTTP/1.0 404 Not Found");
- echo "Nie udało się przenieść {$id} pod {$p_id}";
- }
- exit;
- }
- private function sendAjaxNode($id, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- header("Content-type: text/html");
- $node = $this->_dataSource->getTreeNode($id);
- if ($node) {
- $this->renderNode($node);
- } else {
- header("HTTP/1.0 404 Not Found");
- echo "Rekord {$id} nie istnieje";
- }
- exit;
- }
- private function sendAjaxMoveSort($id, $beforeId, $afterId, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- header("Content-type: text/html");
- $moved = false;
- if ($beforeId > 0) {
- $moved = $this->_dataSource->moveTreeNodeSortBefore($id, $beforeId);
- } else if ($afterId > 0) {
- $moved = $this->_dataSource->moveTreeNodeSortAfter($id, $afterId);
- }
- if ($moved) {
- $node = $this->_dataSource->getTreeNode($id);
- $this->renderNode($node);
- } else {
- header("HTTP/1.0 404 Not Found");
- echo "Nie udało się przenieść {$id} pod {$p_id}";
- }
- exit;
- }
- /**
- * ajax url: &_task=EDIT_INLINE
- * @param $rowID - $_GET['ID']
- * @param $fieldName - $_GET['col']
- */
- private function sendAjaxEditInline($rowID, $fieldName, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- header("Content-type: text/plain");
- die('TODO: L.' . __LINE__);
- $fieldID = $this->_acl->getFieldIdByName($fieldName);
- if (!$fieldID) {
- echo "404: No field by name ({$fieldName})";
- exit;
- }
- if ($DBG) echo "fieldID: {$fieldID}\n";
- $row = $this->_dataSource->getItem($rowID);
- if (!$row) {
- echo "404: No item ID({$rowID})";
- exit;
- }
- if (!$this->_acl->isAllowed($fieldID, 'R', $row)) {
- if ($DBG) echo " R not allowed\n";
- } else {
- if ($DBG) echo " R allowed\n";
- }
- if (!$this->_acl->isAllowed($fieldID, 'W', $row)) {
- if ($DBG) echo " W not allowed\n";
- } else {
- if ($DBG) echo " W allowed\n";
- }
- $fieldVal = '';
- if ($this->_acl->isAllowed($fieldID, 'R', $row)) {
- $fieldVal = V::get($fieldName, $fieldVal, $row);
- }
- $fieldVal = V::get("f{$fieldID}", $fieldVal, $_POST);
- $vCol = $this->_acl->getField($fieldID);
- $vCol['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
- $tsValues = array();
- Lib::loadClass('Typespecial');
- $typeSpecial = Typespecial::getInstance($fieldID, $vCol['name']);
- if ($typeSpecial) {
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
- $specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'</pre>';}
- if (!empty($specialValues)) {
- $tsValues[$row->ID] = implode('<br>', $specialValues);
- }
- }
- ?>
- <label for="<?php echo "f{$fieldID}"; ?>" class="AjaxTableEdit-label">
- <strong title="<?php echo "[{$fieldID}] {$fieldName}"; ?>"><?php echo "{$vCol['label']}"; ?></strong>
- <?php if (!empty($vCol['opis'])) : ?>
- <em><?php echo $vCol['opis']; ?></em>
- <?php endif; ?>
- </label>
- <?php
- $fieldParams = array('widthClass'=>'inside-modal', 'maxGrid'=>6);
- if (!empty($tsValues[$row->ID])) {
- $fieldParams['typespecialValue'] = $tsValues[$row->ID];
- }
- $vDefault = $this->_dataSource->getColDefault($fieldName);
- if (!empty($vDefault)) {
- $fieldParams['default'] = $vDefault;
- }
- echo $this->_acl->showFormItem('W', $fieldID, "f{$fieldID}", $fieldVal, $fieldParams, $row);
- if ($typeSpecial) {
- echo '<p style="padding:100px 0;"></p>';
- }
- exit;
- }
- private function sendAjaxEditInlineSave($rowID, $fieldName, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- sleep(1);// TODO: RMME DBG loading
- die('TODO: L.' . __LINE__);
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save ID(' . $id . ') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">acl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_acl);echo'</pre>';}
- $dbID = $this->_acl->getDB();
- $db = DB::getDB($dbID);
- if (!$db) {
- header('HTTP/1.0 406 Not Acceptable');
- exit;
- }
- $tblName = $this->_acl->getName();
- $fieldID = $this->_acl->getFieldIdByName($fieldName);
- if (!$fieldID) {
- header('HTTP/1.0 404 Not Found');
- echo "404: No field by name ({$fieldName})";
- exit;
- }
- $row = $this->_dataSource->getItem($rowID);
- if (!$row) {
- header('HTTP/1.0 404 Not Found');
- echo "404: No item ID({$rowID})";
- exit;
- }
- if (!$this->_acl->isAllowed($fieldID, 'W', $row)) {
- header('HTTP/1.0 403 Forbidden');
- echo "403: field not allowed to Write ({$fieldName})";
- exit;
- } else {
- if ($DBG) echo " Write allowed\n";
- }
- $sqlObj = new stdClass();
- if (array_key_exists("f{$fieldID}", $args)) {
- $sqlObj->{$fieldName} = $args["f{$fieldID}"];
- if (empty($args["f{$fieldID}"]) && strlen($args["f{$fieldID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
- $sqlObj->{$fieldName} = $this->_acl->fixEmptyValueFromUser($fieldID);
- }
- }
- else {
- if ($DBG) echo " TODO: field value not set\n";
- }
- $sqlObj->ID = $rowID;
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">E('.$tblName.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}
- $ret = $db->UPDATE_OBJ($tblName, $sqlObj);
- if ($ret > 0) {
- echo '<div class="alert alert-success">';
- echo "Rekord zapisany pomyślnie";//"Record saved successfully";
- echo '</div>';
- } else if ($ret == 0) {
- echo '<div class="alert alert-info">';
- echo "Nie wprowadzono żadnych zmian";
- if ($db->has_errors()) {
- //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($db->get_errors());echo'</pre>';
- }
- echo '</div>';
- } else {
- echo '<div class="alert alert-danger">';
- echo '<h4>' . "Wystąpiły błędy!" . '</h4>';
- if ($db->has_errors()) {
- $errors = $db->get_errors();
- echo implode('<br>', $errors);
- }
- echo '</div>';
- }
- exit;
- }
- private function sendAjaxEdit($id, $args) {
- header("Content-type: text/plain");
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- $cols = array();
- $record = $this->_dataSource->getItem($id);
- if (!$this->_acl->canWriteRecord($record) && !$this->_acl->hasPermSuperWrite()) {
- echo '<div class="alert alert-danger">';
- echo "Brak dostępu do rekordu";// TODO: more info - reason
- echo '</div>';
- return;
- }
- $fieldsList = $this->_acl->getFields();
- foreach ($fieldsList as $kID => $vCol) {
- if ($vCol['name'] == 'ID') {
- unset($fieldsList[$kID]);
- continue;
- }
- $cols[$kID] = '';
- if ($this->_acl->isAllowed($kID, 'R', $record)) {
- $cols[$kID] = V::get($vCol['name'], '', $record);
- }
- $cols[$kID] = V::get("f{$kID}", $cols[$kID], $_POST);
- $fieldsList[$kID]['label'] = (!empty($vCol['label']))? $vCol['label'] : str_replace('_', ' ', $vCol['name']);
- }
- $tsValues = array();
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fieldsList (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fieldsList);echo'</pre>';}
- if (!empty($fieldsList)) {
- Lib::loadClass('Typespecial');
- foreach ($fieldsList as $vColID => $vCol) {
- $typeSpecial = Typespecial::getInstance($vColID, $vCol['name']);
- if ($typeSpecial) {
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$vColID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
- $colValue = V::get($vCol['name'], '', $record);
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">V::get('.$vCol['name'].', "", $record) (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($colValue);echo'</pre>';}
- $specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $record->ID, $vCol['name'], $colValue);
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$vColID.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'</pre>';}
- if (!empty($specialValues)) {
- $tsValues[$vColID] = implode('<br>', $specialValues);
- }
- }
- }
- }
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tsValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tsValues);echo'</pre>';}
- $rowFunctionsOut = '';// TODO: $this->_showRowFunctions($record->ID, array('edit', 'cp'), true);
- ?>
- <div class="container AjaxFrmHorizontalEdit">
- <form class="form-horizontal" action="" method="post" id="EDIT_FRM_<?php echo $this->_htmlID; ?>">
- <fieldset>
- <legend>Edycja rekordu Nr <?php echo $record->ID; ?><span class="pull-right valign-btns-bottom"><?php echo $rowFunctionsOut; ?></span></legend>
- <?php $tabindex = 0; foreach ($fieldsList as $kID => $vCol) : ?>
- <?php if ($this->_acl->isAllowed($kID, 'W', $record)) : ?>
- <div class="form-group">
- <label class="col-sm-3 control-label" for="<?php echo "f{$kID}"; ?>"><?php echo $vCol['label']; ?>
- <i class="glyphicon glyphicon-info-sign frm-help" data-toggle="popover" data-trigger="hover" title="" data-content="<?php echo htmlspecialchars($vCol['opis']); ?>" data-original-title="<?php echo "[{$kID}] {$vCol['name']}"; ?>"></i>
- </label>
- <div class="col-sm-9">
- <?php
- $fieldParams = array('appendBack'=>true, 'tabindex'=>(++$tabindex), 'maxGrid'=>8);
- if (!empty($tsValues[$kID])) {
- $fieldParams['typespecialValue'] = $tsValues[$kID];
- }
- echo $this->_acl->showFormItem('W', $kID, "f{$kID}", $cols[$kID], $fieldParams, $record);
- ?>
- </div>
- </div>
- <?php elseif ($this->_acl->isAllowed($kID, 'R', $record)) : ?>
- <div class="form-group">
- <label class="col-sm-3 control-label" for="<?php echo "f{$kID}"; ?>"><?php echo $vCol['label']; ?>
- <i class="glyphicon glyphicon-info-sign frm-help" data-toggle="popover" data-trigger="hover" title="" data-content="<?php echo htmlspecialchars($vCol['opis']); ?>" data-original-title="<?php echo "[{$kID}] {$vCol['name']}"; ?>"></i>
- </label>
- <div class="col-sm-9">
- <p style="margin-top:5px;">
- <?php
- //echo $this->_acl->showFormItem('R', $kID, "f{$kID}", $cols[$kID], array('appendBack'=>true), $record);
- if (!empty($tsValues[$kID])) {
- echo $tsValues[$kID];
- } else if (!empty($record->{$vCol['name']})) {
- echo $record->{$vCol['name']};
- }
- ?>
- </p>
- </div>
- </div>
- <?php endif; ?>
- <?php endforeach; ?>
- <div class="form-group">
- <div class="col-sm-offset-3 col-sm-9">
- <button type="submit" class="btn btn-primary" tabindex="<?php echo (++$tabindex); ?>">Zapisz</button>
- </div>
- </div>
- </fieldset>
- </form>
- <p style="padding:100px 0;"></p>
- </div>
- <script>
- jQuery(document).ready(function(){
- jQuery('textarea').autosize();
- jQuery('.frm-help').popover({trigger:'hover'});
- jQuery('#EDIT_FRM_<?php echo $this->_htmlID; ?>').on('submit', function(){
- var data = jQuery(this).serialize();
- // TODO: change Edit btn to return to edit record with fields -> show form
- var taskCont = jQuery('#<?php echo $this->_htmlID; ?>_task').parent();
- //taskCont.empty();
- taskCont.children().fadeOut('slow');
- var alertCntWrap = jQuery('<div class="AjaxTableAlert AjaxTable-loading"></div>').prependTo(taskCont)
- , alertCnt = jQuery('<div class="container"></div>').prependTo(alertCntWrap);
- jQuery('<div class="alert alert-danger"><div style="padding:0 0 0 20px; background:url(./icon/loading.gif) no-repeat left top;"> zapisywanie ... </div></div>').appendTo(alertCnt);
- jQuery.ajax({
- url: 'index-ajax.php?_zasobID=<?php echo $this->_zasobID; ?>&_cls=<?php echo __CLASS__; ?>&_hash=<?php echo $this->_htmlID; ?>&_task=EDIT_SAVE&ID=<?php echo $record->ID; ?>',
- type: 'POST',
- dataType: 'text',
- data: data,
- async: true,
- success: function (data) {
- alertCntWrap.removeClass('AjaxTable-loading');
- //console.log('request finished L.<?php echo __LINE__; ?>');
- alertCnt.empty();
- var out = '';
- out += data;
- out += '<div class="breadcrumb">' +
- ' <a href="#" onclick="return treeAjaxBackToTree();" class="btn btn-link btn-sm"> <i class="glyphicon glyphicon-arrow-left"></i> Wróć do tabeli <?php echo $this->getLabelHtml(); ?></a>' +
- '<span class="divider">/</span>' +
- ' <a href="#EDIT/<?php echo $id; ?>/' + Math.random(1).toString().substr(2) + '" class="btn btn-link btn-sm"> <i class="glyphicon glyphicon-pencil"></i> Edytuj rekord <?php echo $id; ?></a>' +
- '</div>';
- jQuery(out).appendTo(alertCnt);
- // update node data in tree
- $("#<?php echo $this->_htmlID; ?>").treetable("updateNode", <?php echo $id; ?>);
- },
- error: function (jhr, textStatus, errorThrown) {
- var errorTxt = jhr.responseText || 'Error';
- alertCntWrap.removeClass('AjaxTable-loading');
- //console.log('Request Error: {0}: {1}'.f(textStatus, errorThrown));
- alertCnt.empty();
- jQuery(errorTxt).appendTo(alertCnt);
- var errLinks = jQuery('<div class="breadcrumb"></div>').appendTo(alertCnt);
- jQuery('<a href="#" onclick="return treeAjaxBackToTree();"> <i class="glyphicon glyphicon-arrow-left"></i> Wróć do tabeli <?php echo $this->getLabelHtml(); ?></a>').appendTo(errLinks);
- var backToEditBtn = jQuery('<a href="#EDIT/<?php echo $id; ?>/' + Math.random(1).toString().substr(2) + '" class="btn btn-link btn-sm"> <i class="glyphicon glyphicon-pencil"></i> Popraw dane <?php echo $id; ?></a>').appendTo(errLinks);
- backToEditBtn.on('click', function(){
- alertCnt.remove();
- taskCont.children().fadeIn('slow');
- return false;
- });
- }
- });
- return false;
- });
- jQuery('#EDIT_FRM_<?php echo $this->_htmlID; ?>').find('.show-last-value input').on('input', function(e) {
- var input, btn, btnIco;
- input = jQuery(e.target);
- btn = input.next('.button-appendBack');
- btnIco = btn.find('.glyphicon');
- if (btn.attr('title') != input.val()) {
- btnIco.show();
- } else {
- btnIco.hide();
- }
- });
- jQuery('#EDIT_FRM_<?php echo $this->_htmlID; ?>').find('.show-last-value').find('.button-appendBack').on('click', function(e) {
- var input, btn, btnIco;
- btn = jQuery(e.target);
- btnIco = btn.find('.glyphicon');
- input = btn.prev();
- if (input.is('input')) {
- if (btn.attr('title') != input.val()) {
- input.val(btn.attr('title'));
- btnIco.hide();
- }
- }
- });
- });
- </script>
- <?php
- exit;
- }
- private function sendAjaxEditSave($id, $args) {
- $DBG = ('1' == V::get('DBG', '', $_REQUEST));
- sleep(1);// TODO: RMME DBG loading
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">TODO: save ID(' . $id . ') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($args);echo'</pre>';}
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">acl (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_acl);echo'</pre>';}
- $dbID = $this->_acl->getDB();
- $db = DB::getDB($dbID);
- if (!$db) {
- header('HTTP/1.0 406 Not Acceptable');
- exit;
- }
- $tblName = $this->_acl->getName();
- $record = $db->get_by_id($tblName, $id);
- if (!$this->_acl->canWriteRecord($record) && !$this->_acl->hasPermSuperWrite()) {
- echo '<div class="alert alert-danger">';
- echo "Brak dostępu do rekordu";// TODO: more info - reason
- echo '</div>';
- }
- $sqlObj = new stdClass();
- $fields = $this->_acl->getFields();
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">fields (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($fields);echo'</pre>';}
- foreach ($fields as $kID => $vField) {
- if (!$this->_acl->isAllowed($kID, 'W', $record)) {
- continue;
- }
- if (array_key_exists("f{$kID}", $args)) {
- $sqlObj->{$vField['name']} = $args["f{$kID}"];
- if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
- $sqlObj->{$vField['name']} = $this->_acl->fixEmptyValueFromUser($kID);
- }
- }
- }
- $sqlObj->ID = $id;
- if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sqlObj);echo'</pre>';}
- $ret = $db->UPDATE_OBJ($tblName, $sqlObj);
- if ($ret > 0) {
- echo '<div class="alert alert-success">';
- echo "Rekord zapisany pomyślnie";//"Record saved successfully";
- echo '</div>';
- } else if ($ret == 0) {
- echo '<div class="alert alert-info">';
- echo "Nie wprowadzono żadnych zmian";
- echo '</div>';
- } else {
- header('HTTP/1.0 404 Not Found');
- echo '<div class="alert alert-danger">';
- echo '<h4>' . "Wystąpiły błędy!" . '</h4>';
- if ($db->has_errors()) {
- $errors = $db->get_errors();
- echo implode('<br>', $errors);
- }
- echo '</div>';
- }
- exit;
- }
- public function ajaxTask($task) {
- switch ($task) {
- case 'CHILDREN': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- if ($id > 0) {
- $this->sendAjaxChildren($id, $_REQUEST);
- } else {
- echo '404';
- }
- break;
- }
- case 'ROOT_NODES': {
- $this->sendAjaxChildren(0, $_REQUEST);
- break;
- }
- case 'ADD': {
- echo 'TODO: L.' . __LINE__;
- break;
- }
- case 'MOVE': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- $p_id = V::get('MOVE_TO_ID', 0, $_REQUEST, 'int');
- if ($id > 0 && $p_id > 0) {
- $this->sendAjaxMove($id, $p_id, $_REQUEST);
- } else {
- header("HTTP/1.0 404 Not Found");
- echo 'Błędne parametry';
- exit;
- }
- break;
- }
- case 'MOVE_SORT': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- $beforeId = V::get('before', 0, $_REQUEST, 'int');
- $afterId = V::get('after', 0, $_REQUEST, 'int');
- if ($id > 0 && ($beforeId > 0 || $afterId > 0)) {
- $this->sendAjaxMoveSort($id, $beforeId, $afterId, $_REQUEST);
- } else {
- header("HTTP/1.0 404 Not Found");
- echo 'Błędne parametry';
- exit;
- }
- break;
- }
- case 'NODE': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- if ($id > 0) {
- $this->sendAjaxNode($id, $_REQUEST);
- } else {
- echo '404';
- }
- break;
- }
- case 'EDIT': { // &_task=EDIT&ID=510
- $id = V::get('ID', 0, $_REQUEST, 'int');
- if ($id > 0) {
- $this->sendAjaxEdit($id, $_REQUEST);
- } else {
- echo '404';
- }
- break;
- }
- case 'EDIT_SAVE': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- if ($id > 0) {
- $this->sendAjaxEditSave($id, $_REQUEST);
- } else {
- echo '404';
- }
- break;
- }
- case 'EDIT_INLINE': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- $col = V::get('col', '', $_REQUEST);
- if ($id > 0 && !empty($col)) {
- $this->sendAjaxEditInline($id, $col, $_REQUEST);
- } else {
- echo '404';
- }
- break;
- }
- case 'EDIT_INLINE_SAVE': {
- $id = V::get('ID', 0, $_REQUEST, 'int');
- $col = V::get('col', '', $_REQUEST);
- if ($id > 0 && !empty($col)) {
- $this->sendAjaxEditInlineSave($id, $col, $_REQUEST);
- } else {
- echo '404';
- }
- break;
- }
- default:
- //$this->sendAjaxData($_REQUEST);
- }
- }
- }
|