Jake Vanderwerf
2026-05-03 9bbeea742424837fb58207d88e10dbca0b2cae04
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace JVBase\managers\SEO\render\Traits\_Properties;
 
use JVBase\base\SchemaHelper;
use JVBase\inc\managers\SEO\render\Thing\Intangible\ItemList\OfferCatalog;
use JVBase\managers\SEO\render\Thing\Intangible\Offer;
use JVBase\managers\SEO\render\Thing\Intangible\Service;
use JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\PriceSpecification;
use JVBase\managers\SEO\render\Thing\Product\Product;
use JVBase\meta\Meta;
use JVBase\registrar\Fields;
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
trait hasOfferCatalogTrait {
    /**
     * @var OfferCatalog Indicates an OfferCatalog listing for this Organization, Person, or Service.
     */
    protected OfferCatalog $hasOfferCatalog;
 
    public function getHasOfferCatalog():?OfferCatalog
    {
        return $this->hasOfferCatalog??null;
    }
    public function setHasOfferCatalog(OfferCatalog|array $hasOfferCatalog):void
    {
        if (is_array($hasOfferCatalog)){
            if (array_key_exists('generate', $hasOfferCatalog) && $hasOfferCatalog['generate']) {
                $this->generateOfferCatalog($hasOfferCatalog);
            } else {
                if (!array_key_exists('type', $hasOfferCatalog)) {
                    $hasOfferCatalog['type'] = 'JVBase\inc\managers\SEO\render\Thing\Intangible\ItemList\OfferCatalog';
                }
                $items = [];
                if (array_key_exists('items', $hasOfferCatalog)) {
                    $items = $hasOfferCatalog['items'];
                    unset($hasOfferCatalog['items']);
                }
                $hasOfferCatalog = SchemaHelper::classFromConfig($hasOfferCatalog);
                if (!empty($items)) {
                    $hasOfferCatalog->setItemListElement($items);
                }
            }
        }
        $this->hasOfferCatalog = $hasOfferCatalog;
    }
 
    public function getHasOfferCatalogFieldConfig():array
    {
        return [
            'type'  => 'group',
            'label' => 'Offer Catalog',
            'wrap'  => 'details',
            'fields'    => [
                'generate'  => [
                    'type'  => 'true_false',
                    'label' => 'Generate from Content type?',
                    'default'   => false,
                ],
                'content_type'  => [
                    'type'  => 'checkbox',
                    'label' => 'Select Type to generate from',
                    'options'   => Registrar::getRegistered(),
                    'condition' => [
                        'field' => 'generate',
                        'value' => 1,
                        'operator'  => '=='
                    ]
                ],
                'offer_catalog' => [
                    'type'  => 'repeater',
                    'label' => 'Manually Enter Offer Catalog',
                    'condition' => [
                        'field' => 'generate',
                        'value' => 1,
                        'operator'  => '!='
                    ],
                    'fields'    => [
                        'type'  => [
                            'type'  => 'select',
                            'label' => 'Type',
                            'options'   => [
                                'product'   => 'Product',
                                'service'   => 'Service'
                            ]
                        ],
                        'name'  => [
                            'type'  => 'text',
                            'label' => 'Name',
                        ],
                        'description'   => [
                            'type'  => 'textarea',
                            'label' => 'Description'
                        ],
                        'price' => [
                            'type'  => 'number',
                            'label' => 'Price'
                        ]
                    ]
                ]
            ]
        ];
    }
 
    public function setHasOfferCatalogField(Fields $fields):void
    {
        $fields->addField(
            'hasOfferCatalog',
            [
                'type'  => 'repeater',
                'label' => 'Main Products & Services',
                'fields'    => [
                    'type'  => [
                        'type'  => 'radio',
                        'label' => __('Select Type', 'jvb'),
                        'options'   => [
                            'product'   => 'Product',
                            'service'   => 'Service'
                        ],
                        'required' => true,
                    ],
                    'name'  => [
                        'type'  => 'text',
                        'label' => __('Name of Product or Service', 'jvb'),
                        'required'  => true,
                    ],
                    'description'   => [
                        'type'  => 'textarea',
                        'label' => __('Description (optional)', 'jvb'),
                    ],
                    'price' => [
                        'type'  => 'text',
                        'subtype'   => 'number',
                        'label'     => __('Price', 'jvb'),
                    ],
                    'unitText'  => [
                        'type'  => 'radio',
                        'label' => 'Price per unit',
                        'options'   => [
                            'hour'  => 'Hour',
                            'unit'  => 'Unit',
                        ],
                        'default'   => 'unit',
                    ]
                ],
            ]
        );
    }
 
    public function formatHasOfferCatalogField(Meta $meta):void
    {
        $catalog = $meta->get('hasOfferCatalog');
        if (!empty($catalog)) {
            $offerCatalog = [];
            $name = '';
            $services = array_filter($catalog, function ($item) {
                return $item['type'] === 'service';
            });
            $products =array_filter($catalog, function ($item) {
                return $item['type'] === 'product';
            });
            if (count($products) > 0) {
                $name = 'Products';
                if (count($services) > 0) {
                    $name .= ' & ';
                }
            }
            if (count($services) > 0) {
                $name .= 'Services';
            }
            foreach ($catalog as $row) {
                $offer = new Offer();
 
                $item = match($row['type']) {
                    'product'   => new Product(),
                    'service'   => new Service(),
                };
 
                $item->setName($row['name']);
                if (!empty($row['description'])) {
                    $item->setDescription($row['description']);
                }
                if (!empty($row['price'])) {
                    $price = new PriceSpecification();
                    $price->setPrice($row['price']);
                    $price->setPriceCurrency('CAD');
                    $price->setUnitText($row['unitText']);
                    $offer->setPriceSpecification($price);
                }
                $offer->setItemOffered($item);
                $offerCatalog[] =  $offer;
            }
            if (!empty(!$offerCatalog)) {
                $final = new OfferCatalog();
                $final->setName($name);
                $final->setItemListElement($offerCatalog);
                $this->sethasOfferCatalog($final);
            }
        }
    }
}