| Total Complexity | 42 |
| Total Lines | 424 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ViewsMatViewsPropertiesTrait 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 ViewsMatViewsPropertiesTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait ViewsMatViewsPropertiesTrait |
||
| 15 | { |
||
| 16 | public $href = ''; |
||
| 17 | public $misc; |
||
| 18 | public $view_name; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Show view definition and virtual columns. |
||
| 22 | * |
||
| 23 | * @param mixed $msg |
||
| 24 | */ |
||
| 25 | public function doDefault($msg = '') |
||
| 26 | { |
||
| 27 | $data = $this->misc->getDatabaseAccessor(); |
||
| 28 | |||
| 29 | $attPre = function (&$rowdata) use ($data) { |
||
| 30 | $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']); |
||
| 31 | }; |
||
| 32 | |||
| 33 | $this->printTrail($this->subject); |
||
| 34 | $this->printTabs($this->subject, 'columns'); |
||
| 35 | $this->printMsg($msg); |
||
| 36 | |||
| 37 | // Get view |
||
| 38 | $vdata = $data->getView($_REQUEST[$this->subject]); |
||
| 39 | // Get columns (using same method for getting a view) |
||
| 40 | $attrs = $data->getTableAttributes($_REQUEST[$this->subject]); |
||
| 41 | |||
| 42 | // Show comment if any |
||
| 43 | if (null !== $vdata->fields['relcomment']) { |
||
| 44 | echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n"; |
||
| 45 | } |
||
| 46 | |||
| 47 | $columns = [ |
||
| 48 | 'column' => [ |
||
| 49 | 'title' => $this->lang['strcolumn'], |
||
| 50 | 'field' => Decorator::field('attname'), |
||
| 51 | 'url' => "colproperties?subject=column&{$this->misc->href}&view=".urlencode($_REQUEST[$this->subject]).'&', |
||
| 52 | 'vars' => ['column' => 'attname'], |
||
| 53 | ], |
||
| 54 | 'type' => [ |
||
| 55 | 'title' => $this->lang['strtype'], |
||
| 56 | 'field' => Decorator::field('+type'), |
||
| 57 | ], |
||
| 58 | 'default' => [ |
||
| 59 | 'title' => $this->lang['strdefault'], |
||
| 60 | 'field' => Decorator::field('adsrc'), |
||
| 61 | ], |
||
| 62 | 'actions' => [ |
||
| 63 | 'title' => $this->lang['stractions'], |
||
| 64 | ], |
||
| 65 | 'comment' => [ |
||
| 66 | 'title' => $this->lang['strcomment'], |
||
| 67 | 'field' => Decorator::field('comment'), |
||
| 68 | ], |
||
| 69 | ]; |
||
| 70 | |||
| 71 | $actions = [ |
||
| 72 | 'alter' => [ |
||
| 73 | 'content' => $this->lang['stralter'], |
||
| 74 | 'attr' => [ |
||
| 75 | 'href' => [ |
||
| 76 | 'url' => $this->view_name, |
||
| 77 | 'urlvars' => [ |
||
| 78 | 'action' => 'properties', |
||
| 79 | $this->subject => $_REQUEST[$this->subject], |
||
| 80 | 'column' => Decorator::field('attname'), |
||
| 81 | ], |
||
| 82 | ], |
||
| 83 | ], |
||
| 84 | ], |
||
| 85 | ]; |
||
| 86 | |||
| 87 | echo $this->printTable($attrs, $columns, $actions, "{$this->view_name}-{$this->view_name}", null, $attPre); |
||
| 88 | |||
| 89 | echo "<br />\n"; |
||
| 90 | |||
| 91 | $navlinks = [ |
||
| 92 | 'browse' => [ |
||
| 93 | 'attr' => [ |
||
| 94 | 'href' => [ |
||
| 95 | 'url' => 'display', |
||
| 96 | 'urlvars' => [ |
||
| 97 | 'server' => $_REQUEST['server'], |
||
| 98 | 'database' => $_REQUEST['database'], |
||
| 99 | 'schema' => $_REQUEST['schema'], |
||
| 100 | $this->subject => $_REQUEST[$this->subject], |
||
| 101 | 'subject' => $this->subject, |
||
| 102 | 'return' => $this->subject, |
||
| 103 | ], |
||
| 104 | ], |
||
| 105 | ], |
||
| 106 | 'content' => $this->lang['strbrowse'], |
||
| 107 | ], |
||
| 108 | 'select' => [ |
||
| 109 | 'attr' => [ |
||
| 110 | 'href' => [ |
||
| 111 | 'url' => str_replace('properties', 's', $this->view_name), |
||
| 112 | 'urlvars' => [ |
||
| 113 | 'action' => 'confselectrows', |
||
| 114 | 'server' => $_REQUEST['server'], |
||
| 115 | 'database' => $_REQUEST['database'], |
||
| 116 | 'schema' => $_REQUEST['schema'], |
||
| 117 | $this->subject => $_REQUEST[$this->subject], |
||
| 118 | ], |
||
| 119 | ], |
||
| 120 | ], |
||
| 121 | 'content' => $this->lang['strselect'], |
||
| 122 | ], |
||
| 123 | 'drop' => [ |
||
| 124 | 'attr' => [ |
||
| 125 | 'href' => [ |
||
| 126 | 'url' => str_replace('properties', 's', $this->view_name), |
||
| 127 | 'urlvars' => [ |
||
| 128 | 'action' => 'confirm_drop', |
||
| 129 | 'server' => $_REQUEST['server'], |
||
| 130 | 'database' => $_REQUEST['database'], |
||
| 131 | 'schema' => $_REQUEST['schema'], |
||
| 132 | $this->subject => $_REQUEST[$this->subject], |
||
| 133 | ], |
||
| 134 | ], |
||
| 135 | ], |
||
| 136 | 'content' => $this->lang['strdrop'], |
||
| 137 | ], |
||
| 138 | 'alter' => [ |
||
| 139 | 'attr' => [ |
||
| 140 | 'href' => [ |
||
| 141 | 'url' => $this->view_name, |
||
| 142 | 'urlvars' => [ |
||
| 143 | 'action' => 'confirm_alter', |
||
| 144 | 'server' => $_REQUEST['server'], |
||
| 145 | 'database' => $_REQUEST['database'], |
||
| 146 | 'schema' => $_REQUEST['schema'], |
||
| 147 | $this->subject => $_REQUEST[$this->subject], |
||
| 148 | ], |
||
| 149 | ], |
||
| 150 | ], |
||
| 151 | 'content' => $this->lang['stralter'], |
||
| 152 | ], |
||
| 153 | ]; |
||
| 154 | $this->prtrace($this->view_name); |
||
| 155 | if ($this->view_name === 'materializedviewproperties') { |
||
| 156 | $navlinks['refresh'] = [ |
||
| 157 | 'attr' => [ |
||
| 158 | 'href' => [ |
||
| 159 | 'url' => $this->view_name, |
||
| 160 | 'urlvars' => [ |
||
| 161 | 'action' => 'refresh', |
||
| 162 | 'server' => $_REQUEST['server'], |
||
| 163 | 'database' => $_REQUEST['database'], |
||
| 164 | 'schema' => $_REQUEST['schema'], |
||
| 165 | $this->subject => $_REQUEST[$this->subject], |
||
| 166 | ], |
||
| 167 | ], |
||
| 168 | ], |
||
| 169 | 'content' => $this->lang['strrefresh'], |
||
| 170 | ]; |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->printNavLinks($navlinks, "{$this->view_name}-{$this->view_name}", get_defined_vars()); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function doTree() |
||
| 177 | { |
||
| 178 | $data = $this->misc->getDatabaseAccessor(); |
||
| 179 | |||
| 180 | $reqvars = $this->misc->getRequestVars('column'); |
||
| 181 | $columns = $data->getTableAttributes($_REQUEST[$this->subject]); |
||
| 182 | |||
| 183 | $attrs = [ |
||
| 184 | 'text' => Decorator::field('attname'), |
||
| 185 | 'action' => Decorator::actionurl( |
||
| 186 | 'colproperties', |
||
| 187 | $reqvars, |
||
| 188 | [ |
||
| 189 | $this->subject => $_REQUEST[$this->subject], |
||
| 190 | 'column' => Decorator::field('attname'), |
||
| 191 | ] |
||
| 192 | ), |
||
| 193 | 'icon' => 'Column', |
||
| 194 | 'iconAction' => Decorator::url( |
||
| 195 | 'display', |
||
| 196 | $reqvars, |
||
| 197 | [ |
||
| 198 | $this->subject => $_REQUEST[$this->subject], |
||
| 199 | 'column' => Decorator::field('attname'), |
||
| 200 | 'query' => Decorator::replace( |
||
| 201 | 'SELECT "%column%", count(*) AS "count" FROM %view% GROUP BY "%column%" ORDER BY "%column%"', |
||
| 202 | [ |
||
| 203 | '%column%' => Decorator::field('attname'), |
||
| 204 | '%view%' => $_REQUEST[$this->subject], |
||
| 205 | ] |
||
| 206 | ), |
||
| 207 | ] |
||
| 208 | ), |
||
| 209 | 'toolTip' => Decorator::field('comment'), |
||
| 210 | ]; |
||
| 211 | |||
| 212 | return $this->printTree($columns, $attrs, 'viewcolumns'); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Allow the dumping of the data "in" a view |
||
| 217 | * NOTE:: PostgreSQL doesn't currently support dumping the data in a view |
||
| 218 | * so I have disabled the data related parts for now. In the future |
||
| 219 | * we should allow it conditionally if it becomes supported. This is |
||
| 220 | * a SMOP since it is based on pg_dump version not backend version. |
||
| 221 | * |
||
| 222 | * @param mixed $msg |
||
| 223 | */ |
||
| 224 | public function doExport($msg = '') |
||
| 225 | { |
||
| 226 | $this->printTrail($this->subject); |
||
| 227 | $this->printTabs($this->subject, 'export'); |
||
| 228 | $this->printMsg($msg); |
||
| 229 | |||
| 230 | $subject = $this->subject; |
||
| 231 | $object = $_REQUEST[$this->subject]; |
||
| 232 | |||
| 233 | echo $this->formHeader(); |
||
| 234 | // Data only |
||
| 235 | // echo $this->dataOnly(false); |
||
| 236 | |||
| 237 | // Structure only |
||
| 238 | echo $this->structureOnly(true); |
||
| 239 | |||
| 240 | // Structure and data |
||
| 241 | // echo $this->structureAndData(); |
||
| 242 | |||
| 243 | echo $this->displayOrDownload(); |
||
| 244 | |||
| 245 | echo $this->formFooter($subject, $object); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Show definition for a view or matview. |
||
| 250 | * |
||
| 251 | * @param mixed $msg |
||
| 252 | */ |
||
| 253 | public function doDefinition($msg = '') |
||
| 254 | { |
||
| 255 | $data = $this->misc->getDatabaseAccessor(); |
||
| 256 | |||
| 257 | // Get view |
||
| 258 | $vdata = $data->getView($_REQUEST[$this->subject]); |
||
| 259 | |||
| 260 | $this->printTrail($this->subject); |
||
| 261 | $this->printTabs($this->subject, 'definition'); |
||
| 262 | $this->printMsg($msg); |
||
| 263 | |||
| 264 | if ($vdata->RecordCount() > 0) { |
||
| 265 | // Show comment if any |
||
| 266 | if (null !== $vdata->fields['relcomment']) { |
||
| 267 | echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n"; |
||
| 268 | } |
||
| 269 | |||
| 270 | echo "<table style=\"width: 100%\">\n"; |
||
| 271 | echo "<tr><th class=\"data\">{$this->lang['strdefinition']}</th></tr>\n"; |
||
| 272 | echo '<tr><td class="data1">', $this->misc->printVal($vdata->fields['vwdefinition']), "</td></tr>\n"; |
||
| 273 | echo "</table>\n"; |
||
| 274 | } else { |
||
| 275 | echo "<p>{$this->lang['strnodata']}</p>\n"; |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->printNavLinks(['alter' => [ |
||
| 279 | 'attr' => [ |
||
| 280 | 'href' => [ |
||
| 281 | 'url' => $this->view_name, |
||
| 282 | 'urlvars' => [ |
||
| 283 | 'action' => 'edit', |
||
| 284 | 'server' => $_REQUEST['server'], |
||
| 285 | 'database' => $_REQUEST['database'], |
||
| 286 | 'schema' => $_REQUEST['schema'], |
||
| 287 | $this->subject => $_REQUEST[$this->subject], |
||
| 288 | ], |
||
| 289 | ], |
||
| 290 | ], |
||
| 291 | 'content' => $this->lang['stralter'], |
||
| 292 | ]], "{$this->view_name}-definition", get_defined_vars()); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Actually creates the new wizard view in the database. |
||
| 297 | * |
||
| 298 | * @param bool $is_materialized true if it's a materialized view, false by default |
||
| 299 | * |
||
| 300 | * @return mixed either a sucess message, a redirection, an error message and who knows |
||
| 301 | */ |
||
| 302 | public function doSaveCreateWiz($is_materialized = false) |
||
| 438 | } |
||
| 439 | } |
||
| 440 | } |
||
| 441 |