| Total Complexity | 59 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ResolveNamesModule 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 ResolveNamesModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class ResolveNamesModule extends Module |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Constructor |
||
| 9 | */ |
||
| 10 | function __construct($id, $data) |
||
| 11 | { |
||
| 12 | parent::__construct($id, $data); |
||
| 13 | } |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Executes all the actions in the $data variable. |
||
| 17 | * @return boolean true on success of false on fialure. |
||
| 18 | */ |
||
| 19 | function execute() |
||
| 20 | { |
||
| 21 | foreach($this->data as $actionType => $action) |
||
| 22 | { |
||
| 23 | if(isset($actionType)) { |
||
| 24 | try { |
||
| 25 | switch($actionType) |
||
| 26 | { |
||
| 27 | case 'checknames': |
||
| 28 | $this->checkNames($action); |
||
| 29 | break; |
||
| 30 | default: |
||
| 31 | $this->handleUnknownActionType($actionType); |
||
| 32 | } |
||
| 33 | } catch (MAPIException $e) { |
||
| 34 | $this->processException($e, $actionType); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Function which checks the names, sent by the client. This function is used |
||
| 42 | * when a user wants to sent an email and want to check the names filled in |
||
| 43 | * by the user in the to, cc and bcc field. This function uses the global |
||
| 44 | * user list to check if the names are correct. |
||
| 45 | * @param array $action the action data, sent by the client |
||
| 46 | * @return boolean true on success or false on failure |
||
| 47 | */ |
||
| 48 | function checkNames($action) |
||
| 49 | { |
||
| 50 | if(isset($action['resolverequests'])) { |
||
| 51 | $data = array(); |
||
| 52 | $excludeLocalContacts = !empty($action['exclude_local_contacts']) ? $action['exclude_local_contacts'] : false; |
||
| 53 | $excludeGABGroups = !empty($action['exclude_gab_groups']) ? $action['exclude_gab_groups'] : false; |
||
| 54 | |||
| 55 | $resolveRequest = $action['resolverequests']; |
||
| 56 | if(!is_array($resolveRequest)) { |
||
| 57 | $resolveRequest = array($resolveRequest); |
||
| 58 | } |
||
| 59 | |||
| 60 | // open addressbook |
||
| 61 | // When local contacts need to be excluded we have pass true as the first argument |
||
| 62 | // so we will not have any Contact Providers set on the Addressbook resource. |
||
| 63 | $ab = $GLOBALS['mapisession']->getAddressbook($excludeLocalContacts); |
||
| 64 | |||
| 65 | $ab_entryid = mapi_ab_getdefaultdir($ab); |
||
| 66 | $ab_dir = mapi_ab_openentry($ab, $ab_entryid); |
||
| 67 | $resolveResponse = Array(); |
||
| 68 | |||
| 69 | // check names |
||
| 70 | foreach($resolveRequest as $query) { |
||
| 71 | if (is_array($query) && isset($query['id'])) { |
||
| 72 | $responseEntry = Array(); |
||
| 73 | $responseEntry['id'] = $query['id']; |
||
| 74 | |||
| 75 | if (!empty($query['display_name']) || !empty($query['email_address'])) { |
||
| 76 | $responseEntry['result'] = $this->searchAddressBook($ab, $ab_dir, $query, $excludeGABGroups); |
||
| 77 | $resolveResponse[] = $responseEntry; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $data['resolveresponse'] = $resolveResponse; |
||
| 83 | |||
| 84 | $this->addActionData('checknames', $data); |
||
| 85 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * This function searches the addressbook specified for users and returns an array with data |
||
| 91 | * Please note that the returning array must be UTF8 |
||
| 92 | * |
||
| 93 | * @param {MAPIAddressbook} $ab The addressbook |
||
|
|
|||
| 94 | * @param {MAPIAbContainer} $ab_dir The addressbook container |
||
| 95 | * @param {String} $query The search query, case is ignored |
||
| 96 | * @param {Boolean} $excludeGABGroups flag to exclude groups from resolving |
||
| 97 | */ |
||
| 98 | function searchAddressBook($ab, $ab_dir, $query, $excludeGABGroups) |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Used to find multiple entries from the contact folders in the Addressbook when resolving |
||
| 207 | * returned an ambiguous result. It will find the Contact folders in the Addressbook and |
||
| 208 | * apply a restriction to extract the entries. |
||
| 209 | * @param {MAPIAddressbook} $ab The addressbook |
||
| 210 | * @param {String} $query The search query, case is ignored |
||
| 211 | * @param {Boolean} $excludeGABGroups flag to exclude groups from resolving |
||
| 212 | */ |
||
| 213 | function getAmbigiousContactResolveResults($ab, $query, $excludeGABGroups) |
||
| 214 | { |
||
| 215 | /* We need to look for the Contact folders at the bottom of the following tree. |
||
| 216 | * |
||
| 217 | * IAddrBook |
||
| 218 | * - Root Container |
||
| 219 | * - HIERARCHY TABLE |
||
| 220 | * - Contacts Folders (Contact Container) |
||
| 221 | * - HIERARCHY TABLE (Contact Container Hierarchy) |
||
| 222 | * - Contact folder 1 |
||
| 223 | * - Contact folder 2 |
||
| 224 | **/ |
||
| 225 | |||
| 226 | $rows = Array(); |
||
| 227 | $contactFolderRestriction = $this->getAmbigiousContactRestriction($query, $excludeGABGroups, PR_EMAIL_ADDRESS); |
||
| 228 | // Open the AB Root Container by not supplying an entryid |
||
| 229 | $abRootContainer = mapi_ab_openentry($ab); |
||
| 230 | |||
| 231 | // Get the 'Contact Folders' |
||
| 232 | $hierarchyTable = mapi_folder_gethierarchytable($abRootContainer, MAPI_DEFERRED_ERRORS); |
||
| 233 | $abHierarchyRows = mapi_table_queryallrows($hierarchyTable, array(PR_AB_PROVIDER_ID, PR_ENTRYID)); |
||
| 234 | |||
| 235 | // Look for the 'Contacts Folders' |
||
| 236 | for($i=0,$len=count($abHierarchyRows);$i<$len;$i++){ |
||
| 237 | // Check if the folder matches the Contact Provider GUID |
||
| 238 | if($abHierarchyRows[$i][PR_AB_PROVIDER_ID] == MUIDZCSAB){ |
||
| 239 | $abContactContainerEntryid = $abHierarchyRows[$i][PR_ENTRYID]; |
||
| 240 | break; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | // Next go into the 'Contacts Folders' and look in the hierarchy table for the Contact folders. |
||
| 245 | if($abContactContainerEntryid){ |
||
| 246 | // Get the rows from hierarchy table of the 'Contacts Folders' |
||
| 247 | $abContactContainer = mapi_ab_openentry($ab, $abContactContainerEntryid); |
||
| 248 | $abContactContainerHierarchyTable = mapi_folder_gethierarchytable($abContactContainer, MAPI_DEFERRED_ERRORS); |
||
| 249 | $abContactContainerHierarchyRows = mapi_table_queryallrows($abContactContainerHierarchyTable, array(PR_DISPLAY_NAME, PR_OBJECT_TYPE, PR_ENTRYID)); |
||
| 250 | |||
| 251 | // Loop through all the contact folders found under the 'Contacts Folders' hierarchy |
||
| 252 | for($j=0,$len=count($abContactContainerHierarchyRows);$j<$len;$j++){ |
||
| 253 | |||
| 254 | // Open, get contents table, restrict, sort and then merge the result in the list of $rows |
||
| 255 | $abContactFolder = mapi_ab_openentry($ab, $abContactContainerHierarchyRows[$j][PR_ENTRYID]); |
||
| 256 | $abContactFolderTable = mapi_folder_getcontentstable($abContactFolder, MAPI_DEFERRED_ERRORS); |
||
| 257 | |||
| 258 | mapi_table_restrict($abContactFolderTable, $contactFolderRestriction, TBL_BATCH); |
||
| 259 | mapi_table_sort($abContactFolderTable, array(PR_DISPLAY_NAME => TABLE_SORT_ASCEND), TBL_BATCH); |
||
| 260 | |||
| 261 | // Go go gadget, merge! |
||
| 262 | $rows = array_merge($rows, mapi_table_queryallrows($abContactFolderTable, array(PR_ACCOUNT, PR_DISPLAY_NAME, PR_ENTRYID, PR_OBJECT_TYPE, PR_SMTP_ADDRESS, PR_DISPLAY_TYPE_EX, PR_EMAIL_ADDRESS, PR_OBJECT_TYPE, PR_DISPLAY_TYPE))); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return $rows; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Setup the restriction used for resolving in the Contact folders or GAB. |
||
| 271 | * @param {String} $query The search query, case is ignored |
||
| 272 | * @param {Boolean} $excludeGABGroups flag to exclude groups from resolving |
||
| 273 | * @param {Number} $content the PROPTAG to search in. |
||
| 274 | */ |
||
| 275 | function getAmbigiousContactRestriction($query, $excludeGABGroups, $content) |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Function does customization of exception based on module data. |
||
| 337 | * like, here it will generate display message based on actionType |
||
| 338 | * for particular exception. |
||
| 339 | * |
||
| 340 | * @param object $e Exception object |
||
| 341 | * @param string $actionType the action type, sent by the client |
||
| 342 | * @param MAPIobject $store Store object of message. |
||
| 343 | * @param string $parententryid parent entryid of the message. |
||
| 344 | * @param string $entryid entryid of the message. |
||
| 345 | * @param array $action the action data, sent by the client |
||
| 346 | */ |
||
| 347 | function handleException(&$e, $actionType = null, $store = null, $parententryid = null, $entryid = null, $action = null) |
||
| 363 | } |
||
| 364 | } |
||
| 365 | ?> |
||
| 366 |