Subversion Repositories oidplus

Rev

Rev 277 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. class OIDplusGuid extends OIDplusObject {
  21.         private $guid;
  22.  
  23.         public function __construct($guid) {
  24.                 if (uuid_valid($guid)) {
  25.                         $this->guid = uuid_canonize($guid); // It is a real GUID (leaf node)
  26.                 } else {
  27.                         $this->guid = $guid; // It is a category name
  28.                 }
  29.         }
  30.  
  31.         public static function parse($node_id) {
  32.                 @list($namespace, $guid) = explode(':', $node_id, 2);
  33.                 if ($namespace !== 'guid') return false;
  34.                 return new self($guid);
  35.         }
  36.  
  37.         public static function objectTypeTitle() {
  38.                 return _L('Globally Unique Identifier (GUID)');
  39.         }
  40.  
  41.         public static function objectTypeTitleShort() {
  42.                 return _L('GUID');
  43.         }
  44.  
  45.         public static function ns() {
  46.                 return 'guid';
  47.         }
  48.  
  49.         public static function root() {
  50.                 return 'guid:';
  51.         }
  52.  
  53.         public function isRoot() {
  54.                 return $this->guid == '';
  55.         }
  56.  
  57.         public function nodeId($with_ns=true) {
  58.                 return $with_ns ? 'guid:'.$this->guid : $this->guid;
  59.         }
  60.  
  61.         public function addString($str) {
  62.                 if (uuid_valid($str)) {
  63.                         // real GUID
  64.                         return 'guid:'.uuid_canonize($str);
  65.                 } else {
  66.                         // just a category
  67.                         return 'guid:'.$this->guid.'/'.$str;
  68.                 }
  69.         }
  70.  
  71.         public function crudShowId(OIDplusObject $parent) {
  72.                 $tmp = explode('/',$this->guid);
  73.                 return end($tmp);
  74.         }
  75.  
  76.         public function crudInsertPrefix() {
  77.                 return '';
  78.         }
  79.  
  80.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  81.                 if ($parent == null) return $this->objectTypeTitle();
  82.                 return $this->crudShowId($parent);
  83.         }
  84.  
  85.         public function defaultTitle() {
  86.                 return $this->guid;
  87.         }
  88.  
  89.         public function isLeafNode() {
  90.                 return uuid_valid($this->guid);
  91.         }
  92.  
  93.         public function getContentPage(&$title, &$content, &$icon) {
  94.                 $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
  95.  
  96.                 if ($this->isRoot()) {
  97.                         $title = OIDplusGuid::objectTypeTitle();
  98.  
  99.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  100.                         if ($res->num_rows() > 0) {
  101.                                 $content  = _L('Please select a GUID in the tree view at the left to show its contents.');
  102.                         } else {
  103.                                 $content  = _L('Currently, no GUID is registered in the system.');
  104.                         }
  105.  
  106.                         if (!$this->isLeafNode()) {
  107.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  108.                                         $content .= '<h2>'._L('Manage root objects / categories').'</h2>';
  109.                                 } else {
  110.                                         $content .= '<h2>'._L('Available objects / categories').'</h2>';
  111.                                 }
  112.                                 $content .= '%%CRUD%%';
  113.                         }
  114.                 } else {
  115.                         $title = $this->getTitle();
  116.  
  117.                         if ($this->isLeafNode()) {
  118.                                 ob_start();
  119.                                 uuid_info($this->guid);
  120.                                 $info = ob_get_contents();
  121.                                 ob_end_clean();
  122.                                 $info = preg_replace('@:\s*(.+)\n@ismU', ": <code>\\1</code><br>", $info);
  123.  
  124.                                 $content = '<h2>'._L('Technical information').'</h2>' .
  125.                                        '<p>'._L('UUID').': <code>' . uuid_canonize($this->guid) . '</code><br>' .
  126.                                        ''._L('C++ notation').': <code>' . uuid_c_syntax($this->guid) . '</code><br>' .
  127.                                        "$info";
  128.                                 //      "<a href=\"https://misc.daniel-marschall.de/tools/uuid_mac_decoder/interprete_uuid.php?uuid=".urlencode($this->guid)."\">More technical information</a></p>";
  129.                         } else {
  130.                                 $content = '';
  131.                         }
  132.  
  133.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%';
  134.  
  135.                         if (!$this->isLeafNode()) {
  136.                                 if ($this->userHasWriteRights()) {
  137.                                         $content .= '<h2>'._L('Create or change subsequent objects / categories').'</h2>';
  138.                                 } else {
  139.                                         $content .= '<h2>'._L('Subsequent objects / categories').'</h2>';
  140.                                 }
  141.                                 $content .= '%%CRUD%%';
  142.                         }
  143.                 }
  144.         }
  145.  
  146.         // TODO: It would be nice if category and leaf items could have different pictures.
  147.         //       But the problem is, that the RA link should have a orange "GUID" icon, not a folder icon
  148.         /*
  149.         public function getIcon($row) {
  150.                 if (!$this->isLeafNode()) return null; // foldericon
  151.                 return parent::getIcon($row);
  152.         }
  153.         */
  154.  
  155.         public function one_up() {
  156.                 // A GUID is a GUID, there is no hierarchy
  157.                 return false;
  158.         }
  159.  
  160.         public function distance($to) {
  161.                 // Distance between GUIDs is not possible
  162.                 return null;
  163.         }
  164.  
  165.         public function getAltIds() {
  166.                 if ($this->isRoot()) return array();
  167.                 if (!$this->isLeafNode()) return array();
  168.                 $ids = parent::getAltIds();
  169.                 $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), _L('OID representation of UUID'));
  170.                 return $ids;
  171.         }
  172. }