1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Dotdigitalgroup\Email\Model\Config\Source\Settings;
class Addressbooks implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var \Dotdigitalgroup\Email\Helper\Data
*/
private $helper;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* Addressbooks constructor.
*
* @param \Magento\Framework\Registry $registry
* @param \Dotdigitalgroup\Email\Helper\Data $data
*/
public function __construct(
\Magento\Framework\Registry $registry,
\Dotdigitalgroup\Email\Helper\Data $data
) {
$this->registry = $registry;
$this->helper = $data;
}
/**
* Retrieve list of options.
*
* @return array
*/
public function toOptionArray()
{
$fields = [];
// Add a "Do Not Map" Option
$fields[] = ['value' => 0, 'label' => '-- Please Select --'];
$apiEnabled = $this->helper->isEnabled($this->helper->getWebsite());
if ($apiEnabled) {
$savedAddressbooks = $this->registry->registry('addressbooks');
if ($savedAddressbooks) {
$addressBooks = $savedAddressbooks;
} else {
$client = $this->helper->getWebsiteApiClient($this->helper->getWebsite());
//make an api call an register the addressbooks
$addressBooks = $client->getAddressBooks();
if ($addressBooks) {
$this->registry->unregister('addressbooks'); // additional measure
$this->registry->register('addressbooks', $addressBooks);
}
}
//set up fields with book id and label
foreach ($addressBooks as $book) {
if (isset($book->id)) {
$fields[] = [
'value' => (string)$book->id,
'label' => (string)$book->name,
];
}
}
}
return $fields;
}
}