Add and delete new folders to the list to be synchronised

Write a php code for adding and removing device folder from device.
Write a js code for adding and removing folder into store while user select / deselect folder
This commit is contained in:
csoni
2017-09-13 17:42:57 +05:30
parent 41cc4f675d
commit 6bc2a9788c
12 changed files with 658 additions and 254 deletions

View File

@ -121,6 +121,22 @@ class PluginMDMModule extends Module
return $client->RemoveDevice($deviceid);
}
/**
* Function witch is use get details of given device.
* @param string $deviceid id of device.
* @return array contains device props.
*/
function getDeviceDetails($deviceid)
{
$client = $this->getSoapClient();
$deviceRawData = $client->GetDeviceDetails($deviceid);
$device = array();
$device['props'] = $this->getDeviceProps($deviceRawData->data);
$device["sharedfolders"] = array("item" => $this->getAdditionalFolderList($deviceid));
return $device;
}
/**
* Executes all the actions in the $data variable.
* @return boolean true on success of false on fialure.
@ -173,18 +189,18 @@ class PluginMDMModule extends Module
break;
case 'open':
$data = array();
$rawData = $this->getDevices();
foreach($rawData as $device){
if($device->data['deviceid'] === $actionData['entryid']) {
$data['props'] = $this->getDeviceProps($device->data);
$data["sharedfolders"] = array("item" => $this->getAdditionalFolderList($actionData['entryid']));
}
}
$item = array("item" => $data);
$device = $this->getDeviceDetails($actionData["entryid"]);
$item = array("item" => $device);
$this->addActionData('item', $item);
$GLOBALS['bus']->addData($this->getResponseData());
break;
case 'save':
$this ->saveDevice($actionData);
$device = $this->getDeviceDetails($actionData["entryid"]);
$item = array("item" => $device);
$this->addActionData('update', $item);
$GLOBALS['bus']->addData($this->getResponseData());
break;
default:
$this->handleUnknownActionType($actionType);
}
@ -201,8 +217,21 @@ class PluginMDMModule extends Module
$display_message = dgettext('plugin_mdm', 'Unable to connect to Z-Push Server. Not found.');
}
} else if ($fault->faultcode === "ERROR") {
$display_message = dgettext('plugin_mdm', 'Device ID could not be found');
}
$errors = (explode(": ", $fault->getMessage()));
switch ($errors[0]) {
case "ASDevice->AddAdditionalFolder()":
case "ZPushAdmin::AdditionalFolderAdd()":
$display_message = dgettext('plugin_mdm', "Folder can not be added because there is already an additional folder with the same name");
break;
case "ASDevice->RemoveAdditionalFolder()":
case "ZPushAdmin::AdditionalFolderRemove()":
$display_message = dgettext('plugin_mdm', "Folder can not be removed because there is no folder known with given folder id");
break;
default:
$display_message = dgettext('plugin_mdm', "Device ID could not be found");
}
}
$this->sendFeedback(false, array("type" => ERROR_GENERAL, "info" => array('display_message' => $display_message)));
}
catch (Exception $e) {
@ -268,7 +297,11 @@ class PluginMDMModule extends Module
$folderType = $this->getSyncFolderType($type, $name);
if (isset($contentData[$folderid][self::FOLDERUUID])) {
$synchedFolderTypes[$folderType]++;
if(isset($synchedFolderTypes[$folderType])){
$synchedFolderTypes[$folderType]++;
} else {
$synchedFolderTypes[$folderType] = 1;
}
}
}
}
@ -295,7 +328,6 @@ class PluginMDMModule extends Module
*/
function getSyncFolderType($type, $name)
{
$folderType = '';
switch ($type) {
case SYNC_FOLDER_TYPE_APPOINTMENT:
case SYNC_FOLDER_TYPE_USER_APPOINTMENT:
@ -353,5 +385,193 @@ class PluginMDMModule extends Module
}
return $data;
}
/**
* Function which is use to remove additional folder which was shared with given device.
* @param string $entryId id of device.
* @param string $folderid id of folder which will remove from device.
*/
function additionalFolderRemove($entryId, $folderid)
{
$client = $this->getSoapClient();
$client->AdditionalFolderRemove($entryId, $folderid);
}
/**
* Function which is use to add additional folder which will share with given device.
* @param string $entryId id of device.
* @param array $folder folder which will share with device.
*/
function additionalFolderAdd($entryId, $folder)
{
$client = $this->getSoapClient();
$containerClass = isset($folder[PR_CONTAINER_CLASS]) ? $folder[PR_CONTAINER_CLASS] : "IPF.Note";
$folderId = bin2hex($folder[PR_SOURCE_KEY]);
$userName = $folder["user"];
$folderName = $userName === "SYSTEM" ? $folder[PR_DISPLAY_NAME] : $folder[PR_DISPLAY_NAME]." - ".$userName;
$folderType = $this->getFolderTypeFromContainerClass($containerClass);
$client->AdditionalFolderAdd($entryId, $userName, $folderId, $folderName, $folderType, FLD_FLAGS_REPLYASUSER);
}
/**
* Function which use to save the device.
* It will use to add or remove folders in the device.
* @param array $data array of added and removed folders.
*/
function saveDevice($data)
{
$entryid = $data["entryid"];
if (isset($data['sharedfolders'])) {
if (isset($data['sharedfolders']['remove'])) {
$deletedFolders = $data['sharedfolders']['remove'];
foreach ($deletedFolders as $folder) {
$this->additionalFolderRemove($entryid, $folder["folderid"]);
}
}
if (isset($data['sharedfolders']['add'])) {
$addFolders = $data['sharedfolders']['add'];
$hierarchyFolders = $this->getHierarchyList();
foreach ($addFolders as $folder) {
foreach ($hierarchyFolders as $hierarchyFolder) {
$folderEntryid = bin2hex($hierarchyFolder[PR_ENTRYID]);
if ($folderEntryid === $folder["entryid"]) {
$this->additionalFolderAdd($entryid, $hierarchyFolder);
continue 2;
}
}
}
}
}
}
/**
* Gets the hierarchy list of all required stores.
* Function which is use to get the hierarchy list with PR_SOURCE_KEY.
* @return array the array of all hierarchy folders.
*/
function getHierarchyList()
{
$storeList = $GLOBALS["mapisession"]->getAllMessageStores();
$properties = $GLOBALS["properties"]->getFolderListProperties();
$properties["source_key"] = PR_SOURCE_KEY;
$storeData = array();
foreach ($storeList as $store) {
$msgstore_props = mapi_getprops($store, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_IPM_SUBTREE_ENTRYID, PR_IPM_OUTBOX_ENTRYID, PR_IPM_SENTMAIL_ENTRYID, PR_IPM_WASTEBASKET_ENTRYID, PR_MDB_PROVIDER, PR_IPM_PUBLIC_FOLDERS_ENTRYID, PR_IPM_FAVORITES_ENTRYID, PR_OBJECT_TYPE, PR_STORE_SUPPORT_MASK, PR_MAILBOX_OWNER_ENTRYID, PR_MAILBOX_OWNER_NAME, PR_USER_ENTRYID, PR_USER_NAME, PR_QUOTA_WARNING_THRESHOLD, PR_QUOTA_SEND_THRESHOLD, PR_QUOTA_RECEIVE_THRESHOLD, PR_MESSAGE_SIZE_EXTENDED, PR_MAPPING_SIGNATURE, PR_COMMON_VIEWS_ENTRYID, PR_FINDER_ENTRYID));
$storeType = $msgstore_props[PR_MDB_PROVIDER];
if ($storeType == ZARAFA_STORE_DELEGATE_GUID) {
$storeUserName = $GLOBALS["mapisession"]->getUserNameOfStore($msgstore_props[PR_ENTRYID]);
} else if( $storeType == ZARAFA_STORE_PUBLIC_GUID){
$storeUserName = "SYSTEM";
} else {
$storeUserName = $msgstore_props[PR_USER_NAME];
}
if (isset($msgstore_props[PR_IPM_SUBTREE_ENTRYID])) {
$subtreeFolderEntryID = $msgstore_props[PR_IPM_SUBTREE_ENTRYID];
try {
$subtreeFolder = mapi_msgstore_openentry($store, $subtreeFolderEntryID);
} catch (MAPIException $e) {
// We've handled the event
$e->setHandled();
}
try {
// remove hidden folders, folders with PR_ATTR_HIDDEN property set
// should not be shown to the client
$restriction = Array(RES_OR, Array(
Array(RES_PROPERTY,
Array(
RELOP => RELOP_EQ,
ULPROPTAG => PR_ATTR_HIDDEN,
VALUE => Array(PR_ATTR_HIDDEN => false)
)
),
Array(RES_NOT,
Array(
Array(RES_EXIST,
Array(
ULPROPTAG => PR_ATTR_HIDDEN
)
)
)
)
));
$expand = Array(
Array(
'folder' => $subtreeFolder,
'props' => mapi_getprops($subtreeFolder, Array(PR_ENTRYID, PR_SUBFOLDERS))
)
);
// Start looping through the $expand array, during each loop we grab the first item in
// the array and obtain the hierarchy table for that particular folder. If one of those
// subfolders has subfolders of its own, it will be appended to $expand again to ensure
// it will be expanded later.
while (!empty($expand)) {
$item = array_shift($expand);
$hierarchyTable = mapi_folder_gethierarchytable($item['folder'], MAPI_DEFERRED_ERRORS);
mapi_table_restrict($hierarchyTable, $restriction, TBL_BATCH);
mapi_table_setcolumns($hierarchyTable, $properties);
// Load the hierarchy in small batches
$batchcount = 100;
do {
$rows = mapi_table_queryrows($hierarchyTable, null, 0, $batchcount);
foreach ($rows as $subfolder) {
// If the subfolders has subfolders of its own, append the folder
// to the $expand array, so it can be expanded in the next loop.
if ($subfolder[PR_SUBFOLDERS]) {
$folderObject = mapi_msgstore_openentry($store, $subfolder[PR_ENTRYID]);
array_push($expand, array('folder' => $folderObject, 'props' => $subfolder));
}
$subfolder["user"] = $storeUserName;
// Add the folder to the return list.
array_push($storeData, $subfolder);
}
// When the server returned a different number of rows then was requested,
// we have reached the end of the table and we should exit the loop.
} while (count($rows) === $batchcount);
}
} catch (MAPIException $e) {
// We've handled the event
$e->setHandled();
}
}
}
return $storeData;
}
/**
* Function which is use get folder types from the container class
* @param string $containerClass container class of folder
* @return int folder type
*/
function getFolderTypeFromContainerClass($containerClass)
{
switch ($containerClass) {
case "IPF.Note":
return SYNC_FOLDER_TYPE_USER_MAIL;
case "IPF.Appointment":
return SYNC_FOLDER_TYPE_USER_APPOINTMENT;
case "IPF.Contact":
return SYNC_FOLDER_TYPE_USER_CONTACT;
case "IPF.StickyNote":
return SYNC_FOLDER_TYPE_USER_NOTE;
case "IPF.Task":
return SYNC_FOLDER_TYPE_USER_TASK;
case "IPF.Journal":
return SYNC_FOLDER_TYPE_USER_JOURNAL;
default:
return SYNC_FOLDER_TYPE_UNKNOWN;
}
}
};
?>