| Total Complexity | 54 |
| Total Lines | 317 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Complex classes like calendar_owner_etemplate_widget 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 calendar_owner_etemplate_widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class calendar_owner_etemplate_widget extends Etemplate\Widget\Taglist |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Make sure all the needed select options are there |
||
| 29 | * |
||
| 30 | * @param string $cname |
||
| 31 | * @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont' |
||
| 32 | */ |
||
| 33 | public function beforeSendToClient($cname, array $expand=null) |
||
| 34 | { |
||
| 35 | |||
| 36 | Framework::includeJS('.','et2_widget_owner','calendar'); |
||
| 37 | Framework::includeCSS('calendar','calendar'); |
||
| 38 | |||
| 39 | $bo = new calendar_bo(); |
||
| 40 | |||
| 41 | $form_name = self::form_name($cname, $this->id, $expand); |
||
| 42 | |||
| 43 | $value =& self::get_array(self::$request->content, $form_name); |
||
| 44 | |||
| 45 | if (!is_array(self::$request->sel_options[$form_name])) |
||
| 46 | { |
||
| 47 | self::$request->sel_options[$form_name] = array(); |
||
| 48 | } |
||
| 49 | $sel_options =& self::$request->sel_options[$form_name]; |
||
| 50 | |||
| 51 | // Get user accounts, formatted nicely for grouping and matching |
||
| 52 | // the ajax call calendar_uiforms->ajax_owner() - users first |
||
| 53 | $accounts = array(); |
||
| 54 | $list = array('accounts', 'owngroups'); |
||
| 55 | foreach($list as $type) |
||
| 56 | { |
||
| 57 | $account_options = array('account_type' => $type); |
||
| 58 | $accounts_type = Api\Accounts::link_query('',$account_options); |
||
| 59 | if($type == 'accounts') |
||
| 60 | { |
||
| 61 | $accounts_type = array_intersect_key($accounts_type, $GLOBALS['egw']->acl->get_grants('calendar')); |
||
| 62 | } |
||
| 63 | $accounts += $accounts_type; |
||
| 64 | } |
||
| 65 | $sel_options += array_map( |
||
| 66 | function($account_id, $account_name) |
||
| 67 | { |
||
| 68 | $data = array( |
||
| 69 | 'value' => ''.$account_id, |
||
| 70 | 'label' => $account_name, |
||
| 71 | 'app' => lang('api-accounts'), |
||
| 72 | ); |
||
| 73 | if ($account_id > 0) |
||
| 74 | { |
||
| 75 | $contact_obj = new Api\Contacts(); |
||
| 76 | if (($contact = $contact_obj->read('account:'.$account_id, true))) |
||
| 77 | { |
||
| 78 | $data['icon'] = Api\Framework::link('/api/avatar.php', array( |
||
| 79 | 'contact_id' => $contact['id'], |
||
| 80 | 'etag' => $contact['etag'] ? $contact['etag'] : 1 |
||
| 81 | )); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | else |
||
| 85 | { |
||
| 86 | // Add in group memberships as strings |
||
| 87 | $data['resources'] = array_map(function($a) { return ''.$a;},$GLOBALS['egw']->accounts->members($account_id, true)); |
||
| 88 | } |
||
| 89 | return $data; |
||
| 90 | }, |
||
| 91 | array_keys($accounts), $accounts |
||
| 92 | ); |
||
| 93 | |||
| 94 | if(!is_array($value)) |
||
| 95 | { |
||
| 96 | // set value with an empty string only if sel options are not |
||
| 97 | // loaded, for example: setting calendar owner via URL when |
||
| 98 | // calendar app is not yet loaded. |
||
| 99 | $value = !empty($sel_options) ? array(): explode(',', $value); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Add external owners that a select account widget will not find |
||
| 103 | foreach($value as &$owner) |
||
| 104 | { |
||
| 105 | $label = self::get_owner_label($owner); |
||
| 106 | $info = array(); |
||
| 107 | if(!is_numeric($owner)) |
||
| 108 | { |
||
| 109 | $resource = $bo->resources[substr($owner, 0,1)]; |
||
| 110 | if($resource['info'] && !($info = $bo->resource_info($owner))) |
||
| 111 | { |
||
| 112 | continue; // ignore that resource, we would get a PHP Fatal: Unsupported operand types |
||
| 113 | } |
||
| 114 | } |
||
| 115 | else if (!in_array($owner, array_keys($accounts))) |
||
| 116 | { |
||
| 117 | $resource = array('app'=> 'api-accounts'); |
||
| 118 | } |
||
| 119 | if ($resource && is_numeric ($owner) && (int)$owner < 0) |
||
|
|
|||
| 120 | { |
||
| 121 | // Add in group memberships as strings |
||
| 122 | $info['resources'] = array_map(function($a) { return ''.$a;},$GLOBALS['egw']->accounts->members($owner, true)); |
||
| 123 | } |
||
| 124 | |||
| 125 | $option = array('value' => $owner, 'label' => $label, 'app' => lang($resource['app'])) + $info; |
||
| 126 | $sel_option_index = $this->get_index($sel_options, 'value', $owner); |
||
| 127 | if($sel_option_index === false) |
||
| 128 | { |
||
| 129 | $sel_options[] = $option; |
||
| 130 | } |
||
| 131 | else |
||
| 132 | { |
||
| 133 | $sel_options[$sel_option_index] = array_merge($sel_options[$sel_option_index], $option); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the index of an array (sel_options) containing the given value |
||
| 140 | * |
||
| 141 | * @param Array $array |
||
| 142 | * @param string $key key we're checking to match value |
||
| 143 | * @param string $value Value we're looking for |
||
| 144 | * @return boolean|int Returns index |
||
| 145 | */ |
||
| 146 | private function get_index(&$array, $key, $value) |
||
| 147 | { |
||
| 148 | foreach($array as $_key => $_value) |
||
| 149 | { |
||
| 150 | if($_value[$key] === $value) return $_key; |
||
| 151 | } |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | /** |
||
| 155 | * Validate input |
||
| 156 | * |
||
| 157 | * @param string $cname current namespace |
||
| 158 | * @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont' |
||
| 159 | * @param array $content |
||
| 160 | * @param array &$validated=array() validated content |
||
| 161 | */ |
||
| 162 | public function validate($cname, array $expand, array $content, &$validated=array()) |
||
| 176 | } |
||
| 177 | } |
||
| 178 | /** |
||
| 179 | * Handle ajax searches for owner across all supported resources |
||
| 180 | * |
||
| 181 | * @return Array List of matching results |
||
| 182 | */ |
||
| 183 | public static function ajax_owner($id = null) |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get just the label for a single owner |
||
| 316 | * @param string $id |
||
| 317 | */ |
||
| 318 | public static function get_owner_label($id) |
||
| 341 | } |
||
| 342 | } |