Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1118 | 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.         /**
  30.          * @param $java
  31.          */
  32.         public function __construct($java) {
  33.                 // TODO: syntax checks
  34.                 $this->java = $java;
  35.         }
  36.  
  37.         /**
  38.          * @param string $node_id
  39.          * @return OIDplusJava|null
  40.          */
  41.         public static function parse(string $node_id)/*: ?OIDplusJava*/ {
  42.                 @list($namespace, $java) = explode(':', $node_id, 2);
  43.                 if ($namespace !== self::ns()) return null;
  44.                 return new self($java);
  45.         }
  46.  
  47.         /**
  48.          * @return string
  49.          */
  50.         public static function objectTypeTitle(): string {
  51.                 return _L('Java Package Names');
  52.         }
  53.  
  54.         /**
  55.          * @return string
  56.          */
  57.         public static function objectTypeTitleShort(): string {
  58.                 return _L('Package');
  59.         }
  60.  
  61.         /**
  62.          * @return string
  63.          */
  64.         public static function ns(): string {
  65.                 return 'java';
  66.         }
  67.  
  68.         /**
  69.          * @return string
  70.          */
  71.         public static function root(): string {
  72.                 return self::ns().':';
  73.         }
  74.  
  75.         /**
  76.          * @return bool
  77.          */
  78.         public function isRoot(): bool {
  79.                 return $this->java == '';
  80.         }
  81.  
  82.         /**
  83.          * @param bool $with_ns
  84.          * @return string
  85.          */
  86.         public function nodeId(bool $with_ns=true): string {
  87.                 return $with_ns ? self::root().$this->java : $this->java;
  88.         }
  89.  
  90.         /**
  91.          * @param string $str
  92.          * @return string
  93.          * @throws OIDplusException
  94.          */
  95.         public function addString(string $str): string {
  96.                 if ($this->isRoot()) {
  97.                         return self::root().$str;
  98.                 } else {
  99.                         if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
  100.                         return $this->nodeId() . '.' . $str;
  101.                 }
  102.         }
  103.  
  104.         /**
  105.          * @param OIDplusObject $parent
  106.          * @return string
  107.          */
  108.         public function crudShowId(OIDplusObject $parent): string {
  109.                 return $this->java;
  110.         }
  111.  
  112.         /**
  113.          * @return string
  114.          * @throws OIDplusException
  115.          */
  116.         public function crudInsertPrefix(): string {
  117.                 return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
  118.         }
  119.  
  120.         /**
  121.          * @param OIDplusObject|null $parent
  122.          * @return string
  123.          */
  124.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  125.                 if ($parent == null) return $this->objectTypeTitle();
  126.                 return $this->java;
  127.         }
  128.  
  129.         /**
  130.          * @return string
  131.          */
  132.         public function defaultTitle(): string {
  133.                 return $this->java;
  134.         }
  135.  
  136.         /**
  137.          * @return bool
  138.          */
  139.         public function isLeafNode(): bool {
  140.                 return false;
  141.         }
  142.  
  143.         /**
  144.          * @param string $title
  145.          * @param string $content
  146.          * @param string $icon
  147.          * @return void
  148.          * @throws OIDplusException
  149.          */
  150.         public function getContentPage(string &$title, string &$content, string &$icon) {
  151.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  152.  
  153.                 if ($this->isRoot()) {
  154.                         $title = OIDplusJava::objectTypeTitle();
  155.  
  156.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  157.                         if ($res->any()) {
  158.                                 $content  = '<p>'._L('Please select a Java Package Name in the tree view at the left to show its contents.').'</p>';
  159.                         } else {
  160.                                 $content  = '<p>'._L('Currently, no Java Package Name is registered in the system.').'</p>';
  161.                         }
  162.  
  163.                         if (!$this->isLeafNode()) {
  164.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  165.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  166.                                 } else {
  167.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  168.                                 }
  169.                                 $content .= '%%CRUD%%';
  170.                         }
  171.                 } else {
  172.                         $title = $this->getTitle();
  173.  
  174.                         $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
  175.  
  176.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
  177.  
  178.                         if (!$this->isLeafNode()) {
  179.                                 if ($this->userHasWriteRights()) {
  180.                                         $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  181.                                 } else {
  182.                                         $content .= '<h2>'._L('Subordinate objects').'</h2>';
  183.                                 }
  184.                                 $content .= '%%CRUD%%';
  185.                         }
  186.                 }
  187.         }
  188.  
  189.         /**
  190.          * @return OIDplusJava|null
  191.          */
  192.         public function one_up()/*: ?OIDplusJava*/ {
  193.                 $oid = $this->java;
  194.  
  195.                 $p = strrpos($oid, '.');
  196.                 if ($p === false) return self::parse($oid);
  197.                 if ($p == 0) return self::parse('.');
  198.  
  199.                 $oid_up = substr($oid, 0, $p);
  200.  
  201.                 return self::parse(self::ns().':'.$oid_up);
  202.         }
  203.  
  204.         /**
  205.          * @param $to
  206.          * @return int|null
  207.          */
  208.         public function distance($to) {
  209.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  210.                 if (!($to instanceof $this)) return null;
  211.  
  212.                 $a = $to->java;
  213.                 $b = $this->java;
  214.  
  215.                 if (substr($a,0,1) == '.') $a = substr($a,1);
  216.                 if (substr($b,0,1) == '.') $b = substr($b,1);
  217.  
  218.                 $ary = explode('.', $a);
  219.                 $bry = explode('.', $b);
  220.  
  221.                 $min_len = min(count($ary), count($bry));
  222.  
  223.                 for ($i=0; $i<$min_len; $i++) {
  224.                         if ($ary[$i] != $bry[$i]) return null;
  225.                 }
  226.  
  227.                 return count($ary) - count($bry);
  228.         }
  229.  
  230.         /**
  231.          * @return string
  232.          */
  233.         public function getDirectoryName(): string {
  234.                 if ($this->isRoot()) return $this->ns();
  235.                 return $this->ns().'_'.md5($this->nodeId(false));
  236.         }
  237.  
  238.         /**
  239.          * @param string $mode
  240.          * @return string
  241.          */
  242.         public static function treeIconFilename(string $mode): string {
  243.                 return 'img/'.$mode.'_icon16.png';
  244.         }
  245. }
  246.