| Conditions | 27 |
| Paths | 378 |
| Total Lines | 226 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 153 | public function render($expanded = false, $buttons = true) |
||
| 154 | { |
||
| 155 | self::$logger->debug('>>render(expanded=['.$expanded.'], buttons=['.$buttons.'])'); |
||
| 156 | |||
| 157 | $config = ConfigProvider::getInstance(); |
||
| 158 | $sessionProvider = $config->get('session.provider.name'); |
||
| 159 | $session = ServiceFactory::getInstance($sessionProvider, 'Alpha\Util\Http\Session\SessionProviderInterface'); |
||
| 160 | |||
| 161 | $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt($this->name)) : $this->name); |
||
| 162 | |||
| 163 | $html = ''; |
||
| 164 | |||
| 165 | // render text-box for many-to-one relations |
||
| 166 | if ($this->relationObject->getRelationType() == 'MANY-TO-ONE') { |
||
| 167 | // value to appear in the text-box |
||
| 168 | $inputBoxValue = $this->relationObject->getRelatedClassDisplayFieldValue(); |
||
| 169 | |||
| 170 | $html .= '<div class="form-group">'; |
||
| 171 | $html .= '<label for="'.$this->name.'_display">'.$this->label.'</label>'; |
||
| 172 | |||
| 173 | $html .= '<input type="text" size="70" class="form-control" name="'.$this->name.'_display" id="'.$this->name.'_display" value="'.$inputBoxValue.'" disabled/>'; |
||
| 174 | |||
| 175 | $js = " if(window.jQuery) { |
||
| 176 | window.jQuery.dialog = new BootstrapDialog({ |
||
| 177 | title: 'Please select', |
||
| 178 | message: 'Loading...', |
||
| 179 | onshow: function(dialogRef){ |
||
| 180 | dialogRef.getModalBody().load('".$config->get('app.url')."/recordselector/12m/'+document.getElementById('".$fieldname."').value+'/".$this->name.'/'.urlencode($this->relationObject->getRelatedClass()).'/'.$this->relationObject->getRelatedClassField().'/'.$this->relationObject->getRelatedClassDisplayField()."'); |
||
| 181 | }, |
||
| 182 | buttons: [ |
||
| 183 | { |
||
| 184 | icon: 'glyphicon glyphicon-remove', |
||
| 185 | label: 'Cancel', |
||
| 186 | cssClass: 'btn btn-default btn-xs', |
||
| 187 | action: function(dialogItself){ |
||
| 188 | dialogItself.close(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | ] |
||
| 192 | }); |
||
| 193 | window.jQuery.dialog.open(); |
||
| 194 | }"; |
||
| 195 | |||
| 196 | $tmp = new Button($js, 'Select', 'relBut', '', 'glyphicon-check'); |
||
| 197 | $html .= '<div class="centered lower">'.$tmp->render().'</div>'; |
||
| 198 | |||
| 199 | // hidden field to store the actual value of the relation |
||
| 200 | $html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value="'.$this->relationObject->getValue().'"/>'; |
||
| 201 | |||
| 202 | if ($this->relationObject->getRule() != '') { |
||
| 203 | $html .= '<input type="hidden" id="'.$fieldname.'_msg" value="'.$this->relationObject->getHelper().'"/>'; |
||
| 204 | $html .= '<input type="hidden" id="'.$fieldname.'_rule" value="'.$this->relationObject->getRule().'"/>'; |
||
| 205 | } |
||
| 206 | |||
| 207 | $html .= '</div>'; |
||
| 208 | } |
||
| 209 | |||
| 210 | // render read-only list for one-to-many relations |
||
| 211 | if ($this->relationObject->getRelationType() == 'ONE-TO-MANY') { |
||
| 212 | $objects = $this->relationObject->getRelated(); |
||
| 213 | |||
| 214 | if (count($objects) > 0) { |
||
| 215 | // render tags differently |
||
| 216 | if ($this->name == 'tags' && $this->relationObject->getRelatedClass() == 'TagObject') { |
||
| 217 | $html .= '<p><strong>'.$this->label.':</strong>'; |
||
| 218 | |||
| 219 | foreach ($objects as $tag) { |
||
| 220 | $html .= ' <a href="'.$config->get('app.url').'/search/'.$tag->get('content').'">'.$tag->get('content').'</a>'; |
||
| 221 | } |
||
| 222 | |||
| 223 | $html .= '</p>'; |
||
| 224 | } else { |
||
| 225 | $html .= '<div><strong>'.$this->label.':</strong>'; |
||
| 226 | if ($buttons) { |
||
| 227 | $html .= '<div class="spread">'; |
||
| 228 | $tmp = new Button("document.getElementById('relation_field_".$this->name."').style.display = '';", 'Show', $this->name.'DisBut', '', 'glyphicon-list'); |
||
| 229 | $html .= $tmp->render(); |
||
| 230 | $tmp = new Button("document.getElementById('relation_field_".$this->name."').style.display = 'none';", 'Hide', $this->name.'HidBut', '', 'glyphicon-minus'); |
||
| 231 | $html .= $tmp->render(); |
||
| 232 | $html .= '</div>'; |
||
| 233 | } |
||
| 234 | $html .= '</div>'; |
||
| 235 | |||
| 236 | $html .= '<div id="relation_field_'.$this->name.'" style="display:'.($expanded ? '' : 'none').';">'; |
||
| 237 | |||
| 238 | $customControllerName = Controller::getCustomControllerName(get_class($objects[0])); |
||
| 239 | |||
| 240 | $request = new Request(array('method' => 'GET')); |
||
| 241 | $URI = $request->getURI(); |
||
| 242 | |||
| 243 | foreach ($objects as $obj) { |
||
| 244 | |||
| 245 | // check to see if we are in the admin back-end |
||
| 246 | if (mb_strpos($URI, '/tk/') !== false) { |
||
| 247 | $viewURL = FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.get_class($obj).'&ActiveRecordID='.$obj->getID()); |
||
| 248 | $editURL = FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.get_class($obj).'&ActiveRecordID='.$obj->getID().'&view=edit'); |
||
| 249 | } else { |
||
| 250 | if (isset($customControllerName)) { |
||
| 251 | if ($config->get('app.use.pretty.urls')) { |
||
| 252 | $viewURL = $config->get('app.url').'/'.urlencode($customControllerName).'/ActiveRecordID/'.$obj->getID(); |
||
| 253 | $editURL = $config->get('app.url').'/'.urlencode($customControllerName).'/ActiveRecordID/'.$obj->getID().'/edit'; |
||
| 254 | } else { |
||
| 255 | $viewURL = $config->get('app.url').'/?act='.urlencode($customControllerName).'&ActiveRecordID='.$obj->getID(); |
||
| 256 | $editURL = $config->get('app.url').'/?act='.urlencode($customControllerName).'&ActiveRecordID='.$obj->getID().'&view=edit'; |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | $viewURL = $config->get('app.url').'/record/'.urlencode(get_class($obj)).'/'.$obj->getID(); |
||
| 260 | $editURL = $config->get('app.url').'/record/'.urlencode(get_class($obj)).'/'.$obj->getID().'/edit'; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /* |
||
| 265 | * If any display headers were set with setRelatedClassHeaderFields, use them otherwise |
||
| 266 | * use the ID of the related class as the only header. |
||
| 267 | */ |
||
| 268 | $headerFields = $this->relationObject->getRelatedClassHeaderFields(); |
||
| 269 | if (count($headerFields) > 0) { |
||
| 270 | foreach ($headerFields as $field) { |
||
| 271 | $label = $obj->getDataLabel($field); |
||
| 272 | $value = $obj->get($field); |
||
| 273 | |||
| 274 | if ($field == 'created_by' || $field == 'updated_by') { |
||
| 275 | $person = new PersonObject(); |
||
| 276 | $person->load($value); |
||
| 277 | $value = $person->getUsername(); |
||
| 278 | } |
||
| 279 | |||
| 280 | $html .= '<em>'.$label.': </em>'.$value.' '; |
||
| 281 | } |
||
| 282 | // if the related Record has been updated, render the update time |
||
| 283 | if ($obj->getCreateTS() != $obj->getUpdateTS()) { |
||
| 284 | try { |
||
| 285 | $html .= '<em>'.$obj->getDataLabel('updated_ts').': </em>'.$obj->get('updated_ts'); |
||
| 286 | } catch (IllegalArguementException $e) { |
||
| 287 | $html .= '<em>Updated: </em>'.$obj->get('updated_ts'); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } else { |
||
| 291 | $html .= '<em>'.$obj->getDataLabel('ID').': </em>'.$obj->get('ID'); |
||
| 292 | } |
||
| 293 | // ensures that line returns are rendered |
||
| 294 | $value = str_replace("\n", '<br>', $obj->get($this->relationObject->getRelatedClassDisplayField())); |
||
| 295 | $html .= '<p>'.$value.'</p>'; |
||
| 296 | |||
| 297 | $html .= '<div class="centered">'; |
||
| 298 | $html .= '<a href="'.$viewURL.'">View</a>'; |
||
| 299 | // if the current user owns it, they get the edit link |
||
| 300 | if ($session->get('currentUser') != null && $session->get('currentUser')->getID() == $obj->getCreatorId()) { |
||
| 301 | $html .= ' <a href="'.$editURL.'">Edit</a>'; |
||
| 302 | } |
||
| 303 | $html .= '</div>'; |
||
| 304 | } |
||
| 305 | $html .= '</div>'; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | // render text-box for many-to-many relations |
||
| 311 | if ($this->relationObject->getRelationType() == 'MANY-TO-MANY') { |
||
| 312 | // value to appear in the text-box |
||
| 313 | $inputBoxValue = $this->relationObject->getRelatedClassDisplayFieldValue($this->accessingClassName); |
||
| 314 | // replace commas with line returns |
||
| 315 | $inputBoxValue = str_replace(',', "\n", $inputBoxValue); |
||
| 316 | |||
| 317 | $html .= '<div class="form-group">'; |
||
| 318 | $html .= '<label for="'.$this->name.'_display">'.$this->label.'</label>'; |
||
| 319 | |||
| 320 | $html .= '<textarea id="'.$this->name.'_display" class="form-control" rows="5" readonly>'; |
||
| 321 | $html .= $inputBoxValue; |
||
| 322 | $html .= '</textarea>'; |
||
| 323 | |||
| 324 | $fieldname1 = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt($this->name)) : $this->name); |
||
| 325 | $fieldname2 = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt($this->name.'_ID')) : $this->name.'_ID'); |
||
| 326 | |||
| 327 | $js = "if(window.jQuery) { |
||
| 328 | BootstrapDialog.show({ |
||
| 329 | title: 'Please select', |
||
| 330 | message: 'Loading...', |
||
| 331 | onshow: function(dialogRef){ |
||
| 332 | dialogRef.getModalBody().load('".$config->get('app.url')."/recordselector/m2m/'+document.getElementById('".$fieldname2."').value+'/".$this->name.'/'.urlencode($this->relationObject->getRelatedClass('left')).'/'.$this->relationObject->getRelatedClassDisplayField('left').'/'.urlencode($this->relationObject->getRelatedClass('right')).'/'.$this->relationObject->getRelatedClassDisplayField('right').'/'.urlencode($this->accessingClassName)."/'+document.getElementById('".$fieldname1."').value); |
||
| 333 | }, |
||
| 334 | buttons: [ |
||
| 335 | { |
||
| 336 | icon: 'glyphicon glyphicon-remove', |
||
| 337 | label: 'Cancel', |
||
| 338 | cssClass: 'btn btn-default btn-xs', |
||
| 339 | action: function(dialogItself){ |
||
| 340 | dialogItself.close(); |
||
| 341 | } |
||
| 342 | }, |
||
| 343 | { |
||
| 344 | icon: 'glyphicon glyphicon-ok', |
||
| 345 | label: 'Okay', |
||
| 346 | cssClass: 'btn btn-default btn-xs', |
||
| 347 | action: function(dialogItself) { |
||
| 348 | setParentFieldValues(); |
||
| 349 | $('[id=\'".$this->name."_display\']').blur(); |
||
| 350 | dialogItself.close(); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | ] |
||
| 354 | }); |
||
| 355 | }"; |
||
| 356 | |||
| 357 | $tmp = new Button($js, 'Select', 'relBut', '', 'glyphicon-check'); |
||
| 358 | $html .= '<div class="centered lower">'.$tmp->render().'</div>'; |
||
| 359 | |||
| 360 | $html .= '</div>'; |
||
| 361 | |||
| 362 | // hidden field to store the ID of the current record |
||
| 363 | $html .= '<input type="hidden" name="'.$fieldname2.'" id="'.$fieldname2.'" value="'.$this->relationObject->getValue().'"/>'; |
||
| 364 | |||
| 365 | // hidden field to store the IDs of the related records on the other side of the rel (this is what we check for when saving) |
||
| 366 | if ($this->relationObject->getSide($this->accessingClassName) == 'left') { |
||
| 367 | $lookupIDs = $this->relationObject->getLookup()->loadAllFieldValuesByAttribute('leftID', $this->relationObject->getValue(), 'rightID', 'DESC'); |
||
| 368 | } else { |
||
| 369 | $lookupIDs = $this->relationObject->getLookup()->loadAllFieldValuesByAttribute('rightID', $this->relationObject->getValue(), 'leftID', 'DESC'); |
||
| 370 | } |
||
| 371 | |||
| 372 | $html .= '<input type="hidden" name="'.$fieldname1.'" id="'.$fieldname1.'" value="'.implode(',', $lookupIDs).'"/>'; |
||
| 373 | } |
||
| 374 | |||
| 375 | self::$logger->debug('<<__render [html]'); |
||
| 376 | |||
| 377 | return $html; |
||
| 378 | } |
||
| 379 | |||
| 533 |