Subversion Repositories oidplus

Rev

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