Subversion Repositories oidplus

Rev

Rev 112 | 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 OIDplusJava extends OIDplusObject {
  23.         private $java;
  24.  
  25.         public function __construct($java) {
  26.                 // TODO: syntax checks
  27.                 $this->java = $java;
  28.         }
  29.  
  30.         public static function parse($node_id) {
  31.                 @list($namespace, $java) = explode(':', $node_id, 2);
  32.                 if ($namespace !== 'java') return false;
  33.                 return new self($java);
  34.         }
  35.  
  36.         public static function objectTypeTitle() {
  37.                 return "Java Package Names";
  38.         }
  39.  
  40.         public static function objectTypeTitleShort() {
  41.                 return "Package";
  42.         }
  43.  
  44.         public static function ns() {
  45.                 return 'java';
  46.         }
  47.  
  48.         public static function root() {
  49.                 return 'java:';
  50.         }
  51.  
  52.         public function isRoot() {
  53.                 return $this->java == '';
  54.         }
  55.  
  56.         public function nodeId() {
  57.                 return 'java:'.$this->java;
  58.         }
  59.  
  60.         public function addString($str) {
  61.                 if ($this->isRoot()) {
  62.                         return 'java:'.$str;
  63.                 } else {
  64.                         if (strpos($str,'.') !== false) die("Please only submit one arc.");
  65.                         return $this->nodeId() . '.' . $str;
  66.                 }
  67.         }
  68.  
  69.         public function crudShowId(OIDplusObject $parent) {
  70.                 return $this->java;
  71.         }
  72.  
  73.         public function crudInsertPrefix() {
  74.                 return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
  75.         }
  76.  
  77.         public function jsTreeNodeName(OIDplusObject $parent = null) {
  78.                 if ($parent == null) return $this->objectTypeTitle();
  79.                 return $this->java;
  80.         }
  81.  
  82.         public function defaultTitle() {
  83.                 return $this->java;
  84.         }
  85.  
  86.         public function isLeafNode() {
  87.                 return false;
  88.         }
  89.  
  90.         public function getContentPage(&$title, &$content, &$icon) {
  91.                 $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
  92.  
  93.                 if ($this->isRoot()) {
  94.                         $title = OIDplusJava::objectTypeTitle();
  95.  
  96.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = ?", array(self::root()));
  97.                         if (OIDplus::db()->num_rows($res) > 0) {
  98.                                 $content  = 'Please select a Java Package Name in the tree view at the left to show its contents.';
  99.                         } else {
  100.                                 $content  = 'Currently, no Java Package Name is registered in the system.';
  101.                         }
  102.  
  103.                         if (!$this->isLeafNode()) {
  104.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  105.                                         $content .= '<h2>Manage root objects</h2>';
  106.                                 } else {
  107.                                         $content .= '<h2>Available objects</h2>';
  108.                                 }
  109.                                 $content .= '%%CRUD%%';
  110.                         }
  111.                 } else {
  112.                         $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
  113.  
  114.                         $content .= '<h2>Description</h2>%%DESC%%'; // TODO: add more meta information about the object type
  115.  
  116.                         if (!$this->isLeafNode()) {
  117.                                 if ($this->userHasWriteRights()) {
  118.                                         $content .= '<h2>Create or change subsequent objects</h2>';
  119.                                 } else {
  120.                                         $content .= '<h2>Subsequent objects</h2>';
  121.                                 }
  122.                                 $content .= '%%CRUD%%';
  123.                         }
  124.                 }
  125.         }
  126.  
  127.         public function one_up() {
  128.                 $oid = $this->java;
  129.  
  130.                 $p = strrpos($oid, '.');
  131.                 if ($p === false) return $oid;
  132.                 if ($p == 0) return '.';
  133.  
  134.                 $oid_up = substr($oid, 0, $p);
  135.  
  136.                 return self::parse(self::ns().':'.$oid_up);
  137.         }
  138.  
  139.         public function distance($to) {
  140.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  141.                 if (!($to instanceof $this)) return false;
  142.  
  143.                 $a = $to->java;
  144.                 $b = $this->java;
  145.  
  146.                 if (substr($a,0,1) == '.') $a = substr($a,1);
  147.                 if (substr($b,0,1) == '.') $b = substr($b,1);
  148.  
  149.                 $ary = explode('.', $a);
  150.                 $bry = explode('.', $b);
  151.  
  152.                 $min_len = min(count($ary), count($bry));
  153.  
  154.                 for ($i=0; $i<$min_len; $i++) {
  155.                         if ($ary[$i] != $bry[$i]) return false;
  156.                 }
  157.  
  158.                 return count($ary) - count($bry);
  159.         }
  160. }
  161.  
  162. OIDplus::registerObjectType('OIDplusJava');
  163.