| Total Complexity | 42 |
| Total Lines | 263 |
| 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() { |
||
| 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 | $host = $this->validator->hostname($_SERVER['SERVER_NAME']); |
||
| 39 | if ($host === FALSE) { |
||
| 40 | throw new \Exception("We don't know our own hostname?!? Giving up."); |
||
| 41 | } |
||
| 42 | $retval .= "<a href='" . \core\CAT::getRootUrlPath() . "'>" . _("Start page") . "</a> |
||
| 43 | </p> |
||
| 44 | </div> <!-- sidebar -->"; |
||
| 45 | return $retval; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * constructs a <div> called 'header' for use on the top of the page |
||
| 50 | * @param string $cap1 caption to display in this div |
||
| 51 | * @param string $language current language (this one gets pre-set in the lang selector drop-down |
||
| 52 | */ |
||
| 53 | private function headerDiv($cap1, $language) { |
||
| 54 | |||
| 55 | $place = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
||
| 56 | |||
| 57 | $retval = "<div class='header'> |
||
| 58 | <div id='header_toprow'> |
||
| 59 | <div id='header_captions' style='display:inline-block; float:left; min-width:400px;'> |
||
| 60 | <h1>$cap1</h1> |
||
| 61 | </div><!--header_captions--> |
||
| 62 | <div id='langselection' style='padding-top:20px; padding-left:10px;'> |
||
| 63 | <form action='$place' method='GET' accept-charset='UTF-8'>" . _("View this page in") . " |
||
| 64 | <select id='lang' name='lang' onchange='this.form.submit()'>"; |
||
| 65 | |||
| 66 | foreach (CONFIG['LANGUAGES'] as $lang => $value) { |
||
| 67 | $retval .= "<option value='$lang' " . (strtoupper($language) == strtoupper($lang) ? "selected" : "" ) . " >" . $value['display'] . "</option> "; |
||
| 68 | } |
||
| 69 | $retval .= "</select>"; |
||
| 70 | |||
| 71 | foreach ($_GET as $var => $value) { |
||
| 72 | if ($var != "lang" && $value != "") { |
||
| 73 | $retval .= "<input type='hidden' name='" . htmlspecialchars($var) . "' value='" . htmlspecialchars($value) . "'>"; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | $retval .= "</form> |
||
| 77 | </div><!--langselection-->"; |
||
| 78 | $host = $this->validator->hostname($_SERVER['SERVER_NAME']); |
||
| 79 | if ($host === FALSE) { |
||
| 80 | throw new \Exception("We don't know our own hostname?!? Giving up."); |
||
| 81 | } |
||
| 82 | $logoUrl = "//" . $host . CONFIG['PATHS']['cat_base_url'] . "/resources/images/consortium_logo.png"; |
||
| 83 | $retval .= "<div class='consortium_logo'> |
||
| 84 | <img id='test_locate' src='$logoUrl' alt='Consortium Logo'> |
||
| 85 | </div> <!-- consortium_logo --> |
||
| 86 | </div><!--header_toprow--> |
||
| 87 | </div> <!-- header -->"; |
||
| 88 | return $retval; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * This starts HTML in a default way. Most pages would call this. |
||
| 93 | * Exception: if you need to add extra code in <head> or modify the <body> tag |
||
| 94 | * (e.g. onload) then you should call defaultPagePrelude, close head, open body, |
||
| 95 | * and then call productheader. |
||
| 96 | * |
||
| 97 | * @param string $pagetitle Title of the page to display |
||
| 98 | * @param string $area the area in which this page is (displays some matching <h1>s) |
||
| 99 | * @param boolean $authRequired |
||
| 100 | */ |
||
| 101 | public function pageheader($pagetitle, $area, $authRequired = TRUE) { |
||
| 102 | $retval = ""; |
||
| 103 | $retval .= $this->defaultPagePrelude($pagetitle, $authRequired); |
||
| 104 | $retval .= "</head></body>"; |
||
| 105 | $retval .= $this->productheader($area); |
||
| 106 | return $retval; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * the entire top of the page (<body> part) |
||
| 111 | * |
||
| 112 | * @param string $area the area we are in |
||
| 113 | */ |
||
| 114 | public function productheader($area) { |
||
| 115 | $langObject = new \core\common\Language(); |
||
| 116 | $language = $langObject->getLang(); |
||
| 117 | // this <div is closing in footer, keep it in PHP for Netbeans syntax |
||
| 118 | // highlighting to work |
||
| 119 | $retval = "<div class='maincontent'>"; |
||
| 120 | // default values which match almost every case; override where needed |
||
| 121 | $cap1 = CONFIG['APPEARANCE']['productname_long']; |
||
| 122 | $advancedControls = TRUE; |
||
| 123 | switch ($area) { |
||
| 124 | case "ADMIN-IDP": |
||
| 125 | $cap2 = sprintf(_("Administrator Interface - Identity Provider"),$this->ui->nomenclature_inst); |
||
| 126 | break; |
||
| 127 | case "ADMIN-IDP-USERS": |
||
| 128 | $cap2 = sprintf(_("Administrator Interface - %s User Management"), \core\ProfileSilverbullet::PRODUCTNAME); |
||
| 129 | break; |
||
| 130 | case "ADMIN": |
||
| 131 | $cap2 = _("Administrator Interface"); |
||
| 132 | break; |
||
| 133 | case "USERMGMT": |
||
| 134 | $cap2 = _("Management of User Details"); |
||
| 135 | break; |
||
| 136 | case "FEDERATION": |
||
| 137 | $cap2 = sprintf(_("Administrator Interface - %s Management"),$this->ui->nomenclature_fed); |
||
| 138 | break; |
||
| 139 | case "USER": |
||
| 140 | $cap1 = sprintf(_("Welcome to %s"), CONFIG['APPEARANCE']['productname']); |
||
| 141 | $cap2 = CONFIG['APPEARANCE']['productname_long']; |
||
| 142 | $advancedControls = FALSE; |
||
| 143 | break; |
||
| 144 | case "SUPERADMIN": |
||
| 145 | $cap2 = _("CIC"); |
||
| 146 | break; |
||
| 147 | case "DIAG": |
||
| 148 | $cap2 = _("Diagnostics"); |
||
| 149 | break; |
||
| 150 | default: |
||
| 151 | $cap2 = "It is an error if you ever see this string."; |
||
| 152 | $advancedControls = FALSE; |
||
| 153 | } |
||
| 154 | |||
| 155 | $retval .= $this->headerDiv($cap1, $language); |
||
| 156 | // content from here on will SCROLL instead of being fixed at the top |
||
| 157 | $retval .= "<div class='pagecontent'>"; // closes in footer again |
||
| 158 | $retval .= "<div class='trick'>"; // closes in footer again |
||
| 159 | $retval .= "<div id='secondrow' style='border-bottom:5px solid ".CONFIG['APPEARANCE']['colour1']."; min-height:100px;'> |
||
| 160 | <div id='secondarycaptions' style='display:inline-block; float:left'> |
||
| 161 | <h2>$cap2</h2> |
||
| 162 | </div><!--secondarycaptions-->"; |
||
| 163 | |||
| 164 | if (isset(CONFIG['APPEARANCE']['MOTD']) && CONFIG['APPEARANCE']['MOTD'] != "") { |
||
| 165 | $retval .= "<div id='header_MOTD' style='display:inline-block; padding-left:20px;vertical-align:top;'> |
||
| 166 | <p class='MOTD'>" . CONFIG['APPEARANCE']['MOTD'] . "</p> |
||
| 167 | </div><!--header_MOTD-->"; |
||
| 168 | } |
||
| 169 | $retval .= $this->sidebar($advancedControls); |
||
| 170 | $retval .= "</div><!--secondrow-->"; |
||
| 171 | return $retval; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * |
||
| 176 | * @param string $pagetitle Title of the page to display |
||
| 177 | * @param boolean $authRequired does the user need to be autenticated to access this page? |
||
| 178 | */ |
||
| 179 | public function defaultPagePrelude($pagetitle, $authRequired = TRUE) { |
||
| 180 | if ($authRequired === TRUE) { |
||
| 181 | $auth = new \web\lib\admin\Authentication(); |
||
| 182 | $auth->authenticate(); |
||
| 183 | } |
||
| 184 | $langObject = new \core\common\Language(); |
||
| 185 | $langObject->setTextDomain("web_admin"); |
||
| 186 | $ourlocale = $langObject->getLang(); |
||
| 187 | header("Content-Type:text/html;charset=utf-8"); |
||
| 188 | $retval = "<!DOCTYPE html> |
||
| 189 | <html xmlns='http://www.w3.org/1999/xhtml' lang='$ourlocale'> |
||
| 190 | <head lang='$ourlocale'> |
||
| 191 | <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"; |
||
| 192 | |||
| 193 | if (strrpos($_SERVER['PHP_SELF'], "admin/")) { |
||
| 194 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "admin/"); |
||
| 195 | } elseif (strrpos($_SERVER['PHP_SELF'], "accountstatus/")) { |
||
| 196 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "accountstatus/"); |
||
| 197 | } elseif (strrpos($_SERVER['PHP_SELF'], "diag/")) { |
||
| 198 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "diag/"); |
||
| 199 | } |
||
| 200 | else { |
||
| 201 | $cutoffPosition = strrpos($_SERVER['PHP_SELF'], "/"); |
||
| 202 | } |
||
| 203 | |||
| 204 | $host = $this->validator->hostname($_SERVER['SERVER_NAME']); |
||
| 205 | if ($host === FALSE) { |
||
| 206 | throw new \Exception("We don't know our own hostname!"); |
||
| 207 | } |
||
| 208 | $cssUrl = "//$host" . substr($_SERVER['PHP_SELF'], 0, $cutoffPosition )."/resources/css/cat.css.php"; |
||
| 209 | |||
| 210 | $retval .= "<link rel='stylesheet' type='text/css' href='$cssUrl' />"; |
||
| 211 | $retval .= "<title>" . htmlspecialchars($pagetitle) . "</title>"; |
||
| 212 | return $retval; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * HTML code for the EU attribution |
||
| 217 | * |
||
| 218 | * @return string HTML code with GEANT Org and EU attribution as required for FP7 / H2020 projects |
||
| 219 | */ |
||
| 220 | public function attributionEurope() { |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * displays the admin area footer |
||
| 250 | */ |
||
| 251 | public function footer() { |
||
| 277 | } |
||
| 278 | |||
| 279 | } |
||
| 280 |