PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/bitrix/mail.client.config.dirs/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 191 lines | 133 code | 45 blank | 13 comment | 18 complexity | 9de99ac96d98f1f01da1a219e3ebf2f7 MD5 | raw file
  1. <?php
  2. if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
  3. {
  4. die();
  5. }
  6. use Bitrix\Mail;
  7. use Bitrix\Mail\Helper\MailboxDirectoryHelper;
  8. use Bitrix\Mail\MailboxDirectory;
  9. use Bitrix\Main;
  10. use Bitrix\Main\Context;
  11. use Bitrix\Main\Engine\Contract\Controllerable;
  12. use Bitrix\Main\Error;
  13. use Bitrix\Main\Errorable;
  14. use Bitrix\Main\ErrorCollection;
  15. use Bitrix\Main\Loader;
  16. use Bitrix\Main\Localization\Loc;
  17. Loc::loadMessages(__DIR__ . '/../mail.client/class.php');
  18. Loader::includeModule('mail');
  19. class CMailClientConfigDirsComponent extends CBitrixComponent implements Controllerable, Errorable
  20. {
  21. /** @var ErrorCollection */
  22. protected $errorCollection;
  23. /**
  24. * @inheritDoc
  25. */
  26. public function configureActions()
  27. {
  28. $this->errorCollection = new Main\ErrorCollection();
  29. return [];
  30. }
  31. public function executeComponent()
  32. {
  33. global $USER, $APPLICATION;
  34. $APPLICATION->setTitle(Loc::getMessage('MAIL_CLIENT_CONFIG_DIRS_TITLE'));
  35. if (!is_object($USER) || !$USER->isAuthorized())
  36. {
  37. $APPLICATION->authForm('');
  38. return;
  39. }
  40. $request = Context::getCurrent()->getRequest();
  41. $mailboxId = (int)$request->getQuery('mailboxId');
  42. if (!$mailboxId)
  43. {
  44. showError(Loc::getMessage('MAIL_CLIENT_ELEMENT_NOT_FOUND'));
  45. return;
  46. }
  47. $mailboxHelper = Mail\Helper\Mailbox::createInstance($mailboxId, false);
  48. if (!$mailboxHelper)
  49. {
  50. LocalRedirect('/mail');
  51. }
  52. $mailboxHelper->cacheDirs();
  53. $mailboxDirsHelper = $mailboxHelper->getDirsHelper();
  54. $this->arResult['DIRS'] = $mailboxDirsHelper->buildTreeDirs();
  55. $this->arResult['MAX_LEVEL'] = 1;
  56. $this->arResult['OUTCOME'] = $mailboxDirsHelper->getOutcome();
  57. $this->arResult['TRASH'] = $mailboxDirsHelper->getTrash();
  58. $this->arResult['SPAM'] = $mailboxDirsHelper->getSpam();
  59. $this->arResult['MAILBOX_ID'] = $mailboxId;
  60. $this->arResult['MAX_LEVEL_DIRS'] = MailboxDirectoryHelper::getMaxLevelDirs();
  61. ob_start();
  62. $this->includeComponentTemplate('dirs');
  63. $this->arResult['DIRS_TREE'] = ob_get_clean();
  64. $this->includeComponentTemplate();
  65. }
  66. public function saveAction()
  67. {
  68. $request = Context::getCurrent()->getRequest();
  69. $mailboxId = (int)$request->getPost("mailboxId");
  70. $dirs = (array)$request->getPost("dirs");
  71. $dirsTypes = (array)$request->getPost("dirsTypes");
  72. if (!$mailboxId || (empty($dirs) && empty($dirsTypes)))
  73. {
  74. $this->errorCollection[] = new Error(Loc::getMessage('MAIL_CLIENT_FORM_ERROR'));
  75. return false;
  76. }
  77. global $USER;
  78. if (!Mail\Helper\Message::isMailboxOwner($mailboxId, $USER->GetID()))
  79. {
  80. $this->errorCollection[] = new Error('access denied');
  81. return false;
  82. }
  83. $mailboxDirsHelper = new MailboxDirectoryHelper($mailboxId);
  84. $mailboxDirsHelper->toggleSyncDirs($dirs);
  85. $mailboxDirsHelper->saveDirsTypes($dirsTypes);
  86. return [];
  87. }
  88. public function levelAction()
  89. {
  90. $request = Context::getCurrent()->getRequest();
  91. $mailboxId = (int)$request->getPost("mailboxId");
  92. $dir = (array)$request->getPost("dir");
  93. if (!$mailboxId || empty($dir) || empty($dir['dirMd5']))
  94. {
  95. $this->errorCollection[] = new Error(Loc::getMessage('MAIL_CLIENT_FORM_ERROR'));
  96. return false;
  97. }
  98. global $USER;
  99. if (!Mail\Helper\Message::isMailboxOwner($mailboxId, $USER->GetID()))
  100. {
  101. $this->errorCollection[] = new Error('access denied');
  102. return false;
  103. }
  104. $parent = MailboxDirectory::fetchOneByMailboxIdAndHash($mailboxId, $dir['dirMd5']);
  105. if ($parent == null)
  106. {
  107. $this->errorCollection[] = new Error(Loc::getMessage('MAIL_CLIENT_MAILBOX_NOT_FOUND'));
  108. return false;
  109. }
  110. if ($parent->getLevel() >= MailboxDirectoryHelper::getMaxLevelDirs())
  111. {
  112. $this->errorCollection[] = new Error(Loc::getMessage('MAIL_CLIENT_CONFIG_DIRS_MAX_LEVEL_DIRS'));
  113. return false;
  114. }
  115. $mailboxDirsHelper = new MailboxDirectoryHelper($mailboxId);
  116. if (!$mailboxDirsHelper->syncChildren($parent))
  117. {
  118. $this->errorCollection = $mailboxDirsHelper->getErrors();
  119. return false;
  120. }
  121. $dirs = $mailboxDirsHelper->getAllLevelByParentId($parent);
  122. $mailboxDirsHelper->setDirs($dirs);
  123. $this->arResult['DIRS'] = $mailboxDirsHelper->buildTreeDirs();
  124. $this->arResult['MAX_LEVEL'] = 1;
  125. ob_start();
  126. $this->includeComponentTemplate('dirs');
  127. return ['dirs' => $this->arResult['DIRS'], 'html' => ob_get_clean()];
  128. }
  129. /**
  130. * Getting array of errors.
  131. * @return Error[]
  132. */
  133. public function getErrors()
  134. {
  135. return $this->errorCollection->toArray();
  136. }
  137. /**
  138. * Getting once error with the necessary code.
  139. * @param string $code Code of error.
  140. * @return Error
  141. */
  142. public function getErrorByCode($code)
  143. {
  144. return $this->errorCollection->getErrorByCode($code);
  145. }
  146. }