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