FusionDirectory
class_SimplePluginError.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2019-2020 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
25 {
26  protected $object;
27  protected $tab;
28  protected $attribute;
29 
30  public function __construct ($origin, string $htmlMessage = '', int $code = 0, Throwable $previous = NULL)
31  {
32  $this->setOrigin($origin);
33 
34  parent::__construct($htmlMessage, $code, $previous);
35  }
36 
37  public function setOrigin ($origin)
38  {
39  if ($origin instanceof \FusionDirectory\Core\SimplePlugin\Attribute) {
40  $this->attribute = $origin;
41  $this->tab = $origin->getParent();
42  $this->object = $this->tab->parent;
43  } else {
44  $this->attribute = NULL;
45  if ($origin instanceof SimpleTab) {
46  $this->tab = $origin;
47  $this->object = $this->tab->parent;
48  } elseif ($origin instanceof simpleTabs) {
49  $this->tab = NULL;
50  $this->object = $origin;
51  } elseif ($origin !== NULL) {
52  trigger_error('Invalid origin of class '.get_class($origin));
53  }
54  }
55  if (!$this->object instanceof simpleTabs) {
56  $this->object = NULL;
57  }
58  }
59 
60  public function toArray (): array
61  {
62  $array = parent::toArray();
63 
64  if (isset($this->object->dn)) {
65  $array['dn'] = $this->object->dn;
66  }
67 
68  if (isset($this->tab)) {
69  $array['tab'] = get_class($this->tab);
70  }
71 
72  if (isset($this->attribute)) {
73  $array['attribute'] = $this->attribute->getLdapName();
74  }
75 
76  return $array;
77  }
78 
79  public function __toString ()
80  {
81  $msg = '';
82 
83  if (isset($this->object->dn)) {
84  $msg .= $this->object->dn.' > ';
85  }
86 
87  if (isset($this->tab) && isset($this->tab->parent->by_name[get_class($this->tab)])) {
88  $msg .= $this->tab->parent->by_name[get_class($this->tab)].' > ';
89  }
90 
91  if (isset($this->attribute)) {
92  $label = $this->attribute->getLabel();
93  if (empty($label)) {
94  $msg .= $this->attribute->getLdapName();
95  } else {
96  $msg .= $label;
97  }
98  }
99 
100  return $msg.': '.$this->getMessage();
101  }
102 
103  public function computeMsgDialogParameters (): array
104  {
105  $html = '';
106 
107  $breadcrumbs = [];
108 
109  if (isset($this->object->dn)) {
110  $breadcrumbs[] = htmlescape($this->object->dn);
111  }
112 
113  if (isset($this->tab) && isset($this->tab->parent->by_name[get_class($this->tab)])) {
114  $breadcrumbs[] = htmlescape($this->tab->parent->by_name[get_class($this->tab)]);
115  }
116 
117  if (isset($this->attribute)) {
118  $label = $this->attribute->getLabel();
119  if (empty($label)) {
120  $breadcrumbs[] = '<i>'.htmlescape($this->attribute->getLdapName()).'</i>';
121  } else {
122  $breadcrumbs[] = htmlescape($label);
123  }
124  $example = $this->attribute->getExample();
125  }
126 
127  if (!empty($breadcrumbs)) {
128  $html .= implode(htmlescape(' > '), $breadcrumbs);
129  }
130 
131  $html .= '<br/><br/>';
132 
133  $html .= $this->htmlMessage;
134 
135  /* Stylize example */
136  if (!empty($example)) {
137  $html .= '<br/><br/><i>'.htmlescape(sprintf(_('Example: %s'), $example)).'</i> ';
138  }
139 
140  return [_('Error'), $html, ERROR_DIALOG, FusionDirectoryError::formatTrace($this)];
141  }
142 
143  public static function relocate ($origin, FusionDirectoryError $error)
144  {
145  if ($error instanceof SimplePluginError) {
146  $error->setOrigin($origin);
147  return $error;
148  } else {
149  return new SimplePluginError($origin, $error->getHtmlMessage(), $error->getCode(), $error);
150  }
151  }
152 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
This class contains all function to manage tabs classes.
Error returned by any method of SimplePlugin.
Parent class for all errors in FusionDirectory.