Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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