| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 172 |
| Code Lines | 87 |
| 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 |
||
| 89 | public function beforeSendToClient($cname, array $expand=null) |
||
| 90 | { |
||
| 91 | // No name, no way to get parameters client-side. |
||
| 92 | if(!$this->id) $this->id = self::GLOBAL_ID; |
||
| 93 | |||
| 94 | $form_name = self::form_name($cname, $this->id, $expand); |
||
| 95 | |||
| 96 | // Store properties at top level, so all customfield widgets can share |
||
| 97 | if($this->attrs['app']) |
||
| 98 | { |
||
| 99 | $app = $this->attrs['app']; |
||
| 100 | } |
||
| 101 | else |
||
| 102 | { |
||
| 103 | $app =& $this->getElementAttribute(self::GLOBAL_VALS, 'app'); |
||
| 104 | if($this->getElementAttribute($form_name, 'app')) |
||
| 105 | { |
||
| 106 | $app =& $this->getElementAttribute($form_name, 'app'); |
||
| 107 | } |
||
| 108 | else |
||
| 109 | { |
||
| 110 | // Checking creates it even if it wasn't there |
||
| 111 | unset(self::$request->modifications[$form_name]['app']); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | if($this->getElementAttribute($form_name, 'customfields')) |
||
| 116 | { |
||
| 117 | $customfields =& $this->getElementAttribute($form_name, 'customfields'); |
||
| 118 | } |
||
| 119 | elseif($app) |
||
| 120 | { |
||
| 121 | // Checking creates it even if it wasn't there |
||
| 122 | unset(self::$request->modifications[$form_name]['customfields']); |
||
| 123 | $customfields =& $this->getElementAttribute(self::GLOBAL_VALS, 'customfields'); |
||
| 124 | } |
||
| 125 | |||
| 126 | if(!$app) |
||
| 127 | { |
||
| 128 | $app =& $this->setElementAttribute(self::GLOBAL_VALS, 'app', $GLOBALS['egw_info']['flags']['currentapp']); |
||
| 129 | if ($this->attrs['sub-app']) $app .= '-'.$this->attrs['sub-app']; |
||
| 130 | $customfields =& $this->setElementAttribute(self::GLOBAL_VALS, 'customfields', Api\Storage\Customfields::get($app)); |
||
| 131 | } |
||
| 132 | |||
| 133 | // if we are in the etemplate editor or the app has no cf's, load the cf's from the app the tpl belongs too |
||
| 134 | if ($app && $app != 'stylite' && $app != $GLOBALS['egw_info']['flags']['currentapp'] && !isset($customfields) && |
||
| 135 | ($GLOBALS['egw_info']['flags']['currentapp'] == 'etemplate' || !$this->attrs['customfields']) || !isset($customfields)) |
||
| 136 | { |
||
| 137 | // app changed |
||
| 138 | $customfields =& Api\Storage\Customfields::get($app); |
||
| 139 | } |
||
| 140 | // Filter fields |
||
| 141 | if($this->attrs['field-names']) |
||
| 142 | { |
||
| 143 | $fields_name = explode(',', $this->attrs['field-names']); |
||
| 144 | foreach($fields_name as &$f) |
||
| 145 | { |
||
| 146 | if ($f[0] == "!") |
||
| 147 | { |
||
| 148 | $f= substr($f,1); |
||
| 149 | $negate_fields[]= $f; |
||
| 150 | } |
||
| 151 | $field_filters []= $f; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | $fields = $customfields; |
||
| 156 | |||
| 157 | $use_private = self::expand_name($this->attrs['use-private'],0,0,'','',self::$cont); |
||
| 158 | $this->attrs['sub-type'] = self::expand_name($this->attrs['sub-type'],0,0,'','',self::$cont); |
||
| 159 | |||
| 160 | foreach((array)$fields as $key => $field) |
||
| 161 | { |
||
| 162 | // remove private or non-private cf's, if only one kind should be displayed |
||
| 163 | if ((string)$use_private !== '' && (boolean)$field['private'] != (boolean)$use_private) |
||
| 164 | { |
||
| 165 | unset($fields[$key]); |
||
| 166 | } |
||
| 167 | |||
| 168 | // Remove filtered fields |
||
| 169 | if($field_filters && in_array($key, $negate_fields) && in_array($key, $field_filters)) |
||
| 170 | { |
||
| 171 | unset($fields[$key]); |
||
| 172 | } |
||
| 173 | |||
| 174 | // Rmove fields for none private cutomfields when name refers to a single custom field |
||
| 175 | $matches = null; |
||
| 176 | if (($pos=strpos($form_name,self::$prefix)) !== false && |
||
| 177 | preg_match($preg = '/'.self::$prefix.'([^\]]+)/',$form_name,$matches) && !isset($fields[$name=$matches[1]])) |
||
| 178 | { |
||
| 179 | unset($fields[$key]); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | // check if name refers to a single custom field --> show only that |
||
| 183 | $matches = null; |
||
| 184 | if (($pos=strpos($form_name,self::$prefix)) !== false && // allow the prefixed name to be an array index too |
||
| 185 | preg_match($preg = '/'.self::$prefix.'([^\]]+)/',$form_name,$matches) && isset($fields[$name=$matches[1]])) |
||
| 186 | { |
||
| 187 | $fields = array($name => $fields[$name]); |
||
| 188 | $value = array(self::$prefix.$name => $value); |
||
| 189 | $form_name = self::$prefix.$name; |
||
| 190 | } |
||
| 191 | |||
| 192 | if(!is_array($fields)) $fields = array(); |
||
| 193 | switch($type = $this->type) |
||
| 194 | { |
||
| 195 | case 'customfields-types': |
||
| 196 | foreach(self::$cf_types as $lname => $label) |
||
| 197 | { |
||
| 198 | $sel_options[$lname] = lang($label); |
||
| 199 | $fields_with_vals[]=$lname; |
||
| 200 | } |
||
| 201 | $link_types = array_intersect_key(Api\Link::app_list('query'), Api\Link::app_list('title')); |
||
| 202 | // Explicitly add in filemanager, which does not support query or title |
||
| 203 | $link_types['filemanager'] = lang('filemanager'); |
||
| 204 | |||
| 205 | ksort($link_types); |
||
| 206 | foreach($link_types as $lname => $label) |
||
| 207 | { |
||
| 208 | $sel_options[$lname] = '- '.$label; |
||
| 209 | } |
||
| 210 | self::$transformation['type'][$type]['sel_options'] = $sel_options; |
||
| 211 | self::$transformation['type'][$type]['no_lang'] = true; |
||
| 212 | return parent::beforeSendToClient($cname, $expand); |
||
| 213 | case 'customfields-list': |
||
| 214 | foreach(array_reverse($fields) as $lname => $field) |
||
| 215 | { |
||
| 216 | if (!empty($this->attrs['sub-type']) && !empty($field['type2']) && |
||
| 217 | strpos(','.$field['type2'].',',','.$field['type2'].',') === false) continue; // not for our content type// |
||
| 218 | if (isset($value[self::$prefix.$lname]) && $value[self::$prefix.$lname] !== '') //break; |
||
| 219 | { |
||
| 220 | $fields_with_vals[]=$lname; |
||
| 221 | } |
||
| 222 | //$stop_at_field = $name; |
||
| 223 | } |
||
| 224 | break; |
||
| 225 | default: |
||
| 226 | foreach(array_reverse($fields) as $lname => $field) |
||
| 227 | { |
||
| 228 | $fields_with_vals[]=$lname; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | // need to encode values/select-options to keep their order |
||
| 232 | foreach($customfields as &$data) |
||
| 233 | { |
||
| 234 | if (!empty($data['values'])) |
||
| 235 | { |
||
| 236 | Select::fix_encoded_options($data['values']); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | if($fields != $customfields) |
||
| 240 | { |
||
| 241 | // This widget has different settings from global |
||
| 242 | $this->setElementAttribute($form_name, 'customfields', $fields); |
||
| 243 | $this->setElementAttribute($form_name, 'fields', array_merge( |
||
| 244 | array_fill_keys(array_keys($customfields), false), |
||
| 245 | array_fill_keys(array_keys($fields), true) |
||
| 246 | )); |
||
| 247 | } |
||
| 248 | parent::beforeSendToClient($cname, $expand); |
||
| 249 | |||
| 250 | // Re-format date custom fields from Y-m-d |
||
| 251 | $field_settings =& self::get_array(self::$request->modifications, "{$this->id}[customfields]",true); |
||
| 252 | if (true) $field_settings = array(); |
||
| 253 | $link_types = Api\Link::app_list(); |
||
| 254 | foreach($fields as $fname => $field) |
||
| 255 | { |
||
| 256 | // Run beforeSendToClient for each field |
||
| 257 | $widget = $this->_widget($fname, $field); |
||
| 258 | if(method_exists($widget, 'beforeSendToClient')) |
||
| 259 | { |
||
| 260 | $widget->beforeSendToClient($this->id == self::GLOBAL_ID ? '' : $this->id, $expand); |
||
| 261 | } |
||
| 437 |