Subversion Repositories oidplus

Rev

Rev 277 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

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