| Conditions | 14 |
| Paths | 4096 |
| Total Lines | 113 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 23 | public function toHtml() |
||
| 24 | { |
||
| 25 | $iso = api_get_language_isocode(api_get_interface_language()); |
||
| 26 | $formatResult = $this->getAttribute('formatResult'); |
||
| 27 | $formatSelection = $this->getAttribute('formatSelection'); |
||
| 28 | $formatCondition = ''; |
||
| 29 | |||
| 30 | if (!empty($formatResult)) { |
||
| 31 | $formatCondition .= ', |
||
| 32 | templateResult : '.$formatResult; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (!empty($formatSelection)) { |
||
| 36 | $formatCondition .= ', |
||
| 37 | templateSelection : '.$formatSelection; |
||
| 38 | } |
||
| 39 | |||
| 40 | $width = 'element'; |
||
| 41 | $givenWidth = '100%'; |
||
| 42 | if (!empty($givenWidth)) { |
||
| 43 | $width = $givenWidth; |
||
| 44 | } |
||
| 45 | |||
| 46 | //Get the minimumInputLength for select2 |
||
| 47 | $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ? |
||
| 48 | $this->getAttribute('minimumInputLength') : 3 |
||
| 49 | ; |
||
| 50 | |||
| 51 | $plHolder = $this->getAttribute('placeholder'); |
||
| 52 | if (empty($plHolder)) { |
||
| 53 | $plHolder = preg_replace("/'/", "\\'", get_lang('SelectAnOption')); |
||
| 54 | } |
||
| 55 | |||
| 56 | $id = $this->getAttribute('id'); |
||
| 57 | |||
| 58 | if (empty($id)) { |
||
| 59 | $id = $this->getAttribute('name'); |
||
| 60 | $this->setAttribute('id', $id); |
||
| 61 | } |
||
| 62 | // URL must return ajax json_encode arrady [items => [['id'=>1, 'text'='content']] |
||
| 63 | $url = $this->getAttribute('url'); |
||
| 64 | |||
| 65 | if (!$url) { |
||
| 66 | $url = $this->getAttribute('url_function'); |
||
| 67 | } else { |
||
| 68 | $url = "'$url'"; |
||
| 69 | } |
||
| 70 | |||
| 71 | $tagsAttr = $this->getAttribute('tags'); |
||
| 72 | $multipleAttr = $this->getAttribute('multiple'); |
||
| 73 | |||
| 74 | $tags = !empty($tagsAttr) ? (bool) $tagsAttr : false; |
||
| 75 | $tags = $tags ? 'true' : 'false'; |
||
| 76 | |||
| 77 | $multiple = !empty($multipleAttr) ? (bool) $multipleAttr : false; |
||
| 78 | $multiple = $multiple ? 'true' : 'false'; |
||
| 79 | |||
| 80 | $max = $this->getAttribute('maximumSelectionLength'); |
||
| 81 | $max = !empty($max) ? "maximumSelectionLength: $max, " : ''; |
||
| 82 | |||
| 83 | // wait XX milliseconds before triggering the request |
||
| 84 | $delay = ((int) $this->getAttribute('delay')) ?: 1000; |
||
| 85 | |||
| 86 | $html = <<<JS |
||
| 87 | <script> |
||
| 88 | $(function(){ |
||
| 89 | $('#{$this->getAttribute('id')}').select2({ |
||
| 90 | language: '$iso', |
||
| 91 | placeholder: '$plHolder', |
||
| 92 | allowClear: true, |
||
| 93 | width: '$width', |
||
| 94 | minimumInputLength: '$minimumInputLength', |
||
| 95 | tags: $tags, |
||
| 96 | ajax: { |
||
| 97 | url: $url, |
||
| 98 | delay: $delay, |
||
| 99 | dataType: 'json', |
||
| 100 | data: function(params) { |
||
| 101 | return { |
||
| 102 | q: params.term, // search term |
||
| 103 | page_limit: 10, |
||
| 104 | }; |
||
| 105 | }, |
||
| 106 | processResults: function (data, page) { |
||
| 107 | // Parse the results into the format expected by Select2 |
||
| 108 | if (data.items) { |
||
| 109 | return { |
||
| 110 | results: data.items |
||
| 111 | }; |
||
| 112 | } |
||
| 113 | return { |
||
| 114 | results: '' |
||
| 115 | }; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $formatCondition |
||
| 119 | }); |
||
| 120 | }); |
||
| 121 | </script> |
||
| 122 | JS; |
||
| 123 | |||
| 124 | $this->removeAttribute('formatResult'); |
||
| 125 | $this->removeAttribute('formatSelection'); |
||
| 126 | $this->removeAttribute('minimumInputLength'); |
||
| 127 | $this->removeAttribute('maximumSelectionLength'); |
||
| 128 | $this->removeAttribute('tags'); |
||
| 129 | $this->removeAttribute('placeholder'); |
||
| 130 | $this->removeAttribute('class'); |
||
| 131 | $this->removeAttribute('url'); |
||
| 132 | $this->removeAttribute('url_function'); |
||
| 133 | $this->setAttribute('style', 'width: 100%;'); |
||
| 134 | |||
| 135 | return parent::toHtml().$html; |
||
| 136 | } |
||
| 209 |