| Conditions | 24 |
| Paths | 452 |
| Total Lines | 129 |
| Code Lines | 68 |
| 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 |
||
| 183 | public static function ajax_owner($id = null) |
||
| 184 | { |
||
| 185 | // Handle a request for a single ID |
||
| 186 | if($id) |
||
| 187 | { |
||
| 188 | $label = self::get_owner_label($id); |
||
| 189 | Api\Json\Response::get()->data($label); |
||
| 190 | return $label; |
||
| 191 | } |
||
| 192 | |||
| 193 | $bo = new calendar_bo(); |
||
| 194 | $query = $_REQUEST['query']; |
||
| 195 | |||
| 196 | // Arbitrarily limited to 50 / resource |
||
| 197 | $options = array('start' => 0, 'num_rows' => 50, |
||
| 198 | // Filter accounts out of addressbook |
||
| 199 | 'filter' => array('account_id' => null)) + |
||
| 200 | array_diff_key($_REQUEST, array_flip(array('menuaction','query'))); |
||
| 201 | $results = array(); |
||
| 202 | |||
| 203 | // Contacts matching accounts the user does not have permission for cause |
||
| 204 | // confusion as user selects the contact and there's nothing there, so |
||
| 205 | // we remove those contacts |
||
| 206 | $remove_contacts = array(); |
||
| 207 | |||
| 208 | $resources = array_merge(array('' => $bo->resources['']),$bo->resources); |
||
| 209 | $contacts_obj = new Api\Contacts(); |
||
| 210 | foreach($resources as $type => $data) |
||
| 211 | { |
||
| 212 | $mapped = array(); |
||
| 213 | $_results = array(); |
||
| 214 | |||
| 215 | // Handle Api\Accounts seperately |
||
| 216 | if($type == '') |
||
| 217 | { |
||
| 218 | $account_options = $options + array('account_type' => 'both'); |
||
| 219 | $_results += $remove_contacts = Api\Accounts::link_query($query, $account_options); |
||
| 220 | if (!empty($_REQUEST['checkgrants'])) |
||
| 221 | { |
||
| 222 | $grants = $GLOBALS['egw']->acl->get_grants('calendar'); |
||
| 223 | $_results = array_intersect_key($_results, $grants); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | // App provides a custom search function |
||
| 227 | else if ($data['app'] && $data['search']) |
||
| 228 | { |
||
| 229 | $_results = call_user_func_array($data['search'], array($query, $options)); |
||
| 230 | } |
||
| 231 | // Use standard link registry |
||
| 232 | else if ($data['app'] && Link::get_registry($data['app'], 'query')) |
||
| 233 | { |
||
| 234 | $_results = Link::query($data['app'], $query,$options); |
||
| 235 | } |
||
| 236 | |||
| 237 | // There are always special cases |
||
| 238 | switch ($type) |
||
| 239 | { |
||
| 240 | case 'l': |
||
| 241 | // Include mailing lists |
||
| 242 | $lists = array_filter( |
||
| 243 | $contacts_obj->get_lists(Api\Acl::READ), |
||
| 244 | function($element) use($query) { |
||
| 245 | return (stripos($element, $query) !== false); |
||
| 246 | } |
||
| 247 | ); |
||
| 248 | foreach($lists as $list_id => $list) |
||
| 249 | { |
||
| 250 | $_results[$list_id] = array( |
||
| 251 | 'label' => $list, |
||
| 252 | 'resources' => $bo->enum_mailing_list($type.$list_id) |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | break; |
||
| 256 | } |
||
| 257 | if(!$_results) |
||
| 258 | { |
||
| 259 | continue; |
||
| 260 | } |
||
| 261 | |||
| 262 | foreach(array_unique($_results, SORT_REGULAR) as $id => $title) |
||
| 263 | { |
||
| 264 | if($id && $title) |
||
| 265 | { |
||
| 266 | // Magicsuggest uses id, not value. |
||
| 267 | $value = array( |
||
| 268 | 'id' => $type.$id, |
||
| 269 | 'value'=> $type.$id, |
||
| 270 | 'label' => $title, |
||
| 271 | 'app' => lang($data['app']) |
||
| 272 | ); |
||
| 273 | if(is_array($value['label'])) |
||
| 274 | { |
||
| 275 | $value = array_merge($value, $value['label']); |
||
| 276 | } |
||
| 277 | switch($type) |
||
| 278 | { |
||
| 279 | case 'r': |
||
| 280 | // TODO: fetch resources photo |
||
| 281 | break; |
||
| 282 | case 'c': |
||
| 283 | case '': |
||
| 284 | $contact = $contacts_obj->read($type === '' ? 'account:'.$id : $id, true); |
||
| 285 | if (is_array($contact)) $value['icon'] = Api\Framework::link('/api/avatar.php', array( |
||
| 286 | 'contact_id' => $contact['id'], |
||
| 287 | 'etag' => $contact['etag'] ? $contact['etag'] : 1 |
||
| 288 | )); |
||
| 289 | if($id < 0) |
||
| 290 | { |
||
| 291 | $value['resources'] = $GLOBALS['egw']->accounts->members($id, true); |
||
| 292 | } |
||
| 293 | break; |
||
| 294 | default : |
||
| 295 | // do nothing |
||
| 296 | } |
||
| 297 | $mapped[] = $value; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | if(count($mapped)) |
||
| 301 | { |
||
| 302 | $results = array_merge($results, $mapped); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | // switch regular JSON response handling off |
||
| 307 | Api\Json\Request::isJSONRequest(false); |
||
| 308 | |||
| 309 | header('Content-Type: application/json; charset=utf-8'); |
||
| 310 | echo json_encode($results); |
||
| 311 | exit(); |
||
| 312 | } |
||
| 342 | } |