CustomerSetup.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. * Customer resource setup model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Setup;
  9. use Magento\Eav\Model\Config;
  10. use Magento\Eav\Model\Entity\Setup\Context;
  11. use Magento\Eav\Setup\EavSetup;
  12. use Magento\Framework\App\CacheInterface;
  13. use Magento\Framework\Setup\ModuleDataSetupInterface;
  14. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
  15. /**
  16. * @codeCoverageIgnore
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class CustomerSetup extends EavSetup
  20. {
  21. /**
  22. * EAV configuration
  23. *
  24. * @var Config
  25. */
  26. protected $eavConfig;
  27. /**
  28. * Init
  29. *
  30. * @param ModuleDataSetupInterface $setup
  31. * @param Context $context
  32. * @param CacheInterface $cache
  33. * @param CollectionFactory $attrGroupCollectionFactory
  34. * @param Config $eavConfig
  35. */
  36. public function __construct(
  37. ModuleDataSetupInterface $setup,
  38. Context $context,
  39. CacheInterface $cache,
  40. CollectionFactory $attrGroupCollectionFactory,
  41. Config $eavConfig
  42. ) {
  43. $this->eavConfig = $eavConfig;
  44. parent::__construct($setup, $context, $cache, $attrGroupCollectionFactory);
  45. }
  46. /**
  47. * Add customer attributes to customer forms
  48. *
  49. * @return void
  50. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  51. * @SuppressWarnings(PHPMD.NPathComplexity)
  52. */
  53. public function installCustomerForms()
  54. {
  55. $customer = (int)parent::getEntityTypeId('customer');
  56. $customerAddress = (int)parent::getEntityTypeId('customer_address');
  57. $attributeIds = [];
  58. $select = $this->getSetup()->getConnection()->select()->from(
  59. ['ea' => $this->getSetup()->getTable('eav_attribute')],
  60. ['entity_type_id', 'attribute_code', 'attribute_id']
  61. )->where(
  62. 'ea.entity_type_id IN(?)',
  63. [$customer, $customerAddress]
  64. );
  65. foreach ($this->getSetup()->getConnection()->fetchAll($select) as $row) {
  66. $attributeIds[$row['entity_type_id']][$row['attribute_code']] = $row['attribute_id'];
  67. }
  68. $data = [];
  69. $entities = $this->getDefaultEntities();
  70. $attributes = $entities['customer']['attributes'];
  71. foreach ($attributes as $attributeCode => $attribute) {
  72. $attributeId = $attributeIds[$customer][$attributeCode];
  73. $attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
  74. $attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
  75. if ($attribute['system'] != true || $attribute['visible'] != false) {
  76. $usedInForms = ['customer_account_create', 'customer_account_edit', 'checkout_register'];
  77. if (!empty($attribute['adminhtml_only'])) {
  78. $usedInForms = ['adminhtml_customer'];
  79. } else {
  80. $usedInForms[] = 'adminhtml_customer';
  81. }
  82. if (!empty($attribute['admin_checkout'])) {
  83. $usedInForms[] = 'adminhtml_checkout';
  84. }
  85. foreach ($usedInForms as $formCode) {
  86. $data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
  87. }
  88. }
  89. }
  90. $attributes = $entities['customer_address']['attributes'];
  91. foreach ($attributes as $attributeCode => $attribute) {
  92. $attributeId = $attributeIds[$customerAddress][$attributeCode];
  93. $attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
  94. $attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
  95. if (false === ($attribute['system'] == true && $attribute['visible'] == false)) {
  96. $usedInForms = [
  97. 'adminhtml_customer_address',
  98. 'customer_address_edit',
  99. 'customer_register_address',
  100. ];
  101. foreach ($usedInForms as $formCode) {
  102. $data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
  103. }
  104. }
  105. }
  106. if ($data) {
  107. $this->getSetup()->getConnection()
  108. ->insertMultiple($this->getSetup()->getTable('customer_form_attribute'), $data);
  109. }
  110. }
  111. /**
  112. * Retrieve default entities: customer, customer_address
  113. *
  114. * @return array
  115. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  116. */
  117. public function getDefaultEntities()
  118. {
  119. $entities = [
  120. 'customer' => [
  121. 'entity_type_id' => \Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
  122. 'entity_model' => \Magento\Customer\Model\ResourceModel\Customer::class,
  123. 'attribute_model' => \Magento\Customer\Model\Attribute::class,
  124. 'table' => 'customer_entity',
  125. 'increment_model' => \Magento\Eav\Model\Entity\Increment\NumericValue::class,
  126. 'additional_attribute_table' => 'customer_eav_attribute',
  127. 'entity_attribute_collection' => \Magento\Customer\Model\ResourceModel\Attribute\Collection::class,
  128. 'attributes' => [
  129. 'website_id' => [
  130. 'type' => 'static',
  131. 'label' => 'Associate to Website',
  132. 'input' => 'select',
  133. 'source' => \Magento\Customer\Model\Customer\Attribute\Source\Website::class,
  134. 'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Website::class,
  135. 'sort_order' => 10,
  136. 'position' => 10,
  137. 'adminhtml_only' => 1,
  138. ],
  139. 'store_id' => [
  140. 'type' => 'static',
  141. 'label' => 'Create In',
  142. 'input' => 'select',
  143. 'source' => \Magento\Customer\Model\Customer\Attribute\Source\Store::class,
  144. 'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Store::class,
  145. 'sort_order' => 20,
  146. 'visible' => false,
  147. 'adminhtml_only' => 1,
  148. ],
  149. 'created_in' => [
  150. 'type' => 'static',
  151. 'label' => 'Created From',
  152. 'input' => 'text',
  153. 'required' => false,
  154. 'sort_order' => 20,
  155. 'position' => 20,
  156. 'adminhtml_only' => 1,
  157. ],
  158. 'prefix' => [
  159. 'type' => 'static',
  160. 'label' => 'Name Prefix',
  161. 'input' => 'text',
  162. 'required' => false,
  163. 'sort_order' => 30,
  164. 'visible' => false,
  165. 'system' => false,
  166. 'position' => 30,
  167. ],
  168. 'firstname' => [
  169. 'type' => 'static',
  170. 'label' => 'First Name',
  171. 'input' => 'text',
  172. 'sort_order' => 40,
  173. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  174. 'position' => 40,
  175. ],
  176. 'middlename' => [
  177. 'type' => 'static',
  178. 'label' => 'Middle Name/Initial',
  179. 'input' => 'text',
  180. 'required' => false,
  181. 'sort_order' => 50,
  182. 'visible' => false,
  183. 'system' => false,
  184. 'position' => 50,
  185. ],
  186. 'lastname' => [
  187. 'type' => 'static',
  188. 'label' => 'Last Name',
  189. 'input' => 'text',
  190. 'sort_order' => 60,
  191. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  192. 'position' => 60,
  193. ],
  194. 'suffix' => [
  195. 'type' => 'static',
  196. 'label' => 'Name Suffix',
  197. 'input' => 'text',
  198. 'required' => false,
  199. 'sort_order' => 70,
  200. 'visible' => false,
  201. 'system' => false,
  202. 'position' => 70,
  203. ],
  204. 'email' => [
  205. 'type' => 'static',
  206. 'label' => 'Email',
  207. 'input' => 'text',
  208. 'sort_order' => 80,
  209. 'validate_rules' => '{"input_validation":"email"}',
  210. 'position' => 80,
  211. 'admin_checkout' => 1,
  212. ],
  213. 'group_id' => [
  214. 'type' => 'static',
  215. 'label' => 'Group',
  216. 'input' => 'select',
  217. 'source' => \Magento\Customer\Model\Customer\Attribute\Source\Group::class,
  218. 'sort_order' => 25,
  219. 'position' => 25,
  220. 'adminhtml_only' => 1,
  221. 'admin_checkout' => 1,
  222. ],
  223. 'dob' => [
  224. 'type' => 'static',
  225. 'label' => 'Date of Birth',
  226. 'input' => 'date',
  227. 'frontend' => \Magento\Eav\Model\Entity\Attribute\Frontend\Datetime::class,
  228. 'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class,
  229. 'required' => false,
  230. 'sort_order' => 90,
  231. 'visible' => false,
  232. 'system' => false,
  233. 'input_filter' => 'date',
  234. 'validate_rules' => '{"input_validation":"date"}',
  235. 'position' => 90,
  236. 'admin_checkout' => 1,
  237. ],
  238. 'password_hash' => [
  239. 'type' => 'static',
  240. 'input' => 'hidden',
  241. 'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Password::class,
  242. 'required' => false,
  243. 'sort_order' => 81,
  244. 'visible' => false,
  245. ],
  246. 'rp_token' => [
  247. 'type' => 'static',
  248. 'input' => 'hidden',
  249. 'required' => false,
  250. 'sort_order' => 115,
  251. 'visible' => false,
  252. ],
  253. 'rp_token_created_at' => [
  254. 'type' => 'static',
  255. 'input' => 'date',
  256. 'validate_rules' => '{"input_validation":"date"}',
  257. 'required' => false,
  258. 'sort_order' => 120,
  259. 'visible' => false,
  260. ],
  261. 'default_billing' => [
  262. 'type' => 'static',
  263. 'label' => 'Default Billing Address',
  264. 'input' => 'text',
  265. 'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Billing::class,
  266. 'required' => false,
  267. 'sort_order' => 82,
  268. 'visible' => false,
  269. ],
  270. 'default_shipping' => [
  271. 'type' => 'static',
  272. 'label' => 'Default Shipping Address',
  273. 'input' => 'text',
  274. 'backend' => \Magento\Customer\Model\Customer\Attribute\Backend\Shipping::class,
  275. 'required' => false,
  276. 'sort_order' => 83,
  277. 'visible' => false,
  278. ],
  279. 'taxvat' => [
  280. 'type' => 'static',
  281. 'label' => 'Tax/VAT Number',
  282. 'input' => 'text',
  283. 'required' => false,
  284. 'sort_order' => 100,
  285. 'visible' => false,
  286. 'system' => false,
  287. 'validate_rules' => '{"max_text_length":255}',
  288. 'position' => 100,
  289. 'admin_checkout' => 1,
  290. ],
  291. 'confirmation' => [
  292. 'type' => 'static',
  293. 'label' => 'Is Confirmed',
  294. 'input' => 'text',
  295. 'required' => false,
  296. 'sort_order' => 85,
  297. 'visible' => false,
  298. ],
  299. 'created_at' => [
  300. 'type' => 'static',
  301. 'label' => 'Created At',
  302. 'input' => 'date',
  303. 'required' => false,
  304. 'sort_order' => 86,
  305. 'visible' => false,
  306. 'system' => false,
  307. ],
  308. 'gender' => [
  309. 'type' => 'static',
  310. 'label' => 'Gender',
  311. 'input' => 'select',
  312. 'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
  313. 'required' => false,
  314. 'sort_order' => 110,
  315. 'visible' => false,
  316. 'system' => false,
  317. 'validate_rules' => '[]',
  318. 'position' => 110,
  319. 'admin_checkout' => 1,
  320. 'option' => ['values' => ['Male', 'Female']],
  321. ],
  322. 'disable_auto_group_change' => [
  323. 'type' => 'static',
  324. 'label' => 'Disable Automatic Group Change Based on VAT ID',
  325. 'input' => 'boolean',
  326. 'backend' => \Magento\Customer\Model\Attribute\Backend\Data\Boolean::class,
  327. 'position' => 28,
  328. 'required' => false,
  329. 'adminhtml_only' => true
  330. ]
  331. ],
  332. ],
  333. 'customer_address' => [
  334. 'entity_type_id' => \Magento\Customer\Api\AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
  335. 'entity_model' => \Magento\Customer\Model\ResourceModel\Address::class,
  336. 'attribute_model' => \Magento\Customer\Model\Attribute::class,
  337. 'table' => 'customer_address_entity',
  338. 'additional_attribute_table' => 'customer_eav_attribute',
  339. 'entity_attribute_collection' =>
  340. \Magento\Customer\Model\ResourceModel\Address\Attribute\Collection::class,
  341. 'attributes' => [
  342. 'prefix' => [
  343. 'type' => 'static',
  344. 'label' => 'Name Prefix',
  345. 'input' => 'text',
  346. 'required' => false,
  347. 'sort_order' => 10,
  348. 'visible' => false,
  349. 'system' => false,
  350. 'position' => 10,
  351. ],
  352. 'firstname' => [
  353. 'type' => 'static',
  354. 'label' => 'First Name',
  355. 'input' => 'text',
  356. 'sort_order' => 20,
  357. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  358. 'position' => 20,
  359. ],
  360. 'middlename' => [
  361. 'type' => 'static',
  362. 'label' => 'Middle Name/Initial',
  363. 'input' => 'text',
  364. 'required' => false,
  365. 'sort_order' => 30,
  366. 'visible' => false,
  367. 'system' => false,
  368. 'position' => 30,
  369. ],
  370. 'lastname' => [
  371. 'type' => 'static',
  372. 'label' => 'Last Name',
  373. 'input' => 'text',
  374. 'sort_order' => 40,
  375. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  376. 'position' => 40,
  377. ],
  378. 'suffix' => [
  379. 'type' => 'static',
  380. 'label' => 'Name Suffix',
  381. 'input' => 'text',
  382. 'required' => false,
  383. 'sort_order' => 50,
  384. 'visible' => false,
  385. 'system' => false,
  386. 'position' => 50,
  387. ],
  388. 'company' => [
  389. 'type' => 'static',
  390. 'label' => 'Company',
  391. 'input' => 'text',
  392. 'required' => false,
  393. 'sort_order' => 60,
  394. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  395. 'position' => 60,
  396. ],
  397. 'street' => [
  398. 'type' => 'static',
  399. 'label' => 'Street Address',
  400. 'input' => 'multiline',
  401. 'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class,
  402. 'sort_order' => 70,
  403. 'multiline_count' => 2,
  404. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  405. 'position' => 70,
  406. ],
  407. 'city' => [
  408. 'type' => 'static',
  409. 'label' => 'City',
  410. 'input' => 'text',
  411. 'sort_order' => 80,
  412. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  413. 'position' => 80,
  414. ],
  415. 'country_id' => [
  416. 'type' => 'static',
  417. 'label' => 'Country',
  418. 'input' => 'select',
  419. 'source' => \Magento\Customer\Model\ResourceModel\Address\Attribute\Source\Country::class,
  420. 'sort_order' => 90,
  421. 'position' => 90,
  422. ],
  423. 'region' => [
  424. 'type' => 'static',
  425. 'label' => 'State/Province',
  426. 'input' => 'text',
  427. 'backend' => \Magento\Customer\Model\ResourceModel\Address\Attribute\Backend\Region::class,
  428. 'required' => false,
  429. 'sort_order' => 100,
  430. 'position' => 100,
  431. ],
  432. 'region_id' => [
  433. 'type' => 'static',
  434. 'label' => 'State/Province',
  435. 'input' => 'hidden',
  436. 'source' => \Magento\Customer\Model\ResourceModel\Address\Attribute\Source\Region::class,
  437. 'required' => false,
  438. 'sort_order' => 100,
  439. 'position' => 100,
  440. ],
  441. 'postcode' => [
  442. 'type' => 'static',
  443. 'label' => 'Zip/Postal Code',
  444. 'input' => 'text',
  445. 'sort_order' => 110,
  446. 'validate_rules' => '[]',
  447. 'data' => \Magento\Customer\Model\Attribute\Data\Postcode::class,
  448. 'position' => 110,
  449. 'required' => false,
  450. ],
  451. 'telephone' => [
  452. 'type' => 'static',
  453. 'label' => 'Phone Number',
  454. 'input' => 'text',
  455. 'sort_order' => 120,
  456. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  457. 'position' => 120,
  458. ],
  459. 'fax' => [
  460. 'type' => 'static',
  461. 'label' => 'Fax',
  462. 'input' => 'text',
  463. 'required' => false,
  464. 'sort_order' => 130,
  465. 'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
  466. 'position' => 130,
  467. ],
  468. ],
  469. ],
  470. ];
  471. return $entities;
  472. }
  473. /**
  474. * Gets EAV configuration
  475. *
  476. * @return Config
  477. */
  478. public function getEavConfig()
  479. {
  480. return $this->eavConfig;
  481. }
  482. /**
  483. * Update attributes for customer.
  484. *
  485. * @param array $entityAttributes
  486. * @return void
  487. */
  488. public function upgradeAttributes(array $entityAttributes)
  489. {
  490. foreach ($entityAttributes as $entityType => $attributes) {
  491. foreach ($attributes as $attributeCode => $attributeData) {
  492. $attribute = $this->getEavConfig()->getAttribute($entityType, $attributeCode);
  493. foreach ($attributeData as $key => $value) {
  494. $attribute->setData($key, $value);
  495. }
  496. $attribute->save();
  497. }
  498. }
  499. }
  500. }