20 const firstOrder =
"::first";
21 const autoOrder =
"::auto";
49 $familyIds = self::getFamilyInherits($familyId);
51 foreach ($familyIds as $familyId) {
53 foreach ($relativeOrders as $relativeOrder) {
54 if ($relativeOrder[
"family"] == $familyId) {
55 $subTree[$relativeOrder[
"id"]] = $relativeOrder;
58 self::updateAttributeTree($tree, $subTree);
60 self::checkTree($tree);
61 $linear = self::linearOrderTree($tree);
62 foreach ($relativeOrders as $relativeOrder) {
63 if (array_search($relativeOrder[
"id"], $linear) ===
false) {
64 $linear[] = $relativeOrder[
"id"];
66 if ($relativeOrder[
"parent"] && array_search($relativeOrder[
"parent"], $linear) ===
false) {
67 $linear[] = $relativeOrder[
"parent"];
80 $familyIds = self::getFamilyInherits($familyId);
82 foreach ($familyIds as $familyId) {
84 foreach ($attributes as $oneAttribute) {
85 if ($oneAttribute[
"family"] == $familyId) {
86 $famAttribute[$oneAttribute[
"id"]] = $oneAttribute;
89 self::completeForNumericOrderByFamilyLevel($famAttribute);
90 foreach ($famAttribute as $oneAttribute) {
91 $key = $oneAttribute[
"id"] .
"/" . $oneAttribute[
"family"];
92 $attributes[$key] = $oneAttribute;
96 uasort($attributes,
function (
$a, $b)
98 return self::sortAttributeCallBackfunction(
$a, $b);
108 foreach ($familyAttributes as & $anAttribute) {
109 $anAttribute[
"structLevel"] = self::getStructLevel($anAttribute[
"id"], $familyAttributes);
110 if ($anAttribute[
"numOrder"] === 0 || (is_float($anAttribute[
"numOrder"]))) {
111 $absOrder = self::getNumericOrder($anAttribute[
"id"], $familyAttributes);
112 if ($anAttribute[
"numOrder"] === 0) {
113 $anAttribute[
"numOrder"] = $absOrder;
115 $anAttribute[
"numOrder"] = min($anAttribute[
"numOrder"], $absOrder);
118 $anAttribute[
"familyLevel"] = self::getFamilyLevel($anAttribute[
"family"]);
121 uasort($familyAttributes,
function (
$a, $b)
123 return self::sortAttributeCallBackfunction(
$a, $b);
125 foreach ($familyAttributes as & $anAttribute) {
126 if (!$anAttribute[
"prev"]) {
127 $anAttribute[
"prev"] = self::getPrevious($anAttribute[
"id"], $familyAttributes);
130 uasort($familyAttributes,
function (
$a, $b)
132 if (
$a[
"numOrder"] > $b[
"numOrder"]) {
135 if (
$a[
"numOrder"] < $b[
"numOrder"]) {
145 private static function debug(array
$r, $text =
"")
147 printf(
"\n========= %s======== \n", $text);
149 $first = current($r);
150 printf(
"%20s|",
"index");
151 foreach (array_keys($first) as $h) {
157 foreach ($r as $k => $sr) {
159 foreach ($sr as $item) {
160 printf(
"%20s|", $item);
168 if (
$a[
"structLevel"] > $b[
"structLevel"]) {
171 if (
$a[
"structLevel"] < $b[
"structLevel"]) {
174 if (
$a[
"numOrder"] > $b[
"numOrder"]) {
177 if (
$a[
"numOrder"] < $b[
"numOrder"]) {
191 return count(self::getFamilyInherits(
$famid));
202 static $inherits = [];
204 if (empty($inherits[$familyId])) {
205 $tfromid[] = $familyId;
206 $childfamilyId = $familyId;
207 while ($childfamilyId =
getFamFromId(
"", $childfamilyId)) {
208 $tfromid[] = $childfamilyId;
210 $inherits[$familyId] = array_reverse($tfromid);
212 return $inherits[$familyId];
226 while (!empty($attributes[$attrid][
"parent"])) {
227 $attrid = $attributes[$attrid][
"parent"];
230 throw new \Dcp\Core\Exception(
"ATTR0214", $attrid);
243 protected static function getPrevious($attrid, array $sortedAttributes)
245 $attr = $sortedAttributes[$attrid];
246 $familyLevel = $attr[
"familyLevel"];
247 $structLevel = $attr[
"structLevel"];
249 $previous = self::firstOrder;
252 if ($attribute[
"id"] === $attrid) {
255 if ($attribute[
"familyLevel"] <= $familyLevel && $attribute[
"structLevel"] === $structLevel) {
256 if ($attribute[
"parent"] ===
$parent) {
257 $previous = $attribute[
"id"];
278 if ($attribute[
"parent"] === $attrid) {
279 if ($attribute[
"numOrder"] === 0) {
280 $attribute[
"numOrder"] = self::getNumericOrder($attribute[
"id"], $attributes);
284 $num = $attribute[
"numOrder"] - 0.5;
286 $num = min($num, $attribute[
"numOrder"] - 0.5);
303 foreach ($tree as $node) {
305 $linearOrder[] = $node[
"id"];
306 $linearOrder = array_merge($linearOrder, self::linearOrderTree($node[
"content"]));
322 foreach ($tree as $node) {
323 if ($node[
"id"] === $attrid) {
324 return $node[
"content"];
326 $content = self::getTreeContent($node[
"content"], $attrid,
false);
327 if ($content !==
false) {
343 foreach ($tree as $kNode => & $node) {
344 if ($node[
"id"] === $attrid) {
346 unset($tree[$kNode]);
349 $content = self::deleteNode($node[
"content"], $attrid);
350 if ($content !==
false) {
364 foreach ($onlyFamilyTree as $attrid => $order) {
365 self::updateAttributeTreeItem($tree, $attrid, $onlyFamilyTree);
380 foreach ($tree as $child) {
381 if (!isset($child[
"parent"])) {
382 $child[
"parent"] =
"";
385 if ($child[
"parent"] !==
$parent) {
389 if ($child[
"content"]) {
390 self::checkTree($child[
"content"], $child[
"id"]);
403 $parent = (!empty($orders[$attrid][
"parent"])) ? $orders[$attrid][
"parent"] :
false;
404 $prev = (!empty($orders[$attrid][
"prev"])) ? $orders[$attrid][
"prev"] :
false;
406 if (empty($orders[$attrid][
"isInTree"])) {
408 if (!empty($orders[$attrid][
"id"])) {
409 $node = self::deleteNode($tree, $attrid);
410 if ($node ===
false) {
411 $node = [
"id" => $attrid,
"content" => []];
414 $node = [
"id" => $attrid,
"content" => self::getTreeContent($tree, $attrid) ];
418 if ($prev === self::autoOrder) {
420 } elseif ($prev === self::firstOrder) {
421 array_unshift($tree, $node);
423 if (empty($orders[$prev][
"isInTree"])) {
424 self::updateAttributeTreeItem($tree, $prev, $orders);
427 self::insertAfter($tree, $prev, $node);
431 if (empty($orders[
$parent][
"isInTree"])) {
432 self::updateAttributeTreeItem($tree, $parent, $orders);
434 if ($prev === self::autoOrder) {
435 self::appendNode($tree, $parent, $node);
436 } elseif ($prev === self::firstOrder) {
437 self::prependNode($tree, $parent, $node);
439 if (empty($orders[$prev][
"isInTree"])) {
440 self::updateAttributeTreeItem($tree, $prev, $orders);
442 self::insertAfter($tree, $prev, $node);
445 $orders[$attrid][
"isInTree"] =
true;
449 protected static function insertAfter(array & $array, $ref, array & $new)
451 foreach ($array as $k => & $v) {
452 if ($v[
"id"] == $ref) {
453 array_splice($array, $k + 1, 0, [$new]);
456 if (self::insertAfter($v[
"content"], $ref, $new)) {
465 foreach ($array as $k => & $v) {
467 array_unshift($v[
"content"], $new);
470 if (self::prependNode($v[
"content"],
$parent, $new)) {
479 foreach ($array as $k => & $v) {
481 $v[
"content"][] = $new;
484 if (self::appendNode($v[
"content"],
$parent, $new)) {
static getPrevious($attrid, array $sortedAttributes)
static updateAttributeTreeItem(array &$tree, $attrid, &$orders)
static appendNode(array &$array, $parent, array &$new)
static getFamilyInherits($familyId)
static completeForNumericOrderByFamilyLevel(array &$familyAttributes)
static getFamilyLevel($famid)
print< H1 > Check Database< i > $dbaccess</i ></H1 > $a
static getAbsoluteOrders($relativeOrders, $familyId)
input relativeOrders is a linear Array ( [0] => Array ( [id] => tst_bc500 [parent] => [family] => 529...
Exception class use exceptionCode to identifiy correctly exception.
static linearOrderTree(array $tree)
static updateAttributeTree(array &$tree, array $onlyFamilyTree)
static completeForNumericOrder(array &$attributes, $familyId)
static prependNode(array &$array, $parent, array &$new)
static getNumericOrder($attrid, array $attributes)
static sortAttributeCallBackfunction($a, $b)
static insertAfter(array &$array, $ref, array &$new)
static checkTree(array &$tree, $parent="")
static deleteNode(array &$tree, $attrid)
getFamFromId($dbaccess, $id)
static getStructLevel($attrid, array $attributes)
static getTreeContent(array $tree, $attrid, $default=[])