PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Backend/Modules/Mailmotor/Actions/MassAddressAction.php

http://github.com/forkcms/forkcms
PHP | 140 lines | 75 code | 19 blank | 46 comment | 10 complexity | 6b558e5a85a56e618f278d31788db3e8 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. namespace Backend\Modules\Mailmotor\Actions;
  3. /*
  4. * This file is part of Fork CMS.
  5. *
  6. * For the full copyright and license information, please view the license
  7. * file that was distributed with this source code.
  8. */
  9. use Backend\Core\Engine\Base\Action as BackendBaseAction;
  10. use Backend\Core\Engine\Model as BackendModel;
  11. use Backend\Modules\Mailmotor\Engine\Model as BackendMailmotorModel;
  12. use Backend\Modules\Mailmotor\Engine\CMHelper as BackendMailmotorCMHelper;
  13. /**
  14. * This action is used to update one or more e-mail addresses (delete, ...)
  15. */
  16. class MassAddressAction extends BackendBaseAction
  17. {
  18. /**
  19. * The passed e-mails
  20. *
  21. * @var array
  22. */
  23. private $emails;
  24. /**
  25. * The group ID we have to perform the actions for
  26. *
  27. * @var int
  28. */
  29. private $groupId;
  30. /**
  31. * Delete addresses
  32. */
  33. private function deleteAddresses()
  34. {
  35. // no group set
  36. if ($this->groupId == '') {
  37. $this->groupId = null;
  38. }
  39. // get all groups
  40. $groupIds = BackendMailmotorModel::getGroupIDs();
  41. // loop the emails
  42. foreach ($this->emails as $email) {
  43. // the group ID is not set
  44. if ($this->groupId == null) {
  45. // if no groups were set, break here
  46. if (empty($groupIds)) {
  47. break;
  48. }
  49. // loop the group IDs
  50. foreach ($groupIds as $groupId) {
  51. // try to unsubscribe this address
  52. try {
  53. BackendMailmotorCMHelper::unsubscribe($email, $groupId);
  54. } catch (\Exception $e) {
  55. // do nothing
  56. }
  57. }
  58. // delete all addresses
  59. BackendMailmotorModel::deleteAddresses($email);
  60. } else {
  61. // group ID was set, unsubscribe the address for this group
  62. BackendMailmotorCMHelper::unsubscribe($email, $this->groupId);
  63. }
  64. }
  65. // trigger event
  66. BackendModel::triggerEvent($this->getModule(), 'after_delete_addresses');
  67. // redirect
  68. $this->redirect(
  69. BackendModel::createURLForAction(
  70. 'Addresses'
  71. ) . '&report=delete-addresses' . (!empty($this->groupId) ? '&group_id=' . $this->groupId : '')
  72. );
  73. }
  74. /**
  75. * Execute the action
  76. */
  77. public function execute()
  78. {
  79. parent::execute();
  80. // action to execute
  81. $action = \SpoonFilter::getGetValue('action', array('delete', 'export'), '');
  82. $this->groupId = \SpoonFilter::getGetValue('group_id', null, '');
  83. // no id's provided
  84. if (!$action) {
  85. $this->redirect(BackendModel::createURLForAction('Addresses') . '&error=no-action-selected');
  86. }
  87. if (!isset($_GET['emails'])) {
  88. $this->redirect(
  89. BackendModel::createURLForAction('Addresses') . '&error=no-items-selected'
  90. );
  91. } else {
  92. // redefine id's
  93. $this->emails = (array) $_GET['emails'];
  94. // evaluate $action, see what action was triggered
  95. switch ($action) {
  96. case 'delete':
  97. $this->deleteAddresses();
  98. break;
  99. case 'export':
  100. $this->exportAddresses();
  101. break;
  102. }
  103. }
  104. }
  105. /**
  106. * Export addresses
  107. */
  108. private function exportAddresses()
  109. {
  110. // fetch the creationdate for the addresses
  111. foreach ($this->emails as &$email) {
  112. $address = BackendMailmotorModel::getAddress($email);
  113. $email = array(
  114. 'email' => $email,
  115. 'created_on' => strtotime($address['created_on']),
  116. );
  117. }
  118. // export the addresses
  119. BackendMailmotorModel::exportAddresses($this->emails);
  120. }
  121. }