custom/plugins/LeadManager/src/LeadManager.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Virgin\LeadManager;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. class LeadManager extends Plugin
  12. {
  13. public function install(InstallContext $installContext): void
  14. {
  15. parent::install($installContext); // TODO: Change the autogenerated stub
  16. $newFields =
  17. [
  18. [
  19. 'name' => 'lead_implicit_registration',
  20. 'type' => CustomFieldTypes::BOOL,
  21. 'config' => [
  22. 'componentName' => 'sw-single-select',
  23. 'validation' => 'required',
  24. 'options' => [[
  25. 'label' => [
  26. 'en-GB' => 'implicit registration',
  27. ],
  28. 'value' => true, ],
  29. [
  30. 'label' => [
  31. 'en-GB' => 'explicit registration',
  32. ],
  33. 'value' => false,
  34. ],
  35. ],
  36. 'customFieldType' => 'select',
  37. 'customFieldPosition' => 1,
  38. 'label' => [
  39. 'en-GB' => 'Registration type',
  40. ],
  41. ],
  42. ],
  43. [
  44. 'name' => 'phone_number',
  45. 'type' => CustomFieldTypes::TEXT,
  46. 'config' => [
  47. 'componentName' => 'sw-field',
  48. 'type' => 'text',
  49. 'customFieldType' => 'text',
  50. 'customFieldPosition' => 2,
  51. 'label' => [
  52. 'en-GB' => 'Telephone number',
  53. ],
  54. ],
  55. ],
  56. [
  57. 'name' => 'virgin_guid',
  58. 'type' => CustomFieldTypes::TEXT,
  59. 'config' => [
  60. 'componentName' => 'sw-field',
  61. 'type' => 'text',
  62. 'customFieldType' => 'text',
  63. 'customFieldPosition' => 3,
  64. 'label' => [
  65. 'en-GB' => 'virgin guid',
  66. ],
  67. ],
  68. ],
  69. [
  70. 'name' => 'privacy_consent',
  71. 'type' => CustomFieldTypes::BOOL,
  72. 'config' => [
  73. 'componentName' => 'sw-single-select',
  74. 'validation' => 'required',
  75. 'options' => [[
  76. 'label' => [
  77. 'en-GB' => 'privacy si',
  78. ],
  79. 'value' => true, ],
  80. [
  81. 'label' => [
  82. 'en-GB' => 'privacy no',
  83. ],
  84. 'value' => false,
  85. ], [
  86. 'label' => [
  87. 'en-GB' => 'privacy null',
  88. ],
  89. 'value' => null,
  90. ],
  91. ],
  92. 'customFieldType' => 'select',
  93. 'customFieldPosition' => 4,
  94. 'label' => [
  95. 'en-GB' => 'privacy consent',
  96. ],
  97. ],
  98. ],
  99. [
  100. 'name' => 'zipcode',
  101. 'type' => CustomFieldTypes::TEXT,
  102. 'config' => [
  103. 'componentName' => 'sw-field',
  104. 'type' => 'text',
  105. 'customFieldType' => 'text',
  106. 'customFieldPosition' => 5,
  107. 'label' => [
  108. 'en-GB' => 'zipcode training',
  109. ],
  110. ],
  111. ],
  112. [
  113. 'name' => 'home_training',
  114. 'type' => CustomFieldTypes::BOOL,
  115. 'config' => [
  116. 'componentName' => 'sw-field',
  117. 'type' => 'checkbox',
  118. 'customFieldType' => 'checkbox',
  119. 'customFieldPosition' => 6,
  120. 'label' => [
  121. 'en-GB' => 'home training',
  122. ],
  123. ],
  124. ],
  125. [
  126. 'name' => 'balance',
  127. 'type' => CustomFieldTypes::FLOAT,
  128. 'config' => [
  129. 'componentName' => 'sw-field',
  130. 'type' => 'number',
  131. 'numberType'=> 'float',
  132. 'customFieldType' => 'text',
  133. 'customFieldPosition' => 7,
  134. 'label' => [
  135. 'en-GB' => 'balance',
  136. ],
  137. ],
  138. ],
  139. ];
  140. $this->addCustomerCustomFields($newFields);
  141. }
  142. public function update(UpdateContext $updateContext): void
  143. {
  144. parent::update($updateContext); // TODO: Change the autogenerated stub
  145. $newFields =
  146. [
  147. /*
  148. [
  149. 'name' => 'token_mc_cart',
  150. 'type' => CustomFieldTypes::TEXT,
  151. 'config' => [
  152. 'componentName' => 'sw-field',
  153. 'type' => 'text',
  154. 'customFieldType' => 'text',
  155. 'customFieldPosition' => 9,
  156. 'label' => [
  157. 'en-GB' => 'Token MC cart',
  158. ],
  159. ],
  160. ],
  161. [
  162. 'name' => 'test_test',
  163. 'type' => CustomFieldTypes::TEXT,
  164. 'config' => [
  165. 'componentName' => 'sw-field',
  166. 'type' => 'text',
  167. 'customFieldType' => 'text',
  168. 'customFieldPosition' => 11,
  169. 'label' => [
  170. 'en-GB' => 'Test test',
  171. ],
  172. ],
  173. ], */
  174. /*[
  175. 'name' => 'balance',
  176. 'type' => CustomFieldTypes::FLOAT,
  177. 'config' => [
  178. 'componentName' => 'sw-field',
  179. 'type' => 'number',
  180. 'numberType'=> 'float',
  181. 'customFieldType' => 'text',
  182. 'customFieldPosition' => 7,
  183. 'label' => [
  184. 'en-GB' => 'balance',
  185. ],
  186. ],
  187. ],
  188. [
  189. 'name' => 'mc_code_customer',
  190. 'type' => CustomFieldTypes::TEXT,
  191. 'config' => [
  192. 'componentName' => 'sw-field',
  193. 'type' => 'text',
  194. 'customFieldType' => 'text',
  195. 'customFieldPosition' => 8,
  196. 'label' => [
  197. 'en-GB' => 'mc code',
  198. ],
  199. ],
  200. ],
  201. [
  202. 'name' => 'id_richiesta',
  203. 'type' => CustomFieldTypes::TEXT,
  204. 'config' => [
  205. 'componentName' => 'sw-field',
  206. 'type' => 'text',
  207. 'customFieldType' => 'text',
  208. 'customFieldPosition' => 9,
  209. 'label' => [
  210. 'en-GB' => 'id richiesta',
  211. ],
  212. ],
  213. ],
  214. [
  215. 'name' => 'data_richiesta_mc',
  216. 'type' => CustomFieldTypes::DATETIME,
  217. 'config' => [
  218. 'componentName' => 'sw-field',
  219. 'time_24hr' => true,
  220. 'dateType' => 'datetime',
  221. 'customFieldType' => 'date',
  222. 'customFieldPosition' => 10,
  223. 'label' => [
  224. 'en-GB' => 'data richiesta mc',
  225. ],
  226. ],
  227. ],
  228. [
  229. 'name' => 'unsolved_under_evaluation',
  230. 'type' => CustomFieldTypes::BOOL,
  231. 'config' => [
  232. 'componentName' => 'sw-field',
  233. 'type' => 'checkbox',
  234. 'customFieldType' => 'checkbox',
  235. 'customFieldPosition' => 11,
  236. 'label' => [
  237. 'en-GB' => 'Insoluto check in corso',
  238. ],
  239. ],
  240. ],*/
  241. /*
  242. [
  243. 'name' => 'email',
  244. 'type' => CustomFieldTypes::TEXT,
  245. 'config' => [
  246. 'componentName' => 'sw-field',
  247. 'type' => 'text',
  248. 'customFieldType' => 'text',
  249. 'customFieldPosition' => 12,
  250. 'label' => [
  251. 'en-GB' => 'Partner User Email',
  252. ],
  253. ],
  254. ],
  255. [
  256. 'name' => 'userName',
  257. 'type' => CustomFieldTypes::TEXT,
  258. 'config' => [
  259. 'componentName' => 'sw-field',
  260. 'type' => 'text',
  261. 'customFieldType' => 'text',
  262. 'customFieldPosition' => 13,
  263. 'label' => [
  264. 'en-GB' => 'Partner User Firstname',
  265. ],
  266. ],
  267. ],
  268. [
  269. 'name' => 'surnameName',
  270. 'type' => CustomFieldTypes::TEXT,
  271. 'config' => [
  272. 'componentName' => 'sw-field',
  273. 'type' => 'text',
  274. 'customFieldType' => 'text',
  275. 'customFieldPosition' => 14,
  276. 'label' => [
  277. 'en-GB' => 'Partner User Lastname',
  278. ],
  279. ],
  280. ],
  281. [
  282. 'name' => 'fiscalCode',
  283. 'type' => CustomFieldTypes::TEXT,
  284. 'config' => [
  285. 'componentName' => 'sw-field',
  286. 'type' => 'text',
  287. 'customFieldType' => 'text',
  288. 'customFieldPosition' => 15,
  289. 'label' => [
  290. 'en-GB' => 'Partner User Fiscal Code',
  291. ],
  292. ],
  293. ],
  294. [
  295. 'name' => 'birthdate',
  296. 'type' => CustomFieldTypes::DATETIME,
  297. 'config' => [
  298. 'componentName' => 'sw-field',
  299. 'time_24hr' => false,
  300. 'dateType' => 'datetime',
  301. 'customFieldType' => 'date',
  302. 'customFieldPosition' => 16,
  303. 'label' => [
  304. 'en-GB' => 'Partner User Birth Date',
  305. ],
  306. ],
  307. ],
  308. [
  309. 'name' => 'residentAddress',
  310. 'type' => CustomFieldTypes::TEXT,
  311. 'config' => [
  312. 'componentName' => 'sw-field',
  313. 'dateType' => 'text',
  314. 'customFieldType' => 'text',
  315. 'customFieldPosition' => 17,
  316. 'label' => [
  317. 'en-GB' => 'Partner User Resident Address',
  318. ],
  319. ],
  320. ],
  321. */
  322. [
  323. 'name' => 'activationCode',
  324. 'type' => CustomFieldTypes::TEXT,
  325. 'config' => [
  326. 'componentName' => 'sw-field',
  327. 'dateType' => 'text',
  328. 'customFieldType' => 'text',
  329. 'customFieldPosition' => 18,
  330. 'label' => [
  331. 'en-GB' => 'Shopware Account Activation Code',
  332. ],
  333. ],
  334. ]
  335. ];
  336. $this->addCustomerCustomFields($newFields, 'lead', 'Lead Generation', 'customer');
  337. }
  338. private function addCustomerCustomFields($newProductCustomFields, $setName, $label, $entity)
  339. {
  340. /** @var EntityRepositoryInterface $customFieldSetRepository */
  341. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  342. $customFieldRepository = $this->container->get('custom_field.repository');
  343. $context = Context::createDefaultContext();
  344. foreach ($newProductCustomFields as $newProductCustomField) {
  345. $criteria = new Criteria();
  346. $criteria->addFilter(
  347. new EqualsFilter('custom_field.name', $newProductCustomField['name'])
  348. );
  349. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  350. if ($fieldId == null) {
  351. $createCustomFields[] = $newProductCustomField;
  352. }
  353. }
  354. $criteria = new Criteria();
  355. $criteria->addFilter(
  356. new EqualsFilter('name', $setName)
  357. );
  358. $customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
  359. if (!empty($createCustomFields)) {
  360. if (null == $customFieldSetId) {
  361. $customFieldSetRepository->create([
  362. [
  363. 'name' => $setName,
  364. 'customFields' => $createCustomFields,
  365. 'config' => [
  366. 'label' => [
  367. 'en-GB' => $label,
  368. ],
  369. ],
  370. 'relations' => [
  371. ['entityName' => $entity],
  372. ],
  373. ],
  374. ], $context);
  375. } else {
  376. $customFieldSetRepository->update(
  377. [
  378. [
  379. 'id' => $customFieldSetId,
  380. 'customFields' => $createCustomFields
  381. ]
  382. ], $context);
  383. }
  384. }
  385. }
  386. /*private function addCustomerCustomFields($newCustomerCustomFields)
  387. {
  388. $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  389. $customFieldRepository = $this->container->get('custom_field.repository');
  390. $context = Context::createDefaultContext();
  391. foreach ($newCustomerCustomFields as $newCustomerCustomField) {
  392. $criteria = new Criteria();
  393. $criteria->addFilter(
  394. new EqualsFilter('custom_field.name', $newCustomerCustomField['name'])
  395. );
  396. $fieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
  397. if ($fieldId == null) {
  398. $createCustomFields[] = $newCustomerCustomField;
  399. }
  400. }
  401. if (!empty($createCustomFields)) {
  402. $customFieldSetRepository->create([
  403. [
  404. 'name' => 'lead',
  405. 'customFields' => $createCustomFields,
  406. 'config' => [
  407. 'label' => [
  408. 'en-GB' => 'Lead Generation',
  409. ],
  410. ],
  411. 'relations' => [
  412. ['entityName' => 'customer'],
  413. ],
  414. ],
  415. ], $context);
  416. }
  417. }*/
  418. }