Subversion Repositories oidplus

Rev

Rev 148 | 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusPagePublicObjects extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'public';
  25.         }
  26.  
  27.         public function priority() {
  28.                 return 0;
  29.         }
  30.  
  31.         public function action(&$handled) {
  32.         }
  33.  
  34.         public function init($html=true) {
  35.         }
  36.  
  37.         public function cfgSetValue($name, $value) {
  38.         }
  39.  
  40.         public function gui($id, &$out, &$handled) {
  41.                 if ($id === 'oidplus:system') {
  42.                         $handled = true;
  43.  
  44.                         $out['title'] = OIDplus::config()->systemTitle(); // 'Object Database of ' . $_SERVER['SERVER_NAME'];
  45.                         $out['icon'] = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/system_big.png';
  46.                         $out['text'] = file_get_contents('welcome.html');
  47.  
  48.                         if (strpos($out['text'], '%%OBJECT_TYPE_LIST%%') !== false) {
  49.                                 $tmp = '<ul>';
  50.                                 foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  51.                                         $tmp .= '<li><a '.oidplus_link($ot::root()).'>'.htmlentities($ot::objectTypeTitle()).'</a></li>';
  52.                                 }
  53.                                 $tmp .= '</ul>';
  54.                                 $out['text'] = str_replace('%%OBJECT_TYPE_LIST%%', $tmp, $out['text']);
  55.                         }
  56.  
  57.                         return $out;
  58.                 }
  59.  
  60.                 // Objects will be loaded by includes/classes/OIDplusGui.class.php , if $handled=False all page plugins were probed
  61.         }
  62.  
  63.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  64.                 if ($nonjs) {
  65.                         $json[] = array('id' => 'oidplus:system', 'icon' => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/system.png', 'text' => 'System');
  66.  
  67.                         $parent = '';
  68.                         $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($req_goto));
  69.                         while ($row = OIDplus::db()->fetch_object($res)) {
  70.                                 $parent = $row->parent;
  71.                         }
  72.  
  73.                         $objTypesChildren = array();
  74.                         foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  75.                                 $icon = 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png';
  76.                                 $json[] = array('id' => $ot::root(), 'icon' => $icon, 'text' => $ot::objectTypeTitle());
  77.  
  78.                                 try {
  79.                                         $tmp = OIDplusObject::parse($req_goto);
  80.                                 } catch (Exception $e) {
  81.                                         $tmp = null;
  82.                                 }
  83.                                 if (!is_null($tmp) && ($ot == get_class($tmp))) {
  84.                                         // TODO: Instead of just having 3 levels (parent, this and children), it would be better if we'd had a full tree of all parents
  85.                                         //       on the other hand, for giving search engines content, this is good enough
  86.                                         if (empty($parent)) {
  87.                                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where " .
  88.                                                                                    "parent = ? or " .
  89.                                                                                    "id = ? " .
  90.                                                                                    "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto));
  91.                                         } else {
  92.                                                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where " .
  93.                                                                                    "parent = ? or " .
  94.                                                                                    "id = ? or " .
  95.                                                                                    "id = ? ".
  96.                                                                                    "order by ".OIDplus::db()->natOrder('id'), array($req_goto, $req_goto, $parent));
  97.                                         }
  98.  
  99.                                         $z_used = 0;
  100.                                         $y_used = 0;
  101.                                         $x_used = 0;
  102.                                         $stufe = 0;
  103.                                         $menu_entries = array();
  104.                                         $stufen = array();
  105.                                         while ($row = OIDplus::db()->fetch_object($res)) {
  106.                                                 $obj = OIDplusObject::parse($row->id);
  107.                                                 if (is_null($obj)) continue; // might happen if the objectType is not available/loaded
  108.                                                 if (!$obj->userHasReadRights()) continue;
  109.                                                 $txt = $row->title == '' ? '' : ' -- '.htmlentities($row->title);
  110.  
  111.                                                 if ($row->id == $parent) { $stufe=0; $z_used++; }
  112.                                                 if ($row->id == $req_goto) { $stufe=1; $y_used++; }
  113.                                                 if ($row->parent == $req_goto) { $stufe=2; $x_used++; }
  114.  
  115.                                                 $menu_entry = array('id' => $row->id, 'icon' => '', 'text' => $txt, 'indent' => 0);
  116.                                                 $menu_entries[] = $menu_entry;
  117.                                                 $stufen[] = $stufe;
  118.                                         }
  119.                                         if ($x_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 2) $menu_entry['indent'] += 1;
  120.                                         if ($y_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 1) $menu_entry['indent'] += 1;
  121.                                         if ($z_used) foreach ($menu_entries as $i => &$menu_entry) if ($stufen[$i] >= 0) $menu_entry['indent'] += 1;
  122.                                         $json = array_merge($json, $menu_entries);
  123.                                 }
  124.                         }
  125.  
  126.                         return true;
  127.                 } else {
  128.                         if (isset($req_goto)) {
  129.                                 $goto = $req_goto;
  130.                                 $path = array();
  131.                                 while (true) {
  132.                                         $path[] = $goto;
  133.                                         $res = OIDplus::db()->query("select parent from ".OIDPLUS_TABLENAME_PREFIX."objects where id = ?", array($goto));
  134.                                         if (OIDplus::db()->num_rows($res) == 0) break;
  135.                                         $row = OIDplus::db()->fetch_array($res);
  136.                                         $goto = $row['parent'];
  137.                                         if ($goto == '') continue;
  138.                                 }
  139.  
  140.                                 $goto_path = array_reverse($path);
  141.                         } else {
  142.                                 $goto_path = null;
  143.                         }
  144.  
  145.                         $objTypesChildren = array();
  146.                         foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
  147.                                 $child = array('id' => $ot::root(),
  148.                                                'text' => $ot::objectTypeTitle(),
  149.                                                'state' => array("opened" => true),
  150.                                                'icon' => 'plugins/objectTypes/'.$ot::ns().'/img/treeicon_root.png',
  151.                                                'children' => OIDplusTree::tree_populate($ot::root(), $goto_path)
  152.                                                );
  153.                                 if (!file_exists($child['icon'])) $child['icon'] = null; // default icon (folder)
  154.                                 $objTypesChildren[] = $child;
  155.                         }
  156.  
  157.                         $json[] = array(
  158.                                 'id' => "oidplus:system",
  159.                                 'text' => "Objects",
  160.                                 'state' => array(
  161.                                         "opened" => true,
  162.                                         // "selected" => true)  // "selected" ist buggy: 1) Das select-Event wird beim Laden nicht gefeuert 2) Die direkt untergeordneten Knoten lassen sich nicht öffnen (laden für ewig)
  163.                                 ),
  164.                                 'icon' => 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/system.png',
  165.                                 'children' => $objTypesChildren
  166.                         );
  167.  
  168.                         return true;
  169.                 }
  170.         }
  171.  
  172.         public function tree_search($request) {
  173.                 $ary = array();
  174.                 if ($obj = OIDplusObject::parse($request)) {
  175.                         if ($obj->userHasReadRights()) {
  176.                                 do {
  177.                                         $ary[] = $obj->nodeId();
  178.                                 } while ($obj = $obj->getParent());
  179.                                 $ary = array_reverse($ary);
  180.                         }
  181.                 }
  182.                 return $ary;
  183.         }
  184. }
  185.  
  186. OIDplus::registerPagePlugin(new OIDplusPagePublicObjects());
  187.