-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedrType.php
More file actions
184 lines (162 loc) · 4.47 KB
/
Copy pathEmbedrType.php
File metadata and controls
184 lines (162 loc) · 4.47 KB
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
<?php namespace ProcessWire;
/**
* Embedr Type
*
* Represents a type of embed block (product, article, brand, etc.)
*
* @property int $id
* @property string $name
* @property string $title
* @property string $template
* @property string $icon
* @property int $sort
*/
class EmbedrType extends WireData {
/**
* Default values
*
* @var array
*/
protected $defaults = [
'id' => 0,
'name' => '',
'title' => '',
'template' => '',
'icon' => '',
'sort' => 0,
'config' => '',
'mode' => 'array',
];
/**
* Construct
*/
public function __construct() {
$this->setArray($this->defaults);
parent::__construct();
}
/**
* Set property
*
* @param string $key
* @param mixed $value
* @return self|WireData
*/
public function set($key, $value) {
// CRITICAL: Preserve ID - never allow existing ID to be overwritten with 0
if($key === 'id') {
$currentId = (int) $this->get('id');
$newId = (int) $value;
// If we have an existing ID and new value is 0, keep the existing ID
if($currentId > 0 && $newId === 0) {
return $this;
}
}
if(isset($this->defaults[$key]) && is_int($this->defaults[$key])) {
$value = (int) $value;
}
return parent::set($key, $value);
}
/**
* Get full template path
*
* @return string
*/
public function getTemplatePath() {
$config = $this->wire('config');
// Get components path from ProcessEmbedr module settings
// Use getModuleConfigData to avoid permission issues for guests
$componentsPath = 'components/'; // Default
try {
$moduleConfig = $this->wire('modules')->getModuleConfigData('ProcessEmbedr');
if(!empty($moduleConfig['componentsPath'])) {
$componentsPath = $moduleConfig['componentsPath'];
}
} catch(\Exception $e) {
// Config not accessible - use default
}
return $config->paths->templates . $componentsPath . $this->template;
}
/**
* Check if template file exists
*
* @return bool
*/
public function templateExists() {
if(empty($this->template)) {
return false;
}
return file_exists($this->getTemplatePath());
}
/**
* Validate type data
*
* @return bool|string Returns true if valid, error message if not
*/
public function validate() {
if(empty($this->name)) {
return 'Name is required';
}
if(!preg_match('/^[a-z][a-z0-9_-]*$/i', $this->name)) {
return 'Name must start with a letter and contain only letters, numbers, hyphens and underscores';
}
if(empty($this->title)) {
return 'Title is required';
}
// Template is now optional - can use visual builder instead
return true;
}
/**
* String representation
*
* @return string
*/
public function __toString() {
return $this->title ?: $this->name;
}
/**
* Get config as array
*
* @return array
*/
public function getConfig() {
if(empty($this->config)) {
return $this->getDefaultConfig();
}
$config = json_decode($this->config, true);
if(!is_array($config)) {
return $this->getDefaultConfig();
}
return array_merge($this->getDefaultConfig(), $config);
}
/**
* Set config from array
*
* @param array $config
* @return self
*/
public function setConfig(array $config) {
$this->config = json_encode($config, JSON_PRETTY_PRINT);
return $this;
}
/**
* Get default config structure
*
* @return array
*/
public function getDefaultConfig() {
return [
'layout' => 'grid',
'columns' => 6,
'fields' => [
'image' => 'images',
'title' => 'title',
'description' => 'summary',
'price' => '',
'meta' => '',
],
'link' => true,
'imageWidth' => 192,
'imageHeight' => 192,
];
}
}