Index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  11. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  12. use Magento\Customer\Controller\RegistryConstants;
  13. use Magento\Customer\Model\Address\Mapper;
  14. use Magento\Framework\Message\Error;
  15. use Magento\Framework\DataObjectFactory as ObjectFactory;
  16. use Magento\Framework\Api\DataObjectHelper;
  17. /**
  18. * Class Index
  19. *
  20. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. * @SuppressWarnings(PHPMD.TooManyFields)
  23. * @SuppressWarnings(PHPMD.NumberOfChildren)
  24. */
  25. abstract class Index extends \Magento\Backend\App\Action
  26. {
  27. /**
  28. * Authorization level of a basic admin session
  29. *
  30. * @see _isAllowed()
  31. */
  32. const ADMIN_RESOURCE = 'Magento_Customer::manage';
  33. /**
  34. * @var \Magento\Framework\Validator
  35. * @deprecated 101.0.0
  36. */
  37. protected $_validator;
  38. /**
  39. * Core registry
  40. *
  41. * @var \Magento\Framework\Registry
  42. */
  43. protected $_coreRegistry = null;
  44. /**
  45. * @var \Magento\Framework\App\Response\Http\FileFactory
  46. */
  47. protected $_fileFactory;
  48. /**
  49. * @var \Magento\Customer\Model\CustomerFactory
  50. * @deprecated 101.0.0
  51. */
  52. protected $_customerFactory = null;
  53. /**
  54. * @var \Magento\Customer\Model\AddressFactory
  55. * @deprecated 101.0.0
  56. */
  57. protected $_addressFactory = null;
  58. /**
  59. * @var \Magento\Newsletter\Model\SubscriberFactory
  60. */
  61. protected $_subscriberFactory;
  62. /**
  63. * @var \Magento\Customer\Model\Metadata\FormFactory
  64. */
  65. protected $_formFactory;
  66. /**
  67. * @var CustomerRepositoryInterface
  68. */
  69. protected $_customerRepository;
  70. /**
  71. * @var \Magento\Customer\Helper\View
  72. */
  73. protected $_viewHelper;
  74. /**
  75. * @var \Magento\Framework\Math\Random
  76. * @deprecated 101.0.0
  77. */
  78. protected $_random;
  79. /**
  80. * @var ObjectFactory
  81. */
  82. protected $_objectFactory;
  83. /**
  84. * @var \Magento\Framework\Api\ExtensibleDataObjectConverter
  85. * @deprecated 101.0.0
  86. */
  87. protected $_extensibleDataObjectConverter;
  88. /**
  89. * @var Mapper
  90. */
  91. protected $addressMapper;
  92. /**
  93. * @var AccountManagementInterface
  94. */
  95. protected $customerAccountManagement;
  96. /**
  97. * @var AddressRepositoryInterface
  98. */
  99. protected $addressRepository;
  100. /**
  101. * @var CustomerInterfaceFactory
  102. */
  103. protected $customerDataFactory;
  104. /**
  105. * @var AddressInterfaceFactory
  106. */
  107. protected $addressDataFactory;
  108. /**
  109. * @var \Magento\Customer\Model\Customer\Mapper
  110. */
  111. protected $customerMapper;
  112. /**
  113. * @var \Magento\Framework\Reflection\DataObjectProcessor
  114. * @deprecated 101.0.0
  115. */
  116. protected $dataObjectProcessor;
  117. /**
  118. * @var DataObjectHelper
  119. */
  120. protected $dataObjectHelper;
  121. /**
  122. * @var \Magento\Framework\View\LayoutFactory
  123. * @deprecated 101.0.0
  124. */
  125. protected $layoutFactory;
  126. /**
  127. * @var \Magento\Framework\View\Result\LayoutFactory
  128. */
  129. protected $resultLayoutFactory;
  130. /**
  131. * @var \Magento\Framework\View\Result\PageFactory
  132. */
  133. protected $resultPageFactory;
  134. /**
  135. * @var \Magento\Backend\Model\View\Result\ForwardFactory
  136. */
  137. protected $resultForwardFactory;
  138. /**
  139. * @var \Magento\Framework\Controller\Result\JsonFactory
  140. */
  141. protected $resultJsonFactory;
  142. /**
  143. * Constructor
  144. *
  145. * @param \Magento\Backend\App\Action\Context $context
  146. * @param \Magento\Framework\Registry $coreRegistry
  147. * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
  148. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  149. * @param \Magento\Customer\Model\AddressFactory $addressFactory
  150. * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
  151. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  152. * @param \Magento\Customer\Helper\View $viewHelper
  153. * @param \Magento\Framework\Math\Random $random
  154. * @param CustomerRepositoryInterface $customerRepository
  155. * @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
  156. * @param Mapper $addressMapper
  157. * @param AccountManagementInterface $customerAccountManagement
  158. * @param AddressRepositoryInterface $addressRepository
  159. * @param CustomerInterfaceFactory $customerDataFactory
  160. * @param AddressInterfaceFactory $addressDataFactory
  161. * @param \Magento\Customer\Model\Customer\Mapper $customerMapper
  162. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  163. * @param DataObjectHelper $dataObjectHelper
  164. * @param ObjectFactory $objectFactory
  165. * @param \Magento\Framework\View\LayoutFactory $layoutFactory
  166. * @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
  167. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  168. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  169. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  170. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  171. */
  172. public function __construct(
  173. \Magento\Backend\App\Action\Context $context,
  174. \Magento\Framework\Registry $coreRegistry,
  175. \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
  176. \Magento\Customer\Model\CustomerFactory $customerFactory,
  177. \Magento\Customer\Model\AddressFactory $addressFactory,
  178. \Magento\Customer\Model\Metadata\FormFactory $formFactory,
  179. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  180. \Magento\Customer\Helper\View $viewHelper,
  181. \Magento\Framework\Math\Random $random,
  182. CustomerRepositoryInterface $customerRepository,
  183. \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
  184. Mapper $addressMapper,
  185. AccountManagementInterface $customerAccountManagement,
  186. AddressRepositoryInterface $addressRepository,
  187. CustomerInterfaceFactory $customerDataFactory,
  188. AddressInterfaceFactory $addressDataFactory,
  189. \Magento\Customer\Model\Customer\Mapper $customerMapper,
  190. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  191. DataObjectHelper $dataObjectHelper,
  192. ObjectFactory $objectFactory,
  193. \Magento\Framework\View\LayoutFactory $layoutFactory,
  194. \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
  195. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  196. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  197. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  198. ) {
  199. $this->_coreRegistry = $coreRegistry;
  200. $this->_fileFactory = $fileFactory;
  201. $this->_customerFactory = $customerFactory;
  202. $this->_addressFactory = $addressFactory;
  203. $this->_formFactory = $formFactory;
  204. $this->_subscriberFactory = $subscriberFactory;
  205. $this->_viewHelper = $viewHelper;
  206. $this->_random = $random;
  207. $this->_customerRepository = $customerRepository;
  208. $this->_extensibleDataObjectConverter = $extensibleDataObjectConverter;
  209. $this->addressMapper = $addressMapper;
  210. $this->customerAccountManagement = $customerAccountManagement;
  211. $this->addressRepository = $addressRepository;
  212. $this->customerDataFactory = $customerDataFactory;
  213. $this->addressDataFactory = $addressDataFactory;
  214. $this->customerMapper = $customerMapper;
  215. $this->dataObjectProcessor = $dataObjectProcessor;
  216. $this->_objectFactory = $objectFactory;
  217. $this->dataObjectHelper = $dataObjectHelper;
  218. $this->layoutFactory = $layoutFactory;
  219. $this->resultLayoutFactory = $resultLayoutFactory;
  220. $this->resultPageFactory = $resultPageFactory;
  221. $this->resultForwardFactory = $resultForwardFactory;
  222. $this->resultJsonFactory = $resultJsonFactory;
  223. parent::__construct($context);
  224. }
  225. /**
  226. * Customer initialization
  227. *
  228. * @return string customer id
  229. */
  230. protected function initCurrentCustomer()
  231. {
  232. $customerId = (int)$this->getRequest()->getParam('id');
  233. if ($customerId) {
  234. $this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
  235. }
  236. return $customerId;
  237. }
  238. /**
  239. * Prepare customer default title
  240. *
  241. * @param \Magento\Backend\Model\View\Result\Page $resultPage
  242. * @return void
  243. */
  244. protected function prepareDefaultCustomerTitle(\Magento\Backend\Model\View\Result\Page $resultPage)
  245. {
  246. $resultPage->getConfig()->getTitle()->prepend(__('Customers'));
  247. }
  248. /**
  249. * Add errors messages to session.
  250. *
  251. * @param array|string $messages
  252. * @return void
  253. *
  254. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  255. */
  256. protected function _addSessionErrorMessages($messages)
  257. {
  258. $messages = (array)$messages;
  259. $session = $this->_getSession();
  260. $callback = function ($error) use ($session) {
  261. if (!$error instanceof Error) {
  262. $error = new Error($error);
  263. }
  264. $this->messageManager->addMessage($error);
  265. };
  266. array_walk_recursive($messages, $callback);
  267. }
  268. /**
  269. * Helper function that handles mass actions by taking in a callable for handling a single customer action.
  270. *
  271. * @param callable $singleAction A single action callable that takes a customer ID as input
  272. * @param int[] $customerIds Array of customer Ids to perform the action upon
  273. * @return int Number of customers successfully acted upon
  274. * @deprecated 101.0.0
  275. */
  276. protected function actUponMultipleCustomers(callable $singleAction, $customerIds)
  277. {
  278. if (!is_array($customerIds)) {
  279. $this->messageManager->addError(__('Please select customer(s).'));
  280. return 0;
  281. }
  282. $customersUpdated = 0;
  283. foreach ($customerIds as $customerId) {
  284. try {
  285. $singleAction($customerId);
  286. $customersUpdated++;
  287. } catch (\Exception $exception) {
  288. $this->messageManager->addError($exception->getMessage());
  289. }
  290. }
  291. return $customersUpdated;
  292. }
  293. }