Subversion Repositories oidplus

Rev

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