Subversion Repositories oidplus

Rev

Rev 1316 | 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 OIDplusMenuUtils extends OIDplusBaseClass {
  27.  
  28.         /**
  29.          * @return string
  30.          * @throws OIDplusConfigInitializationException
  31.          * @throws OIDplusException
  32.          */
  33.         public function nonjs_menu(): string {
  34.                 $json = array();
  35.  
  36.                 $static_node_id = $_REQUEST['goto'] ?? 'oidplus:system';
  37.  
  38.                 foreach (OIDplus::getPagePlugins() as $plugin) {
  39.                         // Note: The system (OIDplusMenuUtils) does only show the menu of
  40.                         //       publicPage plugins. Menu entries for RAs and Admins are
  41.                         //       handled by the tree() function of the plugin publicPages/090_login
  42.                         if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) {
  43.                                 $plugin->tree($json, null, true, $static_node_id);
  44.                         }
  45.                 }
  46.  
  47.                 $out = '';
  48.                 foreach ($json as $x) {
  49.                         if ($static_node_id == $x['id']) $out .= '<b>';
  50.                         if (isset($x['indent'])) $out .= str_repeat('&nbsp;', $x['indent']*5);
  51.                         $cur_lang = OIDplus::getCurrentLang();
  52.                         if ($cur_lang != OIDplus::getDefaultLang()) {
  53.                                 $out .= '<a href="?lang='.$cur_lang.'&amp;goto='.urlencode($x['id']).'">';
  54.                         } else {
  55.                                 $out .= '<a href="?goto='.urlencode($x['id']).'">';
  56.                         }
  57.                         if (!empty($x['icon'])) $out .= '<img src="'.$x['icon'].'" alt=""> ';
  58.                         $out .= htmlentities($x['id']).' | '.htmlentities($x['text']).'</a><br>';
  59.                         if ($static_node_id == $x['id']) $out .= '</b>';
  60.                 }
  61.                 return $out;
  62.         }
  63.  
  64.         /**
  65.          * @param string $req_id comes from jsTree via AJAX
  66.          * @param string $req_goto comes from the user (GET argument)
  67.          * @return string[]
  68.          */
  69.         public function json_tree(string $req_id, string $req_goto): array {
  70.                 $json = array();
  71.  
  72.                 if ($req_id === '#') {
  73.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  74.                                 // Note: The system (OIDplusMenuUtils) does only show the menu of
  75.                                 //       publicPage plugins. Menu entries for RAs and Admins are
  76.                                 //       handled by the tree() function of the plugin publicPages/090_login
  77.                                 if (is_subclass_of($plugin, OIDplusPagePluginPublic::class)) {
  78.                                         $plugin->tree($json, null, false, $req_goto);
  79.                                 }
  80.                         }
  81.                 } else {
  82.                         $json = $this->tree_populate($req_id);
  83.                 }
  84.  
  85.                 if (is_array($json)) $this->addHrefIfRequired($json);
  86.  
  87.                 return $json;
  88.         }
  89.  
  90.         /**
  91.          * @param array $json
  92.          * @return void
  93.          */
  94.         protected function addHrefIfRequired(array &$json) {
  95.                 foreach ($json as &$item) {
  96.                         if (isset($item['id'])) {
  97.                                 if (!isset($item['conditionalselect']) || ($item['conditionalselect'] != 'false')) {
  98.                                         if (!isset($item['a_attr'])) {
  99.                                                 $item['a_attr'] = array("href" => "?goto=".urlencode($item['id']));
  100.                                         } else if (!isset($item['a_attr']['href'])) {
  101.                                                 $item['a_attr']['href'] = "?goto=".urlencode($item['id']);
  102.                                         }
  103.                                 }
  104.                         }
  105.  
  106.                         if (isset($item['children'])) {
  107.                                 if (is_array($item['children'])) $this->addHrefIfRequired($item['children']);
  108.                         }
  109.                 }
  110.                 unset($item);
  111.         }
  112.  
  113.         /**
  114.          * @param string $parent
  115.          * @param array|true|null $goto_path
  116.          * @return array
  117.          * @throws OIDplusConfigInitializationException
  118.          * @throws OIDplusException
  119.          */
  120.         public function tree_populate(string $parent, $goto_path=null): array {
  121.                 $children = array();
  122.  
  123.                 $parentObj = OIDplusObject::parse($parent);
  124.  
  125.                 if (is_array($goto_path)) array_shift($goto_path);
  126.  
  127.                 $res = OIDplus::db()->query("select * from ###objects where parent = ?", array($parent));
  128.                 $res->naturalSortByField('id');
  129.                 $max_ent = 0;
  130.                 while ($row = $res->fetch_array()) {
  131.                         $max_ent++;
  132.                         if ($max_ent > 1000) break; // TODO: we need a solution for this!!!
  133.                         $obj = OIDplusObject::parse($row['id']);
  134.                         if (!$obj) continue; // e.g. object-type plugin disabled
  135.  
  136.                         if (!$obj->userHasReadRights()) continue;
  137.  
  138.                         $child = array();
  139.                         $child['id'] = $row['id'];
  140.  
  141.                         // Determine display name (relative OID)
  142.                         $child['text'] = $parentObj ? $obj->jsTreeNodeName($parentObj) : '';
  143.                         $child['text'] .= empty($row['title']) ? /*' -- <i>'.htmlentities('Title missing').'</i>'*/ '' : ' -- <b>' . htmlentities($row['title']) . '</b>';
  144.  
  145.                         // Check if node is confidential, or if one of its parent was confidential
  146.                         $is_confidential = $obj->isConfidential();
  147.                         if ($is_confidential) {
  148.                                 $child['text'] = '<font color="gray"><i>'.$child['text'].'</i></font>';
  149.                         }
  150.  
  151.                         // Determine icon
  152.                         $child['icon'] = $obj->getIcon($row);
  153.  
  154.                         // Check if there are more sub OIDs
  155.                         if ($goto_path === true) {
  156.                                 $child['children'] = $this->tree_populate($row['id'], $goto_path);
  157.                                 $child['state'] = array("opened" => true);
  158.                         } else if (!is_null($goto_path) && (count($goto_path) > 0) && ($goto_path[0] === $row['id'])) {
  159.                                 $child['children'] = $this->tree_populate($row['id'], $goto_path);
  160.                                 $child['state'] = array("opened" => true);
  161.                         } else {
  162.                                 $obj_children = $obj->getChildren();
  163.  
  164.                                 // Variant 1: Fast, but does not check for hidden OIDs
  165.                                 //$child_count = count($obj_children);
  166.  
  167.                                 // variant 2
  168.                                 $child_count = 0;
  169.                                 foreach ($obj_children as $obj_test) {
  170.                                         if (!$obj_test->userHasReadRights()) continue;
  171.                                         $child_count++;
  172.                                 }
  173.  
  174.                                 $child['children'] = $child_count > 0;
  175.                         }
  176.  
  177.                         $children[] = $child;
  178.                 }
  179.  
  180.                 return $children;
  181.         }
  182. }
  183.