| Total Complexity | 40 |
| Total Lines | 254 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PageDecoration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageDecoration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class PageDecoration { |
||
| 15 | |||
| 16 | private $validator; |
||
| 17 | private $ui; |
||
| 18 | |||
| 19 | public function __construct() { |
||
| 20 | $this->validator = new \web\lib\common\InputValidation(); |
||
| 21 | $this->ui = new UIElements(); |
||
| 22 | } |
||
| 23 | /** |
||
| 24 | * Our (very modest and light) sidebar. authenticated admins get more options, like logout |
||
| 25 | * @param boolean $advancedControls |
||
| 26 | */ |
||
| 27 | private function sidebar($advancedControls) { |
||
| 28 | $retval = "<div class='sidebar'><p>"; |
||
| 29 | |||
| 30 | if ($advancedControls) { |
||
| 31 | $retval .= "<strong>" . _("You are:") . "</strong> " |
||
| 32 | . (isset($_SESSION['name']) ? $_SESSION['name'] : _("Unnamed User")) . " |
||
| 33 | <br/> |
||
| 34 | <br/> |
||
| 35 | <a href='" . \core\CAT::getRootUrlPath() . "/admin/overview_user.php'>" . _("Go to your Profile page") . "</a> |
||
| 36 | <a href='" . \core\CAT::getRootUrlPath() . "/admin/inc/logout.php'>" . _("Logout") . "</a> "; |
||
| 37 | } |
||
| 38 | $retval .= "<a href='" . \core\CAT::getRootUrlPath() . "'>" . _("Start page") . "</a> |
||
| 39 | </p> |
||
| 40 | </div> <!-- sidebar -->"; |
||
| 41 | return $retval; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * constructs a <div> called 'header' for use on the top of the page |
||
| 46 | * @param string $cap1 caption to display in this div |
||
| 47 | * @param string $language current language (this one gets pre-set in the lang selector drop-down |
||
| 48 | */ |
||
| 49 | private function headerDiv($cap1, $language) { |
||
| 50 | |||
| 51 | $place = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
||
| 52 | |||
| 53 | $retval = "<div class='header'> |
||
| 54 | <div id='header_toprow'> |
||
| 55 | <div id='header_captions' style='display:inline-block; float:left; min-width:400px;'> |
||
| 56 | <h1>$cap1</h1> |
||
| 57 | </div><!--header_captions--> |
||
| 58 | <div id='langselection' style='padding-top:20px; padding-left:10px;'> |
||
| 59 | <form action='$place' method='GET' accept-charset='UTF-8'>" . _("View this page in") . " |
||
| 60 | <select id='lang' name='lang' onchange='this.form.submit()'>"; |
||
| 61 | |||
| 62 | foreach (CONFIG['LANGUAGES'] as $lang => $value) { |
||
| 63 | $retval .= "<option value='$lang' " . (strtoupper($language) == strtoupper($lang) ? "selected" : "" ) . " >" . $value['display'] . "</option> "; |
||
| 64 | } |
||
| 65 | $retval .= "</select>"; |
||
| 66 | |||
| 67 | foreach ($_GET as $var => $value) { |
||
| 68 | if ($var != "lang" && $value != "") { |
||
| 69 | $retval .= "<input type='hidden' name='" . htmlspecialchars($var) . "' value='" . htmlspecialchars($value) . "'>"; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | $retval .= "</form> |
||
| 73 | </div><!--langselection-->"; |
||
| 74 | $logoUrl = \core\CAT::getRootUrlPath() . "/resources/images/consortium_logo.png"; |
||
| 75 | $retval .= "<div class='consortium_logo'> |
||
| 76 | <img id='test_locate' src='$logoUrl' alt='Consortium Logo'> |
||
| 77 | </div> <!-- consortium_logo --> |
||
| 78 | </div><!--header_toprow--> |
||
| 79 | </div> <!-- header -->"; |
||
| 80 | return $retval; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * This starts HTML in a default way. Most pages would call this. |
||
| 85 | * Exception: if you need to add extra code in <head> or modify the <body> tag |
||
| 86 | * (e.g. onload) then you should call defaultPagePrelude, close head, open body, |
||
| 87 | * and then call productheader. |
||
| 88 | * |
||
| 89 | * @param string $pagetitle Title of the page to display |
||
| 90 | * @param string $area the area in which this page is (displays some matching <h1>s) |
||
| 91 | * @param boolean $authRequired |
||
| 92 | */ |
||
| 93 | public function pageheader($pagetitle, $area, $authRequired = TRUE) { |
||
| 94 | $retval = ""; |
||
| 95 | $retval .= $this->defaultPagePrelude($pagetitle, $authRequired); |
||
| 96 | $retval .= "</head></body>"; |
||
| 97 | $retval .= $this->productheader($area); |
||
| 98 | return $retval; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * the entire top of the page (<body> part) |
||
| 103 | * |
||
| 104 | * @param string $area the area we are in |
||
| 105 | */ |
||
| 106 | public function productheader($area) { |
||
| 107 | $langObject = new \core\common\Language(); |
||
| 108 | $language = $langObject->getLang(); |
||
| 109 | // this <div is closing in footer, keep it in PHP for Netbeans syntax |
||
| 110 | // highlighting to work |
||
| 111 | $retval = "<div class='maincontent'>"; |
||
| 112 | // default values which match almost every case; override where needed |
||
| 113 | $cap1 = CONFIG['APPEARANCE']['productname_long']; |
||
| 114 | $advancedControls = TRUE; |
||
| 115 | switch ($area) { |
||
| 116 | case "ADMIN-IDP": |
||
| 117 | $cap2 = sprintf(_("Administrator Interface - Identity Provider"),$this->ui->nomenclature_inst); |
||
| 118 | break; |
||
| 119 | case "ADMIN-IDP-USERS": |
||
| 120 | $cap2 = sprintf(_("Administrator Interface - %s User Management"), \core\ProfileSilverbullet::PRODUCTNAME); |
||
| 121 | break; |
||
| 122 | case "ADMIN": |
||
| 123 | $cap2 = _("Administrator Interface"); |
||
| 124 | break; |
||
| 125 | case "USERMGMT": |
||
| 126 | $cap2 = _("Management of User Details"); |
||
| 127 | break; |
||
| 128 | case "FEDERATION": |
||
| 129 | $cap2 = sprintf(_("Administrator Interface - %s Management"),$this->ui->nomenclature_fed); |
||
| 130 | break; |
||
| 131 | case "USER": |
||
| 132 | $cap1 = sprintf(_("Welcome to %s"), CONFIG['APPEARANCE']['productname']); |
||
| 133 | $cap2 = CONFIG['APPEARANCE']['productname_long']; |
||
| 134 | $advancedControls = FALSE; |
||
| 135 | break; |
||
| 136 | case "SUPERADMIN": |
||
| 137 | $cap2 = _("CIC"); |
||
| 138 | break; |
||
| 139 | case "DIAG": |
||
| 140 | $cap2 = _("Diagnostics"); |
||
| 141 | break; |
||
| 142 | default: |
||
| 143 | $cap2 = "It is an error if you ever see this string."; |
||
| 144 | $advancedControls = FALSE; |
||
| 145 | } |
||
| 146 | |||
| 147 | $retval .= $this->headerDiv($cap1, $language); |
||
| 148 | // content from here on will SCROLL instead of being fixed at the top |
||
| 149 | $retval .= "<div class='pagecontent'>"; // closes in footer again |
||
| 150 | $retval .= "<div class='trick'>"; // closes in footer again |
||
| 151 | $retval .= "<div id='secondrow' style='border-bottom:5px solid ".CONFIG['APPEARANCE']['colour1']."; min-height:100px;'> |
||
| 152 | <div id='secondarycaptions' style='display:inline-block; float:left'> |
||
| 153 | <h2>$cap2</h2> |
||
| 154 | </div><!--secondarycaptions-->"; |
||
| 155 | |||
| 156 | if (isset(CONFIG['APPEARANCE']['MOTD']) && CONFIG['APPEARANCE']['MOTD'] != "") { |
||
| 157 | $retval .= "<div id='header_MOTD' style='display:inline-block; padding-left:20px;vertical-align:top;'> |
||
| 158 | <p class='MOTD'>" . CONFIG['APPEARANCE']['MOTD'] . "</p> |
||
| 159 | </div><!--header_MOTD-->"; |
||
| 160 | } |
||
| 161 | $retval .= $this->sidebar($advancedControls); |
||
| 162 | $retval .= "</div><!--secondrow-->"; |
||
| 163 | return $retval; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * |
||
| 168 | * @param string $pagetitle Title of the page to display |
||
| 169 | * @param boolean $authRequired does the user need to be autenticated to access this page? |
||
| 170 | */ |
||
| 171 | public function defaultPagePrelude($pagetitle, $authRequired = TRUE) { |
||
| 172 | if ($authRequired === TRUE) { |
||
| 173 | $auth = new \web\lib\admin\Authentication(); |
||
| 174 | $auth->authenticate(); |
||
| 175 | } |
||
| 176 | $langObject = new \core\common\Language(); |
||
| 177 | $langObject->setTextDomain("web_admin"); |
||
| 178 | $ourlocale = $langObject->getLang(); |
||
| 179 | header("Content-Type:text/html;charset=utf-8"); |
||
| 180 | $retval = "<!DOCTYPE html> |
||
| 181 | <html xmlns='http://www.w3.org/1999/xhtml' lang='$ourlocale'> |
||
| 182 | <head lang='$ourlocale'> |
||
| 183 | <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"; |
||
| 184 | |||
| 185 | if (strrpos($_SERVER['PHP_SELF'], "admin/")) { |
||
| 186 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "admin/"); |
||
|
|
|||
| 187 | } elseif (strrpos($_SERVER['PHP_SELF'], "accountstatus/")) { |
||
| 188 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "accountstatus/"); |
||
| 189 | } elseif (strrpos($_SERVER['PHP_SELF'], "diag/")) { |
||
| 190 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "diag/"); |
||
| 191 | } |
||
| 192 | else { |
||
| 193 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "/"); |
||
| 194 | } |
||
| 195 | |||
| 196 | $host = $this->validator->hostname($_SERVER['SERVER_NAME']); |
||
| 197 | if ($host === FALSE) { |
||
| 198 | throw new \Exception("We don't know our own hostname!"); |
||
| 199 | } |
||
| 200 | $cssUrl = \core\CAT::getRootUrlPath() . "/resources/css/cat.css.php"; |
||
| 201 | $retval .= "<link rel='stylesheet' type='text/css' href='$cssUrl' />"; |
||
| 202 | $retval .= "<title>" . htmlspecialchars($pagetitle) . "</title>"; |
||
| 203 | return $retval; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * HTML code for the EU attribution |
||
| 208 | * |
||
| 209 | * @return string HTML code with GEANT Org and EU attribution as required for FP7 / H2020 projects |
||
| 210 | */ |
||
| 211 | public function attributionEurope() { |
||
| 212 | if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam" && isset(CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo']) && CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo'] == "Operations Team") {// SW: APPROVED |
||
| 213 | // we may need to jump up one dir if we are either in admin/ or accountstatus/ |
||
| 214 | // (accountstatus courtesy of my good mood. It's userspace not admin space so |
||
| 215 | // it shouldn't be using this function any more.) |
||
| 216 | |||
| 217 | if (strrpos($_SERVER['PHP_SELF'], "admin/")) { |
||
| 218 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "admin/"); |
||
| 219 | } elseif (strrpos($_SERVER['PHP_SELF'], "accountstatus/")) { |
||
| 220 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "accountstatus/"); |
||
| 221 | } elseif (strrpos($_SERVER['PHP_SELF'], "diag/")) { |
||
| 222 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "diag/"); |
||
| 223 | } else { |
||
| 224 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "/"); |
||
| 225 | } |
||
| 226 | $host = $this->validator->hostname($_SERVER['SERVER_NAME']); |
||
| 227 | if ($host === FALSE) { |
||
| 228 | throw new \Exception("We don't know our own hostname!"); |
||
| 229 | } |
||
| 230 | $logoBase = "//$host" . substr($_SERVER['PHP_SELF'], 0, $cutoffPosition)."/resources/images"; |
||
| 231 | |||
| 232 | return "<span id='logos' style='position:fixed; left:50%;'><img src='$logoBase/dante.png' alt='DANTE' style='height:23px;width:47px'/> |
||
| 233 | <img src='$logoBase/eu.png' alt='EU' style='height:23px;width:27px;border-width:0px;'/></span> |
||
| 234 | <span id='eu_text' style='text-align:right;'><a href='http://ec.europa.eu/dgs/connect/index_en.htm' style='text-decoration:none; vertical-align:top;'>European Commission Communications Networks, Content and Technology</a></span>"; |
||
| 235 | } |
||
| 236 | return " "; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * displays the admin area footer |
||
| 241 | */ |
||
| 242 | public function footer() { |
||
| 268 | } |
||
| 269 | |||
| 270 | } |
||
| 271 |