Subversion Repositories oidplus

Rev

Rev 1121 | Rev 1130 | 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 OIDplusPhp extends OIDplusObject {
  27.         private $php;
  28.  
  29.         /**
  30.          * @param $php
  31.          */
  32.         public function __construct($php) {
  33.                 // TODO: syntax checks
  34.                 $this->php = $php;
  35.         }
  36.  
  37.         /**
  38.          * @param string $node_id
  39.          * @return OIDplusPhp|null
  40.          */
  41.         public static function parse(string $node_id)/*: ?OIDplusPhp*/ {
  42.                 @list($namespace, $php) = explode(':', $node_id, 2);
  43.                 if ($namespace !== self::ns()) return null;
  44.                 return new self($php);
  45.         }
  46.  
  47.         /**
  48.          * @return string
  49.          */
  50.         public static function objectTypeTitle(): string {
  51.                 return _L('PHP Namespaces');
  52.         }
  53.  
  54.         /**
  55.          * @return string
  56.          */
  57.         public static function objectTypeTitleShort(): string {
  58.                 return _L('Namespace');
  59.         }
  60.  
  61.         /**
  62.          * @return string
  63.          */
  64.         public static function ns(): string {
  65.                 return 'php';
  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->php == '';
  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->php : $this->php;
  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.                         $str = ltrim($str,'\\');
  98.                         return self::root().$str;
  99.                 } else {
  100.                         if (strpos($str,'\\') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
  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.                 return $this->php;
  111.         }
  112.  
  113.         /**
  114.          * @return string
  115.          * @throws OIDplusException
  116.          */
  117.         public function crudInsertPrefix(): string {
  118.                 return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
  119.         }
  120.  
  121.         /**
  122.          * @param OIDplusObject|null $parent
  123.          * @return string
  124.          */
  125.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  126.                 if ($parent == null) return $this->objectTypeTitle();
  127.                 if ($parent->isRoot()) {
  128.                         return substr($this->nodeId(), strlen($parent->nodeId()));
  129.                 } else {
  130.                         return substr($this->nodeId(), strlen($parent->nodeId())+1);
  131.                 }
  132.         }
  133.  
  134.         /**
  135.          * @return string
  136.          */
  137.         public function defaultTitle(): string {
  138.                 return $this->php;
  139.         }
  140.  
  141.         /**
  142.          * @return bool
  143.          */
  144.         public function isLeafNode(): bool {
  145.                 return substr($this->php, -4) === '.php';
  146.         }
  147.  
  148.         /**
  149.          * @param string $title
  150.          * @param string $content
  151.          * @param string $icon
  152.          * @return void
  153.          * @throws OIDplusException
  154.          */
  155.         public function getContentPage(string &$title, string &$content, string &$icon) {
  156.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  157.  
  158.                 if ($this->isRoot()) {
  159.                         $title = OIDplusPhp::objectTypeTitle();
  160.  
  161.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  162.                         if ($res->any()) {
  163.                                 $content  = '<p>'._L('Please select a PHP Namespace in the tree view at the left to show its contents.').'</p>';
  164.                         } else {
  165.                                 $content  = '<p>'._L('Currently, no PHP Namespace is registered in the system.').'</p>';
  166.                         }
  167.  
  168.                         if (!$this->isLeafNode()) {
  169.                                 if (OIDplus::authUtils()->isAdminLoggedIn()) {
  170.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  171.                                 } else {
  172.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  173.                                 }
  174.                                 $content .= '%%CRUD%%';
  175.                         }
  176.                 } else {
  177.                         $title = $this->getTitle();
  178.  
  179.                         $content = '<h3>'.explode(':',$this->nodeId())[1].'</h3>';
  180.  
  181.                         $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
  182.  
  183.                         if (OIDplus::baseConfig()->getValue('PLUGIN_PHP_TYPE_LINK_TO_WEBFAN', false)) {
  184.                                 $content .= '<p><strong><a webfan-php-class-link="'.$this->nodeId(false).'" href="https://webfan.de/install/?source='.urlencode($this->nodeId(false)).'" target="_blank">Soure Code</a></strong></p>';
  185.                         }
  186.  
  187.                         if (!$this->isLeafNode()) {
  188.                                 if ($this->userHasWriteRights()) {
  189.                                         $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  190.                                         $content .= '<p>'._L('Append ".php" at the end to mark a node as leaf node (i.e. Interface/Class rather than Namespace)').'</p>';
  191.                                 } else {
  192.                                         $content .= '<h2>'._L('Subordinate objects').'</h2>';
  193.                                 }
  194.                                 $content .= '%%CRUD%%';
  195.                         }
  196.                 }
  197.         }
  198.  
  199.         /**
  200.          * @return OIDplusPhp|null
  201.          */
  202.         public function one_up()/*: ?OIDplusPhp*/ {
  203.                 $oid = $this->php;
  204.  
  205.                 $p = strrpos($oid, '\\');
  206.                 if ($p === false) return self::parse($oid);
  207.                 if ($p == 0) return self::parse('\\');
  208.  
  209.                 $oid_up = substr($oid, 0, $p);
  210.  
  211.                 return self::parse(self::ns().':'.$oid_up);
  212.         }
  213.  
  214.         /**
  215.          * @param $to
  216.          * @return int|null
  217.          */
  218.         public function distance($to) {
  219.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  220.                 if (!$to) return null;
  221.                 if (!($to instanceof $this)) return null;
  222.  
  223.                 $a = $to->php;
  224.                 $b = $this->php;
  225.  
  226.                 if (substr($a,0,1) == '\\') $a = substr($a,1);
  227.                 if (substr($b,0,1) == '\\') $b = substr($b,1);
  228.  
  229.                 $ary = explode('\\', $a);
  230.                 $bry = explode('\\', $b);
  231.  
  232.                 $min_len = min(count($ary), count($bry));
  233.  
  234.                 for ($i=0; $i<$min_len; $i++) {
  235.                         if ($ary[$i] != $bry[$i]) return null;
  236.                 }
  237.  
  238.                 return count($ary) - count($bry);
  239.         }
  240.  
  241.         /**
  242.          * @return string
  243.          */
  244.         public function getDirectoryName(): string {
  245.                 if ($this->isRoot()) return $this->ns();
  246.                 if (OIDplus::baseConfig()->getValue('PLUGIN_PHP_TYPE_LINK_TO_WEBFAN', false)) {
  247.                         return $this->ns().str_replace(['\\', "/"], [\DIRECTORY_SEPARATOR, \DIRECTORY_SEPARATOR], $this->nodeId(false));
  248.                 } else {
  249.                         return $this->ns().'_'.md5($this->nodeId(false));
  250.                 }
  251.         }
  252.  
  253.         /**
  254.          * @param string $mode
  255.          * @return string
  256.          */
  257.         public static function treeIconFilename(string $mode): string {
  258.                 return 'img/'.$mode.'_icon16.png';
  259.         }
  260. }
  261.