-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
178 lines (160 loc) · 4.78 KB
/
controller.php
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
<?php defined('C5_EXECUTE') or die(_('Access Denied.'));
/***************************************************************************************
* ___ __ __ _ __ __ ____ _ __
* / | / /_/ /______(_) /_ __ __/ /____ / __ \(_)________ / /___ ___ __
* / /| |/ __/ __/ ___/ / __ \/ / / / __/ _ \ / / / / / ___/ __ \/ / __ `/ / / /
* / ___ / /_/ /_/ / / / /_/ / /_/ / /_/ __/ / /_/ / (__ ) /_/ / / /_/ / /_/ /
* /_/ |_\__/\__/_/ /_/_.___/\__,_/\__/\___/ /_____/_/____/ .___/_/\__,_/\__, /
* /_/ /____/
* -------------------------------------------------------------------------------------
*
* Class AttributeDisplayPackage
* Package controller
*
* @package Attribute Display
* @version 0.0.1
* @author Andrew Householder <concrete5@aghouseh.com>
*/
class AttributeDisplayPackage extends Package {
/**
* Concrete5 required properties & methods
*/
protected $pkgHandle = 'attribute_display';
protected $appVersionRequired = '5.6.0';
protected $pkgVersion = '0.0.1';
public function getPackageDescription() {
return t('Provides a block & developer libraries for displaying attribute forms.');
}
public function getPackageName() {
return t('Attribute Display');
}
/**
* Package-wide attribute handle set as a property for DRY implementation
*/
public $instructions_attribute_handle = 'product_additional_message';
/**
* Config Keys
* key name => boolean flag if data should be encoded
*/
public $configurable_keys = array(
'ATTRIBUTE_DISPLAY_AVAILABLE_CATEGORIES_VIEW' => true,
'ATTRIBUTE_DISPLAY_AVAILABLE_CATEGORIES_EDIT' => true
);
/**
* Return a simplified list of categories
* @return Array
*/
public function getCategoriesArray() {
$text = Loader::helper('text');
$categories = array();
foreach (AttributeKeyCategory::getList() as $category) {
$handle = $category->getAttributeKeyCategoryHandle();
$categories[$handle] = $text->unhandle($handle);
}
return $categories;
}
/**
* Package Install
*
* @return [void]
*/
public function install($options = null) {
parent::install();
$this->_installConfig($options);
$this->_configure();
}
/**
* Package Upgrade method
*
* @return [void]
*/
public function upgrade() {
$this->_configure();
parent::upgrade();
}
/**
* Unified method to save our config data given an array (typically $_POST/$_REQUEST)
*
* @return [void]
*/
public function setConfigSettings($data) {
if (is_array($data)) {
$pkg = Package::getByHandle($this->pkgHandle);
$json = Loader::helper('json');
foreach ($this->configurable_keys as $key => $is_encoded) {
$pkg->saveConfig($key, ($is_encoded) ? $json->encode($data[$key]) : $data[$key]);
}
}
}
/**
* Retrieve our current settings array
*
* @return [Array]
*/
public function getConfigSettings() {
$pkg = Package::getByHandle($this->pkgHandle);
$json = Loader::helper('json');
$config_settings = array();
foreach ($this->configurable_keys as $key => $is_encoded) {
$datum = $pkg->config($key);
$config_settings[$key] = ($is_encoded) ? $json->decode($datum) : $datum;
}
return $config_settings;
}
/**
* Our configure method that runs on install/upgrade to support our custom installations
*
* @return [void]
*/
protected function _configure() {
$this->_verifyBlockTypes(); // attributes
$this->_verifySinglePages(); // dashboard pages
}
/**
* This method verifies that our block is installed
*
* @return [void]
*/
protected function _verifyBlockTypes() {
$pkg = Package::getByHandle($this->pkgHandle);
if (!$block_type = BlockType::getByHandle($this->pkgHandle)) {
BlockType::installBlockTypeFromPackage($this->pkgHandle, $pkg);
}
}
/**
* Verifies installation of our dashboard single pages
*
* @return [void]
*/
protected function _verifySinglePages() {
$this->_verifySinglePage(
'dashboard/attribute_display',
t('Attribute Display'),
$this->getPackageDescription()
);
$this->_verifySinglePage(
'dashboard/attribute_display/categories',
t('Categories'),
t('Configure which categories are available for display or editing in the block.')
);
}
protected function _verifySinglePage($path, $name, $description) {
$pkg = Package::getByHandle($this->pkgHandle);
$single_page = Page::getByPath($path);
if (!$single_page instanceof Page || $single_page->isError()) {
$single_page = SinglePage::add($path, $pkg);
}
$single_page->update(array(
'cName' => $name,
'cDescription' => $description)
);
}
/**
* Setup our default configuration options within the package
*
* @return [void]
*/
protected function _installConfig($options) {
$this->setConfigSettings($options);
}
}