| | 470 | |
| | 471 | |
| | 472 | /** Get an interface to deal with missing nodes. If the user has the permissions, he will be asked to create a new node for that gateway id, or assign that gateway id to an existing node. |
| | 473 | * @param $gwId The unknown gwId |
| | 474 | * @return html markup |
| | 475 | */ |
| | 476 | public static function getStealOrCreateNewUI($gwId) |
| | 477 | { |
| | 478 | $permissionArray[]=array(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG'), null); |
| | 479 | $permissionArray[]=array(Permission::P('NODE_PERM_EDIT_GATEWAY_ID'), null); |
| | 480 | Security::requireAnyPermission($permissionArray); |
| | 481 | $db = AbstractDb::getObject(); |
| | 482 | $html = ''; |
| | 483 | $allowedNetworks = Security::getObjectsWithPermission(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG')); |
| | 484 | $allowedNodes = Security::getObjectsWithPermission(Permission::P('NODE_PERM_EDIT_GATEWAY_ID')); |
| | 485 | $html .= "<p>"._("Here is what you can do to fix this:")."</p>\n"; |
| | 486 | $html .= "<ul>\n"; |
| | 487 | if($allowedNetworks) { |
| | 488 | //Add a new node for unknown node id |
| | 489 | $html .= "<li>".sprintf(_("You can create a new node with %s as it's associated gateway id. This is typical for new installations."), $gwId)."<br/>\n"; |
| | 490 | |
| | 491 | $networkAdditionalWhere=" AND (FALSE\n"; |
| | 492 | foreach ($allowedNetworks as $network) { |
| | 493 | $idStr = $db->escapeString($network->getId()); |
| | 494 | $networkAdditionalWhere .= " OR network_id='$idStr'\n"; |
| | 495 | } |
| | 496 | $networkAdditionalWhere .= ")\n"; |
| | 497 | $userData['preSelectedObject']=null; |
| | 498 | $userData['allowEmpty']=true; |
| | 499 | $userData['additionalWhere']=$networkAdditionalWhere; |
| | 500 | $name = "{$gwId}_new_node_network"; |
| | 501 | $networkSelectUI = Network :: getSelectUI($name, $userData); |
| | 502 | $html .= sprintf(_("Add a new node in %s"), $networkSelectUI)." \n"; |
| | 503 | $name = "{$gwId}_new_node_submit"; |
| | 504 | $value = _("Add node"); |
| | 505 | $html .= "<input type='submit' size='10' name='{$name}' value='$value'>\n"; |
| | 506 | $html .= "</li>\n"; |
| | 507 | } |
| | 508 | |
| | 509 | if($allowedNetworks || $allowedNodes){ |
| | 510 | //"Steal" an existing node for this ID (typically for hardware replacement) |
| | 511 | $html .= "<li>".sprintf(_("You can \"steal\" an existing node. The node's gateway id will be replaced with %s. This is typical when replacing hardware."), $gwId)."<br/>\n"; |
| | 512 | if($allowedNetworks) { |
| | 513 | $additionalWhere=$networkAdditionalWhere; |
| | 514 | } |
| | 515 | else { |
| | 516 | $additionalWhere=" AND (FALSE\n"; |
| | 517 | foreach ($allowedNetworks as $network) { |
| | 518 | $idStr = $db->escapeString($node->getId()); |
| | 519 | $additionalWhere .= " OR node_id='$idStr'\n"; |
| | 520 | } |
| | 521 | $additionalWhere .= ")\n"; |
| | 522 | } |
| | 523 | |
| | 524 | $userData['preSelectedObject']=null; |
| | 525 | $userData['allowEmpty']=true; |
| | 526 | $userData['additionalWhere']=$additionalWhere; |
| | 527 | $name = "{$gwId}_steal_node"; |
| | 528 | $html .= Node :: getSelectUI($name, $userData); |
| | 529 | $name = "{$gwId}_steal_node_submit"; |
| | 530 | $value = _("Steal node"); |
| | 531 | $html .= "<input type='submit' size='10' name='{$name}' value='$value'>\n"; |
| | 532 | $html .= "</li>\n"; |
| | 533 | } |
| | 534 | $html .= "</ul>\n"; |
| | 535 | return $html; |
| | 536 | |
| | 537 | } |
| | 538 | |
| | 539 | /** |
| | 540 | * Process the interface to deal with missing nodes. |
| | 541 | * @param $gwId The unknown gwId |
| | 542 | * @param $nodeIsNew Output parameter. Will be set to true if a new node was created to resolve the situation |
| | 543 | * @return the created or stolen node object, or null if none was created (or stolen). |
| | 544 | */ |
| | 545 | public static function processStealOrCreateNewUI($gwId, &$nodeIsNew=null) |
| | 546 | { |
| | 547 | // Init values |
| | 548 | $retval = null; |
| | 549 | $nodeIsNew = false; |
| | 550 | $name = "{$gwId}_new_node_submit"; |
| | 551 | if(!empty($_REQUEST[$name])) { |
| | 552 | //Create new node |
| | 553 | $name = "{$gwId}_new_node_network"; |
| | 554 | $network = Network :: processSelectUI($name); |
| | 555 | if($network) { |
| | 556 | Security::requirePermission(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG'), $network); |
| | 557 | //echo _("Adding node"); |
| | 558 | $node = Node::createNewObject($gwId, $network); |
| | 559 | $nodeIsNew = true; |
| | 560 | $retval = $node; |
| | 561 | } |
| | 562 | } |
| | 563 | $name = "{$gwId}_steal_node_submit"; |
| | 564 | if(!empty($_REQUEST[$name])){ |
| | 565 | //"Steal" an existing node for this ID (typically for hardware replacement) |
| | 566 | $name = "{$gwId}_steal_node"; |
| | 567 | $node = Node :: processSelectUI($name); |
| | 568 | if($node) { |
| | 569 | $permissionArray[]=array(Permission::P('NETWORK_PERM_EDIT_ANY_NODE_CONFIG'), $node->getNetwork()); |
| | 570 | $permissionArray[]=array(Permission::P('NODE_PERM_EDIT_GATEWAY_ID'), $node); |
| | 571 | Security::requireAnyPermission($permissionArray); |
| | 572 | //echo _("Stealing node $node"); |
| | 573 | $node->setGatewayId($gwId); |
| | 574 | $retval = $node; |
| | 575 | } |
| | 576 | } |
| | 577 | |
| | 578 | |
| | 579 | return $retval; |
| | 580 | } |
| | 581 | |
| | 582 | |