Conditions | 22 |
Paths | 1022 |
Total Lines | 148 |
Code Lines | 80 |
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 |
||
137 | public static function get_rows(array $query, array &$rows=null) |
||
138 | { |
||
139 | $so_sql = new Api\Storage\Base('phpgwapi', Acl::TABLE, null, '', true); |
||
140 | |||
141 | // client queries destinct rows by their row-id |
||
142 | if (isset($query['col_filter']['id'])) |
||
143 | { |
||
144 | $query['col_filter'][] = $GLOBALS['egw']->db->concat('acl_appname',"':'",'acl_account',"':'",'acl_location'). |
||
145 | ' IN ('.implode(',', array_map(array($GLOBALS['egw']->db, 'quote'), (array)$query['col_filter']['id'])).')'; |
||
146 | unset($query['col_filter']['id']); |
||
147 | } |
||
148 | else |
||
149 | { |
||
150 | $memberships = $GLOBALS['egw']->accounts->memberships($query['account_id'], true); |
||
151 | $memberships[] = $query['account_id']; |
||
152 | |||
153 | Api\Cache::setSession(__CLASS__, 'state', array( |
||
154 | 'account_id' => $query['account_id'], |
||
155 | 'filter' => $query['filter'], |
||
156 | 'acl_appname' => $query['filter2'], |
||
157 | )); |
||
158 | |||
159 | if ($GLOBALS['egw_info']['user']['preferences']['admin']['acl_filter'] != $query['filter']) |
||
160 | { |
||
161 | $GLOBALS['egw']->preferences->add('admin', 'acl_filter', $query['filter']); |
||
162 | $GLOBALS['egw']->preferences->save_repository(false,'user',false); |
||
163 | } |
||
164 | switch($query['filter']) |
||
165 | { |
||
166 | case 'run': |
||
167 | $query['col_filter']['acl_location'] = 'run'; |
||
168 | $query['col_filter']['acl_account'] = $memberships; |
||
169 | break; |
||
170 | default: |
||
171 | case 'other': |
||
172 | //$query['col_filter'][] = "acl_location!='run'"; |
||
173 | // remove everything not an account-id in location, like category based acl |
||
174 | if (substr($GLOBALS['egw']->db->Type, 0, 5) == 'mysql') |
||
175 | { |
||
176 | $query['col_filter'][] = "acl_location REGEXP '^-?[0-9]+$'"; |
||
177 | } |
||
178 | else |
||
179 | { |
||
180 | $query['col_filter'][] = "acl_location SIMILAR TO '-?[0-9]+'"; |
||
181 | } |
||
182 | // get apps not using group-acl (eg. Addressbook) or using it only partialy (eg. InfoLog) |
||
183 | $not_enum_group_acls = Api\Hooks::process('not_enum_group_acls', array(), true); |
||
184 | //error_log(__METHOD__."(filter=$query[filter]) not_enum_group_acl=".array2string($not_enum_group_acls)); |
||
185 | if ($not_enum_group_acls) |
||
186 | { |
||
187 | $sql = '(CASE acl_appname'; |
||
188 | foreach($not_enum_group_acls as $app => $groups) |
||
189 | { |
||
190 | if ($groups === true) |
||
191 | { |
||
192 | $check = $query['account_id']; |
||
193 | } |
||
194 | else |
||
195 | { |
||
196 | $check = array_diff($memberships, $groups); |
||
197 | //error_log(__METHOD__."() app=$app, array_diff(memberships=".array2string($memberships).", groups=".array2string($groups).")=".array2string($check)); |
||
198 | if (!$check) continue; // would give sql error otherwise! |
||
199 | } |
||
200 | $sql .= ' WHEN '.$GLOBALS['egw']->db->quote($app).' THEN '.$GLOBALS['egw']->db->expression(Acl::TABLE, array( |
||
201 | 'acl_account' => $check, |
||
202 | )); |
||
203 | } |
||
204 | $sql .= ' ELSE '; |
||
205 | } |
||
206 | $sql .= $GLOBALS['egw']->db->expression(Acl::TABLE, array( |
||
207 | 'acl_account' => $memberships, |
||
208 | )); |
||
209 | if ($not_enum_group_acls) $sql .= ' END)'; |
||
210 | $query['col_filter'][] = $sql; |
||
211 | break; |
||
212 | |||
213 | case 'own': |
||
214 | $query['col_filter']['acl_location'] = $memberships; |
||
215 | break; |
||
216 | } |
||
217 | // do NOT list group-memberships and other non-ACL stuff here |
||
218 | $query['col_filter']['acl_appname'] = $query['filter2']; |
||
219 | if (empty($query['col_filter']['acl_appname']) && $query['filter'] != 'run') |
||
220 | { |
||
221 | //$query['col_filter'][] = "NOT acl_appname IN ('phpgw_group','sqlfs')"; |
||
222 | $query['col_filter']['acl_appname'] = array_keys($query['acl_rights']); |
||
223 | } |
||
224 | } |
||
225 | $readonlys = array(); |
||
226 | $total = $so_sql->get_rows($query, $rows, $readonlys); |
||
227 | |||
228 | foreach($rows as &$row) |
||
229 | { |
||
230 | // generate a row-id |
||
231 | $row['id'] = $row['acl_appname'].':'.$row['acl_account'].':'.$row['acl_location']; |
||
232 | |||
233 | if ($row['acl_location'] == 'run') |
||
234 | { |
||
235 | $row['acl1'] = lang('run'); |
||
236 | } |
||
237 | else |
||
238 | { |
||
239 | if ($app !== $row['acl_appname']) Api\Translation::add_app($row['app_name']); |
||
240 | foreach($query['acl_rights'][$row['acl_appname']] as $val => $label) |
||
241 | { |
||
242 | if ($row['acl_rights'] & $val) |
||
243 | { |
||
244 | $row['acl'.$val] = lang($label); |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | if (!self::check_access($row['acl_account'], $row['acl_location'], false)) // false: do NOT throw an exception! |
||
249 | { |
||
250 | $row['class'] = 'rowNoEdit'; |
||
251 | } |
||
252 | //error_log(__METHOD__."() $n: ".array2string($row)); |
||
253 | } |
||
254 | //error_log(__METHOD__."(".array2string($query).") returning ".$total); |
||
255 | |||
256 | // Get supporting or all apps for filter2 depending on filter |
||
257 | if($query['filter'] == 'run') |
||
258 | { |
||
259 | $rows['sel_options']['filter2'] = array( |
||
260 | '' => lang('All applications'), |
||
261 | )+Etemplate\Widget\Select::app_options('enabled'); |
||
262 | } |
||
263 | else |
||
264 | { |
||
265 | $rows['sel_options']['filter2'] = array( |
||
266 | array('value' => '', 'label' => lang('All applications')) |
||
267 | ); |
||
268 | $apps = Api\Hooks::process(array( |
||
269 | 'location' => 'acl_rights', |
||
270 | 'owner' => $query['account_id'], |
||
271 | ), array(), true); |
||
272 | foreach(array_keys($apps) as $appname) |
||
273 | { |
||
274 | $rows['sel_options']['filter2'][] = array( |
||
275 | 'value' => $appname, |
||
276 | 'label' => lang($appname) |
||
277 | ); |
||
278 | } |
||
279 | usort($rows['sel_options']['filter2'], function($a,$b) { |
||
280 | return strcasecmp($a['label'], $b['label']); |
||
281 | }); |
||
282 | } |
||
283 | |||
284 | return $total; |
||
285 | } |
||
494 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.