Subversion Repositories oidplus

Rev

Rev 247 | 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusObjectTypePluginGuid extends OIDplusObjectTypePlugin {
  23.  
  24.         public static function getPluginInformation() {
  25.                 $out = array();
  26.                 $out['name'] = 'Globally Unique Identifier (GUID)';
  27.                 $out['author'] = 'ViaThinkSoft';
  28.                 $out['version'] = null;
  29.                 $out['descriptionHTML'] = null;
  30.                 return $out;
  31.         }
  32.  
  33.         public static function getObjectTypeClassName() {
  34.                 return 'OIDplusGuid';
  35.         }
  36.  
  37. }
  38.  
  39. class OIDplusGuid extends OIDplusObject {
  40.         private $guid;
  41.  
  42.         public function __construct($guid) {
  43.                 if (uuid_valid($guid)) {
  44.                         $this->guid = uuid_canonize($guid); // It is a real GUID (leaf node)
  45.                 } else {
  46.                         $this->guid = $guid; // It is a category name
  47.                 }
  48.         }
  49.  
  50.         public static function parse($node_id) {
  51.                 @list($namespace, $guid) = explode(':', $node_id, 2);
  52.                 if ($namespace !== 'guid') return false;
  53.                 return new self($guid);
  54.         }
  55.  
  56.         public static function objectTypeTitle() {
  57.                 return "Globally Unique Identifier (GUID)";
  58.         }
  59.  
  60.         public static function objectTypeTitleShort() {
  61.                 return "GUID";
  62.         }
  63.  
  64.         public static function ns() {
  65.                 return 'guid';
  66.         }
  67.  
  68.         public static function root() {
  69.                 return 'guid:';
  70.         }
  71.  
  72.         public function isRoot() {
  73.                 return $this->guid == '';
  74.         }
  75.  
  76.         public function nodeId($with_ns=true) {
  77.                 return $with_ns ? 'guid:'.$this->guid : $this->guid;
  78.         }
  79.  
  80.         public function addString($str) {
  81.                 if (uuid_valid($str)) {
  82.                         // real GUID
  83.                         return 'guid:'.uuid_canonize($str);
  84.                 } else {
  85.                         // just a category
  86.                         return 'guid:'.$this->guid.'/'.$str;
  87.                 }
  88.         }
  89.  
  90.         public function crudShowId(OIDplusObject $parent) {
  91.                 $tmp = explode('/',$this->guid);
  92.                 return end($tmp);
  93.         }
  94.  
  95.         public function crudInsertPrefix() {
  96.                 return '';
  97.         }
  98.  
  99.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  100.                 if ($parent == null) return $this->objectTypeTitle();
  101.                 return $this->crudShowId($parent);
  102.         }
  103.  
  104.         public function defaultTitle() {
  105.                 return $this->guid;
  106.         }
  107.  
  108.         public function isLeafNode() {
  109.                 return uuid_valid($this->guid);
  110.         }
  111.  
  112.         public function getContentPage(&$title, &$content, &$icon) {
  113.                 $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
  114.  
  115.                 if ($this->isRoot()) {
  116.                         $title = OIDplusGuid::objectTypeTitle();
  117.  
  118.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  119.                         if ($res->num_rows() > 0) {
  120.                                 $content  = 'Please select a GUID in the tree view at the left to show its contents.';
  121.                         } else {
  122.                                 $content  = 'Currently, no GUID is registered in the system.';
  123.                         }
  124.  
  125.                         if (!$this->isLeafNode()) {
  126.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  127.                                         $content .= '<h2>Manage root objects / categories</h2>';
  128.                                 } else {
  129.                                         $content .= '<h2>Available objects / categories</h2>';
  130.                                 }
  131.                                 $content .= '%%CRUD%%';
  132.                         }
  133.                 } else {
  134.                         $title = $this->getTitle();
  135.  
  136.                         if ($this->isLeafNode()) {
  137.                                 ob_start();
  138.                                 uuid_info($this->guid);
  139.                                 $info = ob_get_contents();
  140.                                 ob_end_clean();
  141.                                 $info = preg_replace('@:\s*(.+)\n@ismU', ": <code>\\1</code><br>", $info);
  142.  
  143.                                 $content = "<h2>Technical information</h2><p>UUID: <code>" . uuid_canonize($this->guid) . "</code><br>" .
  144.                                        "C++ notation: <code>" . uuid_c_syntax($this->guid) . "</code><br>" .
  145.                                        "$info";
  146.                                 //      "<a href=\"https://misc.daniel-marschall.de/tools/uuid_mac_decoder/interprete_uuid.php?uuid=".urlencode($this->guid)."\">More technical information</a></p>";
  147.                         } else {
  148.                                 $content = '';
  149.                         }
  150.  
  151.                         $content .= '<h2>Description</h2>%%DESC%%';
  152.  
  153.                         if (!$this->isLeafNode()) {
  154.                                 if ($this->userHasWriteRights()) {
  155.                                         $content .= '<h2>Create or change subsequent objects / categories</h2>';
  156.                                 } else {
  157.                                         $content .= '<h2>Subsequent objects / categories</h2>';
  158.                                 }
  159.                                 $content .= '%%CRUD%%';
  160.                         }
  161.                 }
  162.         }
  163.  
  164.         // TODO: It would be nice if category and leaf items could have different pictures.
  165.         //       But the problem is, that the RA link should have a orange "GUID" icon, not a folder icon
  166.         /*
  167.         public function getIcon($row) {
  168.                 if (!$this->isLeafNode()) return null; // foldericon
  169.                 return parent::getIcon($row);
  170.         }
  171.         */
  172.  
  173.         public function one_up() {
  174.                 // A GUID is a GUID, there is no hierarchy
  175.                 return false;
  176.         }
  177.  
  178.         public function distance($to) {
  179.                 // Distance between GUIDs is not possible
  180.                 return null;
  181.         }
  182.  
  183.         public function getAltIds() {
  184.                 if ($this->isRoot()) return array();
  185.                 if (!$this->isLeafNode()) return array();
  186.                 $ids = parent::getAltIds();
  187.                 $ids[] = new OIDplusAltId('oid', uuid_to_oid($this->guid), 'OID representation of UUID');
  188.                 return $ids;
  189.         }
  190. }
  191.